X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/ef7911fba9c330552599bc6eb9dc22606246dd7e..68b03397a0872d10d3627cea2b92ae36bd59183c:/font-optimizer/subset.pl?ds=inline diff --git a/font-optimizer/subset.pl b/font-optimizer/subset.pl new file mode 100755 index 0000000..fb2eb95 --- /dev/null +++ b/font-optimizer/subset.pl @@ -0,0 +1,131 @@ +#!/usr/bin/perl -CA + # use the -CA flag so @ARGV is interpreted as UTF-8 + +use strict; +use warnings; + +use lib 'ext/Font-TTF/lib'; +use Font::Subsetter; + +use Getopt::Long; + +main(); + +sub help { + print < \$chars, + 'charsfile=s' => \$charsfile, + 'verbose' => \$verbose, + 'include=s' => \$include, + 'exclude=s' => \$exclude, + 'apply=s' => \$apply, + 'licensesubst=s' => \$license_desc_subst, + ) or help(); + + if (defined $chars and defined $charsfile) { + print "ERROR: Only one of '--chars' and --charsfile' can be specified\n\n"; + help(); + } elsif (defined $chars) { + # just use $chars + } elsif (defined $charsfile) { + open my $f, '<', $charsfile or die "Failed to open $charsfile: $!"; + binmode $f, ':utf8'; + local $/; + $chars = <$f>; + } else { + $chars = 'test'; + } + + @ARGV == 2 or help(); + + my ($input_file, $output_file) = @ARGV; + + + if ($verbose) { + dump_sizes($input_file); + print "Generating subsetted font...\n\n"; + } + + my $features; + if ($include) { + $features = { DEFAULT => 0 }; + $features->{$_} = 1 for split /,/, $include; + } elsif ($exclude) { + $features = { DEFAULT => 1 }; + $features->{$_} = 0 for split /,/, $exclude; + } + + my $fold_features; + if ($apply) { + $fold_features = [ split /,/, $apply ]; + } + + my $subsetter = new Font::Subsetter(); + $subsetter->subset($input_file, $chars, { + features => $features, + fold_features => $fold_features, + license_desc_subst => $license_desc_subst, + }); + $subsetter->write($output_file); + + if ($verbose) { + print "\n"; + print "Features:\n "; + print join ' ', $subsetter->feature_status(); + print "\n\n"; + print "Included glyphs:\n "; + print join ' ', $subsetter->glyph_names(); + print "\n\n"; + dump_sizes($output_file); + } + + $subsetter->release(); +} + +sub dump_sizes { + my ($filename) = @_; + my $font = Font::TTF::Font->open($filename) or die "Failed to open $filename: $!"; + print "TTF table sizes:\n"; + my $s = 0; + for (sort keys %$font) { + next if /^ /; + my $l = $font->{$_}{' LENGTH'}; + $s += $l; + print " $_: $l\n"; + } + print "Total size: $s bytes\n\n"; + $font->release(); +}