forked from pipeline-foundation/itext7-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into devsecops
- Loading branch information
Showing
10 changed files
with
351 additions
and
2 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
itext.tests/itext.svg.tests/itext/svg/renderers/SvgImageRendererTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+12.6 KB
...tests/itext.svg.tests/resources/itext/svg/renderers/SvgImageRendererTest/cmp_svgImage.pdf
Binary file not shown.
Binary file added
BIN
+1.63 KB
...sts/itext.svg.tests/resources/itext/svg/renderers/SvgImageRendererTest/cmp_svgWithSvg.pdf
Binary file not shown.
47 changes: 47 additions & 0 deletions
47
...itext.svg.tests/resources/itext/svg/renderers/SvgImageRendererTest/svgImage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions
5
...ext.svg.tests/resources/itext/svg/renderers/SvgImageRendererTest/svgWithSvg.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
061d0402f0e49842d8a2f7624b5787ada8fa9be1 | ||
ce1a909079af2dad4a4b447b0553f1d8e32e8248 |