Skip to content

Commit edb8819

Browse files
Samuel HuylebroeckBenoit Lagae
authored andcommitted
Inline svg integration
RND-918
1 parent 87dd718 commit edb8819

File tree

15 files changed

+139
-8
lines changed

15 files changed

+139
-8
lines changed

src/main/java/com/itextpdf/html2pdf/attach/ProcessorContext.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ public class ProcessorContext {
119119
/** The Processor meta info */
120120
private IMetaInfo metaInfo;
121121

122+
/** Internal state variable to keep track of whether the processor is currently inside an inlineSvg*/
123+
private boolean processingInlineSvg;
124+
122125
/**
123126
* Instantiates a new {@link ProcessorContext} instance.
124127
*
@@ -170,6 +173,7 @@ public ProcessorContext(ConverterProperties converterProperties) {
170173
radioCheckResolver = new RadioCheckResolver();
171174
immediateFlush = converterProperties.isImmediateFlush();
172175
metaInfo = converterProperties.getEventCountingMetaInfo();
176+
processingInlineSvg = false;
173177
}
174178

175179
/**
@@ -357,6 +361,7 @@ public void reset() {
357361
this.fontProvider = new FontProvider(this.fontProvider.getFontSet());
358362
this.tempFonts = null;
359363
this.outlineHandler.reset();
364+
this.processingInlineSvg = false;
360365
}
361366

362367
/**
@@ -396,4 +401,26 @@ public boolean isImmediateFlush(){
396401
public IMetaInfo getEventCountingMetaInfo() {
397402
return metaInfo;
398403
}
404+
405+
/**
406+
* Check if the processor is currently processing an inline svg
407+
* @return True if the processor is processing an inline Svg, false otherwise.
408+
*/
409+
public boolean isProcessingInlineSvg() {
410+
return processingInlineSvg;
411+
}
412+
413+
/**
414+
* Set the processor to processing Inline Svg state
415+
*/
416+
public void startProcessingInlineSvg(){
417+
processingInlineSvg = true;
418+
}
419+
420+
/**
421+
* End the processing Svg State
422+
*/
423+
public void endProcessingInlineSvg(){
424+
processingInlineSvg = false;
425+
}
399426
}

