Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Tokens/HexToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public string GetHexString()
#if NET8_0_OR_GREATER
return Convert.ToHexString(Bytes);
#else
return BitConverter.ToString(Bytes.ToArray()).Replace("-", string.Empty);
return BitConverter.ToString(_bytes).Replace("-", string.Empty);
#endif
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MoveToNextLineShowText : IGraphicsStateOperation
/// <summary>
/// The text to show as hex bytes.
/// </summary>
public byte[]? Bytes { get; }
public ReadOnlyMemory<byte> Bytes { get; }

/// <summary>
/// Create a new <see cref="MoveToNextLineShowText"/>.
Expand All @@ -40,7 +40,7 @@ public MoveToNextLineShowText(string text)
/// Create a new <see cref="MoveToNextLineShowText"/>.
/// </summary>
/// <param name="hexBytes">The bytes of the text to show.</param>
public MoveToNextLineShowText(byte[] hexBytes)
public MoveToNextLineShowText(ReadOnlyMemory<byte> hexBytes)
{
Bytes = hexBytes;
}
Expand All @@ -50,7 +50,7 @@ public void Run(IOperationContext operationContext)
{
var move = MoveToNextLine.Value;

var showText = Text != null ? new ShowText(Text) : new ShowText(Bytes!);
var showText = Text != null ? new ShowText(Text) : new ShowText(Bytes);

move.Run(operationContext);
showText.Run(operationContext);
Expand All @@ -59,14 +59,14 @@ public void Run(IOperationContext operationContext)
/// <inheritdoc />
public void Write(Stream stream)
{
if (Bytes is null)
if (Bytes.IsEmpty)
{
stream.WriteText($"({Text}) {Symbol}");
stream.WriteNewLine();
}
else
{
stream.WriteHex(Bytes);
stream.WriteHex(Bytes.Span);
stream.WriteWhiteSpace();
stream.WriteText(Symbol);
stream.WriteNewLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MoveToNextLineShowTextWithSpacing : IGraphicsStateOperation
/// <summary>
/// The bytes of the text.
/// </summary>
public byte[]? Bytes { get; }
public ReadOnlyMemory<byte> Bytes { get; }

/// <summary>
/// The text to show.
Expand All @@ -57,7 +57,7 @@ public MoveToNextLineShowTextWithSpacing(double wordSpacing, double characterSpa
/// <param name="wordSpacing">The word spacing.</param>
/// <param name="characterSpacing">The character spacing.</param>
/// <param name="hexBytes">The bytes of the text to show.</param>
public MoveToNextLineShowTextWithSpacing(double wordSpacing, double characterSpacing, byte[] hexBytes)
public MoveToNextLineShowTextWithSpacing(double wordSpacing, double characterSpacing, ReadOnlyMemory<byte> hexBytes)
{
WordSpacing = wordSpacing;
CharacterSpacing = characterSpacing;
Expand Down Expand Up @@ -86,9 +86,9 @@ public void Write(Stream stream)
stream.WriteDouble(CharacterSpacing);
stream.WriteWhiteSpace();

if (Bytes != null)
if (!Bytes.IsEmpty)
{
stream.WriteHex(Bytes);
stream.WriteHex(Bytes.Span);
}
else
{
Expand Down
11 changes: 5 additions & 6 deletions src/UglyToad.PdfPig/Graphics/Operations/TextShowing/ShowText.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace UglyToad.PdfPig.Graphics.Operations.TextShowing
{
using PdfPig.Core;
using System;
using System.IO;

/// <inheritdoc />
Expand Down Expand Up @@ -38,7 +39,7 @@ public class ShowText : IGraphicsStateOperation
/// <summary>
/// The bytes of the string to show.
/// </summary>
public byte[]? Bytes { get; }
public ReadOnlyMemory<byte> Bytes { get; }

/// <summary>
/// Create a new <see cref="ShowText"/>.
Expand All @@ -51,7 +52,7 @@ public ShowText(string text)
/// <summary>
/// Create a new <see cref="ShowText"/>.
/// </summary>
public ShowText(byte[] hexBytes)
public ShowText(ReadOnlyMemory<byte> hexBytes)
{
Bytes = hexBytes;
}
Expand All @@ -60,7 +61,6 @@ public ShowText(byte[] hexBytes)
public void Run(IOperationContext operationContext)
{
var input = new MemoryInputBytes(Text != null ? OtherEncodings.StringAsLatin1Bytes(Text) : Bytes);

operationContext.ShowText(input);
}

Expand Down Expand Up @@ -89,10 +89,9 @@ public void Run(IOperationContext operationContext)
/// <inheritdoc />
public void Write(Stream stream)
{

if (Bytes != null)
if (!Bytes.IsEmpty)
{
stream.WriteHex(Bytes);
stream.WriteHex(Bytes.Span);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,29 @@ private static double OperandToDouble(IToken token)
{
return new MoveToNextLineShowText(snl.Data);
}
else if (operands[0] is HexToken hnl)

if (operands[0] is HexToken hnl)
{
return new MoveToNextLineShowText(hnl.Bytes.ToArray());
return new MoveToNextLineShowText(hnl.Memory);
}
else

throw new InvalidOperationException($"Tried to create a move to next line and show text operation with operand type: {operands[0]?.GetType().Name ?? "null"}");
case MoveToNextLineShowTextWithSpacing.Symbol:
var wordSpacing = (NumericToken)operands[0];
var charSpacing = (NumericToken)operands[1];
var text = operands[2];

if (text is StringToken stringToken)
{
return new MoveToNextLineShowTextWithSpacing(wordSpacing.Double, charSpacing.Double, stringToken.Data);
}

if (text is HexToken hexToken)
{
throw new InvalidOperationException($"Tried to create a move to next line and show text operation with operand type: {operands[0]?.GetType().Name ?? "null"}");
return new MoveToNextLineShowTextWithSpacing(wordSpacing.Double, charSpacing.Double, hexToken.Memory);
}

throw new InvalidOperationException($"Tried to create a MoveToNextLineShowTextWithSpacing operation with operand type: {operands[2]?.GetType().Name ?? "null"}");
case MoveToNextLineWithOffset.Symbol:
return new MoveToNextLineWithOffset(OperandToDouble(operands[0]), OperandToDouble(operands[1]));
case MoveToNextLineWithOffsetSetLeading.Symbol:
Expand All @@ -364,7 +379,8 @@ private static double OperandToDouble(IToken token)
{
return new SetNonStrokeColorAdvanced(operands.Take(operands.Count - 1).Select(x => ((NumericToken)x).Data).ToArray(), scnLowerPatternName);
}
else if (operands.All(x => x is NumericToken))

if (operands.All(x => x is NumericToken))
{
return new SetNonStrokeColorAdvanced(operands.Select(x => ((NumericToken)x).Data).ToArray());
}
Expand Down Expand Up @@ -425,14 +441,13 @@ private static double OperandToDouble(IToken token)
{
return new ShowText(s.Data);
}
else if (operands[0] is HexToken h)
{
return new ShowText(h.Bytes.ToArray());
}
else

if (operands[0] is HexToken h)
{
throw new InvalidOperationException($"Tried to create a show text operation with operand type: {operands[0]?.GetType().Name ?? "null"}");
return new ShowText(h.Memory);
}

throw new InvalidOperationException($"Tried to create a show text operation with operand type: {operands[0]?.GetType().Name ?? "null"}");
case ShowTextsWithPositioning.Symbol:
if (operands.Count == 0)
{
Expand Down
Loading