Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Added support for PC (pica) measurements Corrected javadoc errors Updated to version 0.9.0
  • Loading branch information
contrext committed Sep 24, 2018
2 parents b155797 + 8bf6354 commit 93d620d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 51 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>org.wordinator</groupId>
<artifactId>wordinator</artifactId>
<packaging>jar</packaging>
<version>0.8.0</version>
<version>0.9.0</version>
<organization>
<name>wordinator.org</name>
<url>http://wordinator.org</url>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/wordinator/xml2docx/MakeDocx.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
89 changes: 43 additions & 46 deletions src/main/java/org/wordinator/xml2docx/generator/Measurement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,31 +24,15 @@ 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) {
throw new MeasurementException("Measurement '" + value + "' is not numeric.");
}

return result;



}

/**
Expand All @@ -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")
Expand Down
37 changes: 36 additions & 1 deletion src/test/java/org/wordinator/xml2docx/TestMeasurement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ))
Expand All @@ -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
Expand All @@ -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 ))
Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 93d620d

Please sign in to comment.