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 849 2006-04-22 19:46:43Z sam $ |
---|
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 | |
---|
16 | #if defined(HAVE_INTTYPES_H) |
---|
17 | # include <inttypes.h> |
---|
18 | #else |
---|
19 | typedef unsigned char uint8_t; |
---|
20 | typedef unsigned short uint16_t; |
---|
21 | typedef unsigned int uint32_t; |
---|
22 | #endif |
---|
23 | |
---|
24 | #if defined(HAVE_ENDIAN_H) |
---|
25 | # include <endian.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <stdio.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <string.h> |
---|
31 | |
---|
32 | #include "cucul.h" |
---|
33 | #include "caca.h" |
---|
34 | |
---|
35 | int main(int argc, char *argv[]) |
---|
36 | { |
---|
37 | cucul_canvas_t *cv; |
---|
38 | caca_display_t *dp; |
---|
39 | cucul_font_t *f; |
---|
40 | cucul_dither_t *d; |
---|
41 | unsigned char *buf; |
---|
42 | unsigned int w, h; |
---|
43 | char const * const * fonts; |
---|
44 | |
---|
45 | /* Create a canvas */ |
---|
46 | cv = cucul_create_canvas(8, 2); |
---|
47 | |
---|
48 | /* Draw stuff on our canvas */ |
---|
49 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK); |
---|
50 | cucul_putstr(cv, 0, 0, "ABcde"); |
---|
51 | cucul_set_color(cv, CUCUL_COLOR_LIGHTRED, CUCUL_COLOR_BLACK); |
---|
52 | cucul_putstr(cv, 5, 0, "\\o/"); |
---|
53 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
54 | cucul_putstr(cv, 0, 1, "&$âøÿØ?!"); |
---|
55 | |
---|
56 | /* Load a libcucul internal font */ |
---|
57 | fonts = cucul_get_font_list(); |
---|
58 | if(fonts[0] == NULL) |
---|
59 | { |
---|
60 | fprintf(stderr, "error: libcucul was compiled without any fonts\n"); |
---|
61 | return -1; |
---|
62 | } |
---|
63 | f = cucul_load_font(fonts[0], 0); |
---|
64 | if(f == NULL) |
---|
65 | { |
---|
66 | fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]); |
---|
67 | return -1; |
---|
68 | } |
---|
69 | |
---|
70 | /* Create our bitmap buffer (32-bit ARGB) */ |
---|
71 | w = cucul_get_canvas_width(cv) * cucul_get_font_width(f); |
---|
72 | h = cucul_get_canvas_height(cv) * cucul_get_font_height(f); |
---|
73 | buf = malloc(4 * w * h); |
---|
74 | |
---|
75 | /* Render the canvas onto our image buffer */ |
---|
76 | cucul_render_canvas(cv, f, buf, w, h, 4 * w); |
---|
77 | |
---|
78 | /* Just for fun, render the image using libcaca */ |
---|
79 | cucul_set_canvas_size(cv, 80, 32); |
---|
80 | dp = caca_create_display(cv); |
---|
81 | |
---|
82 | #if defined(HAVE_ENDIAN_H) |
---|
83 | if(__BYTE_ORDER == __BIG_ENDIAN) |
---|
84 | #else |
---|
85 | /* This is compile-time optimised with at least -O1 or -Os */ |
---|
86 | uint32_t const rmask = 0x12345678; |
---|
87 | if(*(uint8_t const *)&rmask == 0x12) |
---|
88 | #endif |
---|
89 | d = cucul_create_dither(32, w, h, 4 * w, |
---|
90 | 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); |
---|
91 | else |
---|
92 | d = cucul_create_dither(32, w, h, 4 * w, |
---|
93 | 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff); |
---|
94 | |
---|
95 | cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv), |
---|
96 | cucul_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 | cucul_free_dither(d); |
---|
105 | cucul_free_font(f); |
---|
106 | cucul_free_canvas(cv); |
---|
107 | |
---|
108 | return 0; |
---|
109 | } |
---|
110 | |
---|