File Coverage

blib/lib/Text/Amuse/Compile/Fonts/Family.pm
Criterion Covered Total %
statement 64 65 98.4
branch 8 14 57.1
condition 7 9 77.7
subroutine 18 18 100.0
pod 9 9 100.0
total 106 115 92.1


line stmt bran cond sub pod time code
1             package Text::Amuse::Compile::Fonts::Family;
2 62     62   491 use utf8;
  62         166  
  62         394  
3 62     62   2003 use strict;
  62         142  
  62         1274  
4 62     62   329 use warnings;
  62         152  
  62         2121  
5 62     62   404 use Types::Standard qw/Str Enum StrMatch InstanceOf Bool HashRef ArrayRef/;
  62         199  
  62         428  
6 62     62   75403 use Moo;
  62         33643  
  62         363  
7 62     62   25909 use Text::Amuse::Utils;
  62         3970  
  62         65031  
8              
9             =head1 NAME
10              
11             Text::Amuse::Compile::Fonts::Family - font family object
12              
13             =head1 ACCESSORS
14              
15             =head2 name
16              
17             The font family name. Required.
18              
19             =head2 desc
20              
21             The font family description. Arbitrary string. Required.
22              
23             =head2 type
24              
25             The font type: must be serif, sans or mono.
26              
27             =head1 FONT FILES
28              
29             These accessors aren't strictly required. If provided, they should be
30             an instance of L.
31              
32             =head2 regular
33              
34             =head2 italic
35              
36             =head2 bold
37              
38             =head2 bolditalic
39              
40             =head2 languages
41              
42             An optional arrayref of language codes.
43              
44             =head1 METHODS
45              
46             =head2 has_files
47              
48             Return true if all the 4 font slots are filled. This means we know the
49             physical location of the files, not just its name.
50              
51             =head2 is_sans
52              
53             Return true if the family is a sans font
54              
55             =head2 is_mono
56              
57             Return true if the family is a mono font
58              
59             =head2 is_serif
60              
61             Return true if the family is a serif font
62              
63             =head2 font_files
64              
65             Return an arrayref with the four L
66             objects.
67              
68             =head2 language_names
69              
70             An arrayref with the C codes mapped to their babel equivalent.
71              
72             =head2 has_languages
73              
74             Return true if the font family has languages set.
75              
76             =head2 for_babel_language($babel_lang)
77              
78             =head2 for_language_code($iso_code)
79              
80             Return true if the family has the given language set (babel and iso version)
81              
82             =head2 babel_font_name
83              
84             =head2 babel_font_options
85              
86             =cut
87              
88              
89             has name => (is => 'ro',
90             isa => StrMatch[ qr{\A[a-zA-Z0-9 ]+\z} ],
91             required => 1);
92              
93             has desc => (is => 'ro',
94             isa => Str,
95             required => 1);
96              
97             has type => (is => 'ro',
98             required => 1,
99             isa => Enum[qw/serif sans mono/]);
100              
101             has regular => (is => 'ro', isa => InstanceOf[qw/Text::Amuse::Compile::Fonts::File/]);
102             has italic => (is => 'ro', isa => InstanceOf[qw/Text::Amuse::Compile::Fonts::File/]);
103             has bold => (is => 'ro', isa => InstanceOf[qw/Text::Amuse::Compile::Fonts::File/]);
104             has bolditalic => (is => 'ro', isa => InstanceOf[qw/Text::Amuse::Compile::Fonts::File/]);
105              
106             has has_files => (is => 'lazy', isa => Bool);
107              
108             sub _build_has_files {
109 659     659   20977 my $self = shift;
110 659 50 100     2803 if ($self->regular &&
      66        
      66        
111             $self->italic &&
112             $self->bold &&
113             $self->bolditalic) {
114 27         489 return 1;
115             }
116 632         9862 return 0;
117             }
118              
119             has languages => (is => 'ro', isa => ArrayRef, default => sub { [] });
120              
121             has language_names => (is => 'lazy', isa => ArrayRef);
122              
123             sub _build_language_names {
124 10137     10137   79332 my $self = shift;
125 10137         13929 return [ map { Text::Amuse::Utils::get_latex_lang($_) } @{ $self->languages } ];
  1595         4674  
  10137         139697  
126             }
127              
128             has babel_font_args => (is => 'lazy', isa => HashRef);
129              
130             sub _build_babel_font_args {
131 628     628   6743 my $self = shift;
132 628         1820 my $name = $self->name;
133 628         1175 my @args;
134 628 100       10293 if ($self->has_files) {
135 22         300 my $regular;
136 22 50       113 if ($self->regular->dirname =~ m/\A([A-Za-z0-9\.\/_-]+)\z/) {
137 22         437 push @args, Path => $1;
138 22         91 $name = $regular = $self->regular->basename_and_ext;
139             }
140             else {
141 0         0 warn $self->regular->dirname . " does not look like a path which can be embedded." .
142             " Please make sure the fonts are installed in a standard TeX location\n";
143             }
144 22 50       192 if ($regular) {
145 22         106 my %shapes = (
146             bold => 'BoldFont',
147             italic => 'ItalicFont',
148             bolditalic => 'BoldItalicFont',
149             );
150 22         104 foreach my $shape (sort keys %shapes) {
151 66 50       276 if (my $file = $self->$shape->basename_and_ext) {
152 66         912 push @args, $shapes{$shape}, $file;
153             }
154             }
155             }
156             }
157             return {
158 628         23423 name => $name,
159             opts => \@args,
160             };
161             }
162              
163             sub babel_font_name {
164 868     868 1 15060 shift->babel_font_args->{name};
165             }
166              
167             sub babel_font_options {
168 868     868 1 3765 my ($self, @args) = @_;
169 868 50       2626 die "args must come in pairs" if @args % 2;
170 868 50       1549 push @args, @{$self->babel_font_args->{opts} || [] };
  868         16260  
171 868         17670 my @list;
172 868         3525 while (my @pair = splice @args, 0, 2) {
173 697         3243 push @list, join('=', @pair);
174             }
175 868         3593 return join(",%\n ", @list)
176             }
177              
178             sub is_serif {
179 24837     24837 1 54687 return shift->type eq 'serif';
180             }
181              
182             sub is_mono {
183 24837     24837 1 55172 return shift->type eq 'mono';
184             }
185              
186             sub is_sans {
187 24733     24733 1 55608 return shift->type eq 'sans';
188             }
189              
190             sub font_files {
191 24     24 1 5190 my $self = shift;
192 24         217 return [ $self->regular, $self->italic, $self->bold, $self->bolditalic ];
193             }
194              
195             sub has_languages {
196 13787     13787 1 23040 return scalar(@{shift->language_names});
  13787         222983  
197             }
198              
199             sub for_babel_language {
200 2255     2255 1 227316 my ($self, $lang) = @_;
201 2255         3749 return scalar(grep { $lang eq $_ } @{$self->language_names});
  2243         24534  
  2255         36830  
202             }
203              
204             sub for_language_code {
205 3     3 1 725 my ($self, $lang) = @_;
206 3         7 return scalar(grep { $lang eq $_ } @{$self->languages});
  4         19  
  3         15  
207             }
208              
209              
210             1;