Skip to content

Commit 637560f

Browse files
author
glenn.volckaert
committed
Add support for vertical alignment for inline elements
DEVSIX-6881
1 parent aed01b9 commit 637560f

38 files changed

+750
-107
lines changed

layout/src/main/java/com/itextpdf/layout/exceptions/LayoutExceptionMessageConstant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public final class LayoutExceptionMessageConstant {
4848
+ "role.";
4949
public static final String ROLE_IN_NAMESPACE_IS_NOT_MAPPED_TO_ANY_STANDARD_ROLE = "Role \"{0}\" in namespace {1} "
5050
+ "is not mapped to any standard role.";
51+
public static final String INLINE_VERTICAL_ALIGNMENT_DOESN_T_NEED_A_VALUE =
52+
"Inline vertical alignment \"{0}\" doesn't need a value";
5153

5254
private LayoutExceptionMessageConstant(){}
5355
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.itextpdf.layout.properties;
2+
3+
import com.itextpdf.kernel.exceptions.PdfException;
4+
import com.itextpdf.layout.exceptions.LayoutExceptionMessageConstant;
5+
6+
/**
7+
* A property corresponding to the css vertical-align property and used to
8+
* set vertical alignment on inline blocks, it specifies the type of alignment
9+
* and where needed a numerical value to complete it.
10+
*/
11+
public class InlineVerticalAlignment {
12+
13+
private InlineVerticalAlignmentType type;
14+
private float value;
15+
16+
17+
/**
18+
* Creates a default InlineVerticalAlignment, it gets the type {@link InlineVerticalAlignmentType#BASELINE}.
19+
*/
20+
public InlineVerticalAlignment () {
21+
type = InlineVerticalAlignmentType.BASELINE;
22+
}
23+
24+
/**
25+
* Creates an InlineVerticalAlignment with a specified type.
26+
*
27+
* @param type {@link InlineVerticalAlignmentType}
28+
*/
29+
public InlineVerticalAlignment (InlineVerticalAlignmentType type) {
30+
this.type = type;
31+
}
32+
33+
/**
34+
* Creates an InlineVerticalAlignment with a specified type and a value.
35+
* This will throw a {@link PdfException} when used with a type that does not require a value.
36+
*
37+
* @param type {@link InlineVerticalAlignmentType}
38+
* @param value In the case of {@link InlineVerticalAlignmentType#FIXED} a lenth in pts,
39+
* in case of {@link InlineVerticalAlignmentType#FRACTION} a multiplier value.
40+
*/
41+
public InlineVerticalAlignment (InlineVerticalAlignmentType type, float value) {
42+
if (!(type == InlineVerticalAlignmentType.FRACTION || type == InlineVerticalAlignmentType.FIXED)) {
43+
throw new PdfException(LayoutExceptionMessageConstant.INLINE_VERTICAL_ALIGNMENT_DOESN_T_NEED_A_VALUE)
44+
.setMessageParams(type);
45+
46+
}
47+
this.type = type;
48+
this.value = value;
49+
}
50+
51+
/**
52+
* Gets the type of InlineVerticalAlignment.
53+
*
54+
* @return the type {@link InlineVerticalAlignmentType}
55+
*/
56+
public InlineVerticalAlignmentType getType() {
57+
return type;
58+
}
59+
60+
/**
61+
* Sets the type {@link InlineVerticalAlignmentType}.
62+
*
63+
* @param type {@link InlineVerticalAlignmentType}
64+
*/
65+
public void setType(InlineVerticalAlignmentType type) {
66+
this.type = type;
67+
}
68+
69+
/**
70+
* Gets the value.
71+
*
72+
* @return value In the case of {@link InlineVerticalAlignmentType#FIXED} a lenth in pts,
73+
* in case of {@link InlineVerticalAlignmentType#FRACTION} a multiplier value.
74+
*/
75+
76+
public float getValue() {
77+
return value;
78+
}
79+
80+
/**
81+
* Sets the value.
82+
*
83+
* @param value In the case of {@link InlineVerticalAlignmentType#FIXED} a lenth in pts,
84+
* in case of {@link InlineVerticalAlignmentType#FRACTION} a multiplier value.
85+
*/
86+
public void setValue(float value) {
87+
this.value = value;
88+
}
89+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.itextpdf.layout.properties;
2+
3+
/**
4+
* The possible values for {@link InlineVerticalAlignment#getType()}.
5+
*/
6+
public enum InlineVerticalAlignmentType {
7+
// Strut oriented alignments
8+
BASELINE,
9+
TEXT_TOP,
10+
TEXT_BOTTOM,
11+
SUB,
12+
SUPER,
13+
/**
14+
* Fixed is used when a length value is given in css.
15+
* It needs a companion value in {@link InlineVerticalAlignment#setValue(float)}
16+
*/
17+
FIXED,
18+
/**
19+
* Fixed is used when a percentage value is given in css.
20+
* It needs a companion value in {@link InlineVerticalAlignment#setValue(float)}
21+
*/
22+
FRACTION,
23+
// middle of x height above baseline
24+
MIDDLE,
25+
// From here alignments are box oriented, the others are strut (text line) oriented
26+
TOP,
27+
BOTTOM
28+
}

layout/src/main/java/com/itextpdf/layout/properties/Property.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ public final class Property {
210210
public static final int UNDERLINE = 74;
211211
public static final int VERTICAL_ALIGNMENT = 75;
212212
public static final int VERTICAL_BORDER_SPACING = 116;
213+
public static final int INLINE_VERTICAL_ALIGNMENT = 136;
214+
213215
/**
214216
* Value of 1 is equivalent to no scaling
215217
**/

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ This file is part of the iText (R) project.
4343
*/
4444
package com.itextpdf.layout.renderer;
4545

46-
import com.itextpdf.io.logs.IoLogMessageConstant;
4746
import com.itextpdf.commons.utils.MessageFormatUtil;
47+
import com.itextpdf.io.logs.IoLogMessageConstant;
4848
import com.itextpdf.io.util.NumberUtil;
4949
import com.itextpdf.kernel.colors.Color;
5050
import com.itextpdf.kernel.colors.gradients.AbstractLinearGradientBuilder;
@@ -83,8 +83,8 @@ This file is part of the iText (R) project.
8383
import com.itextpdf.layout.minmaxwidth.MinMaxWidth;
8484
import com.itextpdf.layout.minmaxwidth.MinMaxWidthUtils;
8585
import com.itextpdf.layout.properties.Background;
86-
import com.itextpdf.layout.properties.BackgroundImage;
8786
import com.itextpdf.layout.properties.BackgroundBox;
87+
import com.itextpdf.layout.properties.BackgroundImage;
8888
import com.itextpdf.layout.properties.BaseDirection;
8989
import com.itextpdf.layout.properties.BlendMode;
9090
import com.itextpdf.layout.properties.BorderRadius;
@@ -1840,27 +1840,31 @@ protected UnitValue[] getPaddings() {
18401840
* @return a {@link Rectangle border box} of the renderer
18411841
*/
18421842
protected Rectangle applyPaddings(Rectangle rect, UnitValue[] paddings, boolean reverse) {
1843-
if (!paddings[0].isPointValue()) {
1843+
if (paddings[0] != null && !paddings[0].isPointValue()) {
18441844
Logger logger = LoggerFactory.getLogger(AbstractRenderer.class);
18451845
logger.error(MessageFormatUtil.format(IoLogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED,
18461846
Property.PADDING_TOP));
18471847
}
1848-
if (!paddings[1].isPointValue()) {
1848+
if (paddings[1] != null && !paddings[1].isPointValue()) {
18491849
Logger logger = LoggerFactory.getLogger(AbstractRenderer.class);
18501850
logger.error(MessageFormatUtil.format(IoLogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED,
18511851
Property.PADDING_RIGHT));
18521852
}
1853-
if (!paddings[2].isPointValue()) {
1853+
if (paddings[2] != null && !paddings[2].isPointValue()) {
18541854
Logger logger = LoggerFactory.getLogger(AbstractRenderer.class);
18551855
logger.error(MessageFormatUtil.format(IoLogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED,
18561856
Property.PADDING_BOTTOM));
18571857
}
1858-
if (!paddings[3].isPointValue()) {
1858+
if (paddings[3] != null && !paddings[3].isPointValue()) {
18591859
Logger logger = LoggerFactory.getLogger(AbstractRenderer.class);
18601860
logger.error(MessageFormatUtil.format(IoLogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED,
18611861
Property.PADDING_LEFT));
18621862
}
1863-
return rect.applyMargins(paddings[0].getValue(), paddings[1].getValue(), paddings[2].getValue(), paddings[3].getValue(), reverse);
1863+
return rect.applyMargins(paddings[0] != null ? paddings[0].getValue() : 0,
1864+
paddings[1] != null ? paddings[1].getValue() : 0,
1865+
paddings[2] != null ? paddings[2].getValue() : 0,
1866+
paddings[3] != null ? paddings[3].getValue() : 3,
1867+
reverse);
18641868
}
18651869

18661870
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This file is part of the iText (R) project.
4343
*/
4444
package com.itextpdf.layout.renderer;
4545

46+
import com.itextpdf.commons.utils.MessageFormatUtil;
4647
import com.itextpdf.io.logs.IoLogMessageConstant;
4748
import com.itextpdf.kernel.geom.AffineTransform;
4849
import com.itextpdf.kernel.geom.Point;
@@ -69,11 +70,10 @@ This file is part of the iText (R) project.
6970
import com.itextpdf.layout.renderer.objectfit.ObjectFitApplyingResult;
7071
import com.itextpdf.layout.renderer.objectfit.ObjectFitCalculator;
7172
import com.itextpdf.layout.tagging.LayoutTaggingHelper;
72-
import com.itextpdf.commons.utils.MessageFormatUtil;
7373

74+
import java.util.List;
7475
import org.slf4j.Logger;
7576
import org.slf4j.LoggerFactory;
76-
import java.util.List;
7777

7878
public class ImageRenderer extends AbstractRenderer implements ILeafElementRenderer {
7979

0 commit comments

Comments
 (0)