File: fontbb.pl

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 (135 lines) | stat: -rw-r--r-- 3,111 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/perl

use v5.36;
use Object::Pad;
use utf8;

my $verbose = 1;

@ARGV = ( "Times-Roman",
	  "FreeSerif.ttf",
	  "DejaVuSerif.ttf",
	  "ArialMT.ttf",
	  "MuseJazzText.otf",
	  "MuseJazzChord.ttf" ) unless @ARGV;

use PDF::API2;

sub max { $_[0] >= $_[1] ? $_[0] : $_[1] }
sub min { $_[0] <= $_[1] ? $_[0] : $_[1] }

my $pdf  = PDF::API2->new;
my $page = $pdf->page;
my $gfx  = $page->gfx;

PDF::API2::addFontDirs( $ENV{HOME}."/.fonts", "/usr/share/fonts/" );

my $x0 = 70;
my $y0 = 650;

# Annotations.
my $af = $pdf->font("Helvetica");
my $as = 8;

my $scale = 0.08;
my $dx = 3000 * $scale;
my $dy = 2500 * $scale;
my $sz = 1000 * $scale;
my $dd =  400 * $scale;

my ( $x, $y ) = ( $x0, $y0 );

for my $fn ( @ARGV ) {

    next unless $fn;

    my $font = $pdf->font($fn);
    # PDF::API2 units are 1/1000 em.
    my @xbb = $font->fontbbox;
    push( @xbb, $font->ascender, $font->descender );
    my $xbb = sprintf( "%d %d | %d %d | %d %d", @xbb );
    warn("$fn: $xbb\n");

    # Scale.
    my @bb = map { $_ * $scale } @xbb;
    my $o = $pdf->xo_form;

    $o->line_width(0.5);
    $o->fill_color('black');
    $o->stroke_color('black');

    # Background (first glyph).
    $o->fill_color("yellow");
    $o->rectangle( 0, $bb[1],
		   $sz*$font->width("Á"), $bb[3] );
    $o->fill;

    # Crosslines for origin.
    $o->stroke_color('lightgreen');
    $o->move( -$dd, 0 );
    $o->hline($dd);
    $o->stroke;
    $o->move( 0, -$dd );
    $o->vline($dd);
    $o->stroke;

    # Font bounding box.
    $o->stroke_color("blue");
    $o->fill_color("blue");
    $o->rectangle(@bb[0..3]);
    $o->stroke;
    $o->textstart;
    $o->font( $af, $as );
    $o->translate( $bb[0]+1, $bb[3]+3 );
    $o->text("bounding box");
    $o->translate( $bb[0]+1, $bb[1]-8 );
    $o->text("$xbb[0] $xbb[1]  $fn");
    $o->translate( $bb[2], $bb[3]+3 );
    $o->text("$xbb[2] $xbb[3]", align => "right" );
    $o->textend;

    # Ascender and descender.
    $o->stroke_color("red");
    $o->fill_color("red");
    $o->move( -$dd, $bb[4] );
    $o->hline( $bb[2]+$dd );
    $o->stroke;
    $o->move( -$dd, $bb[5]  );
    $o->hline( $bb[2]+$dd );
    $o->stroke;
    $o->textstart;
    $o->font( $af, $as );
    $o->translate( $bb[2]+$dd, $bb[4]+2 );
    $o->text( "ascender", align => "right" );
    $o->translate( $bb[2]+$dd, $bb[4]-8 );
    $o->text( $xbb[4], align => "right" );
    $o->translate( $bb[2]+$dd, $bb[5]+2 );
    $o->text( "descender", align => "right" );
    $o->translate( $bb[2]+$dd, $bb[5]-8 );
    $o->text( $xbb[5], align => "right" );
    $o->textend;

    # Sample glyphs (with max asc and desc).
    $o->fill_color("black");
    $o->stroke_color("black");
    $o->textstart;
    $o->font( $font, $sz );
    $o->translate( 0, 0 );
    $o->text( "Ág" );
    $o->textend;
    my @obb = ( min($bb[0],-$dd), $bb[1]-10, $bb[2]+$dd, $bb[3]+9 );
#    $o->rectangle(@obb); $o->stroke;
    $o->bbox(@obb);

    if ( $x + $bb[2] + $dd > 590 ) {
	$x = $x0;
	$y -= $dy;
    }

    warn("X: [@{[$o->bbox]}] @ $x,$y\n");
    $gfx->object( $o, $x, $y, 1, 1 );
    $x += $dx;

}

$pdf->saveas("fontbb.pdf");