- Adding a TextDirection enum.

- In the Letter class:
     - Renaming 'Location' to 'StartBaseLine' and adding 'EndBaseLine' for better localisation of the letter ('Location' is also kept).
     - Adding TextDirection.
This commit is contained in:
BobLd
2019-04-19 21:33:31 +01:00
parent 214ef8a958
commit 65647febcf
5 changed files with 78 additions and 11 deletions

View File

@@ -13,9 +13,24 @@
public string Value { get; }
/// <summary>
/// The placement position of the character in PDF space.
/// Text direction of the letter.
/// </summary>
public PdfPoint Location { get; }
public TextDirection TextDirection { get; }
/// <summary>
/// The placement position of the character in PDF space. See <see cref="StartBaseLine"/>
/// </summary>
public PdfPoint Location => StartBaseLine;
/// <summary>
/// The placement position of the character in PDF space (the start point of the baseline). See <see cref="Location"/>
/// </summary>
public PdfPoint StartBaseLine { get; }
/// <summary>
/// The end point of the baseline.
/// </summary>
public PdfPoint EndBaseLine { get; }
/// <summary>
/// The width occupied by the character within the PDF content.
@@ -47,15 +62,17 @@
/// <summary>
/// Create a new letter to represent some text drawn by the Tj operator.
/// </summary>
internal Letter(string value, PdfRectangle glyphRectangle, PdfPoint location, decimal width, decimal fontSize, string fontName, decimal pointSize)
internal Letter(string value, PdfRectangle glyphRectangle, PdfPoint startBaseLine, PdfPoint endBaseLine, decimal width, decimal fontSize, string fontName, decimal pointSize)
{
Value = value;
GlyphRectangle = glyphRectangle;
FontSize = fontSize;
FontName = fontName;
PointSize = pointSize;
Location = location;
Width = width;
StartBaseLine = startBaseLine;
EndBaseLine = endBaseLine;
TextDirection = getTextDirection();
}
/// <summary>
@@ -65,5 +82,22 @@
{
return $"{Value} {Location} {FontName} {PointSize}";
}
private TextDirection getTextDirection()
{
if (System.Math.Abs(StartBaseLine.Y - EndBaseLine.Y) < 10e-5m)
{
return TextDirection.Horizontal;
}
else if (System.Math.Abs(StartBaseLine.X - EndBaseLine.X) < 10e-5m)
{
if (StartBaseLine.Y > EndBaseLine.Y)
{
return TextDirection.Rotate90;
}
return TextDirection.Rotate270;
}
return TextDirection.Unknown;
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace UglyToad.PdfPig.Content
{
/// <summary>
/// Direction of the text.
/// </summary>
public enum TextDirection
{
/// <summary>
/// Text direction not known.
/// </summary>
Unknown,
/// <summary>
/// Usual text direction (Left to Right).
/// </summary>
Horizontal,
/// <summary>
/// Rotated text going down.
/// </summary>
Rotate90,
/// <summary>
/// Rotated text going up.
/// </summary>
Rotate270
}
}

View File

@@ -155,7 +155,7 @@
.Transform(TextMatrices.TextMatrix
.Transform(renderingMatrix.Transform(new PdfRectangle(0, 0, boundingBox.Width, 0))));
ShowGlyph(font, transformedGlyphBounds, transformedPdfBounds.BottomLeft, transformedPdfBounds.Width, unicode, fontSize, pointSize);
ShowGlyph(font, transformedGlyphBounds, transformedPdfBounds.BottomLeft, transformedPdfBounds.BottomRight, transformedPdfBounds.Width, unicode, fontSize, pointSize);
decimal tx, ty;
if (font.IsVertical)
@@ -283,9 +283,9 @@
TextMatrices.TextMatrix = newMatrix;
}
private void ShowGlyph(IFont font, PdfRectangle glyphRectangle, PdfPoint position, decimal width, string unicode, decimal fontSize, decimal pointSize)
private void ShowGlyph(IFont font, PdfRectangle glyphRectangle, PdfPoint startBaseLine, PdfPoint endBaseLine, decimal width, string unicode, decimal fontSize, decimal pointSize)
{
var letter = new Letter(unicode, glyphRectangle, position, width, fontSize, font.Name.Data, pointSize);
var letter = new Letter(unicode, glyphRectangle, startBaseLine, endBaseLine, width, fontSize, font.Name.Data, pointSize);
Letters.Add(letter);
}

View File

@@ -67,8 +67,9 @@
var nextIsWhiteSpace = string.IsNullOrWhiteSpace(letter.Value);
var nextFontDiffers = !string.Equals(letter.FontName, lastLetter.FontName, StringComparison.OrdinalIgnoreCase) && gap > letter.Width * 0.1m;
var nextFontSizeDiffers = Math.Abs(letter.FontSize - lastLetter.FontSize) > 0.1m;
var nextTextDirectionDiffers = letter.TextDirection != lastLetter.TextDirection;
if (nextToLeft || nextBigSpace || nextIsWhiteSpace || nextFontDiffers || nextFontSizeDiffers)
if (nextToLeft || nextBigSpace || nextIsWhiteSpace || nextFontDiffers || nextFontSizeDiffers || nextTextDirectionDiffers)
{
if (lettersSoFar.Count > 0)
{

View File

@@ -27,7 +27,7 @@
/// The number of this page, 1-indexed.
/// </summary>
public int PageNumber { get; }
/// <summary>
/// The current size of the page.
/// </summary>
@@ -112,7 +112,7 @@
internal void SetStrokeColorExact(decimal r, decimal g, decimal b)
{
operations.Add(Push.Value);
operations.Add(new SetStrokeColorDeviceRgb(CheckRgbDecimal(r, nameof(r)),
operations.Add(new SetStrokeColorDeviceRgb(CheckRgbDecimal(r, nameof(r)),
CheckRgbDecimal(g, nameof(g)), CheckRgbDecimal(b, nameof(b))));
}
@@ -258,7 +258,7 @@
var documentSpace = textMatrix.Transform(renderingMatrix.Transform(fontMatrix.Transform(rect)));
var letter = new Letter(c.ToString(), documentSpace, advanceRect.BottomLeft, width, fontSize, font.Name, fontSize);
var letter = new Letter(c.ToString(), documentSpace, advanceRect.BottomLeft, advanceRect.BottomRight, width, fontSize, font.Name, fontSize);
letters.Add(letter);
var tx = advanceRect.Width * horizontalScaling;