blib/lib/Game/TextMapper/Mapper.pm

filter: Statementstmt 87.2% 294 / 337 Branchbran 70.0% 77 / 110 Conditioncond 49.3% 38 / 77 MC/DCmcdc 29.7% 14 / 47 Subroutinesub 95.0% 38 / 40 Podpod 11.1% 2 / 18 totaltotal 73.6% 463 / 629 CC 101 SCAR 39.0
CC
101
Cov
78%
process
130.8
other_text
42.0
svg_header
27.1
CRAP 49.2
« blib/lib/Game/TextMapper/Traveller.pm ↑ Summary blib/lib/Game/TextMapper/Command/random.pm »
1 # Copyright (C) 2009-2022 Alex Schroeder <alex@gnu.org>
2 #
3 # This program is free software: you can redistribute it and/or modify it under
4 # the terms of the GNU Affero General Public License as published by the Free
5 # Software Foundation, either version 3 of the License, or (at your option) any
6 # later version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Affero General Public License along
14 # with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 Game::TextMapper::Mapper - a text map parser and builder
21  
22 =head1 SYNOPSIS
23  
24 use Modern::Perl;
25 use Game::TextMapper::Mapper::Hex;
26 my $map = <<EOT;
27 0101 forest
28 include default.txt
29 EOT
30 my $svg = Game::TextMapper::Mapper::Hex->new(dist_dir => 'share')
31 ->initialize($map)
32 ->svg();
33 print $svg;
34  
35 =head1 DESCRIPTION
36  
37 This class knows how to parse a text containing a map description into SVG
38 definitions, and regions. Once the map is built, this class knows how to
39 generate the SVG for the entire map.
40  
41 The details depend on whether the map is a hex map or a square map. You should
42 use the appropriate class instead of this one: L<Game::TextMapper::Mapper::Hex>
43 or L<Game::TextMapper::Mapper::Square>.
44  
45 =cut
46  
47 package Game::TextMapper::Mapper;
48 11 ▶use Game::TextMapper::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 11 ▶use Modern::Perl '2018';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 11 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 11 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 11 ▶use File::Slurper qw(read_text);
Subroutine BEGIN: called CC=1 SCAR=0.0
53 11 ▶use Encode qw(encode_utf8 decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
54 11 ▶use Mojo::Util qw(url_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 11 ▶use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 11 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 =head1 ATTRIBUTES
59  
60 =head2 dist_dir
61  
62 You need to pass this during instantiation so that the mapper knows where to
63 find files it needs to include.
64  
65 =cut
66  
67 11 has 'local_files';
68 11 has 'dist_dir';
69 11 has 'map';
70 10 ▶has 'regions' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
71 10 ▶has 'attributes' => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
72 10 ▶has 'defs' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
73 10 ▶has 'path' => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
74 10 ▶has 'lines' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
75 10 ▶has 'things' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
76 10 ▶has 'path_attributes' => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
77 11 has 'text_attributes' => '';
78 11 has 'glow_attributes' => '';
79 11 has 'label_attributes' => '';
80 10 ▶has 'messages' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
81 8 ▶has 'seen' => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
82 11 has 'license' => '';
83 10 ▶has 'other' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
84 11 has 'url' => '';
85 10 ▶has 'offset' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
86  
87 11 my $log = Game::TextMapper::Log->get;
88  
89 sub example {
90 1 ▶ return <<"EOT";
Subroutine example: called CC=1 SCAR=0.0
Pod undocumented
91 0101 mountain "mountain"
92 0102 swamp "swamp"
93 0103 hill "hill"
94 0104 forest "forest"
95 0201 empty pyramid "pyramid"
96 0202 tundra "tundra"
97 0203 coast "coast"
98 0204 empty house "house"
99 0301 woodland "woodland"
100 0302 wetland "wetland"
101 0303 plain "plain"
102 0304 sea "sea"
103 0401 hill tower "tower"
104 0402 sand house "house"
105 0403 jungle "jungle"
106 0501 mountain cave "cave"
107 0502 sand "sand"
108 0503 hill castle "castle"
109 0205-0103-0202-0303-0402 road
110 0101-0203 river
111 0401-0303-0403 border
112 include default.txt
113 license <text>Public Domain</text>
114 EOT
115 }
116  
117 =head1 METHODS
118  
119 =head2 initialize($map)
120  
121 Call this to load a map into the mapper.
122  
123 =cut
124  
125 sub initialize {
126 11 ▶ my ($self, $map) = @_;
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
127 11 $map =~ s/&#45;/-/g; # -- are invalid in source comments...
128 11 $self->map($map);
129 11 $self->process(split(/\r?\n/, $map));
130 }
131  
132 sub process {
133 19 ▶ my $self = shift;
Subroutine process: called CC=44 SCAR=48.7
Pod undocumented
134 19 my $line_id = 0;
135 19 foreach (@_) {
136 3940 ▶ if (/^(-?\d\d)(-?\d\d)(\d\d)?\s+(.*)/ or /^(-?\d\d+)\.(-?\d\d+)(?:\.(\d\d+))?\s+(.*)/) {
Branch: 15 branches
BranchTrueFalseTotal
if (/^(-?\d\d)(-?\d\d)(\d\d)?\s+(.*)/u or /^(-?\d\d+)\.(-?\d\d+)(?:\.(\d\d+))?\s+(.*)/u) { } 2603 1337 3940
elsif (/^(-?\d\d-?\d\d(?:\d\d)?(?:--?\d\d-?\d\d(?:\d\d)?)+)\s+(\S+)\s*(?:["\x{201c}](.+)["\x{201d}])?\s*(left|right)?\s*(\d+%)?/u or /^(-?\d\d+\.-?\d\d+(?:\.\d\d+)?(?:--?\d\d+\.-?\d\d+(?:\.\d\d+)?)+)\s+(\S+)\s*(?:["\x{201c}](.+)["\x{201d}])?\s*(left|right)?\s*(\d+%)?/u) { } 312 1025 1337
elsif (/^(\S+)\s+attributes\s+(.*)/u) { } 141 884 1025
elsif (/^(\S+)\s+lib\s+(.*)/u) { } 0 884 884
elsif (/^(\S+)\s+xml\s+(.*)/u) { } 26 858 884
elsif (/^(<.*>)/u) { } 443 415 858
elsif (/^(\S+)\s+path\s+attributes\s+(.*)/u) { } 36 379 415
elsif (/^(\S+)\s+path\s+(.*)/u) { } 15 364 379
elsif (/^text\s+(.*)/u) { } 8 356 364
elsif (/^glow\s+(.*)/u) { } 7 349 356
elsif (/^label\s+(.*)/u) { } 8 341 349
elsif (/^license\s+(.*)/u) { } 5 336 341
elsif (/^other\s+(.*)/u) { } 8 328 336
elsif (/^url\s+(\S+)/u) { } 1 327 328
elsif (/^include\s+(\S*)/u) { } 8 319 327
Condition66%2/3
/^(-?\d\d)(-?\d\d)(\d\d)?\s+(.*)/u or /^(-?\d\d+)\.(-?\d\d+)(?:\.(\d\d+))?\s+(.*)/u
l!l&&r!l&&!r
260301337
MC/DC50%1/2
/^(-?\d\d)(-?\d\d)(\d\d)?\s+(.*)/u or /^(-?\d\d+)\.(-?\d\d+)(?:\.(\d\d+))?\s+(.*)/u
/^(-?\d\d)(-?\d\d)(\d\d)?\s+(.*)/u /^(-?\d\d+)\.(-?\d\d+)(?:\.(\d\d+))?\s+(.*)/u
Truth table66%2/3
A or B
A= /^(-?\d\d)(-?\d\d)(\d\d)?\s+(.*)/uB= /^(-?\d\d+)\.(-?\d\d+)(?:\.(\d\d+))?\s+(.*)/u
ABresult
1X1
011
000
137 2603 ▶ my $region = $self->make_region(x => $1, y => $2, z => $3||'00', map => $self);
Condition50%1/2
$3 || '00'
l!l
02603
MC/DC0%0/1
$3 || '00'
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
138 2603 weaken($region->{map});
139 2603 my $rest = $4;
140 2603 ▶ while (my ($tag, $label, $size) = $rest =~ /\b([a-z]+)=["“]([^"”]+)["”]\s*(\d+)?/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $rest =~ /\b([a-z]+)=["\x{201c}]([^"\x{201d}]+)["\x{201d}]\s*(\d+)?/u) 74 2603 2677
141 74 ▶ if ($tag eq 'name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 'name') { } 37 37 74
142 37 $region->label($label);
143 37 ▶ $region->size($size) if $size;
Branch: 1 branch
BranchTrueFalseTotal
if $size 0 37 37
144 } else {
145 # delay the calling of $self->other_info because the URL or the $self->glow_attributes might not be set
146 37 ▶ push(@{$self->other()}, sub () { $self->other_info($region, $label, $size, "translate(0,45)", 'opacity="0.2"') });
Subroutine __ANON__: called CC=1 SCAR=0.0
147 }
148 74 $rest =~ s/\b([a-z]+)=["“]([^"”]+)["”]\s*(\d+)?//;
149 }
150 2603 ▶ while (my ($label, $size, $transform) = $rest =~ /["“]([^"”]+)["”]\s*(\d+)?((?:\s*[a-z]+\([^\)]+\))*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $rest =~ /["\x{201c}]([^"\x{201d}]+)["\x{201d}]\s*(\d+)?((?:\s*[a-z]+\([^\)]+\))*)/u) 6 2603 2609
151 6 ▶ if ($transform or $region->label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transform or $region->label) { } 0 6 6
Condition33%1/3
$transform or $region->label
l!l&&r!l&&!r
006
MC/DC0%0/2
$transform or $region->label
$transform $region->label
Truth table33%1/3
A or B
A= $transformB= $region->label
ABresult
1X1
011
000
152 # delay the calling of $self->other_text because the URL or the $self->glow_attributes might not be set
153 0 ▶ push(@{$self->other()}, sub () { $self->other_text($region, $label, $size, $transform) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 } else {
155 6 $region->label($label);
156 6 $region->size($size);
157 }
158 6 $rest =~ s/["“]([^"”]+)["”]\s*(\d+)?((?:\s*[a-z]+\([^\)]+\))*)//;
159 }
160 2603 my @types = split(/\s+/, $rest);
161 2603 $region->type(\@types);
162 2603 push(@{$self->regions}, $region);
163 2603 push(@{$self->things}, $region);
164 ▶ } elsif (/^(-?\d\d-?\d\d(?:\d\d)?(?:--?\d\d-?\d\d(?:\d\d)?)+)\s+(\S+)\s*(?:["“](.+)["”])?\s*(left|right)?\s*(\d+%)?/
Condition66%2/3
/^(-?\d\d-?\d\d(?:\d\d)?(?:--?\d\d-?\d\d(?:\d\d)?)+)\s+(\S+)\s*(?:["\x{201c}](.+)["\x{201d}])?\s*(left|right)?\s*(\d+%)?/u or /^(-?\d\d+\.-?\d\d+(?:\.\d\d+)?(?:--?\d\d+\.-?\d\d+(?:\.\d\d+)?)+)\s+(\S+)\s*(?:["\x{201c}](.+)["\x{201d}])?\s*(left|right)?\s*(\d+%)?/u
l!l&&r!l&&!r
31201025
MC/DC50%1/2
/^(-?\d\d-?\d\d(?:\d\d)?(?:--?\d\d-?\d\d(?:\d\d)?)+)\s+(\S+)\s*(?:["\x{201c}](.+)["\x{201d}])?\s*(left|right)?\s*(\d+%)?/u or /^(-?\d\d+\.-?\d\d+(?:\.\d\d+)?(?:--?\d\d+\.-?\d\d+(?:\.\d\d+)?)+)\s+(\S+)\s*(?:["\x{201c}](.+)["\x{201d}])?\s*(left|right)?\s*(\d+%)?/u
/^(-?\d\d-?\d\d(?:\d\d)?(?:--?\d\d-?\d\d(?:\d\d)?)+)\s+(\S+)\s*(?:["\x{201c}](.+)["\x{201d}])?\s*(left|right)?\s*(\d+%)?/u /^(-?\d\d+\.-?\d\d+(?:\.\d\d+)?(?:--?\d\d+\.-?\d\d+(?:\.\d\d+)?)+)\s+(\S+)\s*(?:["\x{201c}](.+)["\x{201d}])?\s*(left|right)?\s*(\d+%)?/u
Truth table66%2/3
A or B
A= /^(-?\d\d-?\d\d(?:\d\d)?(?:--?\d\d-?\d\d(?:\d\d)?)+)\s+(\S+)\s*(?:["\x{201c}](.+)["\x{201d}])?\s*(left|right)?\s*(\d+%)?/uB= /^(-?\d\d+\.-?\d\d+(?:\.\d\d+)?(?:--?\d\d+\.-?\d\d+(?:\.\d\d+)?)+)\s+(\S+)\s*(?:["\x{201c}](.+)["\x{201d}])?\s*(left|right)?\s*(\d+%)?/u
ABresult
1X1
011
000
165 or /^(-?\d\d+\.-?\d\d+(?:\.\d\d+)?(?:--?\d\d+\.-?\d\d+(?:\.\d\d+)?)+)\s+(\S+)\s*(?:["“](.+)["”])?\s*(left|right)?\s*(\d+%)?/) {
166 312 my $line = $self->make_line(map => $self);
167 312 weaken($line->{map});
168 312 my $str = $1;
169 312 $line->type($2);
170 312 $line->label($3);
171 312 $line->side($4);
172 312 $line->start($5);
173 312 $line->id('line' . $line_id++);
174 312 my @points;
175 312 ▶ while ($str =~ /\G(?:(-?\d\d)(-?\d\d)(\d\d)?|(-?\d\d+)\.(-?\d\d+)\.(\d\d+)?)-?/cg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G(?:(-?\d\d)(-?\d\d)(\d\d)?|(-?\d\d+)\.(-?\d\d+)\.(\d\d+)?)-?/cgu) 2899 312 3211
176 2899 ▶ push(@points, Game::TextMapper::Point->new(x => $1||$4, y => $2||$5, z => $3||$6||'00'));
Condition36%4/11
$1 || $4
l!l&&r!l&&!r
289900
$2 || $5
l!l&&r!l&&!r
289900
$3 || $6 || '00'
l!l
02899
$3 or $6
l!l&&r!l&&!r
002899
MC/DC0%0/2
$1 || $4
$1 $4
MC/DC0%0/2
$2 || $5
$2 $5
MC/DC0%0/2
$3 || $6 || '00'
$3 $6
Truth table33%1/3
A || B
A= $1B= $4
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $2B= $5
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $3B= $6
ABresult
1X1
011
000
177 }
178 312 $line->points(\@points);
179 312 push(@{$self->lines}, $line);
180 } elsif (/^(\S+)\s+attributes\s+(.*)/) {
181 141 $self->attributes->{$1} = $2;
182 } elsif (/^(\S+)\s+lib\s+(.*)/) {
183 0 $self->def(qq{<g id="$1">$2</g>});
184 } elsif (/^(\S+)\s+xml\s+(.*)/) {
185 26 $self->def(qq{<g id="$1">$2</g>});
186 } elsif (/^(<.*>)/) {
187 443 $self->def($1);
188 } elsif (/^(\S+)\s+path\s+attributes\s+(.*)/) {
189 36 $self->path_attributes->{$1} = $2;
190 } elsif (/^(\S+)\s+path\s+(.*)/) {
191 15 $self->path->{$1} = $2;
192 } elsif (/^text\s+(.*)/) {
193 8 $self->text_attributes($1);
194 } elsif (/^glow\s+(.*)/) {
195 7 $self->glow_attributes($1);
196 } elsif (/^label\s+(.*)/) {
197 8 $self->label_attributes($1);
198 } elsif (/^license\s+(.*)/) {
199 5 $self->license($1);
200 } elsif (/^other\s+(.*)/) {
201 8 push(@{$self->other()}, $1);
202 } elsif (/^url\s+(\S+)/) {
203 1 $self->url($1);
204 } elsif (/^include\s+(\S*)/) {
205 8 ▶ if (scalar keys %{$self->seen} > 5) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar keys %{$self->seen;} > 5) { } 0 8 8
elsif (not $self->seen->{$1}) { } 8 0 8
206 0 push(@{$self->messages},
207 "Includes are limited to five to prevent loops");
208 } elsif (not $self->seen->{$1}) {
209 8 my $location = $1;
210 8 $self->seen->{$location} = 1;
211 8 my $path;
212 8 ▶ if (index($location, '/') == -1 and -f ($path = Mojo::File->new($self->dist_dir, $location))) {
Branch: 4 branches
BranchTrueFalseTotal
if (index($location, '/') == -1 and -f ($path = 'Mojo::File'->new($self->dist_dir, $location))) { } 8 0 8
elsif ($self->local_files and -f ($path = 'Mojo::File'->new($location))) { } 0 0 0
elsif ($location =~ /^https?:/u) { } 0 0 0
elsif ($self->dist_dir =~ /^https?:/u) { } 0 0 0
Condition33%1/3
index($location, "/") == -1 and -f ($path = "Mojo::File"->new($self->dist_dir, $location))
!ll&&!rl&&r
008
MC/DC0%0/2
index($location, "/") == -1 and -f ($path = "Mojo::File"->new($self->dist_dir, $location))
index($location, "/") == -1 -f ($path = "Mojo::File"->new($self->dist_dir, $location))
Truth table33%1/3
A and B
A= index($location, "/") == -1B= -f ($path = "Mojo::File"->new($self->dist_dir, $location))
ABresult
0X0
100
111
213 # without a slash, it could be a file from dist_dir
214 8 $log->debug("Reading $location");
215 8 $self->process(split(/\n/, decode_utf8($path->slurp())));
216 ▶ } elsif ($self->local_files and -f ($path = Mojo::File->new($location))) {
Condition0%0/3
$self->local_files and -f ($path = "Mojo::File"->new($location))
!ll&&!rl&&r
000
MC/DC0%0/2
$self->local_files and -f ($path = "Mojo::File"->new($location))
$self->local_files -f ($path = "Mojo::File"->new($location))
Truth table0%0/3
A and B
A= $self->local_filesB= -f ($path = "Mojo::File"->new($location))
ABresult
0X0
100
111
217 # it could also be a local file in the same directory, but only if
218 # called from the render command (which sets local_files)
219 0 $log->debug("Reading $location");
220 0 $self->process(split(/\n/, decode_utf8($path->slurp())));
221 } elsif ($location =~ /^https?:/) {
222 0 $log->debug("Getting $location");
223 0 my $ua = Mojo::UserAgent->new;
224 0 my $response = $ua->get($location)->result;
225 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
226 0 $self->process(split(/\n/, $response->text));
227 } else {
228 0 push(@{$self->messages}, "Getting $location: " . $response->status_line);
229 }
230 } elsif ($self->dist_dir =~ /^https?:/) {
231 0 my $url = $self->dist_dir;
232 0 ▶ $url .= '/' unless $url =~ /\/$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[/$]u 0 0 0
233 0 $url .= $location;
234 0 $log->debug("Getting $url");
235 0 my $ua = Mojo::UserAgent->new;
236 0 my $response = $ua->get($url)->result;
237 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
238 0 $self->process(split(/\n/, $response->text));
239 } else {
240 0 push(@{$self->messages}, "Getting $url: " . $response->status_line);
241 }
242 } else {
243 0 $log->warn("No library '$location' in " . $self->dist_dir);
244 0 push(@{$self->messages}, "Library '$location' is must be an existing file on the server or a HTTP/HTTPS URL");
245 }
246 }
247 } else {
248 319 ▶ $log->debug("Did not parse $_") if $_ and not /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ and not /^\s*#/u 0 319 319
Condition66%2/3
$_ and not /^\s*#/u
!ll&&!rl&&r
1581610
MC/DC0%0/2
$_ and not /^\s*#/u
$_ not /^\s*#/u
Truth table66%2/3
A and not(B)
A= $_B= not /^\s*#/u
ABresult
0X0
100
111
249 }
250 }
251 19 return $self;
252 }
253  
254 sub svg_other {
255 10 ▶ my ($self) = @_;
Subroutine svg_other: called CC=3 SCAR=11.0
Pod undocumented
256 10 my $data = "\n";
257 10 for my $other (@{$self->other()}) {
258 45 ▶ if (ref $other eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $other eq 'CODE') { } 37 8 45
259 37 $data .= $other->();
260 } else {
261 8 $data .= $other;
262 }
263 45 $data .= "\n";
264 }
265 10 $self->other(undef);
266 10 return $data;
267 }
268  
269 # Very similar to svg_label, but given that we have a transformation, we
270 # translate the object to it's final position.
271 sub other_text {
272 0 ▶ my ($self, $region, $label, $size, $transform) = @_;
Subroutine other_text: not called CC=6 SCAR=37.4
Pod undocumented
273 0 $transform = sprintf("translate(%.1f,%.1f)", $region->pixels($self->offset)) . $transform;
274 0 my $attributes = "transform=\"$transform\" " . $self->label_attributes;
275 0 ▶ if ($size and not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size and not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/u) 0 0 0
Condition0%0/3
$size and not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/u
!ll&&!rl&&r
000
MC/DC0%0/2
$size and not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/u
$size not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/u
Truth table0%0/3
A and not(B)
A= $sizeB= not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/u
ABresult
0X0
100
111
276 0 $attributes .= " font-size=\"$size\"";
277 }
278 0 ▶ my $data = sprintf(qq{ <g><text text-anchor="middle" %s %s>%s</text>},
Condition0%0/2
$self->glow_attributes || ''
l!l
00
MC/DC0%0/1
$self->glow_attributes || ''
$self->glow_attributes
Truth table0%0/2
A
A= $self->glow_attributes
Aresult
11
00
279 $attributes, $self->glow_attributes||'', $label);
280 0 my $url = $self->url;
281 0 ▶ $url =~ s/\%s/url_escape(encode_utf8($label))/e or $url .= url_escape(encode_utf8($label)) if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
Condition0%0/6
$url && ($url =~ s/\%s/url_escape(&encode_utf8($label));/eu || ($url .= url_escape(&encode_utf8($label))))
!ll&&!rl&&r
000
$url =~ s/\%s/url_escape(&encode_utf8($label));/eu or $url .= url_escape(&encode_utf8($label))
l!l&&r!l&&!r
000
MC/DC0%0/3
$url && ($url =~ s/\%s/url_escape(&encode_utf8($label));/eu || ($url .= url_escape(&encode_utf8($label))))
$url $url =~ s/\%s/url_escape(&encode_utf8($label));/eu $url .= url_escape(&encode_utf8($label))
Truth table0%0/4
A && B or C
A= $urlB= $url =~ s/\%s/url_escape(&encode_utf8($label));/euC= $url .= url_escape(&encode_utf8($label))
ABCresult
0XX0
1000
11X1
1011
282 0 $data .= sprintf(qq{<a xlink:href="%s"><text text-anchor="middle" %s>%s</text></a>},
283 $url, $attributes, $label);
284 0 $data .= qq{</g>\n};
285 0 return $data;
286 }
287  
288 # Very similar to other_text, but without a link and we have extra attributes
289 sub other_info {
290 37 ▶ my ($self, $region, $label, $size, $transform, $attributes) = @_;
Subroutine other_info: called CC=5 SCAR=17.8
Pod undocumented
291 37 $transform = sprintf("translate(%.1f,%.1f)", $region->pixels($self->offset)) . $transform;
292 37 $attributes .= " transform=\"$transform\"";
293 37 ▶ $attributes .= " " . $self->label_attributes if $self->label_attributes;
Branch: 1 branch
BranchTrueFalseTotal
if $self->label_attributes 37 0 37
294 37 ▶ if ($size and not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size and not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/u) 0 37 37
Condition33%1/3
$size and not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/u
!ll&&!rl&&r
3700
MC/DC0%0/2
$size and not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/u
$size not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/u
Truth table33%1/3
A and not(B)
A= $sizeB= not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/u
ABresult
0X0
100
111
295 0 $attributes .= " font-size=\"$size\"";
296 }
297 37 ▶ my $data = sprintf(qq{ <g><text text-anchor="middle" %s %s>%s</text>}, $attributes, $self->glow_attributes||'', $label);
Condition50%1/2
$self->glow_attributes || ''
l!l
037
MC/DC0%0/1
$self->glow_attributes || ''
$self->glow_attributes
Truth table50%1/2
A
A= $self->glow_attributes
Aresult
11
00
298 37 $data .= sprintf(qq{<text text-anchor="middle" %s>%s</text></g>\n}, $attributes, $label);
299 37 return $data;
300 }
301  
302 sub def {
303 469 ▶ my ($self, $svg) = @_;
Subroutine def: called CC=1 SCAR=0.0
Pod undocumented
304 469 $svg =~ s/>\s+</></g;
305 469 push(@{$self->defs}, $svg);
306 }
307  
308 sub merge_attributes {
309 472 ▶ my %attr = ();
Subroutine merge_attributes: called CC=4 SCAR=13.9
Pod undocumented
310 472 for my $attr (@_) {
311 708 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 67 641 708
312 67 ▶ while ($attr =~ /(\S+)=((["']).*?\3)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /(\S+)=((["']).*?\3)/gu) 103 67 170
313 103 $attr{$1} = $2;
314 }
315 }
316 }
317 472 return join(' ', map { $_ . '=' . $attr{$_} } sort keys %attr);
318 }
319  
320 sub svg_header {
321 10 ▶ my ($self) = @_;
Subroutine svg_header: called CC=23 SCAR=33.0
Pod undocumented
322  
323 10 my $header = qq{<?xml version="1.0" encoding="UTF-8" standalone="no"?>
324 <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
325 xmlns:xlink="http://www.w3.org/1999/xlink"
326 };
327 10 ▶ return $header . "\n" unless @{$self->regions};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->regions;} 0 10 10
328 10 my $maxz = 0;
329 10 foreach my $region (@{$self->regions}) {
330 2603 ▶ $maxz = $region->z if $region->z > $maxz;
Branch: 1 branch
BranchTrueFalseTotal
if $region->z > $maxz 0 2603 2603
331 }
332 # These are required to calculate the viewBox for the SVG. Min and max X are
333 # what you would expect. Min and max Y are different, however, since we want
334 # to count all the rows on all the levels, plus an extra separator between
335 # them. Thus, min y is the min y of the first level, and max y is the min y of
336 # the first level + 1 for every level beyond the first, + all the rows for
337 # each level.
338 10 my $min_x_overall;
339 my $max_x_overall;
340 10 my $min_y_overall;
341 10 my $max_y_overall;
342 10 for my $z (0 .. $maxz) {
343 10 my ($minx, $miny, $maxx, $maxy);
344 10 ▶ $self->offset->[$z] = $max_y_overall // 0;
Condition50%1/2
$max_y_overall // 0
l!l
010
MC/DC0%0/1
$max_y_overall // 0
$max_y_overall
Truth table50%1/2
A
A= $max_y_overall
Aresult
11
00
345 10 foreach my $region (@{$self->regions}) {
346 2603 ▶ next unless $region->z == $z;
Branch: 1 branch
BranchTrueFalseTotal
unless $region->z == $z 0 2603 2603
347 2603 ▶ $minx = $region->x unless defined $minx and $minx <= $region->x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $minx and $minx <= $region->x 10 2593 2603
Condition66%2/3
defined $minx and $minx <= $region->x
!ll&&!rl&&r
1002593
MC/DC50%1/2
defined $minx and $minx <= $region->x
defined $minx $minx <= $region->x
Truth table66%2/3
A and B
A= defined $minxB= $minx <= $region->x
ABresult
0X0
100
111
348 2603 ▶ $maxx = $region->x unless defined $maxx and $maxx >= $region->x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxx and $maxx >= $region->x 209 2394 2603
Condition100%3/3
defined $maxx and $maxx >= $region->x
!ll&&!rl&&r
101992394
MC/DC100%2/2
defined $maxx and $maxx >= $region->x
defined $maxx $maxx >= $region->x
Truth table100%3/3
A and B
A= defined $maxxB= $maxx >= $region->x
ABresult
0X0
100
111
349 2603 ▶ $miny = $region->y unless defined $miny and $miny <= $region->y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $miny and $miny <= $region->y 10 2593 2603
Condition66%2/3
defined $miny and $miny <= $region->y
!ll&&!rl&&r
1002593
MC/DC50%1/2
defined $miny and $miny <= $region->y
defined $miny $miny <= $region->y
Truth table66%2/3
A and B
A= defined $minyB= $miny <= $region->y
ABresult
0X0
100
111
350 2603 ▶ $maxy = $region->y unless defined $maxy and $maxy >= $region->y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxy and $maxy >= $region->y 99 2504 2603
Condition100%3/3
defined $maxy and $maxy >= $region->y
!ll&&!rl&&r
10892504
MC/DC100%2/2
defined $maxy and $maxy >= $region->y
defined $maxy $maxy >= $region->y
Truth table100%3/3
A and B
A= defined $maxyB= $maxy >= $region->y
ABresult
0X0
100
111
351 }
352 10 ▶ $min_x_overall = $minx unless defined $min_x_overall and $minx >= $min_x_overall;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min_x_overall and $minx >= $min_x_overall 10 0 10
Condition33%1/3
defined $min_x_overall and $minx >= $min_x_overall
!ll&&!rl&&r
1000
MC/DC0%0/2
defined $min_x_overall and $minx >= $min_x_overall
defined $min_x_overall $minx >= $min_x_overall
Truth table33%1/3
A and B
A= defined $min_x_overallB= $minx >= $min_x_overall
ABresult
0X0
100
111
353 10 ▶ $max_x_overall = $maxx unless defined $min_y_overall and $maxx <= $max_x_overall;;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min_y_overall and $maxx <= $max_x_overall 10 0 10
Condition33%1/3
defined $min_y_overall and $maxx <= $max_x_overall
!ll&&!rl&&r
1000
MC/DC0%0/2
defined $min_y_overall and $maxx <= $max_x_overall
defined $min_y_overall $maxx <= $max_x_overall
Truth table33%1/3
A and B
A= defined $min_y_overallB= $maxx <= $max_x_overall
ABresult
0X0
100
111
354 10 ▶ $min_y_overall = $miny unless defined $min_y_overall; # first row of the first level
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min_y_overall 10 0 10
355 10 ▶ $max_y_overall = $miny unless defined $max_y_overall; # also (!) first row of the first level
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_y_overall 10 0 10
356 10 ▶ $max_y_overall += 1 if $z > 0; # plus a separator row for every extra level
Branch: 1 branch
BranchTrueFalseTotal
if $z > 0 0 10 10
357 10 $max_y_overall += 1 + $maxy - $miny; # plus the number of rows for every level
358 }
359 10 my ($vx1, $vy1, $vx2, $vy2) = $self->viewbox($min_x_overall, $min_y_overall, $max_x_overall, $max_y_overall);
360 10 my ($width, $height) = ($vx2 - $vx1, $vy2 - $vy1);
361 10 $header .= qq{ viewBox="$vx1 $vy1 $width $height">\n};
362 10 $header .= qq{ <!-- min ($min_x_overall, $min_y_overall), max ($max_x_overall, $max_y_overall) -->\n};
363 10 return $header;
364 }
365  
366 sub svg_defs {
367 10 ▶ my ($self) = @_;
Subroutine svg_defs: called CC=12 SCAR=24.8
Pod undocumented
368 # All the definitions are included by default.
369 10 my $doc = " <defs>\n";
370 10 ▶ $doc .= " " . join("\n ", @{$self->defs}, "") if @{$self->defs};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->defs;} 7 3 10
371 # collect region types from attributess and paths in case the sets don't overlap
372 10 my %types = ();
373 10 foreach my $region (@{$self->regions}) {
374 2603 foreach my $type (@{$region->type}) {
375 4082 $types{$type} = 1;
376 }
377 }
378 10 foreach my $line (@{$self->lines}) {
379 312 $types{$line->type} = 1;
380 }
381 # now go through them all
382 10 foreach my $type (sort keys %types) {
383 236 my $path = $self->path->{$type};
384 236 my $attributes = merge_attributes($self->attributes->{$type});
385 my $path_attributes = merge_attributes($self->path_attributes->{'default'},
386 236 $self->path_attributes->{$type});
387 236 my $glow_attributes = $self->glow_attributes;
388 236 ▶ if ($path || $attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path or $attributes) { } 51 185 236
Condition100%3/3
$path or $attributes
l!l&&r!l&&!r
843185
MC/DC100%2/2
$path or $attributes
$path $attributes
Truth table100%3/3
A or B
A= $pathB= $attributes
ABresult
1X1
011
000
389 51 $doc .= qq{ <g id="$type">\n};
390 # just shapes get a glow such, eg. a house (must come first)
391 51 ▶ if ($path && !$attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and not $attributes) 3 48 51
Condition100%3/3
$path and not $attributes
!ll&&!rl&&r
4353
MC/DC100%2/2
$path and not $attributes
$path not $attributes
Truth table100%3/3
A and not(B)
A= $pathB= not $attributes
ABresult
0X0
100
111
392 3 $doc .= qq{ <path $glow_attributes d='$path' />\n}
393 }
394 # region with attributes get a shape (square or hex), eg. plains and grass
395 51 ▶ if ($attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes) 48 3 51
396 48 $doc .= " " . $self->shape($attributes) . "\n";
397 }
398 # and now the attributes themselves the shape itself
399 51 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 8 43 51
400 8 $doc .= qq{ <path $path_attributes d='$path' />\n}
401 }
402 # close
403 51 $doc .= qq{ </g>\n};
404 } else {
405 # nothing
406 }
407 }
408 10 $doc .= qq{ </defs>\n};
409 }
410  
411 sub svg_backgrounds {
412 10 ▶ my $self = shift;
Subroutine svg_backgrounds: called CC=2 SCAR=6.9
Pod undocumented
413 10 my $doc = qq{ <g id="backgrounds">\n};
414 10 foreach my $thing (@{$self->things}) {
415 # make a copy
416 2603 my @types = @{$thing->type};
417 # keep attributes
418 2603 $thing->type([grep { $self->attributes->{$_} } @{$thing->type}]);
419 2603 $doc .= $thing->svg($self->offset);
420 # reset copy
421 2603 $thing->type(\@types);
422 }
423 10 $doc .= qq{ </g>\n};
424 10 return $doc;
425 }
426  
427 sub svg_things {
428 10 ▶ my $self = shift;
Subroutine svg_things: called CC=2 SCAR=6.9
Pod undocumented
429 10 my $doc = qq{ <g id="things">\n};
430 10 foreach my $thing (@{$self->things}) {
431 # drop attributes
432 2603 $thing->type([grep { not $self->attributes->{$_} } @{$thing->type}]);
433 2603 $doc .= $thing->svg($self->offset);
434 }
435 10 $doc .= qq{ </g>\n};
436 10 return $doc;
437 }
438  
439 sub svg_coordinates {
440 10 ▶ my $self = shift;
Subroutine svg_coordinates: called CC=2 SCAR=6.9
Pod undocumented
441 10 my $doc = qq{ <g id="coordinates">\n};
442 10 foreach my $region (@{$self->regions}) {
443 2603 $doc .= $region->svg_coordinates($self->offset);
444 }
445 10 $doc .= qq{ </g>\n};
446 10 return $doc;
447 }
448  
449 sub svg_lines {
450 10 ▶ my $self = shift;
Subroutine svg_lines: called CC=2 SCAR=6.9
Pod undocumented
451 10 my $doc = qq{ <g id="lines">\n};
452 10 foreach my $line (@{$self->lines}) {
453 312 $doc .= $line->svg($self->offset);
454 }
455 10 $doc .= qq{ </g>\n};
456 10 return $doc;
457 }
458  
459 sub svg_regions {
460 10 ▶ my $self = shift;
Subroutine svg_regions: called CC=3 SCAR=11.0
Pod undocumented
461 10 my $doc = qq{ <g id="regions">\n};
462 10 ▶ my $attributes = $self->attributes->{default} || qq{fill="none"};
Condition100%2/2
$self->attributes->{'default'} || 'fill="none"'
l!l
73
MC/DC100%1/1
$self->attributes->{'default'} || 'fill="none"'
$self->attributes->{'default'}
Truth table100%2/2
A
A= $self->attributes->{'default'}
Aresult
11
00
463 10 foreach my $region (@{$self->regions}) {
464 2603 $doc .= $region->svg_region($attributes, $self->offset);
465 }
466 10 $doc .= qq{ </g>\n};
467 }
468  
469 sub svg_line_labels {
470 10 ▶ my $self = shift;
Subroutine svg_line_labels: called CC=2 SCAR=6.9
Pod undocumented
471 10 my $doc = qq{ <g id="line_labels">\n};
472 10 foreach my $line (@{$self->lines}) {
473 312 $doc .= $line->svg_label($self->offset);
474 }
475 10 $doc .= qq{ </g>\n};
476 10 return $doc;
477 }
478  
479 sub svg_labels {
480 10 ▶ my $self = shift;
Subroutine svg_labels: called CC=2 SCAR=6.9
Pod undocumented
481 10 my $doc = qq{ <g id="labels">\n};
482 10 foreach my $region (@{$self->regions}) {
483 2603 $doc .= $region->svg_label($self->url, $self->offset);
484 }
485 10 $doc .= qq{ </g>\n};
486 10 return $doc;
487 }
488  
489 =head2 svg()
490  
491 This method generates the SVG once the map is initialized.
492  
493 =cut
494  
495 sub svg {
496 10 ▶ my ($self) = @_;
Subroutine svg: called CC=3 SCAR=11.0
Pod documented
497  
498 10 my $doc = $self->svg_header();
499 10 $doc .= $self->svg_defs();
500 10 $doc .= $self->svg_backgrounds(); # opaque backgrounds
501 10 $doc .= $self->svg_lines();
502 10 $doc .= $self->svg_things(); # icons, lines
503 10 $doc .= $self->svg_coordinates();
504 10 $doc .= $self->svg_regions();
505 10 $doc .= $self->svg_line_labels();
506 10 $doc .= $self->svg_labels();
507 10 ▶ $doc .= $self->license() ||'';
Condition100%2/2
$self->license || ''
l!l
55
MC/DC100%1/1
$self->license || ''
$self->license
Truth table100%2/2
A
A= $self->license
Aresult
11
00
508 10 $doc .= $self->svg_other();
509  
510 # error messages
511 10 my $y = 10;
512 10 foreach my $msg (@{$self->messages}) {
513 0 $doc .= " <text x='0' y='$y'>$msg</text>\n";
514 0 $y += 10;
515 }
516  
517 # source code (comments may not include -- for SGML compatibility!)
518 # https://stackoverflow.com/questions/10842131/xml-comments-and
519 10 my $source = $self->map();
520 10 $source =~ s/--/&#45;&#45;/g;
521 10 $doc .= "<!-- Source\n$source\n-->\n";
522 10 $doc .= qq{</svg>\n};
523  
524 10 return $doc;
525 }
526  
527 =head1 SEE ALSO
528  
529 L<Game::TextMapper::Mapper::Hex> is for hex maps.
530  
531 L<Game::TextMapper::Mapper::Square> is for square maps.
532  
533 =cut
534  
535 11 1;
« blib/lib/Game/TextMapper/Traveller.pm ↑ Summary blib/lib/Game/TextMapper/Command/random.pm »