* Bind caca_render_canvas function. * Add example font.tags/v0.99.beta18
@@ -17,6 +17,7 @@ | |||||
import ctypes | import ctypes | ||||
from caca import _lib | from caca import _lib | ||||
from caca.font import _Font | |||||
class _Canvas(object): | class _Canvas(object): | ||||
""" Model for Canvas objects. | """ Model for Canvas objects. | ||||
@@ -979,6 +980,23 @@ class Canvas(_Canvas): | |||||
return _lib.caca_flush_figlet(self) | return _lib.caca_flush_figlet(self) | ||||
def render(self, font, buf, width, height, pitch): | |||||
""" Render the canvas onto an image buffer. | |||||
font -- a Font() object | |||||
buf -- the image buffer | |||||
width -- the width (in pixels) of the image | |||||
heigth -- the height (in pixels) of the image | |||||
pitch -- the pitch (in bytes) of the image | |||||
""" | |||||
_lib.caca_render_canvas.argtypes = [ | |||||
_Canvas, _Font, ctypes.c_char_p, | |||||
ctypes.c_int, ctypes.c_int, ctypes.c_int | |||||
] | |||||
_lib.caca_render_canvas.restype = ctypes.c_int | |||||
return _lib.caca_render_canvas(self, font, buf, width, height, pitch) | |||||
class NullCanvas(_Canvas): | class NullCanvas(_Canvas): | ||||
""" Represent a NULL canvas_t, eg to use as canvas mask for blit operations. | """ Represent a NULL canvas_t, eg to use as canvas mask for blit operations. | ||||
""" | """ | ||||
@@ -15,6 +15,7 @@ | |||||
""" Libcaca Python bindings """ | """ Libcaca Python bindings """ | ||||
import ctypes | import ctypes | ||||
import errno | |||||
from caca import _lib | from caca import _lib | ||||
@@ -31,8 +32,9 @@ class _Font(object): | |||||
return self._font | return self._font | ||||
def __del__(self): | def __del__(self): | ||||
if self._font > 0: | |||||
self._free() | |||||
if hasattr(self, "_font"): | |||||
if self._font > 0: | |||||
self._free() | |||||
def __str__(self): | def __str__(self): | ||||
return "<CacaFont>" | return "<CacaFont>" | ||||
@@ -55,5 +57,44 @@ class Font(_Font): | |||||
font -- the memory area containing the font or its name | font -- the memory area containing the font or its name | ||||
size -- the size of the memory area, or 0 if the font name is given | size -- the size of the memory area, or 0 if the font name is given | ||||
""" | """ | ||||
pass | |||||
if size == 0: | |||||
_lib.caca_load_font.argtypes = [ctypes.c_char_p, ctypes.c_int] | |||||
else: | |||||
raise FontError("Unsupported method") | |||||
_lib.caca_load_font.restype = ctypes.c_int | |||||
self._font = _lib.caca_load_font(font, size) | |||||
if self._font == 0: | |||||
err = ctypes.c_int.in_dll(_lib, "errno") | |||||
if err.value == errno.ENOENT: | |||||
raise FontError("Requested built-in font does not exist") | |||||
elif err.value == errno.EINVAL: | |||||
raise FontError("Invalid font data in memory area") | |||||
elif err.value == errno.ENOMEM: | |||||
raise FontError("Not enough memory to allocate font structure") | |||||
def get_width(self): | |||||
""" Get a font's standard glyph width. | |||||
""" | |||||
_lib.caca_get_font_width.argtypes = [_Font] | |||||
_lib.caca_get_font_width.restype = ctypes.c_int | |||||
return _lib.caca_get_font_width(self) | |||||
def get_height(self): | |||||
""" Get a font's standard glyph height. | |||||
""" | |||||
_lib.caca_get_font_height.argtypes = [_Font] | |||||
_lib.caca_get_font_height.restype = ctypes.c_int | |||||
return _lib.caca_get_font_height(self) | |||||
def get_blocks(self): | |||||
""" Get a font's list of supported glyphs. | |||||
""" | |||||
raise FontError("Not Implemented") | |||||
class FontError(Exception): | |||||
pass | |||||
@@ -0,0 +1,80 @@ | |||||
#!/usr/bin/env python | |||||
# -*- coding: utf-8 -*- | |||||
# | |||||
# font libcaca font test program | |||||
# Copyright (c) 2010 Alex Foulon <alxf@lavabit.com> | |||||
# | |||||
# This file is a Python port of "examples/font.c" | |||||
# which is: | |||||
# Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net> | |||||
# 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 ctypes | |||||
import sys | |||||
import caca | |||||
from caca.canvas import Canvas, CanvasError | |||||
from caca.display import Display, DisplayError, Event | |||||
from caca.dither import Dither, DitherError | |||||
from caca.font import Font, FontError | |||||
def main(): | |||||
""" Main function. """ | |||||
try: | |||||
cv = Canvas(8, 2) | |||||
except CanvasError, err: | |||||
sys.stderr.write("%s\n" % err) | |||||
sys.exit(127) | |||||
cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK) | |||||
cv.put_str(0, 0, "ABcde") | |||||
cv.set_color_ansi(caca.COLOR_LIGHTRED, caca.COLOR_BLACK) | |||||
cv.put_str(5, 0, "\\o/") | |||||
cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE) | |||||
cv.put_str(0, 1, "&$âøÿØ?!") | |||||
fonts = caca.get_font_list() | |||||
if not fonts: | |||||
sys.stderr.write("libcaca was compiled without any fonts\n") | |||||
sys.exit(127) | |||||
try: | |||||
f = Font(fonts[0]) | |||||
except FontError, err: | |||||
sys.stderr.write("%s\n" % err) | |||||
sys.exit(127) | |||||
w = cv.get_width() * f.get_width() | |||||
h = cv.get_height() * f.get_height() | |||||
buf = ctypes.c_buffer(4 * w * h) | |||||
cv.render(f, buf, w, h, 4 * w) | |||||
cv.set_size(80, 32) | |||||
try: | |||||
dp = Display(cv) | |||||
except DisplayError, err: | |||||
sys.stderr.write("%s\n" % err) | |||||
sys.exit(127) | |||||
if sys.byteorder == 'big': | |||||
dit = Dither(32, w, h, 4 * w, 0xff0000, 0xff00, 0xff, 0xff000000) | |||||
else: | |||||
dit = Dither(32, w, h, 4 * w, 0xff00, 0xff0000, 0xff000000, 0xff) | |||||
dit.bitmap(cv, 0, 0, cv.get_width(), cv.get_height(), buf) | |||||
dp.refresh() | |||||
dp.get_event(caca.EVENT_KEY_PRESS, Event(), -1) | |||||
if __name__ == "__main__": | |||||
main() | |||||