1 | /* |
---|
2 | * font libcucul font test program |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: font.c 1048 2006-09-17 12:44:18Z jylam $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | #include "config.h" |
---|
15 | #include "common.h" |
---|
16 | |
---|
17 | #if !defined(__KERNEL__) |
---|
18 | # if defined(HAVE_INTTYPES_H) |
---|
19 | # include <inttypes.h> |
---|
20 | # endif |
---|
21 | |
---|
22 | # if defined(HAVE_ENDIAN_H) |
---|
23 | # include <endian.h> |
---|
24 | # endif |
---|
25 | |
---|
26 | # include <stdio.h> |
---|
27 | # include <stdlib.h> |
---|
28 | # include <string.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | #include "cucul.h" |
---|
32 | #include "caca.h" |
---|
33 | |
---|
34 | int main(int argc, char *argv[]) |
---|
35 | { |
---|
36 | cucul_canvas_t *cv; |
---|
37 | caca_display_t *dp; |
---|
38 | cucul_font_t *f; |
---|
39 | cucul_dither_t *d; |
---|
40 | unsigned char *buf; |
---|
41 | unsigned int w, h; |
---|
42 | char const * const * fonts; |
---|
43 | |
---|
44 | /* Create a canvas */ |
---|
45 | cv = cucul_create_canvas(8, 2); |
---|
46 | |
---|
47 | /* Draw stuff on our canvas */ |
---|
48 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK); |
---|
49 | cucul_putstr(cv, 0, 0, "ABcde"); |
---|
50 | cucul_set_color(cv, CUCUL_COLOR_LIGHTRED, CUCUL_COLOR_BLACK); |
---|
51 | cucul_putstr(cv, 5, 0, "\\o/"); |
---|
52 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
53 | cucul_putstr(cv, 0, 1, "&$âøÿØ?!"); |
---|
54 | |
---|
55 | /* Load a libcucul internal font */ |
---|
56 | fonts = cucul_get_font_list(); |
---|
57 | if(fonts[0] == NULL) |
---|
58 | { |
---|
59 | fprintf(stderr, "error: libcucul was compiled without any fonts\n"); |
---|
60 | return -1; |
---|
61 | } |
---|
62 | f = cucul_load_font(fonts[0], 0); |
---|
63 | if(f == NULL) |
---|
64 | { |
---|
65 | fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]); |
---|
66 | return -1; |
---|
67 | } |
---|
68 | |
---|
69 | /* Create our bitmap buffer (32-bit ARGB) */ |
---|
70 | w = cucul_get_canvas_width(cv) * cucul_get_font_width(f); |
---|
71 | h = cucul_get_canvas_height(cv) * cucul_get_font_height(f); |
---|
72 | buf = malloc(4 * w * h); |
---|
73 | |
---|
74 | /* Render the canvas onto our image buffer */ |
---|
75 | cucul_render_canvas(cv, f, buf, w, h, 4 * w); |
---|
76 | |
---|
77 | /* Just for fun, render the image using libcaca */ |
---|
78 | cucul_set_canvas_size(cv, 80, 32); |
---|
79 | dp = caca_create_display(cv); |
---|
80 | |
---|
81 | #if defined(HAVE_ENDIAN_H) |
---|
82 | if(__BYTE_ORDER == __BIG_ENDIAN) |
---|
83 | #else |
---|
84 | /* This is compile-time optimised with at least -O1 or -Os */ |
---|
85 | uint32_t const tmp = 0x12345678; |
---|
86 | if(*(uint8_t const *)&tmp == 0x12) |
---|
87 | #endif |
---|
88 | d = cucul_create_dither(32, w, h, 4 * w, |
---|
89 | 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); |
---|
90 | else |
---|
91 | d = cucul_create_dither(32, w, h, 4 * w, |
---|
92 | 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff); |
---|
93 | |
---|
94 | cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv), |
---|
95 | cucul_get_canvas_height(cv), d, buf); |
---|
96 | caca_refresh_display(dp); |
---|
97 | |
---|
98 | caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1); |
---|
99 | |
---|
100 | /* Free everything */ |
---|
101 | caca_free_display(dp); |
---|
102 | free(buf); |
---|
103 | cucul_free_dither(d); |
---|
104 | cucul_free_font(f); |
---|
105 | cucul_free_canvas(cv); |
---|
106 | |
---|
107 | return 0; |
---|
108 | } |
---|
109 | |
---|