Skip to content

Commit bf664c3

Browse files
committed
Use ReadOnlyMemory<byte> in ShowText operators and implement MoveToNextLineShowTextWithSpacing parsing
1 parent 6a50160 commit bf664c3

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

src/UglyToad.PdfPig.Tokens/HexToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public string GetHexString()
171171
#if NET8_0_OR_GREATER
172172
return Convert.ToHexString(Bytes);
173173
#else
174-
return BitConverter.ToString(Bytes.ToArray()).Replace("-", string.Empty);
174+
return BitConverter.ToString(_bytes).Replace("-", string.Empty);
175175
#endif
176176
}
177177
}

src/UglyToad.PdfPig/Graphics/Operations/TextShowing/MoveToNextLineShowText.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class MoveToNextLineShowText : IGraphicsStateOperation
2525
/// <summary>
2626
/// The text to show as hex bytes.
2727
/// </summary>
28-
public byte[]? Bytes { get; }
28+
public ReadOnlyMemory<byte> Bytes { get; }
2929

3030
/// <summary>
3131
/// Create a new <see cref="MoveToNextLineShowText"/>.
@@ -40,7 +40,7 @@ public MoveToNextLineShowText(string text)
4040
/// Create a new <see cref="MoveToNextLineShowText"/>.
4141
/// </summary>
4242
/// <param name="hexBytes">The bytes of the text to show.</param>
43-
public MoveToNextLineShowText(byte[] hexBytes)
43+
public MoveToNextLineShowText(ReadOnlyMemory<byte> hexBytes)
4444
{
4545
Bytes = hexBytes;
4646
}
@@ -50,7 +50,7 @@ public void Run(IOperationContext operationContext)
5050
{
5151
var move = MoveToNextLine.Value;
5252

53-
var showText = Text != null ? new ShowText(Text) : new ShowText(Bytes!);
53+
var showText = Text != null ? new ShowText(Text) : new ShowText(Bytes);
5454

5555
move.Run(operationContext);
5656
showText.Run(operationContext);
@@ -59,14 +59,14 @@ public void Run(IOperationContext operationContext)
5959
/// <inheritdoc />
6060
public void Write(Stream stream)
6161
{
62-
if (Bytes is null)
62+
if (Bytes.IsEmpty)
6363
{
6464
stream.WriteText($"({Text}) {Symbol}");
6565
stream.WriteNewLine();
6666
}
6767
else
6868
{
69-
stream.WriteHex(Bytes);
69+
stream.WriteHex(Bytes.Span);
7070
stream.WriteWhiteSpace();
7171
stream.WriteText(Symbol);
7272
stream.WriteNewLine();

src/UglyToad.PdfPig/Graphics/Operations/TextShowing/MoveToNextLineShowTextWithSpacing.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class MoveToNextLineShowTextWithSpacing : IGraphicsStateOperation
3131
/// <summary>
3232
/// The bytes of the text.
3333
/// </summary>
34-
public byte[]? Bytes { get; }
34+
public ReadOnlyMemory<byte> Bytes { get; }
3535

3636
/// <summary>
3737
/// The text to show.
@@ -57,7 +57,7 @@ public MoveToNextLineShowTextWithSpacing(double wordSpacing, double characterSpa
5757
/// <param name="wordSpacing">The word spacing.</param>
5858
/// <param name="characterSpacing">The character spacing.</param>
5959
/// <param name="hexBytes">The bytes of the text to show.</param>
60-
public MoveToNextLineShowTextWithSpacing(double wordSpacing, double characterSpacing, byte[] hexBytes)
60+
public MoveToNextLineShowTextWithSpacing(double wordSpacing, double characterSpacing, ReadOnlyMemory<byte> hexBytes)
6161
{
6262
WordSpacing = wordSpacing;
6363
CharacterSpacing = characterSpacing;
@@ -86,9 +86,9 @@ public void Write(Stream stream)
8686
stream.WriteDouble(CharacterSpacing);
8787
stream.WriteWhiteSpace();
8888

89-
if (Bytes != null)
89+
if (!Bytes.IsEmpty)
9090
{
91-
stream.WriteHex(Bytes);
91+
stream.WriteHex(Bytes.Span);
9292
}
9393
else
9494
{

src/UglyToad.PdfPig/Graphics/Operations/TextShowing/ShowText.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace UglyToad.PdfPig.Graphics.Operations.TextShowing
22
{
33
using PdfPig.Core;
4+
using System;
45
using System.IO;
56

67
/// <inheritdoc />
@@ -38,7 +39,7 @@ public class ShowText : IGraphicsStateOperation
3839
/// <summary>
3940
/// The bytes of the string to show.
4041
/// </summary>
41-
public byte[]? Bytes { get; }
42+
public ReadOnlyMemory<byte> Bytes { get; }
4243

4344
/// <summary>
4445
/// Create a new <see cref="ShowText"/>.
@@ -51,7 +52,7 @@ public ShowText(string text)
5152
/// <summary>
5253
/// Create a new <see cref="ShowText"/>.
5354
/// </summary>
54-
public ShowText(byte[] hexBytes)
55+
public ShowText(ReadOnlyMemory<byte> hexBytes)
5556
{
5657
Bytes = hexBytes;
5758
}
@@ -60,7 +61,6 @@ public ShowText(byte[] hexBytes)
6061
public void Run(IOperationContext operationContext)
6162
{
6263
var input = new MemoryInputBytes(Text != null ? OtherEncodings.StringAsLatin1Bytes(Text) : Bytes);
63-
6464
operationContext.ShowText(input);
6565
}
6666

@@ -89,10 +89,9 @@ public void Run(IOperationContext operationContext)
8989
/// <inheritdoc />
9090
public void Write(Stream stream)
9191
{
92-
93-
if (Bytes != null)
92+
if (!Bytes.IsEmpty)
9493
{
95-
stream.WriteHex(Bytes);
94+
stream.WriteHex(Bytes.Span);
9695
}
9796
else
9897
{

src/UglyToad.PdfPig/Graphics/ReflectionGraphicsStateOperationFactory.cs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,29 @@ private static double OperandToDouble(IToken token)
343343
{
344344
return new MoveToNextLineShowText(snl.Data);
345345
}
346-
else if (operands[0] is HexToken hnl)
346+
347+
if (operands[0] is HexToken hnl)
347348
{
348-
return new MoveToNextLineShowText(hnl.Bytes.ToArray());
349+
return new MoveToNextLineShowText(hnl.Memory);
349350
}
350-
else
351+
352+
throw new InvalidOperationException($"Tried to create a move to next line and show text operation with operand type: {operands[0]?.GetType().Name ?? "null"}");
353+
case MoveToNextLineShowTextWithSpacing.Symbol:
354+
var wordSpacing = (NumericToken)operands[0];
355+
var charSpacing = (NumericToken)operands[1];
356+
var text = operands[2];
357+
358+
if (text is StringToken stringToken)
359+
{
360+
return new MoveToNextLineShowTextWithSpacing(wordSpacing.Double, charSpacing.Double, stringToken.Data);
361+
}
362+
363+
if (text is HexToken hexToken)
351364
{
352-
throw new InvalidOperationException($"Tried to create a move to next line and show text operation with operand type: {operands[0]?.GetType().Name ?? "null"}");
365+
return new MoveToNextLineShowTextWithSpacing(wordSpacing.Double, charSpacing.Double, hexToken.Memory);
353366
}
367+
368+
throw new InvalidOperationException($"Tried to create a MoveToNextLineShowTextWithSpacing operation with operand type: {operands[2]?.GetType().Name ?? "null"}");
354369
case MoveToNextLineWithOffset.Symbol:
355370
return new MoveToNextLineWithOffset(OperandToDouble(operands[0]), OperandToDouble(operands[1]));
356371
case MoveToNextLineWithOffsetSetLeading.Symbol:
@@ -364,7 +379,8 @@ private static double OperandToDouble(IToken token)
364379
{
365380
return new SetNonStrokeColorAdvanced(operands.Take(operands.Count - 1).Select(x => ((NumericToken)x).Data).ToArray(), scnLowerPatternName);
366381
}
367-
else if (operands.All(x => x is NumericToken))
382+
383+
if (operands.All(x => x is NumericToken))
368384
{
369385
return new SetNonStrokeColorAdvanced(operands.Select(x => ((NumericToken)x).Data).ToArray());
370386
}
@@ -425,14 +441,13 @@ private static double OperandToDouble(IToken token)
425441
{
426442
return new ShowText(s.Data);
427443
}
428-
else if (operands[0] is HexToken h)
429-
{
430-
return new ShowText(h.Bytes.ToArray());
431-
}
432-
else
444+
445+
if (operands[0] is HexToken h)
433446
{
434-
throw new InvalidOperationException($"Tried to create a show text operation with operand type: {operands[0]?.GetType().Name ?? "null"}");
447+
return new ShowText(h.Memory);
435448
}
449+
450+
throw new InvalidOperationException($"Tried to create a show text operation with operand type: {operands[0]?.GetType().Name ?? "null"}");
436451
case ShowTextsWithPositioning.Symbol:
437452
if (operands.Count == 0)
438453
{

0 commit comments

Comments
 (0)