Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDFBOX-4073 Choosable Coordinate-Unitsystem #114

Open
wants to merge 24 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1674,4 +1674,31 @@ private List<Integer> applyGSUBRules(GsubWorker gsubWorker, ByteArrayOutputStrea
return glyphIdsAfterGsub;

}

/**
* Does a length unit conversion from millimeters or inches (depending on user choice) to points.
*
* @param items Items that needs to be converted.
* @param unit_type Specifies desired length unit (mm or inch) for conversion.
* @throws IllegalArgumentException If desired length unit is not supported.
*/
public static final void convertUnit(List<Float> items, String unitType)
{
float multiplier = 1;
switch (unitType) {
case "mm":
multiplier = 1 / (10 * 2.54f) * 72;
break;
case "inch":
multiplier = 72;
break;
default:
throw new IllegalArgumentException(
"could not find the unit type: " + unitType);
}

for (int i=0; i < items.size(); i++) {
items.set(i, items.get(i) * multiplier);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.apache.pdfbox.pdmodel;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.pdfbox.contentstream.operator.Operator;
import org.apache.pdfbox.contentstream.operator.OperatorName;
Expand Down Expand Up @@ -167,4 +169,29 @@ void testCloseContract() throws IOException
contentStream.close();
}
}

/**
* PDFBOX-4073: test implemented choosable coordinate-unitsystem
* Checks that unitconversion is done right.
* Arguably the test could be also in some other file
*/
@Test
void testUnitConversion()
{
List<Float> itemsMm = Arrays.asList(1.0f, 2.4f);
List<Float> itemsInch = Arrays.asList(1.0f, 2.4f);
List<Float> itemsInMm = Arrays.asList(1.0f /(10 * 2.54f) * 72, 2.4f /(10 * 2.54f) * 72);
List<Float> itemsInInch = Arrays.asList(1.0f * 72, 2.4f * 72);

PDAbstractContentStream.convertUnit(itemsMm, "mm");
PDAbstractContentStream.convertUnit(itemsInch, "inch");
// Should be converted to millimeters
assertEquals(itemsInMm, itemsMm);
// Should be converted to inches
assertEquals(itemsInInch, itemsInch);
// Should throw IllegalArgumentException if the unit is not valid
assertThrows(IllegalArgumentException.class, () -> {
PDAbstractContentStream.convertUnit(itemsMm, "invalid_unit");
});
}
}