1# Render bitmaps from an outline font.
2
3use strict;
4use warnings;
5use Test::More;
6use File::Spec::Functions;
7use Font::FreeType;
8
9my @test = (
10    { char => 'A', x_sz => 72, y_sz => 72, x_res => 72, y_res => 72, aa => 0 },
11    { char => 'A', x_sz => 72, y_sz => 72, x_res => 72, y_res => 72, aa => 1 },
12    { char => 'A', x_sz => 8, y_sz => 8, x_res => 100, y_res => 100, aa => 1 },
13    { char => 'A', x_sz => 8, y_sz => 8, x_res => 100, y_res => 100, aa => 0 },
14    { char => 'A', x_sz => 8, y_sz => 8, x_res => 600, y_res => 600, aa => 0 },
15    { char => '.', x_sz => 300, y_sz => 300, x_res => 72, y_res => 72, aa => 1 },
16);
17plan tests => scalar(@test) * 3 + 2;
18
19my $data_dir = catdir(qw( t data ));
20
21# Load the TTF file.
22# Hinting is turned off, because otherwise the compile-time option to turn
23# it on (if you've licensed the patent) might otherwise make the tests fail
24# for some people.  This should make it always the same, unless the library
25# changes the rendering algorithm.
26my $vera = Font::FreeType->new->face(catfile($data_dir, 'Vera.ttf'),
27                                     load_flags => FT_LOAD_NO_HINTING);
28
29foreach (@test) {
30    my $test_filename = join('.', sprintf('%04X', ord $_->{char}),
31                             @{$_}{qw( x_sz y_sz x_res y_res aa )}) . '.pgm';
32    $test_filename = catfile($data_dir, $test_filename);
33    open my $bmp_file, '<', $test_filename
34      or die "error opening test bitmap data file '$test_filename': $!";
35    $vera->set_char_size($_->{x_sz}, $_->{y_sz}, $_->{x_res}, $_->{y_res});
36    my $glyph = $vera->glyph_from_char($_->{char});
37    my $mode = $_->{aa} ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO;
38    my ($pgm, $left, $top) = $glyph->bitmap_pgm($mode);
39    ok defined $pgm;
40    ok defined $left;
41    ok defined $top;
42}
43
44# Check that after getting an outline we can still render the bitmap.
45my $glyph = $vera->glyph_from_char_code(ord 'B');
46my $ps = $glyph->postscript;
47my ($bmp, $left, $top) = $glyph->bitmap;
48ok($ps && $bmp, 'can get both outline and then bitmap from glyph');
49
50# And the other way around.
51$glyph = $vera->glyph_from_char_code(ord 'C');
52($bmp, $left, $top) = $glyph->bitmap;
53$ps = $glyph->postscript;
54ok($ps && $bmp, 'can get both bitmap and then outline from glyph');
55
56# vim:ft=perl ts=4 sw=4 expandtab:
57