Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Letter's PointSize computation #229

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/UglyToad.PdfPig.Tests/Fonts/PointSizeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace UglyToad.PdfPig.Tests.Fonts
{
using UglyToad.PdfPig.Tests.Dla;
using Xunit;

public class PointSizeTests
{
[Fact]
public void RotatedText()
{
using (var document = PdfDocument.Open(DlaHelper.GetDocumentPath("complex rotated")))
{
var page = document.GetPage(1);

foreach (var letter in page.Letters)
{
Assert.Equal(12, letter.PointSize);
}
}
}
}
}
1 change: 0 additions & 1 deletion src/UglyToad.PdfPig/Content/Letter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public class Letter

/// <summary>
/// The size of the font in points.
/// <para>This is considered experimental because the calculated value is incorrect for some documents at present.</para>
/// </summary>
public double PointSize { get; }

Expand Down
17 changes: 2 additions & 15 deletions src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,7 @@ public void ShowText(IInputBytes bytes)
var renderingMatrix =
TransformationMatrix.FromValues(fontSize * horizontalScaling, 0, 0, fontSize, 0, rise);

// TODO: this does not seem correct, produces the correct result for now but we need to revisit.
// see: https://stackoverflow.com/questions/48010235/pdf-specification-get-font-size-in-points
var fontSizeMatrix = transformationMatrix.Multiply(TextMatrices.TextMatrix).Multiply(fontSize);
var pointSize = Math.Round(fontSizeMatrix.A, 2);
// Assume a rotated letter
if (pointSize == 0)
{
pointSize = Math.Round(fontSizeMatrix.B, 2);
}

if (pointSize < 0)
{
pointSize *= -1;
}
var pointSize = Math.Round(transformationMatrix.Multiply(TextMatrices.TextMatrix).Transform(new PdfRectangle(0, 0, 1, fontSize)).Height, 2);

while (bytes.MoveNext())
{
Expand Down Expand Up @@ -292,7 +279,7 @@ public void ShowText(IInputBytes bytes)
var boundingBox = font.GetBoundingBox(code);

var transformedGlyphBounds = PerformantRectangleTransformer
.Transform(renderingMatrix, textMatrix, transformationMatrix, boundingBox.GlyphBounds);
.Transform(renderingMatrix, textMatrix, transformationMatrix, boundingBox.GlyphBounds);

var transformedPdfBounds = PerformantRectangleTransformer
.Transform(renderingMatrix, textMatrix, transformationMatrix, new PdfRectangle(0, 0, boundingBox.Width, 0));
Expand Down