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
|
namespace UglyToad.PdfPig.Content
|
||||||
{
|
{
|
||||||
using Annotations;
|
|
||||||
using Core;
|
using Core;
|
||||||
using Outline;
|
using Outline;
|
||||||
using System;
|
using System;
|
||||||
@ -69,7 +68,8 @@
|
|||||||
{
|
{
|
||||||
pageTreeMembers.Rotation = rotateToken.Int;
|
pageTreeMembers.Rotation = rotateToken.Int;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var page = pageFactory.Create(
|
var page = pageFactory.Create(
|
||||||
pageNumber,
|
pageNumber,
|
||||||
pageNode.NodeDictionary,
|
pageNode.NodeDictionary,
|
||||||
|
@ -1,117 +1,117 @@
|
|||||||
namespace UglyToad.PdfPig.Graphics.Operations.TextShowing
|
namespace UglyToad.PdfPig.Graphics.Operations.TextShowing
|
||||||
{
|
{
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using PdfPig.Core;
|
using PdfPig.Core;
|
||||||
using Util.JetBrains.Annotations;
|
using Util.JetBrains.Annotations;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Show a text string
|
/// Show a text string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// <para>The input is a sequence of character codes to be shown as glyphs.</para>
|
/// <para>The input is a sequence of character codes to be shown as glyphs.</para>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// Generally each byte represents a single character code, however starting in version 1.2+
|
/// 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.
|
/// 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.
|
/// 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>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// The grouping of character codes in arguments to this operator does not have any impact on the meaning; for example:<br />
|
/// 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 />
|
/// (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.
|
/// However grouping character codes makes the document easier to search and extract text from.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public class ShowText : IGraphicsStateOperation
|
public class ShowText : IGraphicsStateOperation
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The symbol for this operation in a stream.
|
/// The symbol for this operation in a stream.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const string Symbol = "Tj";
|
public const string Symbol = "Tj";
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public string Operator => Symbol;
|
public string Operator => Symbol;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The text string to show.
|
/// The text string to show.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[CanBeNull]
|
[CanBeNull]
|
||||||
public string Text { get; }
|
public string Text { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The bytes of the string to show.
|
/// The bytes of the string to show.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[CanBeNull]
|
[CanBeNull]
|
||||||
public byte[] Bytes { get; }
|
public byte[] Bytes { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new <see cref="ShowText"/>.
|
/// Create a new <see cref="ShowText"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ShowText(string text)
|
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)
|
|
||||||
{
|
{
|
||||||
if (text is null) return null;
|
Text = text;
|
||||||
// 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.
|
/// <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,
|
// When a string is written by enclosing the data in parentheses,
|
||||||
// bytes whose values are the same as those
|
// bytes whose values are the same as those
|
||||||
// of the ASCII characters left parenthesis (40), right parenthesis (41), and backslash (92)
|
// of the ASCII characters left parenthesis (40), right parenthesis (41), and backslash (92)
|
||||||
// must be preceded by a backslash character.
|
// must be preceded by a backslash character.
|
||||||
// All other byte values between 0 and 255 may be used in a string object.
|
// 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
|
// 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.
|
// as single-byte or multiple-byte character codes.
|
||||||
|
|
||||||
// Note: order of replacing is important. Replace slash first before brackets.
|
// Note: order of replacing is important. Replace slash first before brackets.
|
||||||
text = text.Replace(@"\", @"\\)"); // Escape any slash '\' -> '\\'
|
text = text.Replace(@"\", @"\\)"); // Escape any slash '\' -> '\\'
|
||||||
text = text.Replace("(", @"\("); // Escape any open brackets '(' -> '\('
|
text = text.Replace("(", @"\("); // Escape any open brackets '(' -> '\('
|
||||||
text = text.Replace(")", @"\)"); // Escape any close brackets ')' -> '\)'
|
text = text.Replace(")", @"\)"); // Escape any close brackets ')' -> '\)'
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void Write(Stream stream)
|
public void Write(Stream stream)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (Bytes != null)
|
if (Bytes != null)
|
||||||
{
|
{
|
||||||
stream.WriteHex(Bytes);
|
stream.WriteHex(Bytes);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var EscapedText = EscapeText(Text); // escape '(', ')' or '\'
|
var escapedText = EscapeText(Text); // escape '(', ')' or '\'
|
||||||
stream.WriteText($"({EscapedText})");
|
stream.WriteText($"({escapedText})");
|
||||||
}
|
}
|
||||||
|
|
||||||
stream.WriteWhiteSpace();
|
stream.WriteWhiteSpace();
|
||||||
stream.WriteText(Symbol);
|
stream.WriteText(Symbol);
|
||||||
stream.WriteNewLine();
|
stream.WriteNewLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return $"{Text} {Symbol}";
|
return $"{Text} {Symbol}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user