mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
handle messed up numbers in content #355
This commit is contained in:
@@ -96,6 +96,18 @@
|
|||||||
Assert.Equal(0m, AssertNumericToken(token).Data);
|
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]
|
[Fact]
|
||||||
public void HandlesDot()
|
public void HandlesDot()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -170,6 +170,32 @@ endobj";
|
|||||||
AssertCorrectToken<NumericToken, decimal>(tokens[3], -91);
|
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>
|
private static void AssertCorrectToken<T, TData>(IToken token, TData expected) where T : IDataToken<TData>
|
||||||
{
|
{
|
||||||
var cast = Assert.IsType<T>(token);
|
var cast = Assert.IsType<T>(token);
|
||||||
|
|||||||
@@ -20,28 +20,45 @@
|
|||||||
token = null;
|
token = null;
|
||||||
|
|
||||||
StringBuilder characters;
|
StringBuilder characters;
|
||||||
|
var initialSymbol = currentByte == '-' || currentByte == '+';
|
||||||
|
|
||||||
if ((currentByte >= Zero && currentByte <= Nine) || currentByte == '-' || currentByte == '+' || currentByte == '.')
|
if ((currentByte >= Zero && currentByte <= Nine) || currentByte == '.')
|
||||||
{
|
{
|
||||||
characters = stringBuilder;
|
characters = stringBuilder;
|
||||||
characters.Append((char)currentByte);
|
characters.Append((char)currentByte);
|
||||||
}
|
}
|
||||||
|
else if (initialSymbol)
|
||||||
|
{
|
||||||
|
characters = stringBuilder;
|
||||||
|
characters.Append((char) currentByte);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var previousSymbol = initialSymbol;
|
||||||
|
|
||||||
while (inputBytes.MoveNext())
|
while (inputBytes.MoveNext())
|
||||||
{
|
{
|
||||||
var b = inputBytes.CurrentByte;
|
var b = inputBytes.CurrentByte;
|
||||||
|
|
||||||
if ((b >= Zero && b <= Nine) ||
|
if (b == '+' || b == '-')
|
||||||
b == '-' ||
|
|
||||||
b == '+' ||
|
|
||||||
b == '.' ||
|
|
||||||
b == 'E' ||
|
|
||||||
b == 'e')
|
|
||||||
{
|
{
|
||||||
|
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);
|
characters.Append((char)b);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -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"}");
|
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:
|
case MoveToNextLineWithOffset.Symbol:
|
||||||
if (operands.Count < 2)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new MoveToNextLineWithOffset(OperandToDecimal(operands[0]), OperandToDecimal(operands[1]));
|
return new MoveToNextLineWithOffset(OperandToDecimal(operands[0]), OperandToDecimal(operands[1]));
|
||||||
case MoveToNextLineWithOffsetSetLeading.Symbol:
|
case MoveToNextLineWithOffsetSetLeading.Symbol:
|
||||||
if (operands.Count < 2)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new MoveToNextLineWithOffsetSetLeading(OperandToDecimal(operands[0]), OperandToDecimal(operands[1]));
|
return new MoveToNextLineWithOffsetSetLeading(OperandToDecimal(operands[0]), OperandToDecimal(operands[1]));
|
||||||
case PaintShading.Symbol:
|
case PaintShading.Symbol:
|
||||||
return new PaintShading((NameToken)operands[0]);
|
return new PaintShading((NameToken)operands[0]);
|
||||||
|
|||||||
Reference in New Issue
Block a user