File: caca-font.c

package info (click to toggle)
libcaca 0.99.beta20-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,264 kB
  • sloc: ansic: 25,091; php: 2,763; python: 2,637; cs: 1,213; cpp: 1,127; java: 916; objc: 836; makefile: 543; perl: 505; sh: 472; asm: 297; ruby: 215; xml: 33
file content (99 lines) | stat: -rw-r--r-- 2,126 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 *  libcaca Ruby bindings
 *  Copyright (c) 2007-2012 Pascal Terjan <pterjan@linuxfr.org>
 *
 *  This library is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What the Fuck You Want
 *  to Public License, Version 2, as published by Sam Hocevar. See
 *  http://www.wtfpl.net/ for more details.
 */

#include <ruby.h>
#include <caca.h>
#include <errno.h>
#include "common.h"

VALUE cFont;

void font_free(void *font)
{
    caca_free_font((caca_font_t *)font);
}

static VALUE font_alloc(VALUE klass)
{
    VALUE obj;
    obj = Data_Wrap_Struct(klass, 0, font_free, NULL);
    return obj;
}

static VALUE font_initialize(VALUE self, VALUE name)
{
    caca_font_t *font;

    font = caca_load_font(StringValuePtr(name), 0);
    if(font == NULL)
    {
        rb_raise(rb_eRuntimeError, "%s", strerror(errno));
    }
    _SELF = font;
    return self;
}

static VALUE font_list(void)
{
    VALUE ary;
    char const* const* list;

    list = caca_get_font_list();

    ary = rb_ary_new();
    while (*list != NULL)
    {
        rb_ary_push(ary, rb_str_new2(*list));
        list++;
    }

    return ary;
}

static VALUE get_font_width(VALUE self)
{
    return UINT2NUM(caca_get_font_width(_SELF));
}

static VALUE get_font_height(VALUE self)
{
    return UINT2NUM(caca_get_font_height(_SELF));
}

static VALUE get_font_blocks(VALUE self)
{
    VALUE ary;
    uint32_t const *list;

    list = caca_get_font_blocks(_SELF);

    ary = rb_ary_new();
    while (*list != 0L)
    {
        rb_ary_push(ary, ULONG2NUM(*list));
        list++;
    }

    return ary;
}

void Init_caca_font(VALUE mCaca)
{
    cFont = rb_define_class_under(mCaca, "Font", rb_cObject);
    rb_define_alloc_func(cFont, font_alloc);

    rb_define_method(cFont, "initialize", font_initialize, 1);
    rb_define_method(cFont, "width", get_font_width, 0);
    rb_define_method(cFont, "height", get_font_height, 0);
    rb_define_method(cFont, "blocks", get_font_blocks, 0);
    rb_define_singleton_method(cFont, "list", font_list, 0);
}