Skip to content

Commit 822ff32

Browse files
author
dmitry.radchuk
committed
Fix review issues
DEVSIX-5359
1 parent 75514e5 commit 822ff32

File tree

4 files changed

+71
-39
lines changed

4 files changed

+71
-39
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ private void updateBidiLevels(int totalNumberOfTrimmedGlyphs, BaseDirection base
15131513
/**
15141514
* While resolving TextRenderer may split into several ones with different fonts.
15151515
*/
1516-
private void resolveChildrenFonts() {
1516+
private void resolveChildrenFonts() {
15171517
final List<IRenderer> newChildRenderers = new ArrayList<>(getChildRenderers().size());
15181518
boolean updateChildRenderers = false;
15191519
for (final IRenderer child : getChildRenderers()) {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,15 @@ protected LayoutResult directLayout(LayoutContext layoutContext) {
238238
boolean isLastLineReLaidOut = false;
239239

240240
if (result.getStatus() == LayoutResult.NOTHING) {
241-
//Re-layout last line if it doesn't fit
241+
//relayouting the child for allowing the vertical overflow in order to take into account the negative
242+
// leading adjustment in case of a clipped-height context
242243
if (layoutContext.isClippedHeight()) {
243-
OverflowPropertyValue previousOverflowProperty = currentRenderer.<OverflowPropertyValue>getProperty(Property.OVERFLOW_Y);
244+
OverflowPropertyValue previousOverflowProperty = currentRenderer.<OverflowPropertyValue>getProperty(
245+
Property.OVERFLOW_Y);
244246
currentRenderer.setProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);
245247
lineLayoutContext.setClippedHeight(true);
246-
result = (LineLayoutResult) ((LineRenderer) currentRenderer.setParent(this)).layout(lineLayoutContext);
248+
result = (LineLayoutResult) ((LineRenderer) currentRenderer.setParent(this)).layout(
249+
lineLayoutContext);
247250
currentRenderer.setProperty(Property.OVERFLOW_Y, previousOverflowProperty);
248251
isLastLineReLaidOut = true;
249252
}

layout/src/test/java/com/itextpdf/layout/LeadingHeightTest.java

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.itextpdf.kernel.utils.CompareTool;
1010
import com.itextpdf.layout.borders.Border;
1111
import com.itextpdf.layout.element.Cell;
12+
import com.itextpdf.layout.element.Div;
1213
import com.itextpdf.layout.element.Paragraph;
1314
import com.itextpdf.layout.element.Table;
1415
import com.itextpdf.layout.element.Text;
@@ -35,39 +36,44 @@ public class LeadingHeightTest extends ExtendedITextTest {
3536
public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/layout/LeadingHeightTest/";
3637
public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/layout/LeadingHeightTest/";
3738

39+
private static final int HEIGHT_LESS_THAN_REQUIRED = -2;
40+
private static final int HEIGHT_IS_NOT_SET = -1;
41+
private static final int HEIGHT_EXACT_THAT_REQUIRED = 0;
42+
private static final int HEIGHT_MORE_THAN_REQUIRED = 100;
43+
3844
@BeforeClass
3945
public static void beforeClass() {
4046
createOrClearDestinationFolder(DESTINATION_FOLDER);
4147
}
4248

43-
@LogMessages(messages = {@LogMessage(messageTemplate = IoLogMessageConstant.CLIP_ELEMENT, count = 2)})
49+
@LogMessages(messages = {
50+
@LogMessage(messageTemplate = IoLogMessageConstant.CLIP_ELEMENT, count = 2)
51+
})
4452
@Test
4553
public void clippedHeightParagraphTest() throws IOException, InterruptedException {
4654
String outPdf = DESTINATION_FOLDER + "leadingTestHeight.pdf";
4755
String cmpPdf = SOURCE_FOLDER + "cmp_leadingTestHeight.pdf";
4856
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outPdf));
4957
Document doc = new Document(pdfDoc, new PageSize(700, 700));
50-
// This is how table looks like if no height property is set
51-
addTable(doc, 504, "RETIREMENT PLANNING: BECAUSE YOU CAN’T BE A FINANCIAL PLANNER FOREVER.", -1);
52-
// Here we set value from pre layout as height. We expect that this table shall be equal to the previous one
53-
addTable(doc, 360, "RETIREMENT PLANNING: BECAUSE YOU CAN’T BE A FINANCIAL PLANNER FOREVER.", 0);
54-
// Here we set 100 as height. We expect that this will be enough and all text will be placed
55-
addTable(doc, 216, "RETIREMENT PLANNING: BECAUSE YOU CAN’T BE A FINANCIAL PLANNER FOREVER.", 100);
56-
// Here we set 100 as height. We expect that this will be enough to place 3 lines
57-
addTable(doc, 216, "RETIREMENT PLANNING: BECAUSE ***SOME TEST TEXT IS PLACED*** YOU CAN’T BE A FINANCIAL PLANNER FOREVER.", 100);
58-
// Here we set value from pre layout minus 0.5f as height. We expect that this table shall not be equal to the previous one
59-
addTable(doc, 50, "RETIREMENT PLANNING: BECAUSE YOU CAN’T BE A FINANCIAL PLANNER FOREVER.", -2);
58+
addDescription(doc, 600, "This is how table looks like if no height property is set");
59+
addTable(doc, 504, "RETIREMENT PLANNING: BECAUSE YOU CAN’T BE A FINANCIAL PLANNER FOREVER.", HEIGHT_IS_NOT_SET);
60+
addDescription(doc, 456, "Here we set value from pre layout as height. We expect that this table shall be equal to the previous one");
61+
addTable(doc, 360, "RETIREMENT PLANNING: BECAUSE YOU CAN’T BE A FINANCIAL PLANNER FOREVER.", HEIGHT_EXACT_THAT_REQUIRED);
62+
addDescription(doc, 312, "Here we set 100 as height. We expect that this will be enough to place 3 lines");
63+
addTable(doc, 216, "RETIREMENT PLANNING: BECAUSE ***SOME TEST TEXT IS PLACED*** YOU CAN’T BE A FINANCIAL PLANNER FOREVER.", HEIGHT_MORE_THAN_REQUIRED);
64+
addDescription(doc, 146, "Here we set value from pre layout minus 0.5f as height. We expect that this table shall not be equal to the previous one");
65+
addTable(doc, 50, "RETIREMENT PLANNING: BECAUSE YOU CAN’T BE A FINANCIAL PLANNER FOREVER.", HEIGHT_LESS_THAN_REQUIRED);
6066
doc.close();
6167

6268
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, DESTINATION_FOLDER));
6369
}
6470

6571
@Test
66-
public void pageHeightParagraphTest() throws IOException, InterruptedException {
72+
public void paragraphTest() throws IOException, InterruptedException {
6773
String outPdf = DESTINATION_FOLDER + "pageHeightParagraphTest.pdf";
6874
String cmpPdf = SOURCE_FOLDER + "cmp_pageHeightParagraphTest.pdf";
6975
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outPdf));
70-
//176 = 104 + 36 + 36 (page margins)
76+
//Document height = 176 = 104 + 36 + 36, where 104 - is exact size of paragraph after layout and 34 + 34 - page margins
7177
Document doc = new Document(pdfDoc, new PageSize(700, 176));
7278
Paragraph ph = new Paragraph();
7379
Text txt = new Text("RETIREMENT PLANNING: BECAUSE YOU CAN’T BE A FINANCIAL PLANNER FOREVER.");
@@ -80,15 +86,17 @@ public void pageHeightParagraphTest() throws IOException, InterruptedException {
8086

8187
doc.add(ph);
8288
doc.close();
89+
//Partial text expected to be present in the document
90+
//There should be only "RETIREMENT PLANNING: BECAUSE YOU CAN’T BE A FINANCIAL"
8391
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, DESTINATION_FOLDER));
8492
}
8593

