Skip to content

Commit 9b1e9bd

Browse files
authored
Merge pull request #213 from tinokrueger/features/text-segment-formatter-calculate-text-size-exception
Fixed text segment formatter text size calculation
2 parents 2863bd1 + c5d7df2 commit 9b1e9bd

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

PdfSharpCore/Drawing.Layout/XTextSegmentFormatter.cs

Lines changed: 11 additions & 5 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,9 +145,11 @@ 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

149-
var height = blocks.Max(b => b.Location.Y);
150+
var height = blocks.Any()
151+
? blocks.Max(b => b.Location.Y)
152+
: 0;
150153
var maxLineHeight = 0.0;
151154
for (int i = blocks.Count - 1; i >= 0; i--)
152155
{
@@ -170,7 +173,7 @@ public XSize CalculateTextSize(IEnumerable<TextSegment> textSegments, double wid
170173
return new XSize(calculatedWith, height + maxLineHeight);
171174
}
172175

173-
private void ProcessTextSegments(IEnumerable<TextSegment> textSegments, XRect layoutRectangle, XStringFormat format, Action<Block, double, double> applyBlock)
176+
private void ProcessTextSegments(IEnumerable<TextSegment> textSegments, XRect layoutRectangle, XStringFormat format, Action<Block, double, double> applyBlock, bool applyBlockIfLineBreak)
174177
{
175178
if (textSegments.All(ts => string.IsNullOrEmpty(ts.Text)))
176179
{
@@ -242,7 +245,7 @@ private void ProcessTextSegments(IEnumerable<TextSegment> textSegments, XRect la
242245
break;
243246
}
244247

245-
if (block.Type == BlockType.LineBreak)
248+
if (block.Type == BlockType.LineBreak && !applyBlockIfLineBreak)
246249
{
247250
continue;
248251
}
@@ -397,6 +400,9 @@ private void CreateLayout(List<List<Block>> blockUnits, XRect layoutRectangle)
397400

398401
break;
399402
}
403+
404+
// necessary to correctly calculate closing line breaks
405+
block.Location = new XPoint(0, y);
400406
}
401407
else
402408
{

0 commit comments

Comments
 (0)