src/main/java/com/itextpdf/html2pdf/attach/impl/DefaultHtmlProcessor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ private void visit(INode node) {
330330
if (element.name().equals(TagConstants.BODY) || element.name().equals(TagConstants.HTML))
331331
runApplier(element, tagWorker);
332332
for (INode childNode : element.childNodes()) {
333-
visit(childNode);
333+
if(!context.isProcessingInlineSvg()) {
334+
visit(childNode);
335+
}
334336
}
335337
visitPseudoElement(element, tagWorker, CssConstants.AFTER);
336338

src/main/java/com/itextpdf/html2pdf/attach/impl/DefaultTagWorkerMapping.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ This file is part of the iText (R) project.
7171
import com.itextpdf.html2pdf.attach.impl.tags.PreTagWorker;
7272
import com.itextpdf.html2pdf.attach.impl.tags.SelectTagWorker;
7373
import com.itextpdf.html2pdf.attach.impl.tags.SpanTagWorker;
74+
import com.itextpdf.html2pdf.attach.impl.tags.SvgTagWorker;
7475
import com.itextpdf.html2pdf.attach.impl.tags.TableFooterTagWorker;
7576
import com.itextpdf.html2pdf.attach.impl.tags.TableHeaderTagWorker;
7677
import com.itextpdf.html2pdf.attach.impl.tags.TableTagWorker;
@@ -172,6 +173,7 @@ private DefaultTagWorkerMapping() {
172173
workerMapping.putMapping(TagConstants.STRONG, SpanTagWorker.class);
173174
workerMapping.putMapping(TagConstants.SUB, SpanTagWorker.class);
174175
workerMapping.putMapping(TagConstants.SUP, SpanTagWorker.class);
176+
workerMapping.putMapping(TagConstants.SVG, SvgTagWorker.class);
175177
workerMapping.putMapping(TagConstants.TABLE, TableTagWorker.class);
176178
workerMapping.putMapping(TagConstants.TD, TdTagWorker.class);
177179
workerMapping.putMapping(TagConstants.TEXTAREA, TextAreaTagWorker.class);
@@ -224,7 +226,6 @@ private DefaultTagWorkerMapping() {
224226
workerMapping.putMapping(afterPseudoElemName, CssConstants.TABLE, DivTagWorker.class);
225227
workerMapping.putMapping(CssPseudoElementUtil.createPseudoElementTagName(TagConstants.IMG), ImgTagWorker.class);
226228

227-
228229
// custom elements mapping, implementation-specific
229230
workerMapping.putMapping(PageCountElementNode.PAGE_COUNTER_TAG, PageCountWorker.class);
230231
workerMapping.putMapping(PageMarginBoxContextNode.PAGE_MARGIN_BOX_TAG, PageMarginBoxWorker.class);

src/main/java/com/itextpdf/html2pdf/attach/impl/tags/SvgTagWorker.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.itextpdf.layout.IPropertyContainer;
88
import com.itextpdf.layout.element.Image;
99
import com.itextpdf.styledxmlparser.node.IElementNode;
10+
import com.itextpdf.styledxmlparser.node.INode;
1011
import com.itextpdf.svg.exceptions.SvgProcessingException;
1112
import com.itextpdf.svg.processors.ISvgProcessor;
1213
import com.itextpdf.svg.processors.ISvgProcessorResult;
@@ -36,9 +37,8 @@ public SvgTagWorker(IElementNode element, ProcessorContext context) {
3637
svgImage = null;
3738
try{
3839
ISvgProcessor proc = new DefaultSvgProcessor();
39-
//TODO(blocked by DEVSIX-1955, RND-982): uncomment and register in the mapping
40-
//processingResult = proc.process((INode) element);
41-
processingResult = null;
40+
processingResult = proc.process((INode) element);
41+
context.startProcessingInlineSvg();
4242
}catch(SvgProcessingException pe){
4343
LOGGER.error(LogMessageConstant.UNABLE_TO_PROCESS_IMAGE_AS_SVG,pe);
4444
}
@@ -49,6 +49,7 @@ public void processEnd(IElementNode element, ProcessorContext context) {
4949
if(context.getPdfDocument() != null && processingResult != null){
5050
SvgProcessingUtil util = new SvgProcessingUtil();
5151
svgImage = util.createImageFromProcessingResult(processingResult,context.getPdfDocument());
52+
context.endProcessingInlineSvg();
5253
}
5354
}
5455

src/main/java/com/itextpdf/html2pdf/css/apply/impl/DefaultTagCssApplierMapping.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ private DefaultTagCssApplierMapping() {
132132
mapping.putMapping(TagConstants.STRONG, SpanTagCssApplier.class);
133133
mapping.putMapping(TagConstants.SUB, SpanTagCssApplier.class);
134134
mapping.putMapping(TagConstants.SUP, SpanTagCssApplier.class);
135+
mapping.putMapping(TagConstants.SVG, BlockCssApplier.class);
135136
mapping.putMapping(TagConstants.TABLE, TableTagCssApplier.class);
136137
mapping.putMapping(TagConstants.TEXTAREA, BlockCssApplier.class);
137138
mapping.putMapping(TagConstants.TD, TdTagCssApplier.class);

src/main/java/com/itextpdf/html2pdf/html/TagConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ private TagConstants() {
245245
/** The Constant S. */
246246
public static final String S = "s";
247247

248+
/** The Constant SVG. */
249+
public static final String SVG = "svg";
250+
248251
/** The Constant SAMP. */
249252
public static final String SAMP = "samp";
250253

src/test/java/com/itextpdf/html2pdf/element/SvgTest.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,66 @@
99
import com.itextpdf.test.annotations.type.IntegrationTest;
1010
import org.junit.Assert;
1111
import org.junit.BeforeClass;
12-
import org.junit.Ignore;
12+
import org.junit.Rule;
1313
import org.junit.Test;
1414
import org.junit.experimental.categories.Category;
15+
import org.junit.rules.ExpectedException;
1516

1617
import java.io.File;
1718
import java.io.IOException;
1819

20+
import static com.itextpdf.html2pdf.LogMessageConstant.UNABLE_TO_RETRIEVE_FONT;
21+
import static com.itextpdf.html2pdf.LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI;
22+
1923
@Category(IntegrationTest.class)
2024
public class SvgTest extends ExtendedITextTest {
2125

2226
public static final String sourceFolder = "./src/test/resources/com/itextpdf/html2pdf/element/SvgTest/";
2327
public static final String destinationFolder = "./target/test/com/itextpdf/html2pdf/element/SvgTest/";
2428

29+
@Rule
30+
public ExpectedException junitExpectedException = ExpectedException.none();
31+
2532
@BeforeClass
2633
public static void beforeClass() {
2734
createDestinationFolder(destinationFolder);
2835
}
2936

30-
@Ignore("RND-982")
3137
@Test
3238
public void InlineSvgTest() throws IOException, InterruptedException {
33-
String name = "inline";
39+
String name = "inline_svg";
40+
HtmlConverter.convertToPdf(new File(sourceFolder + name + ".html"), new File(destinationFolder + name+".pdf"));
41+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name +".pdf", sourceFolder + "cmp_" + name+".pdf", destinationFolder, "diff_"+name+"_"));
42+
}
43+
44+
@Test
45+
public void InlineNestedSvgTest() throws IOException, InterruptedException {
46+
String name = "inline_nested_svg";
3447
HtmlConverter.convertToPdf(new File(sourceFolder + name + ".html"), new File(destinationFolder + name+".pdf"));
3548
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name +".pdf", sourceFolder + "cmp_" + name+".pdf", destinationFolder, "diff_"+name+"_"));
3649

3750
}
3851

52+
@Test
53+
@LogMessages(messages = {
54+
// TODO RND-883 external font loading in SVG
55+
@LogMessage(messageTemplate = UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, count = 2),
56+
@LogMessage(messageTemplate = UNABLE_TO_RETRIEVE_FONT)
57+
})
58+
public void InlineSvgExternalFontRelativeTest() throws IOException, InterruptedException {
59+
String name = "inline_svg_external_font_relative";
60+
HtmlConverter.convertToPdf(new File(sourceFolder + name + ".html"), new File(destinationFolder + name+".pdf"));
61+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name +".pdf", sourceFolder + "cmp_" + name+".pdf", destinationFolder, "diff_"+name+"_"));
62+
}
63+
64+
@Test
65+
public void InlineSvgExternalFontUrlTest() throws IOException, InterruptedException {
66+
// TODO RND-883 external font loading in SVG
67+
String name = "inline_svg_external_font_url";
68+
HtmlConverter.convertToPdf(new File(sourceFolder + name + ".html"), new File(destinationFolder + name+".pdf"));
69+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name +".pdf", sourceFolder + "cmp_" + name+".pdf", destinationFolder, "diff_"+name+"_"));
70+
}
71+
3972
@Test
4073
@LogMessages(messages = {
4174
@LogMessage(messageTemplate = com.itextpdf.styledxmlparser.LogMessageConstant.ERROR_PARSING_COULD_NOT_MAP_NODE),
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)