| | 113 | def save_png(self, filename, canvas): |
|---|
| | 114 | log.debug('Exporting PNG image to: %s' % filename) |
|---|
| | 115 | view = View(canvas) |
|---|
| | 116 | view.painter = ItemPainter() |
|---|
| | 117 | |
|---|
| | 118 | # Update bounding boxes with a temporaly CairoContext |
|---|
| | 119 | # (used for stuff like calculating font metrics) |
|---|
| | 120 | tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) |
|---|
| | 121 | tmpcr = cairo.Context(tmpsurface) |
|---|
| | 122 | view.update_bounding_box(tmpcr, items=canvas.get_root_items()) |
|---|
| | 123 | tmpcr.show_page() |
|---|
| | 124 | tmpsurface.flush() |
|---|
| | 125 | |
|---|
| | 126 | w, h = view.bounding_box.width, view.bounding_box.height |
|---|
| | 127 | surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w+1), int(h+1)) |
|---|
| | 128 | cr = cairo.Context(surface) |
|---|
| | 129 | view.matrix.translate(-view.bounding_box.x0, -view.bounding_box.y0) |
|---|
| | 130 | view.paint(cr) |
|---|
| | 131 | cr.show_page() |
|---|
| | 132 | surface.write_to_png(filename) |
|---|
| | 133 | |
|---|
| | 134 | def save_pdf(self, filename, canvas): |
|---|
| | 135 | log.debug('Exporting PDF image to: %s' % filename) |
|---|
| | 136 | view = View(canvas) |
|---|
| | 137 | view.painter = ItemPainter() |
|---|
| | 138 | |
|---|
| | 139 | # Update bounding boxes with a temporaly CairoContext |
|---|
| | 140 | # (used for stuff like calculating font metrics) |
|---|
| | 141 | tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) |
|---|
| | 142 | tmpcr = cairo.Context(tmpsurface) |
|---|
| | 143 | view.update_bounding_box(tmpcr) |
|---|
| | 144 | tmpcr.show_page() |
|---|
| | 145 | tmpsurface.flush() |
|---|
| | 146 | |
|---|
| | 147 | w, h = view.bounding_box.width, view.bounding_box.height |
|---|
| | 148 | surface = cairo.PDFSurface(filename, w, h) |
|---|
| | 149 | cr = cairo.Context(surface) |
|---|
| | 150 | view.matrix.translate(-view.bounding_box.x0, -view.bounding_box.y0) |
|---|
| | 151 | view.paint(cr) |
|---|
| | 152 | cr.show_page() |
|---|
| | 153 | surface.flush() |
|---|
| | 154 | surface.finish() |
|---|
| | 155 | |
|---|
| | 167 | @action(name='file-export-png', label='Export to PNG', |
|---|
| | 168 | tooltip='Export the diagram to PNG') |
|---|
| | 169 | def save_png_action(self): |
|---|
| | 170 | title = 'Export diagram to PNG' |
|---|
| | 171 | ext = '.png' |
|---|
| | 172 | diagram = self.gui_manager.main_window.get_current_diagram() |
|---|
| | 173 | filename = self.save_dialog(diagram, title, ext) |
|---|
| | 174 | if filename: |
|---|
| | 175 | self.save_png(filename, diagram.canvas) |
|---|
| | 176 | |
|---|
| | 177 | |
|---|
| | 178 | @action(name='file-export-pdf', label='Export to PDF', |
|---|
| | 179 | tooltip='Export the diagram to PDF') |
|---|
| | 180 | def save_pdf_action(self): |
|---|
| | 181 | title = 'Export diagram to PDF' |
|---|
| | 182 | ext = '.pdf' |
|---|
| | 183 | diagram = self.gui_manager.main_window.get_current_diagram() |
|---|
| | 184 | filename = self.save_dialog(diagram, title, ext) |
|---|
| | 185 | if filename: |
|---|
| | 186 | self.save_pdf(filename, diagram.canvas) |
|---|
| | 187 | |
|---|
| | 188 | |
|---|