From 2011d504a717e73de52c08961fe0f2a21d50b263 Mon Sep 17 00:00:00 2001 From: BobLd Date: Sun, 12 May 2019 19:34:00 +0100 Subject: [PATCH 1/4] 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; From de421d65a143a79da4a26565f08f235b563ff2fc Mon Sep 17 00:00:00 2001 From: BobLd Date: Sun, 12 May 2019 19:39:58 +0100 Subject: [PATCH 2/4] Adding Line, PdfLine --- src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs b/src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs index ab02b6c5..3d3fd255 100644 --- a/src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs +++ b/src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs @@ -45,6 +45,7 @@ "UglyToad.PdfPig.Content.Page", "UglyToad.PdfPig.Content.PageSize", "UglyToad.PdfPig.Content.Word", + "UglyToad.PdfPig.Content.Line", "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", From 97f0f6fe75a6646514d19c27fc7dc9b838b5e82a Mon Sep 17 00:00:00 2001 From: BobLd Date: Tue, 14 May 2019 20:56:34 +0100 Subject: [PATCH 3/4] Minor modifications and updates --- src/UglyToad.PdfPig/Content/Line.cs | 22 +++++++++++++------- src/UglyToad.PdfPig/Geometry/PdfLine.cs | 22 +++++++++++++------- src/UglyToad.PdfPig/Geometry/PdfRectangle.cs | 2 +- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/UglyToad.PdfPig/Content/Line.cs b/src/UglyToad.PdfPig/Content/Line.cs index a40f9c56..16f69823 100644 --- a/src/UglyToad.PdfPig/Content/Line.cs +++ b/src/UglyToad.PdfPig/Content/Line.cs @@ -1,15 +1,14 @@ using System; using System.Collections.Generic; -using System.Text; -using UglyToad.PdfPig.Geometry; using System.Linq; +using UglyToad.PdfPig.Geometry; namespace UglyToad.PdfPig.Content { /// - /// A line. + /// A line of text. /// - public class Line + public class TextLine { /// /// The text of the line. @@ -32,10 +31,10 @@ namespace UglyToad.PdfPig.Content public IReadOnlyList Words { get; } /// - /// Create a new . + /// Create a new . /// - /// The letters contained in the word. - public Line(IReadOnlyList words) + /// The words contained in the word. + public TextLine(IReadOnlyList words) { if (words == null) { @@ -57,7 +56,14 @@ namespace UglyToad.PdfPig.Content var maxY = words.Max(x => x.BoundingBox.Top); BoundingBox = new PdfRectangle(minX, minY, maxX, maxY); - TextDirection = words[0].TextDirection; + if (words.All(x => x.TextDirection == words[0].TextDirection)) + { + TextDirection = words[0].TextDirection; + } + else + { + TextDirection = TextDirection.Unknown; + } } /// diff --git a/src/UglyToad.PdfPig/Geometry/PdfLine.cs b/src/UglyToad.PdfPig/Geometry/PdfLine.cs index 0fd57084..0b8bdb6f 100644 --- a/src/UglyToad.PdfPig/Geometry/PdfLine.cs +++ b/src/UglyToad.PdfPig/Geometry/PdfLine.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace UglyToad.PdfPig.Geometry +namespace UglyToad.PdfPig.Geometry { /// /// A line in a PDF file. @@ -14,6 +10,18 @@ namespace UglyToad.PdfPig.Geometry /// public struct PdfLine { + /// + /// Length of the line. + /// + public decimal Length + { + get + { + decimal l = (Point1.X - Point1.X) * (Point1.X - Point1.X) + (Point1.Y - Point1.Y) * (Point1.Y - Point1.Y); + return (decimal)System.Math.Sqrt((double)l); + } + } + /// /// First point of the line. /// @@ -25,7 +33,7 @@ namespace UglyToad.PdfPig.Geometry public PdfPoint Point2 { get; } /// - /// A line in a PDF file. + /// Create a new . /// /// /// @@ -34,7 +42,7 @@ namespace UglyToad.PdfPig.Geometry 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. + /// Create a new . /// /// First point of the line. /// Second point of the line. diff --git a/src/UglyToad.PdfPig/Geometry/PdfRectangle.cs b/src/UglyToad.PdfPig/Geometry/PdfRectangle.cs index 77457202..aabfc1b8 100644 --- a/src/UglyToad.PdfPig/Geometry/PdfRectangle.cs +++ b/src/UglyToad.PdfPig/Geometry/PdfRectangle.cs @@ -69,7 +69,7 @@ internal PdfRectangle(short x1, short y1, short x2, short y2) : this((decimal) x1, y1, x2, y2) { } /// - /// A rectangle in a PDF file. + /// Create a new . /// /// /// From f4ec425bf0981c5597f4f8cd711e1bf63b00307b Mon Sep 17 00:00:00 2001 From: BobLd Date: Wed, 15 May 2019 19:44:47 +0100 Subject: [PATCH 4/4] - Correction of the PdfLine's length formula; - Moving Line to TextLine --- src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs | 2 +- src/UglyToad.PdfPig/Content/{Line.cs => TextLine.cs} | 0 src/UglyToad.PdfPig/Geometry/PdfLine.cs | 3 ++- 3 files changed, 3 insertions(+), 2 deletions(-) rename src/UglyToad.PdfPig/Content/{Line.cs => TextLine.cs} (100%) diff --git a/src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs b/src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs index 3d3fd255..662780b3 100644 --- a/src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs +++ b/src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs @@ -45,7 +45,7 @@ "UglyToad.PdfPig.Content.Page", "UglyToad.PdfPig.Content.PageSize", "UglyToad.PdfPig.Content.Word", - "UglyToad.PdfPig.Content.Line", + "UglyToad.PdfPig.Content.TextLine", "UglyToad.PdfPig.Content.TextDirection", "UglyToad.PdfPig.Core.TransformationMatrix", "UglyToad.PdfPig.CrossReference.CrossReferenceTable", diff --git a/src/UglyToad.PdfPig/Content/Line.cs b/src/UglyToad.PdfPig/Content/TextLine.cs similarity index 100% rename from src/UglyToad.PdfPig/Content/Line.cs rename to src/UglyToad.PdfPig/Content/TextLine.cs diff --git a/src/UglyToad.PdfPig/Geometry/PdfLine.cs b/src/UglyToad.PdfPig/Geometry/PdfLine.cs index 0b8bdb6f..4903db44 100644 --- a/src/UglyToad.PdfPig/Geometry/PdfLine.cs +++ b/src/UglyToad.PdfPig/Geometry/PdfLine.cs @@ -17,7 +17,8 @@ { get { - decimal l = (Point1.X - Point1.X) * (Point1.X - Point1.X) + (Point1.Y - Point1.Y) * (Point1.Y - Point1.Y); + 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); } }