def test_element_with_attributes(): xml = Xml() xml.element("test") xml.set("x", 10) xml.set("abc-xyz", 20) xml.close() assert xml.to_string() == "<test x='10' abc-xyz='20' />"
def get_text_size_query_key(self, style, styles, text): xml = Xml() xml.element("svg") render_text(xml, 0, 0, text, style, styles, id="t1") xml.close() self.svg = xml.to_string() self.style = style return ("textsize", self.svg)
def test_element_child_with_text(): xml = Xml() xml.element("test") xml.element("a") xml.text("<not-a-tag>") xml.close() xml.text("abc") xml.element("a") xml.text("xyz") xml.close() xml.text("123") xml.close() assert xml.to_string() == \ ("<test>" "<a>" "<not-a-tag></a>" "abc<a>" "xyz</a>" "123</test>")
class RendererSVG: def __init__(self): self.xml = None def begin(self, width, height): self.width = width self.height = height self.xml = Xml() self.xml.element("svg") self.xml.set("xmlns", "http://www.w3.org/2000/svg") self.xml.set("width", width) self.xml.set("height", height) def end(self): self.xml.close("svg") def draw_text(self, x, y, parsed_text, style, styles): render_text(self.xml, x, y, parsed_text, style, styles) def draw_rect(self, rect, fill_color=None, color=None, stroke_width=None, rx=None, ry=None): xml = self.xml xml.element("rect") xml.set("x", rect.x) xml.set("y", rect.y) if rx is not None: xml.set("rx", rx) if ry is not None: xml.set("ry", ry) xml.set("width", rect.width) xml.set("height", rect.height) style = [] if fill_color is None and color is None: color = "black" if fill_color is not None: style.append("fill:" + str(fill_color)) if color is not None: style.append("stroke:" + str(color)) if stroke_width is not None: style.append("stoke-width:" + str(stroke_width)) xml.set("style", ";".join(style)) xml.close() def write(self, filename): self.xml.write(filename) def to_string(self): return self.xml.to_string() def get_text_size_query_key(self, style, styles, text): xml = Xml() xml.element("svg") render_text(xml, 0, 0, text, style, styles, id="t1") xml.close() self.svg = xml.to_string() self.style = style return ("textsize", self.svg) def get_text_size_query(self, style, styles, text): key = self.get_text_size_query_key(style, styles, text) svg = key[1] def compute(): width = float(run_inkscape(("--query-id=t1", "-W"), None, svg)) line_height = style.size * style.line_spacing lines = 1 for (token, value) in text: if token == "newline": lines += value height = lines * line_height return width, height return key, compute def draw_image(self, svgstring, x, y, scale=1.0): self.xml.element("g") transform = ["translate({}, {})".format(x, y)] if scale != 1.0 and scale is not None: transform.append("scale({})".format(scale)) self.xml.set("transform", " ".join(transform)) self.xml.raw_text(svgstring) self.xml.close() def get_image_size(self, filename, scale): root = et.parse(filename).getroot() width = float(root.get("width")) height = float(root.get("height")) return width, height
def test_empty_element(): xml = Xml() xml.element("test") xml.close() assert xml.to_string() == "<test />"