1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Map.Entry;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.MultiCascade;
|
---|
18 | import org.openstreetmap.josm.gui.mappaint.Range;
|
---|
19 | import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
---|
20 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
|
---|
21 | import org.openstreetmap.josm.gui.mappaint.mapcss.parser.MapCSSParser;
|
---|
22 | import org.openstreetmap.josm.gui.mappaint.mapcss.parser.ParseException;
|
---|
23 | import org.openstreetmap.josm.gui.mappaint.mapcss.parser.TokenMgrError;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
25 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
26 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
27 | import org.openstreetmap.josm.tools.Utils;
|
---|
28 |
|
---|
29 | public class MapCSSStyleSource extends StyleSource {
|
---|
30 |
|
---|
31 | final public List<MapCSSRule> rules;
|
---|
32 | private Color backgroundColorOverride;
|
---|
33 |
|
---|
34 | public MapCSSStyleSource(String url, String name, String shortdescription) {
|
---|
35 | super(url, name, shortdescription);
|
---|
36 | rules = new ArrayList<MapCSSRule>();
|
---|
37 | }
|
---|
38 |
|
---|
39 | public MapCSSStyleSource(SourceEntry entry) {
|
---|
40 | super(entry);
|
---|
41 | rules = new ArrayList<MapCSSRule>();
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public void loadStyleSource() {
|
---|
46 | init();
|
---|
47 | rules.clear();
|
---|
48 | try {
|
---|
49 | MapCSSParser parser = new MapCSSParser(getSourceInputStream(), "UTF-8");
|
---|
50 | parser.sheet(this);
|
---|
51 | loadMeta();
|
---|
52 | loadCanvas();
|
---|
53 | } catch(IOException e) {
|
---|
54 | System.err.println(tr("Warning: failed to load Mappaint styles from ''{0}''. Exception was: {1}", url, e.toString()));
|
---|
55 | e.printStackTrace();
|
---|
56 | logError(e);
|
---|
57 | } catch (TokenMgrError e) {
|
---|
58 | System.err.println(tr("Warning: failed to parse Mappaint styles from ''{0}''. Error was: {1}", url, e.getMessage()));
|
---|
59 | e.printStackTrace();
|
---|
60 | logError(e);
|
---|
61 | } catch (ParseException e) {
|
---|
62 | System.err.println(tr("Warning: failed to parse Mappaint styles from ''{0}''. Error was: {1}", url, e.getMessage()));
|
---|
63 | e.printStackTrace();
|
---|
64 | logError(new ParseException(e.getMessage())); // allow e to be garbage collected, it links to the entire token stream
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public InputStream getSourceInputStream() throws IOException {
|
---|
69 | MirroredInputStream in = new MirroredInputStream(url);
|
---|
70 | InputStream zip = in.getZipEntry("mapcss", "style");
|
---|
71 | if (zip != null) {
|
---|
72 | zipIcons = in.getFile();
|
---|
73 | return zip;
|
---|
74 | } else {
|
---|
75 | zipIcons = null;
|
---|
76 | return in;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * load meta info from a selector "meta"
|
---|
82 | */
|
---|
83 | private void loadMeta() {
|
---|
84 | Cascade c = constructSpecial("meta");
|
---|
85 | String pTitle = c.get("title", null, String.class);
|
---|
86 | if (title == null) {
|
---|
87 | title = pTitle;
|
---|
88 | }
|
---|
89 | String pIcon = c.get("icon", null, String.class);
|
---|
90 | if (icon == null) {
|
---|
91 | icon = pIcon;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void loadCanvas() {
|
---|
96 | Cascade c = constructSpecial("canvas");
|
---|
97 | backgroundColorOverride = c.get("background-color", null, Color.class);
|
---|
98 | }
|
---|
99 |
|
---|
100 | private Cascade constructSpecial(String type) {
|
---|
101 |
|
---|
102 | MultiCascade mc = new MultiCascade();
|
---|
103 | Node n = new Node();
|
---|
104 | String code = LanguageInfo.getJOSMLocaleCode();
|
---|
105 | n.put("lang", code);
|
---|
106 | // create a fake environment to read the meta data block
|
---|
107 | Environment env = new Environment(n, mc, "default", this);
|
---|
108 |
|
---|
109 | NEXT_RULE:
|
---|
110 | for (MapCSSRule r : rules) {
|
---|
111 | for (Selector s : r.selectors) {
|
---|
112 | if ((s instanceof GeneralSelector)) {
|
---|
113 | GeneralSelector gs = (GeneralSelector) s;
|
---|
114 | if (gs.base.equals(type))
|
---|
115 | {
|
---|
116 | for (Condition cnd : gs.conds) {
|
---|
117 | if (!cnd.applies(env))
|
---|
118 | continue NEXT_RULE;
|
---|
119 | }
|
---|
120 | for (Instruction i : r.declaration) {
|
---|
121 | i.execute(env);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | return mc.getCascade("default");
|
---|
128 | }
|
---|
129 |
|
---|
130 | public Color getBackgroundColorOverride() {
|
---|
131 | return backgroundColorOverride;
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public void apply(MultiCascade mc, OsmPrimitive osm, double scale, OsmPrimitive multipolyOuterWay, boolean pretendWayIsClosed) {
|
---|
136 | Environment env = new Environment(osm, mc, null, this);
|
---|
137 | for (MapCSSRule r : rules) {
|
---|
138 | for (Selector s : r.selectors) {
|
---|
139 | if (s.applies(env)) {
|
---|
140 | if (s.getRange().contains(scale)) {
|
---|
141 | mc.range = Range.cut(mc.range, s.getRange());
|
---|
142 | } else {
|
---|
143 | mc.range = mc.range.reduceAround(scale, s.getRange());
|
---|
144 | continue;
|
---|
145 | }
|
---|
146 |
|
---|
147 | String sub = s.getSubpart();
|
---|
148 | if (sub == null) {
|
---|
149 | sub = "default";
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (sub.equals("*")) {
|
---|
153 | for (Entry<String, Cascade> entry : mc.getLayers()) {
|
---|
154 | env.layer = entry.getKey();
|
---|
155 | if (Utils.equal(env.layer, "*")) {
|
---|
156 | continue;
|
---|
157 | }
|
---|
158 | for (Instruction i : r.declaration) {
|
---|
159 | i.execute(env);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | env.layer = sub;
|
---|
164 | for (Instruction i : r.declaration) {
|
---|
165 | i.execute(env);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | @Override
|
---|
173 | public String toString() {
|
---|
174 | return Utils.join("\n", rules);
|
---|
175 | }
|
---|
176 | }
|
---|