| [1995] | 1 | /* |
|---|
| [2821] | 2 | * libcaca Ruby bindings |
|---|
| [1995] | 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> |
|---|
| [2821] | 13 | #include <caca.h> |
|---|
| [1995] | 14 | #include <errno.h> |
|---|
| 15 | #include "common.h" |
|---|
| 16 | |
|---|
| 17 | VALUE cFont; |
|---|
| 18 | |
|---|
| 19 | void font_free(void *font) |
|---|
| 20 | { |
|---|
| [2821] | 21 | caca_free_font((caca_font_t *)font); |
|---|
| [1995] | 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 | { |
|---|
| [2821] | 33 | caca_font_t *font; |
|---|
| [1995] | 34 | |
|---|
| [2821] | 35 | font = caca_load_font(StringValuePtr(name), 0); |
|---|
| [1995] | 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 | |
|---|
| [2821] | 49 | list = caca_get_font_list(); |
|---|
| [1995] | 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 | { |
|---|
| [2821] | 63 | return UINT2NUM(caca_get_font_width(_SELF)); |
|---|
| [1995] | 64 | } |
|---|
| 65 | |
|---|
| 66 | static VALUE get_font_height(VALUE self) |
|---|
| 67 | { |
|---|
| [2821] | 68 | return UINT2NUM(caca_get_font_height(_SELF)); |
|---|
| [1995] | 69 | } |
|---|
| 70 | |
|---|
| 71 | static VALUE get_font_blocks(VALUE self) |
|---|
| 72 | { |
|---|
| 73 | VALUE ary; |
|---|
| [2303] | 74 | uint32_t const *list; |
|---|
| [1995] | 75 | |
|---|
| [2821] | 76 | list = caca_get_font_blocks(_SELF); |
|---|
| [1995] | 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 | |
|---|
| [2822] | 88 | void Init_caca_font(VALUE mCaca) |
|---|
| [1995] | 89 | { |
|---|
| [2822] | 90 | cFont = rb_define_class_under(mCaca, "Font", rb_cObject); |
|---|
| [1995] | 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 | |
|---|