source: libcaca/trunk/examples/font2tga.c @ 4333

Last change on this file since 4333 was 4333, checked in by Sam Hocevar, 11 years ago

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1/*
2 *  font2tga      libcaca font test program
3 *  Copyright (c) 2006-2009 Sam Hocevar <sam@hocevar.net>
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://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13#include "config.h"
14
15#if !defined(__KERNEL__)
16#   include <stdio.h>
17#   include <stdlib.h>
18#endif
19
20#include "caca.h"
21
22int main(int argc, char *argv[])
23{
24    uint32_t const *blocks;
25    caca_font_t *f;
26    char const * const * fonts;
27    caca_canvas_t *cv;
28    void *buffer;
29    size_t len;
30    unsigned int i, j, x, y, cells, width;
31
32    fonts = caca_get_font_list();
33    f = caca_load_font(fonts[0], 0);
34    blocks = caca_get_font_blocks(f);
35
36    for(i = 0, cells = 0; blocks[i + 1]; i += 2)
37    {
38        cells += blocks[i + 1] - blocks[i];
39        for(j = blocks[i]; j < blocks[i + 1]; j++)
40            if(caca_utf32_is_fullwidth(j))
41                cells++;
42    }
43
44    for(width = 64; width * width < cells; width *= 2)
45        ;
46
47    /* Create a canvas */
48    cv = caca_create_canvas(width, (cells + width - 1) / (width - 1));
49    caca_set_color_ansi(cv, CACA_RED, CACA_RED);
50    caca_clear_canvas(cv);
51    caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE);
52
53    /* Put all glyphs on the canvas */
54    x = y = 0;
55
56    for(i = 0; blocks[i + 1]; i += 2)
57    {
58        for(j = blocks[i]; j < blocks[i + 1]; j++)
59        {
60            caca_put_char(cv, x, y, j);
61
62            if(caca_utf32_is_fullwidth(j))
63                ++x;
64
65            if(++x >= width - 1)
66            {
67                x = 0;
68                y++;
69            }
70        }
71    }
72
73    caca_free_font(f);
74
75    buffer = caca_export_canvas_to_memory(cv, "tga", &len);
76    fwrite(buffer, len, 1, stdout);
77    free(buffer);
78
79    /* Free everything */
80    caca_free_canvas(cv);
81
82    return 0;
83}
84
Note: See TracBrowser for help on using the repository browser.