Skip to content

Commit 7e1d486

Browse files
committed
finished parsing of device-cmyk() per draft css-color-4
1 parent ae4fc8d commit 7e1d486

File tree

4 files changed

+275
-25
lines changed

4 files changed

+275
-25
lines changed

org/w3c/css/parser/analyzer/CssParser.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6116,6 +6116,9 @@ final public boolean prio() throws ParseException {
61166116
} else if (funcname.equals("lch(")) {
61176117
color.setLCHColor(ac, exp);
61186118
{if ("" != null) return color;}
6119+
} else if (funcname.equals("device-cmyk(")) {
6120+
color.setDeviceCMYKColor(ac, exp);
6121+
{if ("" != null) return color;}
61196122
} else if (funcname.equals("image(")) {
61206123
CssImage img = new CssImage();
61216124
img.setImageList(exp, ac);
@@ -7099,6 +7102,12 @@ private boolean jj_3R_188()
70997102
return false;
71007103
}
71017104

7105+
private boolean jj_3R_217()
7106+
{
7107+
if (jj_scan_token(IDENT)) return true;
7108+
return false;
7109+
}
7110+
71027111
private boolean jj_3R_187()
71037112
{
71047113
if (jj_scan_token(RELFONTLENGTH)) return true;
@@ -7117,12 +7126,6 @@ private boolean jj_3R_185()
71177126
return false;
71187127
}
71197128

7120-
private boolean jj_3R_217()
7121-
{
7122-
if (jj_scan_token(IDENT)) return true;
7123-
return false;
7124-
}
7125-
71267129
private boolean jj_3R_184()
71277130
{
71287131
if (jj_3R_165()) return true;

org/w3c/css/parser/analyzer/CssParser.jj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3056,6 +3056,9 @@ CssValue function() :
30563056
} else if (funcname.equals("lch(")) {
30573057
color.setLCHColor(ac, exp);
30583058
return color;
3059+
} else if (funcname.equals("device-cmyk(")) {
3060+
color.setDeviceCMYKColor(ac, exp);
3061+
return color;
30593062
} else if (funcname.equals("image(")) {
30603063
CssImage img = new CssImage();
30613064
img.setImageList(exp, ac);

org/w3c/css/values/CssColor.java

Lines changed: 135 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.w3c.css.util.InvalidParamException;
1313

1414
import java.math.BigDecimal;
15-
import java.util.HashMap;
1615

1716
import static org.w3c.css.values.CssOperator.COMMA;
1817
import static org.w3c.css.values.CssOperator.SPACE;
@@ -36,6 +35,7 @@ public final int getType() {
3635
HWB hwb = null;
3736
LAB lab = null;
3837
LCH lch = null;
38+
DeviceCMYK cmyk = null;
3939

4040
/**
4141
* Create a new CssColor.
@@ -98,6 +98,8 @@ public String toString() {
9898
return lab.toString();
9999
} else if (lch != null) {
100100
return lch.toString();
101+
} else if (cmyk != null) {
102+
return cmyk.toString();
101103
}
102104
return "*invalid*";
103105
}
@@ -529,24 +531,6 @@ public void setShortRGBColor(ApplContext ac, String s)
529531
}
530532
}
531533

532-
protected boolean computeIdentColor(HashMap<String, Object> definitions,
533-
String s) {
534-
Object obj = definitions.get(s);
535-
if (obj != null) {
536-
if (obj instanceof RGB) {
537-
color = s;
538-
rgb = (RGB) obj;
539-
} else if (obj instanceof RGBA) {
540-
color = s;
541-
rgba = (RGBA) obj;
542-
} else if (obj instanceof String) {
543-
color = (String) obj;
544-
}
545-
return true;
546-
}
547-
return false;
548-
}
549-
550534
/**
551535
* Parse an ident color.
552536
*/
@@ -633,6 +617,14 @@ public boolean equals(Object cssColor) {
633617
return rgba.equals(otherColor.rgba);
634618
} else if ((hsl != null) && (otherColor.hsl != null)) {
635619
return hsl.equals(otherColor.hsl);
620+
} else if ((hwb != null) && (otherColor.hwb != null)) {
621+
return hwb.equals(otherColor.hwb);
622+
} else if ((lab != null) && (otherColor.lab != null)) {
623+
return lab.equals(otherColor.lab);
624+
} else if ((lch != null) && (otherColor.lch != null)) {
625+
return lch.equals(otherColor.lch);
626+
} else if ((cmyk != null) && (otherColor.cmyk != null)) {
627+
return cmyk.equals(otherColor.cmyk);
636628
}
637629
return false;
638630
}
@@ -1123,5 +1115,129 @@ public void setLCHColor(ApplContext ac, CssExpression exp)
11231115
}
11241116

