#! /usr/bin/perl
# Dump a font table structure

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

our ($opt_a, $opt_d, $opt_t, $opt_v);

getopts('a:dt:v:');

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



my ($f, $fv);

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

my @tags;

if ($opt_t)
{   @tags = map {pack('A4',$_)} split(/\s*[,\s]\s*/, $opt_t); }
else
{   
    @tags = sort grep {length($_) == 4} keys %{$f};
    push @tags, 'AP  ' if $opt_a;
}
push @tags, 'TSIV' if ($opt_v && !grep(/TSIV/, @tags));


my %opts = (d => $opt_d);
;
foreach (@tags)
{
    next unless $_;
    if ($_ eq 'TSIV')
    {
        if ($opt_v)
        {
            my $volt_text;
            my ($inf) = IO::File->new("< $opt_v") || die "Can't open file $opt_v";
            local $/ = undef;
            $volt_text .= <$inf>;
            $inf->close;
            print Font::TTF::Dumper::ttfdump($fv->parse_volt($volt_text), 'volt', %opts);
        }
        elsif (defined $f->{'TSIV'})
        { print Font::TTF::Dumper::ttfdump($fv->parse_volt(), 'volt', %opts);  }
        else
        { warn "Font doesn't contain a table with tag '$_'\n"; }
    }
    elsif (length($_) == 4 and exists $f->{$_})
    {
        $f->{$_}->read;
        print Font::TTF::Dumper::ttfdump(\$f->{$_}, $_, %opts); 
    }
    elsif ($_ eq 'AP  ' && $opt_a)
    {
        print Font::TTF::Dumper::ttfdump(\$fv, "AP", %opts);
    }
    elsif ($_ =~ /woff/oi)
    {
    	if (exists $f->{' WOFF'})
    	{
    		$f->{' WOFF'}{'metaData'}->read_dat if defined $f->{' WOFF'}{'metaData'};
    		# $f->{' WOFF'}{'privateData'}->read_dat if defined $f->{' WOFF'}{'privateData'};
    		print Font::TTF::Dumper::ttfdump(\$f->{' WOFF'}, $_, %opts); 
    	}
    	else
    	{ warn "Font isn't WOFF format\n";}
    }
    else
    {
        warn "Font doesn't contain a table with tag '$_'\n";
    }
}


=head1 NAME

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

=head1 SYNOPSIS

  dumpfont [-t taglist] [-a attach.xml] [-v voltsource.vtp] [-d] font.ttf

Opens the fontfile with Font::TTF::Font->open 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 -v is supplied or the tables to dump includes the 'TSIV' table, then the
VOLT source will first be parsed then dumped. If -t not provided, dumps all tables.
If C<taglist> contain 'woff' then the metadata from WOFF-formatted fonts will be dumped.

Normally dumpfont minimizes output by crossreferencing previously dumped structures, 
but this can make it difficult to diff two fonts. -d suppresses this behavior
except where needed to break reference cycles. 

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. 

=head1 AUTHOR

Martin Hosken L<http://scripts.sil.org/FontUtils>.
(see CONTRIBUTORS for other authors).

=head1 LICENSING

Copyright (c) 1998-2016, SIL International (http://www.sil.org)

This script is released under the terms of the Artistic License 2.0.
For details, see the full text of the license in the file LICENSE.

=cut
