Allow to have empty code space, fixes #256 (#257)

This commit is contained in:
Wesley Moret 2021-01-08 15:50:33 -05:00 committed by GitHub
parent 19ac38bf8b
commit 7239396665
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View File

@ -4,6 +4,9 @@
using System.Linq;
using PdfFonts.Cmap;
using PdfPig.Tokens;
using UglyToad.PdfPig.Core;
using UglyToad.PdfPig.PdfFonts.Parser.Parts;
using UglyToad.PdfPig.Tokenization.Scanner;
using Xunit;
public class CodespaceRangeTests
@ -100,6 +103,29 @@
Assert.True(matches);
}
[Fact]
public void ColorspaceParserError()
{
var parser = new CodespaceRangeParser();
var byteArrayInput = new ByteArrayInputBytes(OtherEncodings.StringAsLatin1Bytes("1 begincodespacerange\nendcodespacerange"));
var tokenScanner = new CoreTokenScanner(byteArrayInput);
Assert.True(tokenScanner.MoveNext());
Assert.True(tokenScanner.CurrentToken is NumericToken);
var numeric = (NumericToken) tokenScanner.CurrentToken;
Assert.True(tokenScanner.MoveNext());
Assert.True(tokenScanner.CurrentToken is OperatorToken);
var opToken = (OperatorToken)tokenScanner.CurrentToken;
Assert.Equal("begincodespacerange", opToken.Data);
var cmapBuilder = new CharacterMapBuilder();
parser.Parse(numeric, tokenScanner, cmapBuilder);
Assert.Empty(cmapBuilder.CodespaceRanges);
}
private static byte[] GetHexBytes(params char[] characters)
{
var token = new HexToken(characters);

View File

@ -23,7 +23,18 @@
for (var i = 0; i < numeric.Int; i++)
{
if (!tokenScanner.MoveNext() || !(tokenScanner.CurrentToken is HexToken start))
if (!tokenScanner.MoveNext())
{
throw new InvalidOperationException("Codespace range have reach an unexpected end");
}
if (tokenScanner.CurrentToken is OperatorToken operatorToken && operatorToken.Data == "endcodespacerange")
{
// Don't add this code space range
break;
}
if (!(tokenScanner.CurrentToken is HexToken start))
{
throw new InvalidOperationException("Codespace range contains an unexpected token: " + tokenScanner.CurrentToken);
}