Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/svg/NonGeometricData.java
1463 views
1
package org.newdawn.slick.svg;
2
3
import java.util.Properties;
4
5
import org.newdawn.slick.Color;
6
7
/**
8
* A set of data about a shape that doesn't fit into it's geometric
9
* configuration.
10
*
11
* @author kevin
12
*/
13
public class NonGeometricData {
14
/** The ID of the figure */
15
public static final String ID = "id";
16
/** The fill type */
17
public static final String FILL = "fill";
18
/** The stroke type */
19
public static final String STROKE = "stroke";
20
/** The alpha value for filling */
21
public static final String OPACITY = "opacity";
22
/** The width of the line to draw */
23
public static final String STROKE_WIDTH = "stroke-width";
24
/** The mitre of the line to draw */
25
public static final String STROKE_MITERLIMIT = "stroke-miterlimit";
26
/** The dash definition of the line to draw */
27
public static final String STROKE_DASHARRAY = "stroke-dasharray";
28
/** The offset into the dash definition of the line to draw */
29
public static final String STROKE_DASHOFFSET = "stroke-dashoffset";
30
/** The alpha value for drawing */
31
public static final String STROKE_OPACITY = "stroke-opacity";
32
33
/** Value indicating that no settings has been specified */
34
public static final String NONE = "none";
35
36
/** The meta data stored for the figure */
37
private String metaData = "";
38
/** The attributes stored for the figure */
39
private Properties props = new Properties();
40
41
/**
42
* Create a set of non-geometric data for a figure
43
*
44
* @param metaData The meta data (either label or id) for the figure
45
*/
46
public NonGeometricData(String metaData) {
47
this.metaData = metaData;
48
addAttribute(STROKE_WIDTH, "1");
49
}
50
51
/**
52
* Morph the color from a string
53
*
54
* @param str The string to morph
55
* @return The new color string
56
*/
57
private String morphColor(String str) {
58
if (str.equals("")) {
59
return "#000000";
60
}
61
if (str.equals("white")) {
62
return "#ffffff";
63
}
64
if (str.equals("black")) {
65
return "#000000";
66
}
67
68
return str;
69
}
70
71
/**
72
* Add a configured style attribute into the data set
73
*
74
* @param attribute The attribute to add
75
* @param value The value to assign
76
*/
77
public void addAttribute(String attribute, String value) {
78
if (value == null) {
79
value = "";
80
}
81
82
if (attribute.equals(FILL) ) {
83
value = morphColor(value);
84
}
85
if (attribute.equals(STROKE_OPACITY)) {
86
if (value.equals("0")) {
87
props.setProperty(STROKE, "none");
88
}
89
}
90
if (attribute.equals(STROKE_WIDTH)) {
91
if (value.equals("")) {
92
value = "1";
93
}
94
if (value.endsWith("px")) {
95
value = value.substring(0,value.length()-2);
96
}
97
}
98
if (attribute.equals(STROKE)) {
99
if ("none".equals(props.getProperty(STROKE))) {
100
return;
101
}
102
if ("".equals(props.getProperty(STROKE))) {
103
return;
104
}
105
value = morphColor(value);
106
}
107
108
props.setProperty(attribute, value);
109
}
110
111
/**
112
* Check if a given attribute is in colour format
113
*
114
* @param attribute The attribute to check
115
* @return True if the attirbute value is in colour format
116
*/
117
public boolean isColor(String attribute) {
118
return getAttribute(attribute).startsWith("#");
119
}
120
121
/**
122
* Get the meta data assigned to the figure. Either the label or
123
* the id value.
124
*
125
* @return The meta data assigned to the figure
126
*/
127
public String getMetaData() {
128
return metaData;
129
}
130
131
/**
132
* Get the attribtue value for a given attribute
133
*
134
* @param attribute The attribute whose value should be obtained
135
* @return The value for the given attribute
136
*/
137
public String getAttribute(String attribute) {
138
return props.getProperty(attribute);
139
}
140
141
/**
142
* Get an attribute value converted to a color. isColor should first be checked
143
*
144
* @param attribute The attribute whose value should be interpreted as a color
145
* @return The color based on the attribute
146
*/
147
public Color getAsColor(String attribute) {
148
if (!isColor(attribute)) {
149
throw new RuntimeException("Attribute "+attribute+" is not specified as a color:"+getAttribute(attribute));
150
}
151
152
int col = Integer.parseInt(getAttribute(attribute).substring(1), 16);
153
154
return new Color(col);
155
}
156
157
/**
158
* Get the attribute value as a reference to another entity
159
*
160
* @param attribute The name of the attribute to retrieve
161
* @return The reference part of the attribute value
162
*/
163
public String getAsReference(String attribute) {
164
String value = getAttribute(attribute);
165
if (value.length() < 7) {
166
return "";
167
}
168
169
value = value.substring(5, value.length()-1);
170
171
return value;
172
}
173
174
/**
175
* Get an attribute converted to a float value
176
*
177
* @param attribute The attribute to retrieve
178
* @return The float value derived from the attribute
179
*/
180
public float getAsFloat(String attribute) {
181
String value = getAttribute(attribute);
182
if (value == null) {
183
return 0;
184
}
185
186
try {
187
return Float.parseFloat(value);
188
} catch (NumberFormatException e) {
189
throw new RuntimeException("Attribute "+attribute+" is not specified as a float:"+getAttribute(attribute));
190
}
191
}
192
193
/**
194
* True if the shape is meant to be filled
195
*
196
* @return True if the shape is meant to be filled
197
*/
198
public boolean isFilled() {
199
return isColor(NonGeometricData.FILL);
200
}
201
202
/**
203
* True if the shape is meant to be outlined
204
*
205
* @return True if the shape is meant to be outlined
206
*/
207
public boolean isStroked() {
208
return isColor(NonGeometricData.STROKE) && (getAsFloat(NonGeometricData.STROKE_WIDTH) > 0);
209
}
210
}
211
212