def get_svg_attributes(node, parent_attributes={}): if parent_attributes: attributes = dict(parent_attributes) else: attributes = dict() transform = parse_transform(node) if transform and not transform.equals(Transform()): if attributes.has_key("transform"): t = Transform(attributes["transform"]) t.append(transform) attributes["transform"] = t else: attributes["transform"] = transform color_attrs = parse_color_info(node) attributes.update(color_attrs) return attributes
def parse_transform(e): """ Attempts to extract a transform="matrix()|translate()|scale()|rotate()" attribute. """ from nodebox.graphics import Transform def translate(s): a = to_number_array(s) tx = a[0] ty = 0 if len(a) > 1: ty = a[1] return Transform.translated(tx, ty) def scale(s): a = to_number_array(s) sx = a[0] sy = sx if len(a) > 1: sy = a[1] return Transform.scaled(sx, sy) def rotate(s): a = to_number_array(s) r = a[0] tx = 0 ty = 0 if len(a) > 1: tx = a[1] if len(a) > 2: ty = a[2] t = Transform() t.translate(tx, ty) t.rotate(r) t.translate(-tx, -ty) return t def matrix(s): m = to_number_array(s) return Transform(*m) types = { "translate": translate, "scale": scale, "rotate": rotate, "matrix": matrix } transforms = [] t = get_attribute(e, "transform", default="") a = get_attribute(e, "id", None) if t: v = compress_spaces(t).lstrip().rstrip() v = re.split("\s(?=[a-z])", v) for el in v: type = el.split("(")[0].lstrip().rstrip() s = el.split("(")[1].replace(")", "") transform = types[type](s) transforms.append(transform) transform = Transform() if transforms: for t in transforms: transform.append(t) return transform