-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmapcss_converter.py
executable file
·599 lines (508 loc) · 17.5 KB
/
mapcss_converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#!/usr/bin/python
# Copyright (c) 2011-2013, Darafei Praliaskouski, Vladimir Agafonkin, Maksim Gurtovenko
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
import os
import re
from PIL import Image
import json
try:
import cairo
except ImportError:
import cairocffi as cairo
import tempfile
import io
try:
import rsvg
FoundSVG = True
except ImportError:
try:
from gi.repository import Rsvg
FoundSVG = True
except ImportError:
FoundSVG = False
from mapcss_parser import MapCSSParser
from mapcss_parser import ast
from mapcss_parser import error
from collections import deque
class flag_stack(deque):
def value(self):
return self.count(False) == 0
# operators when comparing to numbers
# do not check for type equality here as the left hand part will come from the tags
# array and is a string
CHECK_OPERATORS_NUM = {
'=': '==',
'<': '<',
'<=': '<=',
'>': '>',
'>=': '>=',
'!=': '!=',
'<>': '!='
}
# operators when comparing to strings
CHECK_OPERATORS = {
'=': '===',
'<': '<',
'<=': '<=',
'>': '>',
'>=': '>=',
'!=': '!==',
'<>': '!=='
}
DASH_PROPERTIES = ('dashes', 'casing-dashes')
NUMERIC_PROPERTIES = (
'z-index',
'width',
'opacity',
'fill-opacity',
'casing-width',
'casing-opacity',
'font-size',
'icon-height',
'icon-opacity',
'icon-width',
'shield-frame-width',
'shield-casing-width',
'shield-opacity',
'text-offset',
'text-offset-x',
'text-offset-y',
'max-width',
'text-halo-radius'
)
images = set()
subparts = set(['default'])
presence_tags = set()
value_tags = dict()
tag_function = 'e_tag' # to which function tag() resolves, may change e.g. to e_localize()
tag_enable = flag_stack()
def add_vtag(k, v = None):
# use None here as a marker for wildcard usage
# treat regular expressions as wildcards as there are usually too many possible outcomes
st = value_tags.get(k, set())
if v and v[0] == '/':
st.add(None)
else:
st.add(v)
value_tags[k] = st
def open_svg_as_image(fn):
tmpfd, tmppath = tempfile.mkstemp(".png")
tmpfile = os.fdopen(tmpfd,'w')
file = StringIO.StringIO()
svg = rsvg.Handle(file=fn)
svgwidth = svg.get_property('width')
svgheight = svg.get_property('height')
svgsurface = cairo.SVGSurface(file, svgwidth, svgheight)
svgctx = cairo.Context(svgsurface)
#size = max(24, svgwidth, svgheight)
#svgctx.scale(size/float(svgwidth),height/float(svgheight))
svg.render_cairo(svgctx)
svgsurface.write_to_png(tmpfile)
tmpfile.close()
svgsurface.finish()
im = Image.open(tmppath)
os.remove(tmppath)
return im
def wrap_key(key):
return "'%s'" % key
def propagate_import(url):
content = open(url).read()
return parser.parse(content)
def escape_value(key, value, subpart):
if isinstance(value, ast.Eval):
return value.as_js(subpart)
elif key in NUMERIC_PROPERTIES:
if float(value) % 1 != 0.0:
return float(value)
else:
return int(float(value))
elif key in DASH_PROPERTIES:
return "[%s]" % ', '.join(value.split(','))
else:
return "'%s'" % value
def mapcss_as_js(self):
imports = "".join(map(lambda imp: propagate_import(imp.url).as_js(), self.imports))
rules = "".join(map(lambda x: x.as_js(), self.rules))
return "%s%s" % (imports, rules)
def rule_as_js(self):
if not tag_enable.value():
return ""
selectors_js = []
actions_js = []
for selector in self.selectors:
selectors_js.append("(%s%s)" % (selector.get_zoom(), selector.as_js()))
for action in self.actions:
actions_js.append(action.as_js(selector.subpart))
return """\n if (%s) %s""" % (" || \n ".join(selectors_js), "".join(actions_js))
def selector_as_js(self):
if not tag_enable.value():
return ""
if self.subject in ['line', 'way']:
sel = 'isWay'
elif self.subject == 'node':
sel = 'isNode'
else:
if self.subject in ['relation', 'coastline']:
subject_property = 'type'
else:
subject_property = 'selector'
sel = "%s == %s" % (subject_property, wrap_key(self.subject))
#TODO: something > something is not supported yet
if self.within_selector:
return 'false'
if self.criteria:
criteria = " && ".join(map(lambda x: x.as_js(), self.criteria))
return "%s && %s" % (sel, criteria)
else:
return sel
def isNumeric(value):
try:
float(value)
return True
except ValueError:
return False
def condition_check_as_js(self):
k = wrap_key(self.key).strip("'\"")
v = wrap_key(self.value).strip("'\"")
add_vtag(k, v)
if self.sign == '=~':
return "%s.test(tags['%s'])" % (v, k)
elif self.sign == '!~':
return "!(%s.test(tags['%s']))" % (v, k)
elif self.sign == '~=':
return "MapCSS.e_tag(tags, '%s').split(';').indexOf('%s') >= 0" % (k, v)
elif isNumeric(v):
return "tags['%s'] %s %s" % (k, CHECK_OPERATORS_NUM[self.sign], v)
else:
return "tags['%s'] %s '%s'" % (k, CHECK_OPERATORS[self.sign], v)
def condition_tag_as_js(self):
presence_tags.add(wrap_key(self.key).strip("'\""))
return "tags.hasOwnProperty('%s')" % (wrap_key(self.key).strip("'\""))
def condition_nottag_as_js(self):
presence_tags.add(wrap_key(self.key).strip("'\""))
return "!tags.hasOwnProperty('%s')" % (wrap_key(self.key).strip("'\""))
def condition_pseudoclass_as_js(self):
#TODO: Not supported yet
return "true"
def condition_class_as_js(self):
return "cssClasses.indexOf('%s') >= 0" % self.name;
def class_statement_as_js(self):
return " if (cssClasses.indexOf('%s') < 0) { cssClasses.push('%s'); }" % (self.name, self.name);
def action_as_js(self, subpart):
if not tag_enable.value():
return "{}"
lines = list()
# only add subpart to the list if it needs any special handling
firstSub = (subpart != "default")
for statement in self.statements:
if isinstance(statement, ast.StyleStatement):
if firstSub:
if subpart == '*':
subpart = 'everything'
subpart = re.sub("-", "_", subpart)
subparts.add(subpart)
firstSub = False
lines.append(statement.as_js(subpart))
else:
lines.append(statement.as_js())
return """{
%s
}\n""" % "\n".join(lines)
def style_statement_as_js(self, subpart):
global tag_function
old_tag_f = tag_function
if self.key == 'text' and isinstance(self.value, ast.Eval):
tag_function = 'e_localize';
val = escape_value(self.key, self.value, subpart)
tag_function = old_tag_f
k = wrap_key(self.key)
if self.key == 'text' and not isinstance(self.value, ast.Eval):
if (self.value == ''):
return " s_%s[%s] = '';" % (subpart, k)
else:
add_vtag(self.value)
return " s_%s[%s] = MapCSS.e_localize(tags, %s);" % (subpart, k, val)
else:
if not isinstance(self.value, ast.Eval) and self.key in ('icon-image', 'fill-image'):
images.add(self.value)
return " s_%s[%s] = %s;" % (subpart, k, val)
def tag_statement_as_js(self, subpart):
k = wrap_key(self.key).strip("'\"")
return " tags['%s'] = '%s'" % (k, escape_value(self.key, self.value.strip("'\""), subpart))
def eval_as_js(self, subpart):
return self.expression.as_js(subpart)
# returns the expression in parentheses if it is an expression or direct if it is just a string
def str_or_expr(val, subpart):
if isinstance(val, ast.EvalExpressionString):
return val.as_js(subpart);
else:
return "(%s)" % val.as_js(subpart)
def eval_function_as_js(self, subpart):
if self.function != 'cond':
args = ", ".join(list(map(lambda arg: arg.as_js(subpart), self.arguments)))
global tag_function
if self.function == 'tag':
if (args == '""'):
return "''"
else:
add_vtag(args.strip("'\""))
return "MapCSS.%s(tags, %s)" % (tag_function, args)
elif self.function == 'prop':
if (args == '""'):
return "''"
else:
return "MapCSS.e_prop(s_%s, %s)" % (subpart, args)
elif self.function == 'cond':
if len(self.arguments) == 3:
old_tag_f = tag_function
# do not localize the condition, but the outcome
tag_function = "e_tag"
check = self.arguments[0].as_js(subpart)
tag_function = "e_localize"
v1 = str_or_expr(self.arguments[1], subpart);
v2 = str_or_expr(self.arguments[2], subpart);
tag_function = old_tag_f
return "(%s ? %s : %s)" % (check, v1, v2)
else:
print("exactly 3 arguments to cond() expected, but got %i" % len(self.arguments))
sys.exit(1)
else:
return "MapCSS.e_%s(%s)" % (self.function, args)
def eval_string_as_js(self, subpart):
return str(self)
def eval_op_as_js(self, subpart):
op = self.operation
if op == '.':
op = '+'
elif op == 'eq':
op = '=='
elif op == 'ne':
op = '!='
return "%s %s %s" % (self.arg1.as_js(subpart), self.operation, self.arg2.as_js(subpart))
def eval_group_as_js(self, subpart):
return "(%s)" % str(self.expression.as_js(subpart))
def selector_get_zoom(self):
zoom = self.zoom
zoom = zoom.strip("|")
if zoom and zoom[0] == 'z':
zoom = zoom[1:].split('-')
if len(zoom) == 1:
return 'zoom === %d && ' % int(zoom[0])
cond = ''
if zoom[0]:
cond += 'zoom >= %d && ' % int(zoom[0])
if zoom[1]:
cond += 'zoom <= %d && ' % int(zoom[1])
return cond
return ''
def create_css_sprite(image_names, icons_path, sprite_filename):
sprite_images = []
external_images = []
image_width = []
image_height = []
for fname in sorted(image_names):
fpath = os.path.join(icons_path, fname)
if not os.path.isfile(fpath):
external_images.append(fname)
continue
if '.svg' in fpath:
if FoundSVG:
image = open_svg_as_image(fpath)
else:
print("SVG image support has not been found, needed for image %s" % (fpath))
raise SystemExit(1)
else:
image = Image.open(fpath)
sprite_images.append({
'name': fname,
'size': image.size,
'image': image,
})
image_width.append(image.size[0])
image_height.append(image.size[1])
if not sprite_images:
return (sprite_images, external_images)
sprite_size = (max(image_width), sum(image_height))
sprite = Image.new(
mode='RGBA',
size=sprite_size,
color=(0,0,0,0))
offset = 0
for data in sprite_images:
data['offset'] = offset
sprite.paste(data['image'], (0, offset))
offset += data['size'][1]
sprite.save(sprite_filename)
return (sprite_images, external_images)
def image_as_js(image):
return """
'%s': {
width: %d,
height: %d,
offset: %d
}""" % (
image['name'],
image['size'][0],
image['size'][1],
image['offset']
)
def supports_as_js(self):
tag_enable.append(self.value())
return ""
def supports_end_as_js(supports):
tag_enable.pop()
return ""
ast.MapCSS.as_js = mapcss_as_js
ast.Rule.as_js = rule_as_js
ast.Selector.as_js = selector_as_js
ast.Selector.get_zoom = selector_get_zoom
ast.ConditionCheck.as_js = condition_check_as_js
ast.ConditionTag.as_js = condition_tag_as_js
ast.ConditionNotTag.as_js = condition_nottag_as_js
ast.ConditionPseudoclass.as_js = condition_pseudoclass_as_js
ast.ConditionClass.as_js = condition_class_as_js
ast.Action.as_js = action_as_js
ast.StyleStatement.as_js = style_statement_as_js
ast.TagStatement.as_js = tag_statement_as_js
ast.ClassStatement.as_js = class_statement_as_js
ast.Eval.as_js = eval_as_js
ast.EvalExpressionString.as_js = eval_string_as_js
ast.EvalExpressionOperation.as_js = eval_op_as_js
ast.EvalExpressionGroup.as_js = eval_group_as_js
ast.EvalFunction.as_js = eval_function_as_js
ast.Supports.as_js = supports_as_js
ast.SupportsEnd.as_js = supports_end_as_js
def dump_ptags(filename, ptags, vtags):
taglist = list()
for t in ptags:
foo = dict()
foo['key'] = t;
taglist.append(foo)
for t in vtags.keys():
vals = vtags[t]
if not None in vals:
for v in vals:
foo = dict()
foo['key'] = t;
foo['value'] = v
taglist.append(foo)
else:
foo = dict()
foo['key'] = t;
taglist.append(foo)
jsonfile = open(filename, "w")
json.dump(taglist, jsonfile, indent=4, sort_keys=True)
jsonfile.close()
if __name__ == "__main__":
from optparse import OptionParser
parser = OptionParser(usage="%prog [options]")
parser.add_option("-i", "--mapcss",
dest="input",
help="MapCSS input file, required")
parser.add_option("-n", "--name",
dest="name",
help="MapCSS style name, optional")
parser.add_option("-o", "--output",
dest="output",
help="JS output file. If not specified [stylename].js will be used")
parser.add_option("-p", "--icons-path",
dest="icons", default=".",
help="Directory with the icon set used in MapCSS file")
parser.add_option("-s", "--output-sprite",
dest="sprite",
help="Filename of generated CSS sprite. If not specified, [stylename].png will be used")
parser.add_option("-t", "--output-taginfo",
dest="taginfo",
help="Dump information about used keys and values in taginfo format. If not specified, no output will be generated")
(options, args) = parser.parse_args()
if not options.input:
print("--mapcss parameter is required")
raise SystemExit(1)
if options.name:
style_name = options.name
else:
style_name = re.sub("\..*", "", options.input)
content = open(options.input).read()
parser = MapCSSParser(debug=False)
try:
mapcss = parser.parse(content)
except error.MapCSSError as error:
print(error)
exit(1)
mapcss_js = mapcss.as_js()
subparts_var = ", ".join(map(lambda subpart: "s_%s = {}" % subpart, subparts))
subparts_var = " var %s;" % subparts_var
subparts_fill = "\n".join(map(lambda subpart: " if (Object.keys(s_%s).length) {\n style['%s'] = s_%s; }" % (subpart, subpart, subpart), subparts))
js = """
(function (MapCSS) {
'use strict';
function restyle(style, tags, zoom, type, selector) {
var cssClasses = [],
isNode = (type === 'node'), isWay = (type === 'way');
%s
%s
%s
return style;
}
""" % (subparts_var, mapcss_js, subparts_fill)
if options.sprite:
sprite = options.sprite
else:
sprite = "%s.png" % style_name
if options.output:
output = options.output
else:
output = "%s.js" % style_name
(sprite_images, external_images) = create_css_sprite(images, options.icons, sprite)
#We don't need to check presence if we already check value
presence_tags -= set(value_tags.keys())
ptags = ""
if presence_tags:
ptags = "'%s'" % "', '".join(sorted(presence_tags))
vtags = ""
if value_tags:
vtags = "'%s'" % "', '".join(sorted(value_tags))
js += """
var sprite_images = {%s};
var external_images = [%s];
var presence_tags = [%s];
var value_tags = [%s];
MapCSS.loadStyle('%s', restyle, sprite_images, external_images, presence_tags, value_tags);
MapCSS.preloadExternalImages('%s');
})(MapCSS);
""" % (
",".join(map(image_as_js, sprite_images)),
", ".join(map(lambda i: "'%s'" % i, external_images)),
ptags,
vtags,
style_name,
style_name)
if output == "-":
sys.stdout.write(js)
else:
with open(output, "w") as fh:
fh.write(js)
if options.taginfo:
dump_ptags(options.taginfo, presence_tags, value_tags)