fix null reference bug and handle escaped escape characters in string tokenization

This commit is contained in:
Eliot Jones
2019-05-11 15:35:56 +01:00
parent 72ef0f174a
commit 5b5a0b7f55
4 changed files with 44 additions and 1 deletions

View File

@@ -6,6 +6,7 @@
using PdfPig.Graphics; using PdfPig.Graphics;
using PdfPig.Graphics.Core; using PdfPig.Graphics.Core;
using PdfPig.Graphics.Operations.General; using PdfPig.Graphics.Operations.General;
using PdfPig.Graphics.Operations.SpecialGraphicsState;
using PdfPig.Graphics.Operations.TextObjects; using PdfPig.Graphics.Operations.TextObjects;
using PdfPig.Graphics.Operations.TextPositioning; using PdfPig.Graphics.Operations.TextPositioning;
using PdfPig.Graphics.Operations.TextShowing; using PdfPig.Graphics.Operations.TextShowing;
@@ -153,6 +154,33 @@ ET";
Assert.Equal(EndText.Value, result[3]); Assert.Equal(EndText.Value, result[3]);
} }
[Fact]
public void HandlesEscapedLineBreaks()
{
const string s = @"q 1 0 0 1 48 434
cm BT 0.0001 Tc 19 0 0 19 0 0 Tm /Tc1 1 Tf ( \(sleep 1; printf ""QUIT\\r\\n""\) | )
Tj ET Q";
var input = StringBytesTestConverter.Convert(s, false);
var result = parser.Parse(input.Bytes);
Assert.Equal(9, result.Count);
Assert.IsType<Push>(result[0]);
Assert.IsType<ModifyCurrentTransformationMatrix>(result[1]);
Assert.IsType<BeginText>(result[2]);
Assert.IsType<SetCharacterSpacing>(result[3]);
Assert.IsType<SetTextMatrix>(result[4]);
Assert.IsType<SetFontAndSize>(result[5]);
var text = Assert.IsType<ShowText>(result[6]);
Assert.IsType<EndText>(result[7]);
Assert.IsType<Pop>(result[8]);
Assert.Equal(@" (sleep 1; printf ""QUIT\r\n"") | ", text.Text);
}
private static string LineEndingsToWhiteSpace(string str) private static string LineEndingsToWhiteSpace(string str)
{ {
return str.Replace("\r\n", " ").Replace('\n', ' ').Replace('\r', ' '); return str.Replace("\r\n", " ").Replace('\n', ' ').Replace('\r', ' ');

View File

@@ -247,6 +247,20 @@ are the same.)";
Assert.Equal(@"this does not end with bracket", AssertStringToken(token).Data); Assert.Equal(@"this does not end with bracket", AssertStringToken(token).Data);
} }
[Fact]
public void HandlesEscapedEscapeCharacters()
{
const string s = @"( \(sleep 1; printf ""QUIT\\r\\n""\) | )";
var input = StringBytesTestConverter.Convert(s);
var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token);
Assert.True(result);
Assert.Equal(@" (sleep 1; printf ""QUIT\r\n"") | ", AssertStringToken(token).Data);
}
private static StringToken AssertStringToken(IToken token) private static StringToken AssertStringToken(IToken token)
{ {
Assert.NotNull(token); Assert.NotNull(token);

View File

@@ -98,7 +98,7 @@
} }
Encoding fromFont = null; Encoding fromFont = null;
font.Match(x => fromFont = x.Encoding != null ? new BuiltInEncoding(x.Encoding) : default(Encoding), x => font?.Match(x => fromFont = x.Encoding != null ? new BuiltInEncoding(x.Encoding) : default(Encoding), x =>
{ {
if (x.Fonts != null && x.Fonts.Count > 0) if (x.Fonts != null && x.Fonts.Count > 0)
{ {

View File

@@ -109,6 +109,7 @@
if (isEscapeActive) if (isEscapeActive)
{ {
builder.Append(c); builder.Append(c);
isEscapeActive = false;
} }
else else
{ {