def __init__(self): self.svg_dom = xml.dom.minidom.Document() self.svg_dom.doctype = "" svg_tag = self.svg_dom.createElement("svg") svg_tag.setAttribute("xmlns", "http://www.w3.org/2000/svg") svg_tag.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink") self.svg_dom.appendChild(svg_tag) def_tag = self.svg_dom.createElement("defs") svg_tag.appendChild(def_tag) self.svg_def = def_tag # set of required macros self.required_defs = set() self.style = CascadingStyles() self.style.appendScope() self.style["fill"] = "#fff" self.style["stroke"] = "#000000" graphic_tag = self.svg_dom.createElement("g") graphic_tag.setAttribute("style", str(self.style)) svg_tag.appendChild(graphic_tag) self.svg_current_layer = graphic_tag
def extractRTFString(s): """Extract a string and some styling info""" bracket_depth = 0 instruction = False inst_code = "" ftable = FontTable() colortable = ColorTable() # The string being generated: std_string = "" style = CascadingStyles() # Want to set these as defaults even if not specified style.appendScope() """TODO: extract styling e.g. {\rtf1\ansi\ansicpg1252\cocoartf949\cocoasubrtf460 {\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural\pardirnatural \f0\fs28 \cf0 Next ads are represented in a book-ended carousel on end screen} """ def do_instruction(inst_code, i): if inst_code == "b": style["font-weight"] = "bold" if inst_code == "ql": style["text-align"] = "left" elif inst_code == "qr": style["text-align"] = "right" elif inst_code == "qj": style["text-align"] = "justify" elif inst_code == "qc": style["text-align"] = "center" elif inst_code == "fonttbl": i = ftable.parseTable(s, i) elif inst_code == "colortbl": i = colortable.parseTable(s, i) elif inst_code[0] == "f" and inst_code[1:].isdigit(): # Font looked up in font table font = ftable.fonts.get(int(inst_code[1:]), {}) for k, v in font.items(): if k != "": style[k] = v elif inst_code[:2] == "fs" and isint(inst_code[2:]): # font size - RTF specifies half pt sizes style["font-size"] = "%.1fpx" % (float(inst_code[2:]) / 2.0) elif inst_code[:2] == "cf" and isint(inst_code[2:]): # font colour is enytry int(inst_code[2:]) in the colour table style["fill"] = "#" + colortable[int(inst_code[2:])] style["stroke"] = "#" + colortable[int(inst_code[2:])] return i i = -1 result_lines = [] while i < len(s) - 1: i += 1 c = s[i] if c == "{": bracket_depth += 1 style.appendScope() elif c == "}": if std_string != "": yield {"string": std_string, "style": str(style)} std_string = "" style.popScope() bracket_depth -= 1 elif c == "\\": if len(inst_code) > 0: i = do_instruction(inst_code, i) instruction = True inst_code = "" if instruction: if c == " ": instruction = False i = do_instruction(inst_code, i) elif c == "\n": instruction = False if inst_code == "": # new line so yield yield {"string": std_string, "style": str(style)} std_string = "" else: i = do_instruction(inst_code, i) elif not c in "\\;": inst_code += c else: if bracket_depth == 1: if not c in "{}\\\n\r": # those characters are escaped std_string += c style.popScope()
def setUp(self): self.cs = CascadingStyles({"font": "arial", "font-size": "12pt"})