Version bump.
[librarian.git] / librarian / font-optimizer / Font / EOTWrapper.pm
1 # Copyright (c) 2009 Philip Taylor
2 #
3 # Permission is hereby granted, free of charge, to any person
4 # obtaining a copy of this software and associated documentation
5 # files (the "Software"), to deal in the Software without
6 # restriction, including without limitation the rights to use,
7 # copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the
9 # Software is furnished to do so, subject to the following
10 # conditions:
11 #
12 # The above copyright notice and this permission notice shall be
13 # included in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 # OTHER DEALINGS IN THE SOFTWARE.
23
24 package Font::EOTWrapper;
25
26 use strict;
27 use warnings;
28
29 use Font::TTF::Font;
30 use Encode;
31
32 use constant TTEMBED_SUBSET => 0x00000001;
33 use constant TTEMBED_TTCOMPRESSED => 0x00000004;
34 use constant TTEMBED_XORENCRYPTDATA => 0x10000000;
35 use constant DEFAULT_CHARSET => 0x01;
36
37 sub convert {
38     my ($in_fn, $out_fn) = @_;
39
40     my $font_data = do {
41         open my $fh, $in_fn or die "Failed to open $in_fn: $!";
42         binmode $fh;
43         local $/;
44         <$fh>
45     };
46
47     my $font = Font::TTF::Font->open($in_fn) or die "Failed to open $in_fn: $!";
48
49     open my $out, '>', $out_fn or die "Failed to open $out_fn: $!";
50     binmode $out;
51
52     $font->{name}->read if $font->{name};
53
54     my $os2 = $font->{'OS/2'};
55     $os2->read;
56
57     my $rootString = '';
58
59     my $header = '';
60     $header .= pack V => length($font_data);
61     $header .= pack V => 0x00020001;
62     $header .= pack V => TTEMBED_SUBSET;
63     $header .= pack C10 => map $os2->{$_}, qw(bFamilyType bSerifStyle bWeight bProportion bContrast bStrokeVariation bArmStyle bLetterform bMidline bXheight);
64     $header .= pack C => DEFAULT_CHARSET;
65     $header .= pack C => (($os2->{fsSelection} & 1) ? 1 : 0);
66     $header .= pack V => $os2->{usWeightClass};
67     $header .= pack v => $os2->{fsType};
68     $header .= pack v => 0x504C;
69     $header .= pack VVVV => map $os2->{$_}, qw(ulUnicodeRange1 ulUnicodeRange2 ulUnicodeRange3 ulUnicodeRange4);
70     $header .= pack VV => map $os2->{$_}, qw(ulCodePageRange1 ulCodePageRange2);
71     $header .= pack V => $font->{head}{checkSumAdjustment};
72     $header .= pack VVVV => 0, 0, 0, 0;
73     $header .= pack v => 0;
74     $header .= pack 'v/a*' => encode 'utf-16le' => $font->{name}->find_name(1); # family name
75     $header .= pack v => 0;
76     $header .= pack 'v/a*' => encode 'utf-16le' => $font->{name}->find_name(2); # style name
77     $header .= pack v => 0;
78     $header .= pack 'v/a*' => encode 'utf-16le' => $font->{name}->find_name(5); # version name
79     $header .= pack v => 0;
80     $header .= pack 'v/a*' => encode 'utf-16le' => $font->{name}->find_name(4); # full name
81     $header .= pack v => 0;
82     $header .= pack 'v/a*' => encode 'utf-16le' => $rootString;
83
84     $out->print(pack V => 4 + length($header) + length($font_data));
85     $out->print($header);
86     $out->print($font_data);
87
88     $font->release;
89 }
90
91 sub extract {
92     my ($in_fn, $out_fn) = @_;
93
94     my $eot_data = do {
95         open my $fh, $in_fn or die "Failed to open $in_fn: $!";
96         binmode $fh;
97         local $/;
98         <$fh>
99     };
100
101     die "Error: EOT too small" if length $eot_data < 16;
102
103     my ($eot_size, $font_data_size, $version, $flags) = unpack VVVV => substr $eot_data, 0, 16;
104
105     die "Error: Invalid EOTSize ($eot_size, should be ".(length $eot_data).")" if $eot_size != length $eot_data;
106     die "Error: Invalid Version ($version)" if not ($version == 0x00020000 or $version == 0x00020001 or $version == 0x00020002);
107     die "Error: Can't handle compressed fonts" if $flags & TTEMBED_TTCOMPRESSED;
108
109     # Skip the header fields
110     my $rest = substr $eot_data, 16+66;
111
112     my ($family_name, $style_name, $version_name, $full_name, $rest2) = unpack 'v/a* xx v/a* xx v/a* xx v/a* a*' => $rest;
113
114     my $font_data;
115     if ($version == 0x00020000) { # not 0x00010000 - spec is wrong (http://lists.w3.org/Archives/Public/www-font/2009JulSep/0862.html)
116         $font_data = $rest2;
117     } elsif ($version == 0x00020001) {
118         my ($root, $data) = unpack 'xx v/a* a*' => $rest2;
119         $font_data = $data;
120     } elsif ($version == 0x00020002) {
121         my ($root, $root_checksum, $eudc_codepage, $signature, $eudc_flags, $eudc_font, $data)
122             = unpack 'xx v/a* V V xx v/a* V v/a* a*' => $rest2;
123         $font_data = $data;
124     }
125
126     if ($flags & TTEMBED_XORENCRYPTDATA) {
127         $font_data ^= ("\x50" x length $font_data);
128     }
129
130     open my $fh, '>', $out_fn or die "Failed to open $out_fn: $!";
131     binmode $fh;
132     print $fh $font_data;
133 }
134
135 # sub rootStringChecksum {
136 #     my $s = 0;
137 #     $s += $_ for unpack 'C*', $_[0];
138 #     return $s ^ 0x50475342;
139 # }
140
141 1;