lib/Graphics/Penplotter/GcodeXY/Font.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« script/template.pl ↑ Summary
1 package Graphics::Penplotter::GcodeXY::Font v0.9.4;
2  
3 9 ▶use v5.38.2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use feature qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶no warnings qw( experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Readonly qw( Readonly );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use Font::FreeType qw( FT_LOAD_NO_HINTING );
Subroutine BEGIN: called CC=1 SCAR=2.6
12  
13 # ---------------------------------------------------------------------------
14 # Graphics::Penplotter::GcodeXY::Font
15 # Role providing TrueType font rendering for GcodeXY.
16 # ---------------------------------------------------------------------------
17  
18 requires qw(_croak _flushPsegments newpath stroke gsave grestore translate _dohatching);
19  
20 # ---------------------------------------------------------------------------
21 # Constants -- private copies of values defined in GcodeXY.pm.
22 # ---------------------------------------------------------------------------
23  
24 Readonly my $EMPTY_STR => q{};
25 Readonly my $SPACE => q{ };
26 Readonly my $EOL => qq{\n};
27  
28 # ---------------------------------------------------------------------------
29 # Module-level font search state
30 # ---------------------------------------------------------------------------
31  
32 my $home = $ENV{'HOME'};
33 my @locations = (
34 './',
35 $home . '/.fonts/',
36 $home . '/.local/share/fonts/',
37 '/usr/share/fonts/truetype/',
38 '/usr/share/fonts/truetype/liberation/',
39 '/usr/share/fonts/truetype/dejavu/',
40 '/usr/share/fonts/truetype/msttcorefonts/',
41 '/usr/share/fonts/',
42 '/usr/local/share/fonts/',
43 'C:/Windows/Fonts/',
44 );
45  
46 # ===========================================================================
47 # FONT METHODS
48 # ===========================================================================
49  
50 # Find, open and size a TrueType font face.
51 # Returns a Font::FreeType::Face object, or undef on failure.
52 sub setfont ($self, $font, $size = undef) {
53 if ( !defined $font ) {
54 $self->_croak('setfont: no font name specified');
55 return undef;
56 }
57 my $nam = $self->findfont($font);
58 if ( $nam eq $EMPTY_STR ) {
59 $self->_croak( 'setfont: font ' . $font . ' not found' );
60 return undef;
61 }
62 my $freetype = Font::FreeType->new;
63 my $face = $freetype->face( $nam, load_flags => FT_LOAD_NO_HINTING );
64 if ( !defined $size ) {
65 $size = $self->{fontsize};
66 }
67 if ( !defined $size ) {
68 $self->_croak('setfont: no font size specified');
69 return undef;
70 }
71 $self->{fontsize} = $size;
72 $self->{fontname} = $nam;
73 $face->set_char_size( $size, $size, 72, 72 );
74 return $face;
75 }
76  
77 # Globally set the current font size (does not open a font face).
78 sub setfontsize ($self, $size) {
79 if ( !defined $size ) {
80 $self->_croak('wrong number of args for setfontsize');
81 return 0;
82 }
83 $self->{fontsize} = $size;
84 return 1;
85 }
86  
87 # Locate a font file by name, searching @locations.
88 # Returns the full path, or the empty string if not found.
89 sub findfont ($self, $name) {
90 if ( !defined $name ) {
91 $self->_croak('expecting 1 argument for findfont');
92 return 0;
93 }
94 # Expand ~ to $HOME
95 $name =~ s{\N{TILDE}}{$home};
96 # If an absolute or relative path, return it directly if it exists
97 if ( ( $name =~ m{\A\N{SOLIDUS}} )
98 || ( $name =~ m{\A\N{FULL STOP}\N{FULL STOP}\N{SOLIDUS}} )
99 || ( $name =~ m{\A\N{FULL STOP}\N{SOLIDUS}} ) )
100 {
101 return -e $name ? $name : $EMPTY_STR;
102 }
103 # Otherwise search the location list
104 for my $dir (@locations) {
105 my $path = $dir . $name;
106 return $path if -f $path;
107 }
108 return $EMPTY_STR;
109 }
110  
111 # Add one or more directories to the font search path.
112 sub addfontpath ($self, @paths) {
113 if ( !@paths ) {
114 $self->_croak('addfontpath: missing parameter(s)');
115 return 0;
116 }
117 for my $path (@paths) {
118 $path =~ s{\A\N{TILDE}}{$home};
119 # Ensure trailing separator
120 if ( ( $path !~ m{\N{SOLIDUS}\z} )
121 && ( $path !~ m{\N{REVERSE SOLIDUS}\z} ) )
122 {
123 $path .= '/';
124 }
125 push @locations, $path;
126 }
127 return 1;
128 }
129  
130 # Render a string without fill, advancing position after each glyph.
131 # The path is flushed after each character.
132 sub stroketext ($self, $face, $string) {
133 if ( !defined $string ) {
134 $self->_croak('stroketext: no string specified');
135 return 0;
136 }
137 if ( !defined $face ) {
138 $self->_croak('stroketext: no face specified');
139 return 0;
140 }
141 $self->stroke();
142 $self->_doglyphs( $face, $string, 0 );
143 return 1;
144 }
145  
146 # Render a string with hatch fill, advancing position after each glyph.
147 # The path is flushed after each character.
148 sub stroketextfill ($self, $face, $string) {
149 if ( !defined $string ) {
150 $self->_croak('stroketextfill: no string specified');
151 return 0;
152 }
153 if ( !defined $face ) {
154 $self->_croak('stroketextfill: no face specified');
155 return 0;
156 }
157 $self->stroke(); # flush before hatching to avoid hatching previous shapes
158 $self->_doglyphs( $face, $string, 1 );
159 return 1;
160 }
161  
162 # Return the total advance width of a string (including kerning).
163 sub textwidth ($self, $face, $s) {
164 my @chars = split //, $s;
165 my $len = scalar @chars;
166 my $k = 0;
167 my $hk = $face->has_kerning();
168 my ( $glyph, $adv );
169 my $width = 0;
170 my $gprev = undef;
171 foreach my $i ( 0 .. $len - 1 ) {
172 $glyph = $face->glyph_from_char_code( ord $chars[$i] );
173 if ( !defined $glyph ) {
174 $self->_croak( 'char not found in font: ' . $chars[$i] );
175 }
176 $adv = $glyph->horizontal_advance();
177 if ( $gprev && $hk ) {
178 $k = $face->kerning( $glyph->index, $gprev->index );
179 }
180 $width += $k if $k;
181 $width += $adv;
182 $gprev = $glyph;
183 }
184 return $width;
185 }
186  
187 # Internal: render each glyph of a string, optionally hatch-filling each one.
188 sub _doglyphs ($self, $face, $s, $fill) {
189 my @chars = split //, $s;
190 my $len = scalar @chars;
191 my $k = 0;
192 my $hk = $face->has_kerning();
193 my ( $glyph, $gprev, $adv, $d );
194 $gprev = undef;
195 foreach my $i ( 0 .. $len - 1 ) {
196 $glyph = $face->glyph_from_char_code( ord $chars[$i] );
197 if ( !defined $glyph ) {
198 $self->_croak( 'char not found in font: ' . $chars[$i] );
199 }
200 $d = $glyph->svg_path();
201 if ( $d eq $EMPTY_STR ) {
202 $d = $SPACE; # space character has no outline
203 }
204 else {
205 # Work around a Font::FreeType bug: remove successive duplicate
206 # path entries (equivalent to the Unix 'uniq' command).
207 my @dtmp = split $EOL, $d;
208 my $dlen = scalar @dtmp;
209 if ( $dlen > 1 ) {
210 while ( $dlen > 0 ) {
211 if ( $dtmp[ $dlen - 1 ] eq $dtmp[ $dlen - 2 ] ) {
212 splice @dtmp, $dlen - 1, 1;
213 $dlen--;
214 $d = join $EOL, @dtmp;
215 }
216 $dlen--;
217 }
218 }
219 }
220 $adv = $glyph->horizontal_advance();
221 if ( $gprev && $hk ) {
222 $k = $face->kerning( $glyph->index, $gprev->index );
223 }
224 if ($k) { $self->translate( $k, 0 ) }
225 if ( $d ne $SPACE ) {
226 $self->_dopath($d);
227 }
228 if ($fill) { $self->_dohatching() }
229 $self->_flushPsegments();
230 $self->newpath();
231 $self->translate( $adv, 0 );
232 $gprev = $glyph;
233 }
234 return 1;
235 }
236  
237  
238 1;
239  
240 __END__
« script/template.pl ↑ Summary