Adding ability to specify 'UserStyleSheetURI' for an SVGDrawer, this … #496
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
…allows SVGs to render without requiring styles to be inline.
Fixes #493
Hi @danfickle,
So I was trying to find a solution to issue 493 where in order for SVG elements to render with the correct css, the styles are required to be declared inline in the SVG. Ideally, SVG elements would be able to reference an external stylesheet to pull their styles as well. This was possible in the older itext/flying saucer implementation by providing a transcoder hint for the svg/png transcoder used by Batik.
Eventually I tracked down the issue in the Batik BridgeContext class, specifically the 'initializeDocument' method tries to set up the CSSEngine on the SVGDOMImplementation using the UserAgent.
The problem that I was running into was the implementation of UserAgent, OpenHtmlUserAgent doesn't override the method 'getUserStyleSheetURI' from its base class UserAgentAdapter and thus always returns null for that method call. That method is what's used in the BridgeContext to setup the CSSEngine for the SVG.
To get the value I needed injected down to the UserAgent class I had to pass it through a number of other classes starting at the public interface point which is the BatikSVGDrawer:
BatikSVGDrawer > BatikSVGImage > PDFTranscoder > OpenHtmlUserAgent
I'm not super familiar with this library, so there may have been a more elegant implementation path that I missed, also unsure of any potential side affects this may have caused but I tried to be as minimal and backwards compatible as possible. Only obvious breaking change I can see is the modification of the existing constructor in OpenHtmlUserAgent which could be breaking if that class is used outside the library, but that could be alleviated by adding the new constructor along side the existing constructor. Using the new stylesheet property would also require the user to construct the BatikSVGDrawer with the argument SvgExternalResourceMode.INSECURE_ALLOW_EXTERNAL_RESOURCE_REQUESTS which may not be ideal.
Example usage:
Thank you for the review and consideration!