From 9339297c29cfbf47fc1ca12ab586b1ee22fd8c26 Mon Sep 17 00:00:00 2001 From: Darafei Praliaskouski Date: Thu, 24 Oct 2013 13:52:38 +0300 Subject: [PATCH] pack backgrounds into textures --- src/libkomwm.py | 35 +++++++++++++++++++------- src/texture_packer.py | 57 +++++++++++++++++++++++++++++++------------ 2 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/libkomwm.py b/src/libkomwm.py index e133356..eab0f0a 100644 --- a/src/libkomwm.py +++ b/src/libkomwm.py @@ -20,8 +20,6 @@ def komap_mapswithme(options, style, filename): basepath = os.path.dirname(filename) drules = ContainerProto() - visibility_file = open(os.path.join(ddir, 'visibility.txt'), "w") - classificator_file = open(os.path.join(ddir, 'classificator.txt'), "w") types_file = open(os.path.join(ddir, 'types.txt'), "w") drules_bin = open(os.path.join(options.outfile + '.bin'), "wb") drules_txt = open(os.path.join(options.outfile + '.txt'), "wb") @@ -56,6 +54,7 @@ def komap_mapswithme(options, style, filename): print >> types_file, "mapswithme" class_tree[row[0].replace("|", "-")] = row[0] class_order.sort() + types_file.close() def mwm_encode_color(st, prefix='', default='black'): if prefix: @@ -64,24 +63,38 @@ def komap_mapswithme(options, style, filename): color = whatever_to_hex(st.get(prefix + 'color', default)) color = color[1] + color[1] + color[3] + color[3] + color[5] + color[5] return int(opacity + color, 16) - - def mwm_encode_image(st, prefix='icon'): + + def mwm_encode_image(st, prefix='icon', bgprefix='symbol'): if prefix: prefix += "-" + if bgprefix: + bgprefix += "-" if prefix + "image" not in st: return False icon = { "file": os.path.join(basepath,st.get(prefix + "image")), "fill-color": "", "color": "", - "width": st.get(prefix + "width", ""), - "height": st.get(prefix + "height", ""), + "symbol-image": "", + "symbol-file": "", + "symbol-fill-color": "", + "symbol-color": "", + #"width": st.get(prefix + "width", ""), + #"height": st.get(prefix + "height", ""), } + if st.get(prefix + "color"): icon["color"] = whatever_to_hex(st.get(prefix + "color")) if st.get(prefix + "fill-color"): icon["fill-color"] = whatever_to_hex(st.get(prefix + "fill-color")) - handle = ":".join([str(i) for i in [st.get(prefix + "image"), icon["fill-color"], icon["color"], icon["width"], icon["height"]]]) + if st.get(bgprefix + "image"): + icon["symbol-image"] = st.get(bgprefix + "image") + icon["symbol-file"] = os.path.join(basepath,st.get(bgprefix + "image")) + if st.get(bgprefix + "color"): + icon["symbol-color"] = whatever_to_hex(st.get(bgprefix + "color")) + if st.get(bgprefix + "fill-color"): + icon["symbol-fill-color"] = whatever_to_hex(st.get(bgprefix + "fill-color")) + handle = ":".join([str(i) for i in [st.get(prefix + "image"), icon["fill-color"], icon["color"], icon["symbol-image"], icon["symbol-fill-color"], icon["symbol-color"]]]) return handle, icon bgpos = 0 @@ -100,6 +113,7 @@ def komap_mapswithme(options, style, filename): txclass["addr:housenumber"] = "addr:housenumber" txclass["ref"] = "ref" txclass["int_name"] = "int_name" + txclass["addr:flats"] = "addr:flats" has_icons_for_areas = False zstyle = {} @@ -112,7 +126,7 @@ def komap_mapswithme(options, style, filename): if True: areastyle = style.get_style_dict("area", txclass, zoom, olddict=zstyle, cache=False) for st in areastyle.values(): - if "icon-image" in st or 'symbol-shape' in st: + if "icon-image" in st or 'symbol-shape' in st or 'symbol-image' in st: has_icons_for_areas = True zstyle = areastyle @@ -131,7 +145,7 @@ def komap_mapswithme(options, style, filename): st = dict([(k, v) for k, v in st.iteritems() if str(v).strip(" 0.")]) if 'width' in st or 'pattern-image' in st: has_lines = True - if 'icon-image' in st or 'symbol-shape' in st: + if 'icon-image' in st or 'symbol-shape' in st or 'symbol-image' in st: has_icons = True if 'fill-color' in st: has_fills = True @@ -283,6 +297,9 @@ def komap_mapswithme(options, style, filename): return -1 viskeys.sort(cmprepl) + visibility_file = open(os.path.join(ddir, 'visibility.txt'), "w") + classificator_file = open(os.path.join(ddir, 'classificator.txt'), "w") + oldoffset = "" for k in viskeys: offset = " " * (k.count("|") - 1) diff --git a/src/texture_packer.py b/src/texture_packer.py index abcba20..f0c3aaa 100644 --- a/src/texture_packer.py +++ b/src/texture_packer.py @@ -11,6 +11,7 @@ from xml.dom import minidom def open_svg_as_image(icon, multiplier = 1.0, max_height = None): fn = icon["file"] + original_multiplier = multiplier max_height = max_height * multiplier maki_resize = [ (18, 0.75, 12, 1), @@ -30,30 +31,57 @@ def open_svg_as_image(icon, multiplier = 1.0, max_height = None): multiplier = dstmul break - dom = minidom.parse(fn) + icon_dom = minidom.parse(fn) if icon.get("fill-color"): - [a.setAttribute("fill", icon["fill-color"]) for a in dom.getElementsByTagName("path") if a.getAttribute("fill")] - [a.setAttribute("fill", icon["fill-color"]) for a in dom.getElementsByTagName("g") if a.getAttribute("fill")] + [a.setAttribute("fill", icon["fill-color"]) for a in icon_dom.getElementsByTagName("path") if a.getAttribute("fill")] + [a.setAttribute("fill", icon["fill-color"]) for a in icon_dom.getElementsByTagName("g") if a.getAttribute("fill")] + [a.setAttribute("fill", icon["fill-color"]) for a in icon_dom.getElementsByTagName("rect") if a.getAttribute("fill") not in ("none", "")] if icon.get("color"): - [a.setAttribute("stroke", icon["color"]) for a in dom.getElementsByTagName("path") if a.getAttribute("stroke")] - [a.setAttribute("stroke", icon["color"]) for a in dom.getElementsByTagName("g") if a.getAttribute("stroke")] + [a.setAttribute("stroke", icon["color"]) for a in icon_dom.getElementsByTagName("path") if a.getAttribute("stroke")] + [a.setAttribute("stroke", icon["color"]) for a in icon_dom.getElementsByTagName("g") if a.getAttribute("stroke")] + [a.setAttribute("stroke", icon["color"]) for a in icon_dom.getElementsByTagName("rect") if a.getAttribute("stroke") not in ("none", "")] tmpfile = StringIO.StringIO() outfile = StringIO.StringIO() - #svg = rsvg.Handle(file=fn) - svg = rsvg.Handle(data=dom.toxml()) + svg = rsvg.Handle(data=icon_dom.toxml()) svgwidth = float(svg.get_property('width')) svgheight = float(svg.get_property('height')) - resheight = svgheight * multiplier + iconheight = svgheight * multiplier if max_height: - resheight = min(resheight, max_height) - reswidth = svgwidth * resheight / svgheight + iconheight = min(iconheight, max_height) + iconwidth = svgwidth * iconheight / svgheight + + reswidth, resheight = iconwidth, iconheight + + if icon.get("symbol-file"): + bg_dom = minidom.parse(icon["symbol-file"]) + if icon.get("symbol-fill-color"): + [a.setAttribute("fill", icon["symbol-fill-color"]) for a in bg_dom.getElementsByTagName("path") if a.getAttribute("fill")] + [a.setAttribute("fill", icon["symbol-fill-color"]) for a in bg_dom.getElementsByTagName("g") if a.getAttribute("fill")] + [a.setAttribute("fill", icon["symbol-fill-color"]) for a in bg_dom.getElementsByTagName("rect") if a.getAttribute("fill") not in ("none", "")] + if icon.get("symbol-color"): + [a.setAttribute("stroke", icon["symbol-color"]) for a in bg_dom.getElementsByTagName("path") if a.getAttribute("stroke")] + [a.setAttribute("stroke", icon["symbol-color"]) for a in bg_dom.getElementsByTagName("g") if a.getAttribute("stroke")] + [a.setAttribute("stroke", icon["symbol-color"]) for a in bg_dom.getElementsByTagName("rect") if a.getAttribute("stroke") not in ("none", "")] + bg_svg = rsvg.Handle(data=bg_dom.toxml()) + bg_width = float(bg_svg.get_property('width')) + bg_height = float(bg_svg.get_property('height')) + reswidth = max(bg_width * original_multiplier, reswidth) + resheight = max(bg_height * original_multiplier, resheight) + svgsurface = cairo.SVGSurface(outfile, reswidth, resheight) svgctx = cairo.Context(svgsurface) - #size = max(24, svgwidth, svgheight) - print fn, svgwidth / reswidth, svgheight / resheight - svgctx.scale(reswidth / svgwidth, resheight / svgheight) + + if icon.get("symbol-file"): + svgctx.save() + svgctx.scale(original_multiplier, original_multiplier) + bg_svg.render_cairo(svgctx) + svgctx.restore() + svgctx.translate((reswidth - iconwidth) / 2., (resheight - iconheight) / 2.) + + svgctx.scale(iconwidth / svgwidth, iconheight / svgheight) svg.render_cairo(svgctx) + svgsurface.write_to_png(tmpfile) svgsurface.finish() tmpfile.seek(0) @@ -65,7 +93,6 @@ def open_svg_as_image(icon, multiplier = 1.0, max_height = None): im = im.crop(bbox) return im - def pack_texture(icons=[], multiplier = 1.0, path = "", rasfilter = []): images = {} strips = [] @@ -110,7 +137,7 @@ def pack_texture(icons=[], multiplier = 1.0, path = "", rasfilter = []): dx += images[img].size[0] dy += strip["height"] dx = 0 - pprint.pprint(strips) + #pprint.pprint(strips) print >>skin, """ """