From d2d2a0dda5a829819c6b8d312a05c134c52c7d6b Mon Sep 17 00:00:00 2001 From: hneemann Date: Wed, 28 Aug 2019 09:20:51 +0200 Subject: [PATCH] improved the SVG export --- .../digital/draw/graphics/GraphicSVG.java | 13 +++- .../digital/draw/graphics/SVGSettings.java | 3 + .../draw/graphics/TextFormatLaTeX.java | 12 +++- src/main/resources/lang/lang_de.xml | 2 + src/main/resources/lang/lang_en.xml | 2 + .../draw/graphics/GraphicSVGLaTeXTest.java | 4 +- .../digital/draw/graphics/SVGExportTest.java | 68 +++++++++++++++++++ src/test/resources/dig/export/labels.dig | 34 ++++++++++ src/test/resources/dig/export/labels2.dig | 33 +++++++++ 9 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 src/test/java/de/neemann/digital/draw/graphics/SVGExportTest.java create mode 100644 src/test/resources/dig/export/labels.dig create mode 100644 src/test/resources/dig/export/labels2.dig diff --git a/src/main/java/de/neemann/digital/draw/graphics/GraphicSVG.java b/src/main/java/de/neemann/digital/draw/graphics/GraphicSVG.java index 98837cce2..a092da7d2 100644 --- a/src/main/java/de/neemann/digital/draw/graphics/GraphicSVG.java +++ b/src/main/java/de/neemann/digital/draw/graphics/GraphicSVG.java @@ -32,10 +32,19 @@ public class GraphicSVG implements Graphic { * @param out the stream */ public GraphicSVG(OutputStream out) { + this(out, SVGSettings.getInstance().getAttributes()); + } + + /** + * Creates a new instance. + * + * @param out the stream + * @param a the attributes + */ + public GraphicSVG(OutputStream out, ElementAttributes a) { this(out, null, DEF_SCALE); - ElementAttributes a = SVGSettings.getInstance().getAttributes(); if (a.get(SVGSettings.LATEX)) - setTextStyle(new TextFormatLaTeX()); + setTextStyle(new TextFormatLaTeX(a.get(SVGSettings.PINS_IN_MATH_MODE))); if (a.get(SVGSettings.HIGH_CONTRAST)) setColorStyle(new ColorStyleHighContrast()); if (a.get(SVGSettings.SMALL_IO)) diff --git a/src/main/java/de/neemann/digital/draw/graphics/SVGSettings.java b/src/main/java/de/neemann/digital/draw/graphics/SVGSettings.java index fbc3afd8a..2e48aa56e 100644 --- a/src/main/java/de/neemann/digital/draw/graphics/SVGSettings.java +++ b/src/main/java/de/neemann/digital/draw/graphics/SVGSettings.java @@ -32,6 +32,8 @@ public final class SVGSettings extends SettingsBase { new Key<>("SVG_noShapeFilling", false); static final Key LATEX = new Key<>("SVG_LaTeX", false); + static final Key PINS_IN_MATH_MODE = + new Key<>("SVG_pinsInMathMode", false).setDependsOn(LATEX); private static final class SettingsHolder { static final SVGSettings INSTANCE = new SVGSettings(); @@ -53,6 +55,7 @@ public final class SVGSettings extends SettingsBase { private static List createKeyList() { ArrayList list = new ArrayList<>(); list.add(LATEX); + list.add(PINS_IN_MATH_MODE); list.add(HIDE_TEST); list.add(NO_SHAPE_FILLING); list.add(SMALL_IO); diff --git a/src/main/java/de/neemann/digital/draw/graphics/TextFormatLaTeX.java b/src/main/java/de/neemann/digital/draw/graphics/TextFormatLaTeX.java index f9cbeb2af..e5d48a88c 100644 --- a/src/main/java/de/neemann/digital/draw/graphics/TextFormatLaTeX.java +++ b/src/main/java/de/neemann/digital/draw/graphics/TextFormatLaTeX.java @@ -19,6 +19,16 @@ import java.util.ArrayList; */ public class TextFormatLaTeX implements GraphicSVG.TextStyle { private static final ArrayList FONT_SIZES = new ArrayList<>(); + private boolean pinStyleInMathMode; + + /** + * Creates a new instance. + * + * @param pinStyleInMathMode if true pin lables are set in math mode + */ + public TextFormatLaTeX(boolean pinStyleInMathMode) { + this.pinStyleInMathMode = pinStyleInMathMode; + } private static final class FontSize { @@ -65,7 +75,7 @@ public class TextFormatLaTeX implements GraphicSVG.TextStyle { public String format(String text, Style style) { try { Text t = new Parser(text).parse(); - if (style.getFontStyle() == Font.ITALIC) + if (style.getFontStyle() == Font.ITALIC || (style == Style.SHAPE_PIN && pinStyleInMathMode)) t = Decorate.math(t); text = LaTeXFormatter.format(t); } catch (ParseException e) { diff --git a/src/main/resources/lang/lang_de.xml b/src/main/resources/lang/lang_de.xml index e39aabb76..e8ec7c7af 100644 --- a/src/main/resources/lang/lang_de.xml +++ b/src/main/resources/lang/lang_de.xml @@ -1364,6 +1364,8 @@ Sind evtl. die Namen der Variablen nicht eindeutig? SVG Exporteinstellungen Text im LaTeX-Format Text wird in LaTeX-Notation eingefügt. Inkscape ist für die Weiterverabeitung erforderlich. + Pin-Labels im Math-Mode + Für Pin-Labels auch dann den Math-Mode verwenden, wenn keine Indizes enthalten sind. Testfälle verbergen Die Testfälle werden nicht mit exportiert. Polygone nicht ausfüllen diff --git a/src/main/resources/lang/lang_en.xml b/src/main/resources/lang/lang_en.xml index 28870f616..66eaa3590 100644 --- a/src/main/resources/lang/lang_en.xml +++ b/src/main/resources/lang/lang_en.xml @@ -1350,6 +1350,8 @@ SVG Export Settings Text in LaTeX notation Text is inserted in LaTeX notation. Inkscape is required for further processing. + Pin-Labels in Math Mode + For pin labels, use math mode even if no indexes are contained. Hide Test Cases The test cases are not exported. Shapes not filled diff --git a/src/test/java/de/neemann/digital/draw/graphics/GraphicSVGLaTeXTest.java b/src/test/java/de/neemann/digital/draw/graphics/GraphicSVGLaTeXTest.java index 45cf712de..5971b5062 100644 --- a/src/test/java/de/neemann/digital/draw/graphics/GraphicSVGLaTeXTest.java +++ b/src/test/java/de/neemann/digital/draw/graphics/GraphicSVGLaTeXTest.java @@ -13,7 +13,7 @@ import java.io.IOException; */ public class GraphicSVGLaTeXTest extends TestCase { public void testFormatText() throws Exception { - GraphicSVG.TextStyle gs = new TextFormatLaTeX(); + GraphicSVG.TextStyle gs = new TextFormatLaTeX(false); assertEquals("$Z_0$", gs.format("$Z_0$", Style.NORMAL)); assertEquals("$Z_{in}$", gs.format("$Z_{in}$", Style.NORMAL)); @@ -41,7 +41,7 @@ public class GraphicSVGLaTeXTest extends TestCase { } private void check(String orig, String LaTeX) throws IOException { - GraphicSVG.TextStyle gs = new TextFormatLaTeX(); + GraphicSVG.TextStyle gs = new TextFormatLaTeX(false); assertEquals(LaTeX, gs.format(orig, Style.NORMAL)); } } diff --git a/src/test/java/de/neemann/digital/draw/graphics/SVGExportTest.java b/src/test/java/de/neemann/digital/draw/graphics/SVGExportTest.java new file mode 100644 index 000000000..fbb92e224 --- /dev/null +++ b/src/test/java/de/neemann/digital/draw/graphics/SVGExportTest.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2019 Helmut Neemann + * Use of this source code is governed by the GPL v3 license + * that can be found in the LICENSE file. + */ +package de.neemann.digital.draw.graphics; + +import de.neemann.digital.core.NodeException; +import de.neemann.digital.core.element.ElementAttributes; +import de.neemann.digital.draw.elements.Circuit; +import de.neemann.digital.draw.elements.PinException; +import de.neemann.digital.draw.library.ElementNotFoundException; +import de.neemann.digital.integration.ToBreakRunner; +import junit.framework.TestCase; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +public class SVGExportTest extends TestCase { + + private static ByteArrayOutputStream export(String file, ExportFactory creator) throws NodeException, PinException, IOException, ElementNotFoundException { + Circuit circuit = new ToBreakRunner(file).getCircuit(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + new Export(circuit, creator).export(baos); + return baos; + } + + public void testSVGExportLabel() throws NodeException, PinException, IOException, ElementNotFoundException { + ElementAttributes attr = new ElementAttributes() + .set(SVGSettings.LATEX, true); + ByteArrayOutputStream baos + = export("dig/export/labels.dig", + (out) -> new GraphicSVG(out, attr)); + + String actual = new String(baos.toByteArray(), StandardCharsets.UTF_8); + assertTrue(actual.contains("$A$")); + assertTrue(actual.contains("$Y_n$")); + } + + public void testSVGExportLabel2() throws NodeException, PinException, IOException, ElementNotFoundException { + ElementAttributes attr = new ElementAttributes() + .set(SVGSettings.LATEX, true) + .set(SVGSettings.PINS_IN_MATH_MODE, false); + ByteArrayOutputStream baos + = export("dig/export/labels2.dig", + (out) -> new GraphicSVG(out, attr)); + + String actual = new String(baos.toByteArray(), StandardCharsets.UTF_8); + assertFalse(actual.contains("$A$")); + assertTrue(actual.contains("$Y_n$")); + } + + public void testSVGExportLabel3() throws NodeException, PinException, IOException, ElementNotFoundException { + ElementAttributes attr = new ElementAttributes() + .set(SVGSettings.LATEX, true) + .set(SVGSettings.PINS_IN_MATH_MODE, true); + ByteArrayOutputStream baos + = export("dig/export/labels2.dig", + (out) -> new GraphicSVG(out, attr)); + + String actual = new String(baos.toByteArray(), StandardCharsets.UTF_8); + assertTrue(actual.contains("$A$")); + assertTrue(actual.contains("$Y_n$")); + } + + +} \ No newline at end of file diff --git a/src/test/resources/dig/export/labels.dig b/src/test/resources/dig/export/labels.dig new file mode 100644 index 000000000..31a4ac683 --- /dev/null +++ b/src/test/resources/dig/export/labels.dig @@ -0,0 +1,34 @@ + + + 1 + + + + Out + + + Label + Y_n + + + + + + In + + + Label + A + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/dig/export/labels2.dig b/src/test/resources/dig/export/labels2.dig new file mode 100644 index 000000000..372db74de --- /dev/null +++ b/src/test/resources/dig/export/labels2.dig @@ -0,0 +1,33 @@ + + + 1 + + + + labels.dig + + + + + Out + + + + + In + + + + + + + + + + + + + + + + \ No newline at end of file