Expose letter's font via GetFont(), make Font property as obsolete and use FontDetails instead
Some checks failed
Build, test and publish draft / build (push) Has been cancelled
Build and test [MacOS] / build (push) Has been cancelled
Run Common Crawl Tests / build (0000-0001) (push) Has been cancelled
Run Common Crawl Tests / build (0002-0003) (push) Has been cancelled
Run Common Crawl Tests / build (0004-0005) (push) Has been cancelled
Run Common Crawl Tests / build (0006-0007) (push) Has been cancelled
Run Integration Tests / build (push) Has been cancelled
Nightly Release / Check if this commit has already been published (push) Has been cancelled
Nightly Release / tests (push) Has been cancelled
Nightly Release / build_and_publish_nightly (push) Has been cancelled

This commit is contained in:
BobLd
2025-09-20 13:46:25 +01:00
parent a53d96cb73
commit 008959457a
8 changed files with 118 additions and 34 deletions

View File

@@ -68,25 +68,11 @@
// update textSequence? // update textSequence?
// update font details to bold // 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 markedContentStack?
// update letters // update letters
cleanLetters[duplicatesOverlappingIndex] = newLetter; cleanLetters[duplicatesOverlappingIndex] = letter.AsBold();
} }
} }

View File

@@ -64,7 +64,7 @@
letter.EndBaseLine, letter.EndBaseLine,
letter.Width, letter.Width,
letter.FontSize, letter.FontSize,
letter.Font, letter.GetFont()!,
letter.RenderingMode, letter.RenderingMode,
letter.StrokeColor, letter.StrokeColor,
letter.FillColor, letter.FillColor,

View File

@@ -1,14 +1,12 @@
namespace UglyToad.PdfPig.Tests.Dla namespace UglyToad.PdfPig.Tests.Dla
{ {
using System; using PdfFonts;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UglyToad.PdfPig.Content; using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.Core;
using UglyToad.PdfPig.DocumentLayoutAnalysis; using UglyToad.PdfPig.DocumentLayoutAnalysis;
using UglyToad.PdfPig.DocumentLayoutAnalysis.ReadingOrderDetector; using UglyToad.PdfPig.DocumentLayoutAnalysis.ReadingOrderDetector;
using UglyToad.PdfPig.Core;
public class UnsupervisedReadingOrderTests public class UnsupervisedReadingOrderTests
{ {
@@ -65,7 +63,7 @@
boundingBox, boundingBox,
boundingBox.BottomLeft, boundingBox.BottomLeft,
boundingBox.BottomRight, 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 }) }) }); var leftTextBlock = new TextBlock(new[] { new TextLine(new[] { new Word(new[] { letter }) }) });
return leftTextBlock; return leftTextBlock;
} }

View File

@@ -86,8 +86,8 @@
var letter = page.Letters[l]; var letter = page.Letters[l];
var expected = DataBoldItalic[l]; var expected = DataBoldItalic[l];
Assert.Equal((string)expected[0], letter.Value); Assert.Equal((string)expected[0], letter.Value);
Assert.Equal((bool)expected[1], letter.Font.IsBold); Assert.Equal((bool)expected[1], letter.FontDetails.IsBold);
Assert.Equal((bool)expected[2], letter.Font.IsItalic); Assert.Equal((bool)expected[2], letter.FontDetails.IsItalic);
} }
} }
} }

View File

@@ -1154,7 +1154,7 @@
{ {
location += letter.Location.X; location += letter.Location.X;
location += letter.Location.Y; location += letter.Location.Y;
location += letter.Font.Name.Length; location += letter.FontDetails.Name.Length;
} }
} }
} }

View File

