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

Adding ability to specify 'UserStyleSheetURI' for an SVGDrawer, this … #496

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -20,6 +20,8 @@ SVGImage buildSVGImage(Element svgElement, Box box, CssContext cssContext, doubl

default void withUserAgent(UserAgentCallback userAgentCallback) {}

default void withUserStyleSheetURI(String userStyleSheetURI) {}

interface SVGImage {
int getIntrinsicWidth();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class BatikSVGDrawer implements SVGDrawer {
private final boolean allowScripts;
private final boolean allowExternalResources;
private UserAgentCallback userAgentCallback;
private String userStyleSheetURI;

public enum SvgScriptMode {
SECURE,
Expand Down Expand Up @@ -83,6 +84,9 @@ public void withUserAgent(UserAgentCallback userAgentCallback) {
this.userAgentCallback = userAgentCallback;
}

@Override
public void withUserStyleSheetURI(String userStyleSheetURI) { this.userStyleSheetURI = userStyleSheetURI; }

@Override
public SVGImage buildSVGImage(Element svgElement, Box box, CssContext c,
double cssWidth, double cssHeight, double dotsPerPixel) {
Expand All @@ -91,7 +95,7 @@ public SVGImage buildSVGImage(Element svgElement, Box box, CssContext c,
double cssMaxHeight = CalculatedStyle.getCSSMaxHeight(c, box);

BatikSVGImage img = new BatikSVGImage(svgElement, box, cssWidth, cssHeight,
cssMaxWidth, cssMaxHeight, dotsPerPixel);
cssMaxWidth, cssMaxHeight, dotsPerPixel, userStyleSheetURI);
img.setFontResolver(fontResolver);
img.setUserAgentCallback(userAgentCallback);
img.setSecurityOptions(allowScripts, allowExternalResources, allowedProtocols);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class BatikSVGImage implements SVGImage {
private UserAgentCallback userAgentCallback;

public BatikSVGImage(Element svgElement, Box box, double cssWidth, double cssHeight,
double cssMaxWidth, double cssMaxHeight, double dotsPerPixel) {
double cssMaxWidth, double cssMaxHeight, double dotsPerPixel, String userStyleSheetURI) {
this.svgElement = svgElement;
this.dotsPerPixel = dotsPerPixel;

Expand All @@ -58,6 +58,11 @@ public BatikSVGImage(Element svgElement, Box box, double cssWidth, double cssHei
SVGAbstractTranscoder.KEY_MAX_HEIGHT,
(float) (cssMaxHeight / dotsPerPixel));
}
if(userStyleSheetURI != null) {
this.pdfTranscoder.addTranscodingHint(
SVGAbstractTranscoder.KEY_USER_STYLESHEET_URI,
userStyleSheetURI);
}

Point dimensions = parseDimensions(svgElement);
double w;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
public class OpenHtmlUserAgent extends UserAgentAdapter {

private final OpenHtmlFontResolver resolver;
private final String userStyleSheetURI;
private final boolean allowScripts;
private final boolean allowExternalResources;
private final Set<String> allowedProtocols;

public OpenHtmlUserAgent(OpenHtmlFontResolver resolver, boolean allowScripts, boolean allowExternalResources, Set<String> allowedProtocols) {
public OpenHtmlUserAgent(OpenHtmlFontResolver resolver, String userStyleSheetURI, boolean allowScripts, boolean allowExternalResources, Set<String> allowedProtocols) {
this.resolver = resolver;
this.userStyleSheetURI = userStyleSheetURI;
this.allowScripts = allowScripts;
this.allowExternalResources = allowExternalResources;
this.allowedProtocols = allowedProtocols;
Expand All @@ -43,4 +45,9 @@ public void checkLoadExternalResource(ParsedURL resourceURL, ParsedURL docURL) t
throw new SecurityException("Tried to fetch external resource (" + resourceURL + ") from SVG. Refused!");
}
}

@Override
public String getUserStyleSheetURI() {
return userStyleSheetURI;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,12 @@ protected BridgeContext createBridgeContext(String svgVersion) {

@Override
protected void transcode(Document svg, String uri, TranscoderOutput out) throws TranscoderException {


String userStyleSheetURI = (String)hints.get(SVGAbstractTranscoder.KEY_USER_STYLESHEET_URI);

// Note: We have to initialize user agent here and not in ::createUserAgent() as method
// is called before our constructor is called in the super constructor.
this.userAgent = new OpenHtmlUserAgent(this.fontResolver, this.allowScripts, this.allowExternalResources, this.allowedProtocols);
this.userAgent = new OpenHtmlUserAgent(this.fontResolver, userStyleSheetURI, this.allowScripts, this.allowExternalResources, this.allowedProtocols);
super.transcode(svg, uri, out);

Rectangle contentBounds = box.getContentAreaEdge(box.getAbsX(), box.getAbsY(), ctx);
Expand Down