performance tweak and test case for #334

This commit is contained in:
Eliot Jones
2021-06-10 16:31:16 -04:00
parent 8163163dfc
commit b3a361e2f2
2 changed files with 23 additions and 4 deletions

View File

@@ -860,8 +860,8 @@ namespace UglyToad.PdfPig.Fonts.CompactFontFormat.CharStrings
return new Type2CharStrings.CommandSequence.CommandIdentifier(precedingValues.Count, false, 255); return new Type2CharStrings.CommandSequence.CommandIdentifier(precedingValues.Count, false, 255);
} }
private static int CalculatePrecedingHintBytes(IReadOnlyList<float> precedingValues, private static int CalculatePrecedingHintBytes(List<float> precedingValues,
IReadOnlyList<Type2CharStrings.CommandSequence.CommandIdentifier> precedingCommands) List<Type2CharStrings.CommandSequence.CommandIdentifier> precedingCommands)
{ {
int SafeStemCount(int counts) int SafeStemCount(int counts)
{ {
@@ -891,8 +891,14 @@ namespace UglyToad.PdfPig.Fonts.CompactFontFormat.CharStrings
precedingNumbers++; precedingNumbers++;
} }
foreach (var identifier in precedingCommands.Where(x => x.CommandIndex == i + 1)) for (var j = 0; j < precedingCommands.Count; j++)
{ {
var identifier = precedingCommands[j];
if (identifier.CommandIndex != i + 1)
{
continue;
}
if (!identifier.IsMultiByteCommand if (!identifier.IsMultiByteCommand
&& (identifier.CommandId == HintmaskByte || identifier.CommandId == CntrmaskByte) && (identifier.CommandId == HintmaskByte || identifier.CommandId == CntrmaskByte)
&& !hasEncounteredInitialHintMask) && !hasEncounteredInitialHintMask)

View File

@@ -4,6 +4,7 @@
using Logging; using Logging;
using PdfPig.Core; using PdfPig.Core;
using PdfPig.Parser.FileStructure; using PdfPig.Parser.FileStructure;
using PdfPig.Tokenization.Scanner;
using Xunit; using Xunit;
public class FileHeaderParserTests public class FileHeaderParserTests
@@ -137,5 +138,17 @@ three %PDF-1.6");
Assert.Equal(0, scanner.CurrentPosition); Assert.Equal(0, scanner.CurrentPosition);
Assert.Equal(0, result.OffsetInFile); Assert.Equal(0, result.OffsetInFile);
} }
[Fact]
public void Issue334()
{
var input = OtherEncodings.StringAsLatin1Bytes("%PDF-1.7\r\n%âãÏÓ\r\n1 0 obj\r\n<</Lang(en-US)>>\r\nendobj");
var scanner = new CoreTokenScanner(new ByteArrayInputBytes(input), ScannerScope.None);
var result = FileHeaderParser.Parse(scanner, false, log);
Assert.Equal(1.7m, result.Version);
}
} }
} }