You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

caca-font.c 2.1 KiB

17 年之前
17 年之前
17 年之前
17 年之前
17 年之前
17 年之前
17 年之前
17 年之前
17 年之前
17 年之前
17 年之前
17 年之前
17 年之前
17 年之前
17 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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://www.wtfpl.net/ 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. }