MapCSS in progress. Vast amounts still to do, but the meat of it is here. POIs are...
[potlatch2.git] / net / systemeD / halcyon / styleparser / MapCSS.as
1 package net.systemeD.halcyon.styleparser {
2
3         /*
4                 MapCSS parser
5                 creates a RuleSet from a .mapcss file
6
7         */
8
9         import flash.events.*;
10         import flash.net.*;
11         import net.systemeD.halcyon.Globals;
12         import net.systemeD.halcyon.Map;
13
14         // ** also needs to support @import rules
15
16         public class MapCSS {
17                 private var map:Map;
18                 private var choosers:Array;
19
20                 private static const WHITESPACE:RegExp  =/^ \s+ /sx;
21                 private static const COMMENT:RegExp             =/^ \/* .+? *\/ \s* /sx;        /* */
22                 private static const CLASS:RegExp               =/^ ([\.:]\w+) \s* /sx;
23                 private static const ZOOM:RegExp                =/^ \| \s* z([\d\-]+) \s* /isx;
24                 private static const GROUP:RegExp               =/^ , \s* /isx;
25                 private static const CONDITION:RegExp   =/^ \[(.+?)\] \s* /sx;
26                 private static const OBJECT:RegExp              =/^ (\w+) \s* /sx;
27                 private static const DECLARATION:RegExp =/^ \{(.+?)\} \s* /sx;
28                 private static const UNKNOWN:RegExp             =/^ (\S+) \s* /sx;
29
30                 private static const ZOOM_MINMAX:RegExp =/^ (\d+)\-(\d+) $/sx;
31                 private static const ZOOM_MIN:RegExp    =/^ (\d+)\-      $/sx;
32                 private static const ZOOM_MAX:RegExp    =/^      \-(\d+) $/sx;
33                 private static const ZOOM_SINGLE:RegExp =/^        (\d+) $/sx;
34
35                 private static const CONDITION_TRUE:RegExp      =/^ \s* (\w+) \s* = \s* yes \s*  $/isx;
36                 private static const CONDITION_FALSE:RegExp     =/^ \s* (\w+) \s* = \s* no  \s*  $/isx;
37                 private static const CONDITION_SET:RegExp       =/^ \s* (\w+) \s* $/sx;
38                 private static const CONDITION_UNSET:RegExp     =/^ \s* !(\w+) \s* $/sx;
39                 private static const CONDITION_EQ:RegExp        =/^ \s* (\w+) \s* =  \s* (.+) \s* $/sx;
40                 private static const CONDITION_NE:RegExp        =/^ \s* (\w+) \s* != \s* (.+) \s* $/sx;
41                 private static const CONDITION_GT:RegExp        =/^ \s* (\w+) \s* >  \s* (.+) \s* $/sx;
42                 private static const CONDITION_GE:RegExp        =/^ \s* (\w+) \s* >= \s* (.+) \s* $/sx;
43                 private static const CONDITION_LT:RegExp        =/^ \s* (\w+) \s* <  \s* (.+) \s* $/sx;
44                 private static const CONDITION_LE:RegExp        =/^ \s* (\w+) \s* <= \s* (.+) \s* $/sx;
45                 private static const CONDITION_REGEX:RegExp     =/^ \s* (\w+) \s* =~\/ \s* (.+) \/ \s* $/sx;
46
47                 private static const ASSIGNMENT:RegExp          =/^ \s* (\S+) \s* \: \s* (.+?) \s* $/sx;
48                 private static const SET_TAG:RegExp                     =/^ \s* set \s+(\S+)\s* = \s* (.+?) \s* $/sx;
49                 private static const SET_TAG_TRUE:RegExp        =/^ \s* set \s+(\S+)\s* $/sx;
50                 private static const EXIT:RegExp                        =/^ \s* exit \s* $/isx;
51
52                 private static const oZOOM:uint=2;
53                 private static const oGROUP:uint=3;
54                 private static const oCONDITION:uint=4;
55                 private static const oOBJECT:uint=5;
56                 private static const oDECLARATION:uint=6;
57
58                 private static const DASH:RegExp=/\-/g;
59                 private static const COLOR:RegExp=/color$/;
60                 private static const BOLD:RegExp=/^bold$/i;
61                 private static const ITALIC:RegExp=/^italic|oblique$/i;
62                 private static const CAPS:RegExp=/^uppercase$/i;
63                 private static const LINE:RegExp=/^line$/i;
64
65                 private static const CSSCOLORS:Object = {
66                         aliceblue:0xf0f8ff,
67                         antiquewhite:0xfaebd7,
68                         aqua:0x00ffff,
69                         aquamarine:0x7fffd4,
70                         azure:0xf0ffff,
71                         beige:0xf5f5dc,
72                         bisque:0xffe4c4,
73                         black:0x000000,
74                         blanchedalmond:0xffebcd,
75                         blue:0x0000ff,
76                         blueviolet:0x8a2be2,
77                         brown:0xa52a2a,
78                         burlywood:0xdeb887,
79                         cadetblue:0x5f9ea0,
80                         chartreuse:0x7fff00,
81                         chocolate:0xd2691e,
82                         coral:0xff7f50,
83                         cornflowerblue:0x6495ed,
84                         cornsilk:0xfff8dc,
85                         crimson:0xdc143c,
86                         cyan:0x00ffff,
87                         darkblue:0x00008b,
88                         darkcyan:0x008b8b,
89                         darkgoldenrod:0xb8860b,
90                         darkgray:0xa9a9a9,
91                         darkgreen:0x006400,
92                         darkkhaki:0xbdb76b,
93                         darkmagenta:0x8b008b,
94                         darkolivegreen:0x556b2f,
95                         darkorange:0xff8c00,
96                         darkorchid:0x9932cc,
97                         darkred:0x8b0000,
98                         darksalmon:0xe9967a,
99                         darkseagreen:0x8fbc8f,
100                         darkslateblue:0x483d8b,
101                         darkslategray:0x2f4f4f,
102                         darkturquoise:0x00ced1,
103                         darkviolet:0x9400d3,
104                         deeppink:0xff1493,
105                         deepskyblue:0x00bfff,
106                         dimgray:0x696969,
107                         dodgerblue:0x1e90ff,
108                         firebrick:0xb22222,
109                         floralwhite:0xfffaf0,
110                         forestgreen:0x228b22,
111                         fuchsia:0xff00ff,
112                         gainsboro:0xdcdcdc,
113                         ghostwhite:0xf8f8ff,
114                         gold:0xffd700,
115                         goldenrod:0xdaa520,
116                         gray:0x808080,
117                         green:0x008000,
118                         greenyellow:0xadff2f,
119                         honeydew:0xf0fff0,
120                         hotpink:0xff69b4,
121                         indianred :0xcd5c5c,
122                         indigo :0x4b0082,
123                         ivory:0xfffff0,
124                         khaki:0xf0e68c,
125                         lavender:0xe6e6fa,
126                         lavenderblush:0xfff0f5,
127                         lawngreen:0x7cfc00,
128                         lemonchiffon:0xfffacd,
129                         lightblue:0xadd8e6,
130                         lightcoral:0xf08080,
131                         lightcyan:0xe0ffff,
132                         lightgoldenrodyellow:0xfafad2,
133                         lightgrey:0xd3d3d3,
134                         lightgreen:0x90ee90,
135                         lightpink:0xffb6c1,
136                         lightsalmon:0xffa07a,
137                         lightseagreen:0x20b2aa,
138                         lightskyblue:0x87cefa,
139                         lightslategray:0x778899,
140                         lightsteelblue:0xb0c4de,
141                         lightyellow:0xffffe0,
142                         lime:0x00ff00,
143                         limegreen:0x32cd32,
144                         linen:0xfaf0e6,
145                         magenta:0xff00ff,
146                         maroon:0x800000,
147                         mediumaquamarine:0x66cdaa,
148                         mediumblue:0x0000cd,
149                         mediumorchid:0xba55d3,
150                         mediumpurple:0x9370d8,
151                         mediumseagreen:0x3cb371,
152                         mediumslateblue:0x7b68ee,
153                         mediumspringgreen:0x00fa9a,
154                         mediumturquoise:0x48d1cc,
155                         mediumvioletred:0xc71585,
156                         midnightblue:0x191970,
157                         mintcream:0xf5fffa,
158                         mistyrose:0xffe4e1,
159                         moccasin:0xffe4b5,
160                         navajowhite:0xffdead,
161                         navy:0x000080,
162                         oldlace:0xfdf5e6,
163                         olive:0x808000,
164                         olivedrab:0x6b8e23,
165                         orange:0xffa500,
166                         orangered:0xff4500,
167                         orchid:0xda70d6,
168                         palegoldenrod:0xeee8aa,
169                         palegreen:0x98fb98,
170                         paleturquoise:0xafeeee,
171                         palevioletred:0xd87093,
172                         papayawhip:0xffefd5,
173                         peachpuff:0xffdab9,
174                         peru:0xcd853f,
175                         pink:0xffc0cb,
176                         plum:0xdda0dd,
177                         powderblue:0xb0e0e6,
178                         purple:0x800080,
179                         red:0xff0000,
180                         rosybrown:0xbc8f8f,
181                         royalblue:0x4169e1,
182                         saddlebrown:0x8b4513,
183                         salmon:0xfa8072,
184                         sandybrown:0xf4a460,
185                         seagreen:0x2e8b57,
186                         seashell:0xfff5ee,
187                         sienna:0xa0522d,
188                         silver:0xc0c0c0,
189                         skyblue:0x87ceeb,
190                         slateblue:0x6a5acd,
191                         slategray:0x708090,
192                         snow:0xfffafa,
193                         springgreen:0x00ff7f,
194                         steelblue:0x4682b4,
195                         tan:0xd2b48c,
196                         teal:0x008080,
197                         thistle:0xd8bfd8,
198                         tomato:0xff6347,
199                         turquoise:0x40e0d0,
200                         violet:0xee82ee,
201                         wheat:0xf5deb3,
202                         white:0xffffff,
203                         whitesmoke:0xf5f5f5,
204                         yellow:0xffff00,
205                         yellowgreen:0x9acd32 };
206
207
208                 public function MapCSS(m:Map) {
209                         map=m;
210                 }
211                 
212                 public function parse(css:String):Array {
213                         var previous:uint=0;                                    // what was the previous CSS word?
214                         var sc:StyleChooser=new StyleChooser(); // currently being assembled
215                         choosers=new Array();
216
217                         var o:Object=new Object();
218                         while (css.length>0) {
219
220                                 // CSS comment
221                                 if ((o=COMMENT.exec(css))) {
222                                         css=css.replace(COMMENT,'');
223
224                                 // Whitespace (probably only at beginning of file)
225                                 } else if ((o=WHITESPACE.exec(css))) {
226                                         css=css.replace(WHITESPACE,'');
227
228                                 // Class - .motorway, .builtup, :hover
229                                 } else if ((o=CLASS.exec(css))) {
230                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
231
232                                         css=css.replace(CLASS,'');
233                                         sc.addCondition(new Condition('set',o[1]));
234                                         previous=oCONDITION;
235                                         Globals.vars.root.addDebug("class: "+o[1]);
236
237                                 // Zoom
238                                 } else if ((o=ZOOM.exec(css))) {
239                                         if (previous!=oOBJECT) { sc.newObject(); }
240
241                                         css=css.replace(ZOOM,'');
242                                         var z:Array=parseZoom(o[1]);
243                                         sc.addZoom(z[0],z[1]);
244                                         Globals.vars.root.addDebug("zoom: "+o[1]+"->"+parseZoom(o[1]));
245                                         previous=oZOOM;
246
247                                 // Grouping - just a comma
248                                 } else if ((o=GROUP.exec(css))) {
249                                         css=css.replace(GROUP,'');
250                                         sc.newGroup();
251                                         Globals.vars.root.addDebug("group");
252                                         previous=oGROUP;
253
254                                 // Condition - [highway=primary]
255                                 } else if ((o=CONDITION.exec(css))) {
256                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
257                                         if (previous!=oOBJECT && previous!=oZOOM) { sc.newObject(); }
258
259                                         css=css.replace(CONDITION,'');
260                                         sc.addCondition(parseCondition(o[1]) as Condition);
261                                         Globals.vars.root.addDebug("condition: "+o[1]+'->'+parseCondition(o[1]));
262                                         previous=oCONDITION;
263
264                                 // Object - way, node, relation
265                                 } else if ((o=OBJECT.exec(css))) {
266                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
267
268                                         css=css.replace(OBJECT,'');
269                                         sc.newObject(o[1]);
270                                         Globals.vars.root.addDebug("object: "+o[1]);
271                                         previous=oOBJECT;
272
273                                 // Declaration - {...}
274                                 } else if ((o=DECLARATION.exec(css))) {
275                                         css=css.replace(DECLARATION,'');
276                                         Globals.vars.root.addDebug("declaration: "+o[1]);
277                                         sc.addStyles(parseDeclaration(o[1]));
278                                         previous=oDECLARATION;
279                                 
280                                 // Unknown pattern
281                                 } else if ((o=UNKNOWN.exec(css))) {
282                                         css=css.replace(UNKNOWN,'');
283                                         Globals.vars.root.addDebug("unknown: "+o[1]);
284                                         // ** do some debugging with o[1]
285
286                                 } else {
287                                         Globals.vars.root.addDebug("choked on "+css);
288                                         return choosers;
289                                 }
290                         }
291                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
292                         return choosers;
293                 }
294                 
295                 private function saveChooser(sc:StyleChooser):void {
296 Globals.vars.root.addDebug("+ saveChooser [rc="+sc.ruleChains[0][0]+"]");
297                         choosers.push(sc);
298                 };
299
300                 // Parse declaration string into list of styles
301
302                 private function parseDeclaration(s:String):Array {
303                         Globals.vars.root.addDebug("entering parseDeclaration with "+s); 
304                         var styles:Array=[];
305                         var t:Object=new Object();
306                         var o:Object=new Object();
307                         var a:String;
308
309                         // Create styles\10
310                         var ss:ShapeStyle =new ShapeStyle() ;
311                         var ps:PointStyle =new PointStyle() ; 
312                         var ts:TextStyle  =new TextStyle()  ; 
313                         var hs:ShieldStyle=new ShieldStyle(); 
314                         var xs:InstructionStyle=new InstructionStyle(); 
315
316                         for each (a in s.split(';')) {
317                                 if ((o=ASSIGNMENT.exec(a))) { t[o[1].replace(DASH,'_')]=o[2]; }
318                                 else if ((o=SET_TAG.exec(a))) { xs.addSetTag(o[1],o[2]); }
319                                 else if ((o=SET_TAG_TRUE.exec(a))) { xs.addSetTag(o[1],true); }
320                                 else if ((o=EXIT.exec(a))) { xs.setPropertyFromString('breaker',true); }
321                         }
322 if (xs.edited) { Globals.vars.root.addDebug("xs.set_tags is *"+xs.set_tags+"*");  }
323
324                         // Find sublayer
325                         var sub:uint=5;
326                         if (t['z_index']) { sub=Number(t['z_index']); delete t['z_index']; }
327                         ss.sublayer=ps.sublayer=ts.sublayer=hs.sublayer=sub;
328                         xs.sublayer=10;
329
330                         // Munge special values
331                         if (t['font_weight']   ) { if (t['font_weight'].match(BOLD)   ) { t['font_bold']=true;   } else { t['font_bold']=false;   } }
332                         if (t['font_style']    ) { if (t['font_style'].match(ITALIC)  ) { t['font_italic']=true; } else { t['font_italic']=false; } }
333                         if (t['text_transform']) { if (t['text_transform'].match(CAPS)) { t['font_caps']=true;   } else { t['font_caps']=false;   } }
334                         if (t['text_position'] ) { if (t['text_position'].match(LINE) ) { t['text_onpath']=true; } else { t['text_onpath']=false; } }
335
336                         // ** Do compound settings (e.g. line: 5px dotted blue;)
337
338                         // Assign each property to the appropriate style
339                         for (a in t) {
340                                 // Parse properties
341                                 // ** also do units, e.g. px/pt
342                                 // ** convert # colours to 0x numbers
343 Globals.vars.root.addDebug("looking at property *"+a+"*"); 
344                                 if (a.match(COLOR) && CSSCOLORS[t[a].toLowerCase()]) { t[a]=CSSCOLORS[t[a].toLowerCase()]; }
345                                 
346                                 // Set in styles
347                                 if      (ss.hasOwnProperty(a)) { ss.setPropertyFromString(a,t[a]); Globals.vars.root.addDebug("added "+a+" to ShapeStyle"); }
348                                 else if (ps.hasOwnProperty(a)) { ps.setPropertyFromString(a,t[a]); Globals.vars.root.addDebug("added "+a+" to PointStyle"); }
349                                 else if (ts.hasOwnProperty(a)) { ts.setPropertyFromString(a,t[a]); Globals.vars.root.addDebug("added "+a+" to TextStyle"); }
350                                 else if (hs.hasOwnProperty(a)) { hs.setPropertyFromString(a,t[a]); Globals.vars.root.addDebug("added "+a+" to ShieldStyle"); }
351                         }
352
353                         // Add each style to list
354                         if (ss.edited) { styles.push(ss); Globals.vars.root.addDebug("added ShapeStyle"); }
355                         if (ps.edited) { styles.push(ps); Globals.vars.root.addDebug("added PointStyle"); }
356                         if (ts.edited) { styles.push(ts); }
357                         if (hs.edited) { styles.push(hs); }
358                         if (xs.edited) { styles.push(xs); }
359                         return styles;
360                 }
361                 
362                 private function parseZoom(s:String):Array {
363                         var o:Object=new Object();
364                         if ((o=ZOOM_MINMAX.exec(s))) { return [o[1],o[2]]; }
365                         else if ((o=ZOOM_MIN.exec(s))) { return [o[1],map.MAXSCALE]; }
366                         else if ((o=ZOOM_MAX.exec(s))) { return [map.MINSCALE,o[1]]; }
367                         else if ((o=ZOOM_SINGLE.exec(s))) { return [o[1],o[1]]; }
368                         return null;
369                 }
370
371                 private function parseCondition(s:String):Object {
372                         var o:Object=new Object();
373                         if      ((o=CONDITION_TRUE.exec(s)))  { return new Condition('true'     ,o[1]); }
374                         else if ((o=CONDITION_FALSE.exec(s))) { return new Condition('false',o[1]); }
375                         else if ((o=CONDITION_SET.exec(s)))   { return new Condition('set'      ,o[1]); }
376                         else if ((o=CONDITION_UNSET.exec(s))) { return new Condition('unset',o[1]); }
377                         else if ((o=CONDITION_NE.exec(s)))    { return new Condition('ne'       ,o[1],o[2]); }
378                         else if ((o=CONDITION_GT.exec(s)))    { return new Condition('>'        ,o[1],o[2]); }
379                         else if ((o=CONDITION_GE.exec(s)))    { return new Condition('>='       ,o[1],o[2]); }
380                         else if ((o=CONDITION_LT.exec(s)))    { return new Condition('<'        ,o[1],o[2]); }
381                         else if ((o=CONDITION_LE.exec(s)))    { return new Condition('<='       ,o[1],o[2]); }
382                         else if ((o=CONDITION_REGEX.exec(s))) { return new Condition('regex',o[1],o[2]); }
383                         else if ((o=CONDITION_EQ.exec(s)))    { return new Condition('eq'       ,o[1],o[2]); }
384                         return null;
385                 }
386
387         }
388 }