In Files

Parent

Imlib2::Font

Public Class Methods

add_path(p1) click to toggle source

Add a path to the list of font paths.

Example:

Imlib2::Font.add_path '/usr/lib/X11/fonts/Truetype'
static VALUE font_add_path(VALUE klass, VALUE path) {
  UNUSED(klass);
  imlib_add_path_to_font_path(StringValuePtr(path));
  return Qtrue;
}
fonts() click to toggle source

Return an array of all known fonts

Example:

font_list = Imlib2::Font.list_fonts
static VALUE font_list_fonts(VALUE klass) {
  VALUE ary;
  char **list;
  int i, len;
  UNUSED(klass);

  list = imlib_list_fonts(&len);

  ary = rb_ary_new();
  for (i = 0; i < len; i++)
    rb_ary_push(ary, rb_str_new2(list[i]));

  /* FIXME: there has got to be a better way to do this: */
  imlib_free_font_list(list, len);
  
  return ary;
}
list() click to toggle source

Return an array of all known fonts

Example:

font_list = Imlib2::Font.list_fonts
static VALUE font_list_fonts(VALUE klass) {
  VALUE ary;
  char **list;
  int i, len;
  UNUSED(klass);

  list = imlib_list_fonts(&len);

  ary = rb_ary_new();
  for (i = 0; i < len; i++)
    rb_ary_push(ary, rb_str_new2(list[i]));

  /* FIXME: there has got to be a better way to do this: */
  imlib_free_font_list(list, len);
  
  return ary;
}
list_fonts() click to toggle source

Return an array of all known fonts

Example:

font_list = Imlib2::Font.list_fonts
static VALUE font_list_fonts(VALUE klass) {
  VALUE ary;
  char **list;
  int i, len;
  UNUSED(klass);

  list = imlib_list_fonts(&len);

  ary = rb_ary_new();
  for (i = 0; i < len; i++)
    rb_ary_push(ary, rb_str_new2(list[i]));

  /* FIXME: there has got to be a better way to do this: */
  imlib_free_font_list(list, len);
  
  return ary;
}
list_paths() click to toggle source

Return an array of font paths.

Example:

path_list = Imlib2::Font.list_paths
static VALUE font_list_paths(VALUE klass) {
  VALUE ary;
  char **list;
  int i, len;
  UNUSED(klass);

  list = imlib_list_font_path(&len);

  ary = rb_ary_new();
  for (i = 0; i < len; i++)
    rb_ary_push(ary, rb_str_new2(list[i]));

  /* FIXME: there has got to be a better way to do this: */
  imlib_free_font_list(list, len);
  
  return ary;
}
load(p1) click to toggle source

Returns a new Imlib2::Font

Note: the specified font must be in the font path. See Imlib2::Font::list_paths() for a list of font paths, and Imlib2::Font::list_fonts() for a list of fonts.

Examples:

font = Imlib2::Font.new 'helvetica/24'
font = Imlib2::Font.load 'helvetica/24'
VALUE font_new(VALUE klass, VALUE font_name) {
  Imlib_Font *font;
  VALUE f_o;
  
  font = malloc(sizeof(Imlib_Font*));
  *font = imlib_load_font(StringValuePtr(font_name));

  f_o = Data_Wrap_Struct(klass, 0, font_free, font);
  rb_obj_call_init(f_o, 0, NULL);

  return f_o;
}
new(p1) click to toggle source

Returns a new Imlib2::Font

Note: the specified font must be in the font path. See Imlib2::Font::list_paths() for a list of font paths, and Imlib2::Font::list_fonts() for a list of fonts.

Examples:

font = Imlib2::Font.new 'helvetica/24'
font = Imlib2::Font.load 'helvetica/24'
VALUE font_new(VALUE klass, VALUE font_name) {
  Imlib_Font *font;
  VALUE f_o;
  
  font = malloc(sizeof(Imlib_Font*));
  *font = imlib_load_font(StringValuePtr(font_name));

  f_o = Data_Wrap_Struct(klass, 0, font_free, font);
  rb_obj_call_init(f_o, 0, NULL);

  return f_o;
}
paths() click to toggle source

