font: generate data for apple fonts from OS X
[sheet.git] / tools / convert-allfonts
index e7d1964bf835c439f2f227fcec80a3e63158af62..5f0c6aa8cd84de36089c58a52f6ccae540a88af4 100755 (executable)
@@ -1,51 +1,83 @@
-#!/bin/sh
+#!/usr/bin/env perl
+use strict;
+use warnings;
+no warnings 'syntax';  # if assignment
 
-cd $(dirname $0)
+use List::Util 'first';
+use File::Basename 'dirname';
 
-CONVBIN=./convert-ttf.pl
-OUTDIR=../ttfsupport
-TTFDIR=~/.fonts
+chdir dirname $0;
 
-# microsoft
+my $convbin = './mkttfinfo';
+my $outdir = '../ttfsupport';
+my @ttfpath = ('~/.fonts', '../data/osx-fonts');  # local fallbacks
 
-TTFDIR_MS=/usr/share/fonts/truetype/msttcorefonts
-if [ -e $TTFDIR_MS ]; then
-$CONVBIN $TTFDIR_MS/Verdana.ttf $OUTDIR/verdana.inc.pl
-$CONVBIN $TTFDIR_MS/Times_New_Roman.ttf $OUTDIR/times.inc.pl
-$CONVBIN $TTFDIR_MS/Arial.ttf $OUTDIR/arial.inc.pl
-$CONVBIN $TTFDIR_MS/Courier_New.ttf $OUTDIR/courier.inc.pl
-$CONVBIN $TTFDIR_MS/Comic_Sans_MS.ttf $OUTDIR/comic.inc.pl
-$CONVBIN $TTFDIR_MS/Georgia.ttf $OUTDIR/georgia.inc.pl
-fi
+if (my $mspath = '/usr/share/fonts/truetype/msttcorefonts') {
+       if (my $src = first { -e } map { glob "$_/Verdana.ttf" } $mspath, @ttfpath) {
+               system $convbin, $src, "$outdir/verdana.inc.pl";
+       }
+       if (my $src = first { -e } map { glob "$_/Times_New_Roman.ttf" } $mspath, @ttfpath) {
+               system $convbin, $src, "$outdir/times.inc.pl";
+       }
+       if (my $src = first { -e } map { glob "$_/Arial.ttf" } $mspath, @ttfpath) {
+               system $convbin, $src, "$outdir/arial.inc.pl";
+       }
+       if (my $src = first { -e } map { glob "$_/Courier_New.ttf" } $mspath, @ttfpath) {
+               system $convbin, $src, "$outdir/courier.inc.pl";
+       }
+       if (my $src = first { -e } map { glob "$_/Comic_Sans_MS.ttf" } $mspath, @ttfpath) {
+               system $convbin, $src, "$outdir/comic.inc.pl";
+       }
+       if (my $src = first { -e } map { glob "$_/Georgia.ttf" } $mspath, @ttfpath) {
+               system $convbin, $src, "$outdir/georgia.inc.pl";
+       }
+}
 
-# bitstream
+if (my $ampath = '/usr/share/fonts/truetype/macfonts') {
+       if (my $src = first { -e } map { glob "$_/Lucida?Grande.ttf" } $ampath, @ttfpath) {
+               system $convbin, $src, "$outdir/lucida.inc.pl";
+       }
+       if (my $src = first { -e } map { glob "$_/AppleGaramond.ttf" } $ampath, @ttfpath) {
+               system $convbin, $src, "$outdir/garamond.inc.pl";
+       }
+}
 
-TTFDIR_DV=/usr/share/fonts/truetype/ttf-dejavu
-if [ -e $TTFDIR_DV ]; then
-$CONVBIN $TTFDIR_DV/DejaVuSans.ttf $OUTDIR/dvsans.inc.pl
-$CONVBIN $TTFDIR_DV/DejaVuSerif.ttf $OUTDIR/dvserif.inc.pl
-$CONVBIN $TTFDIR_DV/DejaVuSansMono.ttf $OUTDIR/dvmono.inc.pl
-fi
+if (my $dvpath = '/usr/share/fonts/truetype/ttf-dejavu') {
+       if (my $src = first { -e } map { glob "$_/DejaVuSans.ttf" } $dvpath, @ttfpath) {
+               system $convbin, $src, "$outdir/dvsans.inc.pl";
+       }
+       if (my $src = first { -e } map { glob "$_/DejaVuSerif.ttf" } $dvpath, @ttfpath) {
+               system $convbin, $src, "$outdir/dvserif.inc.pl";
+       }
+       if (my $src = first { -e } map { glob "$_/DejaVuSansMono.ttf" } $dvpath, @ttfpath) {
+               system $convbin, $src, "$outdir/dvmono.inc.pl";
+       }
+}
 
-# google
+if (my $gdpath = '/usr/share/fonts/truetype/droid') {
+       if (my $src = first { -e } map { glob "$_/DroidSans.ttf" } $gdpath, @ttfpath) {
+               system $convbin, $src, "$outdir/droidsans.inc.pl";
+       }
+       if (my $src = first { -e } map { glob "$_/DroidSerif.ttf" } $gdpath, @ttfpath) {
+               system $convbin, $src, "$outdir/droidserif.inc.pl";
+       }
+       if (my $src = first { -e } map { glob "$_/DroidSansMono.ttf" } $gdpath, @ttfpath) {
+               system $convbin, $src, "$outdir/droidmono.inc.pl";
+       }
+}
 
-TTFDIR_GD=/usr/share/fonts/truetype/droid
-if [ -e $TTFDIR_GD ]; then
-$CONVBIN $TTFDIR_DV/DroidSans.ttf $OUTDIR/droidsans.inc.pl
-$CONVBIN $TTFDIR_DV/DroidSerif.ttf $OUTDIR/droidserif.inc.pl
-$CONVBIN $TTFDIR_DV/DroidSansMono.ttf $OUTDIR/droidmono.inc.pl
-fi
+{
+       if (my $src = first { -e } map { glob "$_/code2000.ttf" } @ttfpath) {
+               system $convbin, $src, "$outdir/c2k.inc.pl";
+       }
+       if (my $src = first { -e } map { glob "$_/arial*uni*.ttf" } @ttfpath) {
+               system $convbin, $src, "$outdir/ariuni.inc.pl";
+       }
+}
 
-# other
-
-find $TTFDIR -iname code2000.ttf -exec \
-       $CONVBIN "{}" $OUTDIR/c2k.inc.pl \;
-
-find $TTFDIR -iname arial\*uni\*.ttf -exec \
-       $CONVBIN "{}" $OUTDIR/ariuni.inc.pl \;
-
-TTFDIR_GU=/usr/share/fonts/truetype/unifont/
-if [ -e $TTFDIR_GU ]; then
-$CONVBIN $TTFDIR_GU/unifont.ttf $OUTDIR/unifont.inc.pl
-fi
+if (my $gupath = '/usr/share/fonts/truetype/unifont') {
+       if (my $src = first { -e } map { glob "$_/unifont.ttf" } $gupath, @ttfpath) {
+               system $convbin, $src, "$outdir/unifont.inc.pl";
+       }
+}