| 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 | use 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 font table with name and other options | ||
| 18 | print ttfdump($font->{'head'}, 'head', %opts); | ||
| 19 | |||
| 20 | # Print one glyph's data: | ||
| 21 | print ttfdump($font->{'loca'}->read->{'glyphs'}[$gid], "glyph_$gid"); | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Font::TTF data structures are trees created from hashes and arrays. When trying to figure | ||
| 26 | out how the structures work, sometimes it is helpful to use Data::Dumper on them. However, | ||
| 27 | many of the object structures have ' PARENT' links that refer back to the object's parent, | ||
| 28 | which means that Data::Dumper ends up dumping the whole font no matter what. | ||
| 29 | |||
| 30 | The main purpose of this module is to invoke Data::Dumper with a | ||
| 31 | filter that skips over the ' PARENT' element of any hash. | ||
| 32 | |||
| 33 | To reduce output further, this module also skips over ' CACHE' elements and any | ||
| 34 | hash element whose value is a Font::TTF::Glyph or Font::TTF::Font object. | ||
| 35 | (Really should make this configurable.) | ||
| 36 | |||
| 37 | If $opts{'d'}, then set Deepcopy mode to minimize use of crossreferencing. | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | use strict; | ||
| 42 | use Data::Dumper; | ||
| 43 | |||
| 44 | use vars qw(@EXPORT @ISA); | ||
| 45 | require Exporter; | ||
| 46 | @ISA = qw( Exporter ); | ||
| 47 | @EXPORT = qw( ttfdump ); | ||
| 48 | |||
| 49 | my %skip = ( Font => 1, Glyph => 1 ); | ||
| 50 | |||
| 51 | sub ttfdump | ||
| 52 | { | ||
| 53 | my ($var, $name, %opts) = @_; | ||
| 54 | my $res; | ||
| 55 | |||
| 56 | my $d = Data::Dumper->new([$var]); | ||
| 57 | $d->Names([$name]) if defined $name; | ||
| 58 | $d->Sortkeys(\&myfilter); # This is the trick to keep from dumping the whole font | ||
| 59 | $d->Deepcopy($opts{'d'}); # Caller controls whether to use crossreferencing | ||
| 60 | $d->Indent(3); # I want array indicies | ||
| 61 | $d->Useqq(1); # Perlquote -- slower but there might be binary data. | ||
| 62 | $res = $d->Dump; | ||
| 63 | $d->DESTROY; | ||
| 64 | $res; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub myfilter | ||
| 68 | { | ||
| 69 | my ($hash) = @_; | ||
| 70 | my @a = grep { | ||
| 71 | ($_ eq ' PARENT' || $_ eq ' CACHE') ? 0 : | ||
| 72 | ref($hash->{$_}) =~ m/^Font::TTF::(.*)$/ ? !$skip{$1} : | ||
| 73 | 1 | ||
| 74 | } (keys %{$hash}) ; | ||
| 75 | # Sort numerically if that is reasonable: | ||
| 76 | return [ sort {$a =~ /\D/ || $b =~ /\D/ ? $a cmp $b : $a <=> $b} @a ]; | ||
| 77 | } | ||
| 78 | |||
| 79 | 1; | ||
| 80 | |||
| 81 | |||
| 82 | =head1 AUTHOR | ||
| 83 | |||
| 84 | Bob Hallissy L<http://scripts.sil.org/FontUtils>. | ||
| 85 | |||
| 86 | |||
| 87 | =head1 LICENSING | ||
| 88 | |||
| 89 | Copyright (c) 1998-2016, SIL International (http://www.sil.org) | ||
| 90 | |||
| 91 | This module is released under the terms of the Artistic License 2.0. | ||
| 92 | For details, see the full text of the license in the file LICENSE. | ||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | =cut | ||
| 97 |