blib/lib/Game/TextMapper/Mapper.pm

filter: Statementstmt 79.6% 294 / 369 Branchbran 60.9% 78 / 128 Conditioncond 49.3% 38 / 77 MC/DCmcdc 29.7% 14 / 47 Subroutinesub 92.6% 38 / 41 Podpod 11.1% 2 / 18 totaltotal 68.2% 464 / 680 CC 110 SCAR 49.6
CC
110
Cov
71%
process
382.0
other_text
42.0
svg_header
27.1
CRAP 142.6
« blib/lib/Game/TextMapper/Solo.pm ↑ Summary blib/lib/Game/TextMapper/Schroeder/Archipelago.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 1 ▶sub example {
Subroutine example: called CC=1 SCAR=0.0
Pod undocumented
90 1 return <<"EOT";
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 11 ▶sub initialize {
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
126 11 my ($self, $map) = @_;
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 19 ▶sub process {
Subroutine process: called CC=53 SCAR=59.5
Pod undocumented
133 19 my $self = shift;
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: 16 branches
BranchTrueFalseTotal
if (/^(-?\d\d)(-?\d\d)(\d\d)?\s+(.*)/u or /^(-?\d\d+)\.(-?\d\d+)(?:\.(\d\d+))?\s+(.*)/u) { } 2598 1342 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) { } 311 1031 1342
elsif (() = /(?:([^>\r\n\t]*?)\s+)?(\d\d)(\d\d)\s+(([A-EX])([\dA])([\dA-F])([\dA])([\dA-C])([\dA-F])([\dA-L])-(\d{1,2}|[\dA-HJ-NP-Z]))(?:\s+([PCTRNSG ]+)\b)?(.*)/u) { } 0 1031 1031
elsif (/^(\S+)\s+attributes\s+(.*)/u) { } 141 890 1031
elsif (/^(\S+)\s+lib\s+(.*)/u) { } 0 890 890
elsif (/^(\S+)\s+xml\s+(.*)/u) { } 26 864 890
elsif (/^(<.*>)/u) { } 448 416 864
elsif (/^(\S+)\s+path\s+attributes\s+(.*)/u) { } 37 379 416
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
259801342
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 2598 ▶ my $region = $self->make_region(x => $1, y => $2, z => $3||'00', map => $self);
Condition50%1/2
$3 || '00'
l!l
02598
MC/DC0%0/1
$3 || '00'
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
138 2598 weaken($region->{map});
139 2598 my $rest = $4;
140 2598 ▶ 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) 72 2598 2670
141 72 ▶ if ($tag eq 'name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 'name') { } 36 36 72
142 36 $region->label($label);
143 36 ▶ $region->size($size) if $size;
Branch: 1 branch
BranchTrueFalseTotal
if $size 0 36 36
144 } else {
145 # delay the calling of $self->other_info because the URL or the $self->glow_attributes might not be set
146 36 ▶ 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 72 $rest =~ s/\b([a-z]+)=["“]([^"”]+)["”]\s*(\d+)?//;
149 }
150 2598 ▶ 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 2598 2604
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 2598 my @types = split(/\s+/, $rest);
161 2598 $region->type(\@types);
162 2598 push(@{$self->regions}, $region);
163 2598 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
31101031
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 311 my $line = $self->make_line(map => $self);
167 311 weaken($line->{map});
168 311 my $str = $1;
169 311 $line->type($2);
170 311 $line->label($3);
171 311 $line->side($4);
172 311 $line->start($5);
173 311 $line->id('line' . $line_id++);
174 311 my @points;
175 311 ▶ 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) 1626 311 1937
176 1626 ▶ 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
162600
$2 || $5
l!l&&r!l&&!r
162600
$3 || $6 || '00'
l!l
01626
$3 or $6
l!l&&r!l&&!r
001626
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 311 $line->points(\@points);
179 311 push(@{$self->lines}, $line);
180 } elsif (my ($name, $x, $y, $uwp, $starport, $size, $atmosphere, $hydrographic, $population, $government, $law, $tech, $bases, $rest) =
181 /(?:([^>\r\n\t]*?)\s+)?(\d\d)(\d\d)\s+(([A-EX])([\dA])([\dA-F])([\dA])([\dA-C])([\dA-F])([\dA-L])-(\d{1,2}|[\dA-HJ-NP-Z]))(?:\s+([PCTRNSG ]+)\b)?(.*)/) {
182 0 my $region = $self->make_region(x => $x, y => $y, z => '00', map => $self);
183 0 weaken($region->{map});
184 0 my @types;
185 0 $region->label($name);
186 # delay the calling of $self->other_info because the URL or the $self->glow_attributes might not be set
187 0 ▶ push(@{$self->other()}, sub () { $self->other_info($region, $uwp, undef, "translate(0,45)", 'opacity="0.2"') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 push(@types, "starport-$starport");
189 0 push(@types, "size-$size");
190 0 push(@types, "atmosphere-$atmosphere");
191 0 push(@types, "hydrosphere-$hydrographic");
192 0 push(@types, "population-$population");
193 0 push(@types, "government-$government");
194 0 push(@types, "law-$law");
195 0 push(@types, "tech-$tech");
196 0 ▶ push(@types, "consulate") if $bases =~ /C/;
Branch: 1 branch
BranchTrueFalseTotal
if $bases =~ /C/u 0 0 0
197 0 ▶ push(@types, "tas") if $bases =~ /T/;
Branch: 1 branch
BranchTrueFalseTotal
if $bases =~ /T/u 0 0 0
198 0 ▶ push(@types, "pirate") if $bases =~ /P/;
Branch: 1 branch
BranchTrueFalseTotal
if $bases =~ /P/u 0 0 0
199 0 ▶ push(@types, "research") if $bases =~ /R/;
Branch: 1 branch
BranchTrueFalseTotal
if $bases =~ /R/u 0 0 0
200 0 ▶ push(@types, "naval") if $bases =~ /N/;
Branch: 1 branch
BranchTrueFalseTotal
if $bases =~ /N/u 0 0 0
201 0 ▶ push(@types, "gas") if $bases =~ /G/;
Branch: 1 branch
BranchTrueFalseTotal
if $bases =~ /G/u 0 0 0
202 0 ▶ push(@types, "scout") if $bases =~ /S/;
Branch: 1 branch
BranchTrueFalseTotal
if $bases =~ /S/u 0 0 0
203 0 my @tokens = split(' ', $rest);
204 0 my %map = (A => "amber", R => "red");
205 0 my ($travelzone) = grep /^([AR])$/, @tokens; # amber or red travel zone
206 0 ▶ push(@types, $map{$travelzone}) if $travelzone;
Branch: 1 branch
BranchTrueFalseTotal
if $travelzone 0 0 0
207 0 push(@types, grep(/^[A-Z][A-Za-z]$/, @tokens));
208 0 $region->type(\@types);
209 0 push(@{$self->regions}, $region);
210 0 push(@{$self->things}, $region);
211 } elsif (/^(\S+)\s+attributes\s+(.*)/) {
212 141 $self->attributes->{$1} = $2;
213 } elsif (/^(\S+)\s+lib\s+(.*)/) {
214 0 $self->def(qq{<g id="$1">$2</g>});
215 } elsif (/^(\S+)\s+xml\s+(.*)/) {
216 26 $self->def(qq{<g id="$1">$2</g>});
217 } elsif (/^(<.*>)/) {
218 448 $self->def($1);
219 } elsif (/^(\S+)\s+path\s+attributes\s+(.*)/) {
220 37 $self->path_attributes->{$1} = $2;
221 } elsif (/^(\S+)\s+path\s+(.*)/) {
222 15 $self->path->{$1} = $2;
223 } elsif (/^text\s+(.*)/) {
224 8 $self->text_attributes($1);
225 } elsif (/^glow\s+(.*)/) {
226 7 $self->glow_attributes($1);
227 } elsif (/^label\s+(.*)/) {
228 8 $self->label_attributes($1);
229 } elsif (/^license\s+(.*)/) {
230 5 $self->license($1);
231 } elsif (/^other\s+(.*)/) {
232 8 push(@{$self->other()}, $1);
233 } elsif (/^url\s+(\S+)/) {
234 1 $self->url($1);
235 } elsif (/^include\s+(\S*)/) {
236 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
237 0 push(@{$self->messages},
238 "Includes are limited to five to prevent loops");
239 } elsif (not $self->seen->{$1}) {
240 8 my $location = $1;
241 8 $self->seen->{$location} = 1;
242 8 my $path;
243 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
244 # without a slash, it could be a file from dist_dir
245 8 $log->debug("Reading $location");
246 8 $self->process(split(/\n/, decode_utf8($path->slurp())));
247 ▶ } 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
248 # it could also be a local file in the same directory, but only if
249 # called from the render command (which sets local_files)
250 0 $log->debug("Reading $location");
251 0 $self->process(split(/\n/, decode_utf8($path->slurp())));
252 } elsif ($location =~ /^https?:/) {
253 0 $log->debug("Getting $location");
254 0 my $ua = Mojo::UserAgent->new;
255 0 my $response = $ua->get($location)->result;
256 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
257 0 $self->process(split(/\n/, $response->text));
258 } else {
259 0 push(@{$self->messages}, "Getting $location: " . $response->code . " " . $response->message);
260 }
261 } elsif ($self->dist_dir =~ /^https?:/) {
262 0 my $url = $self->dist_dir;
263 0 ▶ $url .= '/' unless $url =~ /\/$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[/$]u 0 0 0
264 0 $url .= $location;
265 0 $log->debug("Getting $url");
266 0 my $ua = Mojo::UserAgent->new;
267 0 my $response = $ua->get($url)->result;
268 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
269 0 $self->process(split(/\n/, $response->text));
270 } else {
271 0 push(@{$self->messages}, "Getting $url: " . $response->code . " " . $response->message);
272 }
273 } else {
274 0 $log->warn("No library '$location' in " . $self->dist_dir);
275 0 push(@{$self->messages}, "Library '$location' is must be an existing file on the server or a HTTP/HTTPS URL");
276 }
277 }
278 } else {
279 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
280 }
281 }
282 19 return $self;
283 }
284  
285 10 ▶sub svg_other {
Subroutine svg_other: called CC=3 SCAR=11.0
Pod undocumented
286 10 my ($self) = @_;
287 10 my $data = "\n";
288 10 for my $other (@{$self->other()}) {
289 44 ▶ if (ref $other eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $other eq 'CODE') { } 36 8 44
290 36 $data .= $other->();
291 } else {
292 8 $data .= $other;
293 }
294 44 $data .= "\n";
295 }
296 10 $self->other(undef);
297 10 return $data;
298 }
299  
300 # Very similar to svg_label, but given that we have a transformation, we
301 # translate the object to it's final position.
302 0 ▶sub other_text {
Subroutine other_text: not called CC=6 SCAR=37.4
Pod undocumented
303 0 my ($self, $region, $label, $size, $transform) = @_;
304 0 $transform = sprintf("translate(%.1f,%.1f)", $region->pixels($self->offset)) . $transform;
305 0 my $attributes = "transform=\"$transform\" " . $self->label_attributes;
306 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
307 0 $attributes .= " font-size=\"$size\"";
308 }
309 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
310 $attributes, $self->glow_attributes||'', $label);
311 0 my $url = $self->url;
312 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
313 0 $data .= sprintf(qq{<a xlink:href="%s"><text text-anchor="middle" %s>%s</text></a>},
314 $url, $attributes, $label);
315 0 $data .= qq{</g>\n};
316 0 return $data;
317 }
318  
319 # Very similar to other_text, but without a link and we have extra attributes
320 36 ▶sub other_info {
Subroutine other_info: called CC=5 SCAR=17.8
Pod undocumented
321 36 my ($self, $region, $label, $size, $transform, $attributes) = @_;
322 36 $transform = sprintf("translate(%.1f,%.1f)", $region->pixels($self->offset)) . $transform;
323 36 $attributes .= " transform=\"$transform\"";
324 36 ▶ $attributes .= " " . $self->label_attributes if $self->label_attributes;
Branch: 1 branch
BranchTrueFalseTotal
if $self->label_attributes 36 0 36
325 36 ▶ 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 36 36
Condition33%1/3
$size and not $attributes =~ s/\bfont-size="\d+pt"/font-size="$size"/u
!ll&&!rl&&r
3600
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
326 0 $attributes .= " font-size=\"$size\"";
327 }
328 36 ▶ 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
036
MC/DC0%0/1
$self->glow_attributes || ''
$self->glow_attributes
Truth table50%1/2
A
A= $self->glow_attributes
Aresult
11
00
329 36 $data .= sprintf(qq{<text text-anchor="middle" %s>%s</text></g>\n}, $attributes, $label);
330 36 return $data;
331 }
332  
333 474 ▶sub def {
Subroutine def: called CC=1 SCAR=0.0
Pod undocumented
334 474 my ($self, $svg) = @_;
335 474 $svg =~ s/>\s+</></g;
336 474 push(@{$self->defs}, $svg);
337 }
338  
339 456 ▶sub merge_attributes {
Subroutine merge_attributes: called CC=4 SCAR=13.9
Pod undocumented
340 456 my %attr = ();
341 456 for my $attr (@_) {
342 684 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 61 623 684
343 61 ▶ while ($attr =~ /(\S+)=((["']).*?\3)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /(\S+)=((["']).*?\3)/gu) 94 61 155
344 94 $attr{$1} = $2;
345 }
346 }
347 }
348 456 return join(' ', map { $_ . '=' . $attr{$_} } sort keys %attr);
349 }
350  
351 10 ▶sub svg_header {
Subroutine svg_header: called CC=23 SCAR=33.0
Pod undocumented
352 10 my ($self) = @_;
353  
354 10 my $header = qq{<?xml version="1.0" encoding="UTF-8" standalone="no"?>
355 <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
356 xmlns:xlink="http://www.w3.org/1999/xlink"
357 };
358 10 ▶ return $header . "\n" unless @{$self->regions};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->regions;} 0 10 10
359 10 my $maxz = 0;
360 10 foreach my $region (@{$self->regions}) {
361 2598 ▶ $maxz = $region->z if $region->z > $maxz;
Branch: 1 branch
BranchTrueFalseTotal
if $region->z > $maxz 0 2598 2598
362 }
363 # These are required to calculate the viewBox for the SVG. Min and max X are
364 # what you would expect. Min and max Y are different, however, since we want
365 # to count all the rows on all the levels, plus an extra separator between
366 # them. Thus, min y is the min y of the first level, and max y is the min y of
367 # the first level + 1 for every level beyond the first, + all the rows for
368 # each level.
369 10 my $min_x_overall;
370 my $max_x_overall;
371 10 my $min_y_overall;
372 10 my $max_y_overall;
373 10 for my $z (0 .. $maxz) {
374 10 my ($minx, $miny, $maxx, $maxy);
375 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
376 10 foreach my $region (@{$self->regions}) {
377 2598 ▶ next unless $region->z == $z;
Branch: 1 branch
BranchTrueFalseTotal
unless $region->z == $z 0 2598 2598
378 2598 ▶ $minx = $region->x unless defined $minx and $minx <= $region->x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $minx and $minx <= $region->x 10 2588 2598
Condition66%2/3
defined $minx and $minx <= $region->x
!ll&&!rl&&r
1002588
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
379 2598 ▶ $maxx = $region->x unless defined $maxx and $maxx >= $region->x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxx and $maxx >= $region->x 209 2389 2598
Condition100%3/3
defined $maxx and $maxx >= $region->x
!ll&&!rl&&r
101992389
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
380 2598 ▶ $miny = $region->y unless defined $miny and $miny <= $region->y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $miny and $miny <= $region->y 10 2588 2598
Condition66%2/3
defined $miny and $miny <= $region->y
!ll&&!rl&&r
1002588
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
381 2598 ▶ $maxy = $region->y unless defined $maxy and $maxy >= $region->y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxy and $maxy >= $region->y 99 2499 2598
Condition100%3/3
defined $maxy and $maxy >= $region->y
!ll&&!rl&&r
10892499
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
382 }
383 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
384 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
385 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
386 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
387 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
388 10 $max_y_overall += 1 + $maxy - $miny; # plus the number of rows for every level
389 }
390 10 my ($vx1, $vy1, $vx2, $vy2) = $self->viewbox($min_x_overall, $min_y_overall, $max_x_overall, $max_y_overall);
391 10 my ($width, $height) = ($vx2 - $vx1, $vy2 - $vy1);
392 10 $header .= qq{ viewBox="$vx1 $vy1 $width $height">\n};
393 10 $header .= qq{ <!-- min ($min_x_overall, $min_y_overall), max ($max_x_overall, $max_y_overall) -->\n};
394 10 return $header;
395 }
396  
397 10 ▶sub svg_defs {
Subroutine svg_defs: called CC=12 SCAR=24.8
Pod undocumented
398 10 my ($self) = @_;
399 # All the definitions are included by default.
400 10 my $doc = " <defs>\n";
401 10 ▶ $doc .= " " . join("\n ", @{$self->defs}, "") if @{$self->defs};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->defs;} 7 3 10
402 # collect region types from attributess and paths in case the sets don't overlap
403 10 my %types = ();
404 10 foreach my $region (@{$self->regions}) {
405 2598 foreach my $type (@{$region->type}) {
406 4092 $types{$type} = 1;
407 }
408 }
409 10 foreach my $line (@{$self->lines}) {
410 311 $types{$line->type} = 1;
411 }
412 # now go through them all
413 10 foreach my $type (sort keys %types) {
414 228 my $path = $self->path->{$type};
415 228 my $attributes = merge_attributes($self->attributes->{$type});
416 my $path_attributes = merge_attributes($self->path_attributes->{'default'},
417 228 $self->path_attributes->{$type});
418 228 my $glow_attributes = $self->glow_attributes;
419 228 ▶ if ($path || $attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path or $attributes) { } 46 182 228
Condition100%3/3
$path or $attributes
l!l&&r!l&&!r
838182
MC/DC100%2/2
$path or $attributes
$path $attributes
Truth table100%3/3
A or B
A= $pathB= $attributes
ABresult
1X1
011
000
420 46 $doc .= qq{ <g id="$type">\n};
421 # just shapes get a glow such, eg. a house (must come first)
422 46 ▶ if ($path && !$attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and not $attributes) 3 43 46
Condition100%3/3
$path and not $attributes
!ll&&!rl&&r
3853
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
423 3 $doc .= qq{ <path $glow_attributes d='$path' />\n}
424 }
425 # region with attributes get a shape (square or hex), eg. plains and grass
426 46 ▶ if ($attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes) 43 3 46
427 43 $doc .= " " . $self->shape($attributes) . "\n";
428 }
429 # and now the attributes themselves the shape itself
430 46 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 8 38 46
431 8 $doc .= qq{ <path $path_attributes d='$path' />\n}
432 }
433 # close
434 46 $doc .= qq{ </g>\n};
435 } else {
436 # nothing
437 }
438 }
439 10 $doc .= qq{ </defs>\n};
440 }
441  
442 10 ▶sub svg_backgrounds {
Subroutine svg_backgrounds: called CC=2 SCAR=6.9
Pod undocumented
443 10 my $self = shift;
444 10 my $doc = qq{ <g id="backgrounds">\n};
445 10 foreach my $thing (@{$self->things}) {
446 # make a copy
447 2598 my @types = @{$thing->type};
448 # keep attributes
449 2598 $thing->type([grep { $self->attributes->{$_} } @{$thing->type}]);
450 2598 $doc .= $thing->svg($self->offset);
451 # reset copy
452 2598 $thing->type(\@types);
453 }
454 10 $doc .= qq{ </g>\n};
455 10 return $doc;
456 }
457  
458 10 ▶sub svg_things {
Subroutine svg_things: called CC=2 SCAR=6.9
Pod undocumented
459 10 my $self = shift;
460 10 my $doc = qq{ <g id="things">\n};
461 10 foreach my $thing (@{$self->things}) {
462 # drop attributes
463 2598 $thing->type([grep { not $self->attributes->{$_} } @{$thing->type}]);
464 2598 $doc .= $thing->svg($self->offset);
465 }
466 10 $doc .= qq{ </g>\n};
467 10 return $doc;
468 }
469  
470 10 ▶sub svg_coordinates {
Subroutine svg_coordinates: called CC=2 SCAR=6.9
Pod undocumented
471 10 my $self = shift;
472 10 my $doc = qq{ <g id="coordinates">\n};
473 10 foreach my $region (@{$self->regions}) {
474 2598 $doc .= $region->svg_coordinates($self->offset);
475 }
476 10 $doc .= qq{ </g>\n};
477 10 return $doc;
478 }
479  
480 10 ▶sub svg_lines {
Subroutine svg_lines: called CC=2 SCAR=6.9
Pod undocumented
481 10 my $self = shift;
482 10 my $doc = qq{ <g id="lines">\n};
483 10 foreach my $line (@{$self->lines}) {
484 311 $doc .= $line->svg($self->offset);
485 }
486 10 $doc .= qq{ </g>\n};
487 10 return $doc;
488 }
489  
490 10 ▶sub svg_regions {
Subroutine svg_regions: called CC=3 SCAR=11.0
Pod undocumented
491 10 my $self = shift;
492 10 my $doc = qq{ <g id="regions">\n};
493 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
494 10 foreach my $region (@{$self->regions}) {
495 2598 $doc .= $region->svg_region($attributes, $self->offset);
496 }
497 10 $doc .= qq{ </g>\n};
498 }
499  
500 10 ▶sub svg_line_labels {
Subroutine svg_line_labels: called CC=2 SCAR=6.9
Pod undocumented
501 10 my $self = shift;
502 10 my $doc = qq{ <g id="line_labels">\n};
503 10 foreach my $line (@{$self->lines}) {
504 311 $doc .= $line->svg_label($self->offset);
505 }
506 10 $doc .= qq{ </g>\n};
507 10 return $doc;
508 }
509  
510 10 ▶sub svg_labels {
Subroutine svg_labels: called CC=2 SCAR=6.9
Pod undocumented
511 10 my $self = shift;
512 10 my $doc = qq{ <g id="labels">\n};
513 10 foreach my $region (@{$self->regions}) {
514 2598 $doc .= $region->svg_label($self->url, $self->offset);
515 }
516 10 $doc .= qq{ </g>\n};
517 10 return $doc;
518 }
519  
520 =head2 svg()
521  
522 This method generates the SVG once the map is initialized.
523  
524 =cut
525  
526 10 ▶sub svg {
Subroutine svg: called CC=3 SCAR=11.0
Pod documented
527 10 my ($self) = @_;
528  
529 10 my $doc = $self->svg_header();
530 10 $doc .= $self->svg_defs();
531 10 $doc .= $self->svg_backgrounds(); # opaque backgrounds
532 10 $doc .= $self->svg_lines();
533 10 $doc .= $self->svg_things(); # icons, lines
534 10 $doc .= $self->svg_coordinates();
535 10 $doc .= $self->svg_regions();
536 10 $doc .= $self->svg_line_labels();
537 10 $doc .= $self->svg_labels();
538 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
539 10 $doc .= $self->svg_other();
540  
541 # error messages
542 10 my $y = 10;
543 10 foreach my $msg (@{$self->messages}) {
544 0 $doc .= " <text x='0' y='$y'>$msg</text>\n";
545 0 $y += 10;
546 }
547  
548 # source code (comments may not include -- for SGML compatibility!)
549 # https://stackoverflow.com/questions/10842131/xml-comments-and
550 10 my $source = $self->map();
551 10 $source =~ s/--/&#45;&#45;/g;
552 10 $doc .= "<!-- Source\n$source\n-->\n";
553 10 $doc .= qq{</svg>\n};
554  
555 10 return $doc;
556 }
557  
558 =head1 SEE ALSO
559  
560 L<Game::TextMapper::Mapper::Hex> is for hex maps.
561  
562 L<Game::TextMapper::Mapper::Square> is for square maps.
563  
564 =cut
565  
566 11 1;
« blib/lib/Game/TextMapper/Solo.pm ↑ Summary blib/lib/Game/TextMapper/Schroeder/Archipelago.pm »