blib/lib/PDF/Make/Builder/Font.pm

filter: Statementstmt 95.6% 110 / 115 Branchbran 78.8% 41 / 52 Conditioncond 52.3% 11 / 21 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 100% 22 / 22 Podpod 66.6% 8 / 12 totaltotal 82.9% 195 / 235 CC 34 SCAR 13.5
CC
34
Cov
86%
resolve_line_height
7.1
ensure_loaded
7.1
_xs_font
5.1
CRAP 3.9
« blib/lib/PDF/Make/Builder/Shape/Box.pm ↑ Summary blib/lib/PDF/Make/Builder/Page/Header.pm »
1 package PDF::Make::Builder::Font;
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 53 ▶use PDF::Make::Page qw(:fonts);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 53 ▶use PDF::Make::Font ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 53 ▶use Font::Metrics ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 53 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 53 Object::Proto::define('PDF::Make::Builder::Font',
11 'colour:Str:default(#000)',
12 'size:Num:default(9)',
13 'family:Str:default(Helvetica)',
14 'bold:Bool:default(0)',
15 'italic:Bool:default(0)',
16 'line_height:Num',
17 'loaded:HashRef:default({})',
18 'registry:HashRef:default({})',
19 );
20 53 Object::Proto::import_accessors('PDF::Make::Builder::Font');
21 }
22  
23 # Standard 14 font family mapping: family => { variant => page constant }
24 53 my %STD14 = (
25 Times => {
26 normal => TIMES_ROMAN,
27 bold => TIMES_BOLD,
28 italic => TIMES_ITALIC,
29 bolditalic => TIMES_BOLDITALIC,
30 },
31 Helvetica => {
32 normal => HELVETICA,
33 bold => HELVETICA_BOLD,
34 italic => HELVETICA_OBLIQUE,
35 bolditalic => HELVETICA_BOLDOBLIQUE,
36 },
37 Courier => {
38 normal => COURIER,
39 bold => COURIER_BOLD,
40 italic => COURIER_OBLIQUE,
41 bolditalic => COURIER_BOLDOBLIQUE,
42 },
43 Symbol => { normal => SYMBOL },
44 ZapfDingbats => { normal => ZAPFDINGBATS },
45 );
46  
47 # Map family+variant to PDF BaseFont name for XS Font
48 53 my %BASEFONT = (
49 'Times_normal' => 'Times-Roman',
50 'Times_bold' => 'Times-Bold',
51 'Times_italic' => 'Times-Italic',
52 'Times_bolditalic' => 'Times-BoldItalic',
53 'Helvetica_normal' => 'Helvetica',
54 'Helvetica_bold' => 'Helvetica-Bold',
55 'Helvetica_italic' => 'Helvetica-Oblique',
56 'Helvetica_bolditalic' => 'Helvetica-BoldOblique',
57 'Courier_normal' => 'Courier',
58 'Courier_bold' => 'Courier-Bold',
59 'Courier_italic' => 'Courier-Oblique',
60 'Courier_bolditalic' => 'Courier-BoldOblique',
61 'Symbol_normal' => 'Symbol',
62 'ZapfDingbats_normal' => 'ZapfDingbats',
63 );
64  
65 # Cache for Font::Metrics objects (exact per-glyph metrics via Font::Metrics XS)
66 53 my %_fm_cache;
67  
68 # A family registered by Builder::load_ttf, or undef for a Standard 14 family.
69 # The entry is the hashref the Builder owns: { font, path, obj, pages }, shared
70 # by reference with every font cloned from this one, so a page recorded here is
71 # a page the Builder will attach the embedded font to when it finalises.
72 70894 ▶sub _ttf {
Subroutine _ttf: called CC=2 SCAR=7.0
73 70894 my ($self) = @_;
74 70894 my $reg = registry $self;
75 70894 ▶ return undef unless $reg;
Branch: 1 branch
BranchTrueFalseTotal
unless $reg 0 70894 70894
76 70894 return $reg->{ family $self };
77 }
78  
79 9182 ▶sub is_ttf { return defined $_[0]->_ttf ? 1 : 0 }
Subroutine is_ttf: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->_ttf ? : 3 9179 9182
80  
81 43992 ▶sub _xs_font {
Subroutine _xs_font: called CC=5 SCAR=16.3
82 43992 my ($self, $variant) = @_;
83 43992 my $ttf = $self->_ttf;
84 43992 ▶ return $ttf->{font} if $ttf;
Branch: 1 branch
BranchTrueFalseTotal
if $ttf 6 43986 43992
85 43986 ▶ $variant //= $self->_default_variant;
Condition33%1/3
$variant //= $self->_default_variant
l!l&&r!l&&!r
0439860
MC/DC0%0/2
$variant //= $self->_default_variant
$variant $self->_default_variant
Truth table33%1/3
A //= B
A= $variantB= $self->_default_variant
ABresult
1X1
011
000
86 43986 my $fam = family $self;
87 43986 my $key = "${fam}_${variant}";
88 43986 ▶ return $_fm_cache{$key} if $_fm_cache{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $_fm_cache{$key} 43930 56 43986
89 56 my $basefont = $BASEFONT{$key};
90 56 ▶ return undef unless $basefont;
Branch: 1 branch
BranchTrueFalseTotal
unless $basefont 0 56 56
91 56 $_fm_cache{$key} = Font::Metrics->new(name => $basefont);
92 56 return $_fm_cache{$key};
93 }
94  
95 # The show operator this font's text needs, and the operand to give it.
96 #
97 # A loaded TTF is embedded as a Type0 font with Identity-H encoding, so its
98 # operand is two bytes of glyph id per character and goes in a hex string.
99 # encode_utf8 also marks each glyph used, which is what the subsetter embeds,
100 # so this must run for every string actually drawn.
101 9170 ▶sub show_op { return $_[0]->is_ttf ? 'Tj_hex' : 'Tj' }
Subroutine show_op: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_ttf ? : 3 9167 9170
102  
103 9210 ▶sub encode {
Subroutine encode: called CC=3 SCAR=11.0
Pod undocumented
104 9210 my ($self, $text) = @_;
105 9210 my $ttf = $self->_ttf;
106 9210 ▶ return $text unless $ttf;
Branch: 1 branch
BranchTrueFalseTotal
unless $ttf 9207 3 9210
107 3 ▶ return '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 3 3
108 3 my $utf8 = $text;
109 3 utf8::upgrade($utf8); # SvPV then always hands XS UTF-8, never Latin-1
110 3 return $ttf->{font}->encode_utf8($utf8);
111 }
112  
113 52479 ▶sub _default_variant {
Subroutine _default_variant: called CC=5 SCAR=16.1
114 52479 my ($self) = @_;
115 52479 ▶ return 'bolditalic' if bold($self) && italic($self);
Branch: 1 branch
BranchTrueFalseTotal
if <custom op object_func_get> and <custom op object_func_get> 10 52469 52479
Condition100%3/3
<custom op object_func_get> and <custom op object_func_get>
!ll&&!rl&&r
5236910010
MC/DC100%2/2
<custom op object_func_get> and <custom op object_func_get>
<custom op object_func_get> <custom op object_func_get>
Truth table100%3/3
A and B
A= <custom op object_func_get>B= <custom op object_func_get>
ABresult
0X0
100
111
116 52469 ▶ return 'bold' if bold($self);
Branch: 1 branch
BranchTrueFalseTotal
if <custom op object_func_get> 100 52369 52469
117 52369 ▶ return 'italic' if italic($self);
Branch: 1 branch
BranchTrueFalseTotal
if <custom op object_func_get> 30 52339 52369
118 52339 return 'normal';
119 }
120  
121 961 ▶sub effective_line_height {
Subroutine effective_line_height: called CC=3 SCAR=11.0
Pod documented
122 961 my ($self) = @_;
123 961 my $lh = line_height $self;
124 961 ▶ return defined $lh && $lh > 0 ? $lh : size $self;
Branch: 1 branch
BranchTrueFalseTotal
defined $lh && $lh > 0 ? : 166 795 961
Condition66%2/3
defined $lh and $lh > 0
!ll&&!rl&&r
7950166
MC/DC50%1/2
defined $lh and $lh > 0
defined $lh $lh > 0
Truth table66%2/3
A and B
A= defined $lhB= $lh > 0
ABresult
0X0
100
111
125 }
126  
127 # The leading a set of overrides gets when it is resolved against $base.
128 # Returns undef to mean "let effective_line_height take the size", which is
129 # not the same as returning the base's leading.
130 #
131 # A size override has to bring its own line height with it. Taking the base
132 # font's - which is what every caller here used to do - gave a 20pt run a
133 # 9pt slot: the baseline sits one font size below the cursor, the cursor
134 # advances by the slot, and the next block is drawn through the text above
135 # it. That is the <text size="20"> collision.
136 #
137 # An explicit line_height always wins. Otherwise a size override with no
138 # leading of its own scales the base's explicitly-set leading by the size
139 # ratio - the unitless-line-height rule - and where the base never set one,
140 # undef leaves the new size to speak for itself.
141 215 ▶sub resolve_line_height {
Subroutine resolve_line_height: called CC=6 SCAR=19.6
Pod undocumented
142 215 my ($class, $base, $overrides) = @_;
143 215 ▶ return $overrides->{line_height} if defined $overrides->{line_height};
Branch: 1 branch
BranchTrueFalseTotal
if defined $overrides->{"line_height"} 63 152 215
144 152 ▶ return $base->effective_line_height unless defined $overrides->{size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $overrides->{"size"} 94 58 152
145  
146 58 my $base_lh = $base->line_height;
147 58 my $base_size = $base->size;
148 58 ▶ return undef unless defined $base_lh && $base_lh > 0 && $base_size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base_lh and $base_lh > 0 and $base_size 58 0 58
Condition33%2/6
defined $base_lh and $base_lh > 0 and $base_size
!ll&&!rl&&r
5800
defined $base_lh and $base_lh > 0
!ll&&!rl&&r
5800
MC/DC0%0/3
defined $base_lh and $base_lh > 0 and $base_size
defined $base_lh $base_lh > 0 $base_size
Truth table25%1/4
A and B and C
A= defined $base_lhB= $base_lh > 0C= $base_size
ABCresult
0XX0
10X0
1100
1111
149 0 return $base_lh * ($overrides->{size} / $base_size);
150 }
151  
152 43978 ▶sub measure_text {
Subroutine measure_text: called CC=2 SCAR=7.6
Pod documented
153 43978 my ($self, $text) = @_;
154 43978 my $xs = $self->_xs_font;
155 43978 ▶ return $xs->string_width($text, size $self) if $xs;
Branch: 1 branch
BranchTrueFalseTotal
if $xs 43978 0 43978
156 # Fallback for unknown fonts
157 0 return length($text) * 0.52 * (size $self);
158 }
159  
160 5 ▶sub measure_word {
Subroutine measure_word: called CC=2 SCAR=7.6
Pod documented
161 5 my ($self, $word) = @_;
162 5 my $xs = $self->_xs_font;
163 5 ▶ return $xs->string_width($word, size $self) if $xs;
Branch: 1 branch
BranchTrueFalseTotal
if $xs 5 0 5
164 0 return length($word) * 0.52 * (size $self);
165 }
166  
167 9 ▶sub space_width {
Subroutine space_width: called CC=2 SCAR=7.6
Pod documented
168 9 my ($self) = @_;
169 9 my $xs = $self->_xs_font;
170 9 ▶ return $xs->string_width(' ', size $self) if $xs;
Branch: 1 branch
BranchTrueFalseTotal
if $xs 9 0 9
171 0 return 0.28 * (size $self);
172 }
173  
174 8597 ▶sub hex_to_rgb {
Subroutine hex_to_rgb: called CC=3 SCAR=11.0
Pod documented
175 8597 my ($self, $hex) = @_;
176 8597 $hex =~ s/^#//;
177 8597 my ($r, $g, $b);
178 8597 ▶ if (length($hex) == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $hex == 3) { } 8521 76 8597
elsif (length $hex == 6) { } 73 3 76
179 8521 ($r, $g, $b) = map { hex($_.$_) / 255.0 } split //, $hex;
180 } elsif (length($hex) == 6) {
181 73 $r = hex(substr($hex, 0, 2)) / 255.0;
182 73 $g = hex(substr($hex, 2, 2)) / 255.0;
183 73 $b = hex(substr($hex, 4, 2)) / 255.0;
184 } else {
185 3 return (0, 0, 0);
186 }
187 8594 return ($r, $g, $b);
188 }
189  
190 12 ▶sub resource_name {
Subroutine resource_name: called CC=3 SCAR=11.8
Pod documented
191 12 my ($self, $variant) = @_;
192 12 my $fam = family $self;
193 # One file is one face, so a loaded TTF has no bold/italic variants to
194 # name - asking for bold on it gets the face as supplied.
195 12 ▶ return "F_${fam}" if $self->is_ttf;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ttf 0 12 12
196 12 ▶ $variant //= $self->_default_variant;
Condition33%1/3
$variant //= $self->_default_variant
l!l&&r!l&&!r
0120
MC/DC0%0/2
$variant //= $self->_default_variant
$variant $self->_default_variant
Truth table33%1/3
A //= B
A= $variantB= $self->_default_variant
ABresult
1X1
011
000
197 12 return "F_${fam}_${variant}";
198 }
199  
200 8510 ▶sub ensure_loaded {
Subroutine ensure_loaded: called CC=7 SCAR=19.6
Pod documented
201 8510 my ($self, $xs_page, $variant) = @_;
202 8510 my $fam = family $self;
203  
204 # An embedded font cannot be attached to the page yet: its object number
205 # is only known once the document is written, because subsetting depends
206 # on every glyph drawn with it. Record the page and let the Builder
207 # attach it in _finalise.
208 8510 ▶ if (my $ttf = $self->_ttf) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ttf = $self->_ttf) 3 8507 8510
209 3 my $res_name = "F_${fam}";
210 3 push @{ $ttf->{pages} }, $xs_page
211 0 ▶ unless grep { $_ == $xs_page } @{ $ttf->{pages} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $xs_page;} @{$$ttf{"pages"};} 3 0 3
212 3 return $res_name;
213 }
214  
215 8507 ▶ $variant //= $self->_default_variant;
Condition66%2/3
$variant //= $self->_default_variant
l!l&&r!l&&!r
2684810
MC/DC0%0/2
$variant //= $self->_default_variant
$variant $self->_default_variant
Truth table66%2/3
A //= B
A= $variantB= $self->_default_variant
ABresult
1X1
011
000
216 8507 my $key = "${fam}_${variant}";
217 8507 my $res_name = "F_${key}";
218  
219 # Cache is per-page (keyed by page pointer address) to handle overflow
220 8507 my $page_id = "$xs_page"; # stringified ref = unique per page
221 8507 my $ld = loaded $self;
222 8507 ▶ return $res_name if $ld->{"${key}_${page_id}"};
Branch: 1 branch
BranchTrueFalseTotal
if $$ld{"${key}_$page_id"} 7878 629 8507
223  
224 629 my $family_map = $STD14{$fam};
225 629 ▶ die "PDF::Make::Builder::Font: unknown font family '$fam'" unless $family_map;
Branch: 1 branch
BranchTrueFalseTotal
unless $family_map 0 629 629
226 629 my $font_id = $family_map->{$variant};
227 629 ▶ die "PDF::Make::Builder::Font: unknown variant '$variant' for '$fam'" unless defined $font_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $font_id 0 629 629
228  
229 629 $xs_page->add_std14_font($res_name, $font_id);
230 629 $ld->{"${key}_${page_id}"} = 1;
231 629 loaded $self, $ld;
232 629 return $res_name;
233 }
234  
235 1 ▶sub families { return keys %STD14 }
Subroutine families: called CC=1 SCAR=0.0
Pod documented
236  
237 53 1;
238  
239 __END__
« blib/lib/PDF/Make/Builder/Shape/Box.pm ↑ Summary blib/lib/PDF/Make/Builder/Page/Header.pm »