From 99753a4a0edc3571fa82cee09e2b0c74618e58cb Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Mon, 11 May 2020 20:54:38 +0000 Subject: fontools (11may20) git-svn-id: svn://tug.org/texlive/trunk@55105 c570f23f-e606-0410-a88d-b1316a301751 --- Master/texmf-dist/doc/support/fontools/splitttc | 99 +++++++++++++++++++------ 1 file changed, 77 insertions(+), 22 deletions(-) (limited to 'Master/texmf-dist/doc/support') diff --git a/Master/texmf-dist/doc/support/fontools/splitttc b/Master/texmf-dist/doc/support/fontools/splitttc index 5c6d6037ec5..19c7781427f 100755 --- a/Master/texmf-dist/doc/support/fontools/splitttc +++ b/Master/texmf-dist/doc/support/fontools/splitttc @@ -33,11 +33,12 @@ use strict; use warnings; +use Encode; use File::Basename; use Getopt::Long; use Pod::Usage; -my $VERSION = "20200428"; +my $VERSION = "20200511"; parse_commandline(); @@ -47,8 +48,7 @@ use constant { }; my $filename_in = $ARGV[0]; -my ($basename_out, undef, undef) = fileparse($filename_in, qw(.ttc .otc)); -my $output_prefix = $ARGV{output_prefix} // $basename_out; +my ($basename, undef, undef) = fileparse($filename_in, qw(.ttc .otc)); open my $fh, '<:raw', $filename_in or die "[ERROR] cannot open $filename_in: $!"; @@ -79,7 +79,7 @@ for my $i_font (1..$num_fonts) { : $sfnt_version eq "\x00\x01\x00\x00" ? 'ttf' : die ; - my $filename_out = sprintf "%s%d.%s", $output_prefix, $i_font, $file_ext; + my $filename_out = sprintf "%s%d.%s", $basename, $i_font, $file_ext; # unpack Table Records and create list of tables my $table_records @@ -108,6 +108,13 @@ for my $i_font (1..$num_fonts) { for my $table_record (@table_records) { $table_record->{offset} = $file_pos; $file_pos += length $table_record->{table}; + + if ($table_record->{table_tag} eq 'name') { + my $postscript_name = get_postscript_name($table_record->{table}); + if ($postscript_name) { + $filename_out = $postscript_name . q(.) . $file_ext; + } + } } open my $filehandle_out, '>:raw', $filename_out @@ -126,9 +133,10 @@ for my $i_font (1..$num_fonts) { or die "[ERROR] something went wrong when closing $filename_out: $!"; } -#----------------------------------------------------------------------- -# Read the command-line options -#----------------------------------------------------------------------- + +# -------------------------------------------------------------------------- +# Reads the command-line options. +# -------------------------------------------------------------------------- sub parse_commandline { Getopt::Long::GetOptions( 'help|?' => sub { pod2usage(-verbose => 1) }, @@ -143,6 +151,53 @@ sub parse_commandline { } +# -------------------------------------------------------------------------- +# Parses the 'name' table and returns the font's PostScript name. +# -------------------------------------------------------------------------- +sub get_postscript_name { + my $name_table = shift; + + my ($version, $count, $string_offset) = unpack '@0n3', $name_table; + if ($version > 1) { + # Don't try to parse future versions + return; + } + + my @name_records = unpack "\@6(a12)$count", $name_table; + for my $name_record (@name_records) { + my ($platform_id, $encoding_id, $language_id, + $name_id, $length, $offset) = unpack 'n6', $name_record; + + if ($name_id != 6) { + next; + } + + my $postscript_data + = substr $name_table, $string_offset + $offset, $length; + my $postscript_name; + if ($platform_id == 3 + and $encoding_id == 1 + and $language_id == 0x409) { + $postscript_name = Encode::decode('UTF-16BE', $postscript_data); + } + elsif ($platform_id == 1 + and $encoding_id == 0 + and $language_id == 0) { + $postscript_name = Encode::decode('MacRoman', $postscript_data); + } + else { + # According to the 'Recommendations' in the OpenType spec, + # any PostScript names other than the above two may be ignored. + next; + } + + return $postscript_name; + } + + return; +} + + __END__ @@ -166,7 +221,6 @@ splitttc - split an OpenType Collection F or F file =item B [B<-help>] [B<-version>] -[B<-output-prefix>=I<< >>] B<< >> =back @@ -187,6 +241,7 @@ cannot handle such OpenType Collections; they only work for individual fonts. B takes an OpenType Collection file and splits it into its constituent parts. + =head1 OPTIONS AND ARGUMENTS =over 4 @@ -199,12 +254,6 @@ Print a short description of the syntax and exit. Print version number and exit. -=item B<-output-prefix>=I<< >> - -Write the individual fonts to files named I<< >>1.ttf, -I<< >>2.ttf etc. -The default is to take the prefix from the filename of the input file. - =item B<< >> The F or F file to be split. @@ -215,6 +264,15 @@ You may use either one or two dashes before options, and option names may be shortened to a unique prefix. +=head1 OUTPUT + +B tries to determine the 'PostScript name' of the resulting fonts +and uses this to name the output files. +If it cannot determine the PostScript name, it uses the basename of the input +font collection file plus a three-digit sequence number: +F<<< I<< >> 001.otf >>>, F<<< I<< >> 002.otf >>> etc. + + =head1 AUTHOR Marc Penninga @@ -245,7 +303,7 @@ See the GNU General Public License for more details. =head1 VERSION -This document describes B version 20200428. +This document describes B version 20200511. =head1 RECENT CHANGES @@ -254,16 +312,13 @@ This document describes B version 20200428. =over 12 -=item I<2019-06-25> - -First release. - -=back +=item I<2020-05-11> +Use the 'PostScript name' to name the output fonts. -=begin Really_old_history +=item I<2019-06-25> -=over 12 +First release. =back -- cgit v1.2.3