Skip to content

Commit 0c6ced9

Browse files
Artyom YanchevskyiText-CI
authored andcommitted
Support type3 glyphs with custom names
DEVSIX-2475 Autoported commit. Original commit hash: [a716f19e4]
1 parent 8d779be commit 0c6ced9

File tree

15 files changed

+539
-109
lines changed

15 files changed

+539
-109
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 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.Test;
45+
46+
namespace iText.IO.Font {
47+
public class FontEncodingTest : ExtendedITextTest {
48+
[NUnit.Framework.Test]
49+
public virtual void NotSetDifferenceToMinus1IndexTest() {
50+
FontEncoding encoding = FontEncoding.CreateEmptyFontEncoding();
51+
String[] initialDifferences = (String[])encoding.differences.Clone();
52+
encoding.SetDifference(-1, "a");
53+
NUnit.Framework.Assert.AreEqual(initialDifferences, encoding.differences);
54+
}
55+
56+
[NUnit.Framework.Test]
57+
public virtual void NotSetDifferenceTo256IndexTest() {
58+
FontEncoding encoding = FontEncoding.CreateEmptyFontEncoding();
59+
String[] initialDifferences = (String[])encoding.differences.Clone();
60+
encoding.SetDifference(256, "a");
61+
NUnit.Framework.Assert.AreEqual(initialDifferences, encoding.differences);
62+
}
63+
64+
[NUnit.Framework.Test]
65+
public virtual void SetDifferenceToZeroIndexTest() {
66+
FontEncoding encoding = FontEncoding.CreateEmptyFontEncoding();
67+
encoding.SetDifference(0, "a");
68+
NUnit.Framework.Assert.AreEqual("a", encoding.differences[0]);
69+
}
70+
71+
[NUnit.Framework.Test]
72+
public virtual void SetDifferenceTo255IndexTest() {
73+
FontEncoding encoding = FontEncoding.CreateEmptyFontEncoding();
74+
encoding.SetDifference(255, "a");
75+
NUnit.Framework.Assert.AreEqual("a", encoding.differences[255]);
76+
}
77+
78+
[NUnit.Framework.Test]
79+
public virtual void GetNullDifferenceTest() {
80+
FontEncoding encoding = FontEncoding.CreateEmptyFontEncoding();
81+
NUnit.Framework.Assert.IsNull(encoding.GetDifference(0));
82+
}
83+
84+
[NUnit.Framework.Test]
85+
public virtual void SetDifferenceAndGetTest() {
86+
FontEncoding encoding = FontEncoding.CreateEmptyFontEncoding();
87+
encoding.SetDifference(0, "a");
88+
NUnit.Framework.Assert.AreEqual("a", encoding.GetDifference(0));
89+
}
90+
}
91+
}

itext.tests/itext.kernel.tests/itext/kernel/font/PdfType3FontTest.cs

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ You should have received a copy of the GNU Affero General Public License
2525
using iText.Kernel;
2626
using iText.Kernel.Pdf;
2727
using iText.Test;
28+
using iText.Test.Attributes;
2829