@@ -54,12 +54,20 @@
/// <summary> /// <summary>
/// The name of the font. /// The name of the font.
/// </summary> /// </summary>
public string? FontName => Font?.Name; public string? FontName => FontDetails?.Name;
/// <summary> /// <summary>
/// Details about the font for this letter. /// Details about the font for this letter.
/// </summary> /// </summary>
public FontDetails Font { get; } public FontDetails FontDetails { get; }
/// <summary>
/// Details about the font for this letter.
/// </summary>
[Obsolete("Use FontDetails instead.")]
public FontDetails Font => FontDetails;
private readonly IFont? _font;
/// <summary> /// <summary>
/// Text rendering mode that indicates whether we should draw this letter's strokes, /// Text rendering mode that indicates whether we should draw this letter's strokes,
@@ -95,17 +103,59 @@
/// <summary> /// <summary>
/// Sequence number of the ShowText operation that printed this letter. /// Sequence number of the ShowText operation that printed this letter.
/// </summary> /// </summary>
public int TextSequence { get; } public int TextSequence { get; }
/// <summary> /// <summary>
/// Create a new letter to represent some text drawn by the Tj operator. /// Create a new letter to represent some text drawn by the Tj operator.
/// </summary> /// </summary>
public Letter(string value, PdfRectangle glyphRectangle, public Letter(string value,
PdfRectangle glyphRectangle,
PdfPoint startBaseLine, PdfPoint startBaseLine,
PdfPoint endBaseLine, PdfPoint endBaseLine,
double width, double width,
double fontSize, 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)
{ }
/// <summary>
/// Create a new letter to represent some text drawn by the Tj operator.
/// </summary>
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, TextRenderingMode renderingMode,
IColor strokeColor, IColor strokeColor,
IColor fillColor, IColor fillColor,
@@ -118,7 +168,8 @@
EndBaseLine = endBaseLine; EndBaseLine = endBaseLine;
Width = width; Width = width;
FontSize = fontSize; FontSize = fontSize;
Font = font; FontDetails = fontDetails;
_font = font;
RenderingMode = renderingMode; RenderingMode = renderingMode;
if (renderingMode == TextRenderingMode.Stroke) if (renderingMode == TextRenderingMode.Stroke)
{ {
@@ -135,6 +186,42 @@
TextOrientation = GetTextOrientation(); TextOrientation = GetTextOrientation();
} }
/// <summary>
/// Creates a new <see cref="Letter"/> instance with the same properties as the current instance,
/// but with the font details set to bold.
/// </summary>
/// <returns>
/// A new <see cref="Letter"/> instance with bold font details.
/// </returns>
public Letter AsBold()
{
return new Letter(Value,
GlyphRectangle,
StartBaseLine,
EndBaseLine,
Width,
FontSize,
FontDetails.AsBold(),
_font,
RenderingMode,
StrokeColor,
FillColor,
PointSize,
TextSequence);
}
/// <summary>
/// Retrieves the font associated with this letter, if available.
/// </summary>
/// <returns>
/// The <see cref="IFont"/> instance representing the font used for this letter,
/// or <c>null</c> if no font is associated.
/// </returns>
public IFont? GetFont()
{
return _font;
}
private TextOrientation GetTextOrientation() private TextOrientation GetTextOrientation()
{ {
if (Math.Abs(StartBaseLine.Y - EndBaseLine.Y) < 10e-5) if (Math.Abs(StartBaseLine.Y - EndBaseLine.Y) < 10e-5)

View File

@@ -133,7 +133,7 @@ namespace UglyToad.PdfPig.Graphics
attachTo.EndBaseLine, attachTo.EndBaseLine,
attachTo.Width, attachTo.Width,
attachTo.FontSize, attachTo.FontSize,
attachTo.Font, attachTo.GetFont()!,
attachTo.RenderingMode, attachTo.RenderingMode,
attachTo.StrokeColor, attachTo.StrokeColor,
attachTo.FillColor, attachTo.FillColor,
@@ -158,7 +158,7 @@ namespace UglyToad.PdfPig.Graphics
transformedPdfBounds.BottomRight, transformedPdfBounds.BottomRight,
transformedPdfBounds.Width, transformedPdfBounds.Width,
fontSize, fontSize,
font.Details, font,
currentState.FontState.TextRenderingMode, currentState.FontState.TextRenderingMode,
currentState.CurrentStrokingColor!, currentState.CurrentStrokingColor!,
currentState.CurrentNonStrokingColor!, currentState.CurrentNonStrokingColor!,

View File

@@ -3,7 +3,7 @@
/// <summary> /// <summary>
/// Summary details of the font used to draw a glyph. /// Summary details of the font used to draw a glyph.
/// </summary> /// </summary>
public class FontDetails public sealed class FontDetails
{ {
/// <summary> /// <summary>
/// The normal weight for a font. /// The normal weight for a font.
@@ -35,6 +35,8 @@
/// </summary> /// </summary>
public bool IsItalic { get; } public bool IsItalic { get; }
private readonly Lazy<FontDetails> _bold;
/// <summary> /// <summary>
/// Create a new <see cref="FontDetails"/>. /// Create a new <see cref="FontDetails"/>.
/// </summary> /// </summary>
@@ -44,6 +46,17 @@
IsBold = isBold; IsBold = isBold;
Weight = weight; Weight = weight;
IsItalic = isItalic; IsItalic = isItalic;
_bold = isBold ? new Lazy<FontDetails>(() => this) : new Lazy<FontDetails>(() => new FontDetails(Name, true, Weight, IsItalic));
}
/// <summary>
/// An instance of <see cref="FontDetails"/> with the same properties as the current instance,
/// but with the <see cref="IsBold"/> property set to <c>true</c>.
/// </summary>
public FontDetails AsBold()
{
return _bold.Value;
} }
internal static FontDetails GetDefault(string? name = null) => new FontDetails(name ?? string.Empty, internal static FontDetails GetDefault(string? name = null) => new FontDetails(name ?? string.Empty,
@@ -51,7 +64,7 @@
DefaultWeight, DefaultWeight,
false); false);
internal FontDetails WithName(string name) => name != null internal FontDetails WithName(string? name) => name is not null
? new FontDetails(name, IsBold, Weight, IsItalic) ? new FontDetails(name, IsBold, Weight, IsItalic)
: this; : this;