From 8bf63548b91e8cb3b5298d9b9311dfd5bfdb39d8 Mon Sep 17 00:00:00 2001 From: Eliot Kimber Date: Mon, 24 Sep 2018 12:42:09 -0500 Subject: [PATCH] Added support for PC (pica) measurements Corrected javadoc errors Updated to version 0.9.0 --- pom.xml | 2 +- .../org/wordinator/xml2docx/MakeDocx.java | 4 +- .../xml2docx/generator/DocxGenerator.java | 2 +- .../xml2docx/generator/Measurement.java | 89 +++++++++---------- .../wordinator/xml2docx/TestMeasurement.java | 37 +++++++- 5 files changed, 83 insertions(+), 51 deletions(-) diff --git a/pom.xml b/pom.xml index b80d986..1526656 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.wordinator wordinator jar - 0.8.0 + 0.9.0 wordinator.org http://wordinator.org diff --git a/src/main/java/org/wordinator/xml2docx/MakeDocx.java b/src/main/java/org/wordinator/xml2docx/MakeDocx.java index b00806a..dc859b8 100644 --- a/src/main/java/org/wordinator/xml2docx/MakeDocx.java +++ b/src/main/java/org/wordinator/xml2docx/MakeDocx.java @@ -69,7 +69,7 @@ public static void main( String[] args ) throws ParseException * @param options Command-line options * @param args Command-line arguments * @param log Logger to log messages to. - * @throws ParseException + * @throws ParseException Thrown if there is problem parsing the input */ public static void handleCommandLine( Options options, @@ -233,7 +233,7 @@ public static void transformXml( * @param inFile Single SWPX file * @param outFile If this is a directory, result filename is constructed from input filename. * @param templateDoc Template DOCX document used when constructing new document - * @param log + * @param log Log to put messages to. */ public static void handleSingleSwpxDoc(File inFile, File outFile, XWPFDocument templateDoc, Logger log) { diff --git a/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java b/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java index 062e989..439be04 100644 --- a/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java +++ b/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java @@ -85,7 +85,7 @@ public class DocxGenerator { * @param outFile File to write DOCX result to * @param templateDoc DOTX template to initialize result DOCX with (provides style definitions) * @throws Exception Exception from loading the template document - * @throws FileNotFoundException + * @throws FileNotFoundException If the template odcument is not found */ public DocxGenerator(File inFile, File outFile, XWPFDocument templateDoc) throws FileNotFoundException, Exception { this.inFile = inFile; diff --git a/src/main/java/org/wordinator/xml2docx/generator/Measurement.java b/src/main/java/org/wordinator/xml2docx/generator/Measurement.java index 065dfb8..53b7f3e 100644 --- a/src/main/java/org/wordinator/xml2docx/generator/Measurement.java +++ b/src/main/java/org/wordinator/xml2docx/generator/Measurement.java @@ -7,6 +7,7 @@ public class Measurement { public final static int POINTS_PER_INCH = 72; + private static final int POINTS_PER_PICA = 12; /** * Calculate the number of pixels represented by the specified value. @@ -23,22 +24,7 @@ public static double toPixels(String measurementValue, int dotsPerInch) throws M String numberStr = value.endsWith("px") ? value.substring(0, value.length() - 2) : value; result = Double.parseDouble(numberStr); } else { - String numberStr = value.substring(0, value.length() - 2); - double inches; - if (value.endsWith("pt")) { - double points = Double.parseDouble(numberStr); - inches = points / POINTS_PER_INCH; - } else if (value.endsWith("in")) { - inches = Double.parseDouble(numberStr); - } else if (value.endsWith("mm")) { - double mms = Double.parseDouble(numberStr); - inches = mms / 25.4; - } else if (value.endsWith("cm")) { - double cms = Double.parseDouble(numberStr); - inches = cms / 2.54; - } else { - throw new UnrecognizedUnitException("Unrecognized unit for measurement '" + measurementValue + "'"); - } + double inches = toInches(measurementValue, dotsPerInch); result = inches * dotsPerInch; } } catch (NumberFormatException e) { @@ -46,8 +32,7 @@ public static double toPixels(String measurementValue, int dotsPerInch) throws M } return result; - - + } /** @@ -58,39 +43,51 @@ public static double toPixels(String measurementValue, int dotsPerInch) throws M * @throws MeasurementException Bad measurement value */ public static double toPoints(String measurementValue, int dotsPerInch) throws MeasurementException { - String value = measurementValue.toLowerCase(); Double result = 0.0; - try { - String numberStr = value.substring(0, value.length() - 2); - double inches; - if (value.endsWith("pt")) { - double points = Double.parseDouble(numberStr); - inches = points / POINTS_PER_INCH; - } else if (value.endsWith("px")) { - Double pixels = Double.parseDouble(numberStr); - inches = pixels / dotsPerInch; - } else if ( value.matches("\\-?[0-9\\.]+")) { - Double pixels = Double.parseDouble(value); - inches = pixels / dotsPerInch; - } else if (value.endsWith("in")) { - inches = Double.parseDouble(numberStr); - } else if (value.endsWith("mm")) { - double mms = Double.parseDouble(numberStr); - inches = mms / 25.4; - } else if (value.endsWith("cm")) { - double cms = Double.parseDouble(numberStr); - inches = cms / 2.54; - } else { - throw new UnrecognizedUnitException("Unrecognized unit for measurement '" + measurementValue + "'"); - } - result = inches * POINTS_PER_INCH; - } catch (NumberFormatException e) { - throw new MeasurementException("Measurement '" + value + "' is not numeric."); - } + double inches = toInches(measurementValue, dotsPerInch); + result = inches * POINTS_PER_INCH; return result; } + /** + * Calculate the number of inches represented by the specified measurement value. + * @param measurementValue The measurement value with unit (e.g., "12pt", "1.3in") + * @param dotsPerInch The number of pixels (dots) per inch. + * @return Number of inchines + * @throws MeasurementException Bad measurement value + */ + public static double toInches(String measurementValue, int dotsPerInch) + throws MeasurementException { + String value = measurementValue.toLowerCase(); + String numberStr = value.substring(0, value.length() - 2); + double inches; + if (value.endsWith("pt")) { + double points = Double.parseDouble(numberStr); + inches = points / POINTS_PER_INCH; + } else if (value.endsWith("pc")) { + Double picas = Double.parseDouble(numberStr); + inches = (picas * POINTS_PER_PICA) / POINTS_PER_INCH; + } else if (value.endsWith("px")) { + Double pixels = Double.parseDouble(numberStr); + inches = pixels / dotsPerInch; + } else if ( value.matches("\\-?[0-9\\.]+")) { + Double pixels = Double.parseDouble(value); + inches = pixels / dotsPerInch; + } else if (value.endsWith("in")) { + inches = Double.parseDouble(numberStr); + } else if (value.endsWith("mm")) { + double mms = Double.parseDouble(numberStr); + inches = mms / 25.4; + } else if (value.endsWith("cm")) { + double cms = Double.parseDouble(numberStr); + inches = cms / 2.54; + } else { + throw new UnrecognizedUnitException("Unrecognized unit for measurement '" + measurementValue + "'"); + } + return inches; + } + /** * Calculate the number twips (1/20th of a point) represented by the measurement value. * @param measurementValue The measurement value with unit (e.g., "12pt", "1.3in") diff --git a/src/test/java/org/wordinator/xml2docx/TestMeasurement.java b/src/test/java/org/wordinator/xml2docx/TestMeasurement.java index 157d739..a3425f6 100644 --- a/src/test/java/org/wordinator/xml2docx/TestMeasurement.java +++ b/src/test/java/org/wordinator/xml2docx/TestMeasurement.java @@ -17,6 +17,7 @@ public void testToPixels() throws Throwable { String measurePixelsBare = "100"; // Pixels String measurePixelsPX = "100px"; // Pixels String measurePoints = "100pt"; // Points + String measurePicas = "10pc"; // Picas String measureInches = "10in"; // Inches String measureMM = "100mm"; // Millimeters String measureCM = "10cm"; // Centimeters @@ -27,6 +28,7 @@ public void testToPixels() throws Throwable { assertEquals("Bare pixel failed.", 100.0, Measurement.toPixels(measurePixelsBare, dotsPerInch )); assertEquals("PX unit failed.", 100.0, Measurement.toPixels(measurePixelsPX, dotsPerInch )); assertEquals("PT unit failed.", 100.0, Measurement.toPixels(measurePoints, dotsPerInch )); + assertEquals("PC unit failed.", 120.0, Measurement.toPixels(measurePicas, dotsPerInch )); assertEquals("IN unit failed.", 10.0 * dotsPerInch, Measurement.toPixels(measureInches, dotsPerInch )); assertEquals("MM unit failed.", "283.465", String.format("%.3f", Measurement.toPixels(measureMM, dotsPerInch )) @@ -47,6 +49,7 @@ public void testToPoints() throws Throwable { String measurePixelsBare = "100"; // Pixels String measurePixelsPX = "100px"; // Pixels String measurePoints = "100pt"; // Points + String measurePicas = "10pc"; // Picas String measureInches = "10in"; // Inches String measureMM = "100mm"; // Millimeters String measureCM = "10cm"; // Centimeters @@ -58,6 +61,7 @@ public void testToPoints() throws Throwable { Measurement.toPoints(measurePixelsBare, dotsPerInch )); assertEquals("PX unit failed.", 100.0, Measurement.toPoints(measurePixelsPX, dotsPerInch )); assertEquals("PT unit failed.", 100.0, Measurement.toPoints(measurePoints, dotsPerInch )); + assertEquals("PC unit failed.", 120.0, Measurement.toPoints(measurePicas, dotsPerInch ), 0.01); assertEquals("IN unit failed.", 10.0 * 72, Measurement.toPoints(measureInches, dotsPerInch )); assertEquals("MM unit failed.", "283.465", String.format("%.3f", Measurement.toPoints(measureMM, dotsPerInch )) @@ -73,11 +77,41 @@ public void testToPoints() throws Throwable { } } + @Test + public void testToInches() throws Throwable { + String measurePixelsBare = "100"; // Pixels + String measurePixelsPX = "100px"; // Pixels + String measurePoints = "100pt"; // Points + String measurePicas = "10pc"; // Picas + String measureInches = "10in"; // Inches + String measureMM = "100mm"; // Millimeters + String measureCM = "10cm"; // Centimeters + String measureBogus = "100bg"; // Not a real unit + + int dotsPerInch = 72; + + assertEquals("Bare pixel failed.", 1.38, + Measurement.toInches(measurePixelsBare, dotsPerInch ), 0.1); + assertEquals("PX unit failed.", 1.38, Measurement.toInches(measurePixelsPX, dotsPerInch ), 0.1); + assertEquals("PT unit failed.", 1.38, Measurement.toInches(measurePoints, dotsPerInch ), 0.1); + assertEquals("PC unit failed.", 10.0 / 6, Measurement.toInches(measurePicas, dotsPerInch ), 0.1); + assertEquals("IN unit failed.", 10.0, Measurement.toInches(measureInches, dotsPerInch ), 0.1); + assertEquals("MM unit failed.", 3.94, Measurement.toInches(measureMM, dotsPerInch ), 0.1); + assertEquals("CM unit failed.", 3.94, Measurement.toInches(measureCM, dotsPerInch ), 0.1); + try { + Measurement.toInches(measureBogus, dotsPerInch); + fail("Bogus measurement " + measureBogus + " did not cause exception."); + } catch (UnrecognizedUnitException e) { + // Expected exception. + } + } + @Test public void testToTwips() throws Throwable { String measurePixelsBare = "100"; // Pixels String measurePixelsPX = "100px"; // Pixels String measurePoints = "100pt"; // Points + String measurePicas = "10pc"; // Picas String measureInches = "10in"; // Inches String measureMM = "100mm"; // Millimeters String measureCM = "10cm"; // Centimeters @@ -89,7 +123,8 @@ public void testToTwips() throws Throwable { Measurement.toTwips(measurePixelsBare, dotsPerInch )); assertEquals("PX unit failed.", 100 * 20, Measurement.toTwips(measurePixelsPX, dotsPerInch )); assertEquals("PT unit failed.", 100 * 20, Measurement.toTwips(measurePoints, dotsPerInch )); - assertEquals("IN unit failed.", 10 * 20 * 72, Measurement.toTwips(measureInches, dotsPerInch )); + assertEquals("PC unit failed.", 10 * 12 * 20, Measurement.toTwips(measurePicas, dotsPerInch ), 0.1); + assertEquals("IN unit failed.", 10 * 72 * 20, Measurement.toTwips(measureInches, dotsPerInch )); assertEquals("MM unit failed.", 5660, Measurement.toTwips(measureMM, dotsPerInch)); assertEquals("CM unit failed.", 5660, Measurement.toTwips(measureCM, dotsPerInch)); try {