perl - 是否有一个 Perl 库可以将字体绘制为多边形?
问题描述
我想将文本绘制为多边形,并获得一个包含 (x1,y1)-(x2,y2) 对的线段数组,我可以在矢量绘图应用程序中进行缩放和使用。一种应用可能是用 CNC 编写文本。
因此,例如:
$f = PolyFont->new("Hello World");
@lines = $f->get_lines();
这可能会提供@lines = ([x1,y1],[x2,y2]) 值或类似内容的列表。
字体不必特别漂亮,用线段逼近的话,甚至不需要支持曲线。
如果它会接受一个TTF,那就更好了!
想法?
解决方案
您可以使用该Font::FreeType
模块将字形的轮廓作为一系列线段和贝塞尔弧来获取。这是一个示例,我使用以下方法将大纲保存到新.png
文件中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);
输出:
备注:
- 您可以
Vera.ttf
从这里下载字体文件:https ://www.dafont.com/bitstream_vera_sans.font - 此示例基于CPAN 发行版中的示例脚本magick.pl 。
Font::FreeType
推荐阅读
- xml - XQuery SQL Server NVARCHAR 列
- excel - VBA脚本将工作表中具有相同名称的所有内容从一个工作簿覆盖到另一个工作簿
- python-3.x - 如何按不同列合并数据框行
- javascript - 如何停止复制下拉列表的日期?我使用了一个while循环
- powershell - PowerShell While循环多个条件不起作用
- karate - 试图断言 2 个动态响应
- python - 如何使用包含我要访问的索引号的数组获取 numpy 数组中的所有值
- javascript - 在添加为选项卡的“内容页面”中获取 Microsoft Team 上下文
- mysql - 查询不工作。也许“分组依据”有问题。怎么了?
- yocto - rootfs 中的 Yocto helloword 配方