From 2011d504a717e73de52c08961fe0f2a21d50b263 Mon Sep 17 00:00:00 2001 From: BobLd Date: Sun, 12 May 2019 19:34:00 +0100 Subject: [PATCH] In Content: - Adding a 'Line' of text object - Adding a 'TextDirection' property in the 'Word' object In Geometry: - Adding a 'PdfLine' object - Making the 'PdfRectangle' creator public --- src/UglyToad.PdfPig/Content/Line.cs | 69 ++++++++++++++++++++ src/UglyToad.PdfPig/Content/Word.cs | 15 ++++- src/UglyToad.PdfPig/Geometry/PdfLine.cs | 47 +++++++++++++ src/UglyToad.PdfPig/Geometry/PdfRectangle.cs | 10 ++- 4 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 src/UglyToad.PdfPig/Content/Line.cs create mode 100644 src/UglyToad.PdfPig/Geometry/PdfLine.cs diff --git a/src/UglyToad.PdfPig/Content/Line.cs b/src/UglyToad.PdfPig/Content/Line.cs new file mode 100644 index 00000000..a40f9c56 --- /dev/null +++ b/src/UglyToad.PdfPig/Content/Line.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Text; +using UglyToad.PdfPig.Geometry; +using System.Linq; + +namespace UglyToad.PdfPig.Content +{ + /// + /// A line. + /// + public class Line + { + /// + /// The text of the line. + /// + public string Text { get; } + + /// + /// The text direction of the line. + /// + public TextDirection TextDirection { get; } + + /// + /// The rectangle completely containing the line. + /// + public PdfRectangle BoundingBox { get; } + + /// + /// The words contained in the line. + /// + public IReadOnlyList Words { get; } + + /// + /// Create a new . + /// + /// The letters contained in the word. + public Line(IReadOnlyList 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); + + TextDirection = words[0].TextDirection; + } + + /// + public override string ToString() + { + return Text; + } + } +} diff --git a/src/UglyToad.PdfPig/Content/Word.cs b/src/UglyToad.PdfPig/Content/Word.cs index 10def062..dbdab25a 100644 --- a/src/UglyToad.PdfPig/Content/Word.cs +++ b/src/UglyToad.PdfPig/Content/Word.cs @@ -15,6 +15,11 @@ /// public string Text { get; } + /// + /// The text direction of the word. + /// + public TextDirection TextDirection { get; } + /// /// The rectangle completely containing the word. /// @@ -25,6 +30,11 @@ /// public string FontName { get; } + /// + /// The letters contained in the word. + /// + public IReadOnlyList Letters { get; } + /// /// Create a new . /// @@ -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; } /// diff --git a/src/UglyToad.PdfPig/Geometry/PdfLine.cs b/src/UglyToad.PdfPig/Geometry/PdfLine.cs new file mode 100644 index 00000000..0fd57084 --- /dev/null +++ b/src/UglyToad.PdfPig/Geometry/PdfLine.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace UglyToad.PdfPig.Geometry +{ + /// + /// A line in a PDF file. + /// + /// + /// 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). + /// + public struct PdfLine + { + /// + /// First point of the line. + /// + public PdfPoint Point1 { get; } + + /// + /// Second point of the line. + /// + public PdfPoint Point2 { get; } + + /// + /// A line in a PDF file. + /// + /// + /// + /// + /// + public PdfLine(decimal x1, decimal y1, decimal x2, decimal y2) : this(new PdfPoint(x1, y1), new PdfPoint(x2, y2)) { } + + /// + /// A line in a PDF file. + /// + /// First point of the line. + /// Second point of the line. + public PdfLine(PdfPoint point1, PdfPoint point2) + { + Point1 = point1; + Point2 = point2; + } + } +} diff --git a/src/UglyToad.PdfPig/Geometry/PdfRectangle.cs b/src/UglyToad.PdfPig/Geometry/PdfRectangle.cs index f28fd124..77457202 100644 --- a/src/UglyToad.PdfPig/Geometry/PdfRectangle.cs +++ b/src/UglyToad.PdfPig/Geometry/PdfRectangle.cs @@ -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) + + /// + /// A rectangle in a PDF file. + /// + /// + /// + /// + /// + public PdfRectangle(decimal x1, decimal y1, decimal x2, decimal y2) { decimal bottom; decimal top;