source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/xml/XmlStyleSource.java@ 3858

Last change on this file since 3858 was 3858, checked in by bastiK, 13 years ago

mapcss: support for opacity & icon-image

  • Property svn:eol-style set to native
File size: 14.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.xml;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.InputStream;
7import java.io.InputStreamReader;
8import java.io.IOException;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.LinkedList;
13import java.util.List;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.Node;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.OsmUtils;
19import org.openstreetmap.josm.data.osm.Relation;
20import org.openstreetmap.josm.data.osm.Way;
21import org.openstreetmap.josm.gui.mappaint.Cascade;
22import org.openstreetmap.josm.gui.mappaint.ElemStyle;
23import org.openstreetmap.josm.gui.mappaint.MultiCascade;
24import org.openstreetmap.josm.gui.mappaint.Range;
25import org.openstreetmap.josm.gui.mappaint.StyleSource;
26import org.openstreetmap.josm.gui.preferences.SourceEntry;
27import org.openstreetmap.josm.io.MirroredInputStream;
28import org.openstreetmap.josm.tools.Utils;
29import org.openstreetmap.josm.tools.XmlObjectParser;
30import org.xml.sax.SAXException;
31import org.xml.sax.SAXParseException;
32
33public class XmlStyleSource extends StyleSource {
34
35 protected final HashMap<String, IconPrototype> icons = new HashMap<String, IconPrototype>();
36 protected final HashMap<String, LinePrototype> lines = new HashMap<String, LinePrototype>();
37 protected final HashMap<String, LinemodPrototype> modifiers = new HashMap<String, LinemodPrototype>();
38 protected final HashMap<String, AreaPrototype> areas = new HashMap<String, AreaPrototype>();
39 protected final LinkedList<IconPrototype> iconsList = new LinkedList<IconPrototype>();
40 protected final LinkedList<LinePrototype> linesList = new LinkedList<LinePrototype>();
41 protected final LinkedList<LinemodPrototype> modifiersList = new LinkedList<LinemodPrototype>();
42 protected final LinkedList<AreaPrototype> areasList = new LinkedList<AreaPrototype>();
43
44 public XmlStyleSource(String url, String name, String shortdescription) {
45 super(url, name, shortdescription);
46 }
47
48 public XmlStyleSource(SourceEntry entry) {
49 super(entry);
50 }
51
52 protected void init() {
53 hasError = false;
54 icons.clear();
55 lines.clear();
56 modifiers.clear();
57 areas.clear();
58 iconsList.clear();
59 linesList.clear();
60 modifiersList.clear();
61 areasList.clear();
62 }
63
64 @Override
65 public void loadStyleSource() {
66 init();
67 try {
68 MirroredInputStream in = new MirroredInputStream(url);
69 InputStream zip = in.getZipEntry("xml", "style");
70 InputStreamReader reader = null;
71 if (zip != null) {
72 reader = new InputStreamReader(zip);
73 zipIcons = in.getFile();
74 } else {
75 reader = new InputStreamReader(in);
76 zipIcons = null;
77 }
78
79 XmlObjectParser parser = new XmlObjectParser(new XmlStyleSourceHandler(this));
80 parser.startWithValidation(reader,
81 "http://josm.openstreetmap.de/mappaint-style-1.0",
82 "resource://data/mappaint-style.xsd");
83 while(parser.hasNext()) {
84 }
85
86 } catch(IOException e) {
87 System.err.println(tr("Warning: failed to load Mappaint styles from ''{0}''. Exception was: {1}", url, e.toString()));
88 e.printStackTrace();
89 hasError = true;
90 } catch(SAXParseException e) {
91 System.err.println(tr("Warning: failed to parse Mappaint styles from ''{0}''. Error was: [{1}:{2}] {3}", url, e.getLineNumber(), e.getColumnNumber(), e.getMessage()));
92 e.printStackTrace();
93 hasError = true;
94 } catch(SAXException e) {
95 System.err.println(tr("Warning: failed to parse Mappaint styles from ''{0}''. Error was: {1}", url, e.getMessage()));
96 e.printStackTrace();
97 hasError = true;
98 }
99 }
100
101 private static class WayPrototypesRecord {
102 public LinePrototype line;
103 public List<LinemodPrototype> linemods;
104 public AreaPrototype area;
105 }
106
107 private <T extends Prototype> T update(T current, T candidate, Double scale, MultiCascade mc) {
108 return requiresUpdate(current, candidate, scale, mc) ? candidate : current;
109 }
110
111 /**
112 * checks whether a certain match is better than the current match
113 * @param current can be null
114 * @param candidate the new Prototype that could be used instead
115 * @param scale ignored if null, otherwise checks if scale is within the range of candidate
116 * @param mc side effect: update the valid region for the current MultiCascade
117 */
118 private boolean requiresUpdate(Prototype current, Prototype candidate, Double scale, MultiCascade mc) {
119 if (current == null || candidate.priority >= current.priority) {
120 if (scale == null)
121 return true;
122
123 if (candidate.range.contains(scale)) {
124 mc.range = Range.cut(mc.range, candidate.range);
125 return true;
126 } else {
127 mc.range = mc.range.reduceAround(scale, candidate.range);
128 return false;
129 }
130 }
131 return false;
132 }
133
134 private IconPrototype getNode(OsmPrimitive primitive, Double scale, MultiCascade mc) {
135 IconPrototype icon = null;
136 for (String key : primitive.keySet()) {
137 String val = primitive.get(key);
138 IconPrototype p;
139 if ((p = icons.get("n" + key + "=" + val)) != null) {
140 icon = update(icon, p, scale, mc);
141 }
142 if ((p = icons.get("b" + key + "=" + OsmUtils.getNamedOsmBoolean(val))) != null) {
143 icon = update(icon, p, scale, mc);
144 }
145 if ((p = icons.get("x" + key)) != null) {
146 icon = update(icon, p, scale, mc);
147 }
148 }
149 for (IconPrototype s : iconsList) {
150 if (s.check(primitive))
151 {
152 icon = update(icon, s, scale, mc);
153 }
154 }
155 return icon;
156 }
157
158 /**
159 * @param closed The primitive is a closed way or we pretend it is closed.
160 * This is useful for multipolygon relations and outer ways of untagged
161 * multipolygon relations.
162 */
163 private void get(OsmPrimitive primitive, boolean closed, WayPrototypesRecord p, Double scale, MultiCascade mc) {
164 String lineIdx = null;
165 HashMap<String, LinemodPrototype> overlayMap = new HashMap<String, LinemodPrototype>();
166 for (String key : primitive.keySet()) {
167 String val = primitive.get(key);
168 AreaPrototype styleArea;
169 LinePrototype styleLine;
170 LinemodPrototype styleLinemod;
171 String idx = "n" + key + "=" + val;
172 if ((styleArea = areas.get(idx)) != null && (closed || !styleArea.closed)) {
173 p.area = update(p.area, styleArea, scale, mc);
174 }
175 if ((styleLine = lines.get(idx)) != null) {
176 if (requiresUpdate(p.line, styleLine, scale, mc)) {
177 p.line = styleLine;
178 lineIdx = idx;
179 }
180 }
181 if ((styleLinemod = modifiers.get(idx)) != null) {
182 if (requiresUpdate(null, styleLinemod, scale, mc)) {
183 overlayMap.put(idx, styleLinemod);
184 }
185 }
186 idx = "b" + key + "=" + OsmUtils.getNamedOsmBoolean(val);
187 if ((styleArea = areas.get(idx)) != null && (closed || !styleArea.closed)) {
188 p.area = update(p.area, styleArea, scale, mc);
189 }
190 if ((styleLine = lines.get(idx)) != null) {
191 if (requiresUpdate(p.line, styleLine, scale, mc)) {
192 p.line = styleLine;
193 lineIdx = idx;
194 }
195 }
196 if ((styleLinemod = modifiers.get(idx)) != null) {
197 if (requiresUpdate(null, styleLinemod, scale, mc)) {
198 overlayMap.put(idx, styleLinemod);
199 }
200 }
201 idx = "x" + key;
202 if ((styleArea = areas.get(idx)) != null && (closed || !styleArea.closed)) {
203 p.area = update(p.area, styleArea, scale, mc);
204 }
205 if ((styleLine = lines.get(idx)) != null) {
206 if (requiresUpdate(p.line, styleLine, scale, mc)) {
207 p.line = styleLine;
208 lineIdx = idx;
209 }
210 }
211 if ((styleLinemod = modifiers.get(idx)) != null) {
212 if (requiresUpdate(null, styleLinemod, scale, mc)) {
213 overlayMap.put(idx, styleLinemod);
214 }
215 }
216 }
217 for (AreaPrototype s : areasList) {
218 if ((closed || !s.closed) && s.check(primitive)) {
219 p.area = update(p.area, s, scale, mc);
220 }
221 }
222 for (LinePrototype s : linesList) {
223 if (s.check(primitive)) {
224 p.line = update(p.line, s, scale, mc);
225 }
226 }
227 for (LinemodPrototype s : modifiersList) {
228 if (s.check(primitive)) {
229 if (requiresUpdate(null, s, scale, mc)) {
230 overlayMap.put(s.getCode(), s);
231 }
232 }
233 }
234 overlayMap.remove(lineIdx); // do not use overlay if linestyle is from the same rule (example: railway=tram)
235 if (!overlayMap.isEmpty()) {
236 List<LinemodPrototype> tmp = new LinkedList<LinemodPrototype>();
237 if (p.linemods != null) {
238 tmp.addAll(p.linemods);
239 }
240 tmp.addAll(overlayMap.values());
241 Collections.sort(tmp);
242 p.linemods = tmp;
243 }
244 }
245
246 public void add(XmlCondition c, Collection<XmlCondition> conditions, Prototype prot) {
247 if(conditions != null)
248 {
249 prot.conditions = conditions;
250 if (prot instanceof IconPrototype) {
251 iconsList.add((IconPrototype) prot);
252 } else if (prot instanceof LinemodPrototype) {
253 modifiersList.add((LinemodPrototype) prot);
254 } else if (prot instanceof LinePrototype) {
255 linesList.add((LinePrototype) prot);
256 } else if (prot instanceof AreaPrototype) {
257 areasList.add((AreaPrototype) prot);
258 } else
259 throw new RuntimeException();
260 }
261 else {
262 String key = c.getKey();
263 prot.code = key;
264 if (prot instanceof IconPrototype) {
265 icons.put(key, (IconPrototype) prot);
266 } else if (prot instanceof LinemodPrototype) {
267 modifiers.put(key, (LinemodPrototype) prot);
268 } else if (prot instanceof LinePrototype) {
269 lines.put(key, (LinePrototype) prot);
270 } else if (prot instanceof AreaPrototype) {
271 areas.put(key, (AreaPrototype) prot);
272 } else
273 throw new RuntimeException();
274 }
275 }
276
277 @Override
278 public void apply(MultiCascade mc, OsmPrimitive osm, double scale, OsmPrimitive multipolyOuterWay, boolean pretendWayIsClosed) {
279 Cascade def = mc.getCascade("default");
280 boolean useMinMaxScale = Main.pref.getBoolean("mappaint.zoomLevelDisplay", false);
281
282 if (osm instanceof Node || (osm instanceof Relation && "restriction".equals(osm.get("type")))) {
283 IconPrototype icon = getNode(osm, (useMinMaxScale ? scale : null), mc);
284 if (icon != null) {
285 def.put("icon-image", icon.icon);
286 if (osm instanceof Node) {
287 if (icon.annotate != null) {
288 if (icon.annotate) {
289 def.put("text", "yes");
290 } else {
291 def.remove("text");
292 }
293 }
294 }
295 }
296 } else if (osm instanceof Way || (osm instanceof Relation && ((Relation)osm).isMultipolygon())) {
297 WayPrototypesRecord p = new WayPrototypesRecord();
298 get(osm, pretendWayIsClosed || !(osm instanceof Way) || ((Way) osm).isClosed(), p, (useMinMaxScale ? scale : null), mc);
299 if (p.line != null) {
300 def.put("width", new Float(p.line.getWidth()));
301 def.putOrClear("real-width", p.line.realWidth != null ? new Float(p.line.realWidth) : null);
302 def.putOrClear("color", p.line.color);
303 if (p.line.color != null) {
304 int alpha = p.line.color.getAlpha();
305 if (alpha != 255) {
306 def.put("opacity", ElemStyle.color_int2float(alpha));
307 }
308 }
309 def.putOrClear("dashes", p.line.getDashed());
310 def.putOrClear("dashes-background-color", p.line.dashedColor);
311 }
312 Float refWidth = def.get("width", null, Float.class);
313 if (refWidth != null && p.linemods != null) {
314 int numOver = 0, numUnder = 0;
315
316 while (mc.containsKey(String.format("over_%d", ++numOver))) {}
317 while (mc.containsKey(String.format("under_%d", ++numUnder))) {}
318
319 for (LinemodPrototype mod : p.linemods) {
320 Cascade c;
321 if (mod.over) {
322 c = mc.getCascade(String.format("over_%d", numOver));
323 c.put("object-z-index", new Float(numOver));
324 ++numOver;
325 } else {
326 c = mc.getCascade(String.format("under_%d", numUnder));
327 c.put("object-z-index", new Float(-numUnder));
328 ++numUnder;
329 }
330 c.put("width", new Float(mod.getWidth(refWidth)));
331 c.putOrClear("color", mod.color);
332 if (mod.color != null) {
333 int alpha = mod.color.getAlpha();
334 if (alpha != 255) {
335 c.put("opacity", ElemStyle.color_int2float(alpha));
336 }
337 }
338 c.putOrClear("dashes", mod.getDashed());
339 c.putOrClear("dashes-background-color", mod.dashedColor);
340 }
341 }
342 if (multipolyOuterWay != null) {
343 WayPrototypesRecord p2 = new WayPrototypesRecord();
344 get(multipolyOuterWay, true, p2, (useMinMaxScale ? scale : null), mc);
345 if (Utils.equal(p.area, p2.area)) {
346 p.area = null;
347 }
348 }
349 if (p.area != null) {
350 def.putOrClear("fill-color", p.area.color);
351 def.remove("fill-image");
352 }
353 }
354 }
355
356}
Note: See TracBrowser for help on using the repository browser.