diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/DuplicateOverlappingTextProcessor.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/DuplicateOverlappingTextProcessor.cs index e6900dfe..ed7dc87b 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/DuplicateOverlappingTextProcessor.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/DuplicateOverlappingTextProcessor.cs @@ -68,25 +68,11 @@ // update textSequence? // update font details to bold - var fontDetails = new FontDetails(letter.Font.Name, true, letter.Font.Weight, letter.Font.IsItalic); - - var newLetter = new Letter(letter.Value, - letter.GlyphRectangle, - letter.StartBaseLine, - letter.EndBaseLine, - letter.Width, - letter.FontSize, - fontDetails, - letter.RenderingMode, - letter.StrokeColor, - letter.FillColor, - letter.PointSize, - letter.TextSequence); // update markedContentStack? // update letters - cleanLetters[duplicatesOverlappingIndex] = newLetter; + cleanLetters[duplicatesOverlappingIndex] = letter.AsBold(); } } diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/TextExtractor/ContentOrderTextExtractor.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/TextExtractor/ContentOrderTextExtractor.cs index 67ccaefc..1e23d92e 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/TextExtractor/ContentOrderTextExtractor.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/TextExtractor/ContentOrderTextExtractor.cs @@ -64,7 +64,7 @@ letter.EndBaseLine, letter.Width, letter.FontSize, - letter.Font, + letter.GetFont()!, letter.RenderingMode, letter.StrokeColor, letter.FillColor, diff --git a/src/UglyToad.PdfPig.Tests/Dla/UnsupervisedReadingOrderTests.cs b/src/UglyToad.PdfPig.Tests/Dla/UnsupervisedReadingOrderTests.cs index e139a3f4..5964be46 100644 --- a/src/UglyToad.PdfPig.Tests/Dla/UnsupervisedReadingOrderTests.cs +++ b/src/UglyToad.PdfPig.Tests/Dla/UnsupervisedReadingOrderTests.cs @@ -1,14 +1,12 @@ namespace UglyToad.PdfPig.Tests.Dla { - using System; + using PdfFonts; using System.Collections.Generic; using System.Linq; - using System.Text; - using System.Threading.Tasks; using UglyToad.PdfPig.Content; + using UglyToad.PdfPig.Core; using UglyToad.PdfPig.DocumentLayoutAnalysis; using UglyToad.PdfPig.DocumentLayoutAnalysis.ReadingOrderDetector; - using UglyToad.PdfPig.Core; public class UnsupervisedReadingOrderTests { @@ -65,7 +63,7 @@ boundingBox, boundingBox.BottomLeft, boundingBox.BottomRight, - 10, 1, null, TextRenderingMode.NeitherClip, null, null, 0, 0);// These don't matter + 10, 1, (FontDetails)null, TextRenderingMode.NeitherClip, null, null, 0, 0);// These don't matter var leftTextBlock = new TextBlock(new[] { new TextLine(new[] { new Word(new[] { letter }) }) }); return leftTextBlock; } diff --git a/src/UglyToad.PdfPig.Tests/Integration/FontDescriptorTests.cs b/src/UglyToad.PdfPig.Tests/Integration/FontDescriptorTests.cs index ea3f29e3..d064c835 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/FontDescriptorTests.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/FontDescriptorTests.cs @@ -86,8 +86,8 @@ var letter = page.Letters[l]; var expected = DataBoldItalic[l]; Assert.Equal((string)expected[0], letter.Value); - Assert.Equal((bool)expected[1], letter.Font.IsBold); - Assert.Equal((bool)expected[2], letter.Font.IsItalic); + Assert.Equal((bool)expected[1], letter.FontDetails.IsBold); + Assert.Equal((bool)expected[2], letter.FontDetails.IsItalic); } } } diff --git a/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs b/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs index 73746788..4d452137 100644 --- a/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs +++ b/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs @@ -1154,7 +1154,7 @@ { location += letter.Location.X; location += letter.Location.Y; - location += letter.Font.Name.Length; + location += letter.FontDetails.Name.Length; } } } diff --git a/src/UglyToad.PdfPig/Content/Letter.cs b/src/UglyToad.PdfPig/Content/Letter.cs index ef27d1b2..ec4ad313 100644 --- a/src/UglyToad.PdfPig/Content/Letter.cs +++ b/src/UglyToad.PdfPig/Content/Letter.cs @@ -54,12 +54,20 @@ /// /// The name of the font. /// - public string? FontName => Font?.Name; + public string? FontName => FontDetails?.Name; /// /// Details about the font for this letter. /// - public FontDetails Font { get; } + public FontDetails FontDetails { get; } + + /// + /// Details about the font for this letter. + /// + [Obsolete("Use FontDetails instead.")] + public FontDetails Font => FontDetails; + + private readonly IFont? _font; /// /// Text rendering mode that indicates whether we should draw this letter's strokes, @@ -95,17 +103,59 @@ /// /// Sequence number of the ShowText operation that printed this letter. /// - public int TextSequence { get; } + public int TextSequence { get; } /// /// Create a new letter to represent some text drawn by the Tj operator. /// - public Letter(string value, PdfRectangle glyphRectangle, + public Letter(string value, + PdfRectangle glyphRectangle, PdfPoint startBaseLine, PdfPoint endBaseLine, double width, double fontSize, - FontDetails font, + IFont font, + TextRenderingMode renderingMode, + IColor strokeColor, + IColor fillColor, + double pointSize, + int textSequence) : + this(value, glyphRectangle, + startBaseLine, endBaseLine, + width, fontSize, font.Details, font, + renderingMode, strokeColor, fillColor, + pointSize, textSequence) + { } + + /// + /// Create a new letter to represent some text drawn by the Tj operator. + /// + public Letter(string value, + PdfRectangle glyphRectangle, + PdfPoint startBaseLine, + PdfPoint endBaseLine, + double width, + double fontSize, + FontDetails fontDetails, + TextRenderingMode renderingMode, + IColor strokeColor, + IColor fillColor, + double pointSize, + int textSequence): + this(value, glyphRectangle, + startBaseLine, endBaseLine, + width, fontSize, fontDetails, null, + renderingMode, strokeColor, fillColor, + pointSize, textSequence) + { } + + private Letter(string value, PdfRectangle glyphRectangle, + PdfPoint startBaseLine, + PdfPoint endBaseLine, + double width, + double fontSize, + FontDetails fontDetails, + IFont? font, TextRenderingMode renderingMode, IColor strokeColor, IColor fillColor, @@ -118,7 +168,8 @@ EndBaseLine = endBaseLine; Width = width; FontSize = fontSize; - Font = font; + FontDetails = fontDetails; + _font = font; RenderingMode = renderingMode; if (renderingMode == TextRenderingMode.Stroke) { @@ -135,6 +186,42 @@ TextOrientation = GetTextOrientation(); } + /// + /// Creates a new instance with the same properties as the current instance, + /// but with the font details set to bold. + /// + /// + /// A new instance with bold font details. + /// + public Letter AsBold() + { + return new Letter(Value, + GlyphRectangle, + StartBaseLine, + EndBaseLine, + Width, + FontSize, + FontDetails.AsBold(), + _font, + RenderingMode, + StrokeColor, + FillColor, + PointSize, + TextSequence); + } + + /// + /// Retrieves the font associated with this letter, if available. + /// + /// + /// The instance representing the font used for this letter, + /// or null if no font is associated. + /// + public IFont? GetFont() + { + return _font; + } + private TextOrientation GetTextOrientation() { if (Math.Abs(StartBaseLine.Y - EndBaseLine.Y) < 10e-5) diff --git a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs index 9414f13d..ac52c85e 100644 --- a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs @@ -133,7 +133,7 @@ namespace UglyToad.PdfPig.Graphics attachTo.EndBaseLine, attachTo.Width, attachTo.FontSize, - attachTo.Font, + attachTo.GetFont()!, attachTo.RenderingMode, attachTo.StrokeColor, attachTo.FillColor, @@ -158,7 +158,7 @@ namespace UglyToad.PdfPig.Graphics transformedPdfBounds.BottomRight, transformedPdfBounds.Width, fontSize, - font.Details, + font, currentState.FontState.TextRenderingMode, currentState.CurrentStrokingColor!, currentState.CurrentNonStrokingColor!, diff --git a/src/UglyToad.PdfPig/PdfFonts/FontDetails.cs b/src/UglyToad.PdfPig/PdfFonts/FontDetails.cs index e49e74cc..bd606671 100644 --- a/src/UglyToad.PdfPig/PdfFonts/FontDetails.cs +++ b/src/UglyToad.PdfPig/PdfFonts/FontDetails.cs @@ -3,7 +3,7 @@ /// /// Summary details of the font used to draw a glyph. /// - public class FontDetails + public sealed class FontDetails { /// /// The normal weight for a font. @@ -35,6 +35,8 @@ /// public bool IsItalic { get; } + private readonly Lazy _bold; + /// /// Create a new . /// @@ -44,6 +46,17 @@ IsBold = isBold; Weight = weight; IsItalic = isItalic; + + _bold = isBold ? new Lazy(() => this) : new Lazy(() => new FontDetails(Name, true, Weight, IsItalic)); + } + + /// + /// An instance of with the same properties as the current instance, + /// but with the property set to true. + /// + public FontDetails AsBold() + { + return _bold.Value; } internal static FontDetails GetDefault(string? name = null) => new FontDetails(name ?? string.Empty, @@ -51,7 +64,7 @@ DefaultWeight, false); - internal FontDetails WithName(string name) => name != null + internal FontDetails WithName(string? name) => name is not null ? new FontDetails(name, IsBold, Weight, IsItalic) : this;