Skip to content

Commit 2f0f17c

Browse files
author
Samuel Huylebroeck
committed
Support relative height declarations
Add support for relative height declarations Add tests for relative height declarations Fix tests that used relative height declarations Update WidthHeightApplierUtil to take relative table-height into account DEVSIX-1389
1 parent 18d7613 commit 2f0f17c

25 files changed

+789
-102
lines changed

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/form/renderer/AbstractFormFieldRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ private LayoutResult layout(LayoutContext layoutContext, boolean minMaxWidth) {
205205
float parentWidth = layoutContext.getArea().getBBox().getWidth();
206206
float parentHeight = layoutContext.getArea().getBBox().getHeight();
207207

208-
Float maxHeight = this.<Float>getProperty(Property.MAX_HEIGHT);
209-
Float height = this.<Float>getProperty(Property.HEIGHT);
208+
UnitValue maxHeight = this.<UnitValue>getProperty(Property.MAX_HEIGHT);
209+
UnitValue height = this.<UnitValue>getProperty(Property.HEIGHT);
210210
boolean restoreMaxHeight = hasOwnProperty(Property.MAX_HEIGHT);
211211
boolean restoreHeight = hasOwnProperty(Property.HEIGHT);
212212
setProperty(Property.MAX_HEIGHT, null);

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/form/renderer/AbstractOneLineTextFieldRenderer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ void cropContentLines(List<LineRenderer> lines, Rectangle bBox) {
100100
* Updates the paragraph height.
101101
*/
102102
private void updateParagraphHeight() {
103-
overrideHeightProperties();
104103
Float height = retrieveHeight();
105104
Float minHeight = retrieveMinHeight();
106105
Float maxHeight = retrieveMaxHeight();

src/main/java/com/itextpdf/html2pdf/css/apply/util/WidthHeightApplierUtil.java

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.html2pdf.css.apply.util;
4444

45-
import com.itextpdf.html2pdf.LogMessageConstant;
4645
import com.itextpdf.html2pdf.attach.ProcessorContext;
4746
import com.itextpdf.html2pdf.css.CssConstants;
4847
import com.itextpdf.html2pdf.css.util.CssUtils;
@@ -62,7 +61,9 @@ This file is part of the iText (R) project.
6261
*/
6362
public final class WidthHeightApplierUtil {
6463

65-
/** The logger. */
64+
/**
65+
* The logger.
66+
*/
6667
private static final Logger logger = LoggerFactory.getLogger(WidthHeightApplierUtil.class);
6768

6869
/**
@@ -75,8 +76,8 @@ private WidthHeightApplierUtil() {
7576
* Applies a width or a height to an element.
7677
*
7778
* @param cssProps the CSS properties
78-
* @param context the processor context
79-
* @param element the element
79+
* @param context the processor context
80+
* @param element the element
8081
*/
8182
public static void applyWidthHeight(Map<String, String> cssProps, ProcessorContext context, IPropertyContainer element) {
8283
float em = CssUtils.parseAbsoluteLength(cssProps.get(CssConstants.FONT_SIZE));
@@ -109,53 +110,44 @@ public static void applyWidthHeight(Map<String, String> cssProps, ProcessorConte
109110
if (!CssConstants.AUTO.equals(heightVal)) {
110111
height = CssUtils.parseLengthValueToPt(heightVal, em, rem);
111112
if (height != null) {
112-
if (height.isPointValue()) {
113-
// For tables, height does not have any effect. The height value will be used when
114-
// calculating effective min height value below
115-
if (!applyToTable && !applyToCell) {
116-
element.setProperty(Property.HEIGHT, height.getValue());
117-
}
118-
} else {
119-
logger.error(LogMessageConstant.HEIGHT_VALUE_IN_PERCENT_NOT_SUPPORTED);
113+
// For tables, height does not have any effect. The height value will be used when
114+
// calculating effective min height value below
115+
if (!applyToTable && !applyToCell) {
116+
element.setProperty(Property.HEIGHT, height);
120117
}
121118
}
122119
}
123120
}
124121

125122
String maxHeightVal = cssProps.get(CssConstants.MAX_HEIGHT);
126123
float maxHeightToApply = 0;
124+
UnitValue maxHeight = new UnitValue(UnitValue.POINT, 0);
127125
if (maxHeightVal != null) {
128-
UnitValue maxHeight = CssUtils.parseLengthValueToPt(maxHeightVal, em, rem);
126+
maxHeight = CssUtils.parseLengthValueToPt(maxHeightVal, em, rem);
129127
if (maxHeight != null) {
130-
if (maxHeight.isPointValue()) {
131-
// For tables and cells, max height does not have any effect. See also comments below when MIN_HEIGHT is applied.
132-
if (!applyToTable && !applyToCell) {
133-
maxHeightToApply = maxHeight.getValue();
134-
}
135-
} else {
136-
logger.error(LogMessageConstant.HEIGHT_VALUE_IN_PERCENT_NOT_SUPPORTED);
128+
// For tables and cells, max height does not have any effect. See also comments below when MIN_HEIGHT is applied.
129+
if (!applyToTable && !applyToCell) {
130+
maxHeightToApply = maxHeight.getValue();
137131
}
138132
}
139133
}
140134
if (maxHeightToApply > 0) {
141-
element.setProperty(Property.MAX_HEIGHT, maxHeightToApply);
135+
element.setProperty(Property.MAX_HEIGHT, maxHeight);
142136
}
143137

144138
String minHeightVal = cssProps.get(CssConstants.MIN_HEIGHT);
145139
float minHeightToApply = 0;
140+
UnitValue minHeight = new UnitValue(UnitValue.POINT, 0);
146141
if (minHeightVal != null) {
147-
UnitValue minHeight = CssUtils.parseLengthValueToPt(minHeightVal, em, rem);
142+
minHeight = CssUtils.parseLengthValueToPt(minHeightVal, em, rem);
148143
if (minHeight != null) {
149-
if (minHeight.isPointValue()) {
150-
// For cells, min height does not have any effect. See also comments below when MIN_HEIGHT is applied.
151-
if (!applyToCell) {
152-
minHeightToApply = minHeight.getValue();
153-
}
154-
} else {
155-
logger.error(LogMessageConstant.HEIGHT_VALUE_IN_PERCENT_NOT_SUPPORTED);
144+
// For cells, min height does not have any effect. See also comments below when MIN_HEIGHT is applied.
145+
if (!applyToCell) {
146+
minHeightToApply = minHeight.getValue();
156147
}
157148
}
158149
}
150+
159151
// About tables:
160152
// The height of a table is given by the 'height' property for the 'table' or 'inline-table' element.
161153
// A value of 'auto' means that the height is the sum of the row heights plus any cell spacing or borders.
@@ -165,11 +157,15 @@ public static void applyWidthHeight(Map<String, String> cssProps, ProcessorConte
165157
// The height of a 'table-row' element's box is the maximum of the row's computed 'height', the computed 'height' of each cell in the row,
166158
// and the minimum height (MIN) required by the cells. MIN depends on cell box heights and cell box alignment.
167159
// In CSS 2.1, the height of a cell box is the minimum height required by the content.
168-
if ((applyToTable || applyToCell) && height != null && height.isPointValue() && height.getValue() > minHeightToApply) {
160+
if ((applyToTable || applyToCell) && height != null && height.getValue() > minHeightToApply) {
169161
minHeightToApply = height.getValue();
170-
}
171-
if (minHeightToApply > 0) {
172-
element.setProperty(Property.MIN_HEIGHT, minHeightToApply);
162+
if (minHeightToApply > 0) {
163+
element.setProperty(Property.MIN_HEIGHT, height);
164+
}
165+
} else {
166+
if (minHeightToApply > 0) {
167+
element.setProperty(Property.MIN_HEIGHT, minHeight);
168+
}
173169
}
174170

175171
if (CssConstants.BORDER_BOX.equals(cssProps.get(CssConstants.BOX_SIZING))) {

src/test/java/com/itextpdf/html2pdf/css/HeightTest.java

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,96 @@ public void heightTest07() throws IOException, InterruptedException {
131131
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, diffPrefix));
132132
}
133133

134+
@Test
135+
public void heightTest08() throws IOException, InterruptedException {
136+
String testName = "heightTest08";
137+
String diffPrefix = "diff08_";
138+
139+
HtmlConverter.convertToPdf(new File(sourceFolder + testName + ".html"), new File(destinationFolder + testName + ".pdf"));
140+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, diffPrefix));
141+
}
142+
143+
@Test
144+
public void heightTest09() throws IOException, InterruptedException {
145+
String testName = "heightTest09";
146+
String diffPrefix = "diff09_";
147+
148+
HtmlConverter.convertToPdf(new File(sourceFolder + testName + ".html"), new File(destinationFolder + testName + ".pdf"));
149+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, diffPrefix));
150+
}
151+
152+
@Test
153+
public void heightTest10() throws IOException, InterruptedException {
154+
String testName = "heightTest10";
155+
String diffPrefix = "diff10_";
156+
157+
HtmlConverter.convertToPdf(new File(sourceFolder + testName + ".html"), new File(destinationFolder + testName + ".pdf"));
158+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, diffPrefix));
159+
}
160+
161+
@Test
162+
public void heightTest11() throws IOException, InterruptedException {
163+
String testName = "heightTest11";
164+
String diffPrefix = "diff11_";
165+
166+
HtmlConverter.convertToPdf(new File(sourceFolder + testName + ".html"), new File(destinationFolder + testName + ".pdf"));
167+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, diffPrefix));
168+
}
169+
170+
@Test
171+
public void heightTest12() throws IOException, InterruptedException {
172+
String testName = "heightTest12";
173+
String diffPrefix = "diff12_";
174+
175+
HtmlConverter.convertToPdf(new File(sourceFolder + testName + ".html"), new File(destinationFolder + testName + ".pdf"));
176+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, diffPrefix));
177+
}
178+
179+
@Test
180+
public void heightTest13() throws IOException, InterruptedException{
181+
String testName = "heightTest13";
182+
String diffPrefix = "diff13_";
183+
184+
HtmlConverter.convertToPdf(new File(sourceFolder + testName + ".html"), new File(destinationFolder + testName + ".pdf"));
185+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, diffPrefix));
186+
}
187+
188+
@Test
189+
public void heightTest14() throws IOException, InterruptedException{
190+
String testName = "heightTest14";
191+
String diffPrefix = "diff14_";
192+
193+
HtmlConverter.convertToPdf(new File(sourceFolder + testName + ".html"), new File(destinationFolder + testName + ".pdf"));
194+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, diffPrefix));
195+
}
196+
197+
@Test
198+
public void heightTest15() throws IOException, InterruptedException{
199+
String testName = "heightTest15";
200+
String diffPrefix = "diff15_";
201+
202+
HtmlConverter.convertToPdf(new File(sourceFolder + testName + ".html"), new File(destinationFolder + testName + ".pdf"));
203+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, diffPrefix));
204+
}
205+
206+
@Test
207+
public void heightTest16() throws IOException, InterruptedException{
208+
String testName = "heightTest16";
209+
String diffPrefix = "diff16_";
210+
211+
HtmlConverter.convertToPdf(new File(sourceFolder + testName + ".html"), new File(destinationFolder + testName + ".pdf"));
212+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, diffPrefix));
213+
}
214+
215+
@Test
216+
public void heightTest17() throws IOException, InterruptedException{
217+
String testName = "heightTest17";
218+
String diffPrefix = "diff17_";
219+
220+
HtmlConverter.convertToPdf(new File(sourceFolder + testName + ".html"), new File(destinationFolder + testName + ".pdf"));
221+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, diffPrefix));
222+
}
223+
134224
@Test
135225
public void heightWithCollapsingMarginsTest01() throws IOException, InterruptedException {
136226
String testName = "heightWithCollapsingMarginsTest01";
@@ -150,7 +240,6 @@ public void heightWithCollapsingMarginsTest03() throws IOException, InterruptedE
150240
}
151241

152242
@Test
153-
// TODO DEVSIX-1047
154243
public void heightWithCollapsingMarginsTest04() throws IOException, InterruptedException {
155244
String testName = "heightWithCollapsingMarginsTest04";
156245
String diffPrefix = "diffMargins04_";
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)