| 1 | package PDF::Make::Builder::TOC; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 53 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 53 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 53 | ▶ | use Object::Proto; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 53 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 53 | Object::Proto::define('PDF::Make::Builder::TOC', | |||||||||||||||||||||||||||||||||||||||||
| 8 | 'title:Str:default(Table of Contents)', | ||||||||||||||||||||||||||||||||||||||||||
| 9 | 'title_font_args:HashRef:default({})', | ||||||||||||||||||||||||||||||||||||||||||
| 10 | 'title_padding:Num:default(10)', | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 'font_args:HashRef:default({})', | ||||||||||||||||||||||||||||||||||||||||||
| 12 | 'padding:Num:default(0)', | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 'level_indent:Num:default(2)', | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 'entries:ArrayRef:default([])', | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 'page_index:Int:default(0)', | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 'x:Num', 'y:Num', 'w:Num', 'h:Num', | ||||||||||||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 53 | Object::Proto::import_accessors('PDF::Make::Builder::TOC'); | |||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 17 | ▶ | sub outline { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
outline: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 17 | my ($self, $builder, $level, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 17 | my $e = entries $self; | |||||||||||||||||||||||||||||||||||||||||
| 24 | push @$e, { | ||||||||||||||||||||||||||||||||||||||||||
| 25 | text => $args{text}, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | page_num => $args{page_num}, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 17 | level => $level, | |||||||||||||||||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 17 | entries $self, $e; | |||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 6 | ▶ | sub render { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 6 | my ($self, $builder) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 6 | ▶ | my $all_pages = $builder->pages // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$builder->pages // []
MC/DC0%0/1
$builder->pages // []
$builder->pages
Truth table50%1/2
A
A= $builder->pages
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 6 | my $toc_page_idx = page_index($self); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 6 | ▶ | my $page = ($toc_page_idx >= 0 && $toc_page_idx < @$all_pages) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$toc_page_idx >= 0 and $toc_page_idx < @$all_pages
MC/DC50%1/2
$toc_page_idx >= 0 and $toc_page_idx < @$all_pages
$toc_page_idx >= 0
$toc_page_idx < @$all_pages
Truth table66%2/3
A and B
A= $toc_page_idx >= 0B= $toc_page_idx < @$all_pages
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | ? $all_pages->[$toc_page_idx] | ||||||||||||||||||||||||||||||||||||||||||
| 38 | : $builder->page; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 6 | ▶ | return unless $page; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | my $canvas = $page->canvas; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 5 | my $font = $builder->font; | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # Title | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | my $tf = title_font_args $self; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | ▶ | my $title_size = $tf->{size} // 30; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$tf->{"size"} // 30
MC/DC100%1/1
$tf->{"size"} // 30
$tf->{"size"}
Truth table100%2/2
A
A= $tf->{"size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | my $res = $font->ensure_loaded($page->xs_page, 'bold'); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | ▶ | my ($tr, $tg, $tb) = $font->hex_to_rgb($tf->{colour} // '#000'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$tf->{"colour"} // "#000"
MC/DC100%1/1
$tf->{"colour"} // "#000"
$tf->{"colour"}
Truth table100%2/2
A
A= $tf->{"colour"}
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | my $cx = $page->content_x; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | my $cy = $page->cursor_y - $title_size; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 5 | my $tw = $page->width; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 5 | my $right_x = $cx + $tw; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 5 | my $tj = $font->show_op; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 5 | $canvas->BT | |||||||||||||||||||||||||||||||||||||||||
| 58 | ->rg($tr, $tg, $tb) | ||||||||||||||||||||||||||||||||||||||||||
| 59 | ->Tf($res, $title_size) | ||||||||||||||||||||||||||||||||||||||||||
| 60 | ->Tm(1, 0, 0, 1, $cx, $cy) | ||||||||||||||||||||||||||||||||||||||||||
| 61 | ->$tj($font->encode(title $self)) | ||||||||||||||||||||||||||||||||||||||||||
| 62 | ->ET; | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | $cy -= title_padding $self; | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | # Entries | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | my $ef = font_args $self; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 5 | ▶ | my $entry_size = $ef->{size} // 11; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ef->{"size"} // 11
MC/DC100%1/1
$ef->{"size"} // 11
$ef->{"size"}
Truth table100%2/2
A
A= $ef->{"size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | ▶ | my $entry_lh = $ef->{line_height} // ($entry_size + 4); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ef->{"line_height"} // $entry_size + 4
MC/DC0%0/1
$ef->{"line_height"} // $entry_size + 4
$ef->{"line_height"}
Truth table50%1/2
A
A= $ef->{"line_height"}
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | my $entry_res = $font->ensure_loaded($page->xs_page, 'normal'); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 5 | ▶ | my ($er, $eg, $eb) = $font->hex_to_rgb($ef->{colour} // '#333'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ef->{"colour"} // "#333"
MC/DC100%1/1
$ef->{"colour"} // "#333"
$ef->{"colour"}
Truth table100%2/2
A
A= $ef->{"colour"}
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 5 | my $indent_w = $font->space_width * (level_indent $self); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 5 | my $pad = padding $self; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | for my $entry (@{entries $self}) { | |||||||||||||||||||||||||||||||||||||||||
| 76 | 14 | $cy -= $entry_lh; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 14 | my $indent = $indent_w * ($entry->{level} - 1); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 14 | ▶ | my $text = defined $entry->{text} ? $entry->{text} : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 14 | my $pnum = $entry->{page_num}; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 14 | ▶ | my $pnum_str = defined($pnum) ? "$pnum" : '?'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 14 | my $pnum_w = $font->measure_text($pnum_str) * ($entry_size / $font->size); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 14 | my $text_w = $font->measure_text($text) * ($entry_size / $font->size); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 14 | my $text_x = $cx + $indent; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 14 | my $pnum_x = $right_x - $pnum_w; | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 14 | my $leader_start = $text_x + $text_w + 4; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 14 | my $leader_end = $pnum_x - 2; | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | # Left text | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 14 | $canvas->BT | |||||||||||||||||||||||||||||||||||||||||
| 91 | ->rg($er, $eg, $eb) | ||||||||||||||||||||||||||||||||||||||||||
| 92 | ->Tf($entry_res, $entry_size) | ||||||||||||||||||||||||||||||||||||||||||
| 93 | ->Tm(1, 0, 0, 1, $text_x, $cy) | ||||||||||||||||||||||||||||||||||||||||||
| 94 | ->$tj($font->encode($text)) | ||||||||||||||||||||||||||||||||||||||||||
| 95 | ->ET; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | # Dot leaders: draw vector dots so spacing is visual, not font-dependent | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 14 | ▶ | if ($leader_end > $leader_start + 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 13 | my $dot_y = $cy + ($entry_size * 0.22); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 13 | my $step = $entry_size * 0.42; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 13 | my $dot_len = 0.01; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 13 | my $dot_width = $entry_size * 0.11; | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 13 | $canvas->q | |||||||||||||||||||||||||||||||||||||||||
| 105 | ->RG($er, $eg, $eb) | ||||||||||||||||||||||||||||||||||||||||||
| 106 | ->w($dot_width) | ||||||||||||||||||||||||||||||||||||||||||
| 107 | ->J(1); | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 13 | ▶ | for (my $x = $leader_start; $x <= $leader_end; $x += $step) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 1416 | $canvas->m($x, $dot_y) | |||||||||||||||||||||||||||||||||||||||||
| 111 | ->l($x + $dot_len, $dot_y) | ||||||||||||||||||||||||||||||||||||||||||
| 112 | ->S; | ||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 13 | $canvas->Q; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | # Right page number | ||||||||||||||||||||||||||||||||||||||||||
| 119 | $canvas->BT | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 14 | ->rg($er, $eg, $eb) | |||||||||||||||||||||||||||||||||||||||||
| 121 | ->Tf($entry_res, $entry_size) | ||||||||||||||||||||||||||||||||||||||||||
| 122 | ->Tm(1, 0, 0, 1, $pnum_x, $cy) | ||||||||||||||||||||||||||||||||||||||||||
| 123 | ->$tj($font->encode($pnum_str)) | ||||||||||||||||||||||||||||||||||||||||||
| 124 | ->ET; | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | # Make TOC row clickable (internal GoTo) | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 14 | ▶ | if (defined $pnum && $pnum =~ /^\d+$/ && $pnum > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $pnum and $pnum =~ /^\d+$/ and $pnum > 0
defined $pnum and $pnum =~ /^\d+$/
MC/DC100%3/3
defined $pnum and $pnum =~ /^\d+$/ and $pnum > 0
defined $pnum
$pnum =~ /^\d+$/
$pnum > 0
Truth table100%4/4
A and B and C
A= defined $pnumB= $pnum =~ /^\d+$/C= $pnum > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 9 | my $y1 = $cy - 2; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 9 | my $y2 = $cy + $entry_size + 2; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 9 | $builder->add_link( | |||||||||||||||||||||||||||||||||||||||||
| 131 | on_page => $toc_page_idx, | ||||||||||||||||||||||||||||||||||||||||||
| 132 | page => $pnum - 1, | ||||||||||||||||||||||||||||||||||||||||||
| 133 | rect => [ $text_x, $y1, $right_x, $y2 ], | ||||||||||||||||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 14 | $cy -= $pad; | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 5 | $page->y($cy); | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 53 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||||||||||||||||||||||||