1. Marek Kubica
  2. yuureinohebi


yuureinohebi / gsut.py

#/usr/bin/env python3
"""Ghostscript utilities toolkit, like GLut, but better!"""
from functools import partial
import libgs
import gsu

def std_stream_handler(f):
    """A decorator that wraps the stream handler functions into the apropriate
    ctypes function callback types"""
    return libgs.c_stdstream_t(f)

class GhostScriptInterpreter(object):
    """A high level interface for Ghostscript. To push words, you only have
    to call methods on the object"""
    def __init__(self):
        gs = libgs.gsapi_new_instance()
        libgs.gsapi_init_with_args(gs, [
                    # doesn't matter that much, we use the default name
                    # quiet out copyright messages and stuff
        #libgs.gsapi_set_stdio(gs, self._stdin_handler, self._stdout_handler,
        #        self._stderr_handler)
        # save for later use
        self._instance = gs

    def _stdout_handler(caller_handle, buf, length):
        print(caller_handle, buf, length)
        return 0

    _stdin_handler = _stdout_handler
    _stderr_handler = _stdout_handler

    def number(self, value):
        """Pushes a number on the stack by converting into a string"""
        return self.raw(str(value))

    def raw(self, code):
        """Runs raw PostScript code on the current instance"""
        gsu.run_code(self._instance, code)
        return self

    def __getattr__(self, name):
        """Returns a function that will push the value once called"""
        return partial(self.raw, code=name)

def main():
    gsi = GhostScriptInterpreter()

if __name__ == '__main__':