Skip to content

Commit e95363d

Browse files
committed
Fix overflow functionality to process rotation and long words correctly. Minor changes.
DEVSIX-992 Autoported commit. Original commit hash: [60234f2]
1 parent 070daa9 commit e95363d

File tree

10 files changed

+198
-55
lines changed

10 files changed

+198
-55
lines changed

itext.tests/itext.layout.tests/itext/layout/BlockTest.cs

Lines changed: 163 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public class BlockTest : ExtendedITextTest {
6060
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
6161
+ "/test/itext/layout/BlockTest/";
6262

63+
private const String textByronNarrow = "When a man hath no freedom to fight for at home, " + "Let him combat for that of his neighbours; "
64+
+ "Let him think of the glories of Greece and of Rome, " + "And get knocked on the head for his labours. "
65+
+ "\n" + "To do good to Mankind is the chivalrous plan, " + "And is always as nobly requited; " + "Then battle for Freedom wherever you can, "
66+
+ "And, if not shot or hanged, you'll get knighted.";
67+
68+
private const String textByron = "When a man hath no freedom to fight for at home,\n" + " Let him combat for that of his neighbours;\n"
69+
+ "Let him think of the glories of Greece and of Rome,\n" + " And get knocked on the head for his labours.\n"
70+
+ "\n" + "To do good to Mankind is the chivalrous plan,\n" + " And is always as nobly requited;\n"
71+
+ "Then battle for Freedom wherever you can,\n" + " And, if not shot or hanged, you'll get knighted.";
72+
6373
[NUnit.Framework.OneTimeSetUp]
6474
public static void BeforeClass() {
6575
CreateOrClearDestinationFolder(destinationFolder);
@@ -73,10 +83,6 @@ public virtual void BlockWithSetHeightProperties01() {
7383
String outFileName = destinationFolder + "blockWithSetHeightProperties01.pdf";
7484
String cmpFileName = sourceFolder + "cmp_blockWithSetHeightProperties01.pdf";
7585
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
76-
String textByron = "When a man hath no freedom to fight for at home,\n" + " Let him combat for that of his neighbours;\n"
77-
+ "Let him think of the glories of Greece and of Rome,\n" + " And get knocked on the head for his labours.\n"
78-
+ "\n" + "To do good to Mankind is the chivalrous plan,\n" + " And is always as nobly requited;\n"
79-
+ "Then battle for Freedom wherever you can,\n" + " And, if not shot or hanged, you'll get knighted.";
8086
Document doc = new Document(pdfDocument);
8187
Paragraph p = new Paragraph(textByron);
8288
for (int i = 0; i < 10; i++) {
@@ -127,10 +133,6 @@ public virtual void BlockWithSetHeightProperties02() {
127133
String outFileName = destinationFolder + "blockWithSetHeightProperties02.pdf";
128134
String cmpFileName = sourceFolder + "cmp_blockWithSetHeightProperties02.pdf";
129135
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
130-
String textByron = "When a man hath no freedom to fight for at home,\n" + " Let him combat for that of his neighbours;\n"
131-
+ "Let him think of the glories of Greece and of Rome,\n" + " And get knocked on the head for his labours.\n"
132-
+ "\n" + "To do good to Mankind is the chivalrous plan,\n" + " And is always as nobly requited;\n"
133-
+ "Then battle for Freedom wherever you can,\n" + " And, if not shot or hanged, you'll get knighted.";
134136
Document doc = new Document(pdfDocument);
135137
Paragraph p = new Paragraph(textByron);
136138
Div div = new Div();
@@ -177,6 +179,159 @@ public virtual void BlockWithSetHeightProperties02() {
177179
, "diff"));
178180
}
179181

182+
/// <exception cref="System.IO.IOException"/>
183+
/// <exception cref="System.Exception"/>
184+
[NUnit.Framework.Test]
185+
public virtual void OverflowTest01() {
186+
// TODO DEVSIX-1373
187+
String outFileName = destinationFolder + "overflowTest01.pdf";
188+
String cmpFileName = sourceFolder + "cmp_overflowTest01.pdf";
189+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
190+
Document doc = new Document(pdfDocument);
191+
Paragraph explanation = new Paragraph("In this sample iText will not try to fit text in container's width, because overflow property is set. However no text is hidden."
192+
);
193+
doc.Add(explanation);
194+
Paragraph p = new Paragraph(textByronNarrow);
195+
p.SetWidth(200);
196+
p.SetBorder(new SolidBorder(Color.BLUE, 1));
197+
p.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.HIDDEN);
198+
Div div = new Div();
199+
div.SetWidth(100);
200+
div.SetBorder(new SolidBorder(Color.BLACK, 1));
201+
div.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
202+
div.Add(p);
203+
doc.Add(div);
204+
doc.Close();
205+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
206+
, "diff"));
207+
}
208+
209+
/// <exception cref="System.IO.IOException"/>
210+
/// <exception cref="System.Exception"/>
211+
[NUnit.Framework.Test]
212+
public virtual void OverflowTest02() {
213+
String outFileName = destinationFolder + "overflowTest02.pdf";
214+
String cmpFileName = sourceFolder + "cmp_overflowTest02.pdf";
215+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
216+
Document doc = new Document(pdfDocument);
217+
Paragraph p = new Paragraph();
218+
p.SetWidth(200);
219+
p.SetHeight(100);
220+
p.SetBorder(new SolidBorder(Color.BLUE, 1));
221+
p.SetBackgroundColor(Color.YELLOW);
222+
for (int i = 0; i < 10; i++) {
223+
p.Add(textByronNarrow);
224+
}
225+
p.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);
226+
doc.Add(p);
227+
doc.Add(new Paragraph("Hello!!!").SetBackgroundColor(Color.RED));
228+
doc.Close();
229+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
230+
, "diff"));
231+
}
232+
233+
/// <exception cref="System.IO.IOException"/>
234+
/// <exception cref="System.Exception"/>
235+
[NUnit.Framework.Test]
236+
public virtual void OverflowTest03() {
237+
String outFileName = destinationFolder + "overflowTest03.pdf";
238+
String cmpFileName = sourceFolder + "cmp_overflowTest03.pdf";
239+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
240+
Document doc = new Document(pdfDocument);
241+
Paragraph p = new Paragraph();
242+
p.SetWidth(1400);
243+
p.SetHeight(1400);
244+
p.SetBorder(new SolidBorder(Color.BLUE, 1));
245+
p.SetBackgroundColor(Color.YELLOW);
246+
for (int i = 0; i < 100; i++) {
247+
p.Add(textByronNarrow);
248+
}
249+
p.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);
250+
p.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
251+
doc.Add(p);
252+
doc.Add(new Paragraph("Hello!!!").SetBackgroundColor(Color.RED));
253+
doc.Close();
254+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
255+
, "diff"));
256+
}
257+
258+
/// <exception cref="System.IO.IOException"/>
259+
/// <exception cref="System.Exception"/>
260+
[NUnit.Framework.Ignore("DEVSIX-1375")]
261+
[NUnit.Framework.Test]
262+
public virtual void OverflowTest04() {
263+
String outFileName = destinationFolder + "overflowTest04.pdf";
264+
String cmpFileName = sourceFolder + "cmp_overflowTest04.pdf";
265+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
266+
iText.Layout.Element.Image image = new iText.Layout.Element.Image(ImageDataFactory.Create(sourceFolder + "Desert.jpg"
267+
));
268+
image.SetWidth(200);
269+
Document doc = new Document(pdfDocument);
270+
Paragraph p = new Paragraph();
271+
p.SetRotationAngle(Math.PI / 2);
272+
p.SetWidth(100);
273+
p.SetHeight(100);
274+
p.SetBorder(new SolidBorder(Color.BLUE, 1));
275+
p.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);
276+
p.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
277+
p.Add(image);
278+
doc.Add(p);
279+
doc.Add(new Paragraph("Hello!!!").SetBackgroundColor(Color.RED));
280+
doc.Close();
281+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
282+
, "diff"));
283+
}
284+
285+
/// <exception cref="System.IO.IOException"/>
286+
/// <exception cref="System.Exception"/>
287+
[NUnit.Framework.Ignore("DEVSIX-1373")]
288+
[NUnit.Framework.Test]
289+
public virtual void OverflowTest05() {
290+
String outFileName = destinationFolder + "overflowTest05.pdf";
291+
String cmpFileName = sourceFolder + "cmp_overflowTest05.pdf";
292+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
293+
Document doc = new Document(pdfDocument);
294+
Div div = new Div();
295+
div.SetWidth(100);
296+
div.SetHeight(150);
297+
div.SetBackgroundColor(Color.GREEN);
298+
div.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
299+
div.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);
300+
List list = new List();
301+
list.Add("Make Greeeeeeeeeetzky Great Again");
302+
list.Add("Greeeeeeeeeetzky Great Again Make");
303+
list.Add("Great Again Make Greeeeeeeeeetzky");
304+
list.Add("Again Make Greeeeeeeeeetzky Great");
305+
div.Add(list);
306+
doc.Add(div);
307+
doc.Add(new Paragraph("Hello!!!").SetBackgroundColor(Color.RED));
308+
doc.Close();
309+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
310+
, "diff"));
311+
}
312+
313+
/// <exception cref="System.IO.IOException"/>
314+
/// <exception cref="System.Exception"/>
315+
[NUnit.Framework.Ignore("DEVSIX-1373")]
316+
[NUnit.Framework.Test]
317+
public virtual void OverflowTest06() {
318+
String outFileName = destinationFolder + "overflowTest06.pdf";
319+
String cmpFileName = sourceFolder + "cmp_overflowTest06.pdf";
320+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
321+
Document doc = new Document(pdfDocument);
322+
Div div = new Div();
323+
div.SetWidth(100);
324+
div.SetHeight(100);
325+
div.SetBackgroundColor(Color.GREEN);
326+
div.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);
327+
div.Add(new Paragraph(textByron));
328+
doc.Add(div);
329+
doc.Add(new Paragraph("Hello!!!").SetBackgroundColor(Color.RED));
330+
doc.Close();
331+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
332+
, "diff"));
333+
}
334+
180335
/// <exception cref="System.IO.IOException"/>
181336
/// <exception cref="System.Exception"/>
182337
[NUnit.Framework.Test]
Binary file not shown.

