| 1 | package Khonsu::TOC; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use Khonsu::TOC::Outline; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 3 | ▶ | use parent 'Khonsu::Text'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | sub attributes { | ||||||||||||||||||||
| 8 | 2 | ▶ | my $a = shift; | ||||||||||||||||||
|
Subroutine
attributes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 9 | return ( | ||||||||||||||||||||
| 10 | $a->SUPER::attributes(), | ||||||||||||||||||||
| 11 | title => {$a->RW, $a->STR}, | ||||||||||||||||||||
| 12 | title_font_args => {$a->RW, $a->HR}, | ||||||||||||||||||||
| 13 | title_padding => {$a->RW, $a->NUM}, | ||||||||||||||||||||
| 14 | font_args => {$a->RW, $a->HR}, | ||||||||||||||||||||
| 15 | 2 | ▶ | padding => {$a->RW, $a->NUM, default => sub { 0 }}, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | count => {$a->RW, $a->NUM}, | ||||||||||||||||||||
| 17 | toc_placeholder => {$a->RW, $a->HR}, | ||||||||||||||||||||
| 18 | base_outline => {$a->RW, $a->OBJ}, | ||||||||||||||||||||
| 19 | outlines => {$a->RW, $a->DAR}, | ||||||||||||||||||||
| 20 | 2 | ▶ | level_indent => {$a->RW, $a->NUM, default => sub { 2 }}, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | levels => {$a->RW, $a->HR, default => sub {{ | ||||||||||||||||||||
| 22 | 2 | ▶ | h1 => 1, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | h2 => 2, | ||||||||||||||||||||
| 24 | h3 => 3, | ||||||||||||||||||||
| 25 | h4 => 4, | ||||||||||||||||||||
| 26 | h5 => 5, | ||||||||||||||||||||
| 27 | h6 => 6 | ||||||||||||||||||||
| 28 | }}}, | ||||||||||||||||||||
| 29 | 2 | ); | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | sub add { | ||||||||||||||||||||
| 33 | 2 | ▶ | my ($self, $file, %attributes) = @_; | ||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 34 | 2 | $self->set_attributes(%attributes); | |||||||||||||||||||
| 35 | 2 | $self->toc_placeholder({ | |||||||||||||||||||
| 36 | page => $file->page, | ||||||||||||||||||||
| 37 | }); | ||||||||||||||||||||
| 38 | 2 | $self->base_outline($file->pdf->outlines()->outline); | |||||||||||||||||||
| 39 | 2 | $file->onsave('toc', 'render', %attributes); | |||||||||||||||||||
| 40 | 2 | ▶ | $file->add_page(%{$attributes{page_args} || {}}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 2 | return $file; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | sub outline { | ||||||||||||||||||||
| 45 | 612 | ▶ | my ($self, $file, $type, %attributes) = @_; | ||||||||||||||||||
|
Subroutine
outline: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 612 | $self->count($self->count + 1); | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 612 | ▶ | $attributes{title} ||= $attributes{text}; | ||||||||||||||||||
|
Condition33%1/3
$attributes{"title"} ||= $attributes{"text"}
MC/DC0%0/2
$attributes{"title"} ||= $attributes{"text"}
$attributes{"title"}
$attributes{"text"}
Truth table33%1/3
A ||= B
A= $attributes{"title"}B= $attributes{"text"}
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 612 | delete $attributes{font}; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 612 | my $level = $self->levels->{$type}; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 612 | my $outline = Khonsu::TOC::Outline->new( | |||||||||||||||||||
| 56 | page => $file->page, | ||||||||||||||||||||
| 57 | level => $level, | ||||||||||||||||||||
| 58 | %attributes | ||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 612 | ▶ | if ($level == 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | 102 | $outline->add($file, $self->base_outline); | |||||||||||||||||||
| 63 | 102 | push @{ $self->outlines }, $outline; | |||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||
| 65 | 510 | $outline->indent($self->level_indent * ($level - 1)); | |||||||||||||||||||
| 66 | 510 | my $parent = $self->outlines->[-1]; | |||||||||||||||||||
| 67 | 510 | $level--; | |||||||||||||||||||
| 68 | 510 | ▶ | while ($level > 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 1020 | $parent = $parent->children->[-1]; | |||||||||||||||||||
| 70 | 1020 | $level--; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | 510 | $outline->add($file, $parent->outline); | |||||||||||||||||||
| 73 | 510 | push @{$parent->children}, $outline; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 612 | return $file; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | sub render { | ||||||||||||||||||||
| 80 | 2 | ▶ | my ($self, $file, %attributes) = @_; | ||||||||||||||||||
|
Subroutine
render: called CC=10 SCAR=23.3
Pod
undocumented
| |||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 2 | $self->set_attributes(%attributes); | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 2 | my %position = $self->get_points(); | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 2 | my $page = $self->toc_placeholder->{page}; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 2 | $file->open_page($self->toc_placeholder->{page}->num()); | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 2 | $file->page->toc(1); | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 2 | ▶ | if ($self->title) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 2 | $self->SUPER::add($file, text => $self->title, font => $self->title_font_args); | |||||||||||||||||||
| 94 | 2 | $position{y} += $self->font->size; | |||||||||||||||||||
| 95 | 2 | ▶ | $position{y} += $self->title_padding if $self->title_padding; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 2 | ▶ | return unless scalar @{$self->outlines}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 2 | my $one_toc_link = $self->outlines->[0]->font->size + $self->padding; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 2 | $attributes{page_offset} = 0; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 2 | my $total_height = ($self->count * $one_toc_link) - $position{h}; | |||||||||||||||||||
| 105 | 2 | ▶ | if ($total_height > 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 2 | ▶ | while ($total_height > 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 10 | $attributes{page_offset}++; | |||||||||||||||||||
| 108 | 10 | $file->add_page(toc => 1, num => $self->toc_placeholder->{page}->num() + $attributes{page_offset}); | |||||||||||||||||||
| 109 | 10 | $total_height -= $file->page->h; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 2 | $file->open_page($self->toc_placeholder->{page}->num()); | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 2 | %attributes = (%attributes, %position); | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 2 | my $y = $attributes{y}; | |||||||||||||||||||
| 118 | 2 | my $num = $self->toc_placeholder->{page}->num(); | |||||||||||||||||||
| 119 | 2 | for my $outline (@{$self->outlines}) { | |||||||||||||||||||
| 120 | $outline->render( | ||||||||||||||||||||
| 121 | $file, | ||||||||||||||||||||
| 122 | %attributes, | ||||||||||||||||||||
| 123 | y => $y, | ||||||||||||||||||||
| 124 | padding => $self->padding, | ||||||||||||||||||||
| 125 | 6 | ▶ | num => sub { $_[0] ? ++$num : $num } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 102 | ); | |||||||||||||||||||
| 127 | 102 | $y = $outline->end_y + $self->padding; | |||||||||||||||||||
| 128 | 102 | ▶ | if ($y > $file->page->h - ($file->page->footer ? $file->page->footer->h : 0)) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 129 | 4 | $num++; | |||||||||||||||||||
| 130 | 4 | $file->open_page($num); | |||||||||||||||||||
| 131 | 4 | ▶ | $y = $file->page->header ? $file->page->header->h : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 2 | $file->page_offset($attributes{page_offset}); | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 2 | return $file; | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 3 | 1; | |||||||||||||||||||