#!/usr/bin/perl

=pod

=head1 NAME

showglyphs - create a F<pdf> file that shows all glyphs in a font

=head1 SYNOPSIS

showglyphs I<fontfile>

=head1 DESCRIPTION

This program creates a F<pdf> file that shows all glyphs in a given font.

=head1 FILES

=over 4

=item I<font>

This can be any Type 1 or TrueType font; OpenType fonts are not (yet?)
supported.

=back

=head1 RESTRICTIONS

=over 4

=item B<->

B<showglyphs> only works on Unix/Linux systems.

=item B<->

B<showglyphs> needs a working, TDS-compliant Tex installation

=back

=head1 SEE ALSO

F<afm2afm>, F<autoinst>, F<cmap2enc>, F<pfm2kpx>, F<enc2etx>, 
F<etx2enc>, F<ot2kpx>.

=head1 AUTHOR

Marc Penninga <marc@penninga.info>

=head1 HISTORY

=over 12

=item I<2005-04-28>

First version

=item I<2005-04-29>

A few bugfixes; B<showglyphs> now works with glyphnames containing underscores

=item I<2005-05-24>

Another bugfix

=item I<2005-07-29>

A few updates to the documentation

=back

=cut

use integer;
use warnings; no warnings qw(uninitialized);

$0 =~ s!.*/!!g;
die "Usage: $0 fontfile\n" if @ARGV != 1;

system "mkdir -p ./showglyphs/";
system "cp $ARGV[0] ./showglyphs/";
chdir "./showglyphs";

system "font2afm $ARGV[0]";
$ARGV[0] =~ /([\w-]+)\./ and $fontname = $1;

open AFM, "<${fontname}.afm" or 
    	die "Error: can't open `${fontname}.afm' - $!\n";
{
    local $/;
    $_ = <AFM>;
}

(/StartCharMetrics (\d+)/m and $num_glyphs = $1) or $num_glyphs = 255;
@vec = (".notdef") x (($num_glyphs / 256 + 1) * 256);

for (/(WX.*?$)/gm)  {/N\s+([\w.]+)/ and $vec[$i++] = $1}

system "cp `locate pdftex.map` .";
open TEX, ">tmp.tex" or die "Error: can't create `tmp.tex' - $!\n";
print TEX <<EOF;
\\documentclass[a4paper]{article}
\\setlength\\parindent{0pt}
\\setlength\\parskip{0pt}
\\raggedbottom\\frenchspacing

\\newlength\\tempdima
\\makeatletter
\\setlength\\textheight{\\paperheight}
\\addtolength\\textheight{-2in}
\\setlength\\textwidth{\\paperwidth}
\\addtolength\\textwidth{-2in}

\\setlength\\oddsidemargin{0pt}
\\setlength\\evensidemargin{0pt}

\\setlength\\topmargin{0pt}
\\addtolength\\topmargin{-\\headheight}
\\addtolength\\topmargin{-\\headsep}
\\makeatother

\\newcommand*{\\printglyph}[2]{%
    \\frame{%
    	\\settodepth{\\tempdima}{#1}%
      	\\makebox[2em][l]{%
	    \\rule[-18pt]{0pt}{48pt}%
	    \\makebox[0pt][l]{%
	    	\\raisebox{-14pt}{\\makebox[2em][c]{\\tiny#2}}}%
	    \\raisebox{-\\tempdima}{%
	    	\\makebox[2em][c]{\\linethickness{0pt}\\frame{#1}}}}}}

\\begin{document}
EOF

for ($i = 0; $i < $num_glyphs / 256 + 1; $i++) {
    $filename = sprintf "%s%03d", $fontname, $i + 1;
    
    open  ENC, ">$filename.enc" or 
    	    die "Error: can't create `$filename.enc' - $!\n";

    print ENC "%\n% Created ", scalar localtime, 
    	      " by $0 from `${fontname}.afm'\n";
    print ENC "%\n/", $filename, " [\n";
    
    for ($j = 0; $j < 256; $j += 16) {
    	printf ENC "%% 0x%04x\n", $j;
	for ($k = 0; $k < 16; $k += 4) {
	    print ENC "\t";
	    for ($l = 0; $l < 4; $l++) {
	    	print ENC "/$vec[$i * 256 + $j + $k + $l] ";
	    }
	    print ENC "\n";
	}
    }
    
    print ENC "] def\n";
    close ENC;
    
    system "afm2afm -e '${filename}.enc' -o '${filename}.afm' " . 
    	   "'${fontname}.afm' >>./pdftex.map";
    system "afm2tfm '${fontname}.afm' -T '${filename}.enc' '${filename}.tfm'" 
    	 . ">/dev/null";

    for (@vec[256 * $i..256*($i+1)]) {s/(?<!\\)_/\\_/g}
    print TEX "\\font\\test=${filename} at 24pt\\test\n";
    for $j (0 .. 31) {
    	for $k (0 .. 7) {
    	    print TEX "\\printglyph{\\char", 8 * $j + $k, 
	    	      "}{$vec[$i * 256 + 8 * $j + $k]} ";
	}
	print TEX "\\par\n";
	last if 256 * $i + 8 * ($j + 1) >= $num_glyphs;
    }
}
print TEX "\n\\end{document}\n";
close TEX;

system "pdflatex tmp >/dev/null";
chdir "../";
system "mv showglyphs/tmp.pdf './${fontname}.pdf'";
system "/bin/rm -r ./showglyphs/";

__END__
