Layout/FontConfig.pm

filter: Statementstmt 56.9% 144 / 253 Branchbran 24.7% 48 / 194 Conditioncond 45.4% 15 / 33 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 76.0% 19 / 25 Podpod 69.2% 9 / 13 totaltotal 44.5% 239 / 536 CC 123 SCAR 44.3
CC
123
Cov
43%
_dump
272.0
_fallback
210.0
remap
168.9
CRAP 83.9
« Layout.pm ↑ Summary Layout/Markdown.pm »
1 #! perl
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Text::Layout::FontConfig;
8  
9 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 our $VERSION = "0.045";
12  
13 8 ▶use Text::Layout::FontDescriptor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Text::Layout::FontConfig - Pango style font description for Text::Layout
18  
19 =head1 SYNOPSIS
20  
21 Font descriptors are strings that identify the characteristics of the
22 desired font. For example, C<Sans Italic 20>.
23  
24 The PDF context deals with physical fonts, e.g. built-in fonts like
25 C<Times-Bold> and fonts loaded from font files like
26 C</usr/share/fonts/dejavu/DejaVuSans.ttf>.
27  
28 To map font descriptions to physical fonts, these fonts must be
29 registered. This defines a font family, style, and weight for the
30 font.
31  
32 Note that Text::Layout::FontConfig is a singleton. Creating objects
33 with new() will always return the same object.
34  
35 =cut
36  
37 8 my %fonts;
38 my @dirs;
39 8 my %maps;
40 8 my $loader;
41 8 my $debug = 0;
42  
43 8 my $weights =
44 [ 100 => 'thin',
45 100 => 'hairline',
46 200 => 'extra light',
47 200 => 'ultra light',
48 300 => 'light', # supported
49 350 => 'book', # supported
50 400 => 'normal', # supported
51 400 => 'regular', # supported
52 500 => 'medium', # supported
53 600 => 'semi bold', # supported 'semi'
54 600 => 'demi bold',
55 700 => 'bold', # supported
56 800 => 'extra bold',
57 800 => 'ultra bold',
58 900 => 'black',
59 900 => 'heavy', # supported
60 950 => 'extra black',
61 950 => 'ultra black',
62 ];
63  
64 =head2 METHODS
65  
66 =over
67  
68 =item new( [ atts... ] )
69  
70 For convenience only. Text::Layout::FontConfig is a singleton.
71 Creating objects with new() will always return the same object.
72  
73 Attributes:
74  
75 =over
76  
77 =item corefonts
78  
79 If true, a predefined set of font names (the PDF corefonts) is registered.
80  
81 =back
82  
83 =back
84  
85 =cut
86  
87 7 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
88 7 my ( $pkg, %atts ) = @_;
89 7 my $self = bless {} => $pkg;
90 7 $debug = $self->{debug} = $atts{debug};
91 7 ▶ if ( $atts{corefonts} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atts{"corefonts"}) 5 2 7
92 5 $self->register_corefonts;
93 }
94 7 ▶ if ( $atts{loader} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atts{"loader"}) 0 7 7
95 0 $loader = $atts{loader};
96 }
97 7 return $self;
98 }
99  
100 3 ▶sub reset {
Subroutine reset: called CC=2 SCAR=7.0
Pod undocumented
101 3 my ( $self ) = @_;
102 3 ▶ warn("FC: Reset\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
103 3 %fonts = ();
104 3 @dirs = ();
105 3 %maps = ();
106 }
107  
108 0 ▶sub debug { shift->{debug} }
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
109  
110 =over
111  
112 =item register_fonts( $font, $family, $style [ , $weight ] [ , $props ] )
113  
114 Registers a font fmaily, style and weight for the given font.
115  
116 $font can be the name of a built-in font, or the name of a TrueType or
117 OpenType font file.
118  
119 $family is a font family name such as C<normal>, C<sans>, C<serif>, or
120 C<monospace>. It is possible to specify multiple family names, e.g.,
121 C<times, serif>.
122  
123 $style is the slant style, one of C<normal>, C<oblique>, or C<italic>.
124  
125 $weight is the font weight, like C<normal>, or C<bold>.
126  
127 For convenience, style combinations like "bolditalic" are allowed.
128  
129 A final hash reference can be passed to specify additional properties
130 for this font. Recognized properties are:
131  
132 =over
133  
134 =item *
135  
136 C<shaping> - If set to a true value, this font will require text
137 shaping. This is required for fonts that deal with complex glyph
138 rendering and ligature handling like Devanagari.
139  
140 Text shaping requires module L<HarfBuzz::Shaper>.
141  
142 =item *
143  
144 C<ascender> - If set overrides the font ascender.
145 This may be necessary to improve results for some fonts.
146 The value is expressed in 1/1000th of an em.
147  
148 C<descender> - If set overrides the font descender.
149 This may be necessary to improve results for some fonts.
150 The value is expressed in 1/1000th of an em.
151  
152 =item *
153  
154 C<underline_thickness>, C<underline_position> - Overrides the font
155 specified or calculated values for underline thickness and/or position.
156 This may improve results for some fonts.
157  
158 =item *
159  
160 C<strikeline_thickness>, C<strikeline_position> - Overrides the font
161 specified or calculated values for strikeline thickness and/or position.
162 This may improve results for some fonts.
163  
164 Note that strikeline thickness will default to underline thickness, if set.
165  
166 =item *
167  
168 C<overline_thickness>, C<overline_position> - Overrides the font
169 specified or calculated values for overline thickness and/or position.
170  
171 This may improve results for some fonts.
172  
173 Note that overline thickness will default to underline thickness, if
174 set.
175  
176 =back
177  
178 =back
179  
180 =cut
181  
182 73 ▶sub register_font {
Subroutine register_font: called CC=18 SCAR=36.0
Pod undocumented
183 73 ▶ shift if UNIVERSAL::isa( $_[0], __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 8 65 73
184 73 my $props;
185 73 ▶ $props = pop(@_) if UNIVERSAL::isa( $_[-1], 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[-1], "HASH") 0 73 73
186 73 my ( $font, $family, $style, $weight ) = @_;
187  
188 73 ▶ if ( $style && !$weight && $style =~ s/^(heavy|bold|semi(?:bold)?|medium|book|light)//i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style and not $weight and $style =~ s/^(heavy|bold|semi(?:bold)?|medium|book|light)//i) 34 39 73
Condition83%5/6
$style and not $weight and $style =~ s/^(heavy|bold|semi(?:bold)?|medium|book|light)//i
!ll&&!rl&&r
221734
$style and not $weight
!ll&&!rl&&r
22051
MC/DC66%2/3
$style and not $weight and $style =~ s/^(heavy|bold|semi(?:bold)?|medium|book|light)//i
$style not $weight $style =~ s/^(heavy|bold|semi(?:bold)?|medium|book|light)//i
Truth table75%3/4
A and not(B) and C
A= $styleB= not $weightC= $style =~ s/^(heavy|bold|semi(?:bold)?|medium|book|light)//i
ABCresult
0XX0
10X0
1100
1111
189 34 $weight = $1;
190 }
191 73 ▶ $style = _norm_style( $style // "normal" );
Condition100%2/2
$style // "normal"
l!l
5122
MC/DC100%1/1
$style // "normal"
$style
Truth table100%2/2
A
A= $style
Aresult
11
00
192 73 ▶ $weight = _norm_weight( $weight // "normal" );
Condition100%2/2
$weight // "normal"
l!l
3439
MC/DC100%1/1
$weight // "normal"
$weight
Truth table100%2/2
A
A= $weight
Aresult
11
00
193 73 my $ff;
194 73 ▶ if ( $font =~ /\.[ot]tf$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($font =~ /\.[ot]tf$/) { } 0 73 73
195 0 ▶ if ( $font =~ m;^/; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($font =~ m[^/]) { } 0 0 0
196 0 ▶ $ff = $font if -r -s $font;
Branch: 1 branch
BranchTrueFalseTotal
if -r -s $font 0 0 0
197 }
198 else {
199 0 foreach ( @dirs ) {
200 0 ▶ next unless -r -s "$_/$font";
Branch: 1 branch
BranchTrueFalseTotal
unless -r -s "$_/$font" 0 0 0
201 0 $ff = "$_/$font";
202 0 last;
203 }
204 }
205 }
206 else {
207 # Assume corefont.
208 73 $ff = $font
209 }
210  
211 73 ▶ croak("Cannot find font: ", $font, "\n") unless $ff;
Branch: 1 branch
BranchTrueFalseTotal
unless $ff 0 73 73
212  
213 73 foreach ( split(/\s*,\s*/, $family) ) {
214 73 $fonts{lc $_}->{$style}->{$weight}->{loader} = $loader;
215 73 $fonts{lc $_}->{$style}->{$weight}->{loader_data} = $ff;
216 73 ▶ warn("FC: Registered: $ff for ", lc($_), "-$style-$weight\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 73 73
217 73 ▶ next unless $props;
Branch: 1 branch
BranchTrueFalseTotal
unless $props 73 0 73
218 0 ▶ while ( my($k,$v) = each %$props ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$props) 0 0 0
219 0 $fonts{lc $_}->{$style}->{$weight}->{$k} = $v;
220 }
221 }
222  
223 }
224  
225 =over
226  
227 =item add_fontdirs( @dirs )
228  
229 Adds one or more file paths to be searched for font files.
230  
231 =back
232  
233 =cut
234  
235 0 ▶sub add_fontdirs {
Subroutine add_fontdirs: not called CC=4 SCAR=30.0
Pod documented
236 0 ▶ shift if UNIVERSAL::isa( $_[0], __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
237 0 my ( @d ) = @_;
238  
239 0 foreach ( @d ) {
240 0 ▶ unless ( -d -r -x ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d -r -x $_) 0 0 0
241 0 carp("Skipped font dir: $_ [$!]");
242 0 next;
243 }
244 0 push( @dirs, $_ );
245 }
246 }
247  
248 =over
249  
250 =item register_aliases( $family, $aliases, ... )
251  
252 Adds aliases for existing font families.
253  
254 Multiple aliases can be specified, e.g.
255  
256 $layout->register_aliases( "times", "serif, default" );
257  
258 or
259  
260 $layout->register_aliases( "times", "serif", "default" );
261  
262 =back
263  
264 =cut
265  
266 20 ▶sub register_aliases {
Subroutine register_aliases: called CC=5 SCAR=16.3
Pod documented
267 8 ▶ use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
268 20 ▶ shift if UNIVERSAL::isa( $_[0], __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 20 20
269 20 my ( $family, @aliases ) = @_;
270 carp("Unknown font family: $family")
271 20 ▶ unless exists $fonts{lc $family};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $fonts{lc $family} 0 20 20
272 20 foreach ( @aliases ) {
273 45 foreach ( split( /\s*,\s*/, $_ ) ) {
274 45 $fonts{lc $_} = dclone( $fonts{lc $family} );
275 }
276 }
277 }
278  
279 =over
280  
281 =item register_corefonts( %options )
282  
283 This is a convenience method that registers all built-in corefonts.
284  
285 Aliases for families C<serif>, C<sans>, and C<monospace> are added
286 unless $noaliases is specified.
287  
288 You do not need to call this method if you provide your own font
289 registrations.
290  
291 Options:
292  
293 =over
294  
295 =item aliases
296  
297 If true, register Serif, Sans and Mono as aliases for Times,
298 Helvetica and Courier.
299  
300 This is enabled by default and can be cancelled with C<noaliases>.
301  
302 =item noaliases
303  
304 If true, do not register Serif, Sans and Mono as aliases for Times,
305 Helvetica and Courier.
306  
307 =item remap
308  
309 Remap the core fonts to real TrueType or OpenType font files.
310  
311 Supported values are C<GNU_Free_Fonts> or C<free> to use the GNU Free Fonts
312 (http://ftp.gnu.org/gnu/freefont/) and C<tex> or C<tex-gyre> for the
313 TeX Gyre fonts (https://www.gust.org.pl/projects/e-foundry/tex-gyre/).
314  
315 =back
316  
317 =back
318  
319 =cut
320  
321 5 ▶sub register_corefonts {
Subroutine register_corefonts: called CC=13 SCAR=28.6
Pod documented
322 5 ▶ shift if UNIVERSAL::isa( $_[0], __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 5 0 5
323  
324 5 my %options;
325 5 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 5 5
326 0 $options{noaliases} = shift;
327 }
328 else {
329 5 %options = @_;
330 }
331 5 ▶ my $noaliases = defined($options{aliases}) ? !$options{aliases} : $options{noaliases};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'aliases'} ? : 0 5 5
332  
333 5 ▶ warn("FC: Registering corefonts\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 5 5
334  
335 5 register_font( "Times-Roman", "Times" );
336 5 register_font( "Times-Bold", "Times", "Bold" );
337 5 register_font( "Times-Italic", "Times", "Italic" );
338 5 register_font( "Times-BoldItalic", "Times", "BoldItalic" );
339  
340 5 ▶ register_aliases( "Times", "Serif" )
Branch: 1 branch
BranchTrueFalseTotal
unless $noaliases 5 0 5
341 unless $noaliases;
342  
343 5 register_font( "Helvetica", "Helvetica" );
344 5 register_font( "Helvetica-Bold", "Helvetica", "Bold" );
345 5 register_font( "Helvetica-Oblique", "Helvetica", "Oblique" );
346 5 register_font( "Helvetica-BoldOblique", "Helvetica", "BoldOblique" );
347  
348 5 ▶ register_aliases( "Helvetica", "Sans", "Arial" )
Branch: 1 branch
BranchTrueFalseTotal
unless $noaliases 5 0 5
349 unless $noaliases;
350  
351 5 register_font( "Courier", "Courier" );
352 5 register_font( "Courier-Bold", "Courier", "Bold" );
353 5 register_font( "Courier-Oblique", "Courier", "Italic" );
354 5 register_font( "Courier-BoldOblique", "Courier", "BoldItalic" );
355  
356 5 ▶ register_aliases( "Courier", "Mono", "Monospace", "fixed" )
Branch: 1 branch
BranchTrueFalseTotal
unless $noaliases 5 0 5
357 unless $noaliases;
358 5 ▶ register_aliases( "Courier", "Mono", "Monospace", "fixed" )
Branch: 1 branch
BranchTrueFalseTotal
unless $noaliases 5 0 5
359 unless $noaliases;
360  
361 5 register_font( "ZapfDingbats", "Dingbats" );
362  
363 5 if ( 0 ) {
364 register_font( "Georgia", "Georgia" );
365 register_font( "Georgia,Bold", "Georgia", "Bold" );
366 register_font( "Georgia,Italic", "Georgia", "Italic" );
367 register_font( "Georgia,BoldItalic", "Georgia", "BoldItalic" );
368  
369 register_font( "Verdana", "Verdana" );
370 register_font( "Verdana,Bold", "Verdana", "Bold" );
371 register_font( "Verdana,Italic", "Verdana", "Italic" );
372 register_font( "Verdana,BoldItalic", "Verdana", "BoldItalic" );
373  
374 register_font( "WebDings", "WebDings" );
375 register_font( "WingDings", "WingDings" );
376 }
377  
378 # Corefont remapping to real font files.
379 # Biggest problem is to make sure the fonts are installed, and with
380 # the file names used here...
381 5 ▶ $options{remap} //= "";
Condition50%1/2
$options{"remap"} //= ""
l!l
05
MC/DC0%0/1
$options{"remap"} //= ""
$options{"remap"}
Truth table50%1/2
A
A= $options{"remap"}
Aresult
11
00
382  
383 # GNU Free Fonts.
384 # http://ftp.gnu.org/gnu/freefont/freefont-ttf-20120503.zip
385 5 ▶ if ( $options{remap} =~ /^(?:gnu[-_]?)?free(?:[-_]?fonts)?$/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options{'remap'} =~ /^(?:gnu[-_]?)?free(?:[-_]?fonts)?$/i) { } 0 5 5
elsif ($options{'remap'} =~ /^tex(?:[-_]?gyre)?$/i) { } 0 5 5
elsif ($options{'remap'}) { } 0 5 5
386 0 remap( 'Times-Roman' => "FreeSerif.ttf",
387 'Times-BoldItalic' => "FreeSerifBoldItalic.ttf",
388 'Times-Bold' => "FreeSerifBold.ttf",
389 'Times-Italic' => "FreeSerifItalic.ttf",
390 'Helvetica' => "FreeSans.ttf",
391 'Helvetica-BoldOblique' => "FreeSansBoldOblique.ttf",
392 'Helvetica-Bold' => "FreeSansBold.ttf",
393 'Helvetica-Oblique' => "FreeSansOblique.ttf",
394 'Courier' => "FreeMono.ttf",
395 'Courier-BoldOblique' => "FreeMonoBoldOblique.ttf",
396 'Courier-Bold' => "FreeMonoBold.ttf",
397 'Courier-Oblique' => "FreeMonoOblique.ttf",
398 );
399 }
400  
401 # TeX Gyre fonts.
402 # https://www.gust.org.pl/projects/e-foundry/tex-gyre/whole/tg2_501otf.zip
403 elsif ( $options{remap} =~ /^tex(?:[-_]?gyre)?$/i ) {
404 0 remap( 'Times-Roman' => "texgyretermes-regular.otf",
405 'Times-BoldItalic' => "texgyretermes-bolditalic.otf",
406 'Times-Bold' => "texgyretermes-bold.otf",
407 'Times-Italic' => "texgyretermes-italic.otf",
408 'Helvetica' => "texgyreheros-regular.otf",
409 'Helvetica-BoldOblique' => "texgyreheros-bolditalic.otf",
410 'Helvetica-Bold' => "texgyreheros-bold.otf",
411 'Helvetica-Oblique' => "texgyreheros-italic.otf",
412 'Courier' => "texgyrecursor-regular.otf",
413 'Courier-BoldOblique' => "texgyrecursor-bolditalic.otf",
414 'Courier-Bold' => "texgyrecursor-bold.otf",
415 'Courier-Oblique' => "texgyrecursor-italic.otf",
416 );
417 }
418 elsif ( $options{remap} ) {
419 0 croak("Unrecognized core remap set");
420 }
421 }
422  
423 =over
424  
425 =item remap($font)
426  
427 =item remap( $src => $dst, ... )
428  
429 Handles font remapping. The main purpose is to remap corefonts to real
430 fonts.
431  
432 With a single argument, returns the remapped value, or undef if none.
433  
434 With a hash argument, maps each of the targets (keys) to a font file
435 (value). This file must be present in one of the font directories.
436  
437 Alternatively, the key may be one of C<Times>, C<Helvetica> and
438 C<Courier> and the value an already registered family.
439  
440 =back
441  
442 =cut
443  
444 3 ▶sub remap {
Subroutine remap: called CC=14 SCAR=51.3
Pod documented
445 3 ▶ shift if UNIVERSAL::isa( $_[0], __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 3 0 3
446  
447 3 ▶ return $maps{$_[0]} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 3 0 3
448  
449 0 my %m = @_;
450 0 ▶ while ( my ($k, $v ) = each %m ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %m) 0 0 0
451  
452 # Check for family map.
453 0 ▶ if ( $k =~ /^(Courier|Times|Helvetica)$/
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^(Courier|Times|Helvetica)$/ and defined $fonts{lc $v}) 0 0 0
Condition0%0/3
$k =~ /^(Courier|Times|Helvetica)$/ and defined $fonts{lc $v}
!ll&&!rl&&r
000
MC/DC0%0/2
$k =~ /^(Courier|Times|Helvetica)$/ and defined $fonts{lc $v}
$k =~ /^(Courier|Times|Helvetica)$/ defined $fonts{lc $v}
Truth table0%0/3
A and B
A= $k =~ /^(Courier|Times|Helvetica)$/B= defined $fonts{lc $v}
ABresult
0X0
100
111
454 && defined $fonts{lc $v} ) {
455 0 ▶ if ( $k eq 'Courier' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($k eq 'Courier') { } 0 0 0
elsif ($k eq 'Helvetica') { } 0 0 0
elsif ($k eq 'Times') { } 0 0 0
456 0 $maps{'Courier'} = $fonts{lc $v}{normal}{normal}{loader_data};
457 0 $maps{'Courier-Bold'} = $fonts{lc $v}{normal}{bold}{loader_data};
458 0 $maps{'Courier-Oblique'} = $fonts{lc $v}{italic}{normal}{loader_data};
459 0 $maps{'Courier-BoldOblique'} = $fonts{lc $v}{italic}{bold}{loader_data};
460 }
461 elsif ( $k eq 'Helvetica' ) {
462 0 $maps{'Helvetica'} = $fonts{lc $v}{normal}{normal}{loader_data};
463 0 $maps{'Helvetica-Bold'} = $fonts{lc $v}{normal}{bold}{loader_data};
464 0 $maps{'Helvetica-Oblique'} = $fonts{lc $v}{italic}{normal}{loader_data};
465 0 $maps{'Helvetica-BoldOblique'} = $fonts{lc $v}{italic}{bold}{loader_data};
466 }
467 elsif ( $k eq 'Times' ) {
468 0 $maps{'Times-Roman'} = $fonts{lc $v}{normal}{normal}{loader_data};
469 0 $maps{'Times-Bold'} = $fonts{lc $v}{normal}{bold}{loader_data};
470 0 $maps{'Times-Italic'} = $fonts{lc $v}{italic}{normal}{loader_data};
471 0 $maps{'Times-BoldItalic'} = $fonts{lc $v}{italic}{bold}{loader_data};
472 }
473 0 next;
474 }
475  
476 # Map font to corefont.
477 0 my $ff;
478 0 ▶ if ( $v =~ m;^/; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ m[^/]) { } 0 0 0
479 0 ▶ $ff = $v if -r -s $v;
Branch: 1 branch
BranchTrueFalseTotal
if -r -s $v 0 0 0
480 }
481 else {
482 0 foreach ( @dirs ) {
483 0 ▶ next unless -r -s "$_/$v";
Branch: 1 branch
BranchTrueFalseTotal
unless -r -s "$_/$v" 0 0 0
484 0 $ff = "$_/$v";
485 0 last;
486 }
487 }
488  
489 0 ▶ $maps{$k} = $ff
Branch: 1 branch
BranchTrueFalseTotal
$maps{$k} = $ff or carp("Invalid font mapping (${v}: $!)") 0 0 0
490 or carp("Invalid font mapping ($v: $!)")
491  
492 }
493  
494 0 1;
495 }
496  
497 =over
498  
499 =item find_font( $family, $style, $weight )
500  
501 Returns a font descriptor based on the given family, style and weight.
502  
503 On Linux, fallback using fontconfig.
504  
505 =back
506  
507 =cut
508  
509 27 ▶sub find_font {
Subroutine find_font: called CC=9 SCAR=26.9
Pod documented
510 27 ▶ shift if UNIVERSAL::isa( $_[0], __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 20 7 27
511 27 my $atts;
512 27 ▶ $atts = pop(@_) if UNIVERSAL::isa( $_[-1], 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[-1], "HASH") 0 27 27
513 27 my ( $family, $style, $weight ) = @_;
514 27 ▶ warn("FC: find_font( $family, $style, $weight )\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 27 27
515  
516 27 ▶ my $try = sub {
Subroutine __ANON__: called CC=8 SCAR=24.7
517 27 ▶ if ( $fonts{$family}
Branch: 1 branch
BranchTrueFalseTotal
if ($fonts{$family} and $fonts{$family}{$style} and $fonts{$family}{$style}{$weight}) 27 0 27
Condition33%2/6
$fonts{$family} and $fonts{$family}{$style} and $fonts{$family}{$style}{$weight}
!ll&&!rl&&r
0027
$fonts{$family} and $fonts{$family}{$style}
!ll&&!rl&&r
0027
MC/DC0%0/3
$fonts{$family} and $fonts{$family}{$style} and $fonts{$family}{$style}{$weight}
$fonts{$family} $fonts{$family}{$style} $fonts{$family}{$style}{$weight}
Truth table25%1/4
A and B and C
A= $fonts{$family}B= $fonts{$family}{$style}C= $fonts{$family}{$style}{$weight}
ABCresult
0XX0
10X0
1100
1111
518 && $fonts{$family}->{$style}
519 && $fonts{$family}->{$style}->{$weight} ) {
520  
521 27 my $ff = $fonts{$family}->{$style}->{$weight};
522 27 my %i = ( family => $family,
523 style => $style,
524 weight => $weight );
525 ;
526 27 ▶ if ( $ff->{font} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ff->{'font'}) { } 0 27 27
elsif ($ff->{'loader_data'}) { } 27 0 27
527 0 $i{font} = $ff->{font};
528 }
529 elsif ( $ff->{loader_data} ) {
530 27 $i{loader_data} = $ff->{loader_data};
531 27 $i{loader} = $loader;
532 27 $i{cache} = $ff;
533 }
534 else {
535 0 return;
536 }
537  
538 27 for ( qw( shaping ascender descender nosubset direction language
539 underline_thickness underline_position
540 strikeline_thickness strikeline_position
541 overline_thickness overline_position
542 ) ) {
543 324 $i{$_} = $ff->{$_};
544 }
545  
546 27 ▶ if ( $debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 27 27
547 warn("FC: found( $i{family}, $i{style}, $i{weight} ) -> ",
548 0 $i{loader_data}, "\n");
549 }
550 27 return Text::Layout::FontDescriptor->new(%i);
551 }
552 27 };
553  
554 27 ▶ $style = _norm_style( $style // "normal" );
Condition50%1/2
$style // "normal"
l!l
270
MC/DC0%0/1
$style // "normal"
$style
Truth table50%1/2
A
A= $style
Aresult
11
00
555 27 ▶ $weight = _norm_weight( $weight // "normal" );
Condition50%1/2
$weight // "normal"
l!l
270
MC/DC0%0/1
$weight // "normal"
$weight
Truth table50%1/2
A
A= $weight
Aresult
11
00
556 27 my $res = $try->();
557 27 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 27 0 27
558  
559 # TODO: Some form of font fallback.
560 0 ▶ if ( _fallback( $family, $style, $weight ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_fallback($family, $style, $weight)) 0 0 0
561 0 $res = $try->();
562 0 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
563 }
564  
565 # Nope.
566 0 croak("Cannot find font: $family $style $weight\n");
567 }
568  
569 =over
570  
571 =item from_string( $description )
572  
573 Returns a font descriptor using a Pango-style font description, e.g.
574 C<Sans Italic 14>.
575  
576 On Linux, fallback using fontconfig.
577  
578 =back
579  
580 =cut
581  
582 8 my $stylep = qr/^(?:heavy|bold|semi(?:bold)?|medium|book|light)? (oblique|italic)$/ix;
583 8 my $weightp = qr/^(heavy|bold|semi(?:bold)?|medium|book|light) (?:oblique|italic)?$/ix;
584  
585 7 ▶sub from_string {
Subroutine from_string: called CC=4 SCAR=15.0
Pod documented
586 7 ▶ shift if UNIVERSAL::isa( $_[0], __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 7 0 7
587 7 my ( $description ) = @_;
588  
589 7 my $i = parse($description);
590  
591 7 my $res = find_font( $i->{family}, $i->{style}, $i->{weight} );
592 7 ▶ $res->set_size($i->{size}) if $res && $i->{size};
Branch: 1 branch
BranchTrueFalseTotal
if $res and $i->{"size"} 7 0 7
Condition33%1/3
$res and $i->{"size"}
!ll&&!rl&&r
007
MC/DC0%0/2
$res and $i->{"size"}
$res $i->{"size"}
Truth table33%1/3
A and B
A= $resB= $i->{"size"}
ABresult
0X0
100
111
593 7 $res;
594 }
595  
596 =over
597  
598 =item parse( $description )
599  
600 Parses a Pango-style font description and returns a hash ref with keys
601 C<family>, C<style>, C<weight>, and C<size>.
602  
603 Unspecified items are returned as empty strings or, in the case of
604 C<size>, zero.
605  
606 =back
607  
608 =cut
609  
610 7 ▶sub parse {
Subroutine parse: called CC=10 SCAR=33.0
Pod documented
611 7 ▶ shift if UNIVERSAL::isa( $_[0], __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 7 7
612 7 my ( $description ) = @_;
613  
614 7 my $family = "";
615 7 my $style = "";
616 7 my $weight = "";
617 7 my $size = 0;
618  
619 7 my @p = split( ' ', $description );
620 7 ▶ $size = pop(@p) if $p[-1] =~ /^\d+(?:\.\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $p[-1] =~ /^\d+(?:\.\d+)?$/ 7 0 7
621  
622 7 for ( @p ) {
623 7 my $t = lc;
624 7 ▶ if ( ! $family ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $family) { } 7 0 7
elsif ($t =~ /$stylep/) { } 0 0 0
elsif ($t =~ /$weightp/) { } 0 0 0
elsif ($t eq 'normal') { } 0 0 0
625 7 $family = $t;
626 }
627 elsif ( $t =~ $stylep ) {
628 0 $style = "italic";
629 0 ▶ $weight = $1 if $t =~ $weightp;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /$weightp/ 0 0 0
630 }
631 elsif ( $t =~ $weightp ) {
632 0 $weight = $1;
633 0 ▶ $style = $1 if $t =~ $stylep;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /$stylep/ 0 0 0
634 }
635 elsif ( $t eq "normal" ) {
636 0 $style = $weight = "";
637 }
638 else {
639 0 carp("Unknown font property: $t");
640 0 $family .= " " . $_;
641 }
642 }
643  
644 7 return { family => $family,
645 style => $style,
646 weight => $weight,
647 size => $size,
648 };
649 }
650  
651 =over
652  
653 =item from_filename( $filename )
654  
655 Returns a font descriptor from a filename. Tries to infer Pango data
656 from the name.
657  
658 =back
659  
660 =cut
661  
662 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
663  
664 0 ▶sub from_filename {
Subroutine from_filename: not called CC=10 SCAR=47.0
Pod documented
665 0 ▶ shift if UNIVERSAL::isa( $_[0], __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
666 0 my ( $file, $size ) = @_;
667 0 ▶ ( $file, my $sel ) = ( $1, $2 ) if $file =~ /^(.*\.ttc)(:.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^(.*\.ttc)(:.*)/ 0 0 0
668 0 ( my $b, undef, undef ) = fileparse( $file, qr/\.\w+/ );
669 0 my ( $family, $style, $weight ) = ( $b, "normal", "normal" );
670  
671 0 ▶ if ( lc($b) =~ m/^
Branch: 1 branch
BranchTrueFalseTotal
if (lc($b) =~ /^ ( .*? ) -? (roman?|normal|regular)? (light|book|medium|semi(?:bold)?|bold|heavy)? (italic|ital|oblique|obli)? $/xi) 0 0 0
672 ( .*? )
673 -?
674 (roman?|normal|regular)?
675 (light|book|medium|semi(?:bold)?|bold|heavy)?
676 (italic|ital|oblique|obli)?
677 $/ix ) {
678 0 ▶ $family = $1 if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 0 0 0
679 0 ▶ $style = "italic" if $4;
Branch: 1 branch
BranchTrueFalseTotal
if $4 0 0 0
680 0 ▶ $weight = $3 if $3;
Branch: 1 branch
BranchTrueFalseTotal
if $3 0 0 0
681 }
682  
683 0 ▶ my $fd = Text::Layout::FontDescriptor->new
Branch: 1 branch
BranchTrueFalseTotal
$size ? : 0 0 0
Condition0%0/2
$sel // ""
l!l
00
MC/DC0%0/1
$sel // ""
$sel
Truth table0%0/2
A
A= $sel
Aresult
11
00
684 ( loader_data => $file.($sel//""),
685 loader => $loader,
686 family => $family,
687 style => $style,
688 weight => $weight,
689 $size ? ( size => $size ) : (),
690 );
691  
692 0 ▶ $fonts{$family}{$style}{$weight} //= $fd;
Condition0%0/3
$fonts{$family}{$style}{$weight} //= $fd
l!l&&r!l&&!r
000
MC/DC0%0/2
$fonts{$family}{$style}{$weight} //= $fd
$fonts{$family}{$style}{$weight} $fd
Truth table0%0/3
A //= B
A= $fonts{$family}{$style}{$weight}B= $fd
ABresult
1X1
011
000
693  
694 0 return $fd;
695 }
696  
697 ################ Helper Routines ################
698  
699 0 ▶sub set_loader {
Subroutine set_loader: not called CC=3 SCAR=24.8
Pod undocumented
700 0 ▶ shift if UNIVERSAL::isa( $_[0], __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
701 0 $loader = shift;
702 0 ▶ croak("Font loader must be a code reference")
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($loader, "CODE") 0 0 0
703 unless UNIVERSAL::isa( $loader, "CODE" );
704 }
705  
706 # Normalize (and check) a style specification.
707  
708 109 ▶sub _norm_style {
Subroutine _norm_style: called CC=3 SCAR=11.0
709 109 my ( $style ) = @_;
710 109 $style = lc $style;
711 109 ▶ return "italic" if $style =~ $stylep;
Branch: 1 branch
BranchTrueFalseTotal
if $style =~ /$stylep/ 57 52 109
712  
713 52 ▶ carp("Unhandled font style: $style\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $style =~ /^(regular|normal)?$/ 0 52 52
714 unless $style =~ /^(regular|normal)?$/;
715  
716 52 return "normal";
717 }
718  
719 # Normalize (and check) a weight specification.
720  
721 110 ▶sub _norm_weight {
Subroutine _norm_weight: called CC=3 SCAR=11.0
722 110 my ( $weight ) = @_;
723 110 $weight = lc $weight;
724 110 ▶ return $1 if $weight =~ $weightp;
Branch: 1 branch
BranchTrueFalseTotal
if $weight =~ /$weightp/ 56 54 110
725  
726 54 ▶ carp("Unhandled font weight: $weight\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $weight =~ /^(regular|normal)?$/ 0 54 54
727 unless $weight =~ /^(regular|normal)?$/;
728  
729 54 return "normal";
730 }
731  
732 8 my $fallback;
733  
734 0 ▶sub _fallback {
Subroutine _fallback: not called CC=14 SCAR=53.5
735 0 ▶ unless ( defined $fallback ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fallback) 0 0 0
736 0 $fallback = '';
737 0 foreach ( split( /:/, $ENV{PATH} ) ) {
738 0 ▶ next unless -f -x "$_/fc-match";
Branch: 1 branch
BranchTrueFalseTotal
unless -f -x "$_/fc-match" 0 0 0
739 0 $fallback = "$_/fc-match";
740 0 last;
741 }
742 }
743 0 ▶ return unless $fallback;
Branch: 1 branch
BranchTrueFalseTotal
unless $fallback 0 0 0
744  
745 0 my ( $family, $style, $weight ) = @_;
746 0 ▶ warn("FC: fallback( $family, $style, $weight )\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
747  
748 0 my $pattern = $family;
749 0 ▶ $pattern .= ":$style" if $style;
Branch: 1 branch
BranchTrueFalseTotal
if $style 0 0 0
750 0 ▶ $pattern .= ":$weight" if $weight;
Branch: 1 branch
BranchTrueFalseTotal
if $weight 0 0 0
751  
752 open( my $fd, '-|',
753 $fallback, '-s', '--format=%{file}\n', $pattern )
754 0 ▶ or do { $fallback = ''; return };
Branch: 1 branch
BranchTrueFalseTotal
open my $fd, "-|", $fallback, "-s", "--format=%{file}\\n", $pattern or do { $fallback = ""; return } 0 0 0
755  
756 0 my $res;
757 0 local $_;
758 0 ▶ while ( <$fd> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fd)) 0 0 0
759 0 chomp;
760 0 ▶ next unless -f -r $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -f -r $_ 0 0 0
761 0 ▶ next unless /\.[ot]tf$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless /\.[ot]tf$/i 0 0 0
762 0 $res = $_;
763 0 last;
764 }
765  
766 0 close($fd);
767 0 ▶ register_font( $res, $family, $style, $weight ) if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
768 0 ▶ warn("FC: Lookup $pattern -> $res\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
769 0 return $res;
770 }
771  
772 =head1 SEE ALSO
773  
774 L<Text::Layout>, L<Text::Layout::FontDescriptor>.
775  
776 =head1 AUTHOR
777  
778 Johan Vromans, C<< <JV at CPAN dot org> >>
779  
780 =head1 SUPPORT
781  
782 This module is part of <Text::Layout>.
783  
784 Development takes place on GitHub:
785 L<https://github.com/sciurius/perl-Text-Layout>.
786  
787 You can find documentation for this module with the perldoc command.
788  
789 perldoc Text::Layout::FontConfig
790  
791 Please report any bugs or feature requests using the issue tracker for
792 Text::Layout on GitHub.
793  
794 =head1 LICENSE
795  
796 See L<Text::Layout>, L<Text::Layout::FontDescriptor>, L<HarfBuzz::Shaper>.
797  
798 =cut
799  
800 0 ▶sub _dump {
Subroutine _dump: not called CC=16 SCAR=56.1
801 0 foreach my $family ( sort keys %fonts ) {
802 0 foreach my $style ( qw( normal italic ) ) {
803 0 foreach my $weight ( qw( normal light book medium semi semibold bold heavy ) ) {
804 0 my $f = $fonts{$family}{$style}{$weight};
805 0 ▶ next unless $f;
Branch: 1 branch
BranchTrueFalseTotal
unless $f 0 0 0
806 printf STDERR ( "%-13s %s%s%s%s%s %s\n",
807 $family,
808 $style eq 'normal' ? "-" : "i",
809 $weight eq 'bold' ? "b"
810 : $weight eq 'light' ? "l"
811 : $weight eq 'book' ? "k"
812 : $weight eq 'medium' ? "m"
813 : $weight eq 'semi' ? "s"
814 : $weight eq 'semibold' ? "s"
815 : $weight eq 'heavy' ? "h"
816 : "-",
817 $f->{shaping} ? "s" : "-",
818 $f->{interline} ? "l" : "-",
819 $f->{font} ? "+" : " ",
820 $f->{loader_data},
821 0 ▶ );
Branch: 11 branches
BranchTrueFalseTotal
$style eq 'normal' ? : 0 0 0
$weight eq 'bold' ? : 0 0 0
$weight eq 'light' ? : 0 0 0
$weight eq 'book' ? : 0 0 0
$weight eq 'medium' ? : 0 0 0
$weight eq 'semi' ? : 0 0 0
$weight eq 'semibold' ? : 0 0 0
$weight eq 'heavy' ? : 0 0 0
$f->{'shaping'} ? : 0 0 0
$f->{'interline'} ? : 0 0 0
$f->{'font'} ? : 0 0 0
822 }
823 }
824 }
825 }
826  
827 # END { _dump }
828  
829 8 1;
« Layout.pm ↑ Summary Layout/Markdown.pm »