diff --git a/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/FontDecoration.java b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/FontDecoration.java index 6ffa285e4..7758e5b4c 100644 --- a/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/FontDecoration.java +++ b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/FontDecoration.java @@ -23,7 +23,7 @@ import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.font.TextAttribute; -import java.util.Hashtable; +import java.util.HashMap; import java.util.Map; import io.sf.carte.echosvg.test.TestFonts; @@ -39,36 +39,41 @@ public class FontDecoration implements Painter { @Override public void paint(Graphics2D g) { + // Set anti-aliasing g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + // Set a background color Color backgroundColor = new Color(0x08081a); g.setBackground(backgroundColor); // Set default font g.setFont(new Font(TestFonts.FONT_FAMILY_SANS1, Font.BOLD, 12)); - // Colors used for labels and test output - Color labelColor = new Color(0x666699); - Color fontColor = Color.black; - - // - Map attributes = new Hashtable<>(); + // Create a font with the desired attributes, including STRIKETHROUGH + Map attributes = new HashMap<>(); attributes.put(TextAttribute.FAMILY, TestFonts.FONT_FAMILY_SANS1); attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_EXTRABOLD); attributes.put(TextAttribute.SIZE, 20); attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON); - Font font = new Font(attributes); - g.setFont(font); - g.setPaint(labelColor); + Font fontST = new Font(attributes); - g.drawString("Strike Through", 10, 40); - g.setPaint(fontColor); - g.translate(0, 30); - Map attributes2 = new Hashtable<>(attributes); + // A similar font but with UNDERLINE instead of STRIKETHROUGH + Map attributes2 = new HashMap<>(attributes); attributes2.remove(TextAttribute.STRIKETHROUGH); attributes2.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); - font = new Font(attributes2); - g.setFont(font); + Font fontUL = new Font(attributes2); + + // Set the STRIKETHROUGH font and a color + g.setFont(fontST); + g.setPaint(new Color(0x666699)); + // Draw a string + g.drawString("Strike Through", 10, 40); + + // Now draw with a different color and the UNDERLINE font + g.setPaint(Color.black); + g.setFont(fontUL); + g.translate(0, 30); + // Draw a new string g.drawString("Underline", 10, 70); } diff --git a/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGAccuracyTest.java b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGAccuracyTest.java index c30075500..507c38b63 100644 --- a/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGAccuracyTest.java +++ b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGAccuracyTest.java @@ -22,21 +22,21 @@ import java.awt.Dimension; import java.awt.Font; -import java.awt.FontFormatException; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.URL; +import java.nio.charset.StandardCharsets; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; import io.sf.carte.echosvg.dom.GenericDOMImplementation; import io.sf.carte.echosvg.svggen.SVGGeneratorContext; -import io.sf.carte.echosvg.svggen.SVGGraphics2D; import io.sf.carte.echosvg.svggen.SVGGeneratorContext.GraphicContextDefaults; +import io.sf.carte.echosvg.svggen.SVGGraphics2D; import io.sf.carte.echosvg.test.TestFonts; import io.sf.carte.echosvg.test.TestUtil; import io.sf.carte.echosvg.util.SVGConstants; @@ -94,9 +94,9 @@ void setSaveSVG(File saveSVG) { * operation fails. * * @param expectError false if no error expected - * @throws FontFormatException + * @throws IOException If an I/O error occurs */ - void runTest(boolean expectError) throws IOException, FontFormatException { + void runTest(boolean expectError) throws IOException { SVGGraphics2D g2d = buildSVGGraphics2D(); g2d.setSVGCanvasSize(CANVAS_SIZE); @@ -105,7 +105,7 @@ void runTest(boolean expectError) throws IOException, FontFormatException { // Generate SVG content // ByteArrayOutputStream bos = new ByteArrayOutputStream(2048); - OutputStreamWriter osw = new OutputStreamWriter(bos, "UTF-8"); + OutputStreamWriter osw = new OutputStreamWriter(bos, StandardCharsets.UTF_8); painter.paint(g2d); configureSVGGraphics2D(g2d); g2d.stream(osw); @@ -153,9 +153,10 @@ private void save(byte[] data) throws IOException { /** * Builds an SVGGraphics2D with a default configuration. - * @throws FontFormatException + * + * @return the SVGGraphics2D. */ - protected SVGGraphics2D buildSVGGraphics2D() throws IOException, FontFormatException { + protected SVGGraphics2D buildSVGGraphics2D() { DOMImplementation impl = GenericDOMImplementation.getDOMImplementation(); String namespaceURI = SVGConstants.SVG_NAMESPACE_URI; Document domFactory = impl.createDocument(namespaceURI, SVGConstants.SVG_SVG_TAG, null); diff --git a/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGAccuracyTestValidator.java b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGAccuracyTestValidator.java index 625d32f33..11a6fbb12 100644 --- a/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGAccuracyTestValidator.java +++ b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGAccuracyTestValidator.java @@ -46,7 +46,7 @@ public class SVGAccuracyTestValidator { * @throws IOException */ @Test - public void testSVGAccuracyValidator() throws Exception { + public void testSVGAccuracyValidator() throws IOException { new PainterWithException().test(); new NullReferenceURL().test(); new InexistantReferenceURL().test(); @@ -62,7 +62,7 @@ public void paint(Graphics2D g) { g.fillRect(0, 0, 20, 20); } - public void test() throws Exception { + public void test() throws IOException { Painter painter = this; URL refURL = new URL("http", "dummyHost", "dummyFile.svg"); SVGAccuracyTest t = new SVGAccuracyTest(painter, refURL); @@ -87,7 +87,7 @@ public void paint(Graphics2D g) { static class NullReferenceURL extends ValidPainterTest { - public void test() throws Exception { + public void test() throws IOException { SVGAccuracyTest t = new SVGAccuracyTest(this, null); try { t.runTest(true); @@ -100,7 +100,7 @@ public void test() throws Exception { static class InexistantReferenceURL extends ValidPainterTest { - public void test() throws Exception { + public void test() throws IOException { SVGAccuracyTest t = new SVGAccuracyTest(this, new URL("http", "dummyHost", "dummyFile.svg")); try { t.runTest(true); @@ -113,7 +113,7 @@ public void test() throws Exception { static class DiffWithReferenceImage extends ValidPainterTest { - public void test() throws Exception { + public void test() throws IOException { File tmpFile = File.createTempFile("EmptySVGReference", null); tmpFile.deleteOnExit(); @@ -125,7 +125,7 @@ public void test() throws Exception { static class SameAsReferenceImage extends ValidPainterTest { - public void test() throws Exception { + public void test() throws IOException { File tmpFile = File.createTempFile("SVGReference", null); tmpFile.deleteOnExit(); diff --git a/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGGeneratorTest.java b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGGeneratorTest.java index 77819e580..968ad2969 100644 --- a/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGGeneratorTest.java +++ b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGGeneratorTest.java @@ -51,7 +51,7 @@ */ public class SVGGeneratorTest { - private static final String GENERATOR_REFERENCE_BASE; + static final String GENERATOR_REFERENCE_BASE; private static final String RENDERING_DIR = "rendering"; @@ -65,15 +65,15 @@ public class SVGGeneratorTest { private static final String PLATFORM_VARIATION_SUFFIX = io.sf.carte.echosvg.test.svg.PreconfiguredRenderingTest.PLATFORM_VARIATION_SUFFIX; - private static final String CANDIDATE_REF_DIR = "candidate-ref"; + static final String CANDIDATE_REF_DIR = "candidate-ref"; private static final String RENDERING_CANDIDATE_REF_DIR = "candidate-reference"; private static final String PNG_EXTENSION = ".png"; - private static final String SVG_EXTENSION = ".svg"; + static final String SVG_EXTENSION = ".svg"; - private static final String PLAIN_GENERATION_PREFIX = ""; + static final String PLAIN_GENERATION_PREFIX = ""; private static final String CUSTOM_CONTEXT_GENERATION_PREFIX = "Context"; @@ -93,72 +93,72 @@ public static void beforeClass() throws FontFormatException, IOException { } @Test - public void testATransform() throws Exception { + public void testATransform() throws TranscoderException, IOException { runTests("ATransform"); } @Test - public void testAttributedCharacterIterator() throws Exception { + public void testAttributedCharacterIterator() throws TranscoderException, IOException { runTests("AttributedCharacterIterator"); } @Test - public void testBasicShapes() throws Exception { + public void testBasicShapes() throws TranscoderException, IOException { runTests("BasicShapes"); } @Test - public void testBasicShapes2() throws Exception { + public void testBasicShapes2() throws TranscoderException, IOException { runTests("BasicShapes2"); } @Test - public void testBStroke() throws Exception { + public void testBStroke() throws TranscoderException, IOException { runTests("BStroke"); } @Test - public void testBug4389() throws Exception { + public void testBug4389() throws TranscoderException, IOException { runTests("Bug4389"); } @Test - public void testBug4945() throws Exception { + public void testBug4945() throws TranscoderException, IOException { runTests("Bug4945"); } @Test - public void testBug6535() throws Exception { + public void testBug6535() throws TranscoderException, IOException { runTests("Bug6535"); } @Test - public void testBug17965() throws Exception { + public void testBug17965() throws TranscoderException, IOException { runTests("Bug17965"); } @Test - public void testClip() throws Exception { + public void testClip() throws TranscoderException, IOException { runTests("Clip"); } @Test - public void testColor1() throws Exception { + public void testColor1() throws TranscoderException, IOException { runTests("Color1"); } @Test - public void testColor2() throws Exception { + public void testColor2() throws TranscoderException, IOException { runTests("Color2"); } @Test - public void testDrawImage() throws Exception { + public void testDrawImage() throws TranscoderException, IOException { runTests("DrawImage"); } @Test - public void testFont1() throws Exception { + public void testFont1() throws TranscoderException, IOException { assumeTrue(isWindows(), "Test uses logical fonts, reference data is for Windows"); runTests("Font1"); } @@ -169,73 +169,73 @@ private static boolean isWindows() { } @Test - public void testFontDecoration() throws Exception { + public void testFontDecoration() throws TranscoderException, IOException { runTests("FontDecoration"); } @Test - public void testGVector() throws Exception { + public void testGVector() throws TranscoderException, IOException { runTests("GVector"); } @Test - public void testGradient() throws Exception { + public void testGradient() throws TranscoderException, IOException { runTests("Gradient"); } @Test - public void testGraphicObjects() throws Exception { + public void testGraphicObjects() throws TranscoderException, IOException { runTests("GraphicObjects"); } @Test - public void testIdentityTest() throws Exception { + public void testIdentityTest() throws TranscoderException, IOException { runTests("IdentityTest"); } @Test - public void testLookup() throws Exception { + public void testLookup() throws TranscoderException, IOException { runTests("Lookup"); } @Test - public void testNegativeLengths() throws Exception { + public void testNegativeLengths() throws TranscoderException, IOException { runTests("NegativeLengths"); } @Test - public void testPaints() throws Exception { + public void testPaints() throws TranscoderException, IOException { runTests("Paints"); } @Test - public void testRHints() throws Exception { + public void testRHints() throws TranscoderException, FontFormatException, IOException { TestFonts.registerFont(Font.TRUETYPE_FONT, "Anton-Regular.ttf"); runTests("RHints"); } @Test - public void testRescale() throws Exception { + public void testRescale() throws TranscoderException, IOException { runTests("Rescale"); } @Test - public void testShearTest() throws Exception { + public void testShearTest() throws TranscoderException, IOException { runTests("ShearTest"); } @Test - public void testTexture() throws Exception { + public void testTexture() throws TranscoderException, IOException { runTests("Texture"); } @Test - public void testTextSpacePreserve() throws Exception { + public void testTextSpacePreserve() throws TranscoderException, IOException { runTests("TextSpacePreserve"); } @Test - public void testTransformCollapse() throws Exception { + public void testTransformCollapse() throws TranscoderException, IOException { runTests("TransformCollapse"); } @@ -243,11 +243,10 @@ public void testTransformCollapse() throws Exception { * The id should be the Painter's class name prefixed with the package name * defined in getPackageName * - * @throws IOException + * @throws IOException If an I/O error occurs * @throws TranscoderException - * @throws FontFormatException */ - void runTests(String painterClassname) throws IOException, TranscoderException, FontFormatException { + void runTests(String painterClassname) throws IOException, TranscoderException { String clName = getClass().getPackage().getName() + "." + painterClassname; Class cl = null; @@ -357,13 +356,13 @@ private SVGAccuracyTest makeSVGAccuracyTest(Painter painter, String id) throws M return test; } - private String getNonQualifiedClassName(Painter painter) { + static String getNonQualifiedClassName(Painter painter) { String cl = painter.getClass().getName(); int n = cl.lastIndexOf('.'); return cl.substring(n + 1); } - private URL getReferenceURL(Painter painter, String prefix) throws MalformedURLException { + static URL getReferenceURL(Painter painter, String prefix) throws MalformedURLException { String suffix = prefix + getNonQualifiedClassName(painter) + SVG_EXTENSION; URL refUrl = new URL(GENERATOR_REFERENCE_BASE + ACCEPTED_REF_DIR + '/' + suffix); File acceptedReference = new File(refUrl.getFile()); diff --git a/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGGraphics2DDOMTest.java b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGGraphics2DDOMTest.java new file mode 100644 index 000000000..0355c189c --- /dev/null +++ b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGGraphics2DDOMTest.java @@ -0,0 +1,149 @@ +/* + + See the NOTICE file distributed with this work for additional + information regarding copyright ownership. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + */ +package io.sf.carte.echosvg.svggen.test; + +import java.awt.Font; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.junit.jupiter.api.Test; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.w3c.dom.Element; + +import io.sf.carte.echosvg.svggen.SVGGeneratorContext; +import io.sf.carte.echosvg.svggen.SVGGeneratorContext.GraphicContextDefaults; +import io.sf.carte.echosvg.svggen.SVGGraphics2D; +import io.sf.carte.echosvg.test.TestFonts; +import io.sf.carte.echosvg.util.SVGConstants; + +/** + * Test the SVGGraphics2D with the JDK built-in DOM implementation. + * + * @version $Id$ + */ +public class SVGGraphics2DDOMTest { + + @Test + public void testFontDecoration() throws IOException { + runTest("FontDecoration"); + } + + private void runTest(String painterClassname) throws IOException { + String clName = getClass().getPackage().getName() + '.' + painterClassname; + Class cl = null; + + try { + cl = Class.forName(clName); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(clName); + } + + Object o = null; + + try { + o = cl.getDeclaredConstructor().newInstance(); + } catch (Exception e) { + throw new IllegalArgumentException(clName); + } + + if (!(o instanceof Painter)) { + throw new IllegalArgumentException(clName); + } + + Painter painter = (Painter) o; + + SVGAccuracyTest acctest = makeSVGAccuracyTest(painter, painterClassname); + + acctest.runTest(false); + } + + private SVGAccuracyTest makeSVGAccuracyTest(Painter painter, String id) throws MalformedURLException { + String cl = SVGGeneratorTest.getNonQualifiedClassName(painter); + + SVGAccuracyTest test = new SVGAccuracyTest(painter, + SVGGeneratorTest.getReferenceURL(painter, SVGGeneratorTest.PLAIN_GENERATION_PREFIX)); + + String filename = new URL(SVGGeneratorTest.GENERATOR_REFERENCE_BASE + SVGGeneratorTest.CANDIDATE_REF_DIR + + '/' + cl + SVGGeneratorTest.SVG_EXTENSION).getFile(); + + test.setSaveSVG(new File(filename)); + + return test; + } + + static class DOMSVGGenAccuracy extends SVGAccuracyTest { + + public DOMSVGGenAccuracy(Painter painter, URL refURL) { + super(painter, refURL); + } + + /** + * Builds an SVGGraphics2D with a default configuration. + * + * @throws IllegalStateException if the parser could not be configured. + */ + @Override + protected SVGGraphics2D buildSVGGraphics2D() { + // We need a Document that holds an SVG root element. + // First obtain a DocumentBuilder as a way to get it. + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + + DocumentBuilder builder; + try { + builder = factory.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + throw new IllegalStateException(e); + } + + // Now the document which is what is needed + Document doc = builder.newDocument(); + + // Create a SVG DTD and the root element + DocumentType dtd = builder.getDOMImplementation().createDocumentType("svg", SVGConstants.SVG_PUBLIC_ID, + SVGConstants.SVG_SYSTEM_ID); + Element svgRoot = doc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_SVG_TAG); + // Append those to the document + doc.appendChild(dtd); + doc.appendChild(svgRoot); + + // For simplicity, create a generator context with some defaults + SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc); + + // Create the context defaults, with a default font just in case + GraphicContextDefaults defaults = new GraphicContextDefaults(); + defaults.setFont(new Font(TestFonts.FONT_FAMILY_SANS1, Font.PLAIN, 12)); + // Set the defaults + ctx.setGraphicContextDefaults(defaults); + // We want enough precision + ctx.setPrecision(12); + + return new SVGGraphics2D(ctx, false); + } + + } + +}