From e1bf7086ab2e6722acf0840cb0d50cd11c735144 Mon Sep 17 00:00:00 2001 From: Justin Kunimune Date: Tue, 2 Jan 2024 09:25:44 -1000 Subject: [PATCH] do not localize numbers in SVGs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SVGs only accept «0» as the zero character and «.» as the decimal separator. when I started aggressively using String.format(), it started localizing numbers to sometimes use ⋅ and ,, tho, which of course resulted in invalid SVGs. luckily when it *reads* doubles it seems to assume US locale. --- src/image/Path.java | 3 ++- src/image/SVGMap.java | 15 +++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/image/Path.java b/src/image/Path.java index a750015..e3f3166 100644 --- a/src/image/Path.java +++ b/src/image/Path.java @@ -13,6 +13,7 @@ import static java.lang.Math.max; import static java.lang.Math.sin; import static java.lang.Math.toDegrees; import static java.lang.String.format; +import static java.util.Locale.US; public class Path { /** @@ -242,7 +243,7 @@ public class Path { private static String formatDouble(double d) { //format a number with the minimum number of digits, but at most 3 - String str = format("%.3f", d); + String str = format(US, "%.3f", d); if (str.contains(".")) str = str.replaceFirst("0+$", ""); if (str.endsWith(".")) diff --git a/src/image/SVGMap.java b/src/image/SVGMap.java index 531a6f5..badff08 100644 --- a/src/image/SVGMap.java +++ b/src/image/SVGMap.java @@ -51,6 +51,7 @@ import static java.lang.Double.parseDouble; import static java.lang.Math.PI; import static java.lang.Math.hypot; import static java.lang.String.format; +import static java.util.Locale.US; import static utils.Math2.linInterp; import static utils.Math2.max; import static utils.Quantity.parseQuantity; @@ -166,7 +167,7 @@ public class SVGMap implements Iterable, SavableImage { // @Override /* only available in Java 14+ */ // public void declaration(String version, String encoding, String standalone) { -// elements.add(new Content(String.format( +// elements.add(new Content(format( // "\n", // version, encoding, standalone))); // } @@ -218,8 +219,10 @@ public class SVGMap implements Iterable, SavableImage { Quantity displayWidth = parseQuantity(attributes.getValue("width")); Quantity displayHeight = parseQuantity(attributes.getValue("height")); if (attributes.getValue("viewBox") == null) - attributes.addAttribute("", "", "viewBox", "", - format("%f %f %f %f", 0., 0., displayWidth.value, displayHeight.value)); + attributes.addAttribute( + "", "", "viewBox", "", + format(US, "%f %f %f %f", + 0., 0., displayWidth.value, displayHeight.value)); String[] values = attributes.getValue("viewBox").split("\\s", 4); double vbMinX = parseDouble(values[0]); double vbMinY = parseDouble(values[1]); @@ -590,7 +593,7 @@ public class SVGMap implements Iterable, SavableImage { } public String toString() { - return format(formatSpecifier, width.value, width.units, height.value, height.units, + return format(US, formatSpecifier, width.value, width.units, height.value, height.units, vbMinX, vbMinY, vbWidth, vbHeight); } } @@ -630,7 +633,7 @@ public class SVGMap implements Iterable, SavableImage { } public String toString() { - return format(formatSpecifier, x, y); + return format(US, formatSpecifier, x, y); } } @@ -648,7 +651,7 @@ public class SVGMap implements Iterable, SavableImage { } public String toString() { - return format(formatSpecifier, Path.toString(commands)); + return format(US, formatSpecifier, Path.toString(commands)); } } }