improved the SVG export

This commit is contained in:
hneemann 2019-08-28 09:20:51 +02:00
parent 775bdb7295
commit d2d2a0dda5
9 changed files with 166 additions and 5 deletions

View File

@ -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))

View File

@ -32,6 +32,8 @@ public final class SVGSettings extends SettingsBase {
new Key<>("SVG_noShapeFilling", false);
static final Key<Boolean> LATEX =
new Key<>("SVG_LaTeX", false);
static final Key<Boolean> 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<Key> createKeyList() {
ArrayList<Key> 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);

View File

@ -19,6 +19,16 @@ import java.util.ArrayList;
*/
public class TextFormatLaTeX implements GraphicSVG.TextStyle {
private static final ArrayList<FontSize> 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) {

View File

@ -1364,6 +1364,8 @@ Sind evtl. die Namen der Variablen nicht eindeutig?</string>
<string name="menu_exportSVGSettings">SVG Exporteinstellungen</string>
<string name="key_SVG_LaTeX">Text im LaTeX-Format</string>
<string name="key_SVG_LaTeX_tt">Text wird in LaTeX-Notation eingefügt. Inkscape ist für die Weiterverabeitung erforderlich.</string>
<string name="key_SVG_pinsInMathMode">Pin-Labels im Math-Mode</string>
<string name="key_SVG_pinsInMathMode_tt">Für Pin-Labels auch dann den Math-Mode verwenden, wenn keine Indizes enthalten sind.</string>
<string name="key_SVG_hideTest">Testfälle verbergen</string>
<string name="key_SVG_hideTest__">Die Testfälle werden nicht mit exportiert.</string>
<string name="key_SVG_noShapeFilling">Polygone nicht ausfüllen</string>

View File

@ -1350,6 +1350,8 @@
<string name="menu_exportSVGSettings">SVG Export Settings</string>
<string name="key_SVG_LaTeX">Text in LaTeX notation</string>
<string name="key_SVG_LaTeX_tt">Text is inserted in LaTeX notation. Inkscape is required for further processing.</string>
<string name="key_SVG_pinsInMathMode">Pin-Labels in Math Mode</string>
<string name="key_SVG_pinsInMathMode_tt">For pin labels, use math mode even if no indexes are contained.</string>
<string name="key_SVG_hideTest">Hide Test Cases</string>
<string name="key_SVG_hideTest__">The test cases are not exported.</string>
<string name="key_SVG_noShapeFilling">Shapes not filled</string>

View File

@ -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));
}
}

View File

@ -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$"));
}
}

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<circuit>
<version>1</version>
<attributes/>
<visualElements>
<visualElement>
<elementName>Out</elementName>
<elementAttributes>
<entry>
<string>Label</string>
<string>Y_n</string>
</entry>
</elementAttributes>
<pos x="460" y="340"/>
</visualElement>
<visualElement>
<elementName>In</elementName>
<elementAttributes>
<entry>
<string>Label</string>
<string>A</string>
</entry>
</elementAttributes>
<pos x="420" y="340"/>
</visualElement>
</visualElements>
<wires>
<wire>
<p1 x="420" y="340"/>
<p2 x="460" y="340"/>
</wire>
</wires>
<measurementOrdering/>
</circuit>

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<circuit>
<version>1</version>
<attributes/>
<visualElements>
<visualElement>
<elementName>labels.dig</elementName>
<elementAttributes/>
<pos x="440" y="200"/>
</visualElement>
<visualElement>
<elementName>Out</elementName>
<elementAttributes/>
<pos x="520" y="200"/>
</visualElement>
<visualElement>
<elementName>In</elementName>
<elementAttributes/>
<pos x="420" y="200"/>
</visualElement>
</visualElements>
<wires>
<wire>
<p1 x="420" y="200"/>
<p2 x="440" y="200"/>
</wire>
<wire>
<p1 x="500" y="200"/>
<p2 x="520" y="200"/>
</wire>
</wires>
<measurementOrdering/>
</circuit>