diff --git a/src/UglyToad.PdfPig.Fonts/TrueType/Tables/GlyphDataTable.cs b/src/UglyToad.PdfPig.Fonts/TrueType/Tables/GlyphDataTable.cs index 7681497c..4bfad7a1 100644 --- a/src/UglyToad.PdfPig.Fonts/TrueType/Tables/GlyphDataTable.cs +++ b/src/UglyToad.PdfPig.Fonts/TrueType/Tables/GlyphDataTable.cs @@ -240,9 +240,15 @@ flags = (CompositeGlyphFlags)data.ReadUnsignedShort(); var glyphIndex = data.ReadUnsignedShort(); - var childGlyph = glyphs[glyphIndex]; + if (glyphIndex >= glyphs.Length) + { + // Unsure why this happens but fixes #1213 + continue; // TODO - Is there a better fix? + } - if (childGlyph == null) + IGlyphDescription? childGlyph = glyphs[glyphIndex]; + + if (childGlyph is null) { if (!compositeLocations.TryGetValue(glyphIndex, out var missingComposite)) { @@ -319,7 +325,7 @@ } } - builderGlyph = builderGlyph ?? emptyGlyph; + builderGlyph ??= emptyGlyph; return new Glyph(false, builderGlyph.Instructions, builderGlyph.EndPointsOfContours, builderGlyph.Points, compositeLocation.Bounds); } @@ -426,7 +432,7 @@ } } - private class CompositeComponent + private sealed class CompositeComponent { public int Index { get; } diff --git a/src/UglyToad.PdfPig.Tests/Integration/Documents/GlyphDataTableReadCompositeGlyphError.pdf b/src/UglyToad.PdfPig.Tests/Integration/Documents/GlyphDataTableReadCompositeGlyphError.pdf new file mode 100644 index 00000000..e14e9079 Binary files /dev/null and b/src/UglyToad.PdfPig.Tests/Integration/Documents/GlyphDataTableReadCompositeGlyphError.pdf differ diff --git a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs index 4a73f8f9..e122ca40 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs @@ -11,6 +11,21 @@ public class GithubIssuesTests { + [Fact] + public void Issue1213() + { + var path = IntegrationHelpers.GetDocumentPath("GlyphDataTableReadCompositeGlyphError.pdf"); + + using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true })) + { + for (int p = 1; p <= document.NumberOfPages; p++) + { + var page = document.GetPage(p); + Assert.NotNull(page); + } + } + } + [Fact] public void Issue1208() {