2930
namespace iText.Kernel.Font {
3031
public class PdfType3FontTest : ExtendedITextTest {
3132
private const float EPS = 1e-4f;
3233

3334
[NUnit.Framework.Test]
35+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
3436
public virtual void AddDifferentGlyphsInConstructorTest() {
3537
PdfDictionary dictionary = new PdfDictionary();
3638
dictionary.Put(PdfName.FontMatrix, new PdfArray());
@@ -41,7 +43,7 @@ public virtual void AddDifferentGlyphsInConstructorTest() {
4143
dictionary.Put(PdfName.Widths, new PdfArray());
4244
dictionary.Put(PdfName.ToUnicode, PdfName.IdentityH);
4345
dictionary.Put(PdfName.Encoding, new PdfName("zapfdingbatsencoding"));
44-
PdfType3Font type3Font = new _PdfType3Font_58(dictionary);
46+
PdfType3Font type3Font = new _PdfType3Font_64(dictionary);
4547
NUnit.Framework.Assert.IsNotNull(type3Font.GetFontProgram());
4648
int spaceGlyphCode = 32;
4749
Glyph glyph = type3Font.GetFontProgram().GetGlyph(spaceGlyphCode);
@@ -51,8 +53,8 @@ public virtual void AddDifferentGlyphsInConstructorTest() {
5153
NUnit.Framework.Assert.AreEqual(new Glyph(AGlyphCode, 0, new char[] { 'A' }), glyph);
5254
}
5355

54-
private sealed class _PdfType3Font_58 : PdfType3Font {
55-
public _PdfType3Font_58(PdfDictionary baseArg1)
56+
private sealed class _PdfType3Font_64 : PdfType3Font {
57+
public _PdfType3Font_64(PdfDictionary baseArg1)
5658
: base(baseArg1) {
5759
}
5860

@@ -62,14 +64,15 @@ protected internal override PdfDocument GetDocument() {
6264
}
6365

6466
[NUnit.Framework.Test]
67+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
6568
public virtual void AddAlreadyExistingGlyphTest() {
6669
PdfDictionary dictionary = new PdfDictionary();
6770
dictionary.Put(PdfName.FontMatrix, new PdfArray());
6871
PdfDictionary charProcs = new PdfDictionary();
6972
charProcs.Put(new PdfName("A"), new PdfStream());
7073
dictionary.Put(PdfName.CharProcs, charProcs);
7174
dictionary.Put(PdfName.Widths, new PdfArray());
72-
PdfType3Font type3Font = new _PdfType3Font_82(dictionary);
75+
PdfType3Font type3Font = new _PdfType3Font_89(dictionary);
7376
Type3Glyph type3Glyph = type3Font.AddGlyph('A', 1, 2, 3, 5, 8);
7477
NUnit.Framework.Assert.AreEqual(0, type3Glyph.GetWx(), EPS);
7578
NUnit.Framework.Assert.AreEqual(0, type3Glyph.GetLlx(), EPS);
@@ -78,8 +81,8 @@ public virtual void AddAlreadyExistingGlyphTest() {
7881
NUnit.Framework.Assert.AreEqual(0, type3Glyph.GetUry(), EPS);
7982
}
8083

81-
private sealed class _PdfType3Font_82 : PdfType3Font {
82-
public _PdfType3Font_82(PdfDictionary baseArg1)
84+
private sealed class _PdfType3Font_89 : PdfType3Font {
85+
public _PdfType3Font_89(PdfDictionary baseArg1)
8386
: base(baseArg1) {
8487
}
8588

@@ -89,6 +92,7 @@ protected internal override PdfDocument GetDocument() {
8992
}
9093

9194
[NUnit.Framework.Test]
95+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
9296
public virtual void SetFontStretchTest() {
9397
PdfDictionary dictionary = new PdfDictionary();
9498
dictionary.Put(PdfName.FontMatrix, new PdfArray());
@@ -104,6 +108,7 @@ public virtual void SetFontStretchTest() {
104108
}
105109

106110
[NUnit.Framework.Test]
111+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
107112
public virtual void SetPdfFontFlagsTest() {
108113
PdfDictionary dictionary = new PdfDictionary();
109114
dictionary.Put(PdfName.FontMatrix, new PdfArray());
@@ -118,6 +123,7 @@ public virtual void SetPdfFontFlagsTest() {
118123
}
119124

120125
[NUnit.Framework.Test]
126+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
121127
public virtual void GlyphWithUnicodeBiggerThan32CannotBeEncodedTest() {
122128
PdfDictionary dictionary = new PdfDictionary();
123129
dictionary.Put(PdfName.FontMatrix, new PdfArray());
@@ -130,13 +136,14 @@ public virtual void GlyphWithUnicodeBiggerThan32CannotBeEncodedTest() {
130136
}
131137

132138
[NUnit.Framework.Test]
139+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
133140
public virtual void ContainsGlyphTest() {
134141
PdfDictionary dictionary = new PdfDictionary();
135142
dictionary.Put(PdfName.FontMatrix, new PdfArray());
136143
PdfDictionary charProcs = new PdfDictionary();
137144
dictionary.Put(PdfName.CharProcs, charProcs);
138145
dictionary.Put(PdfName.Widths, new PdfArray());
139-
PdfType3Font type3Font = new _PdfType3Font_148(dictionary);
146+
PdfType3Font type3Font = new _PdfType3Font_159(dictionary);
140147
NUnit.Framework.Assert.IsFalse(type3Font.ContainsGlyph(333));
141148
NUnit.Framework.Assert.IsFalse(type3Font.ContainsGlyph(-5));
142149
NUnit.Framework.Assert.IsFalse(type3Font.ContainsGlyph(32));
@@ -146,8 +153,8 @@ public virtual void ContainsGlyphTest() {
146153
NUnit.Framework.Assert.IsTrue(type3Font.ContainsGlyph(65));
147154
}
148155

149-
private sealed class _PdfType3Font_148 : PdfType3Font {
150-
public _PdfType3Font_148(PdfDictionary baseArg1)
156+
private sealed class _PdfType3Font_159 : PdfType3Font {
157+
public _PdfType3Font_159(PdfDictionary baseArg1)
151158
: base(baseArg1) {
152159
}
153160

@@ -157,6 +164,7 @@ protected internal override PdfDocument GetDocument() {
157164
}
158165

159166
[NUnit.Framework.Test]
167+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
160168
public virtual void FlushExceptionTest() {
161169
NUnit.Framework.Assert.That(() => {
162170
PdfDictionary dictionary = new PdfDictionary();
@@ -173,6 +181,7 @@ public virtual void FlushExceptionTest() {
173181
}
174182

175183
[NUnit.Framework.Test]
184+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
176185
public virtual void FillFontDescriptorTest() {
177186
PdfDictionary dictionary = new PdfDictionary();
178187
dictionary.Put(PdfName.FontMatrix, new PdfArray());
@@ -183,14 +192,14 @@ public virtual void FillFontDescriptorTest() {
183192
String fontStretch = "test";
184193
fontDescriptor.Put(PdfName.FontStretch, new PdfName(fontStretch));
185194
dictionary.Put(PdfName.FontDescriptor, fontDescriptor);
186-
PdfType3Font type3Font = new _PdfType3Font_189(dictionary);
195+
PdfType3Font type3Font = new _PdfType3Font_202(dictionary);
187196
NUnit.Framework.Assert.IsNotNull(type3Font.fontProgram);
188197
NUnit.Framework.Assert.IsNotNull(type3Font.fontProgram.GetFontNames());
189198
NUnit.Framework.Assert.AreEqual(fontStretch, type3Font.fontProgram.GetFontNames().GetFontStretch());
190199
}
191200

192-
private sealed class _PdfType3Font_189 : PdfType3Font {
193-
public _PdfType3Font_189(PdfDictionary baseArg1)
201+
private sealed class _PdfType3Font_202 : PdfType3Font {
202+
public _PdfType3Font_202(PdfDictionary baseArg1)
194203
: base(baseArg1) {
195204
}
196205

@@ -199,6 +208,54 @@ protected internal override PdfDocument GetDocument() {
199208
}
200209
}
201210

211+
[NUnit.Framework.Test]
212+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
213+
public virtual void NoCharProcsTest() {
214+
PdfDictionary dictionary = new PdfDictionary();
215+
dictionary.Put(PdfName.FontMatrix, new PdfArray());
216+
dictionary.Put(PdfName.Widths, new PdfArray());
217+
NUnit.Framework.Assert.DoesNotThrow(() => new PdfType3Font(dictionary));
218+
}
219+
220+
[NUnit.Framework.Test]
221+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
222+
public virtual void NoEncodingTest() {
223+
PdfDictionary dictionary = new PdfDictionary();
224+
dictionary.Put(PdfName.FontMatrix, new PdfArray());
225+
PdfDictionary charProcs = new PdfDictionary();
226+
dictionary.Put(PdfName.CharProcs, charProcs);
227+
dictionary.Put(PdfName.Widths, new PdfArray());
228+
NUnit.Framework.Assert.DoesNotThrow(() => new PdfType3Font(dictionary));
229+
}
230+
231+
[NUnit.Framework.Test]
232+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
233+
public virtual void NoDifferenceTest() {
234+
PdfDictionary dictionary = new PdfDictionary();
235+
dictionary.Put(PdfName.FontMatrix, new PdfArray());
236+
PdfDictionary charProcs = new PdfDictionary();
237+
dictionary.Put(PdfName.CharProcs, charProcs);
238+
dictionary.Put(PdfName.Widths, new PdfArray());
239+
PdfDictionary encoding = new PdfDictionary();
240+
dictionary.Put(PdfName.Encoding, encoding);
241+
NUnit.Framework.Assert.DoesNotThrow(() => new PdfType3Font(dictionary));
242+
}
243+
244+
[NUnit.Framework.Test]
245+
public virtual void NoCharProcGlyphForDifferenceTest() {
246+
PdfDictionary font = new PdfDictionary();
247+
font.Put(PdfName.FontMatrix, new PdfArray());
248+
font.Put(PdfName.Widths, new PdfArray());
249+
font.Put(PdfName.CharProcs, new PdfDictionary());
250+
PdfDictionary encoding = new PdfDictionary();
251+
PdfArray differences = new PdfArray();
252+
differences.Add(0, new PdfNumber(65));
253+
differences.Add(1, new PdfName("A"));
254+
encoding.Put(PdfName.Differences, differences);
255+
font.Put(PdfName.Encoding, encoding);
256+
NUnit.Framework.Assert.DoesNotThrow(() => new PdfType3Font(font));
257+
}
258+
202259
private class DisableEnsureUnderlyingObjectHasIndirectReference : PdfType3Font {
203260
internal DisableEnsureUnderlyingObjectHasIndirectReference(PdfType3FontTest _enclosing, PdfDictionary fontDictionary
204261
)

0 commit comments

Comments
 (0)