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
9 changes: 8 additions & 1 deletion src/UglyToad.PdfPig/Content/Letter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public class Letter
/// </summary>
internal decimal PointSize { get; }

/// <summary>
/// Sequence number of the ShowText operation that printed this letter.
/// </summary>
public int TextSequence { get; }

/// <summary>
/// Create a new letter to represent some text drawn by the Tj operator.
/// </summary>
Expand All @@ -75,7 +80,8 @@ internal Letter(string value, PdfRectangle glyphRectangle,
decimal fontSize,
string fontName,
IColor color,
decimal pointSize)
decimal pointSize,
int textSequence)
{
Value = value;
GlyphRectangle = glyphRectangle;
Expand All @@ -86,6 +92,7 @@ internal Letter(string value, PdfRectangle glyphRectangle,
FontName = fontName;
Color = color ?? GrayColor.Black;
PointSize = pointSize;
TextSequence = textSequence;
TextDirection = GetTextDirection();
}

Expand Down
14 changes: 11 additions & 3 deletions src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ internal class ContentStreamProcessor : IOperationContext
private Stack<CurrentGraphicsState> graphicsStack = new Stack<CurrentGraphicsState>();
private IFont activeExtendedGraphicsStateFont = null;

//a sequence number of ShowText operation to determine whether letters belong to same operation or not (letters that belong to different operations have less changes to belong to same word)
private int textSequence = 0;

public TextMatrices TextMatrices { get; } = new TextMatrices();

public TransformationMatrix CurrentTransformationMatrix
Expand Down Expand Up @@ -187,7 +190,8 @@ public void ShowText(IInputBytes bytes)
unicode,
fontSize,
color,
pointSize);
pointSize,
textSequence);

decimal tx, ty;
if (font.IsVertical)
Expand All @@ -209,6 +213,8 @@ public void ShowText(IInputBytes bytes)

public void ShowPositionedText(IReadOnlyList<IToken> tokens)
{
textSequence++;

var currentState = GetCurrentState();

var textState = currentState.FontState;
Expand Down Expand Up @@ -361,7 +367,8 @@ private void ShowGlyph(IFont font, PdfRectangle glyphRectangle,
string unicode,
decimal fontSize,
IColor color,
decimal pointSize)
decimal pointSize,
int textSequence)
{
var letter = new Letter(unicode, glyphRectangle,
startBaseLine,
Expand All @@ -370,7 +377,8 @@ private void ShowGlyph(IFont font, PdfRectangle glyphRectangle,
fontSize,
font.Name.Data,
color,
pointSize);
pointSize,
textSequence);

Letters.Add(letter);
}
Expand Down
10 changes: 9 additions & 1 deletion src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class PdfPageBuilder
{
private readonly PdfDocumentBuilder documentBuilder;
private readonly List<IGraphicsStateOperation> operations = new List<IGraphicsStateOperation>();

//a sequence number of ShowText operation to determine whether letters belong to same operation or not (letters that belong to different operations have less changes to belong to same word)
private static int textSequence = 0;

internal IReadOnlyList<IGraphicsStateOperation> Operations => operations;

/// <summary>
Expand Down Expand Up @@ -240,6 +244,8 @@ private static List<Letter> DrawLetters(string text, IWritingFont font, Transfor

var width = 0m;

textSequence++;

for (var i = 0; i < text.Length; i++)
{
var c = text[i];
Expand All @@ -261,7 +267,9 @@ private static List<Letter> DrawLetters(string text, IWritingFont font, Transfor

var letter = new Letter(c.ToString(), documentSpace, advanceRect.BottomLeft, advanceRect.BottomRight, width, fontSize, font.Name,
GrayColor.Black,
fontSize);
fontSize,
textSequence);

letters.Add(letter);

var tx = advanceRect.Width * horizontalScaling;
Expand Down