Skip to content

Commit ba533ac

Browse files
author
Andrei Stryhelski
committed
Add tests for device-cmyk functionality
DEVSIX-6783
1 parent fb001fc commit ba533ac

33 files changed

+357
-29
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ This file is part of the iText (R) project.
6161
import com.itextpdf.layout.properties.BlendMode;
6262
import com.itextpdf.layout.properties.BackgroundRepeat.BackgroundRepeatValue;
6363
import com.itextpdf.layout.properties.Property;
64+
import com.itextpdf.layout.properties.TransparentColor;
6465
import com.itextpdf.layout.properties.UnitValue;
6566
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
6667
import com.itextpdf.styledxmlparser.css.util.CssBackgroundUtils;
@@ -298,10 +299,8 @@ private static int getBackgroundSidePropertyIndex(final int propertiesNumber, fi
298299
private static void applyBackgroundColor(final String backgroundColorStr, final IPropertyContainer element,
299300
BackgroundBox clip) {
300301
if (backgroundColorStr != null && !CssConstants.TRANSPARENT.equals(backgroundColorStr)) {
301-
float[] rgbaColor = CssDimensionParsingUtils.parseRgbaColor(backgroundColorStr);
302-
Color color = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]);
303-
float opacity = rgbaColor[3];
304-
final Background backgroundColor = new Background(color, opacity, clip);
302+
TransparentColor color = CssDimensionParsingUtils.parseColor(backgroundColorStr);
303+
final Background backgroundColor = new Background(color.getColor(), color.getOpacity(), clip);
305304
element.setProperty(Property.BACKGROUND, backgroundColor);
306305
}
307306
}

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

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

