Skip to content

Commit f019e31

Browse files
committed
Upgrade to css4j 4.0
1 parent 1b46412 commit f019e31

File tree

3 files changed

+99
-55
lines changed

3 files changed

+99
-55
lines changed

build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ description = 'css4j-awt'
1212
dependencies {
1313
api('io.sf.carte:css4j') {
1414
version {
15-
strictly '[3.4.0,)'
16-
prefer '3.9.1'
15+
strictly '[4.0,)'
16+
prefer '4.0'
1717
}
1818
}
19-
testImplementation group: 'io.sf.carte', name: 'css4j', classifier: 'tests', version: '3.9.1'
19+
testImplementation group: 'io.sf.carte', name: 'css4j', classifier: 'tests', version: '4.0'
2020
testImplementation 'io.sf.carte:xml-dtd:4.1.1'
2121
testImplementation 'nu.validator:htmlparser:1.4.16'
2222
testImplementation 'org.slf4j:slf4j-api:2.0.6'

junit/io/sf/carte/doc/style/css/awt/AWTHelperTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,42 @@ public void testGetAWTColor() throws CSSPropertyValueException {
6262
assertEquals(0, color.getBlue());
6363
assertEquals(255, color.getAlpha());
6464
//
65+
style.setCssText("color: lch(32.67 12.93 244.59)");
66+
cssColor = style.getPropertyCSSValue("color");
67+
assertNotNull(cssColor);
68+
assertEquals(CssType.TYPED, cssColor.getCssValueType());
69+
assertEquals(CSSValue.Type.COLOR, ((CSSTypedValue) cssColor).getPrimitiveType());
70+
color = AWTHelper.getAWTColor((CSSTypedValue) cssColor);
71+
assertNotNull(color);
72+
assertEquals(58, color.getRed());
73+
assertEquals(80, color.getGreen());
74+
assertEquals(95, color.getBlue());
75+
assertEquals(255, color.getAlpha());
76+
//
77+
style.setCssText("color: color(display-p3 0.253 0.1087 0.7796/0.8)");
78+
cssColor = style.getPropertyCSSValue("color");
79+
assertNotNull(cssColor);
80+
assertEquals(CssType.TYPED, cssColor.getCssValueType());
81+
assertEquals(CSSValue.Type.COLOR, ((CSSTypedValue) cssColor).getPrimitiveType());
82+
color = AWTHelper.getAWTColor((CSSTypedValue) cssColor);
83+
assertNotNull(color);
84+
assertEquals(70, color.getRed());
85+
assertEquals(25, color.getGreen());
86+
assertEquals(207, color.getBlue());
87+
assertEquals(204, color.getAlpha());
88+
//
89+
style.setCssText("color: sandybrown");
90+
cssColor = style.getPropertyCSSValue("color");
91+
assertNotNull(cssColor);
92+
assertEquals(CssType.TYPED, cssColor.getCssValueType());
93+
assertEquals(CSSValue.Type.IDENT, ((CSSTypedValue) cssColor).getPrimitiveType());
94+
color = AWTHelper.getAWTColor((CSSTypedValue) cssColor);
95+
assertNotNull(color);
96+
assertEquals(244, color.getRed());
97+
assertEquals(164, color.getGreen());
98+
assertEquals(96, color.getBlue());
99+
assertEquals(255, color.getAlpha());
100+
//
65101
style.setCssText("color: transparent; ");
66102
cssColor = style.getPropertyCSSValue("color");
67103
assertNotNull(cssColor);

src/io/sf/carte/doc/style/css/awt/AWTHelper.java

+60-52
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
import java.util.HashMap;
1818
import java.util.Map;
1919

20+
import org.w3c.dom.DOMException;
21+
2022
import io.sf.carte.doc.style.css.CSSComputedProperties;
2123
import io.sf.carte.doc.style.css.CSSPrimitiveValue;
2224
import io.sf.carte.doc.style.css.CSSTypedValue;
2325
import io.sf.carte.doc.style.css.CSSUnit;
2426
import io.sf.carte.doc.style.css.CSSValue.CssType;
27+
import io.sf.carte.doc.style.css.CSSValue.Type;
2528
import io.sf.carte.doc.style.css.RGBAColor;
2629
import io.sf.carte.doc.style.css.property.CSSPropertyValueException;
27-
import io.sf.carte.doc.style.css.property.ColorIdentifiers;
2830

2931
/**
3032
* AWT helper methods.
@@ -86,61 +88,53 @@ public static Font createFont(CSSComputedProperties computedStyle) {
8688
* number or an identifier.
8789
* @return the AWT color object, or null if the color was specified as an
8890
* unknown identifier.
89-
* @throws CSSPropertyValueException if the color declaration is malformed or a
90-
* color identifier is unknown.
91+
* @throws CSSPropertyValueException if a color cannot be derived from the CSS
92+
* value.
9193
*/
9294
public static Color getAWTColor(CSSTypedValue cssColor) throws CSSPropertyValueException {
9395
Color awtcolor = null;
9496
if (cssColor != null) {
9597
switch (cssColor.getPrimitiveType()) {
9698
case COLOR:
97-
RGBAColor color = cssColor.toRGBColor();
98-
CSSPrimitiveValue red = color.getRed();
99-
CSSPrimitiveValue green = color.getGreen();
100-
CSSPrimitiveValue blue = color.getBlue();
101-
CSSPrimitiveValue prialpha = color.getAlpha();
102-
//
103-
if (red.getCssValueType() != CssType.TYPED || green.getCssValueType() != CssType.TYPED
104-
|| blue.getCssValueType() != CssType.TYPED || prialpha.getCssValueType() != CssType.TYPED) {
105-
CSSPropertyValueException ex = new CSSPropertyValueException("Unknown color.");
99+
case IDENT:
100+
RGBAColor color;
101+
try {
102+
color = cssColor.toRGBColor();
103+
} catch (RuntimeException e) {
104+
CSSPropertyValueException ex = new CSSPropertyValueException(
105+
"Cannot obtain a RGB color.", e);
106106
ex.setValueText(cssColor.getCssText());
107107
throw ex;
108108
}
109-
float alpha = ((CSSTypedValue) prialpha).getFloatValue(CSSUnit.CSS_NUMBER);
110-
switch (red.getUnitType()) {
111-
case CSSUnit.CSS_PERCENTAGE:
112-
awtcolor = new Color(clipColorValue(((CSSTypedValue) red).getFloatValue(CSSUnit.CSS_PERCENTAGE) / 100f),
113-
clipColorValue(((CSSTypedValue) green).getFloatValue(CSSUnit.CSS_PERCENTAGE) / 100f),
114-
clipColorValue(((CSSTypedValue) blue).getFloatValue(CSSUnit.CSS_PERCENTAGE) / 100f), alpha);
115-
break;
116-
case CSSUnit.CSS_NUMBER:
117-
try {
118-
awtcolor = new Color(clipColorValue((int) ((CSSTypedValue) red).getFloatValue(CSSUnit.CSS_NUMBER)),
119-
clipColorValue((int) ((CSSTypedValue) green).getFloatValue(CSSUnit.CSS_NUMBER)),
120-
clipColorValue((int) ((CSSTypedValue) blue).getFloatValue(CSSUnit.CSS_NUMBER)),
121-
Math.round(alpha * 255f));
122-
} catch (IllegalArgumentException e) {
123-
CSSPropertyValueException ex = new CSSPropertyValueException("Unknown color.", e);
124-
ex.setValueText(cssColor.getCssText());
125-
throw ex;
126-
}
109+
110+
double[] rgb;
111+
try {
112+
rgb = color.toNumberArray();
113+
} catch (RuntimeException e) {
114+
CSSPropertyValueException ex = new CSSPropertyValueException(
115+
"Cannot obtain the color components.", e);
116+
ex.setValueText(cssColor.getCssText());
117+
throw ex;
127118
}
128-
break;
129-
case IDENT:
130-
String sv = cssColor.getStringValue();
131-
String s = ColorIdentifiers.getInstance().getColor(sv);
132-
if (s != null) {
133-
try {
134-
awtcolor = Color.decode(s);
135-
} catch (NumberFormatException e) {
136-
CSSPropertyValueException ex = new CSSPropertyValueException("Unknown color", e);
137-
ex.setValueText(sv);
138-
throw ex;
139-
}
140-
} else if ("transparent".equals(sv)) {
141-
return new Color(0, 0, 0, 0);
142-
} else {
143-
return Color.getColor(sv);
119+
120+
CSSPrimitiveValue prialpha = color.getAlpha();
121+
122+
if (prialpha.getCssValueType() != CssType.TYPED) {
123+
CSSPropertyValueException ex = new CSSPropertyValueException(
124+
"Unsupported alpha channel.");
125+
ex.setValueText(cssColor.getCssText());
126+
throw ex;
127+
}
128+
129+
float alpha = normalizedAlphaComponent((CSSTypedValue) prialpha);
130+
131+
try {
132+
awtcolor = new Color((float) rgb[0], (float) rgb[1], (float) rgb[2], alpha);
133+
} catch (IllegalArgumentException e) {
134+
CSSPropertyValueException ex = new CSSPropertyValueException("Unknown color.",
135+
e);
136+
ex.setValueText(cssColor.getCssText());
137+
throw ex;
144138
}
145139
break;
146140
case NUMERIC:
@@ -156,12 +150,26 @@ public static Color getAWTColor(CSSTypedValue cssColor) throws CSSPropertyValueE
156150
return awtcolor;
157151
}
158152

159-
static int clipColorValue(int color) {
160-
return Math.max(Math.min(255, color), 0);
161-
}
162-
163-
static float clipColorValue(float color) {
164-
return Math.max(Math.min(1f, color), 0f);
153+
/**
154+
* Normalize the alpha component to a [0,1] interval.
155+
*
156+
* @param typed the component.
157+
* @return the normalized component.
158+
*/
159+
private static float normalizedAlphaComponent(CSSTypedValue typed) {
160+
float comp;
161+
short unit = typed.getUnitType();
162+
if (unit == CSSUnit.CSS_PERCENTAGE) {
163+
comp = typed.getFloatValue(CSSUnit.CSS_PERCENTAGE) * 0.01f;
164+
} else if (unit == CSSUnit.CSS_NUMBER) {
165+
comp = typed.getFloatValue(CSSUnit.CSS_NUMBER);
166+
} else if (typed.getPrimitiveType() == Type.IDENT) {
167+
comp = 0f;
168+
} else {
169+
throw new DOMException(DOMException.TYPE_MISMATCH_ERR,
170+
"Wrong component: " + typed.getCssText());
171+
}
172+
return Math.max(Math.min(1f, comp), 0f);
165173
}
166174

167175
}

0 commit comments

Comments
 (0)