Skip to content

Commit

Permalink
#350 #358 - Use width/height attribute of external SVGs linked with i…
Browse files Browse the repository at this point in the history
…mg tag.

This is mostly a revert of d9cdd6a

I forgot about external SVGs rather than inline SVGs where the width/height attribute are converted to CSS earlier in the render process.
  • Loading branch information
danfickle committed Jul 4, 2019
1 parent 97143ee commit 6af18a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,6 @@ public void testReplacedPluginLatex() throws IOException {
* does not shutdown rendering altogether. Issue 353.
*/
@Test
@Ignore // Sizing is now all wrong for linked SVGs.
public void testSvgLinkedFromImgTag() throws IOException {
assertTrue(vt.runTest("svg-linked-from-img-tag", WITH_SVG));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,48 @@ public void setSecurityOptions(boolean allowScripts, boolean allowExternalResour
this.pdfTranscoder.setSecurityOptions(allowScripts, allowExternalResources);
this.pdfTranscoder.addTranscodingHint(SVGAbstractTranscoder.KEY_EXECUTE_ONLOAD, allowScripts);
}

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) {
XRLog.general(Level.WARNING,
"Invalid integer passed as dimension for SVG: "
+ attrValue);
return null;
}
}

public Point parseWidthHeightAttributes(Element e) {
String widthAttr = e.getAttribute("width");
Integer width = widthAttr.isEmpty() ? null : parseLength(widthAttr);

String heightAttr = e.getAttribute("height");
Integer height = heightAttr.isEmpty() ? null : parseLength(heightAttr);

if (width != null && height != null) {
return new Point(width, height);
}

return DEFAULT_DIMENSIONS;
}

public Point parseDimensions(Element e) {
String viewBoxAttr = e.getAttribute("viewBox");
String[] splitViewBox = viewBoxAttr.split("\\s+");
if (splitViewBox.length != 4) {
return DEFAULT_DIMENSIONS;
return parseWidthHeightAttributes(e);
}
try {
int viewBoxWidth = Integer.parseInt(splitViewBox[2]);
int viewBoxHeight = Integer.parseInt(splitViewBox[3]);

return new Point(viewBoxWidth, viewBoxHeight);
} catch (NumberFormatException ex) {
return DEFAULT_DIMENSIONS;
return parseWidthHeightAttributes(e);
}
}

Expand Down

0 comments on commit 6af18a0

Please sign in to comment.