Skip to content

Commit f436541

Browse files
BezrukovMiText-CI
authored andcommitted
Fix NPE in PdfDocument.getFont with direct font
DEVSIX-3499 Autoported commit. Original commit hash: [a734801d8]
1 parent e0d5efb commit f436541

File tree

14 files changed

+81
-16
lines changed

14 files changed

+81
-16
lines changed

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

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ You should have received a copy of the GNU Affero General Public License
2222
*/
2323
using System;
2424
using iText.IO.Font.Otf;
25+
using iText.IO.Util;
2526
using iText.Kernel;
2627
using iText.Kernel.Pdf;
2728
using iText.Test;
@@ -43,7 +44,7 @@ public virtual void AddDifferentGlyphsInConstructorTest() {
4344
dictionary.Put(PdfName.Widths, new PdfArray());
4445
dictionary.Put(PdfName.ToUnicode, PdfName.IdentityH);
4546
dictionary.Put(PdfName.Encoding, new PdfName("zapfdingbatsencoding"));
46-
PdfType3Font type3Font = new _PdfType3Font_64(dictionary);
47+
PdfType3Font type3Font = new _PdfType3Font_65(dictionary);
4748
NUnit.Framework.Assert.IsNotNull(type3Font.GetFontProgram());
4849
int spaceGlyphCode = 32;
4950
Glyph glyph = type3Font.GetFontProgram().GetGlyph(spaceGlyphCode);
@@ -53,8 +54,8 @@ public virtual void AddDifferentGlyphsInConstructorTest() {
5354
NUnit.Framework.Assert.AreEqual(new Glyph(AGlyphCode, 0, new char[] { 'A' }), glyph);
5455
}
5556

56-
private sealed class _PdfType3Font_64 : PdfType3Font {
57-
public _PdfType3Font_64(PdfDictionary baseArg1)
57+
private sealed class _PdfType3Font_65 : PdfType3Font {
58+
public _PdfType3Font_65(PdfDictionary baseArg1)
5859
: base(baseArg1) {
5960
}
6061

@@ -72,7 +73,7 @@ public virtual void AddAlreadyExistingGlyphTest() {
7273
charProcs.Put(new PdfName("A"), new PdfStream());
7374
dictionary.Put(PdfName.CharProcs, charProcs);
7475
dictionary.Put(PdfName.Widths, new PdfArray());
75-
PdfType3Font type3Font = new _PdfType3Font_89(dictionary);
76+
PdfType3Font type3Font = new _PdfType3Font_90(dictionary);
7677
Type3Glyph type3Glyph = type3Font.AddGlyph('A', 1, 2, 3, 5, 8);
7778
NUnit.Framework.Assert.AreEqual(0, type3Glyph.GetWx(), EPS);
7879
NUnit.Framework.Assert.AreEqual(0, type3Glyph.GetLlx(), EPS);
@@ -81,8 +82,8 @@ public virtual void AddAlreadyExistingGlyphTest() {
8182
NUnit.Framework.Assert.AreEqual(0, type3Glyph.GetUry(), EPS);
8283
}
8384

84-
private sealed class _PdfType3Font_89 : PdfType3Font {
85-
public _PdfType3Font_89(PdfDictionary baseArg1)
85+
private sealed class _PdfType3Font_90 : PdfType3Font {
86+
public _PdfType3Font_90(PdfDictionary baseArg1)
8687
: base(baseArg1) {
8788
}
8889

@@ -143,7 +144,7 @@ public virtual void ContainsGlyphTest() {
143144
PdfDictionary charProcs = new PdfDictionary();
144145
dictionary.Put(PdfName.CharProcs, charProcs);
145146
dictionary.Put(PdfName.Widths, new PdfArray());
146-
PdfType3Font type3Font = new _PdfType3Font_159(dictionary);
147+
PdfType3Font type3Font = new _PdfType3Font_160(dictionary);
147148
NUnit.Framework.Assert.IsFalse(type3Font.ContainsGlyph(333));
148149
NUnit.Framework.Assert.IsFalse(type3Font.ContainsGlyph(-5));
149150
NUnit.Framework.Assert.IsFalse(type3Font.ContainsGlyph(32));
@@ -153,8 +154,8 @@ public virtual void ContainsGlyphTest() {
153154
NUnit.Framework.Assert.IsTrue(type3Font.ContainsGlyph(65));
154155
}
155156

156-
private sealed class _PdfType3Font_159 : PdfType3Font {
157-
public _PdfType3Font_159(PdfDictionary baseArg1)
157+
private sealed class _PdfType3Font_160 : PdfType3Font {
158+
public _PdfType3Font_160(PdfDictionary baseArg1)
158159
: base(baseArg1) {
159160
}
160161

@@ -192,14 +193,14 @@ public virtual void FillFontDescriptorTest() {
192193
String fontStretch = "test";
193194
fontDescriptor.Put(PdfName.FontStretch, new PdfName(fontStretch));
194195
dictionary.Put(PdfName.FontDescriptor, fontDescriptor);
195-
PdfType3Font type3Font = new _PdfType3Font_202(dictionary);
196+
PdfType3Font type3Font = new _PdfType3Font_203(dictionary);
196197
NUnit.Framework.Assert.IsNotNull(type3Font.fontProgram);
197198
NUnit.Framework.Assert.IsNotNull(type3Font.fontProgram.GetFontNames());
198199
NUnit.Framework.Assert.AreEqual(fontStretch, type3Font.fontProgram.GetFontNames().GetFontStretch());
199200
}
200201

201-
private sealed class _PdfType3Font_202 : PdfType3Font {
202-
public _PdfType3Font_202(PdfDictionary baseArg1)
202+
private sealed class _PdfType3Font_203 : PdfType3Font {
203+
public _PdfType3Font_203(PdfDictionary baseArg1)
203204
: base(baseArg1) {
204205
}
205206

@@ -241,6 +242,34 @@ public virtual void NoDifferenceTest() {
241242
NUnit.Framework.Assert.DoesNotThrow(() => new PdfType3Font(dictionary));
242243
}
243244

245+
[NUnit.Framework.Test]
246+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
247+
public virtual void MissingFontMatrixTest() {
248+
PdfDictionary dictionary = new PdfDictionary();
249+
dictionary.Put(PdfName.Widths, new PdfArray());
250+
dictionary.Put(PdfName.ToUnicode, PdfName.IdentityH);
251+
dictionary.Put(PdfName.Encoding, new PdfName("zapfdingbatsencoding"));
252+
NUnit.Framework.Assert.That(() => {
253+
new PdfType3Font(dictionary);
254+
}
255+
, NUnit.Framework.Throws.InstanceOf<PdfException>().With.Message.EqualTo(MessageFormatUtil.Format(PdfException.MissingRequiredFieldInFontDictionary, PdfName.FontMatrix)))
256+
;
257+
}
258+
259+
[NUnit.Framework.Test]
260+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
261+
public virtual void MissingWidthsTest() {
262+
PdfDictionary dictionary = new PdfDictionary();
263+
dictionary.Put(PdfName.FontMatrix, new PdfArray());
264+
dictionary.Put(PdfName.ToUnicode, PdfName.IdentityH);
265+
dictionary.Put(PdfName.Encoding, new PdfName("zapfdingbatsencoding"));
266+
NUnit.Framework.Assert.That(() => {
267+
new PdfType3Font(dictionary);
268+
}
269+
, NUnit.Framework.Throws.InstanceOf<PdfException>().With.Message.EqualTo(MessageFormatUtil.Format(PdfException.MissingRequiredFieldInFontDictionary, PdfName.Widths)))
270+
;
271+
}
272+
244273
[NUnit.Framework.Test]
245274
public virtual void NoCharProcGlyphForDifferenceTest() {
246275
PdfDictionary font = new PdfDictionary();

itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfDocumentUnitTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,39 @@ source product.
4444
using System;
4545
using System.Collections.Generic;
4646
using System.IO;
47+
using iText.IO.Font;
4748
using iText.IO.Source;
49+
using iText.Kernel.Font;
4850
using iText.Kernel.Pdf.Layer;
4951
using iText.Test;
5052
using iText.Test.Attributes;
5153

5254
namespace iText.Kernel.Pdf {
5355
public class PdfDocumentUnitTest : ExtendedITextTest {
56+
[NUnit.Framework.Test]
57+
[LogMessage(iText.IO.LogMessageConstant.TYPE3_FONT_INITIALIZATION_ISSUE)]
58+
public virtual void GetFontWithDirectFontDictionaryTest() {
59+
PdfDictionary initialFontDict = new PdfDictionary();
60+
initialFontDict.Put(PdfName.Subtype, PdfName.Type3);
61+
initialFontDict.Put(PdfName.FontMatrix, new PdfArray(new float[] { 0.001F, 0, 0, 0.001F, 0, 0 }));
62+
initialFontDict.Put(PdfName.Widths, new PdfArray());
63+
PdfDictionary encoding = new PdfDictionary();
64+
initialFontDict.Put(PdfName.Encoding, encoding);
65+
PdfArray differences = new PdfArray();
66+
differences.Add(new PdfNumber(AdobeGlyphList.NameToUnicode("a")));
67+
differences.Add(new PdfName("a"));
68+
encoding.Put(PdfName.Differences, differences);
69+
NUnit.Framework.Assert.IsNull(initialFontDict.GetIndirectReference());
70+
using (PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) {
71+
// prevent no pages exception on close
72+
doc.AddNewPage();
73+
PdfType3Font font1 = (PdfType3Font)doc.GetFont(initialFontDict);
74+
NUnit.Framework.Assert.IsNotNull(font1);
75+
// prevent no glyphs for type3 font on close
76+
font1.AddGlyph('a', 0, 0, 0, 0, 0);
77+
}
78+
}
79+
5480
[NUnit.Framework.Test]
5581
public virtual void CopyPagesWithOCGDifferentNames() {
5682
IList<IList<String>> ocgNames = new List<IList<String>>();

0 commit comments

Comments
 (0)