Skip to content

Commit

Permalink
Merge branch 'develop' into devsecops
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed Aug 18, 2023
2 parents 05cf563 + 2d6af24 commit 3452c5b
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
using iText.Layout;
using iText.StyledXmlParser.Node;
using iText.StyledXmlParser.Resolver.Resource;
using iText.Svg.Converter;
using iText.Svg.Element;
using iText.Svg.Processors;
using iText.Svg.Processors.Impl;
using iText.Svg.Xobject;

namespace iText.Svg.Renderers {
[NUnit.Framework.Category("IntegrationTest")]
public class SvgImageRendererTest : SvgIntegrationTest {
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/svg/renderers/SvgImageRendererTest/";

public static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
+ "/test/itext/svg/SvgImageRendererTest/";

[NUnit.Framework.OneTimeSetUp]
public static void BeforeClass() {
CreateDestinationFolder(DESTINATION_FOLDER);
}

[NUnit.Framework.Test]
public virtual void SvgWithSvgTest() {
String svgFileName = SOURCE_FOLDER + "svgWithSvg.svg";
String cmpFileName = SOURCE_FOLDER + "cmp_svgWithSvg.pdf";
String outFileName = DESTINATION_FOLDER + "svgWithSvg.pdf";
using (Document document = new Document(new PdfDocument(new PdfWriter(outFileName, new WriterProperties().
SetCompressionLevel(0))))) {
INode parsedSvg = SvgConverter.Parse(new FileStream(svgFileName, FileMode.Open, FileAccess.Read));
ISvgProcessorResult result = new DefaultSvgProcessor().Process(parsedSvg, null);
ISvgNodeRenderer topSvgRenderer = result.GetRootRenderer();
float[] wh = SvgConverter.ExtractWidthAndHeight(topSvgRenderer);
SvgImageXObject svgImageXObject = new SvgImageXObject(new Rectangle(0, 0, wh[0], wh[1]), result, new ResourceResolver
(SOURCE_FOLDER));
SvgImage svgImage = new SvgImage(svgImageXObject);
document.Add(svgImage);
document.Add(svgImage);
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
, "diff"));
}

[NUnit.Framework.Test]
public virtual void CustomSvgImageTest() {
String svgFileName = SOURCE_FOLDER + "svgImage.svg";
String cmpFileName = SOURCE_FOLDER + "cmp_svgImage.pdf";
String outFileName = DESTINATION_FOLDER + "svgImage.pdf";
using (Document document = new Document(new PdfDocument(new PdfWriter(outFileName, new WriterProperties().
SetCompressionLevel(0))))) {
INode parsedSvg = SvgConverter.Parse(new FileStream(svgFileName, FileMode.Open, FileAccess.Read));
ISvgProcessorResult result = new DefaultSvgProcessor().Process(parsedSvg, null);
ISvgNodeRenderer topSvgRenderer = result.GetRootRenderer();
float[] wh = SvgConverter.ExtractWidthAndHeight(topSvgRenderer);
SvgImageXObject svgImageXObject = new SvgImageXObject(new Rectangle(0, 0, wh[0], wh[1]), result, new ResourceResolver
(SOURCE_FOLDER));
SvgImage svgImage = new SvgImage(svgImageXObject);
document.Add(svgImage);
document.Add(svgImage);
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
, "diff"));
}
}
}
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion itext/itext.layout/itext/layout/renderer/ListRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private IRenderer CreateListSymbolRenderer(int index, IRenderer renderer) {
}
else {
if (defaultListSymbol is Image) {
return new ImageRenderer((Image)defaultListSymbol);
return ((Image)defaultListSymbol).GetRenderer();
}
else {
if (defaultListSymbol is ListNumberingType) {
Expand Down
62 changes: 62 additions & 0 deletions itext/itext.svg/itext/svg/element/SvgImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using iText.Kernel.Pdf;
using iText.Layout.Element;
using iText.Layout.Renderer;
using iText.Svg.Renderers;
using iText.Svg.Xobject;

namespace iText.Svg.Element {
/// <summary>A layout element that represents SVG image for inclusion in the document model.</summary>
public class SvgImage : Image {
/// <summary>
/// Creates an
/// <see cref="SvgImage"/>
/// from the
/// <see cref="iText.Svg.Xobject.SvgImageXObject"/>
/// which represents Form XObject and contains
/// processor result with the SVG information and resource resolver for the SVG image.
/// </summary>
/// <param name="xObject">
/// an internal
/// <see cref="iText.Svg.Xobject.SvgImageXObject"/>.
/// </param>
public SvgImage(SvgImageXObject xObject)
: base(xObject) {
}

/// <summary>
/// Draws SVG image to a canvas-like object maintained in the
/// <see cref="iText.Svg.Renderers.SvgDrawContext"/>.
/// </summary>
/// <param name="document">pdf that shall contain the SVG image.</param>
public virtual void Generate(PdfDocument document) {
((SvgImageXObject)xObject).Generate(document);
}

/// <summary><inheritDoc/></summary>
protected override IRenderer MakeNewRenderer() {
return new SvgImageRenderer(this);
}
}
}
49 changes: 49 additions & 0 deletions itext/itext.svg/itext/svg/renderers/SvgImageRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using iText.Layout.Renderer;
using iText.Svg.Element;

namespace iText.Svg.Renderers {
/// <summary>
/// Represents a renderer for the
/// <see cref="iText.Svg.Element.SvgImage"/>
/// layout element.
/// </summary>
public class SvgImageRenderer : ImageRenderer {
/// <summary>Creates an SvgImageRenderer from its corresponding layout object.</summary>
/// <param name="image">
/// the
/// <see cref="iText.Svg.Element.SvgImage"/>
/// which this object should manage
/// </param>
public SvgImageRenderer(SvgImage image)
: base(image) {
}

/// <summary><inheritDoc/></summary>
public override void Draw(DrawContext drawContext) {
((SvgImage)modelElement).Generate(drawContext.GetDocument());
base.Draw(drawContext);
}
}
}
93 changes: 93 additions & 0 deletions itext/itext.svg/itext/svg/xobject/SvgImageXObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
using iText.StyledXmlParser.Resolver.Resource;
using iText.Svg.Processors;
using iText.Svg.Processors.Impl;
using iText.Svg.Renderers;
using iText.Svg.Renderers.Impl;

namespace iText.Svg.Xobject {
/// <summary>A wrapper for Form XObject for SVG images.</summary>
public class SvgImageXObject : PdfFormXObject {
private readonly ISvgProcessorResult result;

private readonly ResourceResolver resourceResolver;

private bool isGenerated = false;

/// <summary>Creates a new instance of Form XObject for the SVG image.</summary>
/// <param name="bBox">the form XObject’s bounding box.</param>
/// <param name="result">processor result containing the SVG information.</param>
/// <param name="resourceResolver">
///
/// <see cref="iText.StyledXmlParser.Resolver.Resource.ResourceResolver"/>
/// for the SVG image.
/// </param>
public SvgImageXObject(Rectangle bBox, ISvgProcessorResult result, ResourceResolver resourceResolver)
: base(bBox) {
this.result = result;
this.resourceResolver = resourceResolver;
}

/// <summary>Returns processor result containing the SVG information.</summary>
/// <returns>{ISvgProcessorResult} processor result.</returns>
public virtual ISvgProcessorResult GetResult() {
return result;
}

/// <summary>Returns resource resolver for the SVG image.</summary>
/// <returns>
///
/// <see cref="iText.StyledXmlParser.Resolver.Resource.ResourceResolver"/>
/// instance
/// </returns>
public virtual ResourceResolver GetResourceResolver() {
return resourceResolver;
}

/// <summary>Processes xObject before first image generation to avoid drawing it twice or more.</summary>
/// <remarks>
/// Processes xObject before first image generation to avoid drawing it twice or more. It allows to reuse the same
/// Form XObject multiple times.
/// </remarks>
/// <param name="document">pdf that shall contain the SVG image.</param>
public virtual void Generate(PdfDocument document) {
if (!isGenerated) {
PdfCanvas canvas = new PdfCanvas(this, document);
SvgDrawContext context = new SvgDrawContext(resourceResolver, result.GetFontProvider());
if (result is SvgProcessorResult) {
context.SetCssContext(((SvgProcessorResult)result).GetContext().GetCssContext());
}
context.AddNamedObjects(result.GetNamedObjects());
context.PushCanvas(canvas);
ISvgNodeRenderer root = new PdfRootSvgNodeRenderer(result.GetRootRenderer());
root.Draw(context);
isGenerated = true;
}
}
}
}
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
061d0402f0e49842d8a2f7624b5787ada8fa9be1
ce1a909079af2dad4a4b447b0553f1d8e32e8248

0 comments on commit 3452c5b

Please sign in to comment.