blib/lib/Kubernetes/Comb/SVG.pm

filter: Statementstmt 99.7% 479 / 480 Branchbran 95.7% 203 / 212 Conditioncond 75.0% 45 / 60 MC/DCmcdc 59.4% 22 / 37 Subroutinesub 100% 80 / 80 Podpod 80.0% 4 / 5 totaltotal 95.3% 833 / 874 CC 141 SCAR 18.0
CC
141
Cov
97%
_tooltip
15.0
_cell_lines
12.0
_cell_baselines
11.0
CRAP 6.0
« blib/lib/Kubernetes/Comb/SVG/Layout.pm ↑ Summary blib/lib/Kubernetes/Comb/SVG/Cell.pm »
1 package Kubernetes::Comb::SVG;
2 # ABSTRACT: Render Kubernetes::Comb custom resources as an SVG honeycomb
3  
4  
5 99 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 99 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 99 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 99 ▶use Types::Common::Numeric qw( PositiveInt PositiveNum );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 99 ▶use Types::Standard qw( Any ArrayRef Bool CodeRef Enum HashRef Maybe Object Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 99 ▶use Kubernetes::Comb::SVG::Cell;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 99 ▶use Kubernetes::Comb::SVG::Layout;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 99 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 99 our $VERSION = '0.002';
15  
16 99 has combs => ( is => 'ro', isa => Any, required => 1 );
17  
18  
19 99 has title => ( is => 'ro', isa => Str, default => 'Combs' );
20  
21  
22 99 has group_label => ( is => 'ro', isa => Maybe[Str] );
23  
24  
25 99 has layout => ( is => 'ro', isa => Enum[qw( depth packed )], default => 'depth' );
26  
27  
28 99 has columns => ( is => 'ro', isa => PositiveInt, default => 6 );
29  
30  
31 # Whether columns came from the caller, see `columns` in the layout class.
32 99 has _columns_given => ( is => 'rwp', isa => Bool, init_arg => undef, default => 0 );
33  
34 421 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
35 421 my ( $self, $args ) = @_;
36 421 ▶ $self->_set__columns_given(1) if exists $args->{columns};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"columns"} 81 340 421
37 }
38  
39 99 has rows => ( is => 'ro', isa => PositiveInt, predicate => 'has_rows' );
40  
41  
42 99 has aspect => ( is => 'ro', isa => PositiveNum, default => 16 / 9 );
43  
44  
45 99 has size => ( is => 'ro', isa => PositiveNum, default => 56 );
46  
47  
48 99 has edges => ( is => 'lazy', isa => Bool );
49  
50 407 ▶sub _build_edges { $_[0]->layout eq 'packed' ? 0 : 1 }
Subroutine _build_edges: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->layout eq 'packed' ? : 110 297 407
51  
52  
53 99 has legend => ( is => 'ro', isa => Bool, default => 1 );
54  
55  
56 99 has link => ( is => 'ro', isa => Maybe[CodeRef] );
57  
58  
59 369 ▶has theme => ( is => 'ro', isa => HashRef, default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
60  
61  
62 380 ▶has blink => ( is => 'ro', isa => ArrayRef[Str], default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
63  
64  
65 99 has blink_seconds => ( is => 'ro', isa => PositiveNum, default => 1.2 );
66  
67  
68 99 has cells => ( is => 'lazy', isa => ArrayRef[Object], init_arg => undef );
69  
70  
71 421 ▶sub _build_cells {
Subroutine _build_cells: called CC=1 SCAR=0.0
72 421 my ( $self ) = @_;
73 421 return [ $self->cell_class->cells_from( $self->combs, group_label => $self->group_label ) ];
74 }
75  
76 99 has _layouter => ( is => 'lazy', isa => Object, init_arg => undef );
77  
78 421 ▶sub _build__layouter {
Subroutine _build__layouter: called CC=4 SCAR=13.9
79 421 my ( $self ) = @_;
80 421 weaken( my $weak = $self );
81 return $self->layout_class->new(
82 cells => $self->cells,
83 size => $self->size,
84 mode => $self->layout,
85 aspect => $self->aspect,
86 # What render puts around the honeycomb: padding, title, the legend as it
87 # wraps for the width of the honeycomb.
88 347 ▶ frame => sub { $weak->_frame(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 421 ▶ $self->_columns_given || $self->layout eq 'depth' ? ( columns => $self->columns ) : (),
Branch: 2 branches
BranchTrueFalseTotal
$self->_columns_given || $self->layout eq 'depth' ? : 371 47 418
$self->has_rows ? : 10 408 418
Condition100%3/3
$self->_columns_given or $self->layout eq "depth"
l!l&&r!l&&!r
8129047
MC/DC100%2/2
$self->_columns_given or $self->layout eq "depth"
$self->_columns_given $self->layout eq "depth"
Truth table100%3/3
A or B
A= $self->_columns_givenB= $self->layout eq "depth"
ABresult
1X1
011
000
90 $self->has_rows ? ( rows => $self->rows ) : ()
91 );
92 }
93  
94 3052 ▶sub cell_class { 'Kubernetes::Comb::SVG::Cell' }
Subroutine cell_class: called CC=1 SCAR=0.0
Pod documented
95  
96  
97 421 ▶sub layout_class { 'Kubernetes::Comb::SVG::Layout' }
Subroutine layout_class: called CC=1 SCAR=0.0
Pod documented
98  
99  
100 #### Phases and colours
101  
102 2631 ▶sub phases {
Subroutine phases: called CC=1 SCAR=0.0
Pod documented
103 2631 my ( $self ) = @_;
104 2631 return ( $self->cell_class->known_phases, 'Unknown' );
105 }
106  
107  
108 # Per phase the stroke colour in light and in dark mode; the fill is the same
109 # colour at a low opacity over the panel, so text keeps its contrast whatever
110 # colour a theme brings.
111 838 ▶sub _default_colours {
Subroutine _default_colours: called CC=1 SCAR=0.0
112 return {
113 838 Running => [ '#1a7f37', '#3fb950' ],
114 Pending => [ '#bf8700', '#e3b341' ],
115 Blocked => [ '#bc4c00', '#fb8f44' ],
116 NeedsConfig => [ '#8250df', '#a371f7' ],
117 Disabled => [ '#8c959f', '#6e7681' ],
118 Error => [ '#cf222e', '#f85149' ],
119 Stopped => [ '#0891b2', '#39c5cf' ],
120 NotDeployed => [ '#0969da', '#58a6ff' ],
121 Unknown => [ '#475569', '#94a3b8' ]
122 };
123 }
124  
125 419 ▶sub _base_colours {
Subroutine _base_colours: called CC=1 SCAR=0.0
126 return (
127 419 [ bg => '#ffffff', '#0d1117' ],
128 [ border => '#d0d7de', '#30363d' ],
129 [ fg => '#1f2328', '#e6edf3' ],
130 [ muted => '#59636e', '#9198a1' ],
131 [ edge => '#57606a', '#9198a1' ]
132 );
133 }
134  
135 # A colour is used only when it is plain colour syntax: #hex, a colour name,
136 # or rgb()/hsl() over a strict character set. Anything else is ignored.
137 14100 ▶sub _colour {
Subroutine _colour: called CC=6 SCAR=17.9
138 14100 my ( $self, $value ) = @_;
139 14100 ▶ return if !defined $value || ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 13969 131 14100
Condition66%2/3
not defined $value or ref $value
l!l&&r!l&&!r
139690131
MC/DC50%1/2
not defined $value or ref $value
not defined $value ref $value
Truth table66%2/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
140 131 ▶ return $value if $value =~ /\A#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\z/ 60 71 131
141 71 ▶ return $value if $value =~ /\A[a-zA-Z]{1,32}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A[a-zA-Z]{1,32}\z/ 18 53 71
142 53 ▶ return $value if $value =~ m{\A(?:rgb|hsl)a?\([0-9a-zA-Z%., /-]{1,64}\)\z}i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ m[\A(?:rgb|hsl)a?\([0-9a-zA-Z%., /-]{1,64}\)\z]i 17 36 53
143 36 return;
144 }
145  
146 # The light and the dark colour a theme gives a key, undef for a mode it
147 # does not give or gives in no plain colour syntax.
148 7050 ▶sub _theme_modes {
Subroutine _theme_modes: called CC=2 SCAR=6.9
149 7050 my ( $self, $key ) = @_;
150 7050 my $value = $self->theme->{$key};
151 14100 ▶ return map { scalar $self->_colour($_) }
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 34 7016 7050
152 7050 ref $value eq 'HASH' ? @{$value}{qw( light dark )} : ( $value, $value );
153 }
154  
155 # The light and the dark colour of a theme key (a phase or a surface): what
156 # the theme gives, one colour for both or one per mode, else the default.
157 5872 ▶sub _theme_colours {
Subroutine _theme_colours: called CC=3 SCAR=11.0
158 5872 my ( $self, $key, $light, $dark ) = @_;
159 5872 my ( $l, $d ) = $self->_theme_modes($key);
160 5872 ▶ return ( defined $l ? $l : $light, defined $d ? $d : $dark );
Branch: 2 branches
BranchTrueFalseTotal
defined $l ? : 40 5832 5872
defined $d ? : 41 5831 5872
161 }
162  
163 # A string for a CSS attribute selector: what the attribute value keeps
164 # (see _text), everything but plain characters as a hex escape. Group names
165 # come from the cluster, so nothing else may reach the style.
166 124 ▶sub _css_string {
Subroutine _css_string: called CC=1 SCAR=0.0
167 124 my ( $self, $value ) = @_;
168 124 $value = ''.$value;
169 124 $value =~ s/[^\x09\x0A\x0D\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//g;
170 124 $value =~ s/([^A-Za-z0-9 _.\/-])/sprintf( '\\%x ', ord $1 )/ge;
171 124 return $value;
172 }
173  
174 124 ▶sub _group_scope { '.comb[data-comb-group="'.$_[0]->_css_string($_[1]).'"]' }
Subroutine _group_scope: called CC=1 SCAR=0.0
175  
176 # The key of a group in blink and theme: `@group` for every phase of the
177 # group, `Phase@group` for one. The phase is everything before the first `@`.
178 1798 ▶sub _group_key { $_[2].'@'.$_[1] }
Subroutine _group_key: called CC=1 SCAR=0.0
179  
180 # The colours of the cells of the groups that occur, as custom properties set
181 # on the cell: the cell's own `--comb-<phase>` wins over the one on the root
182 # element, and the hexagon rules read it from there. `@group` gives every
183 # phase of the group a colour, `Phase@group` one phase, and the more specific
184 # one wins. A mode the theme leaves out keeps the colour it would have had.
185 # Returns the rules for light and for dark.
186 419 ▶sub _group_colours {
Subroutine _group_colours: called CC=9 SCAR=22.0
187 419 my ( $self ) = @_;
188 419 my $colours = $self->_default_colours;
189 419 my @rules = ( [], [] );
190 419 for my $group ( @{ $self->_facts->{groups} } ) {
191 62 my $scope = $self->_group_scope($group);
192 62 my @all = $self->_theme_modes( $self->_group_key( $group, '' ) );
193 62 for my $mode ( 0, 1 ) {
194 124 my @decl;
195 124 ▶ if ( defined $all[$mode] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $all[$mode]) 6 118 124
196 6 push @decl, [ $scope, [ map { $self->_var($_).':'.$all[$mode] } $self->phases ] ];
197 }
198 124 for my $phase ( $self->phases ) {
199 1116 my @one = $self->_theme_modes( $self->_group_key( $group, $phase ) );
200 1116 ▶ next unless defined $one[0] || defined $one[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $one[0] or defined $one[1] 1110 6 1116
Condition100%3/3
defined $one[0] or defined $one[1]
l!l&&r!l&&!r
421110
MC/DC100%2/2
defined $one[0] or defined $one[1]
defined $one[0] defined $one[1]
Truth table100%3/3
A or B
A= defined $one[0]B= defined $one[1]
ABresult
1X1
011
000
201 6 my $own = ( $self->_theme_colours( $phase, @{ $colours->{$phase} } ) )[$mode];
202 6 ▶ my $value = defined $one[$mode] ? $one[$mode] : defined $all[$mode] ? $all[$mode] : $own;
Branch: 2 branches
BranchTrueFalseTotal
defined $one[$mode] ? : 4 2 6
defined $all[$mode] ? : 1 1 2
203 6 push @decl, [ $scope.'.phase-'.$phase, [ $self->_var($phase).':'.$value ] ];
204 }
205 124 push @{ $rules[$mode] }, map { '.comb-svg '.$_->[0].'{'.join( ';', @{ $_->[1] } ).'}' } @decl;
206 }
207 }
208 419 return @rules;
209 }
210  
211 # The cells that blink, as selectors in the order of the legend, then the
212 # groups by name: a phase names the cells of that phase, `@group` the cells
213 # of the group, `Phase@group` the cells of that phase in that group. Only
214 # known phases and groups that occur reach the style, whatever order and
215 # however often they were named.
216 419 ▶sub _blink_scopes {
Subroutine _blink_scopes: called CC=3 SCAR=11.0
217 419 my ( $self ) = @_;
218 419 my %blink = map { $_ => 1 } @{ $self->blink };
219 419 my @scopes = map { '.comb.phase-'.$_ } grep { $blink{$_} } $self->phases;
220 419 for my $group ( @{ $self->_facts->{groups} } ) {
221 62 my $scope = $self->_group_scope($group);
222 62 ▶ push @scopes, $scope if $blink{ $self->_group_key( $group, '' ) };
Branch: 1 branch
BranchTrueFalseTotal
if $blink{$self->_group_key($group, "")} 4 58 62
223 3 push @scopes, map { $scope.'.phase-'.$_ }
224 62 grep { $blink{ $self->_group_key( $group, $_ ) } } $self->phases;
225 }
226 419 return @scopes;
227 }
228  
229 # The period as plain decimals; what does not print as such (inf, a number
230 # beyond %f) gives the default.
231 40 ▶sub _blink_period {
Subroutine _blink_period: called CC=3 SCAR=11.0
232 40 my ( $self ) = @_;
233 40 my $seconds = $self->_n( $self->blink_seconds );
234 40 ▶ return 1.2 unless $seconds =~ /\A[0-9]{1,9}(?:\.[0-9]{1,2})?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $seconds =~ /\A[0-9]{1,9}(?:\.[0-9]{1,2})?\z/ 2 38 40
235 38 ▶ return $seconds < 0.01 ? 0.01 : $seconds;
Branch: 1 branch
BranchTrueFalseTotal
$seconds < 0.01 ? : 1 37 38
236 }
237  
238 # The pulse: fill and outline of the hexagon swell and settle, from and back
239 # to what the cell has anyway, so nothing moves and the texts stay readable.
240 # The phase line takes the text colour, to keep its contrast on the fuller
241 # fill. Without motion the outline is as thick as at the peak.
242 419 ▶sub _blink_css {
Subroutine _blink_css: called CC=2 SCAR=6.9
243 419 my ( $self ) = @_;
244 419 my @scopes = $self->_blink_scopes;
245 419 ▶ return unless @scopes;
Branch: 1 branch
BranchTrueFalseTotal
unless @scopes 379 40 419
246 40 my $s = '.comb-svg';
247 40 my $hex = join( ',', map { $s.' '.$_.' .hex' } @scopes );
248 40 my $width = 'stroke-width:'.$self->_n( $self->size * 0.07 );
249 return (
250 '@keyframes comb-blink{50%{fill-opacity:.4;'.$width.'}}',
251 $hex.'{animation:comb-blink '.$self->_blink_period.'s ease-in-out infinite}',
252 40 join( ',', map { $s.' '.$_.' .phase' } @scopes ).'{fill:var(--comb-fg)}',
253 '@media (prefers-reduced-motion:reduce){'.$hex.'{animation:none;'.$width.'}}'
254 );
255 }
256  
257 19334 ▶sub _var { '--comb-'.lc $_[1] }
Subroutine _var: called CC=1 SCAR=0.0
258  
259 # $small: some cell sets its name in the small two-line font. Its rule is
260 # written only then, so a picture without such a name keeps its bytes.
261 # $reason: some cell has a reason line; the same holds for its rule.
262 419 ▶sub _style {
Subroutine _style: called CC=5 SCAR=16.1
263 419 my ( $self, $small, $reason ) = @_;
264 419 my $r = $self->size;
265 419 my $colours = $self->_default_colours;
266 419 my ( @light, @dark );
267 419 for my $colour ( $self->_base_colours, map { [ $_, @{ $colours->{$_} } ] } $self->phases ) {
268 5866 my ( $light, $dark ) = $self->_theme_colours(@$colour);
269 5866 push @light, $self->_var( $colour->[0] ).':'.$light;
270 5866 push @dark, $self->_var( $colour->[0] ).':'.$dark;
271 }
272 419 push @light, '--comb-tint:.13', '--comb-tint-muted:.06';
273 419 push @dark, '--comb-tint:.2', '--comb-tint-muted:.09';
274  
275 419 my ( $group_light, $group_dark ) = $self->_group_colours;
276 419 my $s = '.comb-svg';
277 my @css = (
278 $s.'{'.join( ';', @light )
279 .';font-family:system-ui,-apple-system,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif}',
280 '@media (prefers-color-scheme:dark){'.$s.'{'.join( ';', @dark ).'}}',
281 @$group_light,
282 @$group_dark ? '@media (prefers-color-scheme:dark){'.join( '', @$group_dark ).'}' : (),
283 $s.' .panel{fill:var(--comb-bg);stroke:var(--comb-border);stroke-width:1}',
284 $s.' text{fill:var(--comb-fg)}',
285 $s.' .heading{font-size:'.$self->_n( $self->_title_font ).'px;font-weight:600}',
286 $s.' .group-name{font-size:'.$self->_n( $self->_group_font )
287 .'px;font-weight:600;fill:var(--comb-muted)}',
288 $s.' .group-rule{stroke:var(--comb-border);stroke-width:1}',
289 $s.' .hex{fill:var(--comb-unknown);fill-opacity:var(--comb-tint);stroke:var(--comb-unknown);stroke-width:'
290 .$self->_n( $r * 0.03 ).';stroke-linejoin:round}',
291 ( map {
292 419 ▶ $s.' .phase-'.$_.' .hex{fill:var('.$self->_var($_).');stroke:var('.$self->_var($_).')}'
Branch: 3 branches
BranchTrueFalseTotal
@$group_dark ? : 5 414 419
$small ? : 19 400 419
$reason ? : 31 388 419
293 } $self->phases ),
294 $s.' .borrowed .hex{stroke-dasharray:'.$self->_n( $r * 0.11 ).' '.$self->_n( $r * 0.08 ).'}',
295 $s.' .disabled .hex{fill-opacity:var(--comb-tint-muted);stroke-opacity:.55}',
296 $s.' .comb text{text-anchor:middle}',
297 $s.' .name{font-size:'.$self->_n( $self->_name_font ).'px;font-weight:500}',
298 $small ? $s.' .name-small{font-size:'.$self->_n( $self->_name_small_font ).'px}' : (),
299 $s.' .phase{font-size:'.$self->_n( $self->_phase_font ).'px;fill:var(--comb-muted)}',
300 $s.' .upstream{font-size:'.$self->_n( $self->_upstream_font )
301 .'px;font-style:italic;fill:var(--comb-muted)}',
302 $reason ? $s.' .reason{font-size:'.$self->_n( $self->_reason_font )
303 .'px;fill:var(--comb-muted)}' : (),
304 $s.' .disabled text{fill:var(--comb-muted);fill-opacity:.8}',
305 $s.' .dep{fill:none;stroke:var(--comb-edge);stroke-opacity:.5;stroke-width:'
306 .$self->_n( $r * 0.025 ).';stroke-linecap:round;pointer-events:none}',
307 $s.' .arrow{fill:var(--comb-edge);fill-opacity:.5}',
308 $s.' .dep-start{fill:var(--comb-edge);fill-opacity:.5;pointer-events:none}',
309 $s.' a{cursor:pointer;text-decoration:none}',
310 $s.' .legend text{font-size:'.$self->_n( $self->_legend_font ).'px;fill:var(--comb-muted)}',
311 $s.' .legend .hex{stroke-width:'.$self->_n( $r * 0.02 ).'}',
312 $s.' .legend .count{font-weight:600;fill:var(--comb-fg)}',
313 $self->_blink_css
314 );
315 419 return $self->_el( 'style', [], $self->_text( join( "\n", @css ) ) );
316 }
317  
318 #### Measures, all derived from size
319  
320 1113 ▶sub _pad { $_[0]->size * 0.45 }
Subroutine _pad: called CC=1 SCAR=0.0
321  
322 1604 ▶sub _title_font { $_[0]->size * 0.34 }
Subroutine _title_font: called CC=1 SCAR=0.0
323  
324 766 ▶sub _title_band { $_[0]->size * 0.75 }
Subroutine _title_band: called CC=1 SCAR=0.0
325  
326 577 ▶sub _group_font { $_[0]->size * 0.2 }
Subroutine _group_font: called CC=1 SCAR=0.0
327  
328 7419 ▶sub _name_font { $_[0]->size * 0.22 }
Subroutine _name_font: called CC=1 SCAR=0.0
329  
330 # A name on two lines that is too wide at _name_font: 13 characters a line
331 # instead of 11.
332 679 ▶sub _name_small_font { $_[0]->size * 0.185 }
Subroutine _name_small_font: called CC=1 SCAR=0.0
333  
334 419 ▶sub _phase_font { $_[0]->size * 0.17 }
Subroutine _phase_font: called CC=1 SCAR=0.0
335  
336 4492 ▶sub _upstream_font { $_[0]->size * 0.15 }
Subroutine _upstream_font: called CC=1 SCAR=0.0
337  
338 # The reason line is as small as the upstream line: 16 characters.
339 3795 ▶sub _reason_font { $_[0]->_upstream_font }
Subroutine _reason_font: called CC=1 SCAR=0.0
340  
341 4549 ▶sub _legend_font { $_[0]->size * 0.19 }
Subroutine _legend_font: called CC=1 SCAR=0.0
342  
343 2936 ▶sub _legend_swatch { $_[0]->size * 0.15 }
Subroutine _legend_swatch: called CC=1 SCAR=0.0
344  
345 2071 ▶sub _legend_row { $_[0]->size * 0.44 }
Subroutine _legend_row: called CC=1 SCAR=0.0
346  
347 766 ▶sub _legend_gap { $_[0]->size * 0.5 }
Subroutine _legend_gap: called CC=1 SCAR=0.0
348  
349 # Air kept between a name and the two sides of its hexagon.
350 8112 ▶sub _name_pad { $_[0]->size * 0.14 }
Subroutine _name_pad: called CC=1 SCAR=0.0
351  
352 703 ▶sub _arrow { $_[0]->size * 0.16 }
Subroutine _arrow: called CC=1 SCAR=0.0
353  
354 # Radius of the dot that marks where an edge starts.
355 426 ▶sub _start_dot { $_[0]->size * 0.04 }
Subroutine _start_dot: called CC=1 SCAR=0.0
356  
357 # Text is not measured, it is estimated: this share of the font size per
358 # character. Good enough for a system sans, and the same on every machine.
359 13241 ▶sub _char_width { 0.59 }
Subroutine _char_width: called CC=1 SCAR=0.0
360  
361 3421 ▶sub _text_width {
Subroutine _text_width: called CC=1 SCAR=0.0
362 3421 my ( $self, $text, $font ) = @_;
363 3421 return length( $text ) * $font * $self->_char_width;
364 }
365  
366 # Cuts a text to what fits into $width at $font, with an ellipsis.
367 1849 ▶sub _fit {
Subroutine _fit: called CC=3 SCAR=11.0
368 1849 my ( $self, $text, $font, $width ) = @_;
369 1849 my $max = int( $width / ( $font * $self->_char_width ) );
370 1849 ▶ $max = 1 if $max < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $max < 1 0 1849 1849
371 1849 ▶ return $text if length $text <= $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $text <= $max 973 876 1849
372 876 return substr( $text, 0, $max - 1 )."\x{2026}";
373 }
374  
375 # How many characters fit into $width at $font, one at the least.
376 7971 ▶sub _chars {
Subroutine _chars: called CC=2 SCAR=7.1
377 7971 my ( $self, $font, $width ) = @_;
378 7971 my $max = int( $width / ( $font * $self->_char_width ) );
379 7971 ▶ return $max < 1 ? 1 : $max;
Branch: 1 branch
BranchTrueFalseTotal
$max < 1 ? : 0 7971 7971
380 }
381  
382 # The name of a cell as it is drawn: { lines => [ one or two ], small => bool }.
383 # A name that fits stays one line. Otherwise it is broken after a hyphen, a
384 # dot or an underscore, at the break that leaves the shortest longer line (the
385 # earlier of two equal ones); when that is too wide for the name font the two
386 # lines take the small one, and what is still too wide is cut there. A name
387 # too long without such a character is cut on its one line.
388 6230 ▶sub _name_lines {
Subroutine _name_lines: called CC=8 SCAR=20.8
389 6230 my ( $self, $name ) = @_;
390 6230 my $width = $self->_layouter->hex_width - 2 * $self->_name_pad;
391 6230 my $length = length $name;
392 6230 ▶ return { lines => [$name], small => 0 }
Branch: 1 branch
BranchTrueFalseTotal
if $length <= $self->_chars($self->_name_font, $width) 5460 770 6230
393 if $length <= $self->_chars( $self->_name_font, $width );
394  
395 770 my ( $at, $longer );
396 770 for my $break ( grep { substr( $name, $_ - 1, 1 ) =~ /\A[-._]\z/ } 1 .. $length - 1 ) {
397 1386 my $rest = $length - $break;
398 1386 ▶ my $long = $break > $rest ? $break : $rest;
Branch: 1 branch
BranchTrueFalseTotal
$break > $rest ? : 636 750 1386
399 1386 ▶ ( $at, $longer ) = ( $break, $long ) if !defined $longer || $long < $longer;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $longer or $long < $longer 770 616 1386
Condition100%3/3
not defined $longer or $long < $longer
l!l&&r!l&&!r
616154616
MC/DC100%2/2
not defined $longer or $long < $longer
not defined $longer $long < $longer
Truth table100%3/3
not(A) or B
A= not defined $longerB= $long < $longer
ABresult
1X1
011
000
400 }
401 770 ▶ return { lines => [ $self->_fit( $name, $self->_name_font, $width ) ], small => 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at 154 616 770
402 unless defined $at;
403  
404 616 my @lines = ( substr( $name, 0, $at ), substr( $name, $at ) );
405 616 ▶ return { lines => \@lines, small => 0 }
Branch: 1 branch
BranchTrueFalseTotal
if $longer <= $self->_chars($self->_name_font, $width) 286 330 616
406 if $longer <= $self->_chars( $self->_name_font, $width );
407 return {
408 330 lines => [ map { $self->_fit( $_, $self->_name_small_font, $width ) } @lines ],
409 small => 1
410 };
411 }
412  
413 # The text lines of a cell, top to bottom, as text elements: { class, rows },
414 # a row being [ text, baseline as an offset from the centre of the cell ].
415 # Where the lines sit is decided by _cell_baselines. A reason too long for
416 # its line takes a second one where the cell has the room, see _reason_break.
417 3115 ▶sub _cell_lines {
Subroutine _cell_lines: called CC=12 SCAR=24.8
418 3115 my ( $self, $cell ) = @_;
419 3115 my $name = $self->_name_lines( $cell->name );
420 3115 my @names = @{ $name->{lines} };
421 3115 ▶ my $step = $self->size * ( $name->{small} ? 0.21 : 0.24 );
Branch: 1 branch
BranchTrueFalseTotal
$name->{'small'} ? : 165 2950 3115
422 3115 my $reason = $self->_shows_reason($cell);
423  
424 # The small lines under the phase, by class.
425 3115 ▶ my @below = ( $reason ? 'reason' : (), $cell->borrowed ? 'upstream' : () );
Branch: 2 branches
BranchTrueFalseTotal
$reason ? : 527 2588 3115
$cell->borrowed ? : 278 2837 3115
426 3115 my @at = $self->_cell_baselines( scalar @names, $step, @below );
427 3115 my @reason;
428 3115 ▶ if ($reason) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason) 527 2588 3115
429 527 my $text = $cell->reason;
430 527 my $one = $at[ @names + 1 ];
431 527 @reason = ( $self->_fit( $text, $self->_reason_font, $self->_reason_width($one) ) );
432 527 ▶ my @two = length $text > $self->_reason_chars($one)
Branch: 1 branch
BranchTrueFalseTotal
length $text > $self->_reason_chars($one) ? : 410 117 527
433 ? $self->_cell_baselines( scalar @names, $step, 'reason', @below )
434 : ();
435 527 ▶ if (@two) {
Branch: 1 branch
BranchTrueFalseTotal
if (@two) 299 228 527
436 299 my @under = @two[ @names + 1, @names + 2 ];
437 299 ▶ if ( my @broken = $self->_reason_break( $text, map { $self->_reason_chars($_) } @under ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@broken) = $self->_reason_break($text, map({$self->_reason_chars($_);} @under))) 230 69 299
438 230 $broken[1] = $self->_fit( $broken[1], $self->_reason_font, $self->_reason_width( $under[1] ) );
439 230 @reason = @broken;
440 230 @at = @two;
441 }
442 }
443 }
444  
445 my @lines = (
446 3115 ▶ { class => $name->{small} ? 'name name-small' : 'name', rows => [ map { [ $_, shift @at ] } @names ] },
Branch: 1 branch
BranchTrueFalseTotal
$name->{'small'} ? : 165 2950 3115
447 { class => 'phase', rows => [ [ $cell->phase, shift @at ] ] }
448 );
449 3115 ▶ push @lines, { class => 'reason', rows => [ map { [ $_, shift @at ] } @reason ] } if $reason;
Branch: 1 branch
BranchTrueFalseTotal
if $reason 527 2588 3115
450 3115 ▶ push @lines, { class => 'upstream', rows => [ [
Branch: 2 branches
BranchTrueFalseTotal
if $cell->borrowed 278 2837 3115
defined $cell->upstream_context ? : 274 4 278
451 $self->_fit(
452 defined $cell->upstream_context ? 'from '.$cell->upstream_context : 'borrowed',
453 $self->_upstream_font, $self->_layouter->hex_width * 0.8
454 ),
455 shift @at
456 ] ] } if $cell->borrowed;
457 3115 return @lines;
458 }
459  
460 # The baselines of the lines of a cell, top to bottom, as offsets from its
461 # centre: $names name lines $step apart, the phase, then one line per class in
462 # @below ('reason', once or twice, and 'upstream'). The lines are stacked by
463 # their advance, the distance of a baseline from the one above.
464 #
465 # A cell without a reason line, and one with nothing but one name line, the
466 # phase and one reason line, starts where a cell with one name line and its
467 # phase sits centred, every further line moving that start up.
468 #
469 # Every other cell with a reason line is tight: the small lines sit closer
470 # together and the block is centred as a whole, to stay inside the hexagon.
471 # The advances of such a block may add up to 0.89 of the size; what the block
472 # leaves of that goes to the phase, up to 0.28 of the size from the name
473 # above it, so that it does not read as one more name line. A block that is
474 # too high even with the phase at 0.24 gives an empty list when it has two
475 # reason lines -- there is no room for the second -- and is drawn as it is
476 # otherwise.
477 3525 ▶sub _cell_baselines {
Subroutine _cell_baselines: called CC=11 SCAR=24.0
478 3525 my ( $self, $names, $step, @below ) = @_;
479 3525 my $r = $self->size;
480 3525 my $reason = grep { $_ eq 'reason' } @below;
481 3525 ▶ my $tight = $reason && $names + @below > 2;
Condition100%3/3
$reason && $names + @below > 2
!ll&&!rl&&r
2588151786
MC/DC100%2/2
$reason && $names + @below > 2
$reason $names + @below > 2
Truth table100%3/3
A && B
A= $reasonB= $names + @below > 2
ABresult
0X0
100
111
482 3525 ▶ my ( $to_phase, $to_below ) = $tight ? ( 0.24 * $r, 0.2 * $r ) : ( 0.3 * $r, 0.25 * $r );
Branch: 1 branch
BranchTrueFalseTotal
$tight ? : 786 2739 3525
483 3525 my @advance = ( $to_below ) x @below;
484 3525 ▶ $advance[1] = 0.17 * $r if $reason > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $reason > 1 410 3115 3525
485  
486 3525 my $at;
487 3525 ▶ if ($tight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tight) { } 786 2739 3525
488 786 my $sum = $step * ( $names - 1 ) + $to_phase;
489 786 $sum += $_ for @advance;
490 786 my $spare = 0.89 * $r - $sum;
491 786 ▶ return if $reason > 1 && $spare < -$r * 1e-9;
Branch: 1 branch
BranchTrueFalseTotal
if $reason > 1 and $spare < -$r * "1e-09" 111 675 786
Condition100%3/3
$reason > 1 and $spare < -$r * "1e-09"
!ll&&!rl&&r
376299111
MC/DC100%2/2
$reason > 1 and $spare < -$r * "1e-09"
$reason > 1 $spare < -$r * "1e-09"
Truth table100%3/3
A and B
A= $reason > 1B= $spare < -$r * "1e-09"
ABresult
0X0
100
111
492 675 ▶ $spare = $spare < 0 ? 0 : $spare > 0.04 * $r ? 0.04 * $r : $spare;
Branch: 2 branches
BranchTrueFalseTotal
$spare < 0 ? : 0 675 675
$spare > 0.04 * $r ? : 587 88 675
493 675 $to_phase += $spare;
494 675 $at = 0.06 * $r - ( $sum + $spare ) / 2;
495 }
496 else {
497 2739 $at = -0.07 * $r - $step * ( $names - 1 ) / 2 - 0.1 * $r * @below;
498 }
499 3414 my @at = ( $at );
500 3414 push @at, $at += $_ for ( $step ) x ( $names - 1 ), $to_phase, @advance;
501 3414 return @at;
502 }
503  
504 # The width a reason line with its baseline at $at may take, and the number
505 # of characters that is: sixteen inside the full-width band of the hexagon,
506 # fewer for a line that reaches below it.
507 1882 ▶sub _reason_width {
Subroutine _reason_width: called CC=1 SCAR=0.0
508 1882 my ( $self, $at ) = @_;
509 1882 my $font = $self->_reason_font;
510 1882 return $self->_line_width( $at - 0.75 * $font, $at + 0.25 * $font );
511 }
512  
513 1125 ▶sub _reason_chars {
Subroutine _reason_chars: called CC=1 SCAR=0.0
514 1125 my ( $self, $at ) = @_;
515 1125 return $self->_chars( $self->_reason_font, $self->_reason_width($at) );
516 }
517  
518 # A reason on two lines of at most $first and $second characters, or nothing
519 # when it cannot be broken. It breaks at a run of whitespace, which is
520 # dropped, and before an upper-case letter that follows a lower-case letter
521 # or a digit. Of the breaks that make both lines fit it takes the one with the
522 # shortest longer line (the earlier of two equal ones); when there is none,
523 # the last break whose first line fits, the second line then being too long
524 # and left to the caller to cut.
525 299 ▶sub _reason_break {
Subroutine _reason_break: called CC=9 SCAR=22.1
526 299 my ( $self, $text, $first, $second ) = @_;
527 299 my ( @best, $longer, @last );
528 299 ▶ while ( $text =~ /\s+|(?<=[\p{Ll}0-9])(?=\p{Lu})/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\s+|(?<=[\p{Ll}0-9])(?=\p{Lu})/g) 611 299 910
529 611 my @lines = ( substr( $text, 0, $-[0] ), substr( $text, $+[0] ) );
530 611 ▶ next if !length $lines[0] || !length $lines[1] || length $lines[0] > $first;
Branch: 1 branch
BranchTrueFalseTotal
if not length $lines[0] && length $lines[1] or length $lines[0] > $first 287 324 611
Condition50%3/6
not length $lines[0] && length $lines[1] or length $lines[0] > $first
l!l&&r!l&&!r
0287324
length $lines[0] && length $lines[1]
!ll&&!rl&&r
00611
MC/DC33%1/3
not length $lines[0] && length $lines[1] or length $lines[0] > $first
length $lines[0] length $lines[1] length $lines[0] > $first
Truth table50%2/4
not(A && B) or C
A= length $lines[0]B= length $lines[1]C= length $lines[0] > $first
ABCresult
0XX1
10X1
1111
1100
531 324 @last = @lines;
532 324 ▶ next if length $lines[1] > $second;
Branch: 1 branch
BranchTrueFalseTotal
if length $lines[1] > $second 144 180 324
533 180 my ( $long ) = sort { $b <=> $a } map { length } @lines;
534 180 ▶ ( $longer, @best ) = ( $long, @lines ) if !defined $longer || $long < $longer;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $longer or $long < $longer 168 12 180
Condition100%3/3
not defined $longer or $long < $longer
l!l&&r!l&&!r
1551312
MC/DC100%2/2
not defined $longer or $long < $longer
not defined $longer $long < $longer
Truth table100%3/3
not(A) or B
A= not defined $longerB= $long < $longer
ABresult
1X1
011
000
535 }
536 299 ▶ return @best ? @best : @last;
Branch: 1 branch
BranchTrueFalseTotal
@best ? : 155 144 299
537 }
538  
539 # Whether a cell gets a reason line: it has a reason and is not Running.
540 6230 ▶sub _shows_reason {
Subroutine _shows_reason: called CC=5 SCAR=16.6
541 6230 my ( $self, $cell ) = @_;
542 6230 my $reason = $cell->reason;
543 6230 ▶ return 0 if !defined $reason || ref $reason || !length $reason;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $reason or ref $reason or not length $reason 5176 1054 6230
Condition66%4/6
not defined $reason or ref $reason or not length $reason
l!l&&r!l&&!r
517601054
not defined $reason or ref $reason
l!l&&r!l&&!r
517601054
MC/DC33%1/3
not defined $reason or ref $reason or not length $reason
not defined $reason ref $reason not length $reason
Truth table50%2/4
not(A) or B or not(C)
A= not defined $reasonB= ref $reasonC= not length $reason
ABCresult
1XX1
01X1
0011
0000
544 1054 ▶ return $cell->phase eq 'Running' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$cell->phase eq 'Running' ? : 0 1054 1054
545 }
546  
547 # The width a line of text may take when it reaches from $top to $bottom,
548 # both offsets from the centre of the cell: the width of the hexagon where it
549 # is narrowest in that span -- full inside the middle band of half a radius
550 # up and down, less towards the corners -- minus the air on both sides.
551 1882 ▶sub _line_width {
Subroutine _line_width: called CC=3 SCAR=11.0
552 1882 my ( $self, $top, $bottom ) = @_;
553 1882 my $r = $self->size;
554 1882 my ( $far ) = sort { $b <=> $a } abs $top, abs $bottom;
555 1882 ▶ my $share = $far <= $r / 2 ? 1 : $far >= $r ? 0 : 2 * ( $r - $far ) / $r;
Branch: 2 branches
BranchTrueFalseTotal
$far <= $r / 2 ? : 1708 174 1882
$far >= $r ? : 0 174 174
556 1882 return $self->_layouter->hex_width * $share - 2 * $self->_name_pad;
557 }
558  
559 #### Escaping
560  
561 # Text content. Drops what XML 1.0 cannot carry, escapes the five special
562 # characters, and writes everything outside ASCII as a character reference,
563 # so the document is plain ASCII whatever the input was.
564 82888 ▶sub _text {
Subroutine _text: called CC=3 SCAR=11.1
565 82888 my ( $self, $value ) = @_;
566 82888 ▶ return '' if !defined $value || ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 0 82888 82888
Condition33%1/3
not defined $value or ref $value
l!l&&r!l&&!r
0082888
MC/DC0%0/2
not defined $value or ref $value
not defined $value ref $value
Truth table33%1/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
567 82888 $value = ''.$value;
568 82888 $value =~ s/[^\x09\x0A\x0D\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//g;
569 82888 $value =~ s/&/&amp;/g;
570 82888 $value =~ s/</&lt;/g;
571 82888 $value =~ s/>/&gt;/g;
572 82888 $value =~ s/"/&quot;/g;
573 82888 $value =~ s/'/&#39;/g;
574 82888 $value =~ s/([\x0D\x7F-\x{10FFFF}])/'&#'.ord($1).';'/ge;
575 82888 return $value;
576 }
577  
578 # Attribute value: as text, and tab and newline as references too, so they
579 # survive attribute value normalisation.
580 67256 ▶sub _attr {
Subroutine _attr: called CC=1 SCAR=0.1
581 67256 my ( $self, $value ) = @_;
582 67256 $value = $self->_text($value);
583 0 $value =~ s/([\x09\x0A])/'&#'.ord($1).';'/ge;
584 67256 return $value;
585 }
586  
587 # One element. Attributes are name/value pairs in the order given, every
588 # value escaped here; $content is markup already and undef closes the tag.
589 28650 ▶sub _el {
Subroutine _el: called CC=3 SCAR=11.0
590 28650 my ( $self, $name, $attrs, $content ) = @_;
591 28650 my @pairs = @$attrs;
592 28650 my $tag = '<'.$name;
593 28650 ▶ while (@pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs) 67256 28650 95906
594 67256 my ( $key, $value ) = splice @pairs, 0, 2;
595 67256 $tag .= ' '.$key.'="'.$self->_attr($value).'"';
596 }
597 28650 ▶ return defined $content ? $tag.'>'.$content.'</'.$name.'>' : $tag.'/>';
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 22719 5931 28650
598 }
599  
600 # Two decimals, as a number: the same on every platform, and no '-0'.
601 84589 ▶sub _n {
Subroutine _n: called CC=1 SCAR=0.0
602 84589 my ( $self, $value ) = @_;
603 84589 return sprintf( '%.2f', $value ) + 0;
604 }
605  
606 #### Render
607  
608 422 ▶sub render {
Subroutine render: called CC=9 SCAR=22.0
Pod documented
609 422 my ( $self ) = @_;
610 422 my $layout = $self->_layouter->layout;
611 419 my $pad = $self->_pad;
612 419 my %cell = map { $_->id => $_ } @{ $self->cells };
613 419 my @placed = grep { $cell{ $_->{id} } } @{ $layout->{cells} };
614  
615 419 my ( %count, @occurring );
616 419 $count{ $cell{ $_->{id} }->phase }++ for @placed;
617 419 @occurring = grep { $count{$_} } $self->phases;
618  
619 419 my ( $content, $legend_rows ) = $self->_content( $layout->{width}, \%count );
620 419 my @legend_rows = @$legend_rows;
621  
622 419 my $ox = $pad;
623 419 my $oy = $pad + $self->_title_band;
624 419 my $bottom = $oy + $layout->{height};
625 419 my $legend_top = $bottom + $self->_legend_gap;
626 419 ▶ $bottom = $legend_top + @legend_rows * $self->_legend_row - $self->_legend_row / 4
Branch: 1 branch
BranchTrueFalseTotal
if @legend_rows 409 10 419
627 if @legend_rows;
628 419 my $width = $content + 2 * $pad;
629 419 my $height = $bottom + $pad;
630  
631 my $summary = ( @placed == 1 ? '1 Comb' : scalar(@placed).' Combs' )
632 419 ▶ .( @occurring ? ': '.join( ', ', map { $count{$_}.' '.$_ } @occurring ) : '' );
Branch: 2 branches
BranchTrueFalseTotal
@placed == 1 ? : 37 382 419
@occurring ? : 414 5 419
633  
634 419 my %at = map { $_->{id} => [ $_->{x} + $ox, $_->{y} + $oy ] } @placed;
635  
636 my @out = (
637 $self->_el( 'title', [ id => 'comb-title' ], $self->_text( $self->title ) ),
638 $self->_el( 'desc', [ id => 'comb-desc' ], $self->_text($summary) ),
639 $self->_style(
640 3115 scalar( grep { $self->_name_lines( $cell{ $_->{id} }->name )->{small} } @placed ),
641 419 scalar( grep { $self->_shows_reason( $cell{ $_->{id} } ) } @placed )
642 ),
643 $self->_el( 'defs', [], $self->_marker ),
644 $self->_el( 'rect', [
645 class => 'panel',
646 x => 0.5,
647 y => 0.5,
648 width => $self->_n( $width - 1 ),
649 height => $self->_n( $height - 1 ),
650 rx => $self->_n( $self->size * 0.22 )
651 ] ),
652 $self->_el( 'text', [
653 class => 'heading',
654 x => $self->_n($ox),
655 y => $self->_n( $pad + $self->_title_font * 0.8 )
656 ], $self->_text( $self->title ) )
657 );
658  
659 92 push @out, map { $self->_group( $_, $ox, $oy, $content ) }
660 419 grep { $_->{heading} } @{ $layout->{groups} };
661  
662 419 ▶ if ( $self->edges ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->edges) 302 117 419
663 142 my @paths = map { $self->_edge( $_, $at{ $_->{from} }, $at{ $_->{to} } ) }
664 302 ▶ grep { $at{ $_->{from} } && $at{ $_->{to} } } @{ $layout->{edges} };
Condition33%1/3
$at{$_->{'from'}} && $at{$_->{'to'}}
!ll&&!rl&&r
00142
MC/DC0%0/2
$at{$_->{'from'}} && $at{$_->{'to'}}
$at{$_->{'from'}} $at{$_->{'to'}}
Truth table33%1/3
A && B
A= $at{$_->{'from'}}B= $at{$_->{'to'}}
ABresult
0X0
100
111
665 302 ▶ push @out, $self->_el( 'g', [ class => 'deps' ], join( '', @paths ) ) if @paths;
Branch: 1 branch
BranchTrueFalseTotal
if @paths 56 246 302
666 }
667  
668 419 push @out, map { $self->_comb( $cell{ $_->{id} }, @{ $at{ $_->{id} } } ) } @placed;
669  
670 419 ▶ push @out, $self->_legend( \@legend_rows, $ox, $legend_top ) if @legend_rows;
Branch: 1 branch
BranchTrueFalseTotal
if @legend_rows 409 10 419
671  
672 419 return $self->_el( 'svg', [
673 xmlns => 'http://www.w3.org/2000/svg',
674 viewBox => '0 0 '.$self->_n($width).' '.$self->_n($height),
675 role => 'img',
676 class => 'comb-svg',
677 'aria-labelledby' => 'comb-title comb-desc'
678 ], "\n".join( "\n", @out )."\n" )."\n";
679 }
680  
681  
682 # The width of the content and the rows of the legend, for a honeycomb that
683 # is $layout_width wide: the title, the legend items and the group headings
684 # can be wider than it, and the legend wraps into the width that results.
685 766 ▶sub _content {
Subroutine _content: called CC=4 SCAR=13.9
686 766 my ( $self, $layout_width, $count ) = @_;
687 my @legend = $self->legend
688 2527 ? map { { phase => $_, count => $count->{$_}, width => $self->_legend_item_width( $_, $count->{$_} ) } }
689 766 ▶ grep { $count->{$_} } $self->phases
Branch: 1 branch
BranchTrueFalseTotal
$self->legend ? : 761 5 766
690 : ();
691 766 my $content = $layout_width;
692 766 for my $width (
693 $self->_text_width( $self->title, $self->_title_font ),
694 2527 ( map { $_->{width} } @legend ),
695 66 ( map { $self->_text_width( $_, $self->_group_font ) } @{ $self->_facts->{groups} } )
696 ) {
697 3359 ▶ $content = $width if $width > $content;
Branch: 1 branch
BranchTrueFalseTotal
if $width > $content 52 3307 3359
698 }
699 766 return ( $content, [ $self->_legend_rows( \@legend, $content ) ] );
700 }
701  
702 # The size of the whole picture around a honeycomb of this size: what the
703 # layout class needs to meet `aspect` with the title and the wrapped legend.
704 347 ▶sub _frame {
Subroutine _frame: called CC=2 SCAR=7.1
705 347 my ( $self, $width, $height ) = @_;
706 347 my ( $content, $rows ) = $self->_content( $width, $self->_facts->{count} );
707 return (
708 347 ▶ $content + 2 * $self->_pad,
Branch: 1 branch
BranchTrueFalseTotal
@$rows ? : 347 0 347
709 2 * $self->_pad + $self->_title_band + $height
710 + ( @$rows ? $self->_legend_gap + @$rows * $self->_legend_row - $self->_legend_row / 4 : 0 )
711 );
712 }
713  
714 # What the frame depends on and the columns do not: the phase counts and the
715 # group names of the cells that are drawn.
716 99 has _facts => ( is => 'lazy', isa => HashRef, init_arg => undef );
717  
718 418 ▶sub _build__facts {
Subroutine _build__facts: called CC=4 SCAR=14.0
719 418 my ( $self ) = @_;
720 418 my ( %seen, %count, %group );
721 418 ▶ for my $cell ( grep { defined $_->id && !$seen{ $_->id }++ } @{ $self->cells } ) {
Condition33%1/3
defined $_->id && !$seen{$_->id}++
!ll&&!rl&&r
003113
MC/DC0%0/2
defined $_->id && !$seen{$_->id}++
defined $_->id !$seen{$_->id}++
Truth table33%1/3
A && not(B)
A= defined $_->idB= !$seen{$_->id}++
ABresult
0X0
100
111
722 3113 $count{ $cell->phase }++;
723 3113 ▶ $group{ $cell->group } = 1 if defined $cell->group;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell->group 112 3001 3113
724 }
725 418 return { count => \%count, groups => [ sort keys %group ] };
726 }
727  
728 419 ▶sub _marker {
Subroutine _marker: called CC=1 SCAR=0.0
729 419 my ( $self ) = @_;
730 419 my $arrow = $self->_n( $self->_arrow );
731 419 return $self->_el( 'marker', [
732 id => 'comb-arrow',
733 viewBox => '0 0 10 10',
734 refX => 0,
735 refY => 5,
736 markerWidth => $arrow,
737 markerHeight => $arrow,
738 markerUnits => 'userSpaceOnUse',
739 orient => 'auto'
740 ], $self->_el( 'path', [ class => 'arrow', d => 'M0 1L10 5L0 9z' ] ) );
741 }
742  
743 # The heading of a group: its name and a faint rule to the right of it. The
744 # unnamed group among named ones gets the rule alone.
745 92 ▶sub _group {
Subroutine _group: called CC=4 SCAR=13.9
746 92 my ( $self, $group, $ox, $oy, $content ) = @_;
747 92 my $x = $group->{heading}{x} + $ox;
748 92 my $y = $group->{heading}{y} + $oy;
749 92 my $font = $self->_group_font;
750 92 my ( $text, $from ) = ( '', $x );
751 92 ▶ if ( defined $group->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group->{"name"}) 62 30 92
752 $text = $self->_el( 'text', [
753 class => 'group-name',
754 x => $self->_n($x),
755 y => $self->_n($y)
756 62 ], $self->_text( $group->{name} ) );
757 62 $from = $x + $self->_text_width( $group->{name}, $font ) + $font * 0.8;
758 }
759 92 my $to = $ox + $content;
760 92 ▶ my $rule = $to - $from > $font
Branch: 1 branch
BranchTrueFalseTotal
$to - $from > $font ? : 91 1 92
761 ? $self->_el( 'line', [
762 class => 'group-rule',
763 x1 => $self->_n($from),
764 y1 => $self->_n( $y - $font * 0.35 ),
765 x2 => $self->_n($to),
766 y2 => $self->_n( $y - $font * 0.35 )
767 ] )
768 : '';
769 return $self->_el( 'g', [
770 class => 'group',
771 92 ▶ defined $group->{name} ? ( 'data-group' => $group->{name} ) : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $group->{'name'} ? : 62 30 92
772 ], $text.$rule );
773 }
774  
775 4718 ▶sub _hexagon {
Subroutine _hexagon: called CC=1 SCAR=0.0
776 4718 my ( $self, $x, $y, $radius ) = @_;
777 4718 my $half = $radius * sqrt(3) / 2;
778 4718 my @points = (
779 [ $x, $y - $radius ],
780 [ $x + $half, $y - $radius / 2 ],
781 [ $x + $half, $y + $radius / 2 ],
782 [ $x, $y + $radius ],
783 [ $x - $half, $y + $radius / 2 ],
784 [ $x - $half, $y - $radius / 2 ]
785 );
786 return $self->_el( 'polygon', [
787 class => 'hex',
788 4718 points => join( ' ', map { $self->_n( $_->[0] ).','.$self->_n( $_->[1] ) } @points )
789 ] );
790 }
791  
792 3115 ▶sub _tooltip {
Subroutine _tooltip: called CC=15 SCAR=27.1
793 3115 my ( $self, $cell ) = @_;
794 3115 my @lines = ( $cell->id );
795 3115 ▶ push @lines, 'namespace: '.$cell->namespace if defined $cell->namespace;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell->namespace 540 2575 3115
796 3115 ▶ push @lines, 'class: '.$cell->class if defined $cell->class;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell->class 40 3075 3115
797 3115 ▶ push @lines, 'phase: '.$cell->phase
Branch: 1 branch
BranchTrueFalseTotal
$cell->phase eq 'Unknown' && defined $cell->raw_phase ? : 49 3066 3115
Condition100%3/3
$cell->phase eq "Unknown" and defined $cell->raw_phase
!ll&&!rl&&r
2053101349
MC/DC100%2/2
$cell->phase eq "Unknown" and defined $cell->raw_phase
$cell->phase eq "Unknown" defined $cell->raw_phase
Truth table100%3/3
A and B
A= $cell->phase eq "Unknown"B= defined $cell->raw_phase
ABresult
0X0
100
111
798 .( $cell->phase eq 'Unknown' && defined $cell->raw_phase ? ' ('.$cell->raw_phase.')' : '' );
799 3115 ▶ push @lines, 'message: '.$cell->message
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell->message and $cell->phase ne "Running" 25 3090 3115
Condition66%2/3
defined $cell->message and $cell->phase ne "Running"
!ll&&!rl&&r
3090025
MC/DC50%1/2
defined $cell->message and $cell->phase ne "Running"
defined $cell->message $cell->phase ne "Running"
Truth table66%2/3
A and B
A= defined $cell->messageB= $cell->phase ne "Running"
ABresult
0X0
100
111
800 if defined $cell->message && $cell->phase ne 'Running';
801 40 ▶ push @lines, map { 'endpoint: '.$_->{name}.( defined $_->{port} ? ' '.$_->{port} : '' ) }
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'port'} ? : 34 6 40
802 3115 @{ $cell->endpoints };
803 3115 ▶ if ( $cell->upstream_recorded ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell->upstream_recorded) 291 2824 3115
804 291 ▶ my @upstream = (
Branch: 2 branches
BranchTrueFalseTotal
defined $cell->upstream_class ? : 14 277 291
defined $cell->upstream_context ? : 285 6 291
805 defined $cell->upstream_class ? $cell->upstream_class : (),
806 defined $cell->upstream_context ? 'context '.$cell->upstream_context : ()
807 );
808 291 ▶ push @lines, 'upstream: '.( @upstream ? join( ', ', @upstream ) : 'recorded' )
Branch: 2 branches
BranchTrueFalseTotal
@upstream ? : 287 4 291
$cell->borrowed ? : 278 13 291
809 .( $cell->borrowed ? '' : ' (not borrowing)' );
810 291 ▶ push @lines, 'via: '.join( ', ', @{ $cell->upstream_via } ) if @{ $cell->upstream_via };
Branch: 1 branch
BranchTrueFalseTotal
if @{$cell->upstream_via;} 14 277 291
811 }
812 3115 ▶ push @lines, 'missing: '.join( ', ', @{ $cell->missing } ) if @{ $cell->missing };
Branch: 1 branch
BranchTrueFalseTotal
if @{$cell->missing;} 7 3108 3115
813 3115 return join( "\n", @lines );
814 }
815  
816 # The href for a cell, or undef: what the link callback answers, when it is
817 # relative or http(s) and carries neither whitespace nor control characters.
818 3115 ▶sub _href {
Subroutine _href: called CC=9 SCAR=22.0
819 3115 my ( $self, $cell ) = @_;
820 3115 ▶ return unless $self->link;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->link 3079 36 3115
821 36 my $href = eval { $self->link->($cell) };
822 36 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 1 35 36
823 1 carp __PACKAGE__.'->render: link callback died for '.$cell->id.': '.$error;
824 1 return;
825 }
826 35 ▶ return if !defined $href || ref $href || !length $href;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $href or ref $href or not length $href 5 30 35
Condition83%5/6
not defined $href or ref $href or not length $href
l!l&&r!l&&!r
4130
not defined $href or ref $href
l!l&&r!l&&!r
4031
MC/DC66%2/3
not defined $href or ref $href or not length $href
not defined $href ref $href not length $href
Truth table75%3/4
not(A) or B or not(C)
A= not defined $hrefB= ref $hrefC= not length $href
ABCresult
1XX1
01X1
0011
0000
827 30 ▶ return if $href =~ /[\x00-\x20\x7F]/;
Branch: 1 branch
BranchTrueFalseTotal
if $href =~ /[\x00-\x20\x7F]/ 6 24 30
828 24 ▶ return $href if $href =~ m{\Ahttps?://}i;
Branch: 1 branch
BranchTrueFalseTotal
if $href =~ m[\Ahttps?://]i 6 18 24
829 18 ▶ return if $href =~ m{\A[^/?#]*:};
Branch: 1 branch
BranchTrueFalseTotal
if $href =~ m[\A[^/?#]*:] 7 11 18
830 11 return $href;
831 }
832  
833 # One text element of a cell. A single row is the text itself; several rows
834 # are one <tspan> each, placed on its own, so the text content of the element
835 # is still the whole of what is shown.
836 7035 ▶sub _cell_text {
Subroutine _cell_text: called CC=2 SCAR=6.9
837 7035 my ( $self, $line, $x, $y ) = @_;
838 7573 my @rows = map { [ $self->_text( $_->[0] ), x => $self->_n($x), y => $self->_n( $y + $_->[1] ) ] }
839 7035 @{ $line->{rows} };
840 7035 ▶ return $self->_el( 'text', [ class => $line->{class}, @{ $rows[0] }[ 1 .. 4 ] ], $rows[0][0] )
Branch: 1 branch
BranchTrueFalseTotal
if @rows == 1 6497 538 7035
841 if @rows == 1;
842 return $self->_el( 'text', [ class => $line->{class} ],
843 538 join( '', map { $self->_el( 'tspan', [ @{$_}[ 1 .. 4 ] ], $_->[0] ) } @rows ) );
844 }
845  
846 3115 ▶sub _comb {
Subroutine _comb: called CC=5 SCAR=16.1
847 3115 my ( $self, $cell, $x, $y ) = @_;
848 3115 my $r = $self->size;
849 3115 my $borrowed = $cell->borrowed;
850 3115 my $disabled = $cell->phase eq 'Disabled';
851  
852 my @parts = (
853 $self->_el( 'title', [], $self->_text( $self->_tooltip($cell) ) ),
854 $self->_hexagon( $x, $y, $r ),
855 3115 map { $self->_cell_text( $_, $x, $y ) } $self->_cell_lines($cell)
856 );
857  
858 3115 ▶ my $group = $self->_el( 'g', [
Branch: 3 branches
BranchTrueFalseTotal
$borrowed ? : 278 2837 3115
$disabled ? : 161 2954 3115
defined $cell->group ? : 112 3003 3115
859 class => join( ' ', 'comb', 'phase-'.$cell->phase,
860 $borrowed ? 'borrowed' : (), $disabled ? 'disabled' : () ),
861 'data-name' => $cell->name,
862 'data-id' => $cell->id,
863 'data-phase' => $cell->phase,
864 defined $cell->group ? ( 'data-comb-group' => $cell->group ) : ()
865 ], join( '', @parts ) );
866  
867 3115 my $href = $self->_href($cell);
868 3115 ▶ return defined $href ? $self->_el( 'a', [ href => $href ], $group ) : $group;
Branch: 1 branch
BranchTrueFalseTotal
defined $href ? : 17 3098 3115
869 }
870  
871 # One dependency, dependent to dependency, drawn between the label-free
872 # corner regions of its two ends: the name, phase and upstream lines fill the
873 # middle band of a hexagon, the regions towards its top and bottom corner are
874 # free. The line starts at a dot just inside the outline of the dependent and
875 # its arrowhead ends inside the dependency.
876 #
877 # Across rows it is a straight line from the corner region facing the
878 # dependency into the corner region facing the dependent. Inside one row it
879 # is a bow through the corner regions of the cells it passes: above the row
880 # when it runs left to right, below when it runs right to left, so the two
881 # edges of a cycle never share a line.
882 142 ▶sub _edge {
Subroutine _edge: called CC=10 SCAR=23.1
883 142 my ( $self, $edge, $from, $to ) = @_;
884 142 my $r = $self->size;
885 142 my ( $dx, $dy ) = ( $to->[0] - $from->[0], $to->[1] - $from->[1] );
886 142 ▶ return () if abs($dx) < 0.01 && abs($dy) < 0.01;
Branch: 1 branch
BranchTrueFalseTotal
if abs $dx < "0.01" and abs $dy < "0.01" 0 142 142
Condition66%2/3
abs $dx < "0.01" and abs $dy < "0.01"
!ll&&!rl&&r
130120
MC/DC0%0/2
abs $dx < "0.01" and abs $dy < "0.01"
abs $dx < "0.01" abs $dy < "0.01"
Truth table66%2/3
A and B
A= abs $dx < "0.01"B= abs $dy < "0.01"
ABresult
0X0
100
111
887  
888 142 my ( @a, @b, @via );
889 142 ▶ if ( abs($dy) < 0.01 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $dy < 0.01) { } 24 118 142
890 24 ▶ my $along = $dx > 0 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$dx > 0 ? : 13 11 24
891 24 my $side = -$along;
892 24 my $steps = abs($dx) / $self->_layouter->step_x;
893 24 ▶ $steps = 3 if $steps > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $steps > 3 1 23 24
894 24 @a = ( $from->[0] + $along * $r * 0.3, $from->[1] + $side * $r * 0.62 );
895 24 @b = ( $to->[0] - $along * $r * 0.35, $to->[1] + $side * $r * 0.55 );
896 24 @via = ( ( $a[0] + $b[0] ) / 2, $from->[1] + $side * $r * ( 0.9 + 0.15 * $steps ) );
897 }
898 else {
899 118 ▶ my $side = $dy > 0 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$dy > 0 ? : 0 118 118
900 118 @a = ( $from->[0] + $self->_clamp( $dx * 0.3, $r * 0.2 ), $from->[1] + $side * $r * 0.62 );
901 118 @b = ( $to->[0] - $self->_clamp( $dx * 0.3, $r * 0.3 ), $to->[1] - $side * $r * 0.6 );
902 }
903  
904 # @b is the tip of the arrowhead; the marker draws it beyond the end of the
905 # path, so the path stops one arrowhead short, along its last direction.
906 142 ▶ my @last = @via ? @via : @a;
Branch: 1 branch
BranchTrueFalseTotal
@via ? : 24 118 142
907 142 my $angle = atan2( $b[1] - $last[1], $b[0] - $last[0] );
908 142 my @end = ( $b[0] - cos($angle) * $self->_arrow, $b[1] - sin($angle) * $self->_arrow );
909  
910 # @a is the centre of the start dot; the line leaves it at its rim, so the
911 # two translucent shapes do not overlap.
912 142 ▶ my @first = @via ? @via : @end;
Branch: 1 branch
BranchTrueFalseTotal
@via ? : 24 118 142
913 142 my $out = atan2( $first[1] - $a[1], $first[0] - $a[0] );
914 142 my @start = ( $a[0] + cos($out) * $self->_start_dot, $a[1] + sin($out) * $self->_start_dot );
915  
916 142 ▶ my $d = 'M'.$self->_n( $start[0] ).' '.$self->_n( $start[1] )
Branch: 1 branch
BranchTrueFalseTotal
@via ? : 24 118 142
917 .( @via ? 'Q'.$self->_n( $via[0] ).' '.$self->_n( $via[1] ).' ' : 'L' )
918 .$self->_n( $end[0] ).' '.$self->_n( $end[1] );
919 return $self->_el( 'path', [
920 class => 'dep',
921 'data-from' => $edge->{from},
922 'data-to' => $edge->{to},
923 142 d => $d,
924 'marker-end' => 'url(#comb-arrow)'
925 ] ).$self->_el( 'circle', [
926 class => 'dep-start',
927 cx => $self->_n( $a[0] ),
928 cy => $self->_n( $a[1] ),
929 r => $self->_n( $self->_start_dot )
930 ] );
931 }
932  
933 236 ▶sub _clamp {
Subroutine _clamp: called CC=3 SCAR=11.0
934 236 my ( $self, $value, $limit ) = @_;
935 236 ▶ return $value > $limit ? $limit : $value < -$limit ? -$limit : $value;
Branch: 2 branches
BranchTrueFalseTotal
$value > $limit ? : 61 175 236
$value < -$limit ? : 61 114 175
936 }
937  
938 #### Legend
939  
940 2527 ▶sub _legend_item_width {
Subroutine _legend_item_width: called CC=1 SCAR=0.0
941 2527 my ( $self, $phase, $count ) = @_;
942 2527 return $self->_legend_swatch * 2.6
943 + $self->_text_width( $phase.' '.$count, $self->_legend_font );
944 }
945  
946 # Fills rows of at most $width, at least one item each.
947 766 ▶sub _legend_rows {
Subroutine _legend_rows: called CC=4 SCAR=13.9
948 766 my ( $self, $items, $width ) = @_;
949 766 my $gap = $self->size * 0.35;
950 766 my ( @rows, $used );
951 766 for my $item (@$items) {
952 2527 ▶ if ( !@rows || $used + $gap + $item->{width} > $width ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @rows or $used + $gap + $item->{"width"} > $width) 956 1571 2527
Condition100%3/3
not @rows or $used + $gap + $item->{"width"} > $width
l!l&&r!l&&!r
7562001571
MC/DC100%2/2
not @rows or $used + $gap + $item->{"width"} > $width
not @rows $used + $gap + $item->{"width"} > $width
Truth table100%3/3
not(A) or B
A= not @rowsB= $used + $gap + $item->{"width"} > $width
ABresult
1X1
011
000
953 956 push @rows, [];
954 956 $used = -$gap;
955 }
956 2527 $item->{x} = $used + $gap;
957 2527 $used = $item->{x} + $item->{width};
958 2527 push @{ $rows[-1] }, $item;
959 }
960 766 return @rows;
961 }
962  
963 409 ▶sub _legend {
Subroutine _legend: called CC=3 SCAR=11.0
964 409 my ( $self, $rows, $ox, $top ) = @_;
965 409 my $swatch = $self->_legend_swatch;
966 409 my @items;
967 409 for my $row ( 0 .. $#$rows ) {
968 559 my $y = $top + $row * $self->_legend_row + $swatch;
969 559 for my $item ( @{ $rows->[$row] } ) {
970 1603 my $x = $ox + $item->{x} + $swatch;
971 push @items, $self->_el( 'g', [
972 class => 'legend-item phase-'.$item->{phase},
973 'data-phase' => $item->{phase},
974 'data-count' => $item->{count}
975 ], $self->_hexagon( $x, $y, $swatch )
976 .$self->_el( 'text', [
977 x => $self->_n( $x + $swatch * 1.6 ),
978 y => $self->_n( $y + $self->_legend_font * 0.35 )
979 ], $self->_text( $item->{phase} ).' '
980 1603 .$self->_el( 'tspan', [ class => 'count' ], $self->_text( $item->{count} ) ) ) );
981 }
982 }
983 409 return $self->_el( 'g', [ class => 'legend' ], join( '', @items ) );
984 }
985  
986 99 1;
987  
988 __END__
« blib/lib/Kubernetes/Comb/SVG/Layout.pm ↑ Summary blib/lib/Kubernetes/Comb/SVG/Cell.pm »