Introduce src dir.
[librarian.git] / src / librarian / font-optimizer / ext / Font-TTF / lib / Font / TTF / Dumper.pm
1 package Font::TTF::Dumper;
2
3 =head1 NAME
4
5 Font::TTF::Dumper - Debug dump of a font datastructure, avoiding recursion on ' PARENT'
6
7 =head1 SYNOPSIS
8
9     Font::TTF::Dumper;
10     
11     # Print a table from the font structure:
12     print ttfdump($font->{$tag});
13     
14     # Print font table with name
15     print ttfdump($font->{'head'}, 'head');
16     
17     # Print one glyph's data:
18     print ttfdump($font->{'loca'}->read->{'glyphs'}[$gid], "glyph_$gid");
19
20 =head1 DESCRIPTION
21
22 Font::TTF data structures are trees created from hashes and arrays. When trying to figure
23 out how the structures work, sometimes it is helpful to use Data::Dumper on them. However,
24 many of the object structures have ' PARENT' links that refer back to the object's parent,
25 which means that Data::Dumper ends up dumping the whole font no matter what.
26
27 The purpose of this module is to do just one thing: invoke Data::Dumper with a
28 filter that skips over the ' PARENT' element of any hash.
29
30 To reduce output further, this module also skips over ' CACHE' elements and any 
31 hash element whose value is a Font::TTF::Glyph or Font::TTF::Font object. 
32 (Really should make this configurable.)
33
34 =cut
35
36 use strict;
37 use Data::Dumper;
38
39 use vars qw(@EXPORT @ISA);
40 require Exporter;
41 @ISA = qw( Exporter );
42 @EXPORT = qw( ttfdump );
43
44 my %skip = ( Font => 1, Glyph => 1 );
45
46 sub ttfdump
47 {
48     my ($var, $name) = @_;
49     my $res;
50     
51     my $d = Data::Dumper->new([$var]);
52     $d->Names([$name]) if defined $name;
53     $d->Sortkeys(\&myfilter);   # This is the trick to keep from dumping the whole font
54     $d->Indent(3);  # I want array indicies
55     $d->Useqq(1);   # Perlquote -- slower but there might be binary data.
56     $res = $d->Dump;
57     $d->DESTROY;
58     $res;
59 }
60
61 sub myfilter
62 {
63     my ($hash) = @_;
64     my @a = grep {
65             ($_ eq ' PARENT' || $_ eq ' CACHE') ? 0 :
66             ref($hash->{$_}) =~ m/^Font::TTF::(.*)$/ ? !$skip{$1} :
67             1
68         } (keys %{$hash}) ;
69     # Sort numerically if that is reasonable:
70     return [ sort {$a =~ /\D/ || $b =~ /\D/ ? $a cmp $b : $a <=> $b} @a ];
71 }
72
73 1;
74
75 =head1 See also
76
77 L<Font::TTF::Font>
78
79 =cut