1 /**********************************************
2 * Copyright (C) 2010 Lukas Laag
3 * This file is part of lib-gwt-svg.
4 *
5 * libgwtsvg is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * libgwtsvg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with libgwtsvg. If not, see http://www.gnu.org/licenses/
17 **********************************************/
18 /*
19 * Copyright (c) 2004 World Wide Web Consortium,
20 *
21 * (Massachusetts Institute of Technology, European Research Consortium for
22 * Informatics and Mathematics, Keio University). All Rights Reserved. This
23 * work is distributed under the W3C(r) Software License [1] in the hope that
24 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26 *
27 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
28 */
29
30 package org.vectomatic.dom.svg;
31
32 import org.vectomatic.dom.svg.utils.SVGConstants;
33
34 import com.google.gwt.core.client.JavaScriptException;
35
36 /**
37 * The {@link org.vectomatic.dom.svg.OMSVGPaint} interface corresponds to
38 * basic type <a href='painting.html#SpecifyingPaint'><paint></a> and
39 * represents the values of properties <code>fill</code> and <code>stroke</code>.
40 * <p>Note: The {@link org.vectomatic.dom.svg.OMSVGPaint} interface is deprecated,
41 * and may be dropped from future versions of the SVG specification.</p>
42 */
43 public class OMSVGPaint extends OMSVGColor {
44 /**
45 * The paint type is not one of predefined types. It is invalid to attempt
46 * to define a new value of this type or to attempt to switch an existing
47 * value to this type.
48 */
49 public static final short SVG_PAINTTYPE_UNKNOWN = 0;
50 /**
51 * An sRGB color has been specified without an alterICC color specification.
52 */
53 public static final short SVG_PAINTTYPE_RGBCOLOR = 1;
54 /**
55 * An sRGB color has been specified along with an alterICC color specification.
56 */
57 public static final short SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR = 2;
58 /**
59 * Corresponds to a <span class='prop-value'>none</span> value on a <a href='painting.html#SpecifyingPaint'><paint></a>
60 * specification.
61 */
62 public static final short SVG_PAINTTYPE_NONE = 101;
63 /**
64 * Corresponds to a <span class='prop-value'>currentColor</span> value on
65 * a <a href='painting.html#SpecifyingPaint'><paint></a> specification.
66 */
67 public static final short SVG_PAINTTYPE_CURRENTCOLOR = 102;
68 /**
69 * A URI has been specified, along with an explicit <span class='prop-value'>none</span>
70 * as the backup paint method in case the URI is unavailable or invalid.
71 */
72 public static final short SVG_PAINTTYPE_URI_NONE = 103;
73 /**
74 * A URI has been specified, along with an sRGB color as the backup paint
75 * method in case the URI is unavailable or invalid.
76 */
77 public static final short SVG_PAINTTYPE_URI_CURRENTCOLOR = 104;
78 /**
79 * A URI has been specified, along with an sRGB color as the backup paint
80 * method in case the URI is unavailable or invalid.
81 */
82 public static final short SVG_PAINTTYPE_URI_RGBCOLOR = 105;
83 /**
84 * A URI has been specified, along with both an sRGB color and alternate ICC
85 * color as the backup paint method in case the URI is unavailable or invalid.
86 */
87 public static final short SVG_PAINTTYPE_URI_RGBCOLOR_ICCCOLOR = 106;
88 /**
89 * Only a URI has been specified.
90 */
91 public static final short SVG_PAINTTYPE_URI = 107;
92
93 private short paintType;
94 private String uri;
95
96 public OMSVGPaint(short paintType) {
97 this.paintType = paintType;
98 switch(paintType) {
99 case SVG_PAINTTYPE_NONE:
100 cssText = SVGConstants.CSS_NONE_VALUE;
101 break;
102 case SVG_PAINTTYPE_CURRENTCOLOR:
103 cssText = SVGConstants.CSS_CURRENTCOLOR_VALUE;
104 colorType = SVG_COLORTYPE_CURRENTCOLOR;
105 break;
106 }
107 }
108
109 @Override
110 public int hashCode() {
111 int hashCode = paintType;
112 if (rgbColor != null) {
113 hashCode += rgbColor.hashCode();
114 }
115 if (iccColor != null) {
116 hashCode += iccColor.hashCode();
117 }
118 if (uri != null) {
119 hashCode += uri.hashCode();
120 }
121 return hashCode;
122 }
123
124 @Override
125 public boolean equals(Object obj) {
126 if (obj instanceof OMSVGPaint) {
127 OMSVGPaint p = (OMSVGPaint)obj;
128 if (paintType == p.paintType) {
129 switch (paintType) {
130 case SVG_PAINTTYPE_NONE:
131 case SVG_PAINTTYPE_CURRENTCOLOR:
132 case SVG_PAINTTYPE_UNKNOWN:
133 return true;
134 case SVG_PAINTTYPE_RGBCOLOR:
135 return rgbColor.equals(p.rgbColor);
136 case SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR:
137 return rgbColor.equals(p.rgbColor) && iccColor.equals(p.iccColor);
138 case SVG_PAINTTYPE_URI:
139 case SVG_PAINTTYPE_URI_NONE:
140 return uri.equals(p.uri);
141 case SVG_PAINTTYPE_URI_RGBCOLOR:
142 return uri.equals(p.uri) && rgbColor.equals(p.rgbColor);
143 case SVG_PAINTTYPE_URI_RGBCOLOR_ICCCOLOR:
144 return uri.equals(p.uri) && rgbColor.equals(p.rgbColor) && iccColor.equals(p.iccColor);
145 }
146 }
147 }
148 return false;
149 }
150
151
152
153 // Implementation of the svg::SVGPaint W3C IDL interface
154 /**
155 * The type of paint, identified by one of the SVG_PAINTTYPE_ constants defined
156 * on this interface.
157 */
158 public final short getPaintType() {
159 return this.paintType;
160 }
161 /**
162 * When the {@link org.vectomatic.dom.svg.OMSVGPaint#getPaintType()} specifies
163 * a URI, this attribute holds the URI string. When the {@link org.vectomatic.dom.svg.OMSVGPaint#getPaintType()}
164 * does not specify a URI, this attribute is null.
165 */
166 public final String getUri() {
167 return this.uri;
168 }
169 /**
170 * Sets the {@link org.vectomatic.dom.svg.OMSVGPaint#getPaintType()} to SVG_PAINTTYPE_URI_NONE
171 * and sets {@link org.vectomatic.dom.svg.OMSVGPaint#getUri()} to the specified
172 * value.
173 * @param uri The URI for the desired paint server.
174 */
175 public final void setUri(String uri) {
176 this.paintType = SVG_PAINTTYPE_URI_NONE;
177 this.uri = uri;
178 this.rgbColor = null;
179 this.iccColor = null;
180 }
181 /**
182 * Sets the paint as specified by the parameters. If <var>paintType</var>
183 * requires a URI, then <var>uri</var> must be non-null; otherwise, <var>uri</var>
184 * must be null. If <var>paintType</var> requires an <code>RGBColor</code>,
185 * then <var>rgbColor</var> must be a string that matches <a href='types.html#DataTypeColor'><color></a>;
186 * otherwise, <var>rgbColor</var> must be null. If <var>paintType</var> requires
187 * an {@link org.vectomatic.dom.svg.OMSVGICCColor}, then <var>iccColor</var>
188 * must be a string that matches <a href='types.html#DataTypeICCColor'><icccolor></a>;
189 * otherwise, <var>iccColor</var> must be null.
190 * @param paintType One of the defined constants for {@link org.vectomatic.dom.svg.OMSVGPaint#getPaintType()}.
191 * @param uri The URI for the desired paint server, or null.
192 * @param rgbColor The specification of an sRGB color, or null.
193 * @param iccColor The specification of an ICC color, or null.
194 * @throws SVGException(SVG_INVALID_VALUE_ERR) Raised if one of the parameters
195 * has an invalid value.
196 */
197 public final void setPaint(short paintType, String uri, String rgbColor, String iccColor) throws JavaScriptException {
198 if (((paintType == SVG_PAINTTYPE_RGBCOLOR && uri == null) || (paintType == SVG_PAINTTYPE_URI_RGBCOLOR && uri != null)) && iccColor == null) {
199 setRGBColor(rgbColor);
200 } else if ((paintType == SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR && uri == null) || (paintType == SVG_PAINTTYPE_URI_RGBCOLOR_ICCCOLOR && uri != null)) {
201 setRGBColorICCColor(rgbColor, iccColor);
202 } else if ((paintType == SVG_PAINTTYPE_NONE && uri == null) || (paintType == SVG_PAINTTYPE_URI_NONE && uri != null)) {
203 setColor(SVG_COLORTYPE_UNKNOWN, rgbColor, iccColor);
204 } else if ((paintType == SVG_PAINTTYPE_CURRENTCOLOR && uri == null) || (paintType == SVG_PAINTTYPE_URI_CURRENTCOLOR && uri != null)) {
205 setColor(SVG_COLORTYPE_CURRENTCOLOR, rgbColor, iccColor);
206 } else if (paintType == SVG_PAINTTYPE_URI) {
207 this.rgbColor = null;
208 this.iccColor = null;
209 this.cssText = "url(" + uri + ")";
210 } else {
211 throw new JavaScriptException("Invalid paint spec");
212 }
213 this.paintType = paintType;
214 this.uri = uri;
215 if (paintType != SVG_PAINTTYPE_URI && cssText != null && uri != null) {
216 cssText = "url(" + uri + ") " + cssText;
217 }
218 }
219
220 @Override
221 public String getDescription() {
222 StringBuilder builder = new StringBuilder("OMSVGPaint(paintType=");
223 builder.append(paintType);
224 builder.append(", uri=");
225 builder.append(uri);
226 builder.append(", colorType=");
227 builder.append(colorType);
228 builder.append(", rgbColor=");
229 builder.append(rgbColor.getDescription());
230 builder.append(", iccColor=");
231 builder.append(iccColor.getDescription());
232 builder.append(", cssValueType=");
233 builder.append(cssValueType);
234 builder.append(", cssText=");
235 builder.append(cssText);
236 builder.append(")");
237 return builder.toString();
238 }
239
240 }