コード例 #1
0
ファイル: base.py プロジェクト: b-xiang/mplcairo
 def _print_ps_impl(self,
                    is_eps,
                    path_or_stream,
                    *,
                    metadata=None,
                    orientation="portrait",
                    papertype=None,
                    **kwargs):
     if papertype is None:
         papertype = mpl.rcParams["ps.papersize"]
     if orientation == "portrait":
         if papertype == "auto":
             width, height = self.figure.get_size_inches()
             papertype = backend_ps._get_papertype(height, width)
     elif orientation == "landscape":
         if papertype == "auto":
             width, height = self.figure.get_size_inches()
             papertype = backend_ps._get_papertype(width, height)
     else:
         raise ValueError(f"Invalid orientation ({orientation!r})")
     metadata = {**metadata} if metadata is not None else {}
     dsc_comments = metadata["_dsc_comments"] = [
         f"%%Orientation: {orientation}"
     ]
     if "Title" in metadata:
         dsc_comments.append("%%Title: {}".format(metadata.pop("Title")))
     if not is_eps:
         dsc_comments.append(f"%%DocumentPaperSizes: {papertype}")
     print_method = partial(
         self._print_method, GraphicsContextRendererCairo._for_eps_output
         if is_eps else GraphicsContextRendererCairo._for_ps_output)
     if mpl.rcParams["ps.usedistiller"]:
         with TemporaryDirectory() as tmp_dirname:
             tmp_name = Path(tmp_dirname, "tmp")
             print_method(tmp_name, metadata=metadata, **kwargs)
             # Assume we can get away without passing the bbox.
             {
                 "ghostscript": backend_ps.gs_distill,
                 "xpdf": backend_ps.xpdf_distill
             }[mpl.rcParams["ps.usedistiller"]](str(tmp_name),
                                                False,
                                                ptype=papertype)
             # If path_or_stream is *already* a text-mode stream then
             # tmp_name needs to be opened in text-mode too.
             with cbook.open_file_cm(path_or_stream, "wb") as stream, \
                  tmp_name.open("r" if cbook.file_requires_unicode(stream)
                                else "rb") as tmp_file:
                 shutil.copyfileobj(tmp_file, stream)
     else:
         print_method(path_or_stream, metadata=metadata, **kwargs)
コード例 #2
0
 def _print_ps_impl(self,
                    is_eps,
                    path_or_stream,
                    orientation="portrait",
                    papertype=None,
                    **kwargs):
     if papertype is None:
         papertype = mpl.rcParams["ps.papersize"]
     if orientation == "portrait":
         if papertype == "auto":
             width, height = self.figure.get_size_inches()
             papertype = backend_ps._get_papertype(height, width)
     elif orientation == "landscape":
         if papertype == "auto":
             width, height = self.figure.get_size_inches()
             papertype = backend_ps._get_papertype(width, height)
     else:
         raise ValueError("Invalid orientation ({!r})".format(orientation))
     dsc_comments = kwargs.setdefault("metadata", {})["_dsc_comments"] = [
         "%%Orientation: {}".format(orientation)
     ]
     if not is_eps:
         dsc_comments.append("%%DocumentPaperSizes: {}".format(papertype))
     print_method = partial(
         self._print_method, GraphicsContextRendererCairo._for_eps_output
         if is_eps else GraphicsContextRendererCairo._for_ps_output)
     if mpl.rcParams["ps.usedistiller"]:
         with TemporaryDirectory() as tmp_dirname:
             tmp_name = str(Path(tmp_dirname, "tmp"))
             print_method(tmp_name, **kwargs)
             # Assume we can get away without passing the bbox.
             {
                 "ghostscript": backend_ps.gs_distill,
                 "xpdf": backend_ps.xpdf_distill
             }[mpl.rcParams["ps.usedistiller"]](tmp_name,
                                                False,
                                                ptype=papertype)
             with open(tmp_name, "rb") as tmp_file, \
                     cbook.open_file_cm(path_or_stream, "wb") as stream:
                 shutil.copyfileobj(tmp_file, stream)
     else:
         print_method(path_or_stream, **kwargs)