diff --git a/src/UglyToad.PdfPig.Tokens/HexToken.cs b/src/UglyToad.PdfPig.Tokens/HexToken.cs index 95987b53c..1343c48ed 100644 --- a/src/UglyToad.PdfPig.Tokens/HexToken.cs +++ b/src/UglyToad.PdfPig.Tokens/HexToken.cs @@ -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 } } diff --git a/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/MoveToNextLineShowText.cs b/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/MoveToNextLineShowText.cs index 519a0c328..e1ee8bb11 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/MoveToNextLineShowText.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/MoveToNextLineShowText.cs @@ -25,7 +25,7 @@ public class MoveToNextLineShowText : IGraphicsStateOperation /// /// The text to show as hex bytes. /// - public byte[]? Bytes { get; } + public ReadOnlyMemory Bytes { get; } /// /// Create a new . @@ -40,7 +40,7 @@ public MoveToNextLineShowText(string text) /// Create a new . /// /// The bytes of the text to show. - public MoveToNextLineShowText(byte[] hexBytes) + public MoveToNextLineShowText(ReadOnlyMemory hexBytes) { Bytes = hexBytes; } @@ -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); @@ -59,14 +59,14 @@ public void Run(IOperationContext operationContext) /// 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(); diff --git a/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/MoveToNextLineShowTextWithSpacing.cs b/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/MoveToNextLineShowTextWithSpacing.cs index 179f8a26e..1cb4ebce6 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/MoveToNextLineShowTextWithSpacing.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/MoveToNextLineShowTextWithSpacing.cs @@ -31,7 +31,7 @@ public class MoveToNextLineShowTextWithSpacing : IGraphicsStateOperation /// /// The bytes of the text. /// - public byte[]? Bytes { get; } + public ReadOnlyMemory Bytes { get; } /// /// The text to show. @@ -57,7 +57,7 @@ public MoveToNextLineShowTextWithSpacing(double wordSpacing, double characterSpa /// The word spacing. /// The character spacing. /// The bytes of the text to show. - public MoveToNextLineShowTextWithSpacing(double wordSpacing, double characterSpacing, byte[] hexBytes) + public MoveToNextLineShowTextWithSpacing(double wordSpacing, double characterSpacing, ReadOnlyMemory hexBytes) { WordSpacing = wordSpacing; CharacterSpacing = characterSpacing; @@ -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 { diff --git a/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/ShowText.cs b/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/ShowText.cs index 3230eb8be..fd9ed9c29 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/ShowText.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/TextShowing/ShowText.cs @@ -1,6 +1,7 @@ namespace UglyToad.PdfPig.Graphics.Operations.TextShowing { using PdfPig.Core; + using System; using System.IO; /// @@ -38,7 +39,7 @@ public class ShowText : IGraphicsStateOperation /// /// The bytes of the string to show. /// - public byte[]? Bytes { get; } + public ReadOnlyMemory Bytes { get; } /// /// Create a new . @@ -51,7 +52,7 @@ public ShowText(string text) /// /// Create a new . /// - public ShowText(byte[] hexBytes) + public ShowText(ReadOnlyMemory hexBytes) { Bytes = hexBytes; } @@ -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); } @@ -89,10 +89,9 @@ public void Run(IOperationContext operationContext) /// public void Write(Stream stream) { - - if (Bytes != null) + if (!Bytes.IsEmpty) { - stream.WriteHex(Bytes); + stream.WriteHex(Bytes.Span); } else { diff --git a/src/UglyToad.PdfPig/Graphics/ReflectionGraphicsStateOperationFactory.cs b/src/UglyToad.PdfPig/Graphics/ReflectionGraphicsStateOperationFactory.cs index ece290ef8..e9a47bea7 100644 --- a/src/UglyToad.PdfPig/Graphics/ReflectionGraphicsStateOperationFactory.cs +++ b/src/UglyToad.PdfPig/Graphics/ReflectionGraphicsStateOperationFactory.cs @@ -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: @@ -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()); } @@ -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) {