Skip to content

Commit

Permalink
Prioritize sorting of hash keys when dumping PDF data.
Browse files Browse the repository at this point in the history
Since only the first occurence of any given reference is dumped in full,
it's better to output Pages before Outlines, etc.
  • Loading branch information
deven committed Jun 27, 2024
1 parent 2b54422 commit 0e34ebc
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/PDF/Data.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,20 @@ sub dump_object {
# Dump output.
my $output = "";

# Hash key sort priority.
my %priority = (
Type => -2,
Version => -1,
Root => 1,
Pages => 2,
PageLabels => 3,
Names => 4,
Dests => 5,
Outlines => 6,
Threads => 7,
StructTreeRoot => 8,
);

# Check mode and object type.
if ($mode eq "outline") {
if (ref $object and $seen->{$object}) {
Expand All @@ -1266,7 +1280,7 @@ sub dump_object {
$output = "(STREAM)";
} else {
$label =~ s/(?<=\w)$/->/;
my @keys = sort { fc($a) cmp fc($b) || $a cmp $b; } keys %{$object};
my @keys = sort { ($priority{$a} // 0) <=> ($priority{$b} // 0) || fc($a) cmp fc($b) || $a cmp $b; } keys %{$object};
my $key_len = max map length $_, @keys;
foreach my $key (@keys) {
my $obj = $object->{$key};
Expand Down Expand Up @@ -1310,7 +1324,7 @@ sub dump_object {
$seen->{$object} = $label;
$output = "{ # $label\n";
$label =~ s/(?<=\w)$/->/;
my @keys = sort { fc($a) cmp fc($b) || $a cmp $b; } keys %{$object};
my @keys = sort { ($priority{$a} // 0) <=> ($priority{$b} // 0) || fc($a) cmp fc($b) || $a cmp $b; } keys %{$object};
my $key_len = max map length $_, @keys;
foreach my $key (@keys) {
my $obj = $object->{$key};
Expand Down

0 comments on commit 0e34ebc

Please sign in to comment.