diff --git a/src/UglyToad.PdfPig.Tests/Fonts/Type1/Type1FontParserTests.cs b/src/UglyToad.PdfPig.Tests/Fonts/Type1/Type1FontParserTests.cs index b8da5ad9..b2a32dc1 100644 --- a/src/UglyToad.PdfPig.Tests/Fonts/Type1/Type1FontParserTests.cs +++ b/src/UglyToad.PdfPig.Tests/Fonts/Type1/Type1FontParserTests.cs @@ -63,7 +63,7 @@ var builder = new StringBuilder(""); foreach (var charString in result.CharStrings.CharStrings) { - var path = result.CharStrings.Generate(charString.Key); + Assert.True(result.CharStrings.TryGenerate(charString.Key, out var path)); builder.AppendLine(path.ToFullSvg()); } diff --git a/src/UglyToad.PdfPig/Fonts/Type1/CharStrings/Type1CharStrings.cs b/src/UglyToad.PdfPig/Fonts/Type1/CharStrings/Type1CharStrings.cs index 201a64db..bad60633 100644 --- a/src/UglyToad.PdfPig/Fonts/Type1/CharStrings/Type1CharStrings.cs +++ b/src/UglyToad.PdfPig/Fonts/Type1/CharStrings/Type1CharStrings.cs @@ -25,27 +25,27 @@ Subroutines = subroutines ?? throw new ArgumentNullException(nameof(subroutines)); } - public PdfPath Generate(string name) + public bool TryGenerate(string name, out PdfPath path) { - PdfPath glyph; + path = default(PdfPath); lock (locker) { - if (glyphs.TryGetValue(name, out var result)) + if (glyphs.TryGetValue(name, out path)) { - return result; + return true; } if (!CharStrings.TryGetValue(name, out var sequence)) { - throw new InvalidOperationException($"No charstring sequence with the name /{name} in this font."); + return false; } - glyph = Run(sequence); + path = Run(sequence); - glyphs[name] = glyph; + glyphs[name] = path; } - return glyph; + return true; } private PdfPath Run(CommandSequence sequence) diff --git a/src/UglyToad.PdfPig/Fonts/Type1/Type1FontProgram.cs b/src/UglyToad.PdfPig/Fonts/Type1/Type1FontProgram.cs index ac4579dc..7c7e2bbb 100644 --- a/src/UglyToad.PdfPig/Fonts/Type1/Type1FontProgram.cs +++ b/src/UglyToad.PdfPig/Fonts/Type1/Type1FontProgram.cs @@ -2,7 +2,6 @@ { using System; using System.Collections.Generic; - using System.Diagnostics; using CharStrings; using Core; using Geometry; @@ -62,7 +61,11 @@ public PdfRectangle? GetCharacterBoundingBox(string characterName) { - var glyph = CharStrings.Generate(characterName); + if (!CharStrings.TryGenerate(characterName, out var glyph)) + { + return null; + } + var bbox = glyph.GetBoundingRectangle(); return bbox; @@ -77,7 +80,7 @@ { if (FontMatrix == null || FontMatrix.Data.Count != 6) { - return TransformationMatrix.FromValues(0.001m, 0, 0, 0.001m, 0, 0);; + return TransformationMatrix.FromValues(0.001m, 0, 0, 0.001m, 0, 0); } var a = ((NumericToken) FontMatrix.Data[0]).Data; diff --git a/src/UglyToad.PdfPig/Tokenization/DictionaryTokenizer.cs b/src/UglyToad.PdfPig/Tokenization/DictionaryTokenizer.cs index 0f21012a..68498ec9 100644 --- a/src/UglyToad.PdfPig/Tokenization/DictionaryTokenizer.cs +++ b/src/UglyToad.PdfPig/Tokenization/DictionaryTokenizer.cs @@ -94,6 +94,12 @@ result[key] = token; } + // skip def. + if (PeekNext(tokens, i) == OperatorToken.Def) + { + i++; + } + key = null; }