You can use the Font::FreeType
module to get the outline of a glyph as a series of line segments and Bézier arcs. Here is an example, where I save the outline to a new .png
file using Image::Magick
:
use feature qw(say);
use strict;
use warnings;
use Font::FreeType;
use Image::Magick;
my $size = 72;
my $dpi = 600;
my $font_filename = 'Vera.ttf';
my $char = 'A';
my $output_filename = $char . '-outline.png';
my $face = Font::FreeType->new->face($font_filename);
$face->set_char_size($size, $size, $dpi, $dpi);
my $glyph = $face->glyph_from_char($char);
my $width = $glyph->horizontal_advance;
my $height = $glyph->vertical_advance;
my $img = Image::Magick->new(size => "${width}x$height");
$img->Read('xc:#ffffff');
$img->Set(stroke => '#8888ff');
my $curr_pos;
$glyph->outline_decompose(
move_to => sub {
my ($x, $y) = @_;
$y = $height - $y;
$curr_pos = "$x,$y";
},
line_to => sub {
my ($x, $y) = @_;
$y = $height - $y;
$img->Draw(primitive => 'line', linewidth => 5, points => "$curr_pos $x,$y");
$curr_pos = "$x,$y";
},
cubic_to => sub {
my ($x, $y, $cx1, $cy1, $cx2, $cy2) = @_;
$y = $height - $y;
$cy1 = $height - $cy1;
$cy2 = $height - $cy2;
$img->Draw(primitive => 'bezier',
points => "$curr_pos $cx1,$cy1 $cx2,$cy2 $x,$y");
$curr_pos = "$x,$y";
},
);
$img->Write($output_filename);
Output:

Notes: