(self, fmt, fobj, *, orientation='portrait')
| 469 | return surface |
| 470 | |
| 471 | def _save(self, fmt, fobj, *, orientation='portrait'): |
| 472 | # save PDF/PS/SVG |
| 473 | |
| 474 | dpi = 72 |
| 475 | self.figure.dpi = dpi |
| 476 | w_in, h_in = self.figure.get_size_inches() |
| 477 | width_in_points, height_in_points = w_in * dpi, h_in * dpi |
| 478 | |
| 479 | if orientation == 'landscape': |
| 480 | width_in_points, height_in_points = ( |
| 481 | height_in_points, width_in_points) |
| 482 | |
| 483 | if fmt == 'ps': |
| 484 | if not hasattr(cairo, 'PSSurface'): |
| 485 | raise RuntimeError('cairo has not been compiled with PS ' |
| 486 | 'support enabled') |
| 487 | surface = cairo.PSSurface(fobj, width_in_points, height_in_points) |
| 488 | elif fmt == 'pdf': |
| 489 | if not hasattr(cairo, 'PDFSurface'): |
| 490 | raise RuntimeError('cairo has not been compiled with PDF ' |
| 491 | 'support enabled') |
| 492 | surface = cairo.PDFSurface(fobj, width_in_points, height_in_points) |
| 493 | elif fmt in ('svg', 'svgz'): |
| 494 | if not hasattr(cairo, 'SVGSurface'): |
| 495 | raise RuntimeError('cairo has not been compiled with SVG ' |
| 496 | 'support enabled') |
| 497 | if fmt == 'svgz': |
| 498 | if isinstance(fobj, str): |
| 499 | fobj = gzip.GzipFile(fobj, 'wb') |
| 500 | else: |
| 501 | fobj = gzip.GzipFile(None, 'wb', fileobj=fobj) |
| 502 | surface = cairo.SVGSurface(fobj, width_in_points, height_in_points) |
| 503 | else: |
| 504 | raise ValueError(f"Unknown format: {fmt!r}") |
| 505 | |
| 506 | self._renderer.dpi = self.figure.dpi |
| 507 | self._renderer.set_context(cairo.Context(surface)) |
| 508 | ctx = self._renderer.gc.ctx |
| 509 | |
| 510 | if orientation == 'landscape': |
| 511 | ctx.rotate(np.pi / 2) |
| 512 | ctx.translate(0, -height_in_points) |
| 513 | # Perhaps add an '%%Orientation: Landscape' comment? |
| 514 | |
| 515 | self.figure.draw(self._renderer) |
| 516 | |
| 517 | ctx.show_page() |
| 518 | surface.finish() |
| 519 | if fmt == 'svgz': |
| 520 | fobj.close() |
| 521 | |
| 522 | print_pdf = functools.partialmethod(_save, "pdf") |
| 523 | print_ps = functools.partialmethod(_save, "ps") |
nothing calls this directly
no test coverage detected