8694
@Test
87-
public void pageHeightParagraphWithWithWorkaroundTest() throws IOException, InterruptedException {
95+
public void pageHeightDivWithNestedParagraphTest() throws IOException, InterruptedException {
8896
String outPdf = DESTINATION_FOLDER + "pageHeightParagraphWorkAroundTest.pdf";
8997
String cmpPdf = SOURCE_FOLDER + "cmp_pageHeightParagraphWorkAroundTest.pdf";
9098
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outPdf));
91-
//176 = 104 + 36 + 36 (page margins)
99+
//Document height = 176 = 104 + 36 + 36, where 104 - is exact size of paragraph after layout and 34 + 34 - page margins
92100
Document doc = new Document(pdfDoc, new PageSize(700, 176));
93101
Paragraph ph = new Paragraph();
94102
Text txt = new Text("RETIREMENT PLANNING: BECAUSE YOU CAN’T BE A FINANCIAL PLANNER FOREVER.");
@@ -100,40 +108,27 @@ public void pageHeightParagraphWithWithWorkaroundTest() throws IOException, Inte
100108
ph.setWidth(585f);
101109

102110

103-
Paragraph ph2 = new Paragraph();
111+
Div ph2 = new Div();
104112
ph2.setHeight(104);
105113
ph2.setMargin(0);
106114
ph2.setPadding(0);
107115
ph2.add(ph);
108116
ph2.setProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);;
109117
doc.add(ph2);
110118
doc.close();
119+
//Full text expected to be present on the first page
111120
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, DESTINATION_FOLDER));
112121
}
113122

