From 4aaab2f4d20878cc970da32cec3ddbbbd06981f6 Mon Sep 17 00:00:00 2001 From: Alex Foulon Date: Thu, 11 Nov 2010 08:46:17 +0000 Subject: [PATCH] * Bind figfont functions * Add example figfont --- python/caca/canvas.py | 28 ++++++++++++++++++++ python/examples/figfont.py | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100755 python/examples/figfont.py diff --git a/python/caca/canvas.py b/python/caca/canvas.py index 0e0dcd3..4618a66 100644 --- a/python/caca/canvas.py +++ b/python/caca/canvas.py @@ -951,6 +951,34 @@ class Canvas(_Canvas): return ctypes.string_at(ret, p.value) + def set_figfont(self, filename): + """ Load a figfont and attach it to a canvas. + + filename -- the figfont file to load. + """ + _lib.caca_canvas_set_figfont.argtypes = [_Canvas, ctypes.c_char_p] + _lib.caca_canvas_set_figfont.restype = ctypes.c_int + + return _lib.caca_canvas_set_figfont(self, filename) + + def put_figchar(self, ch): + """ Paste a character using the current figfont. + + ch -- the character to paste + """ + _lib.caca_put_figchar.argtypes = [_Canvas, ctypes.c_uint32] + _lib.caca_put_figchar.restype = ctypes.c_int + + return _lib.caca_put_figchar(self, ord(ch)) + + def flush_figlet(self): + """ Flush the figlet context + """ + _lib.caca_flush_figlet.argtypes = [_Canvas] + _lib.caca_flush_figlet.restype = ctypes.c_int + + return _lib.caca_flush_figlet(self) + class NullCanvas(_Canvas): """ Represent a NULL canvas_t, eg to use as canvas mask for blit operations. """ diff --git a/python/examples/figfont.py b/python/examples/figfont.py new file mode 100755 index 0000000..9d66c32 --- /dev/null +++ b/python/examples/figfont.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# figfont libcaca FIGfont test program +# Copyright (c) 2010 Alex Foulon +# +# This file is a Python port of "examples/figfont.c" +# which is: +# Copyright (c) 2007-2010 Sam Hocevar +# All Rights Reserverd +# +# This library is free software. It comes without any warranty, to +# the extent permitted by applicable law. You can redistribute it +# and/or modify it under the terms of the Do What The Fuck You Want +# To Public License, Version 2, as published by Sam Hocevar. See +# http://sam.zoy.org/wtfpl/COPYING for more details. +# + +import os +import sys + +import caca +from caca.canvas import Canvas, CanvasError + +def main(): + """ Main function. """ + + color = 0 + + if len(sys.argv) < 3: + sys.stderr.write("Usage: %s \n" \ + % os.path.basename(sys.argv[0])) + sys.exit(2) + + try: + cv = Canvas(0, 0) + except CanvasError, err: + sys.stderr.write("%s\n" % err) + sys.exit(2) + + if cv.set_figfont(sys.argv[1]): + sys.stderr.write("Could not open font...\n") + sys.exit(2) + + for c in sys.argv[2]: + color += 4 + cv.set_color_ansi(1+(color % 15), caca.COLOR_TRANSPARENT) + cv.put_figchar(c) + + sys.stderr.write(cv.export_to_memory("utf8")) + +if __name__ == "__main__": + main() +