11251117

1118+
public void setDeviceCMYKColor(ApplContext ac, CssExpression exp)
1119+
throws InvalidParamException {
1120+
// HWB defined in CSSColor Level 4 and onward, currently used in the CSS level
1121+
if (ac.getCssVersion().compareTo(CssVersion.CSS3) < 0) {
1122+
StringBuilder sb = new StringBuilder();
1123+
sb.append("device-cmyk(").append(exp.toStringFromStart()).append(')');
1124+
throw new InvalidParamException("notversion", sb.toString(),
1125+
ac.getCssVersionString(), ac);
1126+
}
1127+
1128+
color = null;
1129+
cmyk = new DeviceCMYK();
1130+
CssValue val = exp.getValue();
1131+
char op = exp.getOperator();
1132+
boolean gotFallback = false;
1133+
1134+
// C
1135+
if (val == null || op != SPACE) {
1136+
throw new InvalidParamException("invalid-color", ac);
1137+
}
1138+
switch (val.getType()) {
1139+
case CssTypes.CSS_NUMBER:
1140+
case CssTypes.CSS_PERCENTAGE:
1141+
cmyk.setC(ac, val);
1142+
break;
1143+
default:
1144+
throw new InvalidParamException("rgb", val, ac); // FIXME device-cmyk
1145+
}
1146+
1147+
// M
1148+
exp.next();
1149+
val = exp.getValue();
1150+
op = exp.getOperator();
1151+
if (val == null || op != SPACE) {
1152+
exp.starts();
1153+
throw new InvalidParamException("invalid-color", ac);
1154+
}
1155+
switch (val.getType()) {
1156+
case CssTypes.CSS_NUMBER:
1157+
case CssTypes.CSS_PERCENTAGE:
1158+
cmyk.setM(ac, val);
1159+
break;
1160+
default:
1161+
throw new InvalidParamException("rgb", val, ac); // FIXME device-cmyk
1162+
}
1163+
1164+
// Y
1165+
exp.next();
1166+
val = exp.getValue();
1167+
op = exp.getOperator();
1168+
if (val == null) {
1169+
throw new InvalidParamException("invalid-color", exp.toStringFromStart(), ac);
1170+
}
1171+
switch (val.getType()) {
1172+
case CssTypes.CSS_NUMBER:
1173+
case CssTypes.CSS_PERCENTAGE:
1174+
cmyk.setY(ac, val);
1175+
break;
1176+
default:
1177+
throw new InvalidParamException("rgb", val, ac); // FIXME device-cmyk
1178+
}
1179+
// K
1180+
exp.next();
1181+
val = exp.getValue();
1182+
op = exp.getOperator();
1183+
if (val == null) {
1184+
throw new InvalidParamException("invalid-color", exp.toStringFromStart(), ac);
1185+
}
1186+
switch (val.getType()) {
1187+
case CssTypes.CSS_NUMBER:
1188+
case CssTypes.CSS_PERCENTAGE:
1189+
cmyk.setK(ac, val);
1190+
break;
1191+
default:
1192+
throw new InvalidParamException("rgb", val, ac); // FIXME device-cmyk
1193+
}
1194+
1195+
exp.next();
1196+
if (!exp.end()) {
1197+
if (op == SPACE) {
1198+
// now we need an alpha.
1199+
val = exp.getValue();
1200+
op = exp.getOperator();
1201+
1202+
if (val.getType() != CssTypes.CSS_SWITCH) {
1203+
throw new InvalidParamException("rgb", val, ac);
1204+
}
1205+
if (op != SPACE) {
1206+
throw new InvalidParamException("invalid-color", ac);
1207+
}
1208+
exp.next();
1209+
// now we get the alpha value
1210+
val = exp.getValue();
1211+
if (val == null) {
1212+
throw new InvalidParamException("invalid-color", exp.toStringFromStart(), ac);
1213+
}
1214+
switch (val.getType()) {
1215+
case CssTypes.CSS_NUMBER:
1216+
case CssTypes.CSS_PERCENTAGE:
1217+
cmyk.setAlpha(ac, val);
1218+
break;
1219+
default:
1220+
exp.starts();
1221+
throw new InvalidParamException("rgb", val, ac); // FIXME lch
1222+
}
1223+
exp.next();
1224+
}
1225+
if (op == COMMA) {
1226+
//the optional fallback
1227+
if (exp.end()) {
1228+
throw new InvalidParamException("rgb", exp.toStringFromStart(), ac);
1229+
}
1230+
val = exp.getValue();
1231+
cmyk.setFallbackColor(ac, val);
1232+
exp.next();
1233+
}
1234+
}
1235+
// extra values?
1236+
if (!exp.end()) {
1237+
exp.starts();
1238+
throw new InvalidParamException("rgb", exp.toStringFromStart(), ac);
1239+
}
1240+
}
1241+
11261242
}
11271243