114-
public void addTable(Document doc, int y, String text, int heightParam)
123+
private void addTable(Document doc, int y, String text, int heightParam)
115124
{
116125
float width = 585f;
117-
float fontSize = 32f;
118126

119127
Table table = new Table(1);
120128
table.setWidth(width);
121129
table.setFixedLayout();
122130

123-
Paragraph ph = new Paragraph();
124-
Text txt = new Text(text);
125-
txt.setFontSize(fontSize);
126-
ph.add(txt);
127-
ph.setFixedLeading(fontSize);
128-
129-
Cell cell = new Cell();
130-
cell.setPaddingTop(0f);
131-
cell.setPaddingBottom(0f);
132-
cell.add(ph);
133-
cell.setBackgroundColor(ColorConstants.LIGHT_GRAY);
134-
cell.setBorder(null);
135-
136-
table.addCell(cell);
131+
Cell cell = addCell(table, text);
137132

138133
// find out how tall the cell is we just added
139134
LayoutResult result = table.createRendererSubTree()
@@ -148,16 +143,19 @@ public void addTable(Document doc, int y, String text, int heightParam)
148143
);
149144

150145
String heightStr = "Natural";
151-
if (heightParam == -2) {
146+
if (heightParam == HEIGHT_LESS_THAN_REQUIRED) {
152147
float rowHeight = result.getOccupiedArea().getBBox().getHeight();
153148
cell.setHeight(rowHeight - 0.5f);
154149
heightStr = "Calculated " + (rowHeight - 0.5f);
155150
}
156-
if (heightParam == 0)
151+
if (heightParam == HEIGHT_EXACT_THAT_REQUIRED)
157152
{
158153
float rowHeight = result.getOccupiedArea().getBBox().getHeight();
159-
cell.setHeight(rowHeight + 1f);
154+
cell.setHeight(rowHeight);
160155
heightStr = "Calculated " + rowHeight;
156+
if (heightStr.endsWith(".0")) {
157+
heightStr = heightStr.substring(0, heightStr.length() - 2);
158+
}
161159
}
162160
else if (heightParam > 0)
163161
{
@@ -168,7 +166,28 @@ else if (heightParam > 0)
168166
table.setFixedPosition((float) 36, (float) y, width);
169167

170168
doc.add(table);
169+
addCellFooter(doc, y, width, heightStr);
170+
}
171+
172+
private Cell addCell(Table table, String text) {
173+
Paragraph ph = new Paragraph();
174+
Text txt = new Text(text);
175+
txt.setFontSize(32f);
176+
ph.add(txt);
177+
ph.setFixedLeading(32f);
178+
179+
Cell cell = new Cell();
180+
cell.setPaddingTop(0f);
181+
cell.setPaddingBottom(0f);
182+
cell.add(ph);
183+
cell.setBackgroundColor(ColorConstants.LIGHT_GRAY);
184+
cell.setBorder(null);
171185

186+
table.addCell(cell);
187+
return cell;
188+
}
189+
190+
private void addCellFooter(Document doc, float y, float width, String heightStr) {
172191
Table t2 = new Table(1);
173192
t2.setWidth(width);
174193
t2.setFixedLayout();
@@ -181,4 +200,14 @@ else if (heightParam > 0)
181200
t2.setFixedPosition((float) 36, (float) y-18, width);
182201
doc.add(t2);
183202
}
203+
204+
private void addDescription(Document doc, float y, String text) {
205+
Paragraph ph = new Paragraph();
206+
Text txt = new Text(text);
207+
txt.setFontSize(12f);
208+
ph.add(txt);
209+
ph.setFontColor(ColorConstants.RED);
210+
ph.setFixedPosition(1, 40, y, 585f);
211+
doc.add(ph);
212+
}
184213
}

0 commit comments

Comments
 (0)