From 04f785aed407cc9c67c25f4b5f06940d2cd46359 Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Sat, 22 Aug 2020 15:38:59 +0100 Subject: [PATCH] attempt to find bug in #202 due to seemingly empty codespace ranges --- src/UglyToad.PdfPig/PdfFonts/Cmap/CMap.cs | 34 +++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/UglyToad.PdfPig/PdfFonts/Cmap/CMap.cs b/src/UglyToad.PdfPig/PdfFonts/Cmap/CMap.cs index 72e8d0ad..074c369a 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Cmap/CMap.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Cmap/CMap.cs @@ -60,10 +60,18 @@ public bool HasUnicodeMappings => BaseFontCharacterMap.Count > 0; - private readonly int minCodeLength; + /// + /// Issue #202 seems to indicate empty codespace ranges are possible. + /// + private readonly bool hasEmptyCodespace; + private readonly int minCodeLength = 4; private readonly int maxCodeLength; - public CMap(CharacterIdentifierSystemInfo info, int type, int wMode, string name, string version, IReadOnlyDictionary baseFontCharacterMap, IReadOnlyList codespaceRanges, IReadOnlyList cidRanges, IReadOnlyList cidCharacterMappings) + public CMap(CharacterIdentifierSystemInfo info, int type, int wMode, string name, string version, + IReadOnlyDictionary baseFontCharacterMap, + IReadOnlyList codespaceRanges, + IReadOnlyList cidRanges, + IReadOnlyList cidCharacterMappings) { if (cidCharacterMappings == null) { @@ -78,8 +86,17 @@ BaseFontCharacterMap = baseFontCharacterMap ?? throw new ArgumentNullException(nameof(baseFontCharacterMap)); CodespaceRanges = codespaceRanges ?? throw new ArgumentNullException(nameof(codespaceRanges)); CidRanges = cidRanges ?? throw new ArgumentNullException(nameof(cidRanges)); - maxCodeLength = CodespaceRanges.Max(x => x.CodeLength); - minCodeLength = CodespaceRanges.Min(x => x.CodeLength); + + // Issue #202 possible empty codespace ranges? + if (CodespaceRanges.Count > 0) + { + maxCodeLength = CodespaceRanges.Max(x => x.CodeLength); + minCodeLength = CodespaceRanges.Min(x => x.CodeLength); + } + else + { + hasEmptyCodespace = true; + } var characterMappings = new Dictionary(); @@ -135,6 +152,13 @@ public int ReadCode(IInputBytes bytes) { + if (hasEmptyCodespace) + { + var data = new byte[minCodeLength]; + bytes.Read(data); + return data.ToInt(minCodeLength); + } + byte[] result = new byte[maxCodeLength]; result[0] = bytes.CurrentByte; @@ -165,7 +189,7 @@ } } - throw new InvalidOperationException("CMap is invalid"); + throw new PdfDocumentFormatException($"CMap is invalid, min code length was {minCodeLength}, max was {maxCodeLength}."); } private static byte ReadByte(IInputBytes bytes)