File: 200-extents.t

package info (click to toggle)
libtext-layout-perl 0.045-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 548 kB
  • sloc: perl: 4,117; makefile: 2
file content (83 lines) | stat: -rw-r--r-- 1,937 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#! perl

use strict;
use warnings;
use utf8;
use Test::More;
if ( eval { require PDF::API2 } ) {
    plan tests => 12;
}
else {
    plan skip_all => "PDF::API2 not installed";
}

# Currently the extents method is implemented by the PDFAPI2 backend.
use Text::Layout::PDFAPI2;

-d "t" && chdir("t");

#### All this has been tested.
# Create PDF document, with a page and text content.
my $pdf = PDF::API2->new;

# Create a font.
# Polo-SemiScript was one of the smallest files I could find.
my $fontfile = "Polo-SemiScript.ttf";
ok( -s $fontfile, "have $fontfile" );

my $font;
eval { $font = $pdf->ttfont($fontfile)};
diag($@) if $@;
ok( $font, "have font" );

my $text = "The quick brown fox";

my $e = $font->extents( $text, 64 );
ok( $e, "have extents" );

my $exp = { x     =>   1.6,   y      => -12.16,
	    width => 550.336, height =>  56.96,
	    bl    =>  47.104};

for ( qw( x y width height bl ) ) {
    fuzz( $e->{$_}, $exp->{$_}, "extent $_ $e->{$_}" );
}
augment($exp);
for ( qw( xMin yMin xMax yMax ) ) {
    fuzz( $e->{$_}, $exp->{$_}, "extent $_ $e->{$_}" );
}

# visualize( $text, $e );

sub visualize {
    my ( $txt, $e ) = ( @_ );
    my $text = (my $page = $pdf->page)->text;
    my $gfx = $page->gfx;

    $gfx->translate(50,600);
    $gfx->linewidth(1);
    $gfx->rectxy( $e->{xMin}, $e->{yMin}, $e->{xMax}, $e->{yMax} );
    $gfx->strokecolor("cyan");
    $gfx->stroke;
    $gfx->linewidth(0.5);
    $gfx->rect( $e->{x}, $e->{y}, $e->{width}, $e->{height} );
    $gfx->line( $e->{x}, 0, $e->{x}+$e->{width}, 0 );
    $gfx->strokecolor("magenta");
    $gfx->stroke;

    $text->font($font,64);
    $text->translate(50,600);
    $text->text($txt);

    $pdf->save("200.pdf");
}

sub fuzz { ok( $_[0] < $_[1]+0.01 && $_[0] > $_[1]-0.01, $_[2] ) }

sub augment {
    my $e = shift;
    $e->{xMin} = $e->{x};
    $e->{yMin} = $e->{y};
    $e->{xMax} = $e->{x} + $e->{width};
    $e->{yMax} = $e->{y} + $e->{height};
}