Skip to content

Commit 0514ed0

Browse files
Benoit LagaeiText-CI
authored andcommitted
Register clip-path & basic operations
DEVSIX-2067 Autoported commit. Original commit hash: [746e5a218]
1 parent 97f7764 commit 0514ed0

25 files changed

+685
-52
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2018 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
using System;
44+
using iText.Svg.Renderers;
45+
using iText.Test;
46+
47+
namespace iText.Svg.Renderers.Impl {
48+
public class ClipPathSvgNodeRendererIntegrationTest : SvgIntegrationTest {
49+
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
50+
.CurrentContext.TestDirectory) + "/resources/itext/svg/renderers/impl/ClipPathTest/";
51+
52+
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
53+
+ "/test/itext/svg/renderers/impl/ClipPathTest/";
54+
55+
[NUnit.Framework.OneTimeSetUp]
56+
public static void BeforeClass() {
57+
ITextTest.CreateDestinationFolder(destinationFolder);
58+
}
59+
60+
/// <exception cref="System.IO.IOException"/>
61+
/// <exception cref="System.Exception"/>
62+
[NUnit.Framework.Test]
63+
public virtual void RectClipPathComplexTest() {
64+
ConvertAndCompareSinglePageVisually(sourceFolder, destinationFolder, "clippath_rect_complex");
65+
}
66+
67+
/// <exception cref="System.IO.IOException"/>
68+
/// <exception cref="System.Exception"/>
69+
[NUnit.Framework.Test]
70+
public virtual void RectClipPathSimpleTest() {
71+
ConvertAndCompareSinglePageVisually(sourceFolder, destinationFolder, "clippath_rect_simple");
72+
}
73+
74+
/// <exception cref="System.IO.IOException"/>
75+
/// <exception cref="System.Exception"/>
76+
[NUnit.Framework.Test]
77+
public virtual void CircleClipPathComplexTest() {
78+
ConvertAndCompareSinglePageVisually(sourceFolder, destinationFolder, "clippath_circle_complex");
79+
}
80+
81+
/// <exception cref="System.IO.IOException"/>
82+
/// <exception cref="System.Exception"/>
83+
[NUnit.Framework.Test]
84+
public virtual void CircleClipPathSimpleTest() {
85+
ConvertAndCompareSinglePageVisually(sourceFolder, destinationFolder, "clippath_circle_simple");
86+
}
87+
88+
/// <exception cref="System.IO.IOException"/>
89+
/// <exception cref="System.Exception"/>
90+
[NUnit.Framework.Test]
91+
public virtual void MultiClipPathComplexTest() {
92+
ConvertAndCompareSinglePageVisually(sourceFolder, destinationFolder, "clippath_multi_complex");
93+
}
94+
95+
/// <exception cref="System.IO.IOException"/>
96+
/// <exception cref="System.Exception"/>
97+
[NUnit.Framework.Test]
98+
public virtual void MoveClipPathTest() {
99+
ConvertAndCompareSinglePageVisually(sourceFolder, destinationFolder, "clippath_move");
100+
}
101+
102+
/// <exception cref="System.IO.IOException"/>
103+
/// <exception cref="System.Exception"/>
104+
[NUnit.Framework.Test]
105+
public virtual void MoveClipPathRuleMultipleTest() {
106+
ConvertAndCompareSinglePageVisually(sourceFolder, destinationFolder, "clippath_rule_multiple");
107+
}
108+
}
109+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using iText.Kernel.Pdf;
5+
using iText.Kernel.Pdf.Canvas;
6+
using iText.Layout.Font;
7+
using iText.StyledXmlParser.Resolver.Resource;
8+
using iText.Svg;
9+
using iText.Svg.Renderers;
10+
11+
namespace iText.Svg.Renderers.Impl {
12+
public class ClipPathSvgNodeRendererLowLevelIntegrationTest {
13+
private PdfCanvas cv;
14+
15+
private SvgDrawContext sdc;
16+
17+
/// <exception cref="System.IO.FileNotFoundException"/>
18+
[NUnit.Framework.SetUp]
19+
public virtual void SetupDrawContextAndCanvas() {
20+
sdc = new SvgDrawContext(new ResourceResolver(""), new FontProvider());
21+
// set compression to none, in case you want to write to disk and inspect the created document
22+
PdfWriter writer = new PdfWriter(new MemoryStream(), new WriterProperties().SetCompressionLevel(0));
23+
PdfDocument doc = new PdfDocument(writer);
24+
cv = new PdfCanvas(doc.AddNewPage());
25+
sdc.PushCanvas(cv);
26+
}
27+
28+
[NUnit.Framework.TearDown]
29+
public virtual void Close() {
30+
cv.GetDocument().Close();
31+
}
32+
33+
[NUnit.Framework.Test]
34+
public virtual void TestEmptyClipPathRendererNotDrawn() {
35+
ClipPathSvgNodeRenderer clipRenderer = new ClipPathSvgNodeRenderer();
36+
clipRenderer.SetAttributesAndStyles(new Dictionary<String, String>());
37+
clipRenderer.Draw(sdc);
38+
NUnit.Framework.Assert.AreEqual(0, cv.GetContentStream().GetBytes().Length);
39+
}
40+
41+
[NUnit.Framework.Test]
42+
public virtual void TestEmptyEoClipPathRendererNotDrawn() {
43+
ClipPathSvgNodeRenderer clipRenderer = new ClipPathSvgNodeRenderer();
44+
clipRenderer.SetAttribute(SvgConstants.Attributes.CLIP_RULE, SvgConstants.Values.FILL_RULE_EVEN_ODD);
45+
clipRenderer.Draw(sdc);
46+
NUnit.Framework.Assert.AreEqual(0, cv.GetContentStream().GetBytes().Length);
47+
}
48+
49+
[NUnit.Framework.Test]
50+
public virtual void TestRectClipPathRenderer() {
51+
ClipPathSvgNodeRenderer clipRenderer = new ClipPathSvgNodeRenderer();
52+
clipRenderer.SetAttributesAndStyles(new Dictionary<String, String>());
53+
RectangleSvgNodeRenderer rectRenderer = new RectangleSvgNodeRenderer();
54+
rectRenderer.SetAttribute(SvgConstants.Attributes.WIDTH, "400");
55+
rectRenderer.SetAttribute(SvgConstants.Attributes.HEIGHT, "400");
56+
clipRenderer.AddChild(rectRenderer);
57+
clipRenderer.Draw(sdc);
58+
NUnit.Framework.Assert.AreEqual("q\n% rect\n0 0 300 300 re\nh\nW\nn\nQ\n", iText.IO.Util.JavaUtil.GetStringForBytes
59+
(cv.GetContentStream().GetBytes()));
60+
}
61+
62+
[NUnit.Framework.Test]
63+
public virtual void TestRectClipPathEoRendererNoChange() {
64+
ClipPathSvgNodeRenderer clipRenderer = new ClipPathSvgNodeRenderer();
65+
// clip-rule can only be set on elements in a clipPath, and must not have any influence on drawing
66+
clipRenderer.SetAttribute(SvgConstants.Attributes.CLIP_RULE, SvgConstants.Values.FILL_RULE_EVEN_ODD);
67+
RectangleSvgNodeRenderer rectRenderer = new RectangleSvgNodeRenderer();
68+
rectRenderer.SetAttribute(SvgConstants.Attributes.WIDTH, "400");
69+
rectRenderer.SetAttribute(SvgConstants.Attributes.HEIGHT, "400");
70+
clipRenderer.AddChild(rectRenderer);
71+
clipRenderer.Draw(sdc);
72+
NUnit.Framework.Assert.AreEqual("q\n% rect\n0 0 300 300 re\nh\nW\nn\nQ\n", iText.IO.Util.JavaUtil.GetStringForBytes
73+
(cv.GetContentStream().GetBytes()));
74+
}
75+
76+
[NUnit.Framework.Test]
77+
public virtual void TestRectEoClipPathRenderer() {
78+
ClipPathSvgNodeRenderer clipRenderer = new ClipPathSvgNodeRenderer();
79+
clipRenderer.SetAttributesAndStyles(new Dictionary<String, String>());
80+
RectangleSvgNodeRenderer rectRenderer = new RectangleSvgNodeRenderer();
81+
rectRenderer.SetAttribute(SvgConstants.Attributes.WIDTH, "400");
82+
rectRenderer.SetAttribute(SvgConstants.Attributes.HEIGHT, "400");
83+
rectRenderer.SetAttribute(SvgConstants.Attributes.CLIP_RULE, SvgConstants.Values.FILL_RULE_EVEN_ODD);
84+
clipRenderer.AddChild(rectRenderer);
85+
clipRenderer.Draw(sdc);
86+
NUnit.Framework.Assert.AreEqual("q\n% rect\n0 0 300 300 re\nh\nW*\nn\nQ\n", iText.IO.Util.JavaUtil.GetStringForBytes
87+
(cv.GetContentStream().GetBytes()));
88+
}
89+
90+
[NUnit.Framework.Test]
91+
public virtual void TestAppliedClipPathRenderer() {
92+
AbstractBranchSvgNodeRenderer clipPathRenderer = new ClipPathSvgNodeRenderer();
93+
clipPathRenderer.SetAttribute(SvgConstants.Attributes.ID, "randomString");
94+
ISvgNodeRenderer clippedRenderer = new RectangleSvgNodeRenderer();
95+
clippedRenderer.SetAttribute(SvgConstants.Attributes.WIDTH, "80");
96+
clippedRenderer.SetAttribute(SvgConstants.Attributes.HEIGHT, "80");
97+
clipPathRenderer.AddChild(clippedRenderer);
98+
sdc.AddNamedObject("randomString", clipPathRenderer);
99+
ISvgNodeRenderer drawnRenderer = new CircleSvgNodeRenderer();
100+
drawnRenderer.SetAttribute(SvgConstants.Attributes.R, "84");
101+
drawnRenderer.SetAttribute(SvgConstants.Attributes.CLIP_PATH, "url(#randomString)");
102+
drawnRenderer.Draw(sdc);
103+
String expected = "q\n" + "% rect\n" + "0 0 60 60 re\n" + "h\n" + "W\n" + "n\n" + "0 0 0 rg\n" + "% ellipse\n"
104+
+ "63 0 m\n" + "63 34.79 34.79 63 0 63 c\n" + "-34.79 63 -63 34.79 -63 0 c\n" + "-63 -34.79 -34.79 -63 0 -63 c\n"
105+
+ "34.79 -63 63 -34.79 63 0 c\n" + "f\n" + "h\n" + "Q\n";
106+
NUnit.Framework.Assert.AreEqual(expected, iText.IO.Util.JavaUtil.GetStringForBytes(cv.GetContentStream().GetBytes
107+
()));
108+
}
109+
110+
[NUnit.Framework.Test]
111+
public virtual void TestAppliedGroupClipPathRenderer() {
112+
AbstractBranchSvgNodeRenderer clipPathRenderer = new ClipPathSvgNodeRenderer();
113+
clipPathRenderer.SetAttribute(SvgConstants.Attributes.ID, "randomString");
114+
ISvgNodeRenderer clippedRenderer = new RectangleSvgNodeRenderer();
115+
clippedRenderer.SetAttribute(SvgConstants.Attributes.WIDTH, "80");
116+
clippedRenderer.SetAttribute(SvgConstants.Attributes.HEIGHT, "80");
117+
clipPathRenderer.AddChild(clippedRenderer);
118+
sdc.AddNamedObject("randomString", clipPathRenderer);
119+
AbstractBranchSvgNodeRenderer groupRenderer = new GroupSvgNodeRenderer();
120+
groupRenderer.SetAttributesAndStyles(new Dictionary<String, String>());
121+
ISvgNodeRenderer drawnRenderer = new CircleSvgNodeRenderer();
122+
drawnRenderer.SetAttribute(SvgConstants.Attributes.R, "84");
123+
drawnRenderer.SetAttribute(SvgConstants.Attributes.CLIP_PATH, "url(#randomString)");
124+
groupRenderer.AddChild(drawnRenderer);
125+
groupRenderer.Draw(sdc);
126+
String expected = "0 0 0 rg\n" + "q\n" + "q\n" + "% rect\n" + "0 0 60 60 re\n" + "h\n" + "W\n" + "n\n" + "% ellipse\n"
127+
+ "63 0 m\n" + "63 34.79 34.79 63 0 63 c\n" + "-34.79 63 -63 34.79 -63 0 c\n" + "-63 -34.79 -34.79 -63 0 -63 c\n"
128+
+ "34.79 -63 63 -34.79 63 0 c\n" + "f\n" + "h\n" + "Q\n" + "Q\n" + "f\n" + "h\n";
129+
NUnit.Framework.Assert.AreEqual(expected, iText.IO.Util.JavaUtil.GetStringForBytes(cv.GetContentStream().GetBytes
130+
()));
131+
}
132+
133+
[NUnit.Framework.Test]
134+
public virtual void TestEoAppliedGroupClipPathRenderer() {
135+
AbstractBranchSvgNodeRenderer clipPathRenderer = new ClipPathSvgNodeRenderer();
136+
clipPathRenderer.SetAttribute(SvgConstants.Attributes.ID, "randomString");
137+
ISvgNodeRenderer clippedRenderer = new RectangleSvgNodeRenderer();
138+
clippedRenderer.SetAttribute(SvgConstants.Attributes.WIDTH, "80");
139+
clippedRenderer.SetAttribute(SvgConstants.Attributes.HEIGHT, "80");
140+
clippedRenderer.SetAttribute(SvgConstants.Attributes.CLIP_RULE, SvgConstants.Values.FILL_RULE_EVEN_ODD);
141+
ISvgNodeRenderer clippedRenderer2 = new RectangleSvgNodeRenderer();
142+
clippedRenderer2.SetAttribute(SvgConstants.Attributes.WIDTH, "80");
143+
clippedRenderer2.SetAttribute(SvgConstants.Attributes.HEIGHT, "80");
144+
clipPathRenderer.AddChild(clippedRenderer);
145+
clipPathRenderer.AddChild(clippedRenderer2);
146+
sdc.AddNamedObject("randomString", clipPathRenderer);
147+
AbstractBranchSvgNodeRenderer groupRenderer = new GroupSvgNodeRenderer();
148+
groupRenderer.SetAttributesAndStyles(new Dictionary<String, String>());
149+
ISvgNodeRenderer drawnRenderer = new CircleSvgNodeRenderer();
150+
drawnRenderer.SetAttribute(SvgConstants.Attributes.R, "84");
151+
drawnRenderer.SetAttribute(SvgConstants.Attributes.CLIP_PATH, "url(#randomString)");
152+
groupRenderer.AddChild(drawnRenderer);
153+
groupRenderer.Draw(sdc);
154+
String expected = "0 0 0 rg\n" + "q\n" + "q\n" + "% rect\n" + "0 0 60 60 re\n" + "h\n" + "W*\n" + "n\n" +
155+
"% ellipse\n" + "63 0 m\n" + "63 34.79 34.79 63 0 63 c\n" + "-34.79 63 -63 34.79 -63 0 c\n" + "-63 -34.79 -34.79 -63 0 -63 c\n"
156+
+ "34.79 -63 63 -34.79 63 0 c\n" + "f\n" + "h\n" + "Q\n" + "q\n" + "% rect\n" + "0 0 60 60 re\n" + "h\n"
157+
+ "W\n" + "n\n" + "% ellipse\n" + "63 0 m\n" + "63 34.79 34.79 63 0 63 c\n" + "-34.79 63 -63 34.79 -63 0 c\n"
158+
+ "-63 -34.79 -34.79 -63 0 -63 c\n" + "34.79 -63 63 -34.79 63 0 c\n" + "f\n" + "h\n" + "Q\n" + "Q\n"
159+
+ "f\n" + "h\n";
160+
NUnit.Framework.Assert.AreEqual(expected, iText.IO.Util.JavaUtil.GetStringForBytes(cv.GetContentStream().GetBytes
161+
()));
162+
}
163+
}
164+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2018 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
using iText.Svg;
44+
using iText.Svg.Renderers;
45+
46+
namespace iText.Svg.Renderers.Impl {
47+
public class ClipPathSvgNodeRendererUnitTest {
48+
[NUnit.Framework.Test]
49+
public virtual void DeepCopyTest() {
50+
ClipPathSvgNodeRenderer expected = new ClipPathSvgNodeRenderer();
51+
expected.SetAttribute(SvgConstants.Attributes.FILL, "blue");
52+
expected.AddChild(new CircleSvgNodeRenderer());
53+
ISvgNodeRenderer actual = expected.CreateDeepCopy();
54+
NUnit.Framework.Assert.AreEqual(expected, actual);
55+
}
56+
}
57+
}
Lines changed: 30 additions & 0 deletions
Loading
Lines changed: 10 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)