mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-04-05 20:55:01 +08:00
tidy up during investigation #600
This commit is contained in:
parent
11df5b520d
commit
20d3cc9066
@ -1,6 +1,5 @@
|
||||
namespace UglyToad.PdfPig.Content
|
||||
{
|
||||
using Annotations;
|
||||
using Core;
|
||||
using Outline;
|
||||
using System;
|
||||
@ -69,7 +68,8 @@
|
||||
{
|
||||
pageTreeMembers.Rotation = rotateToken.Int;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var page = pageFactory.Create(
|
||||
pageNumber,
|
||||
pageNode.NodeDictionary,
|
||||
|
@ -1,117 +1,117 @@
|
||||
namespace UglyToad.PdfPig.Graphics.Operations.TextShowing
|
||||
{
|
||||
using System.IO;
|
||||
using PdfPig.Core;
|
||||
using Util.JetBrains.Annotations;
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Show a text string
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>The input is a sequence of character codes to be shown as glyphs.</para>
|
||||
/// <para>
|
||||
/// Generally each byte represents a single character code, however starting in version 1.2+
|
||||
/// a composite font might use multi-byte character codes to map to glyphs.
|
||||
/// For these composite fonts, the <see cref="T:UglyToad.PdfPig.Fonts.Cmap.CMap" /> of the font defines the mapping from code to glyph.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The grouping of character codes in arguments to this operator does not have any impact on the meaning; for example:<br />
|
||||
/// (Abc) Tj is equivalent to (A) Tj (b) Tj (c) Tj<br />
|
||||
/// However grouping character codes makes the document easier to search and extract text from.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public class ShowText : IGraphicsStateOperation
|
||||
{
|
||||
/// <summary>
|
||||
/// The symbol for this operation in a stream.
|
||||
/// </summary>
|
||||
public const string Symbol = "Tj";
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Operator => Symbol;
|
||||
|
||||
/// <summary>
|
||||
/// The text string to show.
|
||||
/// </summary>
|
||||
[CanBeNull]
|
||||
public string Text { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The bytes of the string to show.
|
||||
/// </summary>
|
||||
[CanBeNull]
|
||||
public byte[] Bytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="ShowText"/>.
|
||||
/// </summary>
|
||||
public ShowText(string text)
|
||||
{
|
||||
Text = text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="ShowText"/>.
|
||||
/// </summary>
|
||||
public ShowText(byte[] hexBytes)
|
||||
{
|
||||
Bytes = hexBytes;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Run(IOperationContext operationContext)
|
||||
{
|
||||
var input = new ByteArrayInputBytes(Text != null ? OtherEncodings.StringAsLatin1Bytes(Text) : Bytes);
|
||||
|
||||
operationContext.ShowText(input);
|
||||
}
|
||||
|
||||
string EscapeText(string text)
|
||||
namespace UglyToad.PdfPig.Graphics.Operations.TextShowing
|
||||
{
|
||||
using System.IO;
|
||||
using PdfPig.Core;
|
||||
using Util.JetBrains.Annotations;
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Show a text string
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>The input is a sequence of character codes to be shown as glyphs.</para>
|
||||
/// <para>
|
||||
/// Generally each byte represents a single character code, however starting in version 1.2+
|
||||
/// a composite font might use multi-byte character codes to map to glyphs.
|
||||
/// For these composite fonts, the <see cref="T:UglyToad.PdfPig.Fonts.Cmap.CMap" /> of the font defines the mapping from code to glyph.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The grouping of character codes in arguments to this operator does not have any impact on the meaning; for example:<br />
|
||||
/// (Abc) Tj is equivalent to (A) Tj (b) Tj (c) Tj<br />
|
||||
/// However grouping character codes makes the document easier to search and extract text from.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public class ShowText : IGraphicsStateOperation
|
||||
{
|
||||
/// <summary>
|
||||
/// The symbol for this operation in a stream.
|
||||
/// </summary>
|
||||
public const string Symbol = "Tj";
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Operator => Symbol;
|
||||
|
||||
/// <summary>
|
||||
/// The text string to show.
|
||||
/// </summary>
|
||||
[CanBeNull]
|
||||
public string Text { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The bytes of the string to show.
|
||||
/// </summary>
|
||||
[CanBeNull]
|
||||
public byte[] Bytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="ShowText"/>.
|
||||
/// </summary>
|
||||
public ShowText(string text)
|
||||
{
|
||||
if (text is null) return null;
|
||||
// Fix Issue 350 from PDF Spec 1.7 (page 408) on handling 'special characters' of '(', ')' and '\'.
|
||||
|
||||
// The strings must conform to the syntax for string objects.
|
||||
Text = text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="ShowText"/>.
|
||||
/// </summary>
|
||||
public ShowText(byte[] hexBytes)
|
||||
{
|
||||
Bytes = hexBytes;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Run(IOperationContext operationContext)
|
||||
{
|
||||
var input = new ByteArrayInputBytes(Text != null ? OtherEncodings.StringAsLatin1Bytes(Text) : Bytes);
|
||||
|
||||
operationContext.ShowText(input);
|
||||
}
|
||||
|
||||
string EscapeText(string text)
|
||||
{
|
||||
if (text is null) return null;
|
||||
// Fix Issue 350 from PDF Spec 1.7 (page 408) on handling 'special characters' of '(', ')' and '\'.
|
||||
|
||||
// The strings must conform to the syntax for string objects.
|
||||
// When a string is written by enclosing the data in parentheses,
|
||||
// bytes whose values are the same as those
|
||||
// of the ASCII characters left parenthesis (40), right parenthesis (41), and backslash (92)
|
||||
// bytes whose values are the same as those
|
||||
// of the ASCII characters left parenthesis (40), right parenthesis (41), and backslash (92)
|
||||
// must be preceded by a backslash character.
|
||||
// All other byte values between 0 and 255 may be used in a string object.
|
||||
// These rules apply to each individual byte in a string object, whether the string is interpreted by the text-showing operators
|
||||
// as single-byte or multiple-byte character codes.
|
||||
|
||||
// Note: order of replacing is important. Replace slash first before brackets.
|
||||
text = text.Replace(@"\", @"\\)"); // Escape any slash '\' -> '\\'
|
||||
text = text.Replace("(", @"\("); // Escape any open brackets '(' -> '\('
|
||||
// These rules apply to each individual byte in a string object, whether the string is interpreted by the text-showing operators
|
||||
// as single-byte or multiple-byte character codes.
|
||||
|
||||
// Note: order of replacing is important. Replace slash first before brackets.
|
||||
text = text.Replace(@"\", @"\\)"); // Escape any slash '\' -> '\\'
|
||||
text = text.Replace("(", @"\("); // Escape any open brackets '(' -> '\('
|
||||
text = text.Replace(")", @"\)"); // Escape any close brackets ')' -> '\)'
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Write(Stream stream)
|
||||
{
|
||||
|
||||
if (Bytes != null)
|
||||
{
|
||||
stream.WriteHex(Bytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
var EscapedText = EscapeText(Text); // escape '(', ')' or '\'
|
||||
stream.WriteText($"({EscapedText})");
|
||||
}
|
||||
|
||||
stream.WriteWhiteSpace();
|
||||
stream.WriteText(Symbol);
|
||||
stream.WriteNewLine();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Text} {Symbol}";
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Write(Stream stream)
|
||||
{
|
||||
|
||||
if (Bytes != null)
|
||||
{
|
||||
stream.WriteHex(Bytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
var escapedText = EscapeText(Text); // escape '(', ')' or '\'
|
||||
stream.WriteText($"({escapedText})");
|
||||
}
|
||||
|
||||
stream.WriteWhiteSpace();
|
||||
stream.WriteText(Symbol);
|
||||
stream.WriteNewLine();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Text} {Symbol}";
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user