Skip to content

Commit 3ebd117

Browse files
committed
Fixed text size calculation for segments containing only line breaks
1 parent 2863bd1 commit 3ebd117

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

PdfSharpCore/Drawing.Layout/XTextSegmentFormatter.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public void DrawString(IEnumerable<TextSegment> textSegments, XRect layoutRectan
8585
textSegments,
8686
layoutRectangle,
8787
format,
88-
(block, dx, dy) => _gfx.DrawString(block.Text, block.Environment.Font, block.Environment.Brush, dx + block.Location.X, dy + block.Location.Y)
88+
(block, dx, dy) => _gfx.DrawString(block.Text, block.Environment.Font, block.Environment.Brush, dx + block.Location.X, dy + block.Location.Y),
89+
false
8990
);
9091
}
9192

@@ -144,7 +145,7 @@ public XSize CalculateTextSize(IEnumerable<TextSegment> textSegments, double wid
144145
var layoutRectangle = new XRect(0, 0, width, 100000000);
145146
var blocks = new List<Block>();
146147

147-
ProcessTextSegments(textSegments, layoutRectangle, format, (block, dx, dy) => blocks.Add(block));
148+
ProcessTextSegments(textSegments, layoutRectangle, format, (block, dx, dy) => blocks.Add(block), true);
148149

149150
var height = blocks.Max(b => b.Location.Y);
150151
var maxLineHeight = 0.0;
@@ -170,7 +171,7 @@ public XSize CalculateTextSize(IEnumerable<TextSegment> textSegments, double wid
170171
return new XSize(calculatedWith, height + maxLineHeight);
171172
}
172173

173-
private void ProcessTextSegments(IEnumerable<TextSegment> textSegments, XRect layoutRectangle, XStringFormat format, Action<Block, double, double> applyBlock)
174+
private void ProcessTextSegments(IEnumerable<TextSegment> textSegments, XRect layoutRectangle, XStringFormat format, Action<Block, double, double> applyBlock, bool applyBlockIfLineBreak)
174175
{
175176
if (textSegments.All(ts => string.IsNullOrEmpty(ts.Text)))
176177
{
@@ -242,7 +243,7 @@ private void ProcessTextSegments(IEnumerable<TextSegment> textSegments, XRect la
242243
break;
243244
}
244245

245-
if (block.Type == BlockType.LineBreak)
246+
if (block.Type == BlockType.LineBreak && !applyBlockIfLineBreak)
246247
{
247248
continue;
248249
}
@@ -397,6 +398,9 @@ private void CreateLayout(List<List<Block>> blockUnits, XRect layoutRectangle)
397398

398399
break;
399400
}
401+
402+
// necessary to correctly calculate closing line breaks
403+
block.Location = new XPoint(0, y);
400404
}
401405
else
402406
{

0 commit comments

Comments
 (0)