Class: FT2::Glyph

Inherits:
Object
  • Object
show all
Defined in:
ext/ft2-ruby/ft2.c

Direct Known Subclasses

BitmapGlyph, OutlineGlyph

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.initializeObject

Constructor for FT2::Glyph class.

This method is currently empty. You should never call this method directly unless you’re instantiating a derived class (ie, you know what you’re doing).



2391
2392
2393
# File 'ext/ft2-ruby/ft2.c', line 2391

static VALUE ft_glyph_init(VALUE self) {
  return self;
}

Instance Method Details

#advanceObject

Get the advance of a FT2::Glyph object.

Description:

This vector gives the FT2::Glyph object's advance width.

Examples:

advance = glyph.advance


2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
# File 'ext/ft2-ruby/ft2.c', line 2459

static VALUE ft_glyph_advance(VALUE self) {
  FT_Glyph *glyph;
  VALUE ary;

  Data_Get_Struct(self, FT_Glyph, glyph);
  ary = rb_ary_new();
  rb_ary_push(ary, INT2FIX((*glyph)->advance.x));
  rb_ary_push(ary, INT2FIX((*glyph)->advance.y));

  return ary;
}

#cbox(bbox_mode) ⇒ Object Also known as: control_box

Get the control box of a FT2::Glyph object.

Description:

Returns a FT2::Glyph object's `control box'. The control box
encloses all the outline's points, including Bezier control points.
Though it coincides with the exact bounding box for most FT2::Glyph
objects, it can be slightly larger in some situations (like when
rotating an outline which contains Bezier outside arcs).

Computing the control box is very fast, while getting the bounding
box can take much more time as it needs to walk over all segments
and arcs in the outline. To get the latter, you can use the
`ftbbox' component which is dedicated to this single task.

Notes:

Coordinates are relative to the FT2::Glyph object's origin, using
the Y-upwards convention.

If the FT2::Glyph object has been loaded with FT2::Load::NO_SCALE,
`bbox_mode' must be set to FT2::GlyphBBox::UNSCALED to get unscaled
font units.

If `bbox_mode' is set to FT2::GlyphBBox::SUBPIXELS the
bbox coordinates are returned in 26.6 pixels (i.e. 1/64th of
pixels).

Note that the maximum coordinates are exclusive, which means that
one can compute the width and height of the FT2::Glyph object image
(be it in integer or 26.6 pixels) as:

width = bbox.xMax - bbox.xMin; height = bbox.yMax - bbox.yMin;

Note also that for 26.6 coordinates, if `bbox_mode' is set to
FT2::GlyphBBox::GRIDFIT, the coordinates will also be
grid-fitted, which corresponds to:

bbox.xMin = FLOOR(bbox.xMin); bbox.yMin = FLOOR(bbox.yMin);
bbox.xMax = CEILING(bbox.xMax); bbox.yMax = CEILING(bbox.yMax);

To get the bbox in pixel coordinates, set `bbox_mode' to
FT2::GlyphBBox::TRUNCATE.

To get the bbox in grid-fitted pixel coordinates, set `bbox_mode'
to FT2::GlyphBBox::PIXELS.

