def save_pdf(self, filename, canvas): logger.info("Exporting to PDF") logger.debug("PDF path is %s" % filename) view = View(canvas) self.update_painters(view) # Update bounding boxes with a temporaly CairoContext # (used for stuff like calculating font metrics) tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) tmpcr = cairo.Context(tmpsurface) view.update_bounding_box(tmpcr) tmpcr.show_page() tmpsurface.flush() w, h = view.bounding_box.width, view.bounding_box.height surface = cairo.PDFSurface(filename, w, h) cr = cairo.Context(surface) view.matrix.translate(-view.bounding_box.x, -view.bounding_box.y) view.paint(cr) cr.show_page() surface.flush() surface.finish()
def test_pickle_with_view(canvas_fixture): pickled = pickle.dumps(canvas_fixture.canvas) c2 = pickle.loads(pickled) view = View(canvas=c2) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) cr = cairo.Context(surface) view.update_bounding_box(cr) cr.show_page() surface.flush() surface.finish()
def test_pickle_with_view(self): canvas = create_canvas() pickled = pickle.dumps(canvas) c2 = pickle.loads(pickled) view = View(canvas=c2) import cairo surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) cr = cairo.Context(surface) view.update_bounding_box(cr) cr.show_page() surface.flush() surface.finish()
def test_view_registration(view_fixture): canvas = Canvas() # Simple views do not register on the canvas view = View(canvas) assert len(canvas._registered_views) == 0 box = Box() canvas.add(box) # By default no complex updating/calculations are done: assert view not in box._matrix_i2v assert view not in box._matrix_v2i # GTK view does register for updates though view = GtkView(canvas) assert len(canvas._registered_views) == 1 # No entry, since GtkView is not realized and has no window assert view not in box._matrix_i2v assert view not in box._matrix_v2i window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() # Now everything is realized and updated assert view in box._matrix_i2v assert view in box._matrix_v2i view.canvas = None assert len(canvas._registered_views) == 0 assert view not in box._matrix_i2v assert view not in box._matrix_v2i view.canvas = canvas assert len(canvas._registered_views) == 1 assert view in box._matrix_i2v assert view in box._matrix_v2i
def test_view_registration(self): canvas = Canvas() # Simple views do not register on the canvas view = View(canvas) assert len(canvas._registered_views) == 0 box = Box() canvas.add(box) # By default no complex updating/calculations are done: assert not box._matrix_i2v.has_key(view) assert not box._matrix_v2i.has_key(view) # GTK view does register for updates though view = GtkView(canvas) assert len(canvas._registered_views) == 1 # No entry, since GtkView is not realized and has no window assert not box._matrix_i2v.has_key(view) assert not box._matrix_v2i.has_key(view) window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.add(view) window.show_all() # Now everything is realized and updated assert box._matrix_i2v.has_key(view) assert box._matrix_v2i.has_key(view) view.canvas = None assert len(canvas._registered_views) == 0 assert not box._matrix_i2v.has_key(view) assert not box._matrix_v2i.has_key(view) view.canvas = canvas assert len(canvas._registered_views) == 1 assert box._matrix_i2v.has_key(view) assert box._matrix_v2i.has_key(view)
def render(self, canvas, new_surface): view = View(canvas) self.update_painters(view) # Update bounding boxes with a temporary CairoContext # (used for stuff like calculating font metrics) tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) tmpcr = cairo.Context(tmpsurface) view.update_bounding_box(tmpcr) tmpcr.show_page() tmpsurface.flush() w, h = view.bounding_box.width, view.bounding_box.height surface = new_surface(w, h) cr = cairo.Context(surface) view.matrix.translate(-view.bounding_box.x, -view.bounding_box.y) paint(view, cr) cr.show_page() return surface
def save_png(self, filename, canvas): self.logger.info('Exporting to PNG') self.logger.debug('PNG path is %s' % filename) view = View(canvas) self.update_painters(view) # Update bounding boxes with a temporaly CairoContext # (used for stuff like calculating font metrics) tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) tmpcr = cairo.Context(tmpsurface) view.update_bounding_box(tmpcr) tmpcr.show_page() tmpsurface.flush() w, h = view.bounding_box.width, view.bounding_box.height surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w + 1), int(h + 1)) cr = cairo.Context(surface) view.matrix.translate(-view.bounding_box.x, -view.bounding_box.y) view.paint(cr) cr.show_page() surface.write_to_png(filename)
def save_svg(self, filename, canvas): logger.info("Exporting to SVG") logger.debug("SVG path is %s" % filename) view = View(canvas) self.update_painters(view) # Update bounding boxes with a temporary CairoContext # (used for stuff like calculating font metrics) tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) tmpcr = cairo.Context(tmpsurface) view.update_bounding_box(tmpcr) tmpcr.show_page() tmpsurface.flush() w, h = view.bounding_box.width, view.bounding_box.height surface = cairo.SVGSurface(filename, w, h) cr = cairo.Context(surface) view.matrix.translate(-view.bounding_box.x, -view.bounding_box.y) view.paint(cr) cr.show_page() surface.flush() surface.finish()
def save_png(self, filename, canvas): self.logger.info('Exporting to PNG') self.logger.debug('PNG path is %s' % filename) view = View(canvas) self.update_painters(view) # Update bounding boxes with a temporaly CairoContext # (used for stuff like calculating font metrics) tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) tmpcr = cairo.Context(tmpsurface) view.update_bounding_box(tmpcr) tmpcr.show_page() tmpsurface.flush() w, h = view.bounding_box.width, view.bounding_box.height surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w+1), int(h+1)) cr = cairo.Context(surface) view.matrix.translate(-view.bounding_box.x, -view.bounding_box.y) view.paint(cr) cr.show_page() surface.write_to_png(filename)
def __init__(self): self.canvas = Canvas() self.line = Line() self.canvas.add(self.line) self.view = View(self.canvas) self.item = Item()
if name_re and not name_re.search(pname): message("skipping %s" % pname) continue if options.dir: odir = "%s/%s" % (options.dir, odir) outfilename = "%s/%s.%s" % (odir, dname, options.format) if not os.path.exists(odir): message("creating dir %s" % odir) os.makedirs(odir) message("rendering: %s -> %s..." % (pname, outfilename)) view = View(diagram.canvas) view.painter = ItemPainter() tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) tmpcr = cairo.Context(tmpsurface) view.update_bounding_box(tmpcr) tmpcr.show_page() tmpsurface.flush() w, h = view.bounding_box.width, view.bounding_box.height if options.format == "pdf": surface = cairo.PDFSurface(outfilename, w, h) elif options.format == "svg": surface = cairo.SVGSurface(outfilename, w, h) elif options.format == "png": surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w + 1), int(h + 1))
def main(argv=sys.argv[1:]): def message(msg): """ Print message if user set verbose mode. """ if options.verbose: print(msg, file=sys.stderr) usage = "usage: %prog [options] file1 file2..." parser = optparse.OptionParser(usage=usage) parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="verbose output") parser.add_option( "-u", "--use-underscores", dest="underscores", action="store_true", help="use underscores instead of spaces for output filenames", ) parser.add_option("-d", "--dir", dest="dir", metavar="directory", help="output to directory") parser.add_option( "-f", "--format", dest="format", metavar="format", help="output file format, default pdf", default="pdf", choices=["pdf", "svg", "png"], ) parser.add_option( "-r", "--regex", dest="regex", metavar="regex", help="process diagrams which name matches given regular expresion;" " name includes package name; regular expressions are case insensitive", ) (options, args) = parser.parse_args(argv) if not args: parser.print_help() session = Session( services=["event_manager", "component_registry", "element_factory"]) factory = session.get_service("element_factory") name_re = None if options.regex: name_re = re.compile(options.regex, re.I) # we should have some gaphor files to be processed at this point for model in args: message(f"loading model {model}") storage.load(model, factory) message("ready for rendering") for diagram in factory.select(lambda e: e.isKindOf(UML.Diagram)): odir = pkg2dir(diagram.package) # just diagram name dname = diagram.name # full diagram name including package path pname = f"{odir}/{dname}" if options.underscores: odir = odir.replace(" ", "_") dname = dname.replace(" ", "_") if name_re and not name_re.search(pname): message(f"skipping {pname}") continue if options.dir: odir = f"{options.dir}/{odir}" outfilename = f"{odir}/{dname}.{options.format}" if not os.path.exists(odir): message(f"creating dir {odir}") os.makedirs(odir) message(f"rendering: {pname} -> {outfilename}...") view = View(diagram.canvas) view.painter = ItemPainter() tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) tmpcr = cairo.Context(tmpsurface) view.update_bounding_box(tmpcr) tmpcr.show_page() tmpsurface.flush() w, h = view.bounding_box.width, view.bounding_box.height if options.format == "pdf": surface = cairo.PDFSurface(outfilename, w, h) elif options.format == "svg": surface = cairo.SVGSurface(outfilename, w, h) elif options.format == "png": surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w + 1), int(h + 1)) else: assert False, f"unknown format {options.format}" cr = cairo.Context(surface) view.matrix.translate(-view.bounding_box.x, -view.bounding_box.y) paint(view, cr) cr.show_page() if options.format == "png": surface.write_to_png(outfilename) surface.flush() surface.finish()
def __init__(self): self.canvas = Canvas() self.view = View(self.canvas) self.item = Item()
def setUp(self): self.canvas = Canvas() self.line = Line() self.canvas.add(self.line) self.view = View(self.canvas)
def setUp(self): self.canvas = Canvas() self.view = View(self.canvas)
if name_re and not name_re.search(pname): message('skipping %s' % pname) continue if options.dir: odir = '%s/%s' % (options.dir, odir) outfilename = '%s/%s.%s' % (odir, dname, options.format) if not os.path.exists(odir): message('creating dir %s' % odir) os.makedirs(odir) message('rendering: %s -> %s...' % (pname, outfilename)) view = View(diagram.canvas) view.painter = ItemPainter() tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) tmpcr = cairo.Context(tmpsurface) view.update_bounding_box(tmpcr) tmpcr.show_page() tmpsurface.flush() w, h = view.bounding_box.width, view.bounding_box.height if options.format == 'pdf': surface = cairo.PDFSurface(outfilename, w, h) elif options.format == 'svg': surface = cairo.SVGSurface(outfilename, w, h) elif options.format == 'png': surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w + 1),