Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

100 řádky
2.1 KiB

  1. /*
  2. * libcaca Ruby bindings
  3. * Copyright (c) 2007-2012 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. #include <ruby.h>
  12. #include <caca.h>
  13. #include <errno.h>
  14. #include "common.h"
  15. VALUE cFont;
  16. void font_free(void *font)
  17. {
  18. caca_free_font((caca_font_t *)font);
  19. }
  20. static VALUE font_alloc(VALUE klass)
  21. {
  22. VALUE obj;
  23. obj = Data_Wrap_Struct(klass, 0, font_free, NULL);
  24. return obj;
  25. }
  26. static VALUE font_initialize(VALUE self, VALUE name)
  27. {
  28. caca_font_t *font;
  29. font = caca_load_font(StringValuePtr(name), 0);
  30. if(font == NULL)
  31. {
  32. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  33. }
  34. _SELF = font;
  35. return self;
  36. }
  37. static VALUE font_list(void)
  38. {
  39. VALUE ary;
  40. char const* const* list;
  41. list = caca_get_font_list();
  42. ary = rb_ary_new();
  43. while (*list != NULL)
  44. {
  45. rb_ary_push(ary, rb_str_new2(*list));
  46. list++;
  47. }
  48. return ary;
  49. }
  50. static VALUE get_font_width(VALUE self)
  51. {
  52. return UINT2NUM(caca_get_font_width(_SELF));
  53. }
  54. static VALUE get_font_height(VALUE self)
  55. {
  56. return UINT2NUM(caca_get_font_height(_SELF));
  57. }
  58. static VALUE get_font_blocks(VALUE self)
  59. {
  60. VALUE ary;
  61. uint32_t const *list;
  62. list = caca_get_font_blocks(_SELF);
  63. ary = rb_ary_new();
  64. while (*list != 0L)
  65. {
  66. rb_ary_push(ary, ULONG2NUM(*list));
  67. list++;
  68. }
  69. return ary;
  70. }
  71. void Init_caca_font(VALUE mCaca)
  72. {
  73. cFont = rb_define_class_under(mCaca, "Font", rb_cObject);
  74. rb_define_alloc_func(cFont, font_alloc);
  75. rb_define_method(cFont, "initialize", font_initialize, 1);
  76. rb_define_method(cFont, "width", get_font_width, 0);
  77. rb_define_method(cFont, "height", get_font_height, 0);
  78. rb_define_method(cFont, "blocks", get_font_blocks, 0);
  79. rb_define_singleton_method(cFont, "list", font_list, 0);
  80. }