Skip to content

Commit fbac724

Browse files
Snipxitext-teamcity
authored andcommitted
Improve leading calculation for consequent lines with different heights
Now layout behaves more like a line-stacking algorithm than a leading-only algorithm. Leading is applied partially as the top spacing before the line and partially as the bottom spacing instead of just being top spacing as before. For consequent lines with different heights this makes a lot of difference in a sense that lines will not overlap DEVSIX-1053 Autoported commit. Original commit hash: [484fa88]
1 parent 9dc074b commit fbac724

File tree

8 files changed

+26
-16
lines changed

8 files changed

+26
-16
lines changed

itext.tests/itext.layout.tests/itext/layout/CollapsingMarginsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public virtual void CollapsingMarginsTest04() {
193193
p.Add("When a man hath no freedom to fight for at home,\n" + " Let him combat for that of his neighbours;\n"
194194
+ "Let him think of the glories of Greece and of Rome,\n" + " And get knocked on the head for his labours.\n"
195195
+ "To do good to Mankind is the chivalrous plan,\n");
196-
p.Add(new Text("small text").SetFontSize(6));
196+
p.Add(new Text("small text").SetFontSize(5.1f));
197197
p.Add("\nAnd is always as nobly requited;\n" + "Then battle for Freedom wherever you can,\n" + "And, if not shot or hanged, you'll get knighted."
198198
);
199199
Div div1 = new Div();
Binary file not shown.
Binary file not shown.
Binary file not shown.

itext/itext.layout/itext/layout/renderer/LineRenderer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,14 @@ protected internal override MinMaxWidth GetMinMaxWidth(float availableWidth) {
691691
return result.GetNotNullMinMaxWidth(availableWidth);
692692
}
693693

694+
internal virtual float GetTopLeadingIndent(Leading leading) {
695+
return (GetLeadingValue(leading) - occupiedArea.GetBBox().GetHeight()) / 2;
696+
}
697+
698+
internal virtual float GetBottomLeadingIndent(Leading leading) {
699+
return (GetLeadingValue(leading) - occupiedArea.GetBBox().GetHeight()) / 2;
700+
}
701+
694702
private LineRenderer[] SplitNotFittingFloat(int childPos, LayoutResult childResult) {
695703
LineRenderer[] split = Split();
696704
split[0].childRenderers.AddAll(childRenderers.SubList(0, childPos));

itext/itext.layout/itext/layout/renderer/ParagraphRenderer.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
157157
}
158158
float lastYLine = layoutBox.GetY() + layoutBox.GetHeight();
159159
Leading leading = this.GetProperty<Leading>(Property.LEADING);
160-
float leadingValue = 0;
161-
float lastLineHeight = 0;
162-
float lastLineLeading = 0;
160+
float lastLineBottomLeadingIndent = 0;
163161
if (marginsCollapsingEnabled && childRenderers.Count > 0) {
164162
// passing null is sufficient to notify that there is a kid, however we don't care about it and it's margins
165163
marginsCollapseHandler.StartChildMarginsHandling(null, layoutBox);
@@ -222,25 +220,30 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
222220
}
223221
}
224222
}
225-
leadingValue = processedRenderer != null && leading != null ? processedRenderer.GetLeadingValue(leading) :
226-
0;
227-
if (processedRenderer != null && processedRenderer.ContainsImage()) {
228-
leadingValue -= previousDescent;
229-
}
230223
bool lineHasContent = processedRenderer != null && processedRenderer.GetOccupiedArea().GetBBox().GetHeight
231224
() > 0;
232225
// could be false if e.g. line contains only floats
233226
bool doesNotFit = processedRenderer == null;
234227
float deltaY = 0;
235228
if (!doesNotFit) {
236229
if (lineHasContent) {
237-
lastLineLeading = leadingValue;
238-
lastLineHeight = processedRenderer.GetOccupiedArea().GetBBox().GetHeight();
239-
deltaY = lastYLine - leadingValue - processedRenderer.GetYLine();
230+
float indentFromLastLine = previousDescent - lastLineBottomLeadingIndent - (leading != null ? processedRenderer
231+
.GetTopLeadingIndent(leading) : 0) - processedRenderer.GetMaxAscent();
232+
// TODO this is a workaround. To be refactored
233+
if (processedRenderer != null && processedRenderer.ContainsImage()) {
234+
indentFromLastLine += previousDescent;
235+
}
236+
deltaY = lastYLine + indentFromLastLine - processedRenderer.GetYLine();
237+
lastLineBottomLeadingIndent = leading != null ? processedRenderer.GetBottomLeadingIndent(leading) : 0;
238+
// TODO this is a workaround. To be refactored
239+
if (lastLineBottomLeadingIndent < 0 && processedRenderer.ContainsImage()) {
240+
lastLineBottomLeadingIndent = 0;
241+
}
240242
}
241243
// for the first and last line in a paragraph, leading is smaller
242244
if (firstLineInBox) {
243-
deltaY = -(leadingValue - lastLineHeight) / 2;
245+
deltaY = processedRenderer != null && leading != null ? -processedRenderer.GetTopLeadingIndent(leading) :
246+
0;
244247
}
245248
doesNotFit = leading != null && processedRenderer.GetOccupiedArea().GetBBox().GetY() + deltaY < layoutBox.
246249
GetY();
@@ -362,8 +365,7 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
362365
if (FloatingHelper.IsRendererFloating(this, floatPropertyValue)) {
363366
FloatingHelper.IncludeChildFloatsInOccupiedArea(floatRendererAreas, this);
364367
}
365-
float moveDown = Math.Min((lastLineLeading - lastLineHeight) / 2, occupiedArea.GetBBox().GetY() - layoutBox
366-
.GetY());
368+
float moveDown = Math.Min(lastLineBottomLeadingIndent, occupiedArea.GetBBox().GetY() - layoutBox.GetY());
367369
occupiedArea.GetBBox().MoveDown(moveDown);
368370
occupiedArea.GetBBox().SetHeight(occupiedArea.GetBBox().GetHeight() + moveDown);
369371
IRenderer overflowRenderer = null;

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
898612dbe6c7a27077e10b543b975bebd887eefb
1+
484fa88ec7cc665e4b684a76472ba230ac66658e

0 commit comments

Comments
 (0)