Merge tag 'nasm-2.14.01rc5'
[nasm.git] / doc / ttfmetrics.ph
blob0eec5504fb6dab00dc4fa3214c914e8272d48f9f
1 #!/usr/bin/perl
3 use Font::TTF::Font;
4 use Font::TTF::Head;
5 use Font::TTF::Hmtx;
6 use Font::TTF::Cmap;
7 use Font::TTF::Maxp;
8 use Font::TTF::PSNames;
9 use Font::TTF::Post;
11 use strict;
13 sub parse_ttf_file($) {
14 my($filename) = @_;
16 my $fontdata = {
17 widths => {},
18 kern => {}
21 my $f = Font::TTF::Font->open($filename);
23 return undef if (!defined($f));
25 $fontdata->{file} = $filename;
26 $fontdata->{type} = defined($f->{' CFF'}) ? 'otf' : 'ttf';
28 $f->{head}->read();
29 $fontdata->{scale} = $f->{head}{unitsPerEm};
31 $f->{maxp}->read();
32 my $glyphs = $f->{maxp}{numGlyphs};
34 $f->{cmap}->read();
35 $f->{hmtx}->read();
36 $f->{name}->read();
37 $fontdata->{name} = $f->{name}->find_name(6); # PostScript name
38 $f->{post}->read();
39 my $psglyphs = 0;
40 my $psmap = $f->{post}->{VAL};
41 $psmap = [] if (!defined($psmap));
42 #printf "Glyphs with PostScript names: %d\n", scalar(@$psmap);
44 # Can be done as an array of arrays in case of multiple unicodes to
45 # one glyph...
46 my @unimap = $f->{cmap}->reverse();
48 for (my $i = 0; $i < $glyphs; $i++) {
49 my $width = $f->{hmtx}->{advance}[$i];
50 my $psname = $psmap->[$i];
51 if (!defined($psname)) {
52 $psname = Font::TTF::PSNames::lookup($unimap[$i]);
54 next if (!defined($psname) || ($psname eq '.notdef'));
55 $fontdata->{widths}{$psname} = $f->{hmtx}->{advance}[$i];
58 $f->release;
60 return $fontdata;