Skip to content

Commit 5a06e1e

Browse files
committed
Assess if transformedGlyphBounds and use transformedPdfBounds as fallback and fix #987
1 parent 1298356 commit 5a06e1e

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77

88
public class GithubIssuesTests
99
{
10+
[Fact]
11+
public void Issue987()
12+
{
13+
var path = IntegrationHelpers.GetSpecificTestDocumentPath("zeroheightdemo.pdf");
14+
15+
using (var document = PdfDocument.Open(path))
16+
{
17+
var page = document.GetPage(1);
18+
var words = page.GetWords().ToArray();
19+
foreach (var word in words)
20+
{
21+
Assert.True(word.BoundingBox.Width > 0);
22+
Assert.True(word.BoundingBox.Height > 0);
23+
}
24+
}
25+
}
26+
1027
[Fact]
1128
public void Issue982()
1229
{
Binary file not shown.

src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,20 @@ public override void RenderGlyph(IFont font,
100100
{
101101
var transformedGlyphBounds = PerformantRectangleTransformer
102102
.Transform(renderingMatrix, textMatrix, transformationMatrix, characterBoundingBox.GlyphBounds);
103-
103+
104104
var transformedPdfBounds = PerformantRectangleTransformer
105105
.Transform(renderingMatrix,
106106
textMatrix,
107107
transformationMatrix,
108-
new PdfRectangle(0, 0, characterBoundingBox.Width, 0));
108+
new PdfRectangle(0, 0, characterBoundingBox.Width, UserSpaceUnit.PointMultiples));
109109

110110
if (ParsingOptions.ClipPaths)
111111
{
112112
var currentClipping = currentState.CurrentClippingPath;
113113
if (currentClipping?.IntersectsWith(transformedGlyphBounds) == false)
114+
{
114115
return;
116+
}
115117
}
116118

117119
Letter letter = null;
@@ -141,20 +143,26 @@ public override void RenderGlyph(IFont font,
141143
}
142144
}
143145

146+
// The bbox is assumed to be valid if the width or the height is greater than 0.
147+
// The whitespace letter can have a 0 height and still be a valid bbox.
148+
// This could change in the future (i.e. AND instead of OR).
149+
bool isBboxValid = transformedGlyphBounds.Width > double.Epsilon ||
150+
transformedGlyphBounds.Height > double.Epsilon;
151+
144152
// If we did not create a letter for a combined diacritic, create one here.
145153
if (letter is null)
146154
{
147155
letter = new Letter(
148156
unicode,
149-
transformedGlyphBounds,
157+
isBboxValid ? transformedGlyphBounds : transformedPdfBounds,
150158
transformedPdfBounds.BottomLeft,
151159
transformedPdfBounds.BottomRight,
152160
transformedPdfBounds.Width,
153161
fontSize,
154162
font.Details,
155-
currentState.FontState.TextRenderingMode,
156-
currentState.CurrentStrokingColor!,
157-
currentState.CurrentNonStrokingColor!,
163+
currentState.FontState.TextRenderingMode,
164+
currentState.CurrentStrokingColor!,
165+
currentState.CurrentNonStrokingColor!,
158166
pointSize,
159167
TextSequence);
160168
}

0 commit comments

Comments
 (0)