Class: Nuklear::Font

Inherits:
Object
  • Object
show all
Defined in:
ext/nuklear/nkrb_font.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(*args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'ext/nuklear/nkrb_font.c', line 49

VALUE nkrb_font_new(int argc, VALUE *argv, VALUE self) {
  if (argc < 2 || argc > 3)
    rb_raise(rb_eArgError, "Invalid number of arguments (%d for 2 or 3): pass path, size, and an optional arguments hash", argc);
  VALUE path = argv[0];
  VALUE size = argv[1];
  struct nk_font_config config;
  config = nk_font_config((float) NUM2DBL(size));
  if (argc == 3) {
    // TODO support extra font config options
  }

  NkRbFont *font = malloc(sizeof(NkRbFont));
  memset(font, 0, sizeof(NkRbFont));
  nk_font_atlas_init_default(&(font->atlas));
  nk_font_atlas_begin(&(font->atlas));
  font->font = nk_font_atlas_add_from_file(&(font->atlas), StringValueCStr(path), (float) NUM2DBL(size), NULL);
  if (font->font == NULL)
    rb_raise(rb_eStandardError, "Failed to load font file %s", StringValueCStr(path));
  font->img.data = nk_font_atlas_bake(&(font->atlas), &(font->img.width), &(font->img.height), NK_FONT_ATLAS_RGBA32);
  VALUE bytes = rb_str_new((char *) font->img.data, font->img.width * font->img.height * 4);
  VALUE tex = rb_yield_values(3, INT2FIX(font->img.width), INT2FIX(font->img.height), bytes);
  nk_font_atlas_end(&(font->atlas), nk_handle_ptr((void *) NUM2ULL(tex)), &(font->null));
  return Data_Wrap_Struct(cNuklearFont, NULL, nkrb_font_free, font);
}

Instance Method Details

#heightObject



44
45
46
47
# File 'ext/nuklear/nkrb_font.c', line 44

VALUE nkrb_font_height(VALUE self) {
  struct nk_user_font *handle = nkrb_font_to_nk(self);
  return DBL2NUM(handle->height);
}

#width(rtext) ⇒ Object



37
38
39
40
41
42
# File 'ext/nuklear/nkrb_font.c', line 37

VALUE nkrb_font_width(VALUE self, VALUE rtext) {
  const char *text = StringValueCStr(rtext);
  struct nk_user_font *handle = nkrb_font_to_nk(self);
  float width = handle->width(handle->userdata, handle->height, text, (int) strlen(text));
  return DBL2NUM(width);
}