Return an array of font paths.

Example:

path_list = Imlib2::Font.list_paths
static VALUE font_list_paths(VALUE klass) {
  VALUE ary;
  char **list;
  int i, len;
  UNUSED(klass);

  list = imlib_list_font_path(&len);

  ary = rb_ary_new();
  for (i = 0; i < len; i++)
    rb_ary_push(ary, rb_str_new2(list[i]));

  /* FIXME: there has got to be a better way to do this: */
  imlib_free_font_list(list, len);
  
  return ary;
}
remove_path(p1) click to toggle source

Remove a path from the list of font paths.

Example:

Imlib2::Font.remove_path '/usr/lib/X11/fonts/Truetype'
static VALUE font_remove_path(VALUE klass, VALUE path) {
  UNUSED(klass);
  imlib_remove_path_from_font_path(StringValuePtr(path));
  return Qtrue;
}

Public Instance Methods

advance(p1) click to toggle source

Get the horizontal and vertical advance of the given string using this font.

Example:

font = Imlib2::Font.new 'verdana/36'
advances = font.advance "what's my advance?"
['horizontal', 'vertical'].each_index { |i, v|
  puts 'text ' << v << ' advance = ' << advances[i]
}
static VALUE font_text_advance(VALUE self, VALUE text) {
  Imlib_Font *font;
  int   sw = 0, sh = 0;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_get_text_advance(StringValuePtr(text), &sw, &sh);
  
  return rb_ary_new3 (2, INT2FIX(sw), INT2FIX (sh));
}
ascent() click to toggle source

Get font ascent.

Example:

a = font.ascent
static VALUE font_ascent(VALUE self) {
  Imlib_Font *font;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);

  return INT2FIX(imlib_get_font_ascent());
}
descent() click to toggle source

Get font descent.

Example:

a = font.descent
static VALUE font_descent(VALUE self) {
  Imlib_Font *font;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);

  return INT2FIX(imlib_get_font_descent());
}
get_ascent() click to toggle source

Get font ascent.

Example:

a = font.ascent
static VALUE font_ascent(VALUE self) {
  Imlib_Font *font;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);

  return INT2FIX(imlib_get_font_ascent());
}
get_descent() click to toggle source

Get font descent.

Example:

a = font.descent
static VALUE font_descent(VALUE self) {
  Imlib_Font *font;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);

  return INT2FIX(imlib_get_font_descent());
}
get_maximum_ascent() click to toggle source

Get font maximum ascent.

Example:

a = font.maximum_ascent
static VALUE font_maximum_ascent(VALUE self) {
  Imlib_Font *font;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);

  return INT2FIX(imlib_get_maximum_font_ascent());
}
get_maximum_descent() click to toggle source

Get font maximum descent.

Example:

a = font.maximum_descent
static VALUE font_maximum_descent(VALUE self) {
  Imlib_Font *font;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);

  return INT2FIX(imlib_get_maximum_font_descent());
}
get_text_advance(p1) click to toggle source

Get the horizontal and vertical advance of the given string using this font.

Example:

font = Imlib2::Font.new 'verdana/36'
advances = font.advance "what's my advance?"
['horizontal', 'vertical'].each_index { |i, v|
  puts 'text ' << v << ' advance = ' << advances[i]
}
static VALUE font_text_advance(VALUE self, VALUE text) {
  Imlib_Font *font;
  int   sw = 0, sh = 0;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_get_text_advance(StringValuePtr(text), &sw, &sh);
  
  return rb_ary_new3 (2, INT2FIX(sw), INT2FIX (sh));
}
get_text_index_and_location(*args) click to toggle source

Get the character index of the pixel at the given coordinates using this font.

Example:

