Skip to content

Commit cee74eb

Browse files
Introduce generic approach for y-line extraction control; make some of the util methods public in AbstractRenderer
DEVSIX-984
1 parent 7bf0bd1 commit cee74eb

File tree

6 files changed

+89
-56
lines changed

6 files changed

+89
-56
lines changed

forms/src/test/java/com/itextpdf/forms/FormFieldFlatteningTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ This file is part of the iText (R) project.
4343
package com.itextpdf.forms;
4444

4545
import com.itextpdf.forms.fields.PdfFormField;
46+
import com.itextpdf.kernel.geom.Rectangle;
47+
import com.itextpdf.kernel.pdf.PdfArray;
4648
import com.itextpdf.kernel.pdf.PdfDocument;
4749
import com.itextpdf.kernel.pdf.PdfReader;
50+
import com.itextpdf.kernel.pdf.PdfString;
4851
import com.itextpdf.kernel.pdf.PdfWriter;
4952
import com.itextpdf.kernel.utils.CompareTool;
5053
import com.itextpdf.test.ExtendedITextTest;
@@ -55,6 +58,7 @@ This file is part of the iText (R) project.
5558
import org.junit.experimental.categories.Category;
5659

5760
import java.io.IOException;
61+
import java.util.Arrays;
5862

5963
@Category(IntegrationTest.class)
6064
public class FormFieldFlatteningTest extends ExtendedITextTest {
@@ -82,6 +86,25 @@ public void formFlatteningTest01() throws IOException, InterruptedException {
8286
Assert.assertNull(new CompareTool().compareByContent(filename, sourceFolder + "cmp_formFlatteningTest01.pdf", destinationFolder, "diff_"));
8387
}
8488

89+
@Test
90+
public void formFlatteningChoiceFieldTest01() throws IOException, InterruptedException {
91+
String srcFilename = sourceFolder + "formFlatteningSourceChoiceField.pdf";
92+
String filename = destinationFolder + "formFlatteningChoiceFieldTest01.pdf";
93+
94+
PdfDocument doc = new PdfDocument(new PdfReader(srcFilename), new PdfWriter(filename));
95+
96+
PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true);
97+
form.flattenFields();
98+
99+
doc.close();
100+
101+
CompareTool compareTool = new CompareTool();
102+
String errorMessage = compareTool.compareByContent(filename, sourceFolder + "cmp_formFlatteningChoiceFieldTest01.pdf", destinationFolder, "diff_");
103+
if (errorMessage != null) {
104+
Assert.fail(errorMessage);
105+
}
106+
}
107+
85108
@Test
86109
public void formFlatteningTest_DefaultAppearanceGeneration_Rot0() throws IOException, InterruptedException {
87110
String srcFilePattern = "FormFlatteningDefaultAppearance_0_";

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

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,47 @@ public Rectangle getInnerAreaBBox() {
10331033
return rect;
10341034
}
10351035

1036+
/**
1037+
* Applies margins of the renderer on the given rectangle
1038+
*
1039+
* @param rect a rectangle margins will be applied on.
1040+
* @param reverse indicates whether margins will be applied
1041+
* inside (in case of false) or outside (in case of true) the rectangle.
1042+
* @return a {@link Rectangle border box} of the renderer
1043+
* @see #getMargins
1044+
*/
1045+
public Rectangle applyMargins(Rectangle rect, boolean reverse) {
1046+
return this.applyMargins(rect, getMargins(), reverse);
1047+
}
1048+
1049+
/**
1050+
* Applies the border box of the renderer on the given rectangle
1051+
* If the border of a certain side is null, the side will remain as it was.
1052+
*
1053+
* @param rect a rectangle the border box will be applied on.
1054+
* @param reverse indicates whether the border box will be applied
1055+
* inside (in case of false) or outside (in case of false) the rectangle.
1056+
* @return a {@link Rectangle border box} of the renderer
1057+
* @see #getBorders
1058+
*/
1059+
public Rectangle applyBorderBox(Rectangle rect, boolean reverse) {
1060+
Border[] borders = getBorders();
1061+
return applyBorderBox(rect, borders, reverse);
1062+
}
1063+
1064+
/**
1065+
* Applies paddings of the renderer on the given rectangle
1066+
*
1067+
* @param rect a rectangle paddings will be applied on.
1068+
* @param reverse indicates whether paddings will be applied
1069+
* inside (in case of false) or outside (in case of false) the rectangle.
1070+
* @return a {@link Rectangle border box} of the renderer
1071+
* @see #getPaddings
1072+
*/
1073+
public Rectangle applyPaddings(Rectangle rect, boolean reverse) {
1074+
return applyPaddings(rect, getPaddings(), reverse);
1075+
}
1076+
10361077
public boolean isFirstOnRootArea() {
10371078
return isFirstOnRootArea(false);
10381079
}
@@ -1043,24 +1084,24 @@ protected void applyDestinationsAndAnnotation(DrawContext drawContext) {
10431084
applyLinkAnnotation(drawContext.getDocument());
10441085
}
10451086

1046-
static boolean isBorderBoxSizing(IRenderer renderer) {
1087+
protected static boolean isBorderBoxSizing(IRenderer renderer) {
10471088
BoxSizingPropertyValue boxSizing = renderer.<BoxSizingPropertyValue>getProperty(Property.BOX_SIZING);
10481089
return boxSizing != null && boxSizing.equals(BoxSizingPropertyValue.BORDER_BOX);
10491090
}
10501091

1051-
boolean isOverflowProperty(OverflowPropertyValue equalsTo, int overflowProperty) {
1092+
protected boolean isOverflowProperty(OverflowPropertyValue equalsTo, int overflowProperty) {
10521093
return isOverflowProperty(equalsTo, this.<OverflowPropertyValue>getProperty(overflowProperty));
10531094
}
10541095

1055-
static boolean isOverflowProperty(OverflowPropertyValue equalsTo, IRenderer renderer, int overflowProperty) {
1096+
protected static boolean isOverflowProperty(OverflowPropertyValue equalsTo, IRenderer renderer, int overflowProperty) {
10561097
return isOverflowProperty(equalsTo, renderer.<OverflowPropertyValue>getProperty(overflowProperty));
10571098
}
10581099

1059-
static boolean isOverflowProperty(OverflowPropertyValue equalsTo, OverflowPropertyValue rendererOverflowProperty) {
1100+
protected static boolean isOverflowProperty(OverflowPropertyValue equalsTo, OverflowPropertyValue rendererOverflowProperty) {
10601101
return equalsTo.equals(rendererOverflowProperty) || equalsTo.equals(OverflowPropertyValue.FIT) && rendererOverflowProperty == null;
10611102
}
10621103

1063-
static boolean isOverflowFit(OverflowPropertyValue rendererOverflowProperty) {
1104+
protected static boolean isOverflowFit(OverflowPropertyValue rendererOverflowProperty) {
10641105
return rendererOverflowProperty == null || OverflowPropertyValue.FIT.equals(rendererOverflowProperty);
10651106
}
10661107

@@ -1188,7 +1229,7 @@ protected Float retrieveMinWidth(float parentBoxWidth) {
11881229
*
11891230
* @param updatedWidthValue element's new fixed content box width.
11901231
*/
1191-
void updateWidth(UnitValue updatedWidthValue) {
1232+
protected void updateWidth(UnitValue updatedWidthValue) {
11921233
if (updatedWidthValue.isPointValue() && isBorderBoxSizing(this)) {
11931234
updatedWidthValue.setValue(updatedWidthValue.getValue() + calculatePaddingBorderWidth(this));
11941235
}
@@ -1287,7 +1328,7 @@ private float[] calculateRadii(BorderRadius[] radii, Rectangle area, boolean hor
12871328
*
12881329
* @param updatedHeight element's new fixed content box height, shall be not null.
12891330
*/
1290-
void updateHeight(UnitValue updatedHeight) {
1331+
protected void updateHeight(UnitValue updatedHeight) {
12911332
if (isBorderBoxSizing(this) && updatedHeight.isPointValue()) {
12921333
updatedHeight.setValue(updatedHeight.getValue() + calculatePaddingBorderHeight(this));
12931334

@@ -1343,8 +1384,7 @@ protected Float retrieveMaxHeight() {
13431384
*
13441385
* @param updatedMaxHeight element's new content box max-height, shall be not null.
13451386
*/
1346-
1347-
void updateMaxHeight(UnitValue updatedMaxHeight) {
1387+
protected void updateMaxHeight(UnitValue updatedMaxHeight) {
13481388
if (isBorderBoxSizing(this) && updatedMaxHeight.isPointValue()) {
13491389
updatedMaxHeight.setValue(updatedMaxHeight.getValue() + calculatePaddingBorderHeight(this));
13501390

@@ -1393,7 +1433,7 @@ protected Float retrieveMinHeight() {
13931433
*
13941434
* @param updatedMinHeight element's new content box min-height, shall be not null.
13951435
*/
1396-
void updateMinHeight(UnitValue updatedMinHeight) {
1436+
protected void updateMinHeight(UnitValue updatedMinHeight) {
13971437
if (isBorderBoxSizing(this) && updatedMinHeight.isPointValue()) {
13981438
updatedMinHeight.setValue(updatedMinHeight.getValue() + calculatePaddingBorderHeight(this));
13991439
}
@@ -1448,9 +1488,7 @@ protected Float getFirstYLineRecursively() {
14481488
}
14491489

14501490
protected Float getLastYLineRecursively() {
1451-
if (isOverflowProperty(OverflowPropertyValue.HIDDEN, Property.OVERFLOW_X)
1452-
|| isOverflowProperty(OverflowPropertyValue.HIDDEN, Property.OVERFLOW_Y)) {
1453-
// TODO may be this logic should also be based on BlockFormattingContextUtil?
1491+
if (!allowLastYLineRecursiveExtraction()) {
14541492
return null;
14551493
}
14561494
for (int i = childRenderers.size() - 1; i >= 0; i--) {
@@ -1465,17 +1503,9 @@ protected Float getLastYLineRecursively() {
14651503
return null;
14661504
}
14671505

1468-
/**
1469-
* Applies margins of the renderer on the given rectangle
1470-
*
1471-
* @param rect a rectangle margins will be applied on.
1472-
* @param reverse indicates whether margins will be applied
1473-
* inside (in case of false) or outside (in case of true) the rectangle.
1474-
* @return a {@link Rectangle border box} of the renderer
1475-
* @see #getMargins
1476-
*/
1477-
protected Rectangle applyMargins(Rectangle rect, boolean reverse) {
1478-
return this.applyMargins(rect, getMargins(), reverse);
1506+
protected boolean allowLastYLineRecursiveExtraction() {
1507+
return !isOverflowProperty(OverflowPropertyValue.HIDDEN, Property.OVERFLOW_X)
1508+
&& !isOverflowProperty(OverflowPropertyValue.HIDDEN, Property.OVERFLOW_Y);
14791509
}
14801510

14811511
/**
@@ -1525,19 +1555,6 @@ protected UnitValue[] getPaddings() {
15251555
return getPaddings(this);
15261556
}
15271557

1528-
/**
1529-
* Applies paddings of the renderer on the given rectangle
1530-
*
1531-
* @param rect a rectangle paddings will be applied on.
1532-
* @param reverse indicates whether paddings will be applied
1533-
* inside (in case of false) or outside (in case of false) the rectangle.
1534-
* @return a {@link Rectangle border box} of the renderer
1535-
* @see #getPaddings
1536-
*/
1537-
protected Rectangle applyPaddings(Rectangle rect, boolean reverse) {
1538-
return applyPaddings(rect, getPaddings(), reverse);
1539-
}
1540-
15411558
/**
15421559
* Applies given paddings on the given rectangle
15431560
*
@@ -1567,21 +1584,6 @@ protected Rectangle applyPaddings(Rectangle rect, UnitValue[] paddings, boolean
15671584
return rect.applyMargins(paddings[0].getValue(), paddings[1].getValue(), paddings[2].getValue(), paddings[3].getValue(), reverse);
15681585
}
15691586

1570-
/**
1571-
* Applies the border box of the renderer on the given rectangle
1572-
* If the border of a certain side is null, the side will remain as it was.
1573-
*
1574-
* @param rect a rectangle the border box will be applied on.
1575-
* @param reverse indicates whether the border box will be applied
1576-
* inside (in case of false) or outside (in case of false) the rectangle.
1577-
* @return a {@link Rectangle border box} of the renderer
1578-
* @see #getBorders
1579-
*/
1580-
protected Rectangle applyBorderBox(Rectangle rect, boolean reverse) {
1581-
Border[] borders = getBorders();
1582-
return applyBorderBox(rect, borders, reverse);
1583-
}
1584-
15851587
/**
15861588
* Applies the given border box (borders) on the given rectangle
15871589
*

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,7 @@ protected Float getFirstYLineRecursively() {
564564

565565
@Override
566566
protected Float getLastYLineRecursively() {
567-
if (isOverflowProperty(OverflowPropertyValue.HIDDEN, Property.OVERFLOW_X)
568-
|| isOverflowProperty(OverflowPropertyValue.HIDDEN, Property.OVERFLOW_Y)) {
569-
// TODO may be this logic should also be based on BlockFormattingContextUtil?
567+
if (!allowLastYLineRecursiveExtraction()) {
570568
return null;
571569
}
572570
if (lines == null || lines.size() == 0) {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ protected Rectangle applyPaddings(Rectangle rect, UnitValue[] paddings, boolean
161161
}
162162

163163
@Override
164-
protected Rectangle applyPaddings(Rectangle rect, boolean reverse) {
164+
public Rectangle applyPaddings(Rectangle rect, boolean reverse) {
165165
if (bordersHandler instanceof SeparatedTableBorders) {
166166
super.applyPaddings(rect, reverse);
167167
} else {
@@ -1313,11 +1313,21 @@ protected MinMaxWidth getMinMaxWidth() {
13131313
return new MinMaxWidth(minWidth, maxColTotalWidth, additionalWidth);
13141314
}
13151315

1316+
/**
1317+
* @deprecated Will be removed in next major release (iText 7.2).
1318+
* The aim of this method overriding here is achieved by overriding {@link #allowLastYLineRecursiveExtraction} method.
1319+
*/
13161320
@Override
1321+
@Deprecated
13171322
protected Float getLastYLineRecursively() {
13181323
return null;
13191324
}
13201325

1326+
@Override
1327+
protected boolean allowLastYLineRecursiveExtraction() {
1328+
return false;
1329+
}
1330+
13211331
private void initializeTableLayoutBorders() {
13221332
boolean isSeparated = BorderCollapsePropertyValue.SEPARATE.equals(this.<BorderCollapsePropertyValue>getProperty(Property.BORDER_COLLAPSE));
13231333
bordersHandler = isSeparated

0 commit comments

Comments
 (0)