From 29061b1fd2b70e5803610b9b7272ee018707f6fb Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Tue, 28 Jan 2020 16:05:53 +0000 Subject: [PATCH] handle unexpected adobe type 1 format an encoding array in an adobe type 1 font may be missing its declaration ending in 'for', if we encounter 'dup' while looking for the 'for' token we have a special case to go straight into reading the encoding. also handles a case where the page content stream contains a path-closing operator without any path being active. --- .../Type1/Parser/Type1FontParser.cs | 28 +++++++++++++++++-- .../Graphics/ContentStreamProcessor.cs | 10 +++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/UglyToad.PdfPig.Fonts/Type1/Parser/Type1FontParser.cs b/src/UglyToad.PdfPig.Fonts/Type1/Parser/Type1FontParser.cs index f05d27dd..38c6d22c 100644 --- a/src/UglyToad.PdfPig.Fonts/Type1/Parser/Type1FontParser.cs +++ b/src/UglyToad.PdfPig.Fonts/Type1/Parser/Type1FontParser.cs @@ -316,18 +316,42 @@ return (new ArrayToken(result), null); } + var stoppedOnDup = false; + while (scanner.MoveNext() && (!(scanner.CurrentToken is OperatorToken forOperator) || forOperator.Data != "for")) { + if (!(scanner.CurrentToken is OperatorToken operatorToken)) + { + continue; + } + + if (string.Equals(operatorToken.Data, "for", StringComparison.OrdinalIgnoreCase)) + { + break; + } + + if (string.Equals(operatorToken.Data, OperatorToken.Dup.Data, StringComparison.OrdinalIgnoreCase)) + { + stoppedOnDup = true; + break; + } // skip these operators for now, they're probably important... } - if (scanner.CurrentToken != OperatorToken.For) + if (scanner.CurrentToken != OperatorToken.For && !stoppedOnDup) { return (new ArrayToken(result), null); } - while (scanner.MoveNext() && scanner.CurrentToken != OperatorToken.Def && scanner.CurrentToken != OperatorToken.Readonly) + bool IsDefOrReadonly() { + return scanner.CurrentToken == OperatorToken.Def + || scanner.CurrentToken == OperatorToken.Readonly; + } + + while ((stoppedOnDup || scanner.MoveNext()) && !IsDefOrReadonly()) + { + stoppedOnDup = false; if (scanner.CurrentToken != OperatorToken.Dup) { throw new InvalidFontFormatException("Expected the array for encoding to begin with 'dup'."); diff --git a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs index 2f029409..fbd0d00d 100644 --- a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs @@ -413,6 +413,11 @@ public void StrokePath(bool close) { + if (CurrentPath == null) + { + return; + } + if (close) { ClosePath(); @@ -427,6 +432,11 @@ public void FillPath(bool close) { + if (CurrentPath == null) + { + return; + } + if (close) { ClosePath();