Construction/Draw/TikZ.pm

filter: Statementstmt 55.0% 60 / 109 Branchbran 28.8% 15 / 52 Conditioncond 29.6% 8 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 75.0% 9 / 12 Podpod 0.0% 0 / 6 totaltotal 41.8% 92 / 220 CC 42 SCAR 43.5
CC
42
Cov
44%
process_style
154.0
circle
57.5
text
20.0
CRAP 77.6
↑ Summary Construction/Draw/SVG.pm »
1 package Math::Geometry::Construction::Draw::TikZ;
2 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 extends 'Math::Geometry::Construction::Draw';
4  
5 5 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use LaTeX::TikZ as => 'TikZ';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 C<Math::Geometry::Construction::Draw::TikZ> - TikZ output
13  
14 =head1 VERSION
15  
16 Version 0.021
17  
18 =cut
19  
20 5 our $VERSION = '0.021';
21  
22  
23 ###########################################################################
24 # #
25 # Accessors #
26 # #
27 ###########################################################################
28  
29 5 has 'svg_mode' => (isa => 'Bool',
30 is => 'ro',
31 default => 0);
32  
33 5 has 'math_mode' => (isa => 'Bool',
34 is => 'ro',
35 default => 1);
36  
37 5 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
38 5 my ($self, $args) = @_;
39 5 my $seq = TikZ->seq;
40  
41 # add clip
42 5 my $rect = TikZ->rectangle
43 (TikZ->point(0, 0),
44 TikZ->point($self->width, $self->height));
45 5 $seq->clip(TikZ->path($rect));
46 5 $self->_output($seq);
47 }
48  
49 ###########################################################################
50 # #
51 # Generate Output #
52 # #
53 ###########################################################################
54  
55 4 ▶override 'transform_coordinates' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
56 4 my ($self, $x, $y) = @_;
57  
58 4 ($x, $y) = super();
59  
60 0 ▶ if($self->svg_mode) { return($x, $self->height - $y) }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->svg_mode) { } 0 4 4
61 4 else { return($x, $y) }
62 5 };
63  
64 0 ▶override 'is_flipped' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
65 0 ▶ return(super() == 1 ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
super() == 1 ? : 0 0 0
66 5 };
67  
68 3 ▶sub process_style {
Subroutine process_style: called CC=20 SCAR=50.4
Pod undocumented
69 3 my ($self, $element, %style) = @_;
70 3 my $svg_mode = $self->svg_mode;
71  
72 3 ▶ if($element eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($element eq 'text') { } 0 3 3
73 0 ▶ if(exists($style{fill})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $style{"fill"}) 0 0 0
74 0 ▶ $style{color} = $style{fill} if($svg_mode);
Branch: 1 branch
BranchTrueFalseTotal
if $svg_mode 0 0 0
75 0 delete($style{fill});
76 }
77 }
78 else {
79 3 ▶ if(exists($style{stroke})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $style{"stroke"}) 3 0 3
80 3 ▶ $style{color} = $style{stroke} if($svg_mode);
Branch: 1 branch
BranchTrueFalseTotal
if $svg_mode 0 3 3
81 3 delete($style{stroke});
82 }
83  
84 # for some reason 'none' does not work although it should
85 3 ▶ if($style{color} and $style{color} eq 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($style{"color"} and $style{"color"} eq "none") 0 3 3
Condition33%1/3
$style{"color"} and $style{"color"} eq "none"
!ll&&!rl&&r
300
MC/DC0%0/2
$style{"color"} and $style{"color"} eq "none"
$style{"color"} $style{"color"} eq "none"
Truth table33%1/3
A and B
A= $style{"color"}B= $style{"color"} eq "none"
ABresult
0X0
100
111
86 0 ▶ if($style{fill} and $style{fill} ne 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($style{'fill'} and $style{'fill'} ne 'none') { } 0 0 0
Condition0%0/3
$style{"fill"} and $style{"fill"} ne "none"
!ll&&!rl&&r
000
MC/DC0%0/2
$style{"fill"} and $style{"fill"} ne "none"
$style{"fill"} $style{"fill"} ne "none"
Truth table0%0/3
A and B
A= $style{"fill"}B= $style{"fill"} ne "none"
ABresult
0X0
100
111
87 0 $style{color} = $style{fill};
88 }
89 else {
90 delete($style{color})
91 0 }
92 }
93 }
94  
95 3 ▶ while(my ($key, $value) = each(%style)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %style) 2 3 5
96 2 ▶ if($value and ref($value) eq 'ARRAY' and @$value == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value and ref $value eq "ARRAY" and @$value == 3) 0 2 2
Condition33%2/6
$value and ref $value eq "ARRAY" and @$value == 3
!ll&&!rl&&r
200
$value and ref $value eq "ARRAY"
!ll&&!rl&&r
020
MC/DC0%0/3
$value and ref $value eq "ARRAY" and @$value == 3
$value ref $value eq "ARRAY" @$value == 3
Truth table25%1/4
A and B and C
A= $valueB= ref $value eq "ARRAY"C= @$value == 3
ABCresult
0XX0
10X0
1100
1111
97 0 $style{$key} = sprintf('{rgb,255:red,%d;green,%d;blue,%d}',
98 @$value);
99 }
100 2 ▶ if($svg_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($svg_mode) 0 2 2
101 0 ▶ if($key eq 'stroke-dasharray') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "stroke-dasharray") 0 0 0
102 0 ▶ if($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 0 0 0
103 0 my $wsp = qr/[\x{20}\x{9}\x{D}\x{A}]/;
104 0 my $split_pattern = qr/(?:$wsp+\,?$wsp*|\,$wsp*)/;
105 0 my @sections = split($split_pattern, $value);
106 0 my $cmd = 'on';
107 0 foreach(@sections) {
108 0 $_ = "$cmd $_";
109 0 ▶ $cmd = $cmd eq 'on' ? 'off' : 'on';
Branch: 1 branch
BranchTrueFalseTotal
$cmd eq 'on' ? : 0 0 0
110 }
111 0 $style{'dash pattern'} = join(' ', @sections);
112 }
113 0 delete($style{'stroke-dasharray'});
114 }
115 0 ▶ if($key eq 'stroke-dashoffset') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "stroke-dashoffset") 0 0 0
116 0 $style{'dash phase'} = $style{'stroke-dashoffset'};
117 0 delete($style{'stroke-dashoffset'});
118 }
119 }
120 }
121  
122 3 return %style;
123 }
124  
125 0 ▶sub set_background {
Subroutine set_background: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my ($self, $color) = @_;
127 }
128  
129 1 ▶sub line {
Subroutine line: called CC=3 SCAR=11.6
Pod undocumented
130 1 my ($self, %args) = @_;
131  
132 my $line = TikZ->line
133 ([$self->transform_coordinates($args{x1}, $args{y1})],
134 1 [$self->transform_coordinates($args{x2}, $args{y2})]);
135  
136 1 ▶ my %style = $self->process_style('line', %{$args{style} || {}});
Condition50%1/2
$args{'style'} || {}
l!l
10
MC/DC0%0/1
$args{'style'} || {}
$args{'style'}
Truth table50%1/2
A
A= $args{'style'}
Aresult
11
00
137 1 ▶ while(my ($key, $value) = each(%style)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %style) 0 1 1
138 0 $line->mod(TikZ->raw_mod("$key=$value"));
139 }
140  
141 1 $self->output->add($line);
142 }
143  
144 2 ▶sub circle {
Subroutine circle: called CC=16 SCAR=40.5
Pod undocumented
145 2 my ($self, %args) = @_;
146 2 my @raw = ();
147  
148 ($args{cx}, $args{cy}) = $self->transform_coordinates
149 2 ($args{cx}, $args{cy});
150 2 $args{rx} = $self->transform_x_length($args{r});
151 2 $args{ry} = $self->transform_y_length($args{r});
152 2 ▶ if(defined($args{x1}) and defined($args{y1}) and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'x1'} and defined $args{'y1'} and defined $args{'x2'} and defined $args{'y2'}) { } 0 2 2
Condition33%3/9
defined $args{"x1"} and defined $args{"y1"} and defined $args{"x2"} and defined $args{"y2"}
!ll&&!rl&&r
200
defined $args{"x1"} and defined $args{"y1"} and defined $args{"x2"}
!ll&&!rl&&r
200
defined $args{"x1"} and defined $args{"y1"}
!ll&&!rl&&r
200
MC/DC0%0/4
defined $args{"x1"} and defined $args{"y1"} and defined $args{"x2"} and defined $args{"y2"}
defined $args{"x1"} defined $args{"y1"} defined $args{"x2"} defined $args{"y2"}
Truth table20%1/5
A and B and C and D
A= defined $args{"x1"}B= defined $args{"y1"}C= defined $args{"x2"}D= defined $args{"y2"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
153 defined($args{x2}) and defined($args{y2}))
154 {
155 my @boundary = $self->is_flipped
156 ? ([$self->transform_coordinates($args{x2}, $args{y2})],
157 [$self->transform_coordinates($args{x1}, $args{y1})])
158 : ([$self->transform_coordinates($args{x1}, $args{y1})],
159 0 ▶ [$self->transform_coordinates($args{x2}, $args{y2})]);
Branch: 1 branch
BranchTrueFalseTotal
$self->is_flipped ? : 0 0 0
160  
161 0 my @alpha = (); # angles in °
162 0 foreach(@boundary) {
163 0 my $angle = atan2($_->[1] - $args{cy}, $_->[0] - $args{cx});
164 0 ▶ $angle += 6.28318530717959 if($angle < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $angle < 0 0 0 0
165 0 push(@alpha, $angle / 3.14159265358979 * 180);
166 }
167  
168 =pod
169  
170 =begin problematic
171  
172 TexLive crashes on this TikZ construct.
173  
174 my $delta_alpha = $alpha[1] - $alpha[0];
175 $delta_alpha += 360 if($delta_alpha < 0);
176 my $template =
177 '(%f, %f) arc '.
178 '[start angle=%f, delta angle=%f, '.
179 'x radius=%f, y radius=%f]';
180 $raw = TikZ->raw(sprintf($template,
181 @{$boundary[0]},
182 $alpha[0], $delta_alpha,
183 $args{rx}, $args{ry}));
184
185 =end problematic
186  
187 =cut
188  
189  
190 0 ▶ if($alpha[0] > $alpha[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha[0] > $alpha[1]) { } 0 0 0
191 push(@raw, TikZ->raw(sprintf('(%f, %f) arc (%f:%f:%f and %f)',
192 0 @{$boundary[0]},
193 $alpha[0], 360,
194 0 $args{rx}, $args{ry})));
195 push(@raw, TikZ->raw(sprintf('(%f, %f) arc (%f:%f:%f and %f)',
196 $args{cx} + $args{rx}, $args{cy},
197 0, $alpha[1],
198 0 $args{rx}, $args{ry})));
199 }
200 else {
201 push(@raw, TikZ->raw(sprintf('(%f, %f) arc (%f:%f:%f and %f)',
202 0 @{$boundary[0]},
203 $alpha[0], $alpha[1],
204 0 $args{rx}, $args{ry})));
205 }
206 }
207 else {
208 push(@raw, TikZ->raw(sprintf('(%f, %f) ellipse (%f and %f)',
209 $args{cx}, $args{cy},
210 2 $args{rx}, $args{ry})));
211 }
212
213 2 ▶ my %style = $self->process_style('circle', %{$args{style} || {}});
Condition50%1/2
$args{'style'} || {}
l!l
20
MC/DC0%0/1
$args{'style'} || {}
$args{'style'}
Truth table50%1/2
A
A= $args{'style'}
Aresult
11
00
214 2 ▶ while(my ($key, $value) = each(%style)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %style) 2 2 4
215 2 ▶ next if($key eq 'fill');
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "fill" 2 0 2
216 0 foreach(@raw) {
217 0 $_->mod(TikZ->raw_mod("$key=$value"));
218 }
219 }
220 2 ▶ if($style{fill}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style{"fill"}) 2 0 2
221 2 foreach(@raw) {
222 2 $_->mod(TikZ->fill($style{fill}));
223 }
224 }
225
226 2 foreach(@raw) {
227 2 $self->output->add($_);
228 }
229 }
230  
231 0 ▶sub text {
Subroutine text: not called CC=4 SCAR=30.0
Pod undocumented
232 0 my ($self, %args) = @_;
233 0 my $svg_mode = $self->svg_mode;
234 0 ▶ my $template = $self->math_mode
Branch: 1 branch
BranchTrueFalseTotal
$self->math_mode ? : 0 0 0
235 ? '(%f, %f) node {$%s$}' : '(%f, %f) node {%s}';
236  
237 my $content = sprintf
238 ($template,
239 $self->transform_coordinates($args{x}, $args{y}),
240 0 $args{text});
241 0 my $raw = TikZ->raw($content);
242 0 ▶ my %style = $self->process_style('text', %{$args{style} || {}});
Condition0%0/2
$args{'style'} || {}
l!l
00
MC/DC0%0/1
$args{'style'} || {}
$args{'style'}
Truth table0%0/2
A
A= $args{'style'}
Aresult
11
00
243 0 ▶ while(my ($key, $value) = each(%style)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %style) 0 0 0
244 0 $raw->mod(TikZ->raw_mod("$key=$value"));
245 }
246 0 $self->output->add($raw);
247 }
248  
249 5 1;
250  
251  
252 __END__
↑ Summary Construction/Draw/SVG.pm »