x, y, char_w, char_h = font.index "index\nstring\n", 5, 5
static VALUE font_text_index(int argc, VALUE *argv, VALUE self) {
  Imlib_Font *font;
  VALUE text, ary;
  int x, y, i, r[] = { 0, 0, 0, 0 };

  text = argv[0];
  switch (argc) {
    case 2:
      /* two arguments is a string, and an array or hash of x, y */
      switch (TYPE(argv[1])) {
        case T_HASH:
          x = NUM2INT(rb_hash_aref(argv[1], rb_str_new2("x")));
          y = NUM2INT(rb_hash_aref(argv[1], rb_str_new2("y")));
          break;
        case T_ARRAY:
          x = NUM2INT(rb_ary_entry(argv[1], 0));
          y = NUM2INT(rb_ary_entry(argv[1], 1));
          break;
        default:
          rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
      }
      break;
    case 3:
      /* three arguments is a string, x, y */
      x = NUM2INT(argv[1]);
      y = NUM2INT(argv[2]);
      break;
    default:
      rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
  }

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_text_get_index_and_location(StringValuePtr(text), x, y,
                                    &r[0], &r[1], &r[2], &r[3]);
  ary = rb_ary_new();
  for (i = 0; i < 4; i++)
    rb_ary_push(ary, INT2FIX(r[i]));
  
  return ary;
}
get_text_inset(p1) click to toggle source

Get the inset of the given string using this font

Example:

font = Imlib2::Font.new 'palatino/9'
inset = font.inset 'wonder what the inset for this string is...'
static VALUE font_text_inset(VALUE self, VALUE text) {
  Imlib_Font *font;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  
  return INT2FIX(imlib_get_text_inset(StringValuePtr(text)));
}
get_text_location_at_index(p1, p2) click to toggle source

Get the character coordinates of the at the given index using this font.

Example:

x, y, char_w, char_h = font.index "index\nstring\n", 8
static VALUE font_text_location(VALUE self, VALUE text, VALUE index) {
  Imlib_Font *font;
  VALUE ary;
  int i, r[] = { 0, 0, 0, 0 };

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_text_get_location_at_index(StringValuePtr(text), NUM2INT(index), 
                                   &r[0], &r[1], &r[2], &r[3]);

  ary = rb_ary_new();
  for (i = 0; i < 4; i++)
    rb_ary_push(ary, INT2FIX(r[i]));
  
  return ary;
}
get_text_size(p1) click to toggle source

Get the width and height of the given string using this font.

Example:

font = Imlib2::Font.new 'helvetica/12'
size = font.size 'how big am i?'
['width', 'height'].each_index { |i, v|
  puts 'text ' << v << ' = ' << size[i]
}
static VALUE font_text_size(VALUE self, VALUE text) {
  Imlib_Font *font;
  int   sw = 0, sh = 0;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_get_text_size(StringValuePtr(text), &sw, &sh);
  
  return rb_ary_new3 (2, INT2FIX(sw), INT2FIX(sh));
}
index(*args) click to toggle source

Get the character index of the pixel at the given coordinates using this font.

Example:

x, y, char_w, char_h = font.index "index\nstring\n", 5, 5
static VALUE font_text_index(int argc, VALUE *argv, VALUE self) {
  Imlib_Font *font;
  VALUE text, ary;
  int x, y, i, r[] = { 0, 0, 0, 0 };

  text = argv[0];
  switch (argc) {
    case 2:
      /* two arguments is a string, and an array or hash of x, y */
      switch (TYPE(argv[1])) {
        case T_HASH:
          x = NUM2INT(rb_hash_aref(argv[1], rb_str_new2("x")));
          y = NUM2INT(rb_hash_aref(argv[1], rb_str_new2("y")));
          break;
        case T_ARRAY:
          x = NUM2INT(rb_ary_entry(argv[1], 0));
          y = NUM2INT(rb_ary_entry(argv[1], 1));
          break;
        default:
          rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
      }
      break;
    case 3:
      /* three arguments is a string, x, y */
      x = NUM2INT(argv[1]);
      y = NUM2INT(argv[2]);
      break;
    default:
      rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
  }

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_text_get_index_and_location(StringValuePtr(text), x, y,
                                    &r[0], &r[1], &r[2], &r[3]);
  ary = rb_ary_new();
  for (i = 0; i < 4; i++)
    rb_ary_push(ary, INT2FIX(r[i]));
  
  return ary;
}
inset(p1) click to toggle source

