From c10f4c73e48da4f10be75634bdfd87e7ad2012ed Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20Wobst?= Date: Wed, 24 May 2006 06:30:57 +0000 Subject: [PATCH] add a pipeGS method to directly pass the PyX output to ghostscript git-svn-id: https://pyx.svn.sourceforge.net/svnroot/pyx/trunk/pyx@2803 069f4177-920e-0410-937b-c2a4a81bcd90 --- CHANGES | 1 + manual/canvas.tex | 33 +++++++++++++++++++++++++++++++++ pyx/canvas.py | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/CHANGES b/CHANGES index 46c97d95..994d7bb7 100644 --- a/CHANGES +++ b/CHANGES @@ -176,6 +176,7 @@ TODO: - remove set method of canvas - canvas and document modules: - allow file instances as paramter of the writeXXXfile methods (feature request #1419658 by Jason Pratt) + - add a pipeGS method to directly pass the PyX output to ghostscript - style module: - make rellength the default for dash styles - random notes: diff --git a/manual/canvas.tex b/manual/canvas.tex index a8db14a3..44df70bb 100644 --- a/manual/canvas.tex +++ b/manual/canvas.tex @@ -108,6 +108,39 @@ document. the given arguments \var{arg} and \var{kwargs}. \end{methoddesc} +\begin{methoddesc}{pipeGS}{filename="-", device=None, resolution=100, + gscommand="gs", gsoptions="", + textalphabits=4, graphicsalphabits=4, **kwargs} + This method pipes the content of a canvas to the ghostscript + interpreter directly to generate other output formats. At least + \var{filename} or \var{device} must be set. \var{filename} specifies + the bame of the output file. No file extension will be added to that + name in any case. When no \var{filename} is specified, the output is + written to stdout. \var{device} specifies a ghostscript output + device by a string. Depending on your ghostscript configuration + \code{"png16"}, \code{"png16m"}, \code{"png256"}, \code{"png48"}, + \code{"pngalpha"}, \code{"pnggray"}, \code{"pngmono"}, + \code{"jpeg"}, and \code{"jpeggray"} might be available among + others. See the output of \texttt{gs --help} and the ghostscript + documentation for more information. When \var{filename} is specified + but the device is not set, \code{"png16m"} is used when the filename + ends in \texttt{.eps} and \code{"jpeg"} is used when the filename + ends in \texttt{.jpg}. + + \var{resolution} specifies the resolution in dpi (dots per inch). + \var{gscmd} is the command to be used to invoke ghostscript. + \var{gsoptions} are an option string passed to the ghostscript + interpreter. \var{textalphabits} are \var{graphicsalphabits} are + conventient parameters to set the \texttt{TextAlphaBits} and + \texttt{GraphicsAlphaBits} options of ghostscript. You can skip + the addition of those option by set their value to \code{None}. + + \var{kwargs} are passed to the \method{writeEPSfile} method (not + counting the \var{file} parameter), which is used to generate the + input for ghostscript. By that you gain access to the + \class{document.page} constructor arguments. +\end{methoddesc} + For more information about the possible arguments of the \class{document.page} constructor, we refer to Sect.~\ref{document}. diff --git a/pyx/canvas.py b/pyx/canvas.py index a44a08f1..0c27b540 100644 --- a/pyx/canvas.py +++ b/pyx/canvas.py @@ -31,6 +31,7 @@ displayed. """ # from __future__ import nested_scopes +import os class canvasitem: @@ -329,6 +330,7 @@ def _wrappedindocument(method): return method(d, file) return wrappedindocument + class canvas(_canvas): """a canvas holds a collection of canvasitems""" @@ -337,3 +339,23 @@ class canvas(_canvas): writePSfile = _wrappedindocument(document.document.writePSfile) writePDFfile = _wrappedindocument(document.document.writePDFfile) writetofile = _wrappedindocument(document.document.writetofile) + + def pipeGS(self, filename="-", device=None, resolution=100, + gscommand="gs", gsoptions="", + textalphabits=4, graphicsalphabits=4, + **kwargs): + if device is None: + if filename.endswith(".png"): + device = "png16m" + if filename.endswith(".jpg"): + device = "jpeg" + gscommand += " -dEPSCrop -dNOPAUSE -dQUIET -dBATCH -r%i -sDEVICE=%s -sOutputFile=%s" % (resolution, device, filename) + if gsoptions: + gscommand += " %s" % gsoptions + if textalphabits is not None: + gscommand += " -dTextAlphaBits=%i" % textalphabits + if graphicsalphabits is not None: + gscommand += " -dGraphicsAlphaBits=%i" % graphicsalphabits + gscommand += " -" + input = os.popen(gscommand, "w") + self.writeEPSfile(input, **kwargs) -- 2.11.4.GIT