source: libcaca/trunk/examples/font.c @ 2101

Last change on this file since 2101 was 2101, checked in by Sam Hocevar, 13 years ago
  • Rename "test" to "examples". The former will become a real testsuite.
  • Autodetect zzuf in the configure step. It might become handy.
  • Property svn:keywords set to Id
File size: 3.1 KB
RevLine 
[750]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 2101 2007-11-30 23:48:39Z sam $
7 *
[1462]8 *  This program is free software. It comes without any warranty, to
[1452]9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
[750]12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15#include "config.h"
[858]16#include "common.h"
[750]17
[1048]18#if !defined(__KERNEL__)
19#   if defined(HAVE_INTTYPES_H)
20#      include <inttypes.h>
21#   endif
[750]22
[1048]23#   if defined(HAVE_ENDIAN_H)
24#      include <endian.h>
25#   endif
26
27#   include <stdio.h>
28#   include <stdlib.h>
29#   include <string.h>
[767]30#endif
31
[750]32#include "cucul.h"
[767]33#include "caca.h"
[750]34
35int main(int argc, char *argv[])
36{
[811]37    cucul_canvas_t *cv;
38    caca_display_t *dp;
[777]39    cucul_font_t *f;
40    cucul_dither_t *d;
[758]41    unsigned char *buf;
[767]42    unsigned int w, h;
[763]43    char const * const * fonts;
[750]44
[760]45    /* Create a canvas */
[813]46    cv = cucul_create_canvas(8, 2);
[1753]47    if(cv == NULL)
48    {
49        printf("Can't create canvas\n");
50        return -1;
51    }
[760]52
[1753]53
[760]54    /* Draw stuff on our canvas */
[1267]55    cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
[1347]56    cucul_put_str(cv, 0, 0, "ABcde");
[1267]57    cucul_set_color_ansi(cv, CUCUL_LIGHTRED, CUCUL_BLACK);
[1347]58    cucul_put_str(cv, 5, 0, "\\o/");
[1267]59    cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
[1347]60    cucul_put_str(cv, 0, 1, "&$âøÿØ?!");
[750]61
[760]62    /* Load a libcucul internal font */
[763]63    fonts = cucul_get_font_list();
64    if(fonts[0] == NULL)
65    {
66        fprintf(stderr, "error: libcucul was compiled without any fonts\n");
67        return -1;
68    }
69    f = cucul_load_font(fonts[0], 0);
[767]70    if(f == NULL)
71    {
72        fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
73        return -1;
74    }
[758]75
[760]76    /* Create our bitmap buffer (32-bit ARGB) */
[813]77    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
78    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
[758]79    buf = malloc(4 * w * h);
80
[760]81    /* Render the canvas onto our image buffer */
[811]82    cucul_render_canvas(cv, f, buf, w, h, 4 * w);
[758]83
[767]84    /* Just for fun, render the image using libcaca */
[813]85    cucul_set_canvas_size(cv, 80, 32);
[819]86    dp = caca_create_display(cv);
[760]87
[1852]88    {
[767]89#if defined(HAVE_ENDIAN_H)
[1852]90        if(__BYTE_ORDER == __BIG_ENDIAN)
[767]91#else
[1852]92        /* This is compile-time optimised with at least -O1 or -Os */
93        uint32_t const tmp = 0x12345678;
94        if(*(uint8_t const *)&tmp == 0x12)
[767]95#endif
[1852]96            d = cucul_create_dither(32, w, h, 4 * w,
97                                    0xff0000, 0xff00, 0xff, 0xff000000);
98        else
99            d = cucul_create_dither(32, w, h, 4 * w,
100                                    0xff00, 0xff0000, 0xff000000, 0xff);
101    }
[758]102
[816]103    cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv),
104                                  cucul_get_canvas_height(cv), d, buf);
[819]105    caca_refresh_display(dp);
[767]106
[849]107    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
[767]108
[760]109    /* Free everything */
[819]110    caca_free_display(dp);
[758]111    free(buf);
[767]112    cucul_free_dither(d);
[750]113    cucul_free_font(f);
[813]114    cucul_free_canvas(cv);
[750]115
116    return 0;
117}
118
Note: See TracBrowser for help on using the repository browser.