itext/itext.layout/itext/layout/renderer/BlockRenderer.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
172172
if (result.GetOccupiedArea() != null && result.GetStatus() != LayoutResult.NOTHING) {
173173
occupiedArea.SetBBox(Rectangle.GetCommonRectangle(occupiedArea.GetBBox(), result.GetOccupiedArea().GetBBox
174174
()));
175-
if (occupiedArea.GetBBox().GetWidth() > layoutBox.GetWidth()) {
175+
if (occupiedArea.GetBBox().GetWidth() > layoutBox.GetWidth() && !(null == overflowX || OverflowPropertyValue
176+
.FIT.Equals(overflowX))) {
176177
occupiedArea.GetBBox().SetWidth(layoutBox.GetWidth());
177178
}
178179
}
@@ -335,7 +336,8 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
335336
// this check is needed only if margins collapsing is enabled
336337
occupiedArea.SetBBox(Rectangle.GetCommonRectangle(occupiedArea.GetBBox(), result.GetOccupiedArea().GetBBox
337338
()));
338-
if (occupiedArea.GetBBox().GetWidth() > layoutBox.GetWidth()) {
339+
if (occupiedArea.GetBBox().GetWidth() > layoutBox.GetWidth() && !(null == overflowX || OverflowPropertyValue
340+
.FIT.Equals(overflowX))) {
339341
occupiedArea.GetBBox().SetWidth(layoutBox.GetWidth());
340342
}
341343
}
@@ -442,6 +444,9 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
442444
}
443445
}
444446
ApplyVerticalAlignment();
447+
if (wasHeightClipped) {
448+
occupiedArea.GetBBox().MoveUp(overflowPartHeight).DecreaseHeight(overflowPartHeight);
449+
}
445450
FloatingHelper.RemoveFloatsAboveRendererBottom(floatRendererAreas, this);
446451
LayoutArea editedArea_1 = FloatingHelper.AdjustResultOccupiedAreaForFloatAndClear(this, layoutContext.GetFloatRendererAreas
447452
(), layoutContext.GetArea().GetBBox(), clearHeightCorrection, marginsCollapsingEnabled);
@@ -457,10 +462,6 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
457462
}
458463
}
459464
}
460-
if (wasHeightClipped) {
461-
editedArea_1.GetBBox().MoveUp(overflowPartHeight).DecreaseHeight(overflowPartHeight);
462-
occupiedArea.GetBBox().MoveUp(overflowPartHeight).DecreaseHeight(overflowPartHeight);
463-
}
464465
if (null == overflowRenderer_1) {
465466
return new LayoutResult(LayoutResult.FULL, editedArea_1, null, null, causeOfNothing);
466467
}

itext/itext.layout/itext/layout/renderer/FloatingHelper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ internal static void RemoveFloatsAboveRendererBottom(IList<Rectangle> floatRende
228228
internal static LayoutArea AdjustResultOccupiedAreaForFloatAndClear(IRenderer renderer, IList<Rectangle> floatRendererAreas
229229
, Rectangle parentBBox, float clearHeightCorrection, bool marginsCollapsingEnabled) {
230230
LayoutArea occupiedArea = renderer.GetOccupiedArea();
231-
LayoutArea editedArea = occupiedArea.Clone();
231+
LayoutArea editedArea = occupiedArea;
232232
if (IsRendererFloating(renderer)) {
233+
editedArea = occupiedArea.Clone();
233234
if (occupiedArea.GetBBox().GetWidth() > 0) {
234235
floatRendererAreas.Add(occupiedArea.GetBBox());
235236
}
@@ -238,6 +239,7 @@ internal static LayoutArea AdjustResultOccupiedAreaForFloatAndClear(IRenderer re
238239
}
239240
else {
240241
if (clearHeightCorrection > 0 && !marginsCollapsingEnabled) {
242+
editedArea = occupiedArea.Clone();
241243
editedArea.GetBBox().IncreaseHeight(clearHeightCorrection);
242244
}
243245
}

itext/itext.layout/itext/layout/renderer/ImageRenderer.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -326,26 +326,8 @@ public override void Draw(DrawContext drawContext) {
326326
}
327327
PdfXObject xObject = ((Image)(GetModelElement())).GetXObject();
328328
BeginElementOpacityApplying(drawContext);
329-
OverflowPropertyValue? overflowX = this.parent.GetProperty<OverflowPropertyValue?>(Property.OVERFLOW_X);
330-
OverflowPropertyValue? overflowY = this.parent.GetProperty<OverflowPropertyValue?>(Property.OVERFLOW_Y);
331-
bool processOverflow = OverflowPropertyValue.HIDDEN.Equals(overflowX) || OverflowPropertyValue.HIDDEN.Equals
332-
(overflowY);
333-
if (processOverflow) {
334-
drawContext.GetCanvas().SaveState();
335-
Rectangle clippedArea = drawContext.GetDocument().GetPage(occupiedArea.GetPageNumber()).GetPageSize();
336-
if (OverflowPropertyValue.HIDDEN.Equals(overflowX)) {
337-
clippedArea.SetX(occupiedArea.GetBBox().GetX()).SetWidth(occupiedArea.GetBBox().GetWidth());
338-
}
339-
if (OverflowPropertyValue.HIDDEN.Equals(overflowY)) {
340-
clippedArea.SetY(occupiedArea.GetBBox().GetY()).SetHeight(occupiedArea.GetBBox().GetHeight());
341-
}
342-
drawContext.GetCanvas().Rectangle(clippedArea).Clip().NewPath();
343-
}
344329
canvas.AddXObject(xObject, matrix[0], matrix[1], matrix[2], matrix[3], (float)fixedXPosition + deltaX, (float
345330
)fixedYPosition);
346-
if (processOverflow) {
347-
drawContext.GetCanvas().RestoreState();
348-
}
349331
EndElementOpacityApplying(drawContext);
350332
if (true.Equals(GetPropertyAsBoolean(Property.FLUSH_ON_DRAW))) {
351333
xObject.Flush();

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
178178
// also not taking it into account (i.e. not setting it on child renderer) results in differences with html
179179
// when floating span is split on other line;
180180
// TODO may be process floating spans as inline blocks always?
181-
if (childPos > 0) {
181+
if (!wasXOverflowChanged && childPos > 0) {
182182
oldXOverflow = this.GetProperty<OverflowPropertyValue?>(Property.OVERFLOW_X);
183183
wasXOverflowChanged = true;
184184
SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.FIT);
@@ -274,7 +274,7 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
274274
SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.FIT);
275275
}
276276
if (childResult == null) {
277-
if (childPos > 0) {
277+
if (!wasXOverflowChanged && childPos > 0) {
278278
oldXOverflow = this.GetProperty<OverflowPropertyValue?>(Property.OVERFLOW_X);
279279
wasXOverflowChanged = true;
280280
SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.FIT);
@@ -397,8 +397,14 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
397397
split[0].childRenderers = new List<IRenderer>(childRenderers.SubList(0, childPos));
398398
bool wordWasSplitAndItWillFitOntoNextLine = false;
399399
if (childResult is TextLayoutResult && ((TextLayoutResult)childResult).IsWordHasBeenSplit()) {
400+
if (wasXOverflowChanged) {
401+
SetProperty(Property.OVERFLOW_X, oldXOverflow);
402+
}
400403
LayoutResult newLayoutResult = childRenderer.Layout(new LayoutContext(new LayoutArea(layoutContext.GetArea
401404
().GetPageNumber(), layoutBox, wasParentsHeightClipped)));
405+
if (wasXOverflowChanged) {
406+
SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.FIT);
407+
}
402408
if (newLayoutResult is TextLayoutResult && !((TextLayoutResult)newLayoutResult).IsWordHasBeenSplit()) {
403409
wordWasSplitAndItWillFitOntoNextLine = true;
404410
}

0 commit comments

Comments
 (0)