Skip to content

Commit d41cfef

Browse files
rjcohnIdamkinI
authored andcommitted
Fix alignment bug when floats present
Text is laid out taking into account the float width, but the alignment value for center, right, and justified ignore the float. Autoported commit. Original commit hash: [0bdcb54c5]
1 parent 416d572 commit d41cfef

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

itext.tests/itext.layout.tests/itext/layout/AlignmentTest.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ source product.
4646
using iText.IO.Util;
4747
using iText.Kernel.Colors;
4848
using iText.Kernel.Font;
49+
using iText.Kernel.Geom;
4950
using iText.Kernel.Pdf;
5051
using iText.Kernel.Pdf.Xobject;
5152
using iText.Kernel.Utils;
@@ -269,5 +270,42 @@ public virtual void ImageAlignmentTest01() {
269270
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
270271
, "diff"));
271272
}
273+
274+
/// <exception cref="System.IO.IOException"/>
275+
/// <exception cref="System.Exception"/>
276+
[NUnit.Framework.Test]
277+
public virtual void FloatAlignmentTest01() {
278+
String outFileName = destinationFolder + "floatAlignmentTest01.pdf";
279+
String cmpFileName = sourceFolder + "cmp_floatAlignmentTest01.pdf";
280+
PdfWriter writer = new PdfWriter(outFileName);
281+
PdfDocument pdfDoc = new PdfDocument(writer);
282+
pdfDoc.SetDefaultPageSize(new PageSize(350, 450));
283+
Document doc = new Document(pdfDoc);
284+
AddFloatAndText(doc, FloatPropertyValue.RIGHT);
285+
AddFloatAndText(doc, FloatPropertyValue.LEFT);
286+
doc.Add(new AreaBreak());
287+
doc.Add(new Paragraph("All lines after this one have first line indent = 20. " + "Float left is correct, right is not."
288+
));
289+
doc.SetProperty(Property.FIRST_LINE_INDENT, 20f);
290+
AddFloatAndText(doc, FloatPropertyValue.RIGHT);
291+
// Alignment is incorrect because indent is replaced by float adjustment
292+
AddFloatAndText(doc, FloatPropertyValue.LEFT);
293+
doc.Close();
294+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
295+
, "diff"));
296+
}
297+
298+
private static void AddFloatAndText(Document doc, FloatPropertyValue? floatPropertyValue) {
299+
Div div = new Div();
300+
div.SetWidth(150).SetHeight(120);
301+
div.SetProperty(Property.FLOAT, floatPropertyValue);
302+
div.SetBorder(new SolidBorder(1));
303+
doc.Add(div);
304+
doc.Add(new Paragraph("Left aligned.").SetTextAlignment(TextAlignment.LEFT));
305+
doc.Add(new Paragraph("Right aligned.").SetTextAlignment(TextAlignment.RIGHT));
306+
doc.Add(new Paragraph("Center aligned.").SetTextAlignment(TextAlignment.CENTER));
307+
doc.Add(new Paragraph("Justified. " + "The text is laid out using the correct width, but the alignment value uses the full width."
308+
).SetTextAlignment(TextAlignment.JUSTIFIED));
309+
}
272310
}
273311
}
Binary file not shown.

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,20 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
200200
if (result.GetStatus() == LayoutResult.PARTIAL && textAlignment == TextAlignment.JUSTIFIED && !result.IsSplitForcedByNewline
201201
() || textAlignment == TextAlignment.JUSTIFIED_ALL) {
202202
if (processedRenderer != null) {
203-
processedRenderer.Justify(layoutBox.GetWidth() - lineIndent);
203+
//processedRenderer.justify(layoutBox.getWidth() - lineIndent);
204+
//7.0.5 fix: processedRenderer.justify(result.getMinMaxWidth().getAvailableWidth() - lineIndent);
205+
Rectangle floatBox = layoutBox.Clone();
206+
FloatingHelper.AdjustLineAreaAccordingToFloats(floatRendererAreas, floatBox);
207+
processedRenderer.Justify(floatBox.GetWidth() - lineIndent);
204208
}
205209
}
206210
else {
207211
if (textAlignment != TextAlignment.LEFT && processedRenderer != null) {
208-
float deltaX = childBBoxWidth - processedRenderer.GetOccupiedArea().GetBBox().GetWidth();
212+
//float deltaX = childBBoxWidth - processedRenderer.getOccupiedArea().getBBox().getWidth();
213+
//7.0.5 fix: float deltaX = result.getMinMaxWidth().getAvailableWidth() - processedRenderer.getOccupiedArea().getBBox().getWidth();
214+
Rectangle floatBox = layoutBox.Clone();
215+
FloatingHelper.AdjustLineAreaAccordingToFloats(floatRendererAreas, floatBox);
216+
float deltaX = floatBox.GetWidth() - lineIndent - processedRenderer.GetOccupiedArea().GetBBox().GetWidth();
209217
switch (textAlignment) {
210218
case TextAlignment.RIGHT: {
211219
processedRenderer.Move(deltaX, 0);

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
899d2966ee00f8d2a6014855ad65136e29b54e39
1+
0bdcb54c5ec72e3c5865fe0a56a29f1cbccff83f

0 commit comments

Comments
 (0)