diff --git a/src/UglyToad.PdfPig.Fonts/CompactFontFormat/Charsets/CompactFontFormatCharset.cs b/src/UglyToad.PdfPig.Fonts/CompactFontFormat/Charsets/CompactFontFormatCharset.cs index acf882e11..a59248823 100644 --- a/src/UglyToad.PdfPig.Fonts/CompactFontFormat/Charsets/CompactFontFormatCharset.cs +++ b/src/UglyToad.PdfPig.Fonts/CompactFontFormat/Charsets/CompactFontFormatCharset.cs @@ -27,7 +27,7 @@ protected CompactFontFormatCharset(ReadOnlySpan<(int glyphId, int stringId, stri public virtual string GetNameByGlyphId(int glyphId) { - return GlyphIdToStringIdAndName.TryGetValue(glyphId, out var strings) ? strings.name : ".notdef"; + return GlyphIdToStringIdAndName.TryGetValue(glyphId, out var strings) ? strings.name : null; } public virtual string GetNameByStringId(int stringId) diff --git a/src/UglyToad.PdfPig.Fonts/CompactFontFormat/CompactFontFormatFont.cs b/src/UglyToad.PdfPig.Fonts/CompactFontFormat/CompactFontFormatFont.cs index 1132a41b1..825e01c89 100644 --- a/src/UglyToad.PdfPig.Fonts/CompactFontFormat/CompactFontFormatFont.cs +++ b/src/UglyToad.PdfPig.Fonts/CompactFontFormat/CompactFontFormatFont.cs @@ -55,16 +55,6 @@ internal CompactFontFormatFont(CompactFontFormatTopLevelDictionary topDictionary /// public string GetCharacterName(int characterCode, bool isCid) { - if (Encoding is not null) - { - return Encoding.GetName(characterCode); - } - - if (Charset.IsCidCharset) - { - return Charset.GetNameByStringId(characterCode); - } - if (isCid) { if (this is not CompactFontFormatCidFont && !Charset.IsCidCharset) @@ -75,6 +65,16 @@ public string GetCharacterName(int characterCode, bool isCid) return Charset.GetNameByStringId(characterCode); } + if (Encoding is not null) + { + return Encoding.GetName(characterCode); + } + + if (Charset.IsCidCharset) + { + return Charset.GetNameByStringId(characterCode); + } + string characterName = GlyphList.AdobeGlyphList.UnicodeCodePointToName(characterCode); if (characterName.Equals(GlyphList.NotDefined, StringComparison.OrdinalIgnoreCase)) diff --git a/src/UglyToad.PdfPig.Fonts/TrueType/TrueTypeFont.cs b/src/UglyToad.PdfPig.Fonts/TrueType/TrueTypeFont.cs index f0def83a3..baa7108cd 100644 --- a/src/UglyToad.PdfPig.Fonts/TrueType/TrueTypeFont.cs +++ b/src/UglyToad.PdfPig.Fonts/TrueType/TrueTypeFont.cs @@ -138,18 +138,15 @@ public bool TryGetBoundingBox(int characterCode, Func characterCodeTo * This is to fix P2P-33713919.pdf * See https://github.com/BobLd/PdfPig.Rendering.Skia/issues/46 * - * A CFF that carries its own encoding selects glyphs from the character code - * directly. Without an encoding (e.g. an OpenType/CFF embedded as a CIDFont) the - * CID must first be mapped to a glyph index via the CIDToGIDMap (Identity when none), - * then looked up by glyph id in the charset. + * When used as a CIDFont the CID is first mapped to a glyph index via the supplied + * mapping (the CIDToGIDMap, or Identity when none), then resolved by glyph id in the + * embedded CFF (see CompactFontFormatFont.GetCharacterName). * * See https://github.com/UglyToad/PdfPig/issues/1320 */ var cffFont = cffFontCollection.FirstFont; - int cffGlyphId = cffFont.Encoding is null - ? (characterCodeToGlyphId?.Invoke(characterCode) ?? characterCode) - : characterCode; + int cffGlyphId = characterCodeToGlyphId?.Invoke(characterCode) ?? characterCode; var name = cffFont.GetCharacterName(cffGlyphId, true); if (string.IsNullOrEmpty(name)) { @@ -212,18 +209,15 @@ public bool TryGetPath(int characterCode, Func characterCodeToGlyphId * This is to fix P2P-33713919.pdf * See https://github.com/BobLd/PdfPig.Rendering.Skia/issues/46 * - * A CFF that carries its own encoding selects glyphs from the character code - * directly. Without an encoding (e.g. an OpenType/CFF embedded as a CIDFont) the - * CID must first be mapped to a glyph index via the CIDToGIDMap (Identity when none), - * then looked up by glyph id in the charset. + * When used as a CIDFont the CID is first mapped to a glyph index via the supplied + * mapping (the CIDToGIDMap, or Identity when none), then resolved by glyph id in the + * embedded CFF (see CompactFontFormatFont.GetCharacterName). * * See https://github.com/UglyToad/PdfPig/issues/1320 */ var cffFont = cffFontCollection.FirstFont; - int cffGlyphId = cffFont.Encoding is null - ? (characterCodeToGlyphId?.Invoke(characterCode) ?? characterCode) - : characterCode; + int cffGlyphId = characterCodeToGlyphId?.Invoke(characterCode) ?? characterCode; var name = cffFont.GetCharacterName(cffGlyphId, true); if (string.IsNullOrEmpty(name)) { diff --git a/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/P2P-33713919.pdf_2.png b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/P2P-33713919.pdf_2.png index 2472afc31..336c9ec21 100644 Binary files a/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/P2P-33713919.pdf_2.png and b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/P2P-33713919.pdf_2.png differ diff --git a/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/P2P-33713919.pdf_2_loose.png b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/P2P-33713919.pdf_2_loose.png index de2c46068..aadefc5ab 100644 Binary files a/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/P2P-33713919.pdf_2_loose.png and b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/P2P-33713919.pdf_2_loose.png differ diff --git a/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/issues-1176-1.pdf_1.png b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/issues-1176-1.pdf_1.png new file mode 100644 index 000000000..2d23f891f Binary files /dev/null and b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/issues-1176-1.pdf_1.png differ diff --git a/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/issues-1176-1.pdf_1_loose.png b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/issues-1176-1.pdf_1_loose.png new file mode 100644 index 000000000..f1b5cb771 Binary files /dev/null and b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/ExpectedGlyphs/issues-1176-1.pdf_1_loose.png differ diff --git a/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/GenerateLetterGlyphImages.cs b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/GenerateLetterGlyphImages.cs index d41edc842..b0422106b 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/GenerateLetterGlyphImages.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/VisualVerification/GenerateLetterGlyphImages.cs @@ -202,6 +202,12 @@ SKPoint GetPoint(PdfPoint p) graphics.DrawPoint(GetPoint(point), pen); } + [Fact] + public void issues_1176_1() + { + Run("issues-1176-1.pdf"); + } + [Fact] public void P2P_33713919() {