def _add_page(doc, frame): """Adds the Faint frame (and all objects) as a page to the document""" frame_size = frame.get_size() page_width, page_height = frame_size scale_x = frame_size[0] / page_width scale_y = frame_size[1] / page_height stream = Stream() meta = [] if not ifaint.one_color_bg(frame): bmp = frame.get_bitmap() w, h = bmp.get_size() bg_name = doc.add_xobject(XObject(bmp.get_raw_rgb_string(), w, h)) stream.xobject(bg_name, 0, 0, page_width, page_height) for obj in frame.get_objects(): obj_meta = object_to_stream(stream, obj, page_height, scale_x, scale_y) meta.extend(obj_meta) page_id = doc.add_page(page_width, page_height) doc.add_comment("faint meta-data") for entry in meta: doc.add_comment(entry) doc.add_comment("end of faint meta-data") doc.add_stream(stream, page_id)
def write(path, canvas, prettyprint=False, embedRaster=False): global numRast numRast = 0 domImpl = xml.dom.getDOMImplementation() docType = domImpl.createDocumentType( "svg", "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd") doc = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", docType) size = canvas.get_size() svg = doc.getElementsByTagName("svg")[0] svg.setAttributeNode(createAttr(doc, "width", str(size[0]))) svg.setAttributeNode(createAttr(doc, "height", str(size[1]))) # Why must I do this for namespaces?? svg.setAttributeNode(createAttr(doc, "xmlns", "http://www.w3.org/2000/svg")) svg.setAttributeNode( createAttr(doc, "xmlns:xlink", "http://www.w3.org/1999/xlink")) svg.setAttributeNode( createAttr(doc, "xmlns:faint", "http://www.code.google.com/p/faint-graphics-editor")) svg.setAttributeNode( createAttr(doc, "xmlns:svg", "http://www.w3.org/2000/svg")) svg.appendChild(createDefsNode(doc)) if embedRaster: creators["Raster"] = createRasterNodeEmbed else: creators["Raster"] = createRasterNode # Save the background image if it is not single colored if not ifaint.one_color_bg(canvas): svg.appendChild(createBackgroundNode(doc, canvas)) else: # Fixme: If the image is uniform, it could either mean the # background color should be used for the SVG, or the # background should be transparent. Use transparent bg for now # (for transparency, add a way to remove the background layer) pass objects = canvas.get_objects() try: for object in objects: func = creators.get(object.get_type(), createOther) svg.appendChild(func(doc, object)) if path is None: print doc.toprettyxml() else: svgFile = open(path, 'w') if prettyprint: svgFile.write(doc.toprettyxml(encoding="utf-8")) else: svgFile.write(doc.toxml(encoding="utf-8")) except Exception, e: print e raise e
def create_background(image): """Creates a background color or image.""" return (create_background_color(image) if ifaint.one_color_bg( image.get_frame()) else create_background_image(image))