3a7cc0e009809eb8f88629146f789123e5e0cfa3
[sheet.git] / tools / convert-ttf.pl
1 #!/usr/bin/env perl
2 use 5.010;
3 use strict;
4 use warnings;
5
6 use Data::Dump 'pp';
7 use Font::TTF::Font;
8
9 my ($ttfname, $outfile) = @ARGV;
10
11 for ($outfile || ()) {
12         !-e $_ or die "Output file $outfile already exists\n";
13         open my $output, '>', $_ or die "Cannot write to $outfile: $!\n";
14         select $output;
15 }
16
17 {
18         my $ttf = Font::TTF::Font->open($ttfname)
19                 or die "Cannot open truetype in $ttfname: $!";
20
21         my %meta = (
22                 filename => $ttfname,
23                 name     => $ttf->{name}->read->find_name(4) || $ttfname,
24                 version  => $ttf->{head}->{fontRevision},
25                 date     => (map {
26                         $_ && eval {
27                                 require Time::Piece;
28                                 Time::Piece->new($_)->datetime;
29                         }
30                 } $ttf->{head}->getdate),
31         );
32         say pp(\%meta), ',';
33
34         my $support = $ttf->{cmap}->find_ms->{val};
35         warn scalar keys %$support, " characters read from $ttfname\n";
36         say pp(sort { $a <=> $b } keys %$support);
37 }
38
39 __END__
40
41 =head1 NAME
42
43 convert-ttf.pl - Writes unicode characters present in TrueType font
44
45 =head1 SYNOPSIS
46
47     convert-ttf <font.ttf> [<output.inc.pl>]
48
49 =head1 AUTHOR
50
51 Mischa POSLAWSKY <perl@shiar.org>
52
53 =head1 LICENSE
54
55 Licensed under the GNU Affero General Public License version 3.
56