org/w3c/css/values/DeviceCMYK.java

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//
2+
// Author: Yves Lafon <ylafon@w3.org>
3+
//
4+
// (c) COPYRIGHT MIT, ERCIM, Keio University, Beihang University 2018.
5+
// Please first read the full copyright statement in file COPYRIGHT.html
6+
//
7+
package org.w3c.css.values;
8+
9+
import org.w3c.css.util.ApplContext;
10+
import org.w3c.css.util.InvalidParamException;
11+
12+
public class DeviceCMYK {
13+
String output = null;
14+
CssValue vc, vy, vm, vk, alpha, fallback = null;
15+
boolean faSet = false;
16+
17+
/**
18+
* Create a new DeviceCYMK
19+
*/
20+
public DeviceCMYK() {
21+
}
22+
23+
public final void setC(ApplContext ac, CssValue val)
24+
throws InvalidParamException {
25+
output = null;
26+
vc = RGBA.filterAlpha(ac, val);
27+
28+
}
29+
30+
public final void setY(ApplContext ac, CssValue val)
31+
throws InvalidParamException {
32+
output = null;
33+
vy = RGBA.filterAlpha(ac, val);
34+
}
35+
36+
public final void setM(ApplContext ac, CssValue val)
37+
throws InvalidParamException {
38+
output = null;
39+
vm = RGBA.filterAlpha(ac, val);
40+
}
41+
42+
public final void setK(ApplContext ac, CssValue val)
43+
throws InvalidParamException {
44+
output = null;
45+
vk = RGBA.filterAlpha(ac, val);
46+
}
47+
48+
public final void setAlpha(ApplContext ac, CssValue val)
49+
throws InvalidParamException {
50+
output = null;
51+
faSet = true;
52+
alpha = RGBA.filterAlpha(ac, val);
53+
}
54+
55+
public final void setFallbackColor(ApplContext ac, CssValue val)
56+
throws InvalidParamException {
57+
output = null;
58+
switch (val.getType()) {
59+
case CssTypes.CSS_HASH_IDENT:
60+
CssColor c = new org.w3c.css.values.CssColor();
61+
c.setShortRGBColor(ac, val.toString());
62+
fallback = c;
63+
break;
64+
case CssTypes.CSS_IDENT:
65+
fallback = new CssColor(ac, (String) val.get());
66+
break;
67+
case CssTypes.CSS_COLOR:
68+
fallback = val;
69+
break;
70+
case CssTypes.CSS_FUNCTION:
71+
CssFunction attr = (CssFunction) val;
72+
CssExpression params = attr.getParameters();
73+
String fname = attr.getName();
74+
75+
if (fname.equals("attr")) {
76+
CssValue v1 = params.getValue();
77+
params.next();
78+
CssValue v2 = params.getValue();
79+
if ((params.getCount() != 2)) {
80+
throw new InvalidParamException("value",
81+
params.getValue(),
82+
val.toString(), ac);
83+
} else if (v1.getType() != CssTypes.CSS_IDENT) {
84+
throw new InvalidParamException("value",
85+
params.getValue(),
86+
val.toString(), ac);
87+
88+
} else if (!(v2.toString().equals("color"))) {
89+
throw new InvalidParamException("value",
90+
params.getValue(),
91+
val.toString(), ac);
92+
} else {
93+
fallback = val;
94+
}
95+
} else {
96+
throw new InvalidParamException("value",
97+
params.getValue(),
98+
val.toString(), ac);
99+
}
100+
break;
101+
default:
102+
throw new InvalidParamException("value", "color",
103+
val.toString(), ac);
104+
}
105+
}
106+
107+
/**
108+
* Returns a string representation of the object.
109+
*/
110+
public String toString() {
111+
if (output == null) {
112+
StringBuilder sb = new StringBuilder("device-cmyk(");
113+
sb.append(vc).append(' ');
114+
sb.append(vm).append(' ');
115+
sb.append(vy).append(' ');
116+
sb.append(vk);
117+
if (faSet) {
118+
sb.append(" / ").append(alpha);
119+
}
120+
if (fallback != null) {
121+
sb.append(", ").append(fallback);
122+
}
123+
sb.append(')');
124+
output = sb.toString();
125+
}
126+
return output;
127+
}
128+
}

0 commit comments

Comments
 (0)