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