From 957140216667f7598b181954a5ccbf7e44168ad5 Mon Sep 17 00:00:00 2001 From: BobLd <38405645+BobLd@users.noreply.github.com> Date: Sat, 27 Jun 2026 14:07:58 +0100 Subject: [PATCH] Accept out of range characterCode in type 3 font and fix #1349 --- .../Integration/Documents/Type3Test.pdf | Bin 0 -> 2693 bytes .../Integration/GithubIssuesTests.cs | 12 ++++++++++++ src/UglyToad.PdfPig/PdfFonts/Simple/Type3Font.cs | 7 ++++++- 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/UglyToad.PdfPig.Tests/Integration/Documents/Type3Test.pdf diff --git a/src/UglyToad.PdfPig.Tests/Integration/Documents/Type3Test.pdf b/src/UglyToad.PdfPig.Tests/Integration/Documents/Type3Test.pdf new file mode 100644 index 0000000000000000000000000000000000000000..ad26152bccce951b6b1b721d95d66f763ea8684a GIT binary patch literal 2693 zcmcIm+iu%95PcV*e;5=HkQ8YxS&^MAngF@j#bVPoYkSd$pbxY~+iX?QrD!ziPxfK| zVE0G%3@Q1NxP4f-gmv+7=FEMl==Qq*B6-;jgXo`szyBQuVUSQvdi*gAUcI8$=wTtr zt=5&WqR_K2z~9!bn90h~+h8Ar!TEU@$TD|=VZg*E7+%QB|Dkdxen%n~9Ze%&LbLu9UXRWM};xlitX$hGb z8|qg?m&H<2smm8&)1Op1ql2j`ibLk-L_zuMhMOp&fy}949dsqU6kJlSkBc6tZ(!dJ7~% z(g3p7YyT(1>(>Sv&xkJakITwJQdML%u#YIiCGEsZ9FQ2(Sl~VuhcrTEdiv}e(SW0GJYnW)q*!-bCXU_cz4|FzE`=O*Xo=PNsM{qZZK+lda%wP2dy!% zR(q|cQ;9D1GYfvhtXB19DU75R!Xe68AvsFhI@sm4j+(#rnNBvGUP07O`>ky-%nHt3 zO2a?@x)}6mRy-``l*?*j)WRxV(wPqw1J_#F{AMh+yGS)dHU;G3!gJ2&nDd+`Fd(A0 z??~P+3N=xdCc40|#=yb$W>Yk7fW24q77tWOONA;WedSb;s9@j%l}a!VU{h^2X6PLy z0OjigY{-9%n2T!8)$(&Vj28U5mDThSKvAFT!g3V$h$+U34NM${(tCkw8)l>x0lfPSEI4)O*(tgZ}^>;f0U@ literal 0 HcmV?d00001 diff --git a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs index ad86ea028..dad8be9a8 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs @@ -11,6 +11,18 @@ public class GithubIssuesTests { + [Fact] + public void Issues1349() + { + var path = IntegrationHelpers.GetDocumentPath("Type3Test.pdf"); + using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true })) + { + var page = document.GetPage(1); + Assert.NotNull(page); + Assert.NotNull(page.Text); + } + } + [Fact] public void Issues1330() { diff --git a/src/UglyToad.PdfPig/PdfFonts/Simple/Type3Font.cs b/src/UglyToad.PdfPig/PdfFonts/Simple/Type3Font.cs index 34afa1749..b84d98c4b 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Simple/Type3Font.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Simple/Type3Font.cs @@ -136,7 +136,12 @@ private PdfRectangle GetBoundingBoxInGlyphSpace(int characterCode) { if (characterCode < firstChar || characterCode > lastChar) { - throw new InvalidFontFormatException($"The character code was not contained in the widths array: {characterCode}."); + // The code is outside the Widths array range. This is legal: a content + // stream may show codes (e.g. space, 32) that the font does not define a + // glyph for. Such codes have no glyph to paint (no CharProc), so report an + // empty bounding box rather than throwing. The advance width still comes + // from GetWidth, which falls back to the FontBBox width for these codes. + return new PdfRectangle(0, 0, boundingBox.Right, 0); } // The CharProc's d1 operator would declare a precise per-glyph