[Perl]Font::Freetype 模块安装 以及 示例

回复
头像
523066680
Administrator
Administrator
帖子: 518
注册时间: 2016年07月19日 12:14
联系:

[Perl]Font::Freetype 模块安装 以及 示例

帖子 523066680 »

转载请注明出处:http://code-by.org/viewtopic.php?f=59&t=283
By 523066680@163.com

[Perl]Font::Freetype 模块安装 以及 示例错误提示
  • Font-FreeType-0.07>perl Makefile.PL
    Build config: default
    Build flag LIB: -lfreetype
    Build flag INC: -I/usr/include/freetype2
    Can't link/include C library 'ft2build.h', 'freetype', aborting.
解决方法:
  • 找到
    $config->{default}{INC} = '-I/usr/include/freetype2';
    改为
    $config->{default}{INC} = '-IC:/Strawberry/c/include/freetype2';
    然后
    > perl Makefile.PL
    > dmake
    > dmake install
[Finished in 0.2s]
头像
523066680
Administrator
Administrator
帖子: 518
注册时间: 2016年07月19日 12:14
联系:

[Perl][Font::Freetype]枚举字体支持的字符和字符名称

帖子 523066680 »

use Font::FreeType;

my $filename = "C:/Windows/Fonts/Couri.ttf";
my $face = Font::FreeType->new->face($filename);

$face->foreach_char(
sub {
printf("%s\t%s\n", $_->char_code, $_->name) if (defined $_->name );
}
);
部分输出:
> 32 space
> 33 exclam
> 34 quotedbl
> 35 numbersign
> 36 dollar
> 37 percent

备注:
sub { block } 是一个匿名函数,也是回调函数,foreach_char 每次迭代都调用这个函数。
头像
523066680
Administrator
Administrator
帖子: 518
注册时间: 2016年07月19日 12:14
联系:

[Perl]Font::Freetype 生成字符点阵的极简示例代码

帖子 523066680 »

=info
生成字符点阵的极简示例代码
编辑: 523066680, Code-By.Org
改自 examples/render-glyph.pl
=cut

use Data::Dump qw/dump/;
use Font::FreeType;

my ($filename, $char, $size) = ("C:/windows/fonts/arial.ttf", 'A', 20);
my $dpi = 100;

my $face = Font::FreeType->new->face($filename);
$face->set_char_size($size, $size, $dpi, $dpi);

$char = ord($char);
my $glyph = $face->glyph_from_char_code($char);
die "No glyph for character '$char'.\n" if (! $glyph);

print dump $glyph->bitmap;
输出:
>( > [ > pack("H*","0000000000000012f9fff81100000000000000"), > pack("H*","000000000000006affffff6900000000000000"), > pack("H*","00000000000000c8ffa2ffc700000000000000"), > pack("H*","00000000000026ffed0deeff25000000000000"), > pack("H*","00000000000084ff960098ff83000000000000"), > pack("H*","000000000001e0ff38003dffe0010000000000"), > pack("H*","000000000041ffdb000001dfff400000000000"), > pack("H*","00000000009fff8000000084ff9e0000000000"), > pack("H*","000000000af2ff2500000027fff20900000000"), > pack("H*","000000005bffc90000000000cbff5a00000000"), > pack("H*","00000000baff6d00000000006fffb800000000"), > pack("H*","0000001afdfb15000000000016fbfc1a000000"), > pack("H*","00000076ffffffffffffffffffffff75000000"), > pack("H*","000000d3ffffffffffffffffffffffd3000000"), > pack("H*","000032ffec070000000000000009efff310000"), > pack("H*","000091ff8d00000000000000000092ff8f0000"), > pack("H*","0004e9ff270000000000000000002bffe90400"), > pack("H*","004dffc00000000000000000000000c5ff4c00"), > pack("H*","00abff5900000000000000000000005effaa00"), > pack("H*","10f8ec07000000000000000000000008eef810"), > ], > 0, > 20, >)
(想不到 Data::Dump::dump 还自带优化输出效果,本来只是用来观察glyph返回的结构)
在 Perl 中学习一个陌生的对象,可以通过 Data::Dump 输出对象结构,也可以通过 Perldoc 查阅相关文档:
`perldoc Font::Freetype::Glyph`
头像
523066680
Administrator
Administrator
帖子: 518
注册时间: 2016年07月19日 12:14
联系:

生成字符点阵2

帖子 523066680 »

=info
Perl Font::Freetype 生成字符点阵
编辑: 523066680, Code-By.Org
改自 examples/render-glyph.pl
=cut
use Font::FreeType;

my ($filename, $char, $size) = ("C:/windows/fonts/arial.ttf", 'A', 12);
my $dpi = 100;

my $face = Font::FreeType->new->face($filename);
$face->set_char_size($size, $size, $dpi, $dpi);

$char = ord($char);
my $glyph = $face->glyph_from_char_code($char);

my ($bitmap, $left, $top) = $glyph->bitmap();

for my $y ( @$bitmap )
{
grep { printf "%3d", ord($_) } split(//, $y);
print "\n";
}
. 0 0 0 0 0 74255 74 0 0 0 0 0 0 0 0 0 0182151185 0 0 0 0 0 0 0 0 0 35222 5223 40 0 0 0 0 0 0 0 0141121 0121149 0 0 0 0 0 0 0 10232 23 0 22238 15 0 0 0 0 0 0101168 0 0 0169114 0 0 0 0 0 0208 64 0 0 0 64222 2 0 0 0 0 60255255255255255255255 79 0 0 0 0168 99 0 0 0 0 0101189 0 0 0 24223 6 0 0 0 0 0 7230 44 0 0127115 0 0 0 0 0 0 0119154 0 4212 12 0 0 0 0 0 0 0 14230 18
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 0 访客