Skip to content

Commit

Permalink
Properly handle ZapfDingbats font for TrueTypeSimpleFont and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BobLd committed Jan 5, 2025
1 parent 4430a01 commit 53cf4f2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Binary file not shown.
14 changes: 11 additions & 3 deletions src/UglyToad.PdfPig.Tests/Integration/ZapfDingbatsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

public class ZapfDingbatsTests
{
[Fact]
public void TrueTypeSimpleFont1()
{
using (var document = PdfDocument.Open(IntegrationHelpers.GetDocumentPath("capas")))
{
var page = document.GetPage(18);
// ZapfDingbats characters are spaces
Assert.Contains(" ", page.Letters.Select(l => l.Value));
}
}

[Fact]
public void Type1Standard14Font1()
{
Expand All @@ -17,7 +28,6 @@ public void Type1Standard14Font1()
[Fact]
public void Type1Standard14Font2()
{
// This document does not actually contain circular references
using (var document = PdfDocument.Open(IntegrationHelpers.GetDocumentPath("MOZILLA-LINK-5251-1")))
{
var page = document.GetPage(1);
Expand All @@ -33,7 +43,6 @@ public void Type1Standard14Font2()
[Fact]
public void Type1FontSimple1()
{
// This document does not actually contain circular references
using (var document = PdfDocument.Open(IntegrationHelpers.GetDocumentPath("MOZILLA-2775-1")))
{
var page = document.GetPage(11);
Expand All @@ -44,7 +53,6 @@ public void Type1FontSimple1()
[Fact]
public void Type1FontSimple2()
{
// This document does not actually contain circular references
using (var document = PdfDocument.Open(IntegrationHelpers.GetDocumentPath("PDFBOX-492-4.jar-8")))
{
var page = document.GetPage(1);
Expand Down
19 changes: 15 additions & 4 deletions src/UglyToad.PdfPig/PdfFonts/Simple/TrueTypeSimpleFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ internal sealed class TrueTypeSimpleFont : IFont

private readonly double[] widths;

private readonly bool isZapfDingbats;

#nullable disable
public NameToken Name { get; }
#nullable enable
Expand Down Expand Up @@ -63,8 +65,7 @@ public TrueTypeSimpleFont(
Details = descriptor?.ToDetails(Name?.Data)
?? FontDetails.GetDefault(Name?.Data);

// Assumption is ZapfDingbats is not possible here. We need to change the behaviour if not the case
System.Diagnostics.Debug.Assert(!(encoding is ZapfDingbatsEncoding || Details.Name.Contains("ZapfDingbats")));
isZapfDingbats = encoding is ZapfDingbatsEncoding || Details.Name.Contains("ZapfDingbats");
}

public int ReadCharacterCode(IInputBytes bytes, out int codeLength)
Expand Down Expand Up @@ -100,12 +101,22 @@ public bool TryGetUnicode(int characterCode, [NotNullWhen(true)] out string? val
// If the font is a simple font that uses one of the predefined encodings MacRomanEncoding, MacExpertEncoding, or WinAnsiEncoding...

// Map the character code to a character name.
var encodedCharacterName = encoding.GetName(characterCode);
var name = encoding.GetName(characterCode);

// Look up the character name in the Adobe Glyph List or additional Glyph List.
try
{
value = GlyphList.AdobeGlyphList.NameToUnicode(encodedCharacterName);
if (isZapfDingbats)
{
value = GlyphList.ZapfDingbats.NameToUnicode(name);

if (value is not null)
{
return true;
}
}

value = GlyphList.AdobeGlyphList.NameToUnicode(name);
}
catch
{
Expand Down

0 comments on commit 53cf4f2

Please sign in to comment.