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

Last change on this file since 813 was 813, checked in by Sam Hocevar, 15 years ago
  • Renamed main canvas-related functions so that they have "canvas" in their name, eg. cucul_create() -> cucul_create_canvas() etc.
  • Moved buffer-related functions to cucul/buffer.c.
  • Property svn:keywords set to Id
File size: 3.0 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 813 2006-04-18 15:54:33Z 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
19typedef unsigned char uint8_t;
20typedef unsigned short uint16_t;
21typedef 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
35int 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    caca_event_t ev;
42    unsigned char *buf;
43    unsigned int w, h;
44    char const * const * fonts;
45
46    /* Create a canvas */
47    cv = cucul_create_canvas(8, 2);
48
49    /* Draw stuff on our canvas */
50    cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
51    cucul_putstr(cv, 0, 0, "ABcde");
52    cucul_set_color(cv, CUCUL_COLOR_LIGHTRED, CUCUL_COLOR_BLACK);
53    cucul_putstr(cv, 5, 0, "\\o/");
54    cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
55    cucul_putstr(cv, 0, 1, "&$âøÿØ?!");
56
57    /* Load a libcucul internal font */
58    fonts = cucul_get_font_list();
59    if(fonts[0] == NULL)
60    {
61        fprintf(stderr, "error: libcucul was compiled without any fonts\n");
62        return -1;
63    }
64    f = cucul_load_font(fonts[0], 0);
65    if(f == NULL)
66    {
67        fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
68        return -1;
69    }
70
71    /* Create our bitmap buffer (32-bit ARGB) */
72    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
73    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
74    buf = malloc(4 * w * h);
75
76    /* Render the canvas onto our image buffer */
77    cucul_render_canvas(cv, f, buf, w, h, 4 * w);
78
79    /* Just for fun, render the image using libcaca */
80    cucul_set_canvas_size(cv, 80, 32);
81    dp = caca_attach(cv);
82
83#if defined(HAVE_ENDIAN_H)
84    if(__BYTE_ORDER == __BIG_ENDIAN)
85#else
86    /* This is compile-time optimised with at least -O1 or -Os */
87    uint32_t const rmask = 0x12345678;
88    if(*(uint8_t const *)&rmask == 0x12)
89#endif
90        d = cucul_create_dither(32, w, h, 4 * w,
91                                0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
92    else
93        d = cucul_create_dither(32, w, h, 4 * w,
94                                0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff);
95
96    cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv) - 1,
97                                  cucul_get_canvas_height(cv) - 1, d, buf);
98    caca_display(dp);
99
100    caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1);
101
102    /* Free everything */
103    caca_detach(dp);
104    free(buf);
105    cucul_free_dither(d);
106    cucul_free_font(f);
107    cucul_free_canvas(cv);
108
109    return 0;
110}
111
Note: See TracBrowser for help on using the repository browser.