The default value for `bbox_mode' is FT2::GlyphBBox::PIXELS.

Aliases:

FT2::Glyph#control_box

Examples:

bbox_mode = FT2::GlyphBBox::PIXELS
x_min, y_min, x_max, y_max = glyph.cbox bbox_mode


2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
# File 'ext/ft2-ruby/ft2.c', line 2592

static VALUE ft_glyph_cbox(VALUE self, VALUE bbox_mode) {
  FT_Glyph *glyph;
  FT_BBox  bbox;
  VALUE    ary;

  Data_Get_Struct(self, FT_Glyph, glyph);
  FT_Glyph_Get_CBox(*glyph, FIX2INT(bbox_mode), &bbox);

  ary = rb_ary_new();
  rb_ary_push(ary, INT2FIX(bbox.xMin));
  rb_ary_push(ary, INT2FIX(bbox.yMin));
  rb_ary_push(ary, INT2FIX(bbox.xMax));
  rb_ary_push(ary, INT2FIX(bbox.yMax));

  return ary;
}

#classObject Also known as: clazz

Get the FreeType2 class of a FT2::Glyph object.

Note:

This is _not_ the Ruby class of the object.

Aliases:

FT2::Glyph#clazz

Examples:

c = glyph.class


2424
2425
2426
2427
2428
# File 'ext/ft2-ruby/ft2.c', line 2424

static VALUE ft_glyph_class(VALUE self) {
  FT_Glyph *glyph;
  Data_Get_Struct(self, FT_Glyph, glyph);
  return Data_Wrap_Struct(cGlyphClass, 0, dont_free, &(*glyph)->clazz);
}

#dupObject Also known as: copy

Duplicate a FT2::Glyph object.

Aliases:

FT2::Glyph#copy

Examples:

new_glyph = glyph.dup


2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
# File 'ext/ft2-ruby/ft2.c', line 2481

static VALUE ft_glyph_dup(VALUE self) {
  FT_Error err;
  FT_Glyph *glyph, *new_glyph;

  new_glyph = malloc(sizeof(FT_Glyph));
  Data_Get_Struct(self, FT_Glyph, glyph);
  err = FT_Glyph_Copy(*glyph, new_glyph);
  if (err != FT_Err_Ok)
    handle_error(err);

  return Data_Wrap_Struct(cGlyph, 0, glyph_free, new_glyph);
}

#formatObject

Get the format of a FT2::Glyph object’s image.

Glyph Formats:

FT2::GlyphFormat::COMPOSITE
FT2::GlyphFormat::BITMAP
FT2::GlyphFormat::OUTLINE
FT2::GlyphFormat::PLOTTER

Examples:

format = glyph.format


2443
2444
2445
2446
2447
# File 'ext/ft2-ruby/ft2.c', line 2443

static VALUE ft_glyph_format(VALUE self) {
  FT_Glyph *glyph;
  Data_Get_Struct(self, FT_Glyph, glyph);
  return INT2NUM((*glyph)->format);
}

#libraryObject

Get the library of a FT2::Glyph object.

Note:

Glyph objects are not owned or tracked by the library.

Examples:

lib = glyph.library


2405
2406
2407
2408
2409
# File 'ext/ft2-ruby/ft2.c', line 2405

static VALUE ft_glyph_library(VALUE self) {
  FT_Glyph *glyph;
  Data_Get_Struct(self, FT_Glyph, glyph);
  return Data_Wrap_Struct(cLibrary, 0, dont_free, (*glyph)->library);
}

#to_bmap(render_mode, origin, destroy) ⇒ Object Also known as: to_bitmap

Render a FT2::Glyph object as a FT2::BitmapGlyph object.

Description:

Converts a FT2::Glyph object to a FT2::BitmapGlyph object.

render_mode: A set of bit flags that describe how the data is.
origin:      A vector used to translate the glyph image before
             rendering. Can be nil (if no translation).  The
             origin is expressed in 26.6 pixels.
destroy:     A boolean that indicates that the original glyph
             image should be destroyed by this function. It is
             never destroyed in case of error.

Aliases:

FT2::Glyph#to_bitmap

Examples:

glyph_to_bmap = glyph.glyph_to_bmap


2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
# File 'ext/ft2-ruby/ft2.c', line 2630

static VALUE ft_glyph_to_bmap(VALUE self, VALUE render_mode, VALUE origin, VALUE destroy) {
  FT_Error err;
  FT_Glyph *glyph;
  FT_Vector v;
  FT_Bool d;

  Data_Get_Struct(self, FT_Glyph, glyph);
  v.x = NUM2INT(rb_ary_entry(origin, 0));
  v.y = NUM2INT(rb_ary_entry(origin, 1));
  d = (destroy != Qnil) ? 1 : 0;

  err = FT_Glyph_To_Bitmap(glyph, FIX2INT(render_mode), &v, d);
  if (err != FT_Err_Ok)
    handle_error(err);

  return self;
}

#transform(matrix_ary, delta_ary) ⇒ Object

Transform a FT2::Glyph object if it’s format is scalable.

Description:

matrix: A pointer to a 2x2 matrix to apply.
delta:  A pointer to a 2d vector to apply. Coordinates are
        expressed in 1/64th of a pixel.

Note:

The transformation matrix is also applied to the glyph's advance
vector.  This method returns an error if the glyph format is not
scalable (eg, if it's not equal to zero).

Examples:

matrix = [[1, 0],
          [0, 1]]
delta = [1, 1]
transform = glyph.transform matrix, delta


2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
# File 'ext/ft2-ruby/ft2.c', line 2514

static VALUE ft_glyph_transform(VALUE self, VALUE matrix_ary, VALUE delta_ary) {
  FT_Error err;
  FT_Glyph *glyph;
  FT_Matrix matrix;
  FT_Vector delta;

  matrix.xx = DBL2FTFIX(NUM2DBL(rb_ary_entry(rb_ary_entry(matrix_ary, 0), 0)));
  matrix.xy = DBL2FTFIX(NUM2DBL(rb_ary_entry(rb_ary_entry(matrix_ary, 0), 1)));
  matrix.yx = DBL2FTFIX(NUM2DBL(rb_ary_entry(rb_ary_entry(matrix_ary, 1), 0)));
  matrix.yy = DBL2FTFIX(NUM2DBL(rb_ary_entry(rb_ary_entry(matrix_ary, 1), 1)));

  delta.x = NUM2INT(rb_ary_entry(delta_ary, 0));
  delta.y = NUM2INT(rb_ary_entry(delta_ary, 1));

  Data_Get_Struct(self, FT_Glyph, glyph);
  err = FT_Glyph_Transform(*glyph, &matrix, &delta);
  if (err != FT_Err_Ok)
    handle_error(err);

  return self;
}