Merge pull request #31 from BobLd/master

add textline and pdfline
This commit is contained in:
Eliot Jones
2019-05-15 22:34:01 +01:00
committed by GitHub
5 changed files with 156 additions and 2 deletions

View File

@@ -45,6 +45,7 @@
"UglyToad.PdfPig.Content.Page",
"UglyToad.PdfPig.Content.PageSize",
"UglyToad.PdfPig.Content.Word",
"UglyToad.PdfPig.Content.TextLine",
"UglyToad.PdfPig.Content.TextDirection",
"UglyToad.PdfPig.Core.TransformationMatrix",
"UglyToad.PdfPig.CrossReference.CrossReferenceTable",
@@ -60,6 +61,7 @@
"UglyToad.PdfPig.Fonts.Standard14Font",
"UglyToad.PdfPig.Geometry.PdfPath",
"UglyToad.PdfPig.Geometry.PdfPoint",
"UglyToad.PdfPig.Geometry.PdfLine",
"UglyToad.PdfPig.Geometry.PdfRectangle",
"UglyToad.PdfPig.Graphics.Core.LineCapStyle",
"UglyToad.PdfPig.Graphics.Core.LineDashPattern",

View File

@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UglyToad.PdfPig.Geometry;
namespace UglyToad.PdfPig.Content
{
/// <summary>
/// A line of text.
/// </summary>
public class TextLine
{
/// <summary>
/// The text of the line.
/// </summary>
public string Text { get; }
/// <summary>
/// The text direction of the line.
/// </summary>
public TextDirection TextDirection { get; }
/// <summary>
/// The rectangle completely containing the line.
/// </summary>
public PdfRectangle BoundingBox { get; }
/// <summary>
/// The words contained in the line.
/// </summary>
public IReadOnlyList<Word> Words { get; }
/// <summary>
/// Create a new <see cref="TextLine"/>.
/// </summary>
/// <param name="words">The words contained in the word.</param>
public TextLine(IReadOnlyList<Word> words)
{
if (words == null)
{
throw new ArgumentNullException(nameof(words));
}
if (words.Count == 0)
{
throw new ArgumentException("Empty words provided.", nameof(words));
}
Words = words;
Text = string.Join(" ", words.Where(s => !string.IsNullOrWhiteSpace(s.Text)).Select(x => x.Text));
var minX = words.Min(x => x.BoundingBox.Left);
var minY = words.Min(x => x.BoundingBox.Bottom);
var maxX = words.Max(x => x.BoundingBox.Right);
var maxY = words.Max(x => x.BoundingBox.Top);
BoundingBox = new PdfRectangle(minX, minY, maxX, maxY);
if (words.All(x => x.TextDirection == words[0].TextDirection))
{
TextDirection = words[0].TextDirection;
}
else
{
TextDirection = TextDirection.Unknown;
}
}
/// <inheritdoc />
public override string ToString()
{
return Text;
}
}
}

View File

@@ -15,6 +15,11 @@
/// </summary>
public string Text { get; }
/// <summary>
/// The text direction of the word.
/// </summary>
public TextDirection TextDirection { get; }
/// <summary>
/// The rectangle completely containing the word.
/// </summary>
@@ -25,6 +30,11 @@
/// </summary>
public string FontName { get; }
/// <summary>
/// The letters contained in the word.
/// </summary>
public IReadOnlyList<Letter> Letters { get; }
/// <summary>
/// Create a new <see cref="Word"/>.
/// </summary>
@@ -41,15 +51,18 @@
throw new ArgumentException("Empty letters provided.", nameof(letters));
}
Letters = letters;
Text = string.Join(string.Empty, letters.Select(x => x.Value));
var minX = letters.Min(x => x.Location.X);
var minY = letters.Min(x => x.Location.Y);
var maxX = letters.Max(x => x.Location.X + x.Width);
var maxY = letters.Max(x => x.GlyphRectangle.Top);
BoundingBox = new PdfRectangle(minX, minY, maxX, maxY);
FontName = letters[0].FontName;
TextDirection = letters[0].TextDirection;
}
/// <inheritdoc />

View File

@@ -0,0 +1,56 @@
namespace UglyToad.PdfPig.Geometry
{
/// <summary>
/// A line in a PDF file.
/// </summary>
/// <remarks>
/// PDF coordinates are defined with the origin at the lower left (0, 0).
/// The Y-axis extends vertically upwards and the X-axis horizontally to the right.
/// Unless otherwise specified on a per-page basis, units in PDF space are equivalent to a typographic point (1/72 inch).
/// </remarks>
public struct PdfLine
{
/// <summary>
/// Length of the line.
/// </summary>
public decimal Length
{
get
{
decimal l = (Point1.X - Point2.X) * (Point1.X - Point2.X) +
(Point1.Y - Point2.Y) * (Point1.Y - Point2.Y);
return (decimal)System.Math.Sqrt((double)l);
}
}
/// <summary>
/// First point of the line.
/// </summary>
public PdfPoint Point1 { get; }
/// <summary>
/// Second point of the line.
/// </summary>
public PdfPoint Point2 { get; }
/// <summary>
/// Create a new <see cref="PdfLine"/>.
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
public PdfLine(decimal x1, decimal y1, decimal x2, decimal y2) : this(new PdfPoint(x1, y1), new PdfPoint(x2, y2)) { }
/// <summary>
/// Create a new <see cref="PdfLine"/>.
/// </summary>
/// <param name="point1">First point of the line.</param>
/// <param name="point2">Second point of the line.</param>
public PdfLine(PdfPoint point1, PdfPoint point2)
{
Point1 = point1;
Point2 = point2;
}
}
}

View File

@@ -67,7 +67,15 @@
internal PdfRectangle(PdfPoint point1, PdfPoint point2) : this(point1.X, point1.Y, point2.X, point2.Y) { }
internal PdfRectangle(short x1, short y1, short x2, short y2) : this((decimal) x1, y1, x2, y2) { }
internal PdfRectangle(decimal x1, decimal y1, decimal x2, decimal y2)
/// <summary>
/// Create a new <see cref="PdfRectangle"/>.
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
public PdfRectangle(decimal x1, decimal y1, decimal x2, decimal y2)
{
decimal bottom;
decimal top;