1 # Copyright (c) 2009 Philip Taylor
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
12 # The above copyright notice and this permission notice shall be
13 # included in all copies or substantial portions of the Software.
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.
24 package Font::EOTWrapper;
32 use constant TTEMBED_SUBSET => 0x00000001;
33 use constant TTEMBED_TTCOMPRESSED => 0x00000004;
34 use constant TTEMBED_XORENCRYPTDATA => 0x10000000;
35 use constant DEFAULT_CHARSET => 0x01;
38 my ($in_fn, $out_fn) = @_;
41 open my $fh, $in_fn or die "Failed to open $in_fn: $!";
47 my $font = Font::TTF::Font->open($in_fn) or die "Failed to open $in_fn: $!";
49 open my $out, '>', $out_fn or die "Failed to open $out_fn: $!";
52 $font->{name}->read if $font->{name};
54 my $os2 = $font->{'OS/2'};
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;
84 $out->print(pack V => 4 + length($header) + length($font_data));
86 $out->print($font_data);
92 my ($in_fn, $out_fn) = @_;
95 open my $fh, $in_fn or die "Failed to open $in_fn: $!";
101 die "Error: EOT too small" if length $eot_data < 16;
103 my ($eot_size, $font_data_size, $version, $flags) = unpack VVVV => substr $eot_data, 0, 16;
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;
109 # Skip the header fields
110 my $rest = substr $eot_data, 16+66;
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;
115 if ($version == 0x00020000) { # not 0x00010000 - spec is wrong (http://lists.w3.org/Archives/Public/www-font/2009JulSep/0862.html)
117 } elsif ($version == 0x00020001) {
118 my ($root, $data) = unpack 'xx v/a* a*' => $rest2;
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;
126 if ($flags & TTEMBED_XORENCRYPTDATA) {
127 $font_data ^= ("\x50" x length $font_data);
130 open my $fh, '>', $out_fn or die "Failed to open $out_fn: $!";
132 print $fh $font_data;
135 # sub rootStringChecksum {
137 # $s += $_ for unpack 'C*', $_[0];
138 # return $s ^ 0x50475342;