basic support for mm,cm and in units

This commit is contained in:
hneemann 2018-12-06 08:25:22 +01:00
parent a284b45b67
commit 92d1f0575b
2 changed files with 16 additions and 0 deletions

View File

@ -40,11 +40,16 @@ class Context {
private float strokeOpacity; private float strokeOpacity;
private float thickness; private float thickness;
private float fontSize; private float fontSize;
private float pixelPerMM;
private String textAnchor; private String textAnchor;
private boolean fillRuleEvenOdd; private boolean fillRuleEvenOdd;
private HashMap<String, String> classesMap; private HashMap<String, String> classesMap;
Context() { Context() {
this(72 / 25.4f * 4 / 3); // 72 DPI per default
}
Context(float pixelsPerMM) {
parent = null; parent = null;
tr = Transform.IDENTITY; tr = Transform.IDENTITY;
thickness = 1; thickness = 1;
@ -53,6 +58,7 @@ class Context {
fill = Color.BLACK; fill = Color.BLACK;
fillOpacity = 1; fillOpacity = 1;
strokeOpacity = 1; strokeOpacity = 1;
this.pixelPerMM = pixelsPerMM;
} }
private Context(Context parent) { private Context(Context parent) {
@ -66,6 +72,7 @@ class Context {
fontSize = parent.fontSize; fontSize = parent.fontSize;
textAnchor = parent.textAnchor; textAnchor = parent.textAnchor;
fillRuleEvenOdd = parent.fillRuleEvenOdd; fillRuleEvenOdd = parent.fillRuleEvenOdd;
pixelPerMM = parent.pixelPerMM;
} }
Context(Context parent, Element element) throws SvgException { Context(Context parent, Element element) throws SvgException {
@ -260,6 +267,12 @@ class Context {
return 16 * s; return 16 * s;
case "px": case "px":
return s; return s;
case "mm":
return s * c.pixelPerMM;
case "cm":
return 10 * s * c.pixelPerMM;
case "in":
return 25.4f * s * c.pixelPerMM;
default: default:
return c.getFontSize(); return c.getFontSize();
} }

View File

@ -43,6 +43,9 @@ public class ContextTest extends TestCase {
assertEquals(10, Context.readStyle(new Context(), "font-size:10px").getFontSize(), 1e-4); assertEquals(10, Context.readStyle(new Context(), "font-size:10px").getFontSize(), 1e-4);
assertEquals(20, Context.readStyle(new Context(), "font-size:10;font-size:2em").getFontSize(), 1e-4); assertEquals(20, Context.readStyle(new Context(), "font-size:10;font-size:2em").getFontSize(), 1e-4);
assertEquals(15, Context.readStyle(new Context(), "font-size:10;font-size:150%").getFontSize(), 1e-4); assertEquals(15, Context.readStyle(new Context(), "font-size:10;font-size:150%").getFontSize(), 1e-4);
assertEquals(96, Context.readStyle(new Context(), "font-size:25.4mm").getFontSize(), 1e-4);
assertEquals(96, Context.readStyle(new Context(), "font-size:2.54cm").getFontSize(), 1e-4);
assertEquals(96, Context.readStyle(new Context(), "font-size:1in").getFontSize(), 1e-4);
} }
} }