def import_into_document(document, filename, window): """ Parse a Xournal .xoj file and add all strokes to a given document. Note that this works on existing documents and will transfer the strokes to the server, if connected. Positional Arguments: document -- A Document object filename -- The filename of the Xournal document window -- A Gtk.Window, which can be used as the parent of MessageDialogs or the like Return value: The modified Document object, that was given as an argument. """ with open_xoj(filename, "rb") as input: tree = ET.parse(input) if tree.getroot().tag != "xournal": raise Exception("Not a xournal document") pages = tree.findall("page") for p in range(len(pages)): # we ignore layers for now. Cournal uses only layer 0 strokes = pages[p].findall("layer/stroke") for s in range(len(strokes)): stroke = _parse_stroke(strokes[s], document.pages[p].layers[0]) document.pages[p].new_stroke(stroke, send_to_network=True) return document
def save_xoj_file(self, filename): """ Save the whole document as a .xoj file. Positional arguments: filename -- filename of the new .xoj file """ pagenum = 1 try: f = open_xoj(filename, "wb") except IOError as ex: print(_("Error saving document: {}").format(ex)) #FIXME: Move error handler to mainwindow.py and show error message return # Thanks to Xournals awesome XML(-not)-parsing, we can't use elementtree here. # In "Xournal World", <t a="a" b="b"> is not the same as <t b="b" a="a"> ... r = "<?xml version=\"1.0\" standalone=\"no\"?>\n" r += "<xournal version=\"0.4.6\">\n" r += "<title>Xournal document - see http://math.mit.edu/~auroux/software/xournal/</title>\n" for page in self.pages: r += "<page width=\"{}\" height=\"{}\">\n".format(round(page.width, 2), round(page.height, 2)) r += "<background type=\"pdf\"" if pagenum == 1: r += " domain=\"absolute\" filename=\"{}\"".format(self.pdfname) r += " pageno=\"{}\" />\n".format(pagenum) pagenum += 1 for layer in page.layers: r += "<layer>\n" for stroke in layer.strokes: red, g, b, opacity = stroke.color r += "<stroke tool=\"pen\" color=\"#{:02X}{:02X}{:02X}{:02X}\" width=\"{}\">\n".format(red, g, b, opacity, stroke.linewidth) first = stroke.coords[0] for coord in stroke.coords: r += " {} {}".format(coord[0], coord[1]) if len(stroke.coords) < 2: r += " {} {}".format(first[0], first[1]) r += "\n</stroke>\n" r += "</layer>\n" r += "</page>\n" r += "</xournal>" f.write(bytes(r, "UTF-8")) f.close()
def new_document(filename, window): """ Open a Xournal .xoj file Positional Arguments: filename -- The filename of the Xournal document window -- A Gtk.Window, which can be used as the parent of MessageDialogs or the like Return value: The new Document object """ with open_xoj(filename, "rb") as input: tree = ET.parse(input) pdfname = _get_background(tree) document = Document(pdfname) # We created an empty document with a PDF, now we will import the strokes: return import_into_document(document, filename, window)
r, g, b = (0, 128, 0) elif code == "gray": r, g, b = (128, 128, 128) elif code == "lightblue": r, g, b = (0, 192, 255) elif code == "lightgreen": r, g, b = (0, 255, 0) elif code == "magenta": r, g, b = (255, 0, 255) elif code == "orange": r, g, b = (255, 128, 0) elif code == "yellow": r, g, b = (255, 255, 0) elif code == "white": r, g, b = (255, 255, 255) elif re.match(regex, code): r, g, b, opacity = re.match(regex, code).groups() r = int(r, 16) g = int(g, 16) b = int(b, 16) opacity = int(opacity, 16) else: raise Exception("invalid color") return (r, g, b, opacity) # For testing purposes: if __name__ == "__main__": import sys f = open_xoj(sys.argv[1], "rb")
r, g, b = (0, 128, 0) elif code == "gray": r, g, b = (128, 128, 128) elif code == "lightblue": r, g, b = (0, 192, 255) elif code == "lightgreen": r, g, b = (0, 255, 0) elif code == "magenta": r, g, b = (255, 0, 255) elif code == "orange": r, g, b = (255, 128, 0) elif code == "yellow": r, g, b = (255, 255, 0) elif code == "white": r, g, b = (255, 255, 255) elif re.match(regex, code): r, g, b, opacity = re.match(regex, code).groups() r = int(r, 16) g = int(g, 16) b = int(b, 16) opacity = int(opacity, 16) else: raise Exception("invalid color") return (r, g, b, opacity) # For testing purposes: if __name__ == "__main__": import sys f = open_xoj(sys.argv[1], "rb") parse(None, None, f)