4545
import com.itextpdf.html2pdf.attach.ProcessorContext;
4646
import com.itextpdf.html2pdf.css.CssConstants;
47+
import com.itextpdf.kernel.colors.Color;
4748
import com.itextpdf.kernel.colors.ColorConstants;
49+
import com.itextpdf.kernel.colors.DeviceCmyk;
4850
import com.itextpdf.kernel.colors.DeviceRgb;
4951
import com.itextpdf.layout.IPropertyContainer;
5052
import com.itextpdf.layout.borders.Border;
@@ -58,6 +60,7 @@ This file is part of the iText (R) project.
5860
import com.itextpdf.layout.borders.SolidBorder;
5961
import com.itextpdf.layout.properties.BorderRadius;
6062
import com.itextpdf.layout.properties.Property;
63+
import com.itextpdf.layout.properties.TransparentColor;
6164
import com.itextpdf.layout.properties.UnitValue;
6265
import com.itextpdf.styledxmlparser.css.resolve.CssDefaults;
6366
import com.itextpdf.styledxmlparser.css.util.CssDimensionParsingUtils;
@@ -191,13 +194,13 @@ public static Border getCertainBorder(String borderWidth, String borderStyle, St
191194
borderWidthValue = unitValue.getValue();
192195
Border border = null;
193196
if (borderWidthValue > 0) {
194-
DeviceRgb color = (DeviceRgb) ColorConstants.BLACK;
197+
Color color = ColorConstants.BLACK;
195198
float opacity = 1f;
196199
if (borderColor != null) {
197200
if (!CssConstants.TRANSPARENT.equals(borderColor)) {
198-
float[] rgbaColor = CssDimensionParsingUtils.parseRgbaColor(borderColor);
199-
color = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]);
200-
opacity = rgbaColor[3];
201+
TransparentColor tColor = CssDimensionParsingUtils.parseColor(borderColor);
202+
color = tColor.getColor();
203+
opacity = tColor.getOpacity();
201204
} else {
202205
opacity = 0f;
203206
}
@@ -219,16 +222,36 @@ public static Border getCertainBorder(String borderWidth, String borderStyle, St
219222
border = new DoubleBorder(color, borderWidthValue, opacity);
220223
break;
221224
case CssConstants.GROOVE:
222-
border = new GrooveBorder(color, borderWidthValue, opacity);
225+
if (color instanceof DeviceRgb) {
226+
border = new GrooveBorder((DeviceRgb)color, borderWidthValue, opacity);
227+
}
228+
if (color instanceof DeviceCmyk) {
229+
border = new GrooveBorder((DeviceCmyk)color, borderWidthValue, opacity);
230+
}
223231
break;
224232
case CssConstants.RIDGE:
225-
border = new RidgeBorder(color, borderWidthValue, opacity);
233+
if (color instanceof DeviceRgb) {
234+
border = new RidgeBorder((DeviceRgb)color, borderWidthValue, opacity);
235+
}
236+
if (color instanceof DeviceCmyk) {
237+
border = new RidgeBorder((DeviceCmyk)color, borderWidthValue, opacity);
238+
}
226239
break;
227240
case CssConstants.INSET:
228-
border = new InsetBorder(color, borderWidthValue, opacity);
241+
if (color instanceof DeviceRgb) {
242+
border = new InsetBorder((DeviceRgb)color, borderWidthValue, opacity);
243+
}
244+
if (color instanceof DeviceCmyk) {
245+
border = new InsetBorder((DeviceCmyk)color, borderWidthValue, opacity);
246+
}
229247
break;
230248
case CssConstants.OUTSET:
231-
border = new OutsetBorder(color, borderWidthValue, opacity);
249+
if (color instanceof DeviceRgb) {
250+
border = new OutsetBorder((DeviceRgb)color, borderWidthValue, opacity);
251+
}
252+
if (color instanceof DeviceCmyk) {
253+
border = new OutsetBorder((DeviceCmyk)color, borderWidthValue, opacity);
254+
}
232255
break;
233256
default:
234257
border = null;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ public static void applyFontStyles(Map<String, String> cssProps, ProcessorContex
125125
if (cssColorPropValue != null) {
126126
TransparentColor transparentColor;
127127
if (!CssConstants.TRANSPARENT.equals(cssColorPropValue)) {
128-
float[] rgbaColor = CssDimensionParsingUtils.parseRgbaColor(cssColorPropValue);
129-
Color color = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]);
130-
float opacity = rgbaColor[3];
128+
TransparentColor tColor = CssDimensionParsingUtils.parseColor(cssColorPropValue);
129+
Color color = tColor.getColor();
130+
float opacity = tColor.getOpacity();
131131
transparentColor = new TransparentColor(color, opacity);
132132
} else {
133133
transparentColor = new TransparentColor(ColorConstants.BLACK, 0f);
@@ -203,7 +203,7 @@ public static void applyFontStyles(Map<String, String> cssProps, ProcessorContex
203203
}
204204
}
205205

206-
float [] colors = new float[4];
206+
TransparentColor tColor;
207207
Color textDecorationColor;
208208
float opacity = 1f;
209209
String textDecorationColorProp = cssProps.get(CssConstants.TEXT_DECORATION_COLOR);
@@ -220,9 +220,9 @@ public static void applyFontStyles(Map<String, String> cssProps, ProcessorContex
220220
logger.error(Html2PdfLogMessageConstant.HSL_COLOR_NOT_SUPPORTED);
221221
textDecorationColor = ColorConstants.BLACK;
222222
} else {
223-
colors = CssDimensionParsingUtils.parseRgbaColor(textDecorationColorProp);
224-
textDecorationColor = new DeviceRgb(colors[0], colors[1], colors[2]);;
225-
opacity = colors[3];
223+
tColor = CssDimensionParsingUtils.parseColor(textDecorationColorProp);
224+
textDecorationColor = tColor.getColor();
225+
opacity = tColor.getOpacity();
226226
}
227227
}
228228

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

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

4545

4646
import com.itextpdf.html2pdf.attach.ProcessorContext;
47+
import com.itextpdf.kernel.colors.Color;
4748
import com.itextpdf.kernel.colors.ColorConstants;
49+
import com.itextpdf.kernel.colors.DeviceCmyk;
4850
import com.itextpdf.kernel.colors.DeviceRgb;
4951
import com.itextpdf.layout.IPropertyContainer;
5052
import com.itextpdf.layout.borders.Border;
@@ -57,6 +59,7 @@ This file is part of the iText (R) project.
5759
import com.itextpdf.layout.borders.RidgeBorder;
5860
import com.itextpdf.layout.borders.SolidBorder;
5961
import com.itextpdf.layout.properties.Property;
62+
import com.itextpdf.layout.properties.TransparentColor;
6063
import com.itextpdf.layout.properties.UnitValue;
6164
import com.itextpdf.html2pdf.css.CssConstants;
6265
import com.itextpdf.styledxmlparser.css.resolve.CssDefaults;
@@ -151,13 +154,13 @@ public static Border getCertainBorder(String outlineWidth, String outlineStyle,
151154
outlineWidthValue = unitValue.getValue();
152155
Border outline = null;
153156
if (outlineWidthValue > 0) {
154-
DeviceRgb color = (DeviceRgb) ColorConstants.BLACK;
157+
Color color = ColorConstants.BLACK;
155158
float opacity = 1f;
156159
if (outlineColor != null) {
157160
if (!CssConstants.TRANSPARENT.equals(outlineColor)) {
158-
float[] rgbaColor = CssDimensionParsingUtils.parseRgbaColor(outlineColor);
159-
color = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]);
160-
opacity = rgbaColor[3];
161+
TransparentColor tColor = CssDimensionParsingUtils.parseColor(outlineColor);
162+
color = tColor.getColor();
163+
opacity = tColor.getOpacity();
161164
} else {
162165
opacity = 0f;
163166
}
@@ -180,16 +183,36 @@ public static Border getCertainBorder(String outlineWidth, String outlineStyle,
180183
outline = new DoubleBorder(color, outlineWidthValue, opacity);
181184
break;
182185
case CssConstants.GROOVE:
183-
outline = new GrooveBorder(color, outlineWidthValue, opacity);
186+
if (color instanceof DeviceRgb) {
187+
outline = new GrooveBorder((DeviceRgb)color, outlineWidthValue, opacity);
188+
}
189+
if (color instanceof DeviceCmyk) {
190+
outline = new GrooveBorder((DeviceCmyk)color, outlineWidthValue, opacity);
191+
}
184192
break;
185193
case CssConstants.RIDGE:
186-
outline = new RidgeBorder(color, outlineWidthValue, opacity);
194+
if (color instanceof DeviceRgb) {
195+
outline = new RidgeBorder((DeviceRgb)color, outlineWidthValue, opacity);
196+
}
197+
if (color instanceof DeviceCmyk) {
198+
outline = new RidgeBorder((DeviceCmyk)color, outlineWidthValue, opacity);
199+
}
187200
break;
188201
case CssConstants.INSET:
189-
outline = new InsetBorder(color, outlineWidthValue, opacity);
202+
if (color instanceof DeviceRgb) {
203+
outline = new InsetBorder((DeviceRgb)color, outlineWidthValue, opacity);
204+
}
205+
if (color instanceof DeviceCmyk) {
206+
outline = new InsetBorder((DeviceCmyk)color, outlineWidthValue, opacity);
207+
}
190208
break;
191209
case CssConstants.OUTSET:
192-
outline = new OutsetBorder(color, outlineWidthValue, opacity);
210+
if (color instanceof DeviceRgb) {
211+
outline = new OutsetBorder((DeviceRgb)color, outlineWidthValue, opacity);
212+
}
213+
if (color instanceof DeviceCmyk) {
214+
outline = new OutsetBorder((DeviceCmyk)color, outlineWidthValue, opacity);
215+
}
193216
break;
194217
default:
195218
outline = null;

src/main/java/com/itextpdf/html2pdf/logs/Html2PdfLogMessageConstant.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ public final class Html2PdfLogMessageConstant {
201201
/** The Constant WORKER_UNABLE_TO_PROCESS_OTHER_WORKER. */
202202
public static final String WORKER_UNABLE_TO_PROCESS_OTHER_WORKER = "Worker of type {0} unable to process {1}";
203203

204+
/** The Constant ELEMENT_DOES_NOT_FIT_CURRENT_AREA. */
205+
public static final String ELEMENT_DOES_NOT_FIT_CURRENT_AREA = "Element does not fit current area";
206+
204207
private Html2PdfLogMessageConstant() {
205208
//Private constructor will prevent the instantiation of this class directly
206209
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ This file is part of the iText (R) project.
4545
import com.itextpdf.html2pdf.ExtendedHtmlConversionITextTest;
4646
import com.itextpdf.html2pdf.logs.Html2PdfLogMessageConstant;
4747
import com.itextpdf.io.logs.IoLogMessageConstant;
48+
import com.itextpdf.styledxmlparser.css.validate.CssDeclarationValidationMaster;
49+
import com.itextpdf.styledxmlparser.css.validate.impl.CssDefaultValidator;
50+
import com.itextpdf.styledxmlparser.css.validate.impl.CssDeviceCmykAwareValidator;
4851
import com.itextpdf.test.annotations.LogMessage;
4952
import com.itextpdf.test.annotations.LogMessages;
5053
import com.itextpdf.test.annotations.type.IntegrationTest;
@@ -130,6 +133,16 @@ public void border3DTest02() throws IOException, InterruptedException {
130133
convertToPdfAndCompare("border3DTest02", sourceFolder, destinationFolder);
131134
}
132135

136+
@Test
137+
public void border3DCmykTest() throws IOException, InterruptedException {
138+
try {
139+
CssDeclarationValidationMaster.setValidator(new CssDeviceCmykAwareValidator());
140+
convertToPdfAndCompare("border3DCmykTest", sourceFolder, destinationFolder);
141+
} finally {
142+
CssDeclarationValidationMaster.setValidator(new CssDefaultValidator());
143+
}
144+
}
145+
133146
@Test
134147
public void borderTransparencyTest01() throws IOException, InterruptedException {
135148
convertToPdfAndCompare("borderTransparencyTest01", sourceFolder, destinationFolder);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.itextpdf.html2pdf.css;
2+
3+
import com.itextpdf.html2pdf.ExtendedHtmlConversionITextTest;
4+
import com.itextpdf.html2pdf.logs.Html2PdfLogMessageConstant;
5+
import com.itextpdf.styledxmlparser.css.validate.CssDeclarationValidationMaster;
6+
import com.itextpdf.styledxmlparser.css.validate.impl.CssDefaultValidator;
7+
import com.itextpdf.styledxmlparser.css.validate.impl.CssDeviceCmykAwareValidator;
8+
import com.itextpdf.test.annotations.LogMessage;
9+
import com.itextpdf.test.annotations.LogMessages;
10+
import com.itextpdf.test.annotations.type.IntegrationTest;
11+
12+
import java.io.IOException;
13+
import org.junit.AfterClass;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
import org.junit.experimental.categories.Category;
17+
18+
@Category(IntegrationTest.class)
19+
public class DeviceCmykTest extends ExtendedHtmlConversionITextTest {
20+
21+
private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/html2pdf/css"
22+
+ "/DeviceCmykTest/";
23+
private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/html2pdf/css"
24+
+ "/DeviceCmykTest/";
25+
26+
@BeforeClass
27+
public static void beforeClass() {
28+
createOrClearDestinationFolder(DESTINATION_FOLDER);
29+
CssDeclarationValidationMaster.setValidator(new CssDeviceCmykAwareValidator());
30+
}
31+
32+
@AfterClass
33+
public static void after() {
34+
CssDeclarationValidationMaster.setValidator(new CssDefaultValidator());
35+
}
36+
37+
@Test
38+
public void bodyBgColorTest() throws IOException, InterruptedException {
39+
convertToPdfAndCompare("bodyBgColor", SOURCE_FOLDER, DESTINATION_FOLDER);
40+
}
41+
42+
@Test
43+
public void spanColorTest() throws IOException, InterruptedException {
44+
convertToPdfAndCompare("spanColor", SOURCE_FOLDER, DESTINATION_FOLDER);
45+
}
46+
47+
@Test
48+
public void spanBgColorTest() throws IOException, InterruptedException {
49+
convertToPdfAndCompare("spanBgColor", SOURCE_FOLDER, DESTINATION_FOLDER);
50+
}
51+
52+
@Test
53+
public void divColorTest() throws IOException, InterruptedException {
54+
convertToPdfAndCompare("divColor", SOURCE_FOLDER, DESTINATION_FOLDER);
55+
}
56+
57+
@Test
58+
public void divBgColorTest() throws IOException, InterruptedException {
59+
convertToPdfAndCompare("divBgColor", SOURCE_FOLDER, DESTINATION_FOLDER);
60+
}
61+
62+
@Test
63+
public void headerColorTest() throws IOException, InterruptedException {
64+
convertToPdfAndCompare("headerColor", SOURCE_FOLDER, DESTINATION_FOLDER);
65+
}
66+
67+
@Test
68+
public void headerBgColorTest() throws IOException, InterruptedException {
69+
convertToPdfAndCompare("headerBgColor", SOURCE_FOLDER, DESTINATION_FOLDER);
70+
}
71+
72+
@Test
73+
public void paragraphColorTest() throws IOException, InterruptedException {
74+
convertToPdfAndCompare("paragraphColor", SOURCE_FOLDER, DESTINATION_FOLDER);
75+
}
76+
77+
@Test
78+
public void paragraphBgColorTest() throws IOException, InterruptedException {
79+
convertToPdfAndCompare("paragraphBgColor", SOURCE_FOLDER, DESTINATION_FOLDER);
80+
}
81+
82+
@Test
83+
public void borderColorTest() throws IOException, InterruptedException {
84+
convertToPdfAndCompare("borderColor", SOURCE_FOLDER, DESTINATION_FOLDER);
85+
}
86+
87+
@Test
88+
@LogMessages(messages = {
89+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.ELEMENT_DOES_NOT_FIT_CURRENT_AREA)})
90+
public void simpleSvgColorTest() throws IOException, InterruptedException {
91+
convertToPdfAndCompare("simpleSvgColor", SOURCE_FOLDER, DESTINATION_FOLDER);
92+
}
93+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.itextpdf.html2pdf.css.apply.util;
2+
3+
import com.itextpdf.kernel.colors.Color;
4+
import com.itextpdf.kernel.colors.DeviceCmyk;
5+
import com.itextpdf.layout.borders.Border;
6+
import com.itextpdf.test.ExtendedITextTest;
7+
import com.itextpdf.test.annotations.type.UnitTest;
8+
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
import org.junit.experimental.categories.Category;
12+
13+
@Category(UnitTest.class)
14+
public class OutlineApplierUtilTest extends ExtendedITextTest {
15+
16+
@Test
17+
public void grooveBorderColorTest() {
18+
Border border = OutlineApplierUtil.getCertainBorder("10px", "groove", "device-cmyk(0, 81%, 81%, 30%", 12.0f,
19+
12.0f);
20+
Color expected = new DeviceCmyk(0, 81, 81, 30);
21+
Assert.assertEquals(expected, border.getColor());
22+
}
23+
24+
@Test
25+
public void ridgeBorderColorTest() {
26+
Border border = OutlineApplierUtil.getCertainBorder("10px", "ridge", "device-cmyk(0, 81%, 81%, 30%", 12.0f,
27+
12.0f);
28+
Color expected = new DeviceCmyk(0, 81, 81, 30);
29+
Assert.assertEquals(expected, border.getColor());
30+
}
31+
32+
@Test
33+
public void insetBorderColorTest() {
34+
Border border = OutlineApplierUtil.getCertainBorder("10px", "inset", "device-cmyk(0, 81%, 81%, 30%", 12.0f,
35+
12.0f);
36+
Color expected = new DeviceCmyk(0, 81, 81, 30);
37+
Assert.assertEquals(expected, border.getColor());
38+
}
39+
40+
@Test
41+
public void outsetBorderColorTest() {
42+
Border border = OutlineApplierUtil.getCertainBorder("10px", "outset", "device-cmyk(0, 81%, 81%, 30%", 12.0f,
43+
12.0f);
44+
Color expected = new DeviceCmyk(0, 81, 81, 30);
45+
Assert.assertEquals(expected, border.getColor());
46+
}
47+
48+
}

0 commit comments

Comments
 (0)