Skip to content

Commit

Permalink
Tests: add a SVGGraphics2D test which uses JDK's built-in DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosame committed Jul 10, 2024
1 parent 9718846 commit 66b678d
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TextAttribute, Object> attributes = new Hashtable<>();
// Create a font with the desired attributes, including STRIKETHROUGH
Map<TextAttribute, Object> 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<TextAttribute, Object> attributes2 = new Hashtable<>(attributes);
// A similar font but with UNDERLINE instead of STRIKETHROUGH
Map<TextAttribute, Object> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -153,9 +153,10 @@ private void save(byte[] data) throws IOException {

/**
* Builds an <code>SVGGraphics2D</code> with a default configuration.
* @throws FontFormatException
*
* @return the <code>SVGGraphics2D</code>.
*/
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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();

Expand Down
Loading

0 comments on commit 66b678d

Please sign in to comment.