handle messed up numbers in content #355

This commit is contained in:
Eliot Jones
2021-08-11 20:56:06 -04:00
parent a3e316958a
commit 1b472f6992
4 changed files with 63 additions and 18 deletions

View File

@@ -96,6 +96,18 @@
Assert.Equal(0m, AssertNumericToken(token).Data);
}
[Fact]
public void HandleDoubleDashedNumber()
{
// This is a really weird format but seen in the wild. PDF, shine on, you crazy diamond.
var input = StringBytesTestConverter.Convert("--10.25");
var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token);
Assert.True(result);
Assert.Equal(-10.25m, AssertNumericToken(token).Data);
}
[Fact]
public void HandlesDot()
{

View File

@@ -169,7 +169,33 @@ endobj";
AssertCorrectToken<OperatorToken, string>(tokens[2], "Tj");
AssertCorrectToken<NumericToken, decimal>(tokens[3], -91);
}
[Fact]
public void ScansStringWithWeirdWeirdDoubleSymbolNumerics()
{
const string content = @"
0.00 --21.72 TD
/F1 8.00 Tf";
var tokens = new List<IToken>();
var scanner = scannerFactory(StringBytesTestConverter.Convert(content, false).Bytes);
while (scanner.MoveNext())
{
tokens.Add(scanner.CurrentToken);
}
Assert.Equal(6, tokens.Count);
AssertCorrectToken<NumericToken, decimal>(tokens[0], 0m);
AssertCorrectToken<NumericToken, decimal>(tokens[1], -21.72m);
AssertCorrectToken<OperatorToken, string>(tokens[2], "TD");
AssertCorrectToken<NameToken, string>(tokens[3], "F1");
AssertCorrectToken<NumericToken, decimal>(tokens[4], 8m);
AssertCorrectToken<OperatorToken, string>(tokens[5], "Tf");
}
private static void AssertCorrectToken<T, TData>(IToken token, TData expected) where T : IDataToken<TData>
{
var cast = Assert.IsType<T>(token);

View File

@@ -20,28 +20,45 @@
token = null;
StringBuilder characters;
var initialSymbol = currentByte == '-' || currentByte == '+';
if ((currentByte >= Zero && currentByte <= Nine) || currentByte == '-' || currentByte == '+' || currentByte == '.')
if ((currentByte >= Zero && currentByte <= Nine) || currentByte == '.')
{
characters = stringBuilder;
characters.Append((char)currentByte);
}
else if (initialSymbol)
{
characters = stringBuilder;
characters.Append((char) currentByte);
}
else
{
return false;
}
var previousSymbol = initialSymbol;
while (inputBytes.MoveNext())
{
var b = inputBytes.CurrentByte;
if ((b >= Zero && b <= Nine) ||
b == '-' ||
b == '+' ||
b == '.' ||
b == 'E' ||
b == 'e')
if (b == '+' || b == '-')
{
if (previousSymbol)
{
continue;
}
characters.Append((char) b);
previousSymbol = true;
}
else if ((b >= Zero && b <= Nine) ||
b == '.' ||
b == 'E' ||
b == 'e')
{
previousSymbol = false;
characters.Append((char)b);
}
else

View File

@@ -271,18 +271,8 @@ namespace UglyToad.PdfPig.Graphics
throw new InvalidOperationException($"Tried to create a move to next line and show text operation with operand type: {operands[0]?.GetType().Name ?? "null"}");
}
case MoveToNextLineWithOffset.Symbol:
if (operands.Count < 2)
{
return null;
}
return new MoveToNextLineWithOffset(OperandToDecimal(operands[0]), OperandToDecimal(operands[1]));
case MoveToNextLineWithOffsetSetLeading.Symbol:
if (operands.Count < 2)
{
return null;
}
return new MoveToNextLineWithOffsetSetLeading(OperandToDecimal(operands[0]), OperandToDecimal(operands[1]));
case PaintShading.Symbol:
return new PaintShading((NameToken)operands[0]);