Debian Codegraph @

python/examples/font.py

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # font libcaca font test program
5 # Copyright (c) 2010 Alex Foulon <[email protected]>
6 #
7 # This file is a Python port of "examples/font.c"
8 # which is:
9 # Copyright (c) 2006-2010 Sam Hocevar <[email protected]>
10 # All Rights Reserverd
11 #
12 # This library is free software. It comes without any warranty, to
13 # the extent permitted by applicable law. You can redistribute it
14 # and/or modify it under the terms of the Do What the Fuck You Want
15 # to Public License, Version 2, as published by Sam Hocevar. See
16 # http://www.wtfpl.net/ for more details.
17 #
18
19 import ctypes
20 import sys
21
22 import caca
23 from caca.canvas import Canvas, CanvasError
24 from caca.display import Display, DisplayError, Event
25 from caca.dither import Dither, DitherError
26 from caca.font import Font, FontError
27
28 def main():
29 """ Main function. """
30
31 try:
32 cv = Canvas(8, 2)
33 except CanvasError as err:
34 sys.stderr.write("%s\n" % err)
35 sys.exit(127)
36
37 cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK)
38 cv.put_str(0, 0, "ABcde")
39 cv.set_color_ansi(caca.COLOR_LIGHTRED, caca.COLOR_BLACK)
40 cv.put_str(5, 0, "\\o/")
41 cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
42 cv.put_str(0, 1, "&$âøÿØ?!")
43
44 fonts = caca.get_font_list()
45 if not fonts:
46 sys.stderr.write("libcaca was compiled without any fonts\n")
47 sys.exit(127)
48
49 try:
50 f = Font(fonts[0])
51 except FontError as err:
52 sys.stderr.write("%s\n" % err)
53 sys.exit(127)
54
55 w = cv.get_width() * f.get_width()
56 h = cv.get_height() * f.get_height()
57 buf = ctypes.c_buffer(4 * w * h)
58
59 cv.render(f, buf, w, h, 4 * w)
60
61 cv.set_size(80, 32)
62 try:
63 dp = Display(cv)
64 except DisplayError as err:
65 sys.stderr.write("%s\n" % err)
66 sys.exit(127)
67
68 try:
69 if sys.byteorder == 'big':
70 dit = Dither(32, w, h, 4 * w, 0xff0000, 0xff00, 0xff, 0xff000000)
71 else:
72 dit = Dither(32, w, h, 4 * w, 0xff00, 0xff0000, 0xff000000, 0xff)
73
74 dit.bitmap(cv, 0, 0, cv.get_width(), cv.get_height(), buf)
75 except DitherError as err:
76 sys.stderr.write("%s\n" % err)
77 sys.exit(127)
78 else:
79 dp.refresh()
80
81 dp.get_event(caca.EVENT_KEY_PRESS, Event(), -1)
82
83 if __name__ == "__main__":
84 main()
85