|
| 1 | +using System; |
| 2 | +using iText.Kernel.Colors; |
| 3 | +using iText.Kernel.Geom; |
| 4 | +using iText.Kernel.Pdf; |
| 5 | +using iText.Kernel.Pdf.Canvas; |
| 6 | +using iText.Kernel.Pdf.Xobject; |
| 7 | +using iText.Kernel.Utils; |
| 8 | +using iText.StyledXmlParser.Node; |
| 9 | +using iText.Svg; |
| 10 | +using iText.Svg.Converter; |
| 11 | +using iText.Svg.Processors.Impl; |
| 12 | +using iText.Svg.Renderers; |
| 13 | +using iText.Svg.Renderers.Factories; |
| 14 | +using iText.Svg.Renderers.Impl; |
| 15 | +using iText.Test; |
| 16 | + |
| 17 | +namespace iText.Svg.Customization { |
| 18 | + public class CustomizeTextLeafSvgNodeRendererTest : SvgIntegrationTest { |
| 19 | + public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 20 | + .CurrentContext.TestDirectory) + "/resources/itext/svg/customization/CustomizeTextLeafSvgNodeRendererTest/"; |
| 21 | + |
| 22 | + public static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory |
| 23 | + + "/test/itext/svg/customization/CustomizeTextLeafSvgNodeRendererTest/"; |
| 24 | + |
| 25 | + [NUnit.Framework.OneTimeSetUp] |
| 26 | + public static void BeforeClass() { |
| 27 | + ITextTest.CreateDestinationFolder(DESTINATION_FOLDER); |
| 28 | + } |
| 29 | + |
| 30 | + [NUnit.Framework.Test] |
| 31 | + public virtual void TestCustomizeTextLeafSvgNodeRenderer() { |
| 32 | + String pdfFilename = "customizeTextLeafSvgNodeRenderer.pdf"; |
| 33 | + PdfDocument doc = new PdfDocument(new PdfWriter(DESTINATION_FOLDER + pdfFilename)); |
| 34 | + doc.AddNewPage(); |
| 35 | + SvgConverterProperties properties = new SvgConverterProperties(); |
| 36 | + properties.SetRendererFactory(new CustomizeTextLeafSvgNodeRendererTest.CustomTextLeafOverridingSvgNodeRendererFactory |
| 37 | + ()); |
| 38 | + String svg = "<svg viewBox=\"0 0 240 80\" xmlns=\"http://www.w3.org/2000/svg\">\n" + " <text x=\"20\" y=\"35\" class=\"small\">Hello world</text>\n" |
| 39 | + + "</svg>"; |
| 40 | + PdfFormXObject form = SvgConverter.ConvertToXObject(svg, doc, properties); |
| 41 | + new PdfCanvas(doc.GetPage(1)).AddXObjectFittedIntoRectangle(form, new Rectangle(100, 100, 240, 80)); |
| 42 | + doc.Close(); |
| 43 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(DESTINATION_FOLDER + pdfFilename, SOURCE_FOLDER |
| 44 | + + "cmp_" + pdfFilename, DESTINATION_FOLDER, "diff_")); |
| 45 | + } |
| 46 | + |
| 47 | + private class CustomTextLeafOverridingSvgNodeRendererFactory : DefaultSvgNodeRendererFactory { |
| 48 | + public override ISvgNodeRenderer CreateSvgNodeRendererForTag(IElementNode tag, ISvgNodeRenderer parent) { |
| 49 | + if (SvgConstants.Tags.TEXT_LEAF.Equals(tag.Name())) { |
| 50 | + return new CustomizeTextLeafSvgNodeRendererTest.CustomTextLeafSvgNodeRenderer(); |
| 51 | + } |
| 52 | + else { |
| 53 | + return base.CreateSvgNodeRendererForTag(tag, parent); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private class CustomTextLeafSvgNodeRenderer : TextLeafSvgNodeRenderer { |
| 59 | + public override ISvgNodeRenderer CreateDeepCopy() { |
| 60 | + CustomizeTextLeafSvgNodeRendererTest.CustomTextLeafSvgNodeRenderer copy = new CustomizeTextLeafSvgNodeRendererTest.CustomTextLeafSvgNodeRenderer |
| 61 | + (); |
| 62 | + DeepCopyAttributesAndStyles(copy); |
| 63 | + return copy; |
| 64 | + } |
| 65 | + |
| 66 | + protected internal override void DoDraw(SvgDrawContext context) { |
| 67 | + if (this.attributesAndStyles != null && this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.TEXT_CONTENT |
| 68 | + )) { |
| 69 | + PdfCanvas currentCanvas = context.GetCurrentCanvas(); |
| 70 | + currentCanvas.SetFillColor(ColorConstants.RED); |
| 71 | + currentCanvas.MoveText(context.GetTextMove()[0], context.GetTextMove()[1]); |
| 72 | + String initialText = this.attributesAndStyles.Get(SvgConstants.Attributes.TEXT_CONTENT); |
| 73 | + String amendedText = "_" + initialText + "_"; |
| 74 | + currentCanvas.ShowText(amendedText); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments