diff --git a/buildSrc/src/main/java/org/docbook/xsltng/extensions/ImageCall.java b/buildSrc/src/main/java/org/docbook/xsltng/extensions/ImageCall.java index ae521fee3..a8cab9040 100644 --- a/buildSrc/src/main/java/org/docbook/xsltng/extensions/ImageCall.java +++ b/buildSrc/src/main/java/org/docbook/xsltng/extensions/ImageCall.java @@ -21,9 +21,13 @@ import java.io.InputStreamReader; import java.net.URL; import java.util.StringTokenizer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; abstract public class ImageCall extends ExtensionFunctionCall { + public static final double SVG_DPI = 96.0; private final static String svgNamespace = "http://www.w3.org/2000/svg"; + private final static Pattern dimension = Pattern.compile("^\\s*([-+]?[0-9]*(\\.[0-9]*))\\s*([a-zA-Z]+)\\s*$"); protected DebuggingLogger logger = null; protected XPathContext context = null; @@ -121,23 +125,15 @@ protected XdmMap parseProperty(XdmMap map, String name, String value, int tagTyp } break; case "width": - // This property must be returned as an integer; round if necessary - try { - float fwidth = Float.parseFloat(value); - int width = Math.round(fwidth); + Integer width = convertDimension(value); + if (width != null) { map = map.put(new XdmAtomicValue("width"), new XdmAtomicValue(width)); - } catch (NumberFormatException nfe) { - // nevermind } break; case "height": - // This property must be returned as an integer; round if necessary - try { - float fheight = Float.parseFloat(value); - int height = Math.round(fheight); + Integer height = convertDimension(value); + if (height != null) { map = map.put(new XdmAtomicValue("height"), new XdmAtomicValue(height)); - } catch (NumberFormatException nfe) { - // nevermind } break; default: @@ -153,6 +149,53 @@ protected XdmMap parseProperty(XdmMap map, String name, String value, int tagTyp return map; } + private Integer convertDimension(String value) { + // This property must be returned as an integer; round if necessary + // What if we have units? + String unit = null; + double dwidth = 0.0; + + try { + Matcher matches = dimension.matcher(value); + if (matches.find()) { + dwidth = Double.parseDouble(matches.group(1)); + unit = matches.group(3).toLowerCase(); + } else { + dwidth = Double.parseDouble(value); + } + + if (unit != null) { + switch (unit) { + case "in": + dwidth = dwidth * SVG_DPI; + break; + case "cm": + dwidth = dwidth * SVG_DPI / 2.54; + break; + case "mm": + dwidth = dwidth * SVG_DPI / 25.4; + break; + case "pt": + dwidth = dwidth * SVG_DPI / 72.0; + break; + case "pc": + dwidth = dwidth * SVG_DPI / 6.0; + break; + case "px": + break; + default: + System.err.printf("Unrecognized SVG unit '%s'. Assuming pixels.%n", unit); + } + } + + return Math.round((float) dwidth); + } catch (NumberFormatException nfe) { + // nevermind + } + + return null; + } + private XdmMap parseBox(XdmMap map, String line) { int [] corners = new int [4]; int count = 0; diff --git a/src/test/resources/expected/mediaobject.006.html b/src/test/resources/expected/mediaobject.006.html new file mode 100644 index 000000000..2adbe92b5 --- /dev/null +++ b/src/test/resources/expected/mediaobject.006.html @@ -0,0 +1,8 @@ +SVG Graphic Dimensions

SVG Graphic Dimensions

Frank Steimke

Check scaling an svg image where Dimensions are given in SVG file in Inches: <sgv + width="24.09375in" height="9.40625in">. The image is created by the + MagicDraw UML Software.

Issue 432 is that + the generated HTML creates an img with wrong size: + style="width:144px;height:96px;" (which is 1.5in x 1.0in).

Function f:object-properties in objects.xsl uses the + external function ext:image-metadata, which seems to fail when dimensions in an + SVG file are given as value with units.

Original SizeScaled 25%
Inchpxpx
Height9,40625903226
Width24,093752313578

This is an SVG Image scaled by 25%. Expected Dimensions are 578px x 226 + px

Figure 1SVG Image scaled by 25%
\ No newline at end of file diff --git a/src/test/resources/media/EvidenceRequest.svg b/src/test/resources/media/EvidenceRequest.svg new file mode 100644 index 000000000..06c5e167b --- /dev/null +++ b/src/test/resources/media/EvidenceRequest.svg @@ -0,0 +1,903 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/xml/mediaobject.006.xml b/src/test/resources/xml/mediaobject.006.xml new file mode 100644 index 000000000..fc1e41491 --- /dev/null +++ b/src/test/resources/xml/mediaobject.006.xml @@ -0,0 +1,92 @@ + +
+ + SVG Graphic Dimensions + + FrankSteimke + fsteimke.hb@gmail.com + + + Check scaling an svg image where Dimensions are given in SVG file in Inches: <sgv + width="24.09375in" height="9.40625in">. The image is created by the + MagicDraw UML Software. + Issue 432 is that + the generated HTML creates an img with wrong size: + style="width:144px;height:96px;" (which is 1.5in x 1.0in). + Function f:object-properties in objects.xsl uses the + external function ext:image-metadata, which seems to fail when dimensions in an + SVG file are given as value with units. + + + + + + + + + + Original Size + Scaled 25% + + + + Inch + px + px + + + + + Height + 9,40625 + 903 + 226 + + + Width + 24,09375 + 2313 + 578 + + + + +
+ SVG Image scaled by 25% + + + + + + This is an SVG Image scaled by 25%. Expected Dimensions are 578px x 226 + px + + +
+ +