Get the inset of the given string using this font

Example:

font = Imlib2::Font.new 'palatino/9'
inset = font.inset 'wonder what the inset for this string is...'
static VALUE font_text_inset(VALUE self, VALUE text) {
  Imlib_Font *font;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  
  return INT2FIX(imlib_get_text_inset(StringValuePtr(text)));
}
location(p1, p2) click to toggle source

Get the character coordinates of the at the given index using this font.

Example:

x, y, char_w, char_h = font.index "index\nstring\n", 8
static VALUE font_text_location(VALUE self, VALUE text, VALUE index) {
  Imlib_Font *font;
  VALUE ary;
  int i, r[] = { 0, 0, 0, 0 };

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_text_get_location_at_index(StringValuePtr(text), NUM2INT(index), 
                                   &r[0], &r[1], &r[2], &r[3]);

  ary = rb_ary_new();
  for (i = 0; i < 4; i++)
    rb_ary_push(ary, INT2FIX(r[i]));
  
  return ary;
}
maximum_ascent() click to toggle source

Get font maximum ascent.

Example:

a = font.maximum_ascent
static VALUE font_maximum_ascent(VALUE self) {
  Imlib_Font *font;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);

  return INT2FIX(imlib_get_maximum_font_ascent());
}
maximum_descent() click to toggle source

Get font maximum descent.

Example:

a = font.maximum_descent
static VALUE font_maximum_descent(VALUE self) {
  Imlib_Font *font;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);

  return INT2FIX(imlib_get_maximum_font_descent());
}
size(p1) click to toggle source

Get the width and height of the given string using this font.

Example:

font = Imlib2::Font.new 'helvetica/12'
size = font.size 'how big am i?'
['width', 'height'].each_index { |i, v|
  puts 'text ' << v << ' = ' << size[i]
}
static VALUE font_text_size(VALUE self, VALUE text) {
  Imlib_Font *font;
  int   sw = 0, sh = 0;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_get_text_size(StringValuePtr(text), &sw, &sh);
  
  return rb_ary_new3 (2, INT2FIX(sw), INT2FIX(sh));
}
text_advance(p1) click to toggle source

Get the horizontal and vertical advance of the given string using this font.

Example:

font = Imlib2::Font.new 'verdana/36'
advances = font.advance "what's my advance?"
['horizontal', 'vertical'].each_index { |i, v|
  puts 'text ' << v << ' advance = ' << advances[i]
}
static VALUE font_text_advance(VALUE self, VALUE text) {
  Imlib_Font *font;
  int   sw = 0, sh = 0;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_get_text_advance(StringValuePtr(text), &sw, &sh);
  
  return rb_ary_new3 (2, INT2FIX(sw), INT2FIX (sh));
}
text_index(*args) click to toggle source

Get the character index of the pixel at the given coordinates using this font.

Example:

x, y, char_w, char_h = font.index "index\nstring\n", 5, 5
static VALUE font_text_index(int argc, VALUE *argv, VALUE self) {
  Imlib_Font *font;
  VALUE text, ary;
  int x, y, i, r[] = { 0, 0, 0, 0 };

  text = argv[0];
  switch (argc) {
    case 2:
      /* two arguments is a string, and an array or hash of x, y */
      switch (TYPE(argv[1])) {
        case T_HASH:
          x = NUM2INT(rb_hash_aref(argv[1], rb_str_new2("x")));
          y = NUM2INT(rb_hash_aref(argv[1], rb_str_new2("y")));
          break;
        case T_ARRAY:
          x = NUM2INT(rb_ary_entry(argv[1], 0));
          y = NUM2INT(rb_ary_entry(argv[1], 1));
          break;
        default:
          rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
      }
      break;
    case 3:
      /* three arguments is a string, x, y */
      x = NUM2INT(argv[1]);
      y = NUM2INT(argv[2]);
      break;
    default:
      rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
  }

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_text_get_index_and_location(StringValuePtr(text), x, y,
                                    &r[0], &r[1], &r[2], &r[3]);
  ary = rb_ary_new();
  for (i = 0; i < 4; i++)
    rb_ary_push(ary, INT2FIX(r[i]));
  
  return ary;
}
text_index_and_location(*args) click to toggle source

