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