From 00b9d416df5b379d1a24ec0bbad6107f8ec16bce Mon Sep 17 00:00:00 2001 From: Adam Busbin Date: Thu, 23 Apr 2020 14:58:48 -0700 Subject: [PATCH] added check for bad fonts see https://github.com/apache/pdfbox/blob/61ceca8376f08f23f62178bd4fd97e919a690e43/fontbox/src/main/java/org/apache/fontbox/ttf/HorizontalMetricsTable.java line 67 for matching code. --- .../TrueType/Parser/HorizontalMetricsTableParser.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/UglyToad.PdfPig.Fonts/TrueType/Parser/HorizontalMetricsTableParser.cs b/src/UglyToad.PdfPig.Fonts/TrueType/Parser/HorizontalMetricsTableParser.cs index 98ca5926..cb1f8ba7 100644 --- a/src/UglyToad.PdfPig.Fonts/TrueType/Parser/HorizontalMetricsTableParser.cs +++ b/src/UglyToad.PdfPig.Fonts/TrueType/Parser/HorizontalMetricsTableParser.cs @@ -26,10 +26,18 @@ bytesRead += 4; } - + + int numberNonHorizontal = glyphCount - metricCount; + + // handle bad fonts with too many hmetrics + if (numberNonHorizontal < 0) + { + numberNonHorizontal = glyphCount; + } + // The number of entries in the left side bearing field per entry is number of glyphs - number of metrics // For bearings over the metric count, the width is the same as the last width in advanced widths. - var additionalLeftSideBearings = new short[glyphCount - metricCount]; + var additionalLeftSideBearings = new short[numberNonHorizontal]; for (var i = 0; i < additionalLeftSideBearings.Length; i++) {