Skip to content

Commit 89a2564

Browse files
committed
FOP-2872 Fix NPE when using em units on SVG
1 parent 3d6e7c5 commit 89a2564

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

fop-core/src/main/java/org/apache/fop/image/loader/batik/PreloaderSVG.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@
3434
import org.apache.commons.logging.LogFactory;
3535

3636
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
37+
import org.apache.batik.anim.dom.SVGDOMImplementation;
3738
import org.apache.batik.anim.dom.SVGOMDocument;
3839
import org.apache.batik.bridge.BridgeContext;
3940
import org.apache.batik.bridge.DefaultFontFamilyResolver;
4041
import org.apache.batik.bridge.UnitProcessor;
4142
import org.apache.batik.bridge.UserAgent;
43+
import org.apache.batik.css.engine.CSSEngine;
4244

4345
import org.apache.xmlgraphics.image.loader.ImageContext;
4446
import org.apache.xmlgraphics.image.loader.ImageInfo;
@@ -172,6 +174,15 @@ public void displayMessage(String message) {
172174

173175
};
174176
BridgeContext ctx = new BridgeContext(userAg);
177+
if (doc instanceof SVGOMDocument) {
178+
SVGOMDocument document = (SVGOMDocument) doc;
179+
CSSEngine eng = document.getCSSEngine();
180+
if (eng == null) {
181+
SVGDOMImplementation impl = (SVGDOMImplementation) document.getImplementation();
182+
impl.createCSSEngine(document, ctx);
183+
}
184+
}
185+
175186
UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e);
176187

177188
String s;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" standalone="no"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<testcase>
19+
<info>
20+
<p>
21+
This test checks that IDs coming from images properly appear in the IF output.
22+
</p>
23+
</info>
24+
<fo>
25+
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
26+
<fo:layout-master-set>
27+
<fo:simple-page-master master-name="sample">
28+
<fo:region-body/>
29+
</fo:simple-page-master>
30+
</fo:layout-master-set>
31+
<fo:page-sequence master-reference="sample">
32+
<fo:flow flow-name="xsl-region-body">
33+
<fo:block font-size="large" font-style="italic">Testing SVG images, there should be no NPE</fo:block>
34+
<fo:block>
35+
<fo:block> This is a SVG with its size in em: <fo:instream-foreign-object>
36+
<svg class="hazardsymbol" height="5em" version="1.1" viewBox="0 0 600 600" xmlns="http://www.w3.org/2000/svg">
37+
<rect fill="red" height="600" rx="10" ry="10" stroke="black" stroke-width="5" width="600" x="0" y="0"/>
38+
</svg>
39+
</fo:instream-foreign-object>
40+
</fo:block>
41+
</fo:block>
42+
</fo:flow>
43+
</fo:page-sequence>
44+
</fo:root>
45+
</fo>
46+
<if-checks xmlns:if="http://xmlgraphics.apache.org/fop/intermediate">
47+
<eval expected="5em" xpath="//if:page-sequence/descendant::*[local-name() = 'svg'][1]/@height"/>
48+
</if-checks>
49+
</testcase>

0 commit comments

Comments
 (0)