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

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

Add caca_export_area_to_memory() to export only a selected part of the
current canvas. This is useful to export dirty rectangles. Accordingly,
add caca_import_area_from_memory() and caca_import_area_from_file().

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