Font-FreeType
view release on metacpan or search on metacpan
lib/Font/FreeType/Glyph.pm view on Meta::CPAN
my $ps = '';
$self->outline_decompose_({
move_to => sub {
$s = "$_[0] $_[1] moveto\n";
if ($fh) { print $fh $s } else { $ps .= $s }
},
line_to => sub {
$s = "$_[0] $_[1] lineto\n";
if ($fh) { print $fh $s } else { $ps .= $s }
},
cubic_to => sub {
$s = "$_[2] $_[3] $_[4] $_[5] $_[0] $_[1] curveto\n";
if ($fh) { print $fh $s } else { $ps .= $s }
},
});
return $fh ? $self : $ps;
}
sub svg_path
{
my ($self, $fh) = @_;
my $s;
my $path = '';
$self->outline_decompose_({
move_to => sub {
$s .= "M$_[0] $_[1]\n";
if ($fh) { print $fh $s } else { $path .= $s }
},
line_to => sub {
$s = "L$_[0] $_[1]\n";
if ($fh) { print $fh $s } else { $path .= $s }
},
cubic_to => sub {
$s = "C$_[2] $_[3] $_[4] $_[5] $_[0] $_[1]\n";
if ($fh) { print $fh $s } else { $path .= $s }
},
conic_to => sub {
$s = "Q$_[2] $_[3] $_[0] $_[1]\n";
if ($fh) { print $fh $s } else { $path .= $s }
},
});
return $fh ? $self : $path;
}
sub bitmap_pgm
{
my $self = shift;
my ($bmp, $left, $top) = $self->bitmap(@_);
my $wd = length $bmp->[0];
my $ht = @$bmp;
return ("P5\n$wd $ht\n255\n" . join('', @$bmp), $left, $top);
}
sub bitmap_magick
{
require Image::Magick;
my $self = shift;
my ($bmp, $left, $top) = $self->bitmap(@_);
my $wd = length $bmp->[0];
my $ht = @$bmp;
my $img = Image::Magick->new(magick=>'gray', size => "${wd}x$ht",
depth => 8);
my $err = $img->BlobToImage(join '', @$bmp);
croak "error creating Image::Magick object: $err" if $err;
return ($img, $left, $top);
}
1;
__END__
=head1 NAME
Font::FreeType::Glyph - glyphs from font typefaces loaded from Font::FreeType
=head1 SYNOPSIS
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 { ... },
);
=head1 DESCRIPTION
This class represents an individual glyph (character image) loaded from
a font. See L<Font::FreeType::Face|Font::FreeType::Face> for how to
obtain a glyph object, in particular the C<glyph_from_char_code()>
and C<glyph_from_char()> methods.
Things you an do with glyphs include:
=over 4
=item *
Get metadata about the glyph, such as the size of its image and other
metrics.
=item *
Render a bitmap image of the glyph (if it's from a vector font) or
extract the existing bitmap (if it's from a bitmap font), using the
C<bitmap()> method.
=item *
Extract a precise description of the lines and curves that make up
the glyph's outline, using the C<outline_decompose()> method.
lib/Font/FreeType/Glyph.pm view on Meta::CPAN
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 C<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 C<render_mode> argument can be any one of the following:
=over 4
=item FT_RENDER_MODE_NORMAL
The default. Uses antialiasing.
=item FT_RENDER_MODE_LIGHT
Changes the hinting algorithm to make the glyph image closer to it's
real shape, but probably more fuzzy.
Only available with Freetype version 2.1.4 or newer.
=item FT_RENDER_MODE_MONO
Render with antialiasing disabled. Each pixel will be either 0 or 255.
=item FT_RENDER_MODE_LCD
Render in colour for an LCD display, with three times as many pixels
across the image as normal. This mode probably won't work yet.
Only available with Freetype version 2.1.3 or newer.
=item FT_RENDER_MODE_LCD_V
Render in colour for an LCD display, with three times as many rows
down the image as normal. This mode probably won't work yet.
Only available with Freetype version 2.1.3 or newer.
=back
=item bitmap_magick([I<render_mode>])
A simple wrapper around the C<bitmap()> method. Renders the bitmap as
normal and returns it as an L<Image::Magick|Image::Magick> object,
which can then be composited onto a larger bitmapped image, or manipulated
using any of the features available in Image::Magick.
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 C<bitmap()> method.
This method, particularly the use of the left and top offsets for
correct positioning of the bitmap, is demonstrated in the
I<magick.pl> example program.
=item bitmap_pgm([I<render_mode>])
A simple wrapper around the C<bitmap()> method. It renders the bitmap
and constructs it into a PGM (portable grey-map) image file, which it
returns as a string. The optional I<render-mode> is passed directly
to the C<bitmap()> method.
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 L<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 C<bitmap()> method.
The I<render-glyph.pl> example program uses this method.
=item char_code()
The character code (in Unicode) of the glyph. Could potentially
return codes in other character sets if the font doesn't have a Unicode
character mapping, but most modern fonts do.
=item has_outline()
True if the glyph has a vector outline, in which case it is safe to
call C<outline_decompose()>. Otherwise, the glyph only has a bitmap
image.
=item height()
The height of the glyph.
=item horizontal_advance()
The distance from the origin of this glyph to the place where the next
glyph's origin should be. Only applies to horizontal layouts. Always
positive, so for right-to-left text (such as Hebrew) it should be
subtracted from the current glyph's position.
=item index()
The glyph's index number in the font. This number is determined
by the FreeType library, and so isn't necessarily the same as any
special index number used by the font format.
=item left_bearing()
The left side bearing, which is the distance from the origin to
the left of the glyph image. Usually positive for horizontal layouts
( run in 4.273 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )