# Dump a font table structure

use strict;
use Font::TTF::Font;
use Font::TTF::Scripts::AP;
use Font::TTF::Dumper;
use Pod::Usage;
use Getopt::Std;

our ($opt_a, $opt_t);

getopts('a:t:');

unless (defined $ARGV[0])
{
    pod2usage( -verbose => 2, -noperldoc => 1);
    exit;
}



my ($f, $ap);

if ($opt_a)
{
    $ap = Font::TTF::Scripts::AP->read_font($ARGV[0], $opt_a) || die "Can't read font '$ARGV[0]': $!\n";
    $f = $ap->{'font'};
}
else
{
    $f = Font::TTF::Font->open($ARGV[0]) || die "Can't open font file '$ARGV[0]': $!\n";
}

my @tags;

if ($opt_t)
{   @tags = split(/\s*[,\s]\s*/, $opt_t); }
else
{   
    @tags = sort keys %{$f};
    push @tags, 'AP' if $opt_a;
}

foreach (@tags)
{
    next unless $_;
    if (length($_) == 4 and exists $f->{$_})
    {
        $f->{$_}->read;
        print Font::TTF::Dumper::Dumper(\$f->{$_}, $_);
    }
    elsif ($_ eq 'AP')
    {
        print Font::TTF::Dumper::Dumper($ap, "AP");
    }
    else
    {
        warn "Font doesn't contain a table with tag '$_'\n";
    }
}


=head1 TITLE

dumpfont - dump table structures from L<Font::TTF::Font>

=head1 SYNOPSIS

     dumpfont [-t taglist] [-a attach.xml] font.ttf

Opens the fontfile with Font::TTF::Font->open or, if -a specified, 
Font::TTF::Scripts::AP->read_font, and then uses L<Data::Dumper> to pretty-print 
the resultant data structures for one or more font tables to STDOUT.

C<taglist> is a comma-or space-separated list of tags specifying which tables to dump.
If -a is supplied then C<taglist> may contain 'AP' to request dump of the attachment
point structure. If -t not provided, dumps all tables. 

By design, dumpfont silently ignores any ' PARENT' or ' CACHE' elements, as well as
any element whose value is a Font::TTF::Font object, in any of the data structures. 

=cut