1 | /* |
---|
2 | * libcucul Ruby bindings |
---|
3 | * Copyright (c) 2007 Pascal Terjan <pterjan@linuxfr.org> |
---|
4 | * |
---|
5 | * This library is free software. It comes without any warranty, to |
---|
6 | * the extent permitted by applicable law. You can redistribute it |
---|
7 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
8 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
9 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
10 | */ |
---|
11 | |
---|
12 | #include <ruby.h> |
---|
13 | #include <cucul.h> |
---|
14 | #include <errno.h> |
---|
15 | #include "common.h" |
---|
16 | |
---|
17 | VALUE cFont; |
---|
18 | |
---|
19 | void font_free(void *font) |
---|
20 | { |
---|
21 | cucul_free_font((cucul_font_t *)font); |
---|
22 | } |
---|
23 | |
---|
24 | static VALUE font_alloc(VALUE klass) |
---|
25 | { |
---|
26 | VALUE obj; |
---|
27 | obj = Data_Wrap_Struct(klass, 0, font_free, NULL); |
---|
28 | return obj; |
---|
29 | } |
---|
30 | |
---|
31 | static VALUE font_initialize(VALUE self, VALUE name) |
---|
32 | { |
---|
33 | cucul_font_t *font; |
---|
34 | |
---|
35 | font = cucul_load_font(StringValuePtr(name), 0); |
---|
36 | if(font == NULL) |
---|
37 | { |
---|
38 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
39 | } |
---|
40 | _SELF = font; |
---|
41 | return self; |
---|
42 | } |
---|
43 | |
---|
44 | static VALUE font_list(void) |
---|
45 | { |
---|
46 | VALUE ary; |
---|
47 | char const* const* list; |
---|
48 | |
---|
49 | list = cucul_get_font_list(); |
---|
50 | |
---|
51 | ary = rb_ary_new(); |
---|
52 | while (*list != NULL) |
---|
53 | { |
---|
54 | rb_ary_push(ary, rb_str_new2(*list)); |
---|
55 | list++; |
---|
56 | } |
---|
57 | |
---|
58 | return ary; |
---|
59 | } |
---|
60 | |
---|
61 | static VALUE get_font_width(VALUE self) |
---|
62 | { |
---|
63 | return UINT2NUM(cucul_get_font_width(_SELF)); |
---|
64 | } |
---|
65 | |
---|
66 | static VALUE get_font_height(VALUE self) |
---|
67 | { |
---|
68 | return UINT2NUM(cucul_get_font_height(_SELF)); |
---|
69 | } |
---|
70 | |
---|
71 | static VALUE get_font_blocks(VALUE self) |
---|
72 | { |
---|
73 | VALUE ary; |
---|
74 | uint32_t const *list; |
---|
75 | |
---|
76 | list = cucul_get_font_blocks(_SELF); |
---|
77 | |
---|
78 | ary = rb_ary_new(); |
---|
79 | while (*list != 0L) |
---|
80 | { |
---|
81 | rb_ary_push(ary, ULONG2NUM(*list)); |
---|
82 | list++; |
---|
83 | } |
---|
84 | |
---|
85 | return ary; |
---|
86 | } |
---|
87 | |
---|
88 | void Init_cucul_font(VALUE mCucul) |
---|
89 | { |
---|
90 | cFont = rb_define_class_under(mCucul, "Font", rb_cObject); |
---|
91 | rb_define_alloc_func(cFont, font_alloc); |
---|
92 | |
---|
93 | rb_define_method(cFont, "initialize", font_initialize, 1); |
---|
94 | rb_define_method(cFont, "width", get_font_width, 0); |
---|
95 | rb_define_method(cFont, "height", get_font_height, 0); |
---|
96 | rb_define_method(cFont, "blocks", get_font_blocks, 0); |
---|
97 | rb_define_singleton_method(cFont, "list", font_list, 0); |
---|
98 | } |
---|
99 | |
---|