use Font::FreeType; my $freetype = Font::FreeType->new; my $face = $freetype->face('Vera.ttf'); $face->set_char_size(24, 24, 100, 100); my $glyph = $face->glyph_from_char('A'); my $glyph = $face->glyph_from_char_code(65); # Render into an array of strings, one byte per pixel. my ($bitmap, $left, $top) = $glyph->bitmap; # Read vector outline. $glyph->outline_decompose( move_to => sub { ... }, line_to => sub { ... }, conic_to => sub { ... }, cubic_to => sub { ... }, );
Things you an do with glyphs include:
For a detailed description of the meaning of glyph metrics, and the structure of vectorial outlines, see <http://freetype.sourceforge.net/freetype2/docs/glyphs/>
Three values are returned: the bitmap itself, the number of pixels from the origin to where the left of the area the bitmap describes, and the number of pixels from the origin to the top of the area of the bitmap (positive being up).
The bitmap value is a reference to an array. Each item in the array represents a line of the bitmap, starting from the top. Each item is a string of bytes, with one byte representing one pixel of the image, starting from the left. A value of 0 indicates background (outside the glyph outline), and 255 represents a point inside the outline.
If antialiasing is used then shades of grey between 0 and 255 may occur. Antialiasing is performed by default, but can be turned off by passing the "FT_RENDER_MODE_MONO" option.
The size of the bitmap can be obtained as follows:
my ($bitmap, $left, $top) = $glyph->bitmap; my $width = length $bitmap->[0]; my $height = @$bitmap;
The optional "render_mode" argument can be any one of the following:
Only available with Freetype version 2.1.4 or newer.
Only available with Freetype version 2.1.3 or newer.
Only available with Freetype version 2.1.3 or newer.
The image is in the 'gray' format, with a depth of 8 bits.
The left and top distances in pixels are returned as well, in the same way as for the "bitmap()" method.
This method, particularly the use of the left and top offsets for correct positioning of the bitmap, is demonstrated in the magick.pl example program.
The PGM image returned is in the 'binary' format, with one byte per pixel. It is not an efficient format, but can be read by many image manipulation programs. For a detailed description of the format see <http://netpbm.sourceforge.net/doc/pgm.html>
The left and top distances in pixels are returned as well, in the same way as for the "bitmap()" method.
The render-glyph.pl example program uses this method.
The bounding box is returned as a list of four values, so the method should be called as follows:
my ($xmin, $ymin, $xmax, $ymax) = $glyph->outline_bbox();
Vector outlines of glyphs are represented by a sequence of operations. Each operation can start a new curve (by moving the imaginary pen position), or draw a line or curve from the current position of the pen to a new position. This Perl interface will walk through the outline calling subroutines (through code references you supply) for each operation. Arguments are passed to your subroutines as normal, in @_.
Note: when you intend to extract the outline of a glyph, always pass the "FT_LOAD_NO_HINTING" option when creating the face object, or the hinting will distort the outline.
The %callbacks parameter should contain three or four of the following keys, each with a reference to a "sub" as it's value. The "conic_to" handler is optional, but the others are required.
The x and y coordinates of the new pen position are supplied.
The x and y coordinates of the new pen position are supplied. Depending you how you are using this information you may have to keep track of the previous position yourself.
If you don't supply a "conic_to" handler, all conic curves will be automatically translated into cubic curves.
The x and y coordinates of the new pen position are supplied, followed by the x and y coordinates of the control point.
Cubic arcs are the ones produced in PostScript by the "curveto" operator.
The x and y coordinates of the new pen position are supplied, followed by the x and y coordinates of the first control point, then the same for the second control point.
Note that TrueType fonts use conic curves and PostScript ones use cubic curves.
The glyph-to-eps.pl example program shows how to wrap the output in enough extra code to generate a complete EPS file.
If you pass a file-handle to this method then it will write the PostScript code to that file, otherwise it will return it as a string.
The glyph-to-svg.pl example program shows how to wrap the output in enough XML to generate a complete SVG file, and one way of transforming the outline to be the right way up.
If you pass a file-handle to this method then it will write the path string to that file, otherwise it will return it as a string.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.