diff --git a/src/UglyToad.PdfPig.Tests/Integration/Documents/jtehm-melillo-2679746.pdf b/src/UglyToad.PdfPig.Tests/Integration/Documents/jtehm-melillo-2679746.pdf new file mode 100644 index 000000000..ad1e70e59 Binary files /dev/null and b/src/UglyToad.PdfPig.Tests/Integration/Documents/jtehm-melillo-2679746.pdf differ diff --git a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs index 6c343f8e9..f0d5b00a3 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs @@ -11,6 +11,25 @@ public class GithubIssuesTests { + [Fact] + public void Issues1248() + { + var path = IntegrationHelpers.GetDocumentPath("jtehm-melillo-2679746.pdf"); + using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true })) + { + var page = document.GetPage(1); + foreach (var letter in page.Letters) + { + var font = letter.GetFont(); + + if (font?.Name?.Data.Contains("TimesLT") == true) + { + Assert.True(font.TryGetPath(100, out _)); + } + } + } + } + [Fact] public void Issues1238() { diff --git a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type1FontHandler.cs b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type1FontHandler.cs index 34cb0775d..7301f37d2 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type1FontHandler.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type1FontHandler.cs @@ -125,7 +125,7 @@ public IFont Generate(DictionaryToken dictionary) { if (font.TryGetFirst(out var t1Font)) { - fromFont = t1Font.Encoding is not null ? new BuiltInEncoding(t1Font.Encoding) : default(Encoding); + fromFont = (t1Font.Encoding is not null && t1Font.Encoding.Count > 0) ? new BuiltInEncoding(t1Font.Encoding) : default(Encoding); } else if (font.TryGetSecond(out var cffFont)) { @@ -135,7 +135,7 @@ public IFont Generate(DictionaryToken dictionary) var encoding = encodingReader.Read(dictionary, descriptor, fromFont); - if (encoding is null && font != null && font.TryGetFirst(out var t1FontReplacement)) + if (encoding is null && font != null && font.TryGetFirst(out var t1FontReplacement) && t1FontReplacement.Encoding?.Count > 0) { encoding = new BuiltInEncoding(t1FontReplacement.Encoding); } diff --git a/src/UglyToad.PdfPig/PdfFonts/Simple/Type1FontSimple.cs b/src/UglyToad.PdfPig/PdfFonts/Simple/Type1FontSimple.cs index fefb1ddb5..a305ec56a 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Simple/Type1FontSimple.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Simple/Type1FontSimple.cs @@ -29,7 +29,7 @@ internal sealed class Type1FontSimple : IFont private readonly FontDescriptor fontDescriptor; - private readonly Encoding encoding; + private readonly Encoding? encoding; private readonly Union? fontProgram; @@ -67,7 +67,7 @@ public Type1FontSimple( var matrix = DefaultTransformationMatrix; - if (fontProgram != null) + if (fontProgram is not null) { if (fontProgram.TryGetFirst(out var t1Font)) { @@ -183,15 +183,7 @@ public bool TryGetUnicode(int characterCode, [NotNullWhen(true)] out string? val try { - if (isZapfDingbats) - { - value = GlyphList.ZapfDingbats.NameToUnicode(name); - if (value is not null) - { - return true; - } - } - value = GlyphList.AdobeGlyphList.NameToUnicode(name); + value = NameToUnicode(name); } catch { @@ -201,6 +193,34 @@ public bool TryGetUnicode(int characterCode, [NotNullWhen(true)] out string? val return value is not null; } + private string? NameToUnicode(string name) + { + if (isZapfDingbats) + { + string value = GlyphList.ZapfDingbats.NameToUnicode(name); + if (value is not null) + { + return value; + } + } + + return GlyphList.AdobeGlyphList.NameToUnicode(name); + } + + public string UnicodeCodePointToName(int unicodeValue) + { + if (isZapfDingbats) + { + string value = GlyphList.ZapfDingbats.UnicodeCodePointToName(unicodeValue); + if (value is not null) + { + return value; + } + } + + return GlyphList.AdobeGlyphList.UnicodeCodePointToName(unicodeValue); + } + public CharacterBoundingBox GetBoundingBox(int characterCode) { if (cachedBoundingBoxes.TryGetValue(characterCode, out var box)) @@ -255,7 +275,16 @@ private PdfRectangle GetBoundingBoxInGlyphSpace(int characterCode) PdfRectangle? rect = null; if (fontProgram.TryGetFirst(out var t1Font)) { - var name = encoding.GetName(characterCode); + string name; + if (encoding is not null) + { + name = encoding.GetName(characterCode); + } + else + { + name = UnicodeCodePointToName(characterCode); + } + rect = t1Font.GetCharacterBoundingBox(name); } else if (fontProgram.TryGetSecond(out var cffFont)) @@ -308,21 +337,30 @@ public bool TryGetPath(int characterCode, [NotNullWhen(true)] out IReadOnlyList< return false; } - if (fontProgram == null) + if (fontProgram is null) { return false; } if (fontProgram.TryGetFirst(out var t1Font)) { - var name = encoding.GetName(characterCode); + string name; + if (encoding is not null) + { + name = encoding.GetName(characterCode); + } + else + { + name = UnicodeCodePointToName(characterCode); + } + tempPath = t1Font.GetCharacterPath(name); } else if (fontProgram.TryGetSecond(out var cffFont)) { var first = cffFont.FirstFont; string characterName; - if (encoding != null) + if (encoding is not null) { characterName = encoding.GetName(characterCode); } @@ -334,7 +372,7 @@ public bool TryGetPath(int characterCode, [NotNullWhen(true)] out IReadOnlyList< tempPath = first.GetCharacterPath(characterName); } - if (tempPath != null) + if (tempPath is not null) { path = tempPath; return true;