def __init__(self): self.args = None self._args = ['-dEPSCrop'] self.instance = gs.new_instance() self.stdin = self.width = self.height = self.rgbbuf = self.result \ = self.buf = self.rowstride = None # need to keep a reference of this stuff around self._references = { 'stdin': gs.c_stdstream_call_t(self._gsdll_stdin), 'stdout': gs.c_stdstream_call_t(self._gsdll_stdout), 'stderr': gs.c_stdstream_call_t(self._gsdll_stderr), 'display': gs.Display_callback_s( c.c_int(c.sizeof(gs.Display_callback_s)), c.c_int(gs.DISPLAY_VERSION_MAJOR), c.c_int(gs.DISPLAY_VERSION_MINOR), gs.c_display_open(self.display_open), gs.c_display_preclose(self.display_preclose), gs.c_display_close(self.display_close), gs.c_display_presize(self.display_presize), gs.c_display_size(self.display_size), gs.c_display_sync(self.display_sync), gs.c_display_page(self.display_page), c.cast(None, gs.c_display_update), c.cast(None, gs.c_display_memalloc), # NULL, /* memalloc */ c.cast(None, gs.c_display_memfree), # NULL, /* memfree */ c.cast(None, gs.c_display_separation) ) } gs.set_stdio(self.instance, self._references['stdin'], self._references['stdout'], self._references['stderr']) gs.set_display_callback(self.instance, c.byref(self._references['display']))
def _run_ghostscript( args ) : """Run Ghostscript. We have to do a bit of stuffing around to stop Ghostscript from printing warnings to the console. This code was adapted from ghostscript's _gsprint.py. """ # allocate a new Ghostscript instance # NOTE: We only import the ghostscript stuff if it's needed (i.e. when we get here), so that people # can run this program without needing Ghostscript to be installed, if they already have a database. import ghostscript import ghostscript._gsprint as gsp inst = gsp.new_instance() # wrap stdin/stdout/stderr with dummy buffers def wrap( stdin ) : return gsp.c_stdstream_call_t( lambda inst,buf,count: 0 if stdin else count ) stdin_buf = wrap( True ) stdout_buf = wrap( False ) stderr_buf = wrap( False ) gsp.set_stdio( inst , stdin_buf , stdout_buf , stderr_buf ) try : # run Ghostscript args = [ s.encode(locale.getpreferredencoding()) for s in args ] __Ghostscript = getattr( ghostscript , "__Ghostscript" ) __Ghostscript( inst , args ) finally : # clean up gsp.delete_instance( inst ) del inst
def main(argv): code = 1 use_gui, _ = Gtk.init_check(argv) # insert display device parameters as first arguments # this controls the format of the pixbuf that ghostscript will deliver # see display_sync for details # fast CAIRO_FORMAT_RGB24 = gs.DISPLAY_COLORS_RGB | gs.DISPLAY_UNUSED_LAST | \ gs.DISPLAY_DEPTH_8 | gs.DISPLAY_LITTLEENDIAN # interesting SEPARATION_FORMAT = gs.DISPLAY_COLORS_SEPARATION | gs.DISPLAY_ALPHA_NONE | \ gs.DISPLAY_DEPTH_8 | gs.DISPLAY_BIGENDIAN # if there are spot colors they are mixed into the cmyk values CMYK_FORMAT = gs.DISPLAY_COLORS_CMYK | gs.DISPLAY_ALPHA_NONE | \ gs.DISPLAY_DEPTH_8 | gs.DISPLAY_BIGENDIAN dformat = "-dDisplayFormat=%d" % \ ( CAIRO_FORMAT_RGB24 | gs.DISPLAY_TOPFIRST ) nargv = [argv[0], dformat] + argv[1:] #run Ghostscript try: instance = gs.new_instance() gs.set_stdio(instance, gsdll_stdin, gsdll_stdout, gsdll_stderr) if use_gui: gs.set_display_callback(instance, c.byref(display)) code = gs.init_with_args(instance, nargv) if code == 0: code = gs.run_string(instance, start_string) code1 = gs.exit(instance) if code == 0 or code == gs.e_Quit: code = code1 if code == gs.e_Quit: code = 0 # user executed 'quit' gs.delete_instance(instance) except gs.GhostscriptError as e: code = e.code sys.stderr.write(e.message) finally: exit_status = 0; if code in [0, gs.e_Info, gs.e_Quit]: pass elif code == gs.e_Fatal: exit_status = 1 else: exit_status = 255 return exit_status
# # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # __author__ = "Hartmut Goebel <*****@*****.**>" __copyright__ = "Copyright 2010 by Hartmut Goebel <*****@*****.**>" __licence__ = "GNU General Public License version 3 (GPL v3)" __credits__ = "Based on an example from http://www.ghostscript.com/doc/8.63/API.htm" import sys from ghostscript import _gsprint as gs command = "1 2 add == flush\n" instance = gs.new_instance() # set_stdio() is not yet implemented #gs.set_stdio(isntance, gsdll_stdin, gsdll_stdout, gsdll_stderr) code = gs.init_with_args(instance, sys.argv) if code == 0: gs.run_string_begin(instance) gs.run_string_continue(instance, command) gs.run_string_continue(instance, "qu") gs.run_string_continue(instance, "it") gs.run_string_end(instance) code1 = gs.exit(instance) if code == 0 or code == gs.e_Quit: code = code1
# # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # __author__ = "Hartmut Goebel <*****@*****.**>" __copyright__ = "Copyright 2010 by Hartmut Goebel <*****@*****.**>" __licence__ = "GNU General Public License version 3 (GPL v3)" __credits__ = "Based on an example from http://www.ghostscript.com/doc/8.63/API.htm" import sys from ghostscript import _gsprint as gs command = "1 2 add == flush\n" instance = gs.new_instance() # set_stdio() is not yet implemented #gs.set_stdio(isntance, gsdll_stdin, gsdll_stdout, gsdll_stderr) code = gs.init_with_args(instance, sys.argv) if code == 0: gs.run_string_begin(instance); gs.run_string_continue(instance, command); gs.run_string_continue(instance, "qu"); gs.run_string_continue(instance, "it"); gs.run_string_end(instance); code1 = gs.exit(instance) if code == 0 or code == gs.e_Quit: code = code1