Merge branch 'localfile'
[potlatch2.git] / net / systemeD / halcyon / styleparser / RuleSet.as
1 package net.systemeD.halcyon.styleparser {
2
3         import flash.events.*;
4         import flash.net.*;
5     import net.systemeD.halcyon.connection.Entity;
6     import net.systemeD.halcyon.FileBank;
7
8     import net.systemeD.halcyon.connection.*;
9
10     /**
11     * A complete stylesheet, as loaded from a MapCSS file. It contains all selectors, declarations,
12     * and embedded images.
13     *
14     * <p>The RuleSet class has two principal methods: getStyles, which calculates the styles that apply
15     * to an entity (returned as a StyleList); and parse, which parses a MapCSS stylesheet into
16     * a complete RuleSet.</p>
17     */
18
19         public class RuleSet {
20
21                 public var loaded:Boolean=false;                        // is the RuleSet fully loaded and available for use?
22                 private var redrawCallback:Function=null;       // function to call when CSS loaded
23                 private var iconCallback:Function=null;         // function to call when all icons loaded
24                 private var iconsToLoad:uint=0;                         // number of icons left to load (fire iconCallback when ==0)
25                 private var evalsToLoad:uint=0;                         // number of evals left to load (fire redrawCallback when ==0)
26
27                 private var minscale:uint;
28                 private var maxscale:uint;
29                 public var choosers:Array;
30                 public var evals:Array;
31
32                 private static const WHITESPACE:RegExp  =/^ \s+ /sx;
33                 private static const COMMENT:RegExp             =/^ \/\* .+? \*\/ \s* /sx;      /* */
34                 private static const CLASS:RegExp               =/^ ([\.:]\w+) \s* /sx;
35                 private static const NOT_CLASS:RegExp   =/^ !([\.:]\w+) \s* /sx;
36                 private static const ZOOM:RegExp                =/^ \| \s* z([\d\-]+) \s* /isx;
37                 private static const GROUP:RegExp               =/^ , \s* /isx;
38                 private static const CONDITION:RegExp   =/^ \[(.+?)\] \s* /sx;
39                 private static const OBJECT:RegExp              =/^ (\w+) \s* /sx;
40                 private static const DECLARATION:RegExp =/^ \{(.+?)\} \s* /sx;
41                 private static const SUBPART:RegExp             =/^ ::(\w+) \s* /sx;
42                 private static const UNKNOWN:RegExp             =/^ (\S+) \s* /sx;
43
44                 private static const ZOOM_MINMAX:RegExp =/^ (\d+)\-(\d+) $/sx;
45                 private static const ZOOM_MIN:RegExp    =/^ (\d+)\-      $/sx;
46                 private static const ZOOM_MAX:RegExp    =/^      \-(\d+) $/sx;
47                 private static const ZOOM_SINGLE:RegExp =/^        (\d+) $/sx;
48
49                 private static const CONDITION_TRUE:RegExp      =/^ \s* ([:\w]+) \s* = \s* yes \s*  $/isx;
50                 private static const CONDITION_FALSE:RegExp     =/^ \s* ([:\w]+) \s* = \s* no  \s*  $/isx;
51                 private static const CONDITION_SET:RegExp       =/^ \s* ([:\w]+) \s* $/sx;
52                 private static const CONDITION_UNSET:RegExp     =/^ \s* !([:\w]+) \s* $/sx;
53                 private static const CONDITION_EQ:RegExp        =/^ \s* ([:\w]+) \s* =  \s* (.+) \s* $/sx;
54                 private static const CONDITION_NE:RegExp        =/^ \s* ([:\w]+) \s* != \s* (.+) \s* $/sx;
55                 private static const CONDITION_GT:RegExp        =/^ \s* ([:\w]+) \s* >  \s* (.+) \s* $/sx;
56                 private static const CONDITION_GE:RegExp        =/^ \s* ([:\w]+) \s* >= \s* (.+) \s* $/sx;
57                 private static const CONDITION_LT:RegExp        =/^ \s* ([:\w]+) \s* <  \s* (.+) \s* $/sx;
58                 private static const CONDITION_LE:RegExp        =/^ \s* ([:\w]+) \s* <= \s* (.+) \s* $/sx;
59                 private static const CONDITION_REGEX:RegExp     =/^ \s* ([:\w]+) \s* =~\/ \s* (.+) \/ \s* $/sx;
60
61                 private static const ASSIGNMENT_EVAL:RegExp             =/^ \s* (\S+) \s* \:      \s* eval \s* \( \s* ' (.+?) ' \s* \) \s* $/isx;
62                 private static const ASSIGNMENT_TAGVALUE:RegExp =/^ \s* (\S+) \s* \:      \s* tag  \s* \( \s* ' (.+?) ' \s* \) \s* $/isx;
63                 private static const ASSIGNMENT:RegExp                  =/^ \s* (\S+) \s* \:      \s*          (.+?) \s*                   $/sx;
64                 private static const SET_TAG_EVAL:RegExp                =/^ \s* set \s+(\S+)\s* = \s* eval \s* \( \s* ' (.+?) ' \s* \) \s* $/isx;
65                 private static const SET_TAG_TAGVALUE:RegExp    =/^ \s* set \s+(\S+)\s* = \s* tag  \s* \( \s* ' (.+?) ' \s* \) \s* $/isx;
66                 private static const SET_TAG:RegExp                             =/^ \s* set \s+(\S+)\s* = \s*          (.+?) \s*                   $/isx;
67                 private static const SET_TAG_TRUE:RegExp                =/^ \s* set \s+(\S+)\s* $/isx;
68                 private static const DELETE_TAG:RegExp                  =/^ \s* delete \s+(\S+)\s* $/isx;
69                 private static const EXIT:RegExp                                =/^ \s* exit \s* $/isx;
70
71                 private static const oZOOM:uint=2;
72                 private static const oGROUP:uint=3;
73                 private static const oCONDITION:uint=4;
74                 private static const oOBJECT:uint=5;
75                 private static const oDECLARATION:uint=6;
76                 private static const oSUBPART:uint=7;
77
78                 private static const DASH:RegExp=/\-/g;
79                 private static const COLOR:RegExp=/color$/;
80                 private static const BOLD:RegExp=/^bold$/i;
81                 private static const ITALIC:RegExp=/^italic|oblique$/i;
82                 private static const UNDERLINE:RegExp=/^underline$/i;
83                 private static const CAPS:RegExp=/^uppercase$/i;
84                 private static const CENTER:RegExp=/^center$/i;
85                 private static const FALSE:RegExp=/^(no|false|0)$/i;
86
87                 private static const HEX:RegExp=/^#([0-9a-f]+)$/i;
88                 private static const CSSCOLORS:Object = {
89                         aliceblue:0xf0f8ff,
90                         antiquewhite:0xfaebd7,
91                         aqua:0x00ffff,
92                         aquamarine:0x7fffd4,
93                         azure:0xf0ffff,
94                         beige:0xf5f5dc,
95                         bisque:0xffe4c4,
96                         black:0x000000,
97                         blanchedalmond:0xffebcd,
98                         blue:0x0000ff,
99                         blueviolet:0x8a2be2,
100                         brown:0xa52a2a,
101                         burlywood:0xdeb887,
102                         cadetblue:0x5f9ea0,
103                         chartreuse:0x7fff00,
104                         chocolate:0xd2691e,
105                         coral:0xff7f50,
106                         cornflowerblue:0x6495ed,
107                         cornsilk:0xfff8dc,
108                         crimson:0xdc143c,
109                         cyan:0x00ffff,
110                         darkblue:0x00008b,
111                         darkcyan:0x008b8b,
112                         darkgoldenrod:0xb8860b,
113                         darkgray:0xa9a9a9,
114                         darkgreen:0x006400,
115                         darkkhaki:0xbdb76b,
116                         darkmagenta:0x8b008b,
117                         darkolivegreen:0x556b2f,
118                         darkorange:0xff8c00,
119                         darkorchid:0x9932cc,
120                         darkred:0x8b0000,
121                         darksalmon:0xe9967a,
122                         darkseagreen:0x8fbc8f,
123                         darkslateblue:0x483d8b,
124                         darkslategray:0x2f4f4f,
125                         darkturquoise:0x00ced1,
126                         darkviolet:0x9400d3,
127                         deeppink:0xff1493,
128                         deepskyblue:0x00bfff,
129                         dimgray:0x696969,
130                         dodgerblue:0x1e90ff,
131                         firebrick:0xb22222,
132                         floralwhite:0xfffaf0,
133                         forestgreen:0x228b22,
134                         fuchsia:0xff00ff,
135                         gainsboro:0xdcdcdc,
136                         ghostwhite:0xf8f8ff,
137                         gold:0xffd700,
138                         goldenrod:0xdaa520,
139                         gray:0x808080,
140                         green:0x008000,
141                         greenyellow:0xadff2f,
142                         honeydew:0xf0fff0,
143                         hotpink:0xff69b4,
144                         indianred :0xcd5c5c,
145                         indigo :0x4b0082,
146                         ivory:0xfffff0,
147                         khaki:0xf0e68c,
148                         lavender:0xe6e6fa,
149                         lavenderblush:0xfff0f5,
150                         lawngreen:0x7cfc00,
151                         lemonchiffon:0xfffacd,
152                         lightblue:0xadd8e6,
153                         lightcoral:0xf08080,
154                         lightcyan:0xe0ffff,
155                         lightgoldenrodyellow:0xfafad2,
156                         lightgrey:0xd3d3d3,
157                         lightgreen:0x90ee90,
158                         lightpink:0xffb6c1,
159                         lightsalmon:0xffa07a,
160                         lightseagreen:0x20b2aa,
161                         lightskyblue:0x87cefa,
162                         lightslategray:0x778899,
163                         lightsteelblue:0xb0c4de,
164                         lightyellow:0xffffe0,
165                         lime:0x00ff00,
166                         limegreen:0x32cd32,
167                         linen:0xfaf0e6,
168                         magenta:0xff00ff,
169                         maroon:0x800000,
170                         mediumaquamarine:0x66cdaa,
171                         mediumblue:0x0000cd,
172                         mediumorchid:0xba55d3,
173                         mediumpurple:0x9370d8,
174                         mediumseagreen:0x3cb371,
175                         mediumslateblue:0x7b68ee,
176                         mediumspringgreen:0x00fa9a,
177                         mediumturquoise:0x48d1cc,
178                         mediumvioletred:0xc71585,
179                         midnightblue:0x191970,
180                         mintcream:0xf5fffa,
181                         mistyrose:0xffe4e1,
182                         moccasin:0xffe4b5,
183                         navajowhite:0xffdead,
184                         navy:0x000080,
185                         oldlace:0xfdf5e6,
186                         olive:0x808000,
187                         olivedrab:0x6b8e23,
188                         orange:0xffa500,
189                         orangered:0xff4500,
190                         orchid:0xda70d6,
191                         palegoldenrod:0xeee8aa,
192                         palegreen:0x98fb98,
193                         paleturquoise:0xafeeee,
194                         palevioletred:0xd87093,
195                         papayawhip:0xffefd5,
196                         peachpuff:0xffdab9,
197                         peru:0xcd853f,
198                         pink:0xffc0cb,
199                         plum:0xdda0dd,
200                         powderblue:0xb0e0e6,
201                         purple:0x800080,
202                         red:0xff0000,
203                         rosybrown:0xbc8f8f,
204                         royalblue:0x4169e1,
205                         saddlebrown:0x8b4513,
206                         salmon:0xfa8072,
207                         sandybrown:0xf4a460,
208                         seagreen:0x2e8b57,
209                         seashell:0xfff5ee,
210                         sienna:0xa0522d,
211                         silver:0xc0c0c0,
212                         skyblue:0x87ceeb,
213                         slateblue:0x6a5acd,
214                         slategray:0x708090,
215                         snow:0xfffafa,
216                         springgreen:0x00ff7f,
217                         steelblue:0x4682b4,
218                         tan:0xd2b48c,
219                         teal:0x008080,
220                         thistle:0xd8bfd8,
221                         tomato:0xff6347,
222                         turquoise:0x40e0d0,
223                         violet:0xee82ee,
224                         wheat:0xf5deb3,
225                         white:0xffffff,
226                         whitesmoke:0xf5f5f5,
227                         yellow:0xffff00,
228                         yellowgreen:0x9acd32 };
229
230                 /** Constructor */
231
232                 public function RuleSet(mins:uint,maxs:uint,redrawCall:Function=null,iconLoadedCallback:Function=null):void {
233                         minscale = mins;
234                         maxscale = maxs;
235                         redrawCallback = redrawCall;
236                         iconCallback = iconLoadedCallback;
237                 }
238
239                 /** Create a StyleList for an Entity, by creating a blank StyleList, then running each StyleChooser over it.
240                     Optionally, styleUntagged can be set to false, to abort (and return a blank StyleList) if the tag hash is empty.
241                         @see net.systemeD.halcyon.styleparser.StyleList */
242
243                 public function getStyles(obj:Entity, tags:Object, zoom:uint, styleUntagged:Boolean=true):StyleList {
244                         var sl:StyleList=new StyleList();
245                         var tagged:Boolean=styleUntagged;
246                         for (var k:String in tags) { tagged=true; break; }
247                         if (tagged) {
248                                 for each (var sc:StyleChooser in choosers) {
249                                         sc.updateStyles(obj,tags,sl,zoom);
250                                 }
251                         }
252                         return sl;
253                 }
254
255                 /** Run instruction styles only, for CSSTransform. */
256                 public function runInstructions(obj:Entity, tags:Object):Object {
257                         for each (var sc:StyleChooser in choosers) {
258                                 tags=sc.runInstructions(obj,tags);
259                         }
260                         return tags;
261                 }
262
263                 // ---------------------------------------------------------------------------------------------------------
264                 // Loading stylesheet
265
266         /** Load and then parse a MapCSS stylesheet. Usually you will supply a filename, but you can also pass 
267                         a complete stylesheet in the string parameter. (Any string containing space characters will be 
268                         assumed to be a stylesheet rather than a filename.) */
269
270                 public function loadFromCSS(str:String):void {
271                         if (str.match(/[\s\n\r\t]/)!=null) { parseCSS(str); loaded=true; redrawCallback(); return; }
272
273                         var cssLoader:NestedCSSLoader=new NestedCSSLoader();
274                         cssLoader.addEventListener(Event.COMPLETE, doParseCSS);
275                         cssLoader.load(str);
276                 }
277
278                 private function doParseCSS(e:Event):void {
279                         parseCSS(e.target.css);
280                 }
281
282                 private function parseCSS(str:String):void {
283                         parse(str);
284                         loaded=true;
285                         if (evals.length==0) { redrawCallback(); }
286                         loadImages();
287                 }
288
289
290                 /// ------------------------------------------------------------------------------------------------
291                 /** Load all images referenced in the RuleSet (for example, icons or bitmap fills). */
292                 
293                 private function loadImages():void {
294                         var filename:String;
295                         for each (var chooser:StyleChooser in choosers) {
296                                 for each (var style:Style in chooser.styles) {
297                                         if      (style is PointStyle  && PointStyle(style).icon_image   ) { filename=PointStyle(style).icon_image; }
298                                         else if (style is ShapeStyle  && ShapeStyle(style).fill_image   ) { filename=ShapeStyle(style).fill_image; }
299                                         else if (style is ShieldStyle && ShieldStyle(style).shield_image) { filename=ShieldStyle(style).shield_image; }
300                                         else { continue; }
301
302                                         if (filename!='square' && filename!='circle')
303                                                 FileBank.getInstance().addFromFile(filename);
304                                 }
305                         }
306             FileBank.getInstance().onFilesLoaded(iconCallback);
307                 }
308                 
309                 // ------------------------------------------------------------------------------------------------
310                 // Parse CSS
311
312                 /** Parse a MapCSS stylesheet into a set of StyleChoosers. The parser is regular expression-based 
313                         and runs sequentially through the file from start to end. */
314                 public function parse(css:String):void {
315                         var previous:uint=0;                                    // what was the previous CSS word?
316                         var sc:StyleChooser=new StyleChooser(); // currently being assembled
317                         choosers=new Array();
318                         evals=new Array();
319
320                         var o:Object=new Object();
321                         while (css.length>0) {
322
323                                 // CSS comment
324                                 if ((o=COMMENT.exec(css))) {
325                                         css=css.replace(COMMENT,'');
326
327                                 // Whitespace (probably only at beginning of file)
328                                 } else if ((o=WHITESPACE.exec(css))) {
329                                         css=css.replace(WHITESPACE,'');
330
331                                 // Class - .motorway, .builtup, :hover
332                                 } else if ((o=CLASS.exec(css))) {
333                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
334
335                                         css=css.replace(CLASS,'');
336                                         sc.currentChain.addConditionToLast(new Condition('set',o[1]));
337                                         previous=oCONDITION;
338
339                                 // Not class - !.motorway, !.builtup, !:hover
340                                 } else if ((o=NOT_CLASS.exec(css))) {
341                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
342
343                                         css=css.replace(NOT_CLASS,'');
344                                         sc.currentChain.addConditionToLast(new Condition('unset',o[1]));
345                                         previous=oCONDITION;
346
347                                 // Zoom
348                                 } else if ((o=ZOOM.exec(css))) {
349                                         if (previous!=oOBJECT && previous!=oCONDITION) { sc.currentChain.addRule(); }
350
351                                         css=css.replace(ZOOM,'');
352                                         var z:Array=parseZoom(o[1]);
353                                         sc.currentChain.addZoomToLast(z[0],z[1]);
354                                         sc.zoomSpecific=true;
355                                         previous=oZOOM;
356
357                                 // Grouping - just a comma
358                                 } else if ((o=GROUP.exec(css))) {
359                                         css=css.replace(GROUP,'');
360                                         sc.newRuleChain();
361                                         previous=oGROUP;
362
363                                 // Condition - [highway=primary]
364                                 } else if ((o=CONDITION.exec(css))) {
365                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
366                                         if (previous!=oOBJECT && previous!=oZOOM && previous!=oCONDITION) { sc.currentChain.addRule(); }
367                                         css=css.replace(CONDITION,'');
368                                         sc.currentChain.addConditionToLast(parseCondition(o[1]) as Condition);
369                                         previous=oCONDITION;
370
371                                 // Object - way, node, relation
372                                 } else if ((o=OBJECT.exec(css))) {
373                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
374
375                                         css=css.replace(OBJECT,'');
376                                         sc.currentChain.addRule(o[1]);
377                                         previous=oOBJECT;
378
379                                 // Subpart - ::centreline
380                                 } else if ((o=SUBPART.exec(css))) {
381                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
382                                         css=css.replace(SUBPART,'');
383                                         sc.currentChain.setSubpart(o[1]);
384                                         previous=oSUBPART;
385
386                                 // Declaration - {...}
387                                 } else if ((o=DECLARATION.exec(css))) {
388                                         css=css.replace(DECLARATION,'');
389                                         sc.addStyles(parseDeclaration(o[1]));
390                                         previous=oDECLARATION;
391                                 
392                                 // Unknown pattern
393                                 } else if ((o=UNKNOWN.exec(css))) {
394                                         css=css.replace(UNKNOWN,'');
395                                         trace("unknown: "+o[1]);
396                                         // ** do some debugging with o[1]
397
398                                 } else {
399                                         trace("choked on "+css);
400                                         return;
401                                 }
402                         }
403                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
404                 }
405                 
406                 private function saveChooser(sc:StyleChooser):void {
407                         choosers.push(sc);
408                 };
409                 
410                 private function saveEval(expr:String):Eval {
411                         evalsToLoad++;
412                         var e:Eval=new Eval(expr);
413                         e.addEventListener("swf_loaded",evalLoaded, false, 0, true);
414                         evals.push(e);
415                         return e;
416                 }
417                 
418                 private function evalLoaded(e:Event):void {
419                         evalsToLoad--;
420                         if (evalsToLoad==0) { redrawCallback(); }
421                 }
422
423                 // Parse declaration string into list of styles
424
425                 private function parseDeclaration(s:String):Array {
426                         var styles:Array=[];
427                         var t:Object=new Object();
428                         var o:Object=new Object();
429                         var a:String, k:String, v:*;
430
431                         // Create styles\10
432                         var ss:ShapeStyle =new ShapeStyle() ;
433                         var ps:PointStyle =new PointStyle() ; 
434                         var ts:TextStyle  =new TextStyle()  ; 
435                         var hs:ShieldStyle=new ShieldStyle(); 
436                         var xs:InstructionStyle=new InstructionStyle(); 
437
438                         for each (a in s.split(';')) {
439                                 if      ((o=ASSIGNMENT_EVAL.exec(a)))           { t[o[1].replace(DASH,'_')]=saveEval(o[2]); }
440                                 else if ((o=ASSIGNMENT_TAGVALUE.exec(a)))       { t[o[1].replace(DASH,'_')]=new TagValue(o[2]); }
441                                 else if ((o=ASSIGNMENT.exec(a)))                        { t[o[1].replace(DASH,'_')]=o[2]; }
442                                 else if ((o=SET_TAG_EVAL.exec(a)))                      { xs.addSetTag(o[1],saveEval(o[2])); }
443                                 else if ((o=SET_TAG_TAGVALUE.exec(a)))          { xs.addSetTag(o[1],new TagValue(o[2])); }
444                                 else if ((o=SET_TAG.exec(a)))                           { xs.addSetTag(o[1],o[2]); }
445                                 else if ((o=SET_TAG_TRUE.exec(a)))                      { xs.addSetTag(o[1],true); }
446                                 else if ((o=DELETE_TAG.exec(a)))                        { xs.addSetTag(o[1],''); }
447                                 else if ((o=EXIT.exec(a))) { xs.setPropertyFromString('breaker',true); }
448                         }
449
450                         // Find sublayer
451                         var sub:uint=5;
452                         if (t['z_index']) { sub=Number(t['z_index']); delete t['z_index']; }
453                         ss.sublayer=ps.sublayer=ts.sublayer=hs.sublayer=sub;
454                         xs.sublayer=10;
455                         
456                         // Find "interactive" property - it's true unless explicitly set false.
457                         var inter:Boolean=true;
458                         if (t['interactive']) { inter=t['interactive'].match(FALSE) ? false : true; delete t['interactive']; }
459                         ss.interactive=ps.interactive=ts.interactive=hs.interactive=xs.interactive=inter;
460
461                         // Munge special values
462                         if (t['font_weight']    ) { t['font_bold'  ]    = t['font_weight'    ].match(BOLD  )    ? true : false; delete t['font_weight']; }
463                         if (t['font_style']     ) { t['font_italic']    = t['font_style'     ].match(ITALIC)    ? true : false; delete t['font_style']; }
464                         if (t['text_decoration']) { t['font_underline'] = t['text_decoration'].match(UNDERLINE) ? true : false; delete t['text_decoration']; }
465                         if (t['text_position']  ) { t['text_center']    = t['text_position'  ].match(CENTER)    ? true : false; delete t['text_position']; }
466                         if (t['text_transform']) {
467                                 // ** needs other transformations, e.g. lower-case, sentence-case
468                                 if (t['text_transform'].match(CAPS)) { t['font_caps']=true; } else { t['font_caps']=false; }
469                                 delete t['text_transform'];
470                         }
471
472                         // ** Do compound settings (e.g. line: 5px dotted blue;)
473
474                         // Assign each property to the appropriate style
475                         for (a in t) {
476                                 // Parse properties
477                                 // ** also do units, e.g. px/pt/m
478                                 if (a.match(COLOR)) { v = parseCSSColor(t[a]); }
479                                                else { v = t[a]; }
480                                 
481                                 // Set in styles
482                                 if      (ss.hasOwnProperty(a)) { ss.setPropertyFromString(a,v); }
483                                 else if (ps.hasOwnProperty(a)) { ps.setPropertyFromString(a,v); }
484                                 else if (ts.hasOwnProperty(a)) { ts.setPropertyFromString(a,v); }
485                                 else if (hs.hasOwnProperty(a)) { hs.setPropertyFromString(a,v); }
486                         }
487
488                         // Add each style to list
489                         if (ss.edited) { styles.push(ss); }
490                         if (ps.edited) { styles.push(ps); }
491                         if (ts.edited) { styles.push(ts); }
492                         if (hs.edited) { styles.push(hs); }
493                         if (xs.edited) { styles.push(xs); }
494                         return styles;
495                 }
496                 
497                 private function parseZoom(s:String):Array {
498                         var o:Object=new Object();
499                         if ((o=ZOOM_MINMAX.exec(s))) { return [o[1],o[2]]; }
500                         else if ((o=ZOOM_MIN.exec(s))) { return [o[1],maxscale]; }
501                         else if ((o=ZOOM_MAX.exec(s))) { return [minscale,o[1]]; }
502                         else if ((o=ZOOM_SINGLE.exec(s))) { return [o[1],o[1]]; }
503                         return null;
504                 }
505
506                 private function parseCondition(s:String):Object {
507                         var o:Object=new Object();
508                         if      ((o=CONDITION_TRUE.exec(s)))  { return new Condition('true'     ,o[1]); }
509                         else if ((o=CONDITION_FALSE.exec(s))) { return new Condition('false',o[1]); }
510                         else if ((o=CONDITION_SET.exec(s)))   { return new Condition('set'      ,o[1]); }
511                         else if ((o=CONDITION_UNSET.exec(s))) { return new Condition('unset',o[1]); }
512                         else if ((o=CONDITION_NE.exec(s)))    { return new Condition('ne'       ,o[1],o[2]); }
513                         else if ((o=CONDITION_GT.exec(s)))    { return new Condition('>'        ,o[1],o[2]); }
514                         else if ((o=CONDITION_GE.exec(s)))    { return new Condition('>='       ,o[1],o[2]); }
515                         else if ((o=CONDITION_LT.exec(s)))    { return new Condition('<'        ,o[1],o[2]); }
516                         else if ((o=CONDITION_LE.exec(s)))    { return new Condition('<='       ,o[1],o[2]); }
517                         else if ((o=CONDITION_REGEX.exec(s))) { return new Condition('regex',o[1],o[2]); }
518                         else if ((o=CONDITION_EQ.exec(s)))    { return new Condition('eq'       ,o[1],o[2]); }
519                         return null;
520                 }
521
522         /** Convert a colour string from CSS colour name ("blue"), short hex ("#abc") or long hex ("#a0b0c0"), 
523                         to an integer. 
524                         
525                 @default 0 */
526
527         public static function parseCSSColor(colorStr:String):uint {
528             colorStr = colorStr.toLowerCase();
529             if (CSSCOLORS[colorStr] != undefined) {
530                 return CSSCOLORS[colorStr];
531             } else {
532                 var match:Object = HEX.exec(colorStr);
533                 if ( match ) { 
534                   if ( match[1].length == 3) {
535                     // repeat digits. #abc => 0xaabbcc
536                     return Number("0x"+match[1].charAt(0)+match[1].charAt(0)+
537                                        match[1].charAt(1)+match[1].charAt(1)+
538                                        match[1].charAt(2)+match[1].charAt(2));
539                   } else if ( match[1].length == 6) {
540                     return Number("0x"+match[1]);
541                   } else {
542                     return Number("0x000000"); //as good as any
543                   }
544                 }
545             }
546             return 0;
547         }
548         }
549 }