source: libcaca/trunk/test/font.c @ 858

Last change on this file since 858 was 858, checked in by Sam Hocevar, 15 years ago
  • Added "common.h", a placeholder for simple features and functions missing from some systems.
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
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 858 2006-04-24 19:57:23Z 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#include "common.h"
16
17#if defined(HAVE_INTTYPES_H)
18#   include <inttypes.h>
19#endif
20
21#if defined(HAVE_ENDIAN_H)
22#   include <endian.h>
23#endif
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include "cucul.h"
30#include "caca.h"
31
32int main(int argc, char *argv[])
33{
34    cucul_canvas_t *cv;
35    caca_display_t *dp;
36    cucul_font_t *f;
37    cucul_dither_t *d;
38    unsigned char *buf;
39    unsigned int w, h;
40    char const * const * fonts;
41
42    /* Create a canvas */
43    cv = cucul_create_canvas(8, 2);
44
45    /* Draw stuff on our canvas */
46    cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
47    cucul_putstr(cv, 0, 0, "ABcde");
48    cucul_set_color(cv, CUCUL_COLOR_LIGHTRED, CUCUL_COLOR_BLACK);
49    cucul_putstr(cv, 5, 0, "\\o/");
50    cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
51    cucul_putstr(cv, 0, 1, "&$âøÿØ?!");
52
53    /* Load a libcucul internal font */
54    fonts = cucul_get_font_list();
55    if(fonts[0] == NULL)
56    {
57        fprintf(stderr, "error: libcucul was compiled without any fonts\n");
58        return -1;
59    }
60    f = cucul_load_font(fonts[0], 0);
61    if(f == NULL)
62    {
63        fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
64        return -1;
65    }
66
67    /* Create our bitmap buffer (32-bit ARGB) */
68    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
69    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
70    buf = malloc(4 * w * h);
71
72    /* Render the canvas onto our image buffer */
73    cucul_render_canvas(cv, f, buf, w, h, 4 * w);
74
75    /* Just for fun, render the image using libcaca */
76    cucul_set_canvas_size(cv, 80, 32);
77    dp = caca_create_display(cv);
78
79#if defined(HAVE_ENDIAN_H)
80    if(__BYTE_ORDER == __BIG_ENDIAN)
81#else
82    /* This is compile-time optimised with at least -O1 or -Os */
83    uint32_t const rmask = 0x12345678;
84    if(*(uint8_t const *)&rmask == 0x12)
85#endif
86        d = cucul_create_dither(32, w, h, 4 * w,
87                                0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
88    else
89        d = cucul_create_dither(32, w, h, 4 * w,
90                                0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff);
91
92    cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv),
93                                  cucul_get_canvas_height(cv), d, buf);
94    caca_refresh_display(dp);
95
96    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
97
98    /* Free everything */
99    caca_free_display(dp);
100    free(buf);
101    cucul_free_dither(d);
102    cucul_free_font(f);
103    cucul_free_canvas(cv);
104
105    return 0;
106}
107
Note: See TracBrowser for help on using the repository browser.