Get the character index of the pixel at the given coordinates using this font.

Example:

x, y, char_w, char_h = font.index "index\nstring\n", 5, 5
static VALUE font_text_index(int argc, VALUE *argv, VALUE self) {
  Imlib_Font *font;
  VALUE text, ary;
  int x, y, i, r[] = { 0, 0, 0, 0 };

  text = argv[0];
  switch (argc) {
    case 2:
      /* two arguments is a string, and an array or hash of x, y */
      switch (TYPE(argv[1])) {
        case T_HASH:
          x = NUM2INT(rb_hash_aref(argv[1], rb_str_new2("x")));
          y = NUM2INT(rb_hash_aref(argv[1], rb_str_new2("y")));
          break;
        case T_ARRAY:
          x = NUM2INT(rb_ary_entry(argv[1], 0));
          y = NUM2INT(rb_ary_entry(argv[1], 1));
          break;
        default:
          rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
      }
      break;
    case 3:
      /* three arguments is a string, x, y */
      x = NUM2INT(argv[1]);
      y = NUM2INT(argv[2]);
      break;
    default:
      rb_raise(rb_eTypeError, "Invalid argument count (not 2 or 3)");
  }

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_text_get_index_and_location(StringValuePtr(text), x, y,
                                    &r[0], &r[1], &r[2], &r[3]);
  ary = rb_ary_new();
  for (i = 0; i < 4; i++)
    rb_ary_push(ary, INT2FIX(r[i]));
  
  return ary;
}
text_inset(p1) click to toggle source

Get the inset of the given string using this font

Example:

font = Imlib2::Font.new 'palatino/9'
inset = font.inset 'wonder what the inset for this string is...'
static VALUE font_text_inset(VALUE self, VALUE text) {
  Imlib_Font *font;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  
  return INT2FIX(imlib_get_text_inset(StringValuePtr(text)));
}
text_location(p1, p2) click to toggle source

Get the character coordinates of the at the given index using this font.

Example:

x, y, char_w, char_h = font.index "index\nstring\n", 8
static VALUE font_text_location(VALUE self, VALUE text, VALUE index) {
  Imlib_Font *font;
  VALUE ary;
  int i, r[] = { 0, 0, 0, 0 };

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_text_get_location_at_index(StringValuePtr(text), NUM2INT(index), 
                                   &r[0], &r[1], &r[2], &r[3]);

  ary = rb_ary_new();
  for (i = 0; i < 4; i++)
    rb_ary_push(ary, INT2FIX(r[i]));
  
  return ary;
}
text_location_at_index(p1, p2) click to toggle source

Get the character coordinates of the at the given index using this font.

Example:

x, y, char_w, char_h = font.index "index\nstring\n", 8
static VALUE font_text_location(VALUE self, VALUE text, VALUE index) {
  Imlib_Font *font;
  VALUE ary;
  int i, r[] = { 0, 0, 0, 0 };

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_text_get_location_at_index(StringValuePtr(text), NUM2INT(index), 
                                   &r[0], &r[1], &r[2], &r[3]);

  ary = rb_ary_new();
  for (i = 0; i < 4; i++)
    rb_ary_push(ary, INT2FIX(r[i]));
  
  return ary;
}
text_size(p1) click to toggle source

Get the width and height of the given string using this font.

Example:

font = Imlib2::Font.new 'helvetica/12'
size = font.size 'how big am i?'
['width', 'height'].each_index { |i, v|
  puts 'text ' << v << ' = ' << size[i]
}
static VALUE font_text_size(VALUE self, VALUE text) {
  Imlib_Font *font;
  int   sw = 0, sh = 0;

  Data_Get_Struct(self, Imlib_Font, font);
  imlib_context_set_font(*font);
  imlib_get_text_size(StringValuePtr(text), &sw, &sh);
  
  return rb_ary_new3 (2, INT2FIX(sw), INT2FIX(sh));
}

[Validate]

Generated with the Darkfish Rdoc Generator 2.