Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle ZapfDingbats font for TrueTypeSimpleFont and add tests #969

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading