Skip to content

Commit

Permalink
Extended support for absolute CSS units
Browse files Browse the repository at this point in the history
Some changes to BatikSVGImage parseLength method, so that it supports
absolute CSS units and fractional measurements as mentioned in danfickle#592.
  • Loading branch information
DanielWulfert committed Nov 2, 2020
1 parent 9ffd0e4 commit a267a4b
Showing 1 changed file with 74 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package com.openhtmltopdf.svgsupport;

import com.openhtmltopdf.extend.OutputDevice;
import com.openhtmltopdf.extend.SVGDrawer.SVGImage;
import com.openhtmltopdf.extend.UserAgentCallback;
import com.openhtmltopdf.render.Box;
import com.openhtmltopdf.render.RenderingContext;
import com.openhtmltopdf.svgsupport.PDFTranscoder.OpenHtmlFontResolver;
import com.openhtmltopdf.util.LogMessageId;
import com.openhtmltopdf.util.XRLog;
import java.awt.Point;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;

import com.openhtmltopdf.extend.UserAgentCallback;
import com.openhtmltopdf.util.LogMessageId;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.batik.anim.dom.SVGDOMImplementation;
import org.apache.batik.transcoder.SVGAbstractTranscoder;
import org.apache.batik.transcoder.TranscoderException;
Expand All @@ -16,13 +26,6 @@
import org.w3c.dom.Node;
import org.w3c.dom.Text;

import com.openhtmltopdf.extend.OutputDevice;
import com.openhtmltopdf.extend.SVGDrawer.SVGImage;
import com.openhtmltopdf.render.Box;
import com.openhtmltopdf.render.RenderingContext;
import com.openhtmltopdf.svgsupport.PDFTranscoder.OpenHtmlFontResolver;
import com.openhtmltopdf.util.XRLog;

public class BatikSVGImage implements SVGImage {
private final static int DEFAULT_SVG_WIDTH = 400;
private final static int DEFAULT_SVG_HEIGHT = 400;
Expand All @@ -33,6 +36,65 @@ public class BatikSVGImage implements SVGImage {
private OpenHtmlFontResolver fontResolver;
private final PDFTranscoder pdfTranscoder;
private UserAgentCallback userAgentCallback;

private enum AbsoluteCSSUnits {

CENTIMETERS("cm", 37.7952755906),
MILLIMETERS("mm", 3.77952755906),
INCHES("in", 300.0),
PIXELS("px", 1.0),
POINTS("pt", 4.0/3.0),
PICAS("pc", 16.0);

private String unit;
private double pixelRatio;

private AbsoluteCSSUnits(String unit, double pixelRatio) {
this.unit = unit;
this.pixelRatio = pixelRatio;
}

public String getUnit() {
return unit;
}

public int toPixel(Number amount) {
return amount != null ? (int) Math.round(amount.doubleValue() * pixelRatio) : null;
}

public static List<String> getUnits() {
return Arrays.stream(AbsoluteCSSUnits.values())
.map(AbsoluteCSSUnits::getUnit)
.collect(Collectors.toList());
}

public static AbsoluteCSSUnits parseFromUnit(String unit) {
return unit != null
? Arrays.stream(AbsoluteCSSUnits.values())
.filter(cssUnit -> unit.toLowerCase().equals(cssUnit.unit))
.findFirst().orElse(null)
: null;
}
}

private static class HtmlUnitConverter {
public static Pattern UNIT_PATTERN = Pattern.compile("\\s*(\\d+(\\.\\d+)?)\\s*("
+ AbsoluteCSSUnits.getUnits().stream().collect(Collectors.joining("|"))
+ ")?", Pattern.CASE_INSENSITIVE);

public static Integer convert(String value) throws Exception {
Matcher matcher = UNIT_PATTERN.matcher(value);
if (matcher.matches()) {
Double number = Double.valueOf(matcher.group(1));
AbsoluteCSSUnits unit = AbsoluteCSSUnits.parseFromUnit(matcher.group(3));
Integer dots = unit != null
? (int) Math.round(unit.toPixel(number))
: number.intValue();
return dots;
}
return null;
}
}

public BatikSVGImage(Element svgElement, Box box, double cssWidth, double cssHeight,
double cssMaxWidth, double cssMaxHeight, double dotsPerPixel) {
Expand Down Expand Up @@ -113,12 +175,9 @@ public void setUserAgentCallback(UserAgentCallback userAgentCallback) {
}

public Integer parseLength(String attrValue) {
// TODO read length with units and convert to dots.
// length ::= number (~"em" | ~"ex" | ~"px" | ~"in" | ~"cm" | ~"mm" |
// ~"pt" | ~"pc")?
try {
return Integer.valueOf(attrValue);
} catch (NumberFormatException e) {
return HtmlUnitConverter.convert(attrValue);
} catch (Exception e) {
XRLog.log(Level.WARNING, LogMessageId.LogMessageId1Param.GENERAL_INVALID_INTEGER_PASSED_AS_DIMENSION_FOR_SVG, attrValue);
return null;
}
Expand Down

0 comments on commit a267a4b

Please sign in to comment.