Debian Codegraph @

examples/font.c

raw
1 /*
2 * font libcaca font test program
3 * Copyright (c) 2006-2012 Sam Hocevar <[email protected]>
4 * All Rights Reserved
5 *
6 * This program is free software. It comes without any warranty, to
7 * the extent permitted by applicable law. You can redistribute it
8 * and/or modify it under the terms of the Do What the Fuck You Want
9 * to Public License, Version 2, as published by Sam Hocevar. See
10 * http://www.wtfpl.net/ for more details.
11 */
12
13 #include "config.h"
14
15 #if !defined(__KERNEL__)
16 # if defined(HAVE_ENDIAN_H)
17 # include <endian.h>
18 # endif
19
20 # include <stdio.h>
21 # include <stdlib.h>
22 # include <string.h>
23 #endif
24
25 #include "caca.h"
26
27 int main(int argc, char *argv[])
28 {
29 caca_canvas_t *cv;
30 caca_display_t *dp;
31 caca_font_t *f;
32 caca_dither_t *d;
33 uint8_t *buf;
34 unsigned int w, h;
35 char const * const * fonts;
36
37 /* Create a canvas */
38 cv = caca_create_canvas(8, 2);
39 if(cv == NULL)
40 {
41 printf("Can't create canvas\n");
42 return -1;
43 }
44
45
46 /* Draw stuff on our canvas */
47 caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
48 caca_put_str(cv, 0, 0, "ABcde");
49 caca_set_color_ansi(cv, CACA_LIGHTRED, CACA_BLACK);
50 caca_put_str(cv, 5, 0, "\\o/");
51 caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
52 caca_put_str(cv, 0, 1, "&$âøÿØ?!");
53
54 /* Load a libcaca internal font */
55 fonts = caca_get_font_list();
56 if(fonts[0] == NULL)
57 {
58 fprintf(stderr, "error: libcaca was compiled without any fonts\n");
59 return -1;
60 }
61 f = caca_load_font(fonts[0], 0);
62 if(f == NULL)
63 {
64 fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
65 return -1;
66 }
67
68 /* Create our bitmap buffer (32-bit ARGB) */
69 w = caca_get_canvas_width(cv) * caca_get_font_width(f);
70 h = caca_get_canvas_height(cv) * caca_get_font_height(f);
71 buf = malloc(4 * w * h);
72
73 /* Render the canvas onto our image buffer */
74 caca_render_canvas(cv, f, buf, w, h, 4 * w);
75
76 /* Just for fun, render the image using libcaca */
77 caca_set_canvas_size(cv, 80, 32);
78 dp = caca_create_display(cv);
79
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 = caca_create_dither(32, w, h, 4 * w,
89 0xff0000, 0xff00, 0xff, 0xff000000);
90 else
91 d = caca_create_dither(32, w, h, 4 * w,
92 0xff00, 0xff0000, 0xff000000, 0xff);
93 }
94
95 caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv),
96 caca_get_canvas_height(cv), d, buf);
97 caca_refresh_display(dp);
98
99 caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
100
101 /* Free everything */
102 caca_free_display(dp);
103 free(buf);
104 caca_free_dither(d);
105 caca_free_font(f);
106 caca_free_canvas(cv);
107
108 return 0;
109 }
110