Skip to content

Commit 643ca6f

Browse files
author
glenn.volckaert
committed
Support vertical-align css property for inline-blocks in HTML inline-context
DEVSIX-6881
1 parent 817c70b commit 643ca6f

File tree

127 files changed

+1697
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+1697
-5
lines changed

src/main/java/com/itextpdf/html2pdf/css/CssConstants.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ public class CssConstants extends CommonCssConstants {
137137
/** The Constant CAPITALIZE. */
138138
public static final String CAPITALIZE = "capitalize";
139139

140+
/** The Constant CONTENTS. */
141+
public static final String CONTENTS = "contents";
142+
140143
/** The Constant COLLAPSE. */
141144
public static final String COLLAPSE = "collapse";
142145

@@ -155,12 +158,22 @@ public class CssConstants extends CommonCssConstants {
155158
/** The Constant FIRST_EXCEPT. */
156159
public static final String FIRST_EXCEPT = "first-except";
157160

161+
/** The Constant GRID. */
162+
public static final String GRID = "grid";
163+
158164
/** The Constant INLINE. */
159165
public static final String INLINE = "inline";
160166

161167
/** The Constant INLINE_BLOCK. */
162168
public static final String INLINE_BLOCK = "inline-block";
163169

170+
/** The Constant INLINE_FLEX. */
171+
public static final String INLINE_FLEX = "INLINE_FLEX";
172+
173+
174+
/** The Constant INLINE_GRID. */
175+
public static final String INLINE_GRID = "INLINE_GRID";
176+
164177
/** The Constant INLINE_TABLE. */
165178
public static final String INLINE_TABLE = "inline-table";
166179

@@ -215,6 +228,9 @@ public class CssConstants extends CommonCssConstants {
215228
/** The Constant RELATIVE. */
216229
public static final String RELATIVE = "relative";
217230

231+
/** The Constant RUN_IN. */
232+
public static final String RUN_IN = "run-in";
233+
218234
/** The Constant RTL. */
219235
public static final String RTL = "rtl";
220236

@@ -232,13 +248,31 @@ public class CssConstants extends CommonCssConstants {
232248

233249
/** The Constant TABLE. */
234250
public static final String TABLE = "table";
251+
252+
/** The Constant TABLE_CAPTION. */
253+
public static final String TABLE_CAPTION = "table-caption";
235254

236255
/** The Constant TABLE_CELL. */
237256
public static final String TABLE_CELL = "table-cell";
257+
258+
/** The Constant TABLE_COLUMN. */
259+
public static final String TABLE_COLUMN = "table-column";
260+
261+
/** The Constant TABLE_COLUMN_GROUP. */
262+
public static final String TABLE_COLUMN_GROUP = "table-column-group";
263+
264+
/** The Constant TABLE_FOOTER_GROUP. */
265+
public static final String TABLE_FOOTER_GROUP = "table-footer-group";
266+
267+
/** The Constant TABLE_HEADER_GROUP. */
268+
public static final String TABLE_HEADER_GROUP = "table-header-group";
238269

239270
/** The Constant TABLE_ROW. */
240271
public static final String TABLE_ROW = "table-row";
241272

273+
/** The Constant TABLE_ROW_GROUP. */
274+
public static final String TABLE_ROW_GROUP = "table-row-group";
275+
242276
/** The Constant TEXT_BOTTOM. */
243277
public static final String TEXT_BOTTOM = "text-bottom";
244278

src/main/java/com/itextpdf/html2pdf/css/apply/impl/BlockCssApplier.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ This file is part of the iText (R) project.
4444

4545
import com.itextpdf.html2pdf.attach.ITagWorker;
4646
import com.itextpdf.html2pdf.attach.ProcessorContext;
47+
import com.itextpdf.html2pdf.attach.impl.tags.ImgTagWorker;
48+
import com.itextpdf.html2pdf.attach.impl.tags.SpanTagWorker;
4749
import com.itextpdf.html2pdf.css.CssConstants;
4850
import com.itextpdf.html2pdf.css.apply.ICssApplier;
4951
import com.itextpdf.html2pdf.css.apply.util.BackgroundApplierUtil;
@@ -61,6 +63,7 @@ This file is part of the iText (R) project.
6163
import com.itextpdf.html2pdf.css.apply.util.PageBreakApplierUtil;
6264
import com.itextpdf.html2pdf.css.apply.util.PositionApplierUtil;
6365
import com.itextpdf.html2pdf.css.apply.util.TransformationApplierUtil;
66+
import com.itextpdf.html2pdf.css.apply.util.VerticalAlignmentApplierUtil;
6467
import com.itextpdf.html2pdf.css.apply.util.WidthHeightApplierUtil;
6568
import com.itextpdf.layout.IPropertyContainer;
6669
import com.itextpdf.styledxmlparser.node.IStylesContainer;
@@ -96,6 +99,7 @@ public void apply(ProcessorContext context, IStylesContainer stylesContainer, IT
9699
TransformationApplierUtil.applyTransformation(cssProps, context, container);
97100
OutlineApplierUtil.applyOutlines(cssProps, context, container);
98101
OrphansWidowsApplierUtil.applyOrphansAndWidows(cssProps, container);
102+
VerticalAlignmentApplierUtil.applyVerticalAlignmentForBlocks(cssProps, container, isInlineItem(tagWorker));
99103
if (isFlexItem(stylesContainer)) {
100104
FlexApplierUtil.applyFlexItemProperties(cssProps, context, container);
101105
} else {
@@ -107,6 +111,11 @@ public void apply(ProcessorContext context, IStylesContainer stylesContainer, IT
107111
}
108112
}
109113

114+
private static boolean isInlineItem(ITagWorker tagWorker) {
115+
return tagWorker instanceof SpanTagWorker ||
116+
tagWorker instanceof ImgTagWorker;
117+
}
118+
110119
private static boolean isFlexItem(IStylesContainer stylesContainer) {
111120
if (stylesContainer instanceof JsoupElementNode
112121
&& ((JsoupElementNode) stylesContainer).parentNode() instanceof JsoupElementNode) {

src/main/java/com/itextpdf/html2pdf/css/apply/impl/SpanTagCssApplier.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ public void apply(ProcessorContext context, IStylesContainer stylesContainer, IT
8686
applyChildElementStyles(child, cssStyles, context, stylesContainer);
8787
}
8888
}
89-
VerticalAlignmentApplierUtil.applyVerticalAlignmentForInlines(cssStyles, context, stylesContainer, spanTagWorker.getAllElements());
89+
90+
VerticalAlignmentApplierUtil.applyVerticalAlignmentForInlines(cssStyles, context, stylesContainer,
91+
spanTagWorker.getAllElements());
92+
9093
if (cssStyles.containsKey(CssConstants.OPACITY)) {
9194
for (IPropertyContainer elem : spanTagWorker.getAllElements()) {
9295
if (elem instanceof Text && !elem.hasProperty(Property.OPACITY)) {

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ This file is part of the iText (R) project.
4747
import com.itextpdf.layout.IPropertyContainer;
4848
import com.itextpdf.layout.element.IBlockElement;
4949
import com.itextpdf.layout.element.Text;
50+
import com.itextpdf.layout.properties.InlineVerticalAlignment;
51+
import com.itextpdf.layout.properties.InlineVerticalAlignmentType;
5052
import com.itextpdf.layout.properties.Property;
5153
import com.itextpdf.layout.properties.UnitValue;
5254
import com.itextpdf.layout.properties.VerticalAlignment;
53-
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
5455
import com.itextpdf.styledxmlparser.css.util.CssDimensionParsingUtils;
56+
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
5557
import com.itextpdf.styledxmlparser.node.IElementNode;
5658
import com.itextpdf.styledxmlparser.node.INode;
5759
import com.itextpdf.styledxmlparser.node.IStylesContainer;
@@ -98,6 +100,55 @@ public static void applyVerticalAlignmentForCells(Map<String, String> cssProps,
98100
}
99101
}
100102

103+
104+
/**
105+
* Apply vertical alignment to inline elements.
106+
*
107+
* @param cssProps the CSS properties
108+
* @param element the styles container
109+
* @param isInlineTag whether the origin is a tag that defaults to inline
110+
*/
111+
public static void applyVerticalAlignmentForBlocks(Map<String, String> cssProps, IPropertyContainer element,
112+
boolean isInlineTag ) {
113+
String display = cssProps.get(CssConstants.DISPLAY);
114+
if (isInlineTag || CssConstants.INLINE_BLOCK.equals(display)) {
115+
String vAlignVal = cssProps.get(CssConstants.VERTICAL_ALIGN);
116+
if (CssConstants.MIDDLE.equals(vAlignVal)) {
117+
element.setProperty(Property.INLINE_VERTICAL_ALIGNMENT,
118+
new InlineVerticalAlignment(InlineVerticalAlignmentType.MIDDLE));
119+
} else if (CssConstants.BOTTOM.equals(vAlignVal)) {
120+
element.setProperty(Property.INLINE_VERTICAL_ALIGNMENT,
121+
new InlineVerticalAlignment(InlineVerticalAlignmentType.BOTTOM));
122+
} else if (CssConstants.TOP.equals(vAlignVal)) {
123+
element.setProperty(Property.INLINE_VERTICAL_ALIGNMENT,
124+
new InlineVerticalAlignment(InlineVerticalAlignmentType.TOP));
125+
} else if (CssConstants.TEXT_BOTTOM.equals(vAlignVal)) {
126+
element.setProperty(Property.INLINE_VERTICAL_ALIGNMENT,
127+
new InlineVerticalAlignment(InlineVerticalAlignmentType.TEXT_BOTTOM));
128+
} else if (CssConstants.TEXT_TOP.equals(vAlignVal)) {
129+
element.setProperty(Property.INLINE_VERTICAL_ALIGNMENT,
130+
new InlineVerticalAlignment(InlineVerticalAlignmentType.TEXT_TOP));
131+
} else if ( CssConstants.SUPER.equals((vAlignVal))) {
132+
element.setProperty(Property.INLINE_VERTICAL_ALIGNMENT,
133+
new InlineVerticalAlignment(InlineVerticalAlignmentType.SUPER));
134+
} else if ( CssConstants.SUB.equals((vAlignVal))) {
135+
element.setProperty(Property.INLINE_VERTICAL_ALIGNMENT,
136+
new InlineVerticalAlignment(InlineVerticalAlignmentType.SUB));
137+
} else if ( CssTypesValidationUtils.isPercentageValue(vAlignVal) ) {
138+
element.setProperty(Property.INLINE_VERTICAL_ALIGNMENT,
139+
new InlineVerticalAlignment(InlineVerticalAlignmentType.FRACTION,
140+
CssDimensionParsingUtils.parseRelativeValue(vAlignVal,1)));
141+
} else if ( CssTypesValidationUtils.isValidNumericValue(vAlignVal) ) {
142+
element.setProperty(Property.INLINE_VERTICAL_ALIGNMENT,
143+
new InlineVerticalAlignment(InlineVerticalAlignmentType.FIXED,
144+
CssDimensionParsingUtils.parseAbsoluteLength(vAlignVal)));
145+
} else {
146+
element.setProperty(Property.INLINE_VERTICAL_ALIGNMENT,
147+
new InlineVerticalAlignment(InlineVerticalAlignmentType.BASELINE));
148+
}
149+
}
150+
}
151+
101152
/**
102153
* Apply vertical alignment to inline elements.
103154
*

src/main/java/com/itextpdf/html2pdf/css/resolve/HtmlStylesToCssConverter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.html2pdf.css.resolve;
4444

45-
import com.itextpdf.html2pdf.html.AttributeConstants;
4645
import com.itextpdf.html2pdf.css.CssConstants;
46+
import com.itextpdf.html2pdf.html.AttributeConstants;
4747
import com.itextpdf.html2pdf.html.TagConstants;
4848
import com.itextpdf.styledxmlparser.css.CssDeclaration;
4949
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.BorderShorthandResolver;
50-
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
5150
import com.itextpdf.styledxmlparser.css.util.CssDimensionParsingUtils;
51+
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
5252
import com.itextpdf.styledxmlparser.node.IAttribute;
5353
import com.itextpdf.styledxmlparser.node.IElementNode;
5454
import com.itextpdf.styledxmlparser.node.INode;
@@ -565,7 +565,8 @@ public List<CssDeclaration> convert(IElementNode element, String value) {
565565
}
566566
} else if (TagConstants.TABLE.equals(element.name()) || TagConstants.IMG.equals(element.name())) {
567567
if (TagConstants.IMG.equals(element.name())
568-
&& (AttributeConstants.TOP.equals(value) || AttributeConstants.MIDDLE.equals(value) || AttributeConstants.BOTTOM.equals(value))) {
568+
&& (AttributeConstants.TOP.equals(value) || AttributeConstants.MIDDLE.equals(value))) {
569+
// No BOTTOM here because VERTICAL_ALIGN is deprecated and BOTTOM is translated to nothing
569570
result.add(new CssDeclaration(CssConstants.VERTICAL_ALIGN, value));
570571

571572
} else if (AttributeConstants.LEFT.equals(value) || AttributeConstants.RIGHT.equals(value)) {
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.itextpdf.html2pdf.css;
2+
3+
import com.itextpdf.html2pdf.ExtendedHtmlConversionITextTest;
4+
import com.itextpdf.test.annotations.type.IntegrationTest;
5+
6+
import java.io.IOException;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
import org.junit.experimental.categories.Category;
10+
11+
@Category(IntegrationTest.class)
12+
public class VerticalAlignmentInlineBlockTest extends ExtendedHtmlConversionITextTest {
13+
public static final String sourceFolder = "./src/test/resources/com/itextpdf/html2pdf/css/VerticalAlignmentInlineBlockTest/";
14+
public static final String destinationFolder = "./target/test/com/itextpdf/html2pdf/css/VerticalAlignmentInlineBlockTest/";
15+
16+
@BeforeClass
17+
public static void beforeClass() {
18+
createOrClearDestinationFolder(destinationFolder);
19+
}
20+
21+
22+
@Test
23+
public void checkBaselineAlignmentTest() throws IOException, InterruptedException {
24+
convertToPdfAndCompare("baseline", sourceFolder, destinationFolder);
25+
}
26+
27+
@Test
28+
public void checkBaselineAlignmentWitWrapTest() throws IOException, InterruptedException {
29+
convertToPdfAndCompare("baseline-wrap", sourceFolder, destinationFolder);
30+
}
31+
32+
@Test
33+
public void checkBottomAlignmentTest() throws IOException, InterruptedException {
34+
convertToPdfAndCompare("bottom", sourceFolder, destinationFolder);
35+
}
36+
37+
@Test
38+
public void checkBottomAlignmentWitWrapTest() throws IOException, InterruptedException {
39+
convertToPdfAndCompare("bottom-wrap", sourceFolder, destinationFolder);
40+
}
41+
42+
@Test
43+
public void checkMiddleAlignmentTest() throws IOException, InterruptedException {
44+
convertToPdfAndCompare("middle", sourceFolder, destinationFolder);
45+
}
46+
47+
@Test
48+
public void checkMiddleAlignmentWitWrapTest() throws IOException, InterruptedException {
49+
convertToPdfAndCompare("middle-wrap", sourceFolder, destinationFolder);
50+
}
51+
@Test
52+
public void checkTopAlignmentTest() throws IOException, InterruptedException {
53+
convertToPdfAndCompare("top", sourceFolder, destinationFolder);
54+
}
55+
56+
@Test
57+
public void checkTopAlignmentWitWrapTest() throws IOException, InterruptedException {
58+
convertToPdfAndCompare("top-wrap", sourceFolder, destinationFolder);
59+
}
60+
@Test
61+
public void checkMixedAlignmentTest() throws IOException, InterruptedException {
62+
convertToPdfAndCompare("mixed", sourceFolder, destinationFolder);
63+
}
64+
65+
@Test
66+
// TODO DEVSIX-3757 Update reference doc if the result matched the expected result
67+
public void checkMixedAlignment2Test() throws IOException, InterruptedException {
68+
convertToPdfAndCompare("mixed2", sourceFolder, destinationFolder);
69+
}
70+
71+
@Test
72+
// TODO DEVSIX-3757 Update reference doc if the result matched the expected result
73+
public void checkMixedAlignment3Test() throws IOException, InterruptedException {
74+
convertToPdfAndCompare("mixed3", sourceFolder, destinationFolder);
75+
}
76+
@Test
77+
public void checkCustomerExampleTest() throws IOException, InterruptedException {
78+
convertToPdfAndCompare("customerExample", sourceFolder, destinationFolder);
79+
}
80+
81+
@Test
82+
public void checkSingleImageTest() throws IOException, InterruptedException {
83+
convertToPdfAndCompare("singleimage", sourceFolder, destinationFolder);
84+
}
85+
86+
@Test
87+
public void checkElementsInDivAlignmentTest() throws IOException, InterruptedException {
88+
convertToPdfAndCompare("ElementsInDiv", sourceFolder, destinationFolder);
89+
}
90+
91+
@Test
92+
public void checkSpanAlignmentTest() throws IOException, InterruptedException {
93+
convertToPdfAndCompare("span", sourceFolder, destinationFolder);
94+
}
95+
96+
@Test
97+
// TODO DEVSIX-3757 Update reference doc if the result matched the expected result
98+
public void checkStyledElementsAlignmentTest() throws IOException, InterruptedException {
99+
convertToPdfAndCompare("styleAlignment", sourceFolder, destinationFolder);
100+
}
101+
102+
@Test
103+
public void checkUnorderedListAlignmentTest() throws IOException, InterruptedException {
104+
convertToPdfAndCompare("unorderedList", sourceFolder, destinationFolder);
105+
}
106+
107+
@Test
108+
public void orderedListAlignmentTest() throws IOException, InterruptedException {
109+
convertToPdfAndCompare("orderedList", sourceFolder, destinationFolder);
110+
}
111+
112+
@Test
113+
public void tableAlignmentTest() throws IOException, InterruptedException {
114+
convertToPdfAndCompare("table", sourceFolder, destinationFolder);
115+
}
116+
117+
@Test
118+
public void buttonAlignmentTest() throws IOException, InterruptedException {
119+
convertToPdfAndCompare("button", sourceFolder, destinationFolder);
120+
}
121+
122+
@Test
123+
// TODO DEVSIX-3757 Update reference doc if the result matched the expected result
124+
public void formAlignmentTest() throws IOException, InterruptedException {
125+
convertToPdfAndCompare("form", sourceFolder, destinationFolder);
126+
}
127+
128+
@Test
129+
public void headerAlignmentTest() throws IOException, InterruptedException {
130+
convertToPdfAndCompare("header", sourceFolder, destinationFolder);
131+
}
132+
133+
@Test
134+
public void paragraphAlignmentTest() throws IOException, InterruptedException {
135+
convertToPdfAndCompare("paragraph", sourceFolder, destinationFolder);
136+
}
137+
138+
@Test
139+
// TODO DEVSIX-3757 Update reference doc if the result matched the expected result
140+
public void allStylesTest() throws IOException, InterruptedException {
141+
convertToPdfAndCompare("AllStyles", sourceFolder, destinationFolder);
142+
}
143+
}
Loading
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)