Merge pull request #539 from mvantzet/ExtendLetterProperties

Added Letter properties RenderingMode, StrokeColor, FillColor and add…
This commit is contained in:
Eliot Jones 2023-02-18 10:48:47 +00:00 committed by GitHub
commit 761bce8591
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 86 additions and 31 deletions

View File

@ -77,7 +77,9 @@
letter.Width,
letter.FontSize,
fontDetails,
letter.Color,
letter.RenderingMode,
letter.StrokeColor,
letter.FillColor,
letter.PointSize,
letter.TextSequence);

View File

@ -65,7 +65,9 @@
letter.Width,
letter.FontSize,
letter.Font,
letter.Color,
letter.RenderingMode,
letter.StrokeColor,
letter.FillColor,
letter.PointSize,
letter.TextSequence);
}

View File

@ -0,0 +1,24 @@
namespace UglyToad.PdfPig.Tests.Integration
{
using PdfPig.Core;
using Xunit;
public class PageContentTests
{
[Fact]
public void DetectPageContents()
{
var file = IntegrationHelpers.GetDocumentPath("Various Content Types");
using (var document = PdfDocument.Open(file, ParsingOptions.LenientParsingOff))
{
var page = document.GetPage(1);
var letters = page.Letters;
Assert.Contains(letters, l => l.RenderingMode == TextRenderingMode.Stroke); // "REGULAR TEXT"
Assert.Contains(letters, l => l.RenderingMode == TextRenderingMode.Neither); // "INVISIBLE TEXT"
Assert.NotEmpty(page.Content.GetImages());
Assert.NotEmpty(page.Content.Paths);
}
}
}
}

View File

@ -61,12 +61,33 @@
/// Details about the font for this letter.
/// </summary>
public FontDetails Font { get; }
/// <summary>
/// Text rendering mode that indicates whether we should draw this letter's strokes,
/// fill, both, neither (in case of hidden text), etc.
/// If it calls for stroking the <see cref="StrokeColor" /> is used.
/// If it calls for filling, the <see cref="FillColor"/> is used.
/// In modes that perform both filling and stroking, the effect is as if each glyph outline were filled and then stroked in separate operations.
/// </summary>
public TextRenderingMode RenderingMode { get; }
/// <summary>
/// The color of the letter.
/// The primary color of the letter, which is either the <see cref="StrokeColor"/> in case
/// <see cref="RenderingMode"/> is <see cref="TextRenderingMode.Stroke"/>, or otherwise
/// it is the <see cref="FillColor"/>.
/// </summary>
public IColor Color { get; }
/// <summary>
/// Stroking color
/// </summary>
public IColor StrokeColor { get; }
/// <summary>
/// Non-stroking (fill) color
/// </summary>
public IColor FillColor { get; }
/// <summary>
/// The size of the font in points.
/// </summary>
@ -86,7 +107,9 @@
double width,
double fontSize,
FontDetails font,
IColor color,
TextRenderingMode renderingMode,
IColor strokeColor,
IColor fillColor,
double pointSize,
int textSequence)
{
@ -97,7 +120,17 @@
Width = width;
FontSize = fontSize;
Font = font;
Color = color ?? GrayColor.Black;
RenderingMode = renderingMode;
if (renderingMode == TextRenderingMode.Stroke)
{
Color = StrokeColor = strokeColor ?? GrayColor.Black;
FillColor = fillColor;
}
else
{
Color = FillColor = fillColor ?? GrayColor.Black;
StrokeColor = strokeColor;
}
PointSize = pointSize;
TextSequence = textSequence;
TextOrientation = GetTextOrientation();

View File

@ -292,14 +292,7 @@
var transformedPdfBounds = PerformantRectangleTransformer
.Transform(renderingMatrix, textMatrix, transformationMatrix, new PdfRectangle(0, 0, boundingBox.Width, 0));
// If the text rendering mode calls for filling, the current nonstroking color in the graphics state is used;
// if it calls for stroking, the current stroking color is used.
// In modes that perform both filling and stroking, the effect is as if each glyph outline were filled and then stroked in separate operations.
// TODO: expose color as something more advanced
var color = currentState.FontState.TextRenderingMode != TextRenderingMode.Stroke
? currentState.CurrentNonStrokingColor
: currentState.CurrentStrokingColor;
Letter letter = null;
if (Diacritics.IsInCombiningDiacriticRange(unicode) && bytes.CurrentOffset > 0 && letters.Count > 0)
{
@ -319,26 +312,16 @@
attachTo.Width,
attachTo.FontSize,
attachTo.Font,
attachTo.Color,
attachTo.RenderingMode,
attachTo.StrokeColor,
attachTo.FillColor,
attachTo.PointSize,
attachTo.TextSequence);
}
else
{
letter = new Letter(
unicode,
transformedGlyphBounds,
transformedPdfBounds.BottomLeft,
transformedPdfBounds.BottomRight,
transformedPdfBounds.Width,
fontSize,
font.Details,
color,
pointSize,
textSequence);
}
}
else
// If we did not create a letter for a combined diacritic, create one here.
if (letter == null)
{
letter = new Letter(
unicode,
@ -348,7 +331,9 @@
transformedPdfBounds.Width,
fontSize,
font.Details,
color,
currentState.FontState.TextRenderingMode,
currentState.CurrentStrokingColor,
currentState.CurrentNonStrokingColor,
pointSize,
textSequence);
}

View File

@ -895,7 +895,16 @@
var documentSpace = textMatrix.Transform(renderingMatrix.Transform(fontMatrix.Transform(rect)));
var letter = new Letter(c.ToString(), documentSpace, advanceRect.BottomLeft, advanceRect.BottomRight, width, (double)fontSize, FontDetails.GetDefault(name),
var letter = new Letter(
c.ToString(),
documentSpace,
advanceRect.BottomLeft,
advanceRect.BottomRight,
width,
(double)fontSize,
FontDetails.GetDefault(name),
TextRenderingMode.Fill,
GrayColor.Black,
GrayColor.Black,
(double)fontSize,
textSequence);