|
| 1 | +package com.itextpdf.svg.customization; |
| 2 | + |
| 3 | +import com.itextpdf.kernel.colors.ColorConstants; |
| 4 | +import com.itextpdf.kernel.geom.Rectangle; |
| 5 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 6 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 7 | +import com.itextpdf.kernel.pdf.canvas.PdfCanvas; |
| 8 | +import com.itextpdf.kernel.pdf.xobject.PdfFormXObject; |
| 9 | +import com.itextpdf.kernel.utils.CompareTool; |
| 10 | +import com.itextpdf.styledxmlparser.node.IElementNode; |
| 11 | +import com.itextpdf.svg.SvgConstants; |
| 12 | +import com.itextpdf.svg.SvgConstants.Tags; |
| 13 | +import com.itextpdf.svg.converter.SvgConverter; |
| 14 | +import com.itextpdf.svg.processors.impl.SvgConverterProperties; |
| 15 | +import com.itextpdf.svg.renderers.ISvgNodeRenderer; |
| 16 | +import com.itextpdf.svg.renderers.SvgDrawContext; |
| 17 | +import com.itextpdf.svg.renderers.SvgIntegrationTest; |
| 18 | +import com.itextpdf.svg.renderers.factories.DefaultSvgNodeRendererFactory; |
| 19 | +import com.itextpdf.svg.renderers.impl.TextLeafSvgNodeRenderer; |
| 20 | +import com.itextpdf.test.ITextTest; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.BeforeClass; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +public class CustomizeTextLeafSvgNodeRendererTest extends SvgIntegrationTest { |
| 28 | + |
| 29 | + public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/svg/customization/CustomizeTextLeafSvgNodeRendererTest/"; |
| 30 | + public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/svg/customization/CustomizeTextLeafSvgNodeRendererTest/"; |
| 31 | + |
| 32 | + @BeforeClass |
| 33 | + public static void beforeClass() { |
| 34 | + ITextTest.createDestinationFolder(DESTINATION_FOLDER); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testCustomizeTextLeafSvgNodeRenderer() throws IOException, InterruptedException { |
| 39 | + String pdfFilename = "customizeTextLeafSvgNodeRenderer.pdf"; |
| 40 | + PdfDocument doc = new PdfDocument(new PdfWriter(DESTINATION_FOLDER + pdfFilename)); |
| 41 | + doc.addNewPage(); |
| 42 | + |
| 43 | + SvgConverterProperties properties = new SvgConverterProperties(); |
| 44 | + properties.setRendererFactory(new CustomTextLeafOverridingSvgNodeRendererFactory()); |
| 45 | + |
| 46 | + String svg = "<svg viewBox=\"0 0 240 80\" xmlns=\"http://www.w3.org/2000/svg\">\n" |
| 47 | + + " <text x=\"20\" y=\"35\" class=\"small\">Hello world</text>\n" |
| 48 | + + "</svg>"; |
| 49 | + |
| 50 | + PdfFormXObject form = SvgConverter.convertToXObject(svg, doc, properties); |
| 51 | + new PdfCanvas(doc.getPage(1)).addXObjectFittedIntoRectangle(form, new Rectangle(100, 100, 240, 80)); |
| 52 | + |
| 53 | + doc.close(); |
| 54 | + Assert.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + pdfFilename, SOURCE_FOLDER + "cmp_" + pdfFilename, DESTINATION_FOLDER, "diff_")); |
| 55 | + } |
| 56 | + |
| 57 | + private static class CustomTextLeafOverridingSvgNodeRendererFactory extends DefaultSvgNodeRendererFactory { |
| 58 | + @Override |
| 59 | + public ISvgNodeRenderer createSvgNodeRendererForTag(IElementNode tag, ISvgNodeRenderer parent) { |
| 60 | + if (Tags.TEXT_LEAF.equals(tag.name())) { |
| 61 | + return new CustomTextLeafSvgNodeRenderer(); |
| 62 | + } else { |
| 63 | + return super.createSvgNodeRendererForTag(tag, parent); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static class CustomTextLeafSvgNodeRenderer extends TextLeafSvgNodeRenderer { |
| 69 | + @Override |
| 70 | + public ISvgNodeRenderer createDeepCopy() { |
| 71 | + CustomTextLeafSvgNodeRenderer copy = new CustomTextLeafSvgNodeRenderer(); |
| 72 | + deepCopyAttributesAndStyles(copy); |
| 73 | + return copy; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void doDraw(SvgDrawContext context) { |
| 78 | + if (this.attributesAndStyles != null && this.attributesAndStyles.containsKey(SvgConstants.Attributes.TEXT_CONTENT)) { |
| 79 | + PdfCanvas currentCanvas = context.getCurrentCanvas(); |
| 80 | + currentCanvas.setFillColor(ColorConstants.RED); |
| 81 | + currentCanvas.moveText(context.getTextMove()[0], context.getTextMove()[1]); |
| 82 | + String initialText = this.attributesAndStyles.get(SvgConstants.Attributes.TEXT_CONTENT); |
| 83 | + String amendedText = "_" + initialText + "_"; |
| 84 | + currentCanvas.showText(amendedText); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments