Commit bca1aa32 authored by Jos van den Oever's avatar Jos van den Oever

Normalize the fo:border* attributes.

parent 9633550c
......@@ -20,6 +20,8 @@ public class UnitHandler extends XMLFilterImpl {
final private Fixer nonNegativeLengthFixer = new NonNegativeLengthFixer();
final private Fixer borderFixer = new BorderFixer();
UnitHandler(OdfRngInfo info) {
// todo: point3D and clipShape
// length
......@@ -37,6 +39,11 @@ public class UnitHandler extends XMLFilterImpl {
info,
"([0-9]*[1-9][0-9]*(\\.[0-9]*)?|0+\\.[0-9]*[1-9][0-9]*|\\.[0-9]*[1-9][0-9]*)((cm)|(mm)|(in)|(pt)|(pc)|(px))",
nonNegativeLengthFixer);
add(NC.fo, "border-bottom", borderFixer);
add(NC.fo, "border-left", borderFixer);
add(NC.fo, "border-right", borderFixer);
add(NC.fo, "border-top", borderFixer);
add(NC.fo, "border", borderFixer);
}
private void addPattern(OdfRngInfo info, String pattern, Fixer fixer) {
......@@ -193,6 +200,37 @@ class LosslessNumberConversion {
format.setMaximumFractionDigits(fractionDigits + 2);
return format.format(value * 3 / 4) + "pt";
}
@Nullable
String convertPt(String string, int fractionDigits) {
matcher.reset(string);
if (!matcher.find()) {
return null;
}
double value = Float.parseFloat(matcher.group(1));
final String unit = matcher.group(3);
switch (unit) {
case "cm":
value = value * 72 / 2.54;
break;
case "mm":
value = value * 72 / 25.4;
break;
case "in":
value = value * 72;
break;
case "pc":
value = value * 12;
break;
case "px":
value = value * 3 / 4;
break;
}
// hardcoded limit on precision! can be lossy!
format.setMaximumFractionDigits(fractionDigits);
return format.format(value) + "pt";
}
}
class NonNegativeLengthFixer implements Fixer {
......@@ -204,3 +242,20 @@ class NonNegativeLengthFixer implements Fixer {
return (v == null) ? value : v;
}
}
class BorderFixer implements Fixer {
final private LosslessNumberConversion nc = new LosslessNumberConversion();
public String fix(String value) {
int index = value.indexOf(' ');
if (index != -1) {
String number = value.substring(0, index);
number = nc.convertPt(number, 2);
if (number != null) {
value = number + value.substring(index);
}
}
return value;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment