|
1 | 1 | /*
|
2 | 2 |
|
3 |
| - Copyright (c) 2017-2021, Carlos Amengual. |
4 |
| -
|
5 |
| - SPDX-License-Identifier: BSD-3-Clause |
| 3 | + Copyright (c) 2017-2024, Carlos Amengual. |
6 | 4 |
|
7 | 5 | Licensed under a BSD-style License. You can find the license here:
|
8 | 6 | https://css4j.github.io/LICENSE.txt
|
9 | 7 |
|
10 | 8 | */
|
| 9 | +/* |
| 10 | + * SPDX-License-Identifier: BSD-3-Clause |
| 11 | + */ |
11 | 12 |
|
12 | 13 | package io.sf.carte.example;
|
13 | 14 |
|
14 | 15 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
15 | 16 |
|
| 17 | +import java.awt.Dimension; |
| 18 | +import java.awt.Font; |
16 | 19 | import java.io.ByteArrayOutputStream;
|
| 20 | +import java.io.CharArrayWriter; |
17 | 21 | import java.io.InputStream;
|
18 | 22 | import java.io.InputStreamReader;
|
19 | 23 | import java.io.Reader;
|
20 | 24 | import java.nio.charset.StandardCharsets;
|
21 | 25 |
|
| 26 | +import javax.xml.parsers.DocumentBuilder; |
| 27 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 28 | +import javax.xml.parsers.ParserConfigurationException; |
| 29 | + |
22 | 30 | import org.junit.jupiter.api.Test;
|
| 31 | +import org.w3c.dom.Document; |
| 32 | +import org.w3c.dom.DocumentType; |
| 33 | +import org.w3c.dom.Element; |
23 | 34 |
|
24 | 35 | import io.sf.carte.echosvg.anim.dom.SVGDOMImplementation;
|
25 | 36 | import io.sf.carte.echosvg.dom.util.SAXDocumentFactory;
|
| 37 | +import io.sf.carte.echosvg.svggen.SVGGeneratorContext; |
| 38 | +import io.sf.carte.echosvg.svggen.SVGGeneratorContext.GraphicContextDefaults; |
| 39 | +import io.sf.carte.echosvg.svggen.SVGGraphics2D; |
26 | 40 | import io.sf.carte.echosvg.transcoder.ErrorHandler;
|
27 | 41 | import io.sf.carte.echosvg.transcoder.TranscoderException;
|
28 | 42 | import io.sf.carte.echosvg.transcoder.TranscoderInput;
|
|
35 | 49 | */
|
36 | 50 | public class EchoSVGTest {
|
37 | 51 |
|
| 52 | + @Test |
| 53 | + public void testSVGGraphics2D() throws Exception { |
| 54 | + FontDecorationPainter painter = new FontDecorationPainter(); |
| 55 | + |
| 56 | + SVGGraphics2D g2d = createSVGGraphics2D(); |
| 57 | + |
| 58 | + // Set some appropriate dimension |
| 59 | + g2d.setSVGCanvasSize(new Dimension(300, 400)); |
| 60 | + |
| 61 | + painter.paint(g2d); |
| 62 | + |
| 63 | + CharArrayWriter wri = new CharArrayWriter(1900); |
| 64 | + g2d.stream(wri); |
| 65 | + |
| 66 | + int len = wri.toCharArray().length; |
| 67 | + assertTrue(len > 1800, "Char array had length of only " + len + "."); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates a <code>SVGGraphics2D</code> with certain defaults. |
| 72 | + * |
| 73 | + * @return the <code>SVGGraphics2D</code>. |
| 74 | + */ |
| 75 | + static SVGGraphics2D createSVGGraphics2D() { |
| 76 | + // We need a Document that holds an SVG root element. |
| 77 | + // First obtain a DocumentBuilder as a way to get it. |
| 78 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 79 | + factory.setNamespaceAware(true); |
| 80 | + |
| 81 | + DocumentBuilder builder; |
| 82 | + try { |
| 83 | + builder = factory.newDocumentBuilder(); |
| 84 | + } catch (ParserConfigurationException e) { |
| 85 | + throw new IllegalStateException(e); |
| 86 | + } |
| 87 | + |
| 88 | + // Now the document which is what is needed |
| 89 | + Document doc = builder.newDocument(); |
| 90 | + |
| 91 | + // Create a SVG DTD |
| 92 | + DocumentType dtd = builder.getDOMImplementation().createDocumentType("svg", |
| 93 | + "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"); |
| 94 | + // And the root element in the SVG namespace |
| 95 | + Element svgRoot = doc.createElementNS("http://www.w3.org/2000/svg", "svg"); |
| 96 | + |
| 97 | + // Append those to the document |
| 98 | + doc.appendChild(dtd); |
| 99 | + doc.appendChild(svgRoot); |
| 100 | + |
| 101 | + /* |
| 102 | + * Now the document is ready: let's create some context objects and then the |
| 103 | + * SVGGraphics2D. |
| 104 | + */ |
| 105 | + |
| 106 | + // For simplicity, create a generator context with some defaults |
| 107 | + SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc); |
| 108 | + // Set a comment to put in the SVG documents, overriding the default |
| 109 | + ctx.setComment("Generated by My Application"); |
| 110 | + |
| 111 | + // Set a compression a bit higher than the default |
| 112 | + ctx.setCompressionLevel(6); |
| 113 | + |
| 114 | + // Create the context defaults, with a default font just in case |
| 115 | + GraphicContextDefaults defaults = new GraphicContextDefaults(); |
| 116 | + defaults.setFont(new Font("Arial", Font.PLAIN, 12)); |
| 117 | + // Set the defaults |
| 118 | + ctx.setGraphicContextDefaults(defaults); |
| 119 | + |
| 120 | + return new SVGGraphics2D(ctx, false); |
| 121 | + } |
| 122 | + |
38 | 123 | @Test
|
39 | 124 | public void testTranscoding() throws Exception {
|
40 | 125 | org.w3c.dom.DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
|
41 | 126 | SAXDocumentFactory f = new SAXDocumentFactory(impl);
|
42 | 127 | Reader re = loadDocumentFromClasspath("/hdrbg.svg");
|
43 |
| - org.w3c.dom.Document document = f.createDocument("https://css4j.github.io/imag/hdrbg.svg", re); |
| 128 | + org.w3c.dom.Document document = f.createDocument("https://css4j.github.io/imag/hdrbg.svg", |
| 129 | + re); |
44 | 130 |
|
45 | 131 | PNGTranscoder trans = new PNGTranscoder();
|
46 | 132 | TranscoderInput input = new TranscoderInput(document);
|
47 | 133 | ByteArrayOutputStream ostream = new ByteArrayOutputStream(700);
|
48 | 134 | TranscoderOutput output = new TranscoderOutput(ostream);
|
49 | 135 | ErrorHandler handler = new ExceptionErrorHandler();
|
50 | 136 | trans.setErrorHandler(handler);
|
| 137 | + |
| 138 | + String[] text = { "Copyright", "Copyright © 2024 ACME Inc.", "Description", |
| 139 | + "Header background", "Software", "EchoSVG" }; |
| 140 | + |
| 141 | + String[] iText = { "Description", "en", "Description", "My image.", "Description", "es", |
| 142 | + "Descripción", "Mi imagen." }; |
| 143 | + |
| 144 | + trans.addTranscodingHint(PNGTranscoder.KEY_KEYWORD_TEXT, text); |
| 145 | + |
| 146 | + trans.addTranscodingHint(PNGTranscoder.KEY_COMPRESSED_TEXT, text); |
| 147 | + |
| 148 | + trans.addTranscodingHint(PNGTranscoder.KEY_INTERNATIONAL_TEXT, iText); |
| 149 | + |
| 150 | + trans.addTranscodingHint(PNGTranscoder.KEY_COMPRESSION_LEVEL, 4); |
| 151 | + |
51 | 152 | trans.transcode(input, output);
|
52 | 153 |
|
53 |
| - assertTrue(ostream.size() > 400); |
| 154 | + assertTrue(ostream.size() > 1070, "Image had size of only " + ostream.size() + "."); |
54 | 155 | }
|
55 | 156 |
|
56 | 157 | private Reader loadDocumentFromClasspath(String filename) {
|
|
0 commit comments