Skip to content

Commit 8f20ec7

Browse files
committed
Generalize logic of bidi reordering for non-text elements. Minor refactoring
DEVSIX-1053
1 parent d8ad537 commit 8f20ec7

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

layout/src/main/java/com/itextpdf/layout/renderer/AbstractRenderer.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,20 @@ This file is part of the iText (R) project.
4949
import com.itextpdf.kernel.geom.AffineTransform;
5050
import com.itextpdf.kernel.geom.Point;
5151
import com.itextpdf.kernel.geom.Rectangle;
52-
import com.itextpdf.kernel.pdf.*;
52+
import com.itextpdf.kernel.pdf.PdfArray;
53+
import com.itextpdf.kernel.pdf.PdfDictionary;
54+
import com.itextpdf.kernel.pdf.PdfDocument;
55+
import com.itextpdf.kernel.pdf.PdfName;
56+
import com.itextpdf.kernel.pdf.PdfNumber;
57+
import com.itextpdf.kernel.pdf.PdfObject;
58+
import com.itextpdf.kernel.pdf.PdfPage;
5359
import com.itextpdf.kernel.pdf.action.PdfAction;
5460
import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
5561
import com.itextpdf.kernel.pdf.canvas.CanvasArtifact;
5662
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
5763
import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
5864
import com.itextpdf.kernel.pdf.tagging.PdfStructElem;
5965
import com.itextpdf.kernel.pdf.tagutils.IAccessibleElement;
60-
import com.itextpdf.layout.Document;
6166
import com.itextpdf.kernel.pdf.tagutils.TagTreePointer;
6267
import com.itextpdf.layout.IPropertyContainer;
6368
import com.itextpdf.layout.border.Border;
@@ -69,12 +74,22 @@ This file is part of the iText (R) project.
6974
import com.itextpdf.layout.layout.LayoutPosition;
7075
import com.itextpdf.layout.minmaxwidth.MinMaxWidth;
7176
import com.itextpdf.layout.minmaxwidth.MinMaxWidthUtils;
72-
import com.itextpdf.layout.property.*;
77+
import com.itextpdf.layout.property.Background;
78+
import com.itextpdf.layout.property.BackgroundImage;
79+
import com.itextpdf.layout.property.HorizontalAlignment;
80+
import com.itextpdf.layout.property.Property;
81+
import com.itextpdf.layout.property.TransparentColor;
82+
import com.itextpdf.layout.property.UnitValue;
7383
import org.slf4j.Logger;
7484
import org.slf4j.LoggerFactory;
7585

7686
import java.text.MessageFormat;
77-
import java.util.*;
87+
import java.util.ArrayList;
88+
import java.util.Arrays;
89+
import java.util.Collections;
90+
import java.util.HashMap;
91+
import java.util.List;
92+
import java.util.Map;
7893

7994
/**
8095
* Defines the most common properties and behavior that are shared by most
@@ -742,9 +757,6 @@ protected Float getFirstYLineRecursively() {
742757
}
743758

744759
protected Float getLastYLineRecursively() {
745-
if (childRenderers.size() == 0) {
746-
return null;
747-
}
748760
for (int i = childRenderers.size() - 1; i >= 0; i--) {
749761
IRenderer child = childRenderers.get(i);
750762
if (child instanceof AbstractRenderer) {

layout/src/main/java/com/itextpdf/layout/renderer/LineRenderer.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ This file is part of the iText (R) project.
5757
import com.itextpdf.layout.layout.MinMaxWidthLayoutResult;
5858
import com.itextpdf.layout.layout.TextLayoutResult;
5959
import com.itextpdf.layout.minmaxwidth.MinMaxWidth;
60+
import com.itextpdf.layout.minmaxwidth.MinMaxWidthUtils;
6061
import com.itextpdf.layout.property.BaseDirection;
6162
import com.itextpdf.layout.property.FloatPropertyValue;
6263
import com.itextpdf.layout.property.Leading;
@@ -259,20 +260,18 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
259260
boolean isInlineBlockChild = isInlineBlockChild(childRenderer);
260261
if (!childWidthWasReplaced) {
261262
if (isInlineBlockChild && childRenderer instanceof AbstractRenderer) {
262-
childBlockMinMaxWidth = ((AbstractRenderer)childRenderer).getMinMaxWidth(layoutContext.getArea().getBBox().getWidth());
263+
childBlockMinMaxWidth = ((AbstractRenderer)childRenderer).getMinMaxWidth(MinMaxWidthUtils.getMax());
263264
float childMaxWidth = childBlockMinMaxWidth.getMaxWidth() + MIN_MAX_WIDTH_CORRECTION_EPS;
264265
// Decrease the calculated width by margins, paddings and borders so that even for 100% width the content definitely fits
265266
// TODO DEVSIX-1174 fix depending on box-sizing
266-
if (childBlockMinMaxWidth != null) {
267-
if (childMaxWidth > bbox.getWidth() && bbox.getWidth() != layoutContext.getArea().getBBox().getWidth()) {
268-
childResult = new LineLayoutResult(LayoutResult.NOTHING, null, null, childRenderer, childRenderer);
269-
} else {
270-
if (bbox.getWidth() == layoutContext.getArea().getBBox().getWidth() && childBlockMinMaxWidth.getMinWidth() > layoutContext.getArea().getBBox().getWidth()) {
271-
LoggerFactory.getLogger(LineRenderer.class).warn(LogMessageConstant.INLINE_BLOCK_ELEMENT_WILL_BE_CLIPPED);
272-
childRenderer.setProperty(Property.FORCED_PLACEMENT, true);
273-
}
274-
bbox.setWidth(childMaxWidth);
267+
if (childMaxWidth > bbox.getWidth() && bbox.getWidth() != layoutContext.getArea().getBBox().getWidth()) {
268+
childResult = new LineLayoutResult(LayoutResult.NOTHING, null, null, childRenderer, childRenderer);
269+
} else {
270+
if (bbox.getWidth() == layoutContext.getArea().getBBox().getWidth() && childBlockMinMaxWidth.getMinWidth() > layoutContext.getArea().getBBox().getWidth()) {
271+
LoggerFactory.getLogger(LineRenderer.class).warn(LogMessageConstant.INLINE_BLOCK_ELEMENT_WILL_BE_CLIPPED);
272+
childRenderer.setProperty(Property.FORCED_PLACEMENT, true);
275273
}
274+
bbox.setWidth(Math.min(childMaxWidth, layoutContext.getArea().getBBox().getWidth()));
276275
}
277276
}
278277
}
@@ -477,7 +476,7 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
477476

478477
// We shouldn't forget about images, float, inline-blocks that has to be inserted somewhere.
479478
// TODO determine correct place to insert this content. Probably consider inline floats separately.
480-
Map<TextRenderer, IRenderer> insertAfter = new HashMap<>();
479+
Map<TextRenderer, List<IRenderer>> insertAfter = new HashMap<>();
481480
List<IRenderer> starterNonTextRenderers = new ArrayList<>();
482481
TextRenderer lastTextRenderer = null;
483482

@@ -496,7 +495,10 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
496495
}
497496
lastTextRenderer = (TextRenderer) child;
498497
} else if (lastTextRenderer != null) {
499-
insertAfter.put(lastTextRenderer, child);
498+
if (!insertAfter.containsKey(lastTextRenderer)) {
499+
insertAfter.put(lastTextRenderer, new ArrayList<IRenderer>());
500+
}
501+
insertAfter.get(lastTextRenderer).add(child);
500502
} else {
501503
starterNonTextRenderers.add(child);
502504
}
@@ -527,7 +529,7 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
527529

528530
// Insert non-text renderers
529531
if (insertAfter.containsKey((TextRenderer)renderer)) {
530-
children.add(insertAfter.get((TextRenderer)renderer));
532+
children.addAll(insertAfter.get((TextRenderer)renderer));
531533
insertAfter.remove((TextRenderer)renderer);
532534
}
533535

0 commit comments

Comments
 (0)