tools/lib/Term/Fabulous/Screenshot/BoxDrawing.pm

filter: Statementstmt 40.2% 70 / 174 Branchbran 19.8% 21 / 106 Conditioncond 45.7% 16 / 35 MC/DCmcdc 38.1% 8 / 21 Subroutinesub 65.3% 17 / 26 Podpod 100% 2 / 2 totaltotal 36.8% 134 / 364 CC 71 SCAR 52.2
CC
71
Cov
34%
_line_shapes
650.0
_rail_extent
56.0
_arc_shapes
42.0
CRAP 184.3
↑ Summary blib/lib/Term/Fabulous/Render/Border.pm »
1 package Term::Fabulous::Screenshot::BoxDrawing;
2  
3 2 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our @EXPORT_OK = qw(is_drawn_glyph glyph_shapes);
13  
14 # Terminals draw the box drawing (U+2500-U+257F) and block element
15 # (U+2580-U+259F) characters themselves instead of taking them from the
16 # font, so lines meet across cells without gaps and half blocks split a
17 # cell exactly; many draw the Braille patterns (U+2800-U+28FF) and the
18 # sextants (U+1FB00-U+1FB3B) too. These shapes do the same for the
19 # screenshot renderers.
20 #
21 # Shapes are in the coordinates of one cell, (0, 0) at its top-left
22 # corner, in whole units of the renderer (pixels for PNG, user units for
23 # SVG): rectangles, filled with the cell's foreground color mixed over its
24 # background (coverage 1 is the foreground itself), polylines stroked in
25 # the foreground color, and circles (Braille dots) filled with it.
26  
27 2 ▶use constant { NONE => 0, LIGHT => 1, HEAVY => 2, DOUBLE => 3 };
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # The four arms of each line character: up, right, down, left.
30 2 my %ARMS_BY_CODEPOINT = (
31 0x2500 => [ 0, 1, 0, 1 ], 0x2501 => [ 0, 2, 0, 2 ], 0x2502 => [ 1, 0, 1, 0 ], 0x2503 => [ 2, 0, 2, 0 ],
32 0x250C => [ 0, 1, 1, 0 ], 0x250D => [ 0, 2, 1, 0 ], 0x250E => [ 0, 1, 2, 0 ], 0x250F => [ 0, 2, 2, 0 ],
33 0x2510 => [ 0, 0, 1, 1 ], 0x2511 => [ 0, 0, 1, 2 ], 0x2512 => [ 0, 0, 2, 1 ], 0x2513 => [ 0, 0, 2, 2 ],
34 0x2514 => [ 1, 1, 0, 0 ], 0x2515 => [ 1, 2, 0, 0 ], 0x2516 => [ 2, 1, 0, 0 ], 0x2517 => [ 2, 2, 0, 0 ],
35 0x2518 => [ 1, 0, 0, 1 ], 0x2519 => [ 1, 0, 0, 2 ], 0x251A => [ 2, 0, 0, 1 ], 0x251B => [ 2, 0, 0, 2 ],
36 0x251C => [ 1, 1, 1, 0 ], 0x251D => [ 1, 2, 1, 0 ], 0x251E => [ 2, 1, 1, 0 ], 0x251F => [ 1, 1, 2, 0 ],
37 0x2520 => [ 2, 1, 2, 0 ], 0x2521 => [ 2, 2, 1, 0 ], 0x2522 => [ 1, 2, 2, 0 ], 0x2523 => [ 2, 2, 2, 0 ],
38 0x2524 => [ 1, 0, 1, 1 ], 0x2525 => [ 1, 0, 1, 2 ], 0x2526 => [ 2, 0, 1, 1 ], 0x2527 => [ 1, 0, 2, 1 ],
39 0x2528 => [ 2, 0, 2, 1 ], 0x2529 => [ 2, 0, 1, 2 ], 0x252A => [ 1, 0, 2, 2 ], 0x252B => [ 2, 0, 2, 2 ],
40 0x252C => [ 0, 1, 1, 1 ], 0x252D => [ 0, 1, 1, 2 ], 0x252E => [ 0, 2, 1, 1 ], 0x252F => [ 0, 2, 1, 2 ],
41 0x2530 => [ 0, 1, 2, 1 ], 0x2531 => [ 0, 1, 2, 2 ], 0x2532 => [ 0, 2, 2, 1 ], 0x2533 => [ 0, 2, 2, 2 ],
42 0x2534 => [ 1, 1, 0, 1 ], 0x2535 => [ 1, 1, 0, 2 ], 0x2536 => [ 1, 2, 0, 1 ], 0x2537 => [ 1, 2, 0, 2 ],
43 0x2538 => [ 2, 1, 0, 1 ], 0x2539 => [ 2, 1, 0, 2 ], 0x253A => [ 2, 2, 0, 1 ], 0x253B => [ 2, 2, 0, 2 ],
44 0x253C => [ 1, 1, 1, 1 ], 0x253D => [ 1, 1, 1, 2 ], 0x253E => [ 1, 2, 1, 1 ], 0x253F => [ 1, 2, 1, 2 ],
45 0x2540 => [ 2, 1, 1, 1 ], 0x2541 => [ 1, 1, 2, 1 ], 0x2542 => [ 2, 1, 2, 1 ], 0x2543 => [ 2, 1, 1, 2 ],
46 0x2544 => [ 2, 2, 1, 1 ], 0x2545 => [ 1, 1, 2, 2 ], 0x2546 => [ 1, 2, 2, 1 ], 0x2547 => [ 2, 2, 1, 2 ],
47 0x2548 => [ 1, 2, 2, 2 ], 0x2549 => [ 2, 1, 2, 2 ], 0x254A => [ 2, 2, 2, 1 ], 0x254B => [ 2, 2, 2, 2 ],
48 0x2550 => [ 0, 3, 0, 3 ], 0x2551 => [ 3, 0, 3, 0 ], 0x2552 => [ 0, 3, 1, 0 ], 0x2553 => [ 0, 1, 3, 0 ],
49 0x2554 => [ 0, 3, 3, 0 ], 0x2555 => [ 0, 0, 1, 3 ], 0x2556 => [ 0, 0, 3, 1 ], 0x2557 => [ 0, 0, 3, 3 ],
50 0x2558 => [ 1, 3, 0, 0 ], 0x2559 => [ 3, 1, 0, 0 ], 0x255A => [ 3, 3, 0, 0 ], 0x255B => [ 1, 0, 0, 3 ],
51 0x255C => [ 3, 0, 0, 1 ], 0x255D => [ 3, 0, 0, 3 ], 0x255E => [ 1, 3, 1, 0 ], 0x255F => [ 3, 1, 3, 0 ],
52 0x2560 => [ 3, 3, 3, 0 ], 0x2561 => [ 1, 0, 1, 3 ], 0x2562 => [ 3, 0, 3, 1 ], 0x2563 => [ 3, 0, 3, 3 ],
53 0x2564 => [ 0, 3, 1, 3 ], 0x2565 => [ 0, 1, 3, 1 ], 0x2566 => [ 0, 3, 3, 3 ], 0x2567 => [ 1, 3, 0, 3 ],
54 0x2568 => [ 3, 1, 0, 1 ], 0x2569 => [ 3, 3, 0, 3 ], 0x256A => [ 1, 3, 1, 3 ], 0x256B => [ 3, 1, 3, 1 ],
55 0x256C => [ 3, 3, 3, 3 ],
56 0x2574 => [ 0, 0, 0, 1 ], 0x2575 => [ 1, 0, 0, 0 ], 0x2576 => [ 0, 1, 0, 0 ], 0x2577 => [ 0, 0, 1, 0 ],
57 0x2578 => [ 0, 0, 0, 2 ], 0x2579 => [ 2, 0, 0, 0 ], 0x257A => [ 0, 2, 0, 0 ], 0x257B => [ 0, 0, 2, 0 ],
58 0x257C => [ 0, 2, 0, 1 ], 0x257D => [ 1, 0, 2, 0 ], 0x257E => [ 0, 1, 0, 2 ], 0x257F => [ 2, 0, 1, 0 ],
59 );
60  
61 # Dashed lines: [ horizontal (1) or vertical (0), weight, dashes ].
62 2 my %DASHES_BY_CODEPOINT = (
63 0x2504 => [ 1, LIGHT, 3 ], 0x2505 => [ 1, HEAVY, 3 ], 0x2506 => [ 0, LIGHT, 3 ], 0x2507 => [ 0, HEAVY, 3 ],
64 0x2508 => [ 1, LIGHT, 4 ], 0x2509 => [ 1, HEAVY, 4 ], 0x250A => [ 0, LIGHT, 4 ], 0x250B => [ 0, HEAVY, 4 ],
65 0x254C => [ 1, LIGHT, 2 ], 0x254D => [ 1, HEAVY, 2 ], 0x254E => [ 0, LIGHT, 2 ], 0x254F => [ 0, HEAVY, 2 ],
66 );
67  
68 # Rounded corners: the two arms each one connects, [ vertical, horizontal ].
69 2 my %ARC_BY_CODEPOINT = (
70 0x256D => [ 'down', 'right' ],
71 0x256E => [ 'down', 'left' ],
72 0x256F => [ 'up', 'left' ],
73 0x2570 => [ 'up', 'right' ],
74 );
75  
76 # Block elements as rectangles in eighths of the cell: [ left, top, right,
77 # bottom ], plus the coverage of the foreground (1 for solid, less for the
78 # shades). Quadrants split the cell at its middle.
79 my %BLOCKS_BY_CODEPOINT = (
80 0x2580 => [ [ 0, 0, 8, 4 ] ],
81 16 ( map { ( 0x2580 + $_ => [ [ 0, 8 - $_, 8, 8 ] ] ) } 1 .. 8 ), # lower one eighth .. full block
82 2 ( map { ( 0x2588 + $_ => [ [ 0, 0, 8 - $_, 8 ] ] ) } 1 .. 7 ), # left seven eighths .. left one eighth
83 0x2590 => [ [ 4, 0, 8, 8 ] ],
84 0x2591 => [ [ 0, 0, 8, 8, 0.25 ] ],
85 0x2592 => [ [ 0, 0, 8, 8, 0.5 ] ],
86 0x2593 => [ [ 0, 0, 8, 8, 0.75 ] ],
87 0x2594 => [ [ 0, 0, 8, 1 ] ],
88 0x2595 => [ [ 7, 0, 8, 8 ] ],
89 0x2596 => [ [ 0, 4, 4, 8 ] ],
90 0x2597 => [ [ 4, 4, 8, 8 ] ],
91 0x2598 => [ [ 0, 0, 4, 4 ] ],
92 0x2599 => [ [ 0, 0, 4, 4 ], [ 0, 4, 8, 8 ] ],
93 0x259A => [ [ 0, 0, 4, 4 ], [ 4, 4, 8, 8 ] ],
94 0x259B => [ [ 0, 0, 8, 4 ], [ 0, 4, 4, 8 ] ],
95 0x259C => [ [ 0, 0, 8, 4 ], [ 4, 4, 8, 8 ] ],
96 0x259D => [ [ 4, 0, 8, 4 ] ],
97 0x259E => [ [ 4, 0, 8, 4 ], [ 0, 4, 4, 8 ] ],
98 0x259F => [ [ 4, 0, 8, 4 ], [ 0, 4, 8, 8 ] ],
99 );
100  
101 2 ▶use constant ARC_SEGMENTS => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
104 2 BRAILLE_FIRST => 0x2800,
105 BRAILLE_LAST => 0x28FF,
106 SEXTANT_FIRST => 0x1FB00,
107 SEXTANT_LAST => 0x1FB3B,
108 2 };
109  
110 # Braille dot bits by [ column, row ].
111 2 my @BRAILLE_DOTS = ( [ 0, 0, 0x01 ], [ 0, 1, 0x02 ], [ 0, 2, 0x04 ], [ 1, 0, 0x08 ], [ 1, 1, 0x10 ], [ 1, 2, 0x20 ], [ 0, 3, 0x40 ], [ 1, 3, 0x80 ] );
112  
113 24 ▶sub is_drawn_glyph ($glyph) {
Subroutine is_drawn_glyph: called CC=8 SCAR=20.8
Pod documented
114 24 ▶ return 0 unless length($glyph) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $glyph == 1 0 24 24
115 24 my $codepoint = ord $glyph;
116 24 ▶ return 1 if $codepoint >= 0x2500 && $codepoint <= 0x259F;
Branch: 1 branch
BranchTrueFalseTotal
if $codepoint >= 9472 and $codepoint <= 9631 2 22 24
Condition100%3/3
$codepoint >= 9472 and $codepoint <= 9631
!ll&&!rl&&r
2202
MC/DC100%2/2
$codepoint >= 9472 and $codepoint <= 9631
$codepoint >= 9472 $codepoint <= 9631
Truth table100%3/3
A and B
A= $codepoint >= 9472B= $codepoint <= 9631
ABresult
0X0
100
111
117 22 ▶ return 1 if $codepoint >= BRAILLE_FIRST && $codepoint <= BRAILLE_LAST;
Branch: 1 branch
BranchTrueFalseTotal
if $codepoint >= 10240 and $codepoint <= 10495 8 14 22
Condition100%3/3
$codepoint >= 10240 and $codepoint <= 10495
!ll&&!rl&&r
4108
MC/DC100%2/2
$codepoint >= 10240 and $codepoint <= 10495
$codepoint >= 10240 $codepoint <= 10495
Truth table100%3/3
A and B
A= $codepoint >= 10240B= $codepoint <= 10495
ABresult
0X0
100
111
118 14 ▶ return 1 if $codepoint >= SEXTANT_FIRST && $codepoint <= SEXTANT_LAST;
Branch: 1 branch
BranchTrueFalseTotal
if $codepoint >= 129792 and $codepoint <= 129851 8 6 14
Condition100%3/3
$codepoint >= 129792 and $codepoint <= 129851
!ll&&!rl&&r
518
MC/DC100%2/2
$codepoint >= 129792 and $codepoint <= 129851
$codepoint >= 129792 $codepoint <= 129851
Truth table100%3/3
A and B
A= $codepoint >= 129792B= $codepoint <= 129851
ABresult
0X0
100
111
119 6 return 0;
120 }
121  
122 # The dots of a Braille pattern: circles at the centers of a 2 x 4 grid,
123 # as terminals that draw them themselves show them.
124 5 ▶sub _braille_shapes ( $codepoint, $cell ) {
Subroutine _braille_shapes: called CC=1 SCAR=0.0
125 5 my ( $width, $height ) = @$cell{qw(width height)};
126 5 my $bits = $codepoint - BRAILLE_FIRST;
127 5 my $radius = min( $width / 2, $height / 4 ) * 0.4;
128 11 return map { { type => 'circle', cx => $width * ( 1 + 2 * $_->[0] ) / 4, cy => $height * ( 1 + 2 * $_->[1] ) / 8, r => $radius } }
129 5 grep { $bits & $_->[2] } @BRAILLE_DOTS;
130 }
131  
132 # Sextants: two columns and three rows of blocks. The code points count
133 # the masks 1 to 62 without the left (21) and right (42) halves.
134 6 ▶sub _sextant_shapes ( $codepoint, $cell ) {
Subroutine _sextant_shapes: called CC=3 SCAR=11.0
135 6 my ( $width, $height ) = @$cell{qw(width height)};
136 6 my $mask = $codepoint - SEXTANT_FIRST + 1;
137 6 ▶ $mask++ if $mask >= 21;
Branch: 1 branch
BranchTrueFalseTotal
if $mask >= 21 3 3 6
138 6 ▶ $mask++ if $mask >= 42;
Branch: 1 branch
BranchTrueFalseTotal
if $mask >= 42 2 4 6
139 6 my @x = ( 0, int( $width / 2 + 0.5 ), $width );
140 6 my @y = ( 0, int( $height / 3 + 0.5 ), int( 2 * $height / 3 + 0.5 ), $height );
141 6 return map { _rect( $x[ $_ % 2 ], $y[ int( $_ / 2 ) ], $x[ $_ % 2 + 1 ], $y[ int( $_ / 2 ) + 1 ] ) } grep { $mask & ( 1 << $_ ) } 0 .. 5;
142 }
143  
144 # The shapes of $glyph in a cell $width x $height with light lines
145 # $thickness thick, all whole numbers. Dies for a glyph is_drawn_glyph
146 # rejects.
147 13 ▶sub glyph_shapes ( $glyph, $width, $height, $thickness ) {
Subroutine glyph_shapes: called CC=12 SCAR=34.0
Pod documented
148 13 ▶ croak "Term::Fabulous::Screenshot::BoxDrawing: '$glyph' is not a box drawing or block element character" unless is_drawn_glyph($glyph);
Branch: 1 branch
BranchTrueFalseTotal
unless is_drawn_glyph($glyph) 1 12 13
149 12 ▶ croak "Term::Fabulous::Screenshot::BoxDrawing: the cell ($width x $height) and the line thickness ($thickness) must be positive whole numbers"
Branch: 1 branch
BranchTrueFalseTotal
unless _is_positive_integer($width) and _is_positive_integer($height) and _is_positive_integer($thickness) 1 11 12
Condition66%4/6
_is_positive_integer($width) and _is_positive_integer($height) and _is_positive_integer($thickness)
!ll&&!rl&&r
1011
_is_positive_integer($width) and _is_positive_integer($height)
!ll&&!rl&&r
0111
MC/DC33%1/3
_is_positive_integer($width) and _is_positive_integer($height) and _is_positive_integer($thickness)
_is_positive_integer($width) _is_positive_integer($height) _is_positive_integer($thickness)
Truth table50%2/4
A and B and C
A= _is_positive_integer($width)B= _is_positive_integer($height)C= _is_positive_integer($thickness)
ABCresult
0XX0
10X0
1100
1111
150 unless _is_positive_integer($width) && _is_positive_integer($height) && _is_positive_integer($thickness);
151  
152 11 my $codepoint = ord $glyph;
153 11 my $cell = _cell_metrics( $width, $height, $thickness );
154 11 ▶ return _braille_shapes( $codepoint, $cell ) if $codepoint >= BRAILLE_FIRST && $codepoint <= BRAILLE_LAST;
Branch: 1 branch
BranchTrueFalseTotal
if $codepoint >= 10240 and $codepoint <= 10495 5 6 11
Condition66%2/3
$codepoint >= 10240 and $codepoint <= 10495
!ll&&!rl&&r
065
MC/DC50%1/2
$codepoint >= 10240 and $codepoint <= 10495
$codepoint >= 10240 $codepoint <= 10495
Truth table66%2/3
A and B
A= $codepoint >= 10240B= $codepoint <= 10495
ABresult
0X0
100
111
155 6 ▶ return _sextant_shapes( $codepoint, $cell ) if $codepoint >= SEXTANT_FIRST;
Branch: 1 branch
BranchTrueFalseTotal
if $codepoint >= 129792 6 0 6
156 0 ▶ return _block_shapes( $BLOCKS_BY_CODEPOINT{$codepoint}, $cell ) if exists $BLOCKS_BY_CODEPOINT{$codepoint};
Branch: 1 branch
BranchTrueFalseTotal
if exists $BLOCKS_BY_CODEPOINT{$codepoint} 0 0 0
157 0 ▶ return _line_shapes( $ARMS_BY_CODEPOINT{$codepoint}, $cell ) if exists $ARMS_BY_CODEPOINT{$codepoint};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ARMS_BY_CODEPOINT{$codepoint} 0 0 0
158 0 ▶ return _dash_shapes( @{ $DASHES_BY_CODEPOINT{$codepoint} }, $cell ) if exists $DASHES_BY_CODEPOINT{$codepoint};
Branch: 1 branch
BranchTrueFalseTotal
if exists $DASHES_BY_CODEPOINT{$codepoint} 0 0 0
159 0 ▶ return _arc_shapes( @{ $ARC_BY_CODEPOINT{$codepoint} }, $cell ) if exists $ARC_BY_CODEPOINT{$codepoint};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ARC_BY_CODEPOINT{$codepoint} 0 0 0
160 0 return _diagonal_shapes( $codepoint, $cell );
161 }
162  
163 35 ▶sub _is_positive_integer ($value) {
Subroutine _is_positive_integer: called CC=2 SCAR=6.9
164 35 ▶ return $value =~ /\A[1-9][0-9]*\z/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[1-9][0-9]*\z/u ? : 34 1 35
165 }
166  
167 # A light line starts at x0 (vertical) or y0 (horizontal), so that every
168 # cell puts its lines at the same place and they meet seamlessly.
169 11 ▶sub _cell_metrics ( $width, $height, $thickness ) {
Subroutine _cell_metrics: called CC=1 SCAR=0.0
170 return {
171 11 width => $width,
172 height => $height,
173 thickness => $thickness,
174 x0 => int( ( $width - $thickness ) / 2 ),
175 y0 => int( ( $height - $thickness ) / 2 ),
176 };
177 }
178  
179 16 ▶sub _rect ( $left, $top, $right, $bottom, $coverage = 1 ) {
Subroutine _rect: called CC=4 SCAR=15.8
180 16 ▶ return () if $right <= $left || $bottom <= $top;
Branch: 1 branch
BranchTrueFalseTotal
if $right <= $left or $bottom <= $top 0 16 16
Condition33%1/3
$right <= $left or $bottom <= $top
l!l&&r!l&&!r
0016
MC/DC0%0/2
$right <= $left or $bottom <= $top
$right <= $left $bottom <= $top
Truth table33%1/3
A or B
A= $right <= $leftB= $bottom <= $top
ABresult
1X1
011
000
181 16 return { type => 'rect', x => $left, y => $top, width => $right - $left, height => $bottom - $top, coverage => $coverage };
182 }
183  
184 0 ▶sub _block_shapes ( $blocks, $cell ) {
Subroutine _block_shapes: not called CC=2 SCAR=17.9
185 0 my ( $width, $height ) = @$cell{qw(width height)};
186 0 ▶ my $x_at = sub ($eighths) { int( $width * $eighths / 8 + 0.5 ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
187 0 ▶ my $y_at = sub ($eighths) { int( $height * $eighths / 8 + 0.5 ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 ▶ return map { _rect( $x_at->( $_->[0] ), $y_at->( $_->[1] ), $x_at->( $_->[2] ), $y_at->( $_->[3] ), $_->[4] // 1 ) } @$blocks;
Condition0%0/2
$_->[4] // 1
l!l
00
MC/DC0%0/1
$_->[4] // 1
$_->[4]
Truth table0%0/2
A
A= $_->[4]
Aresult
11
00
189 }
190  
191 # The strokes of a line of the given weight across an axis that starts a
192 # light line at $start: [ from, to ] pairs, one for a single line, two
193 # rails for a double line.
194 0 ▶sub _strokes ( $weight, $start, $thickness ) {
Subroutine _strokes: not called CC=4 SCAR=30.0
195 0 ▶ return [ [ $start, $start + $thickness ] ] if $weight == LIGHT;
Branch: 1 branch
BranchTrueFalseTotal
if $weight == 1 0 0 0
196 0 ▶ if ( $weight == HEAVY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weight == 2) 0 0 0
197 0 my $from = $start - int( $thickness / 2 );
198 0 return [ [ $from, $from + 2 * $thickness ] ];
199 }
200 0 ▶ return [ [ $start - $thickness, $start ], [ $start + $thickness, $start + 2 * $thickness ] ] if $weight == DOUBLE;
Branch: 1 branch
BranchTrueFalseTotal
if $weight == 3 0 0 0
201 0 return [];
202 }
203  
204 # Lines from the middle of the cell to its edges. Each arm is a single
205 # stroke or two rails. Single strokes reach across the arms that cross
206 # them, so corners and junctions are solid. A rail of a double arm stops
207 # at the arm on its side, overlapping its nearest stroke, or, when there
208 # is none, runs to the far side of the arm opposite, which closes the
209 # outer corner.
210 0 ▶sub _line_shapes ( $arms, $cell ) {
Subroutine _line_shapes: not called CC=25 SCAR=64.8
211 0 my ( $up, $right, $down, $left ) = @$arms;
212 0 my ( $width, $height, $thickness, $x0, $y0 ) = @$cell{qw(width height thickness x0 y0)};
213  
214 0 my %vertical = ( up => _strokes( $up, $x0, $thickness ), down => _strokes( $down, $x0, $thickness ) );
215 0 my %horizontal = ( left => _strokes( $left, $y0, $thickness ), right => _strokes( $right, $y0, $thickness ) );
216 0 my @vertical_strokes = map { @$_ } values %vertical;
217 0 my @horizontal_strokes = map { @$_ } values %horizontal;
218  
219 # How far single strokes reach across the middle.
220 0 ▶ my $vertical_low = @vertical_strokes ? min( map { $_->[0] } @vertical_strokes ) : $x0;
Branch: 1 branch
BranchTrueFalseTotal
@vertical_strokes ? : 0 0 0
221 0 ▶ my $vertical_high = @vertical_strokes ? max( map { $_->[1] } @vertical_strokes ) : $x0 + $thickness;
Branch: 1 branch
BranchTrueFalseTotal
@vertical_strokes ? : 0 0 0
222 0 ▶ my $horizontal_low = @horizontal_strokes ? min( map { $_->[0] } @horizontal_strokes ) : $y0;
Branch: 1 branch
BranchTrueFalseTotal
@horizontal_strokes ? : 0 0 0
223 0 ▶ my $horizontal_high = @horizontal_strokes ? max( map { $_->[1] } @horizontal_strokes ) : $y0 + $thickness;
Branch: 1 branch
BranchTrueFalseTotal
@horizontal_strokes ? : 0 0 0
224  
225 0 my @shapes;
226 0 foreach my $arm ( [ left => $left ], [ right => $right ], [ up => $up ], [ down => $down ] ) {
227 0 my ( $direction, $weight ) = @$arm;
228 0 ▶ next if $weight == NONE;
Branch: 1 branch
BranchTrueFalseTotal
if $weight == 0 0 0 0
229 0 ▶ my $is_horizontal = $direction eq 'left' || $direction eq 'right';
Condition0%0/3
$direction eq 'left' || $direction eq 'right'
l!l&&r!l&&!r
000
MC/DC0%0/2
$direction eq 'left' || $direction eq 'right'
$direction eq 'left' $direction eq 'right'
Truth table0%0/3
A || B
A= $direction eq 'left'B= $direction eq 'right'
ABresult
1X1
011
000
230 0 ▶ my $strokes = $is_horizontal ? $horizontal{$direction} : $vertical{$direction};
Branch: 1 branch
BranchTrueFalseTotal
$is_horizontal ? : 0 0 0
231 0 ▶ my $towards_end = $direction eq 'left' || $direction eq 'up'; # the arm runs from 0 towards the middle
Condition0%0/3
$direction eq 'left' || $direction eq 'up'
l!l&&r!l&&!r
000
MC/DC0%0/2
$direction eq 'left' || $direction eq 'up'
$direction eq 'left' $direction eq 'up'
Truth table0%0/3
A || B
A= $direction eq 'left'B= $direction eq 'up'
ABresult
1X1
011
000
232 0 ▶ my $length = $is_horizontal ? $width : $height;
Branch: 1 branch
BranchTrueFalseTotal
$is_horizontal ? : 0 0 0
233  
234 0 my @spans;
235 0 ▶ if ( $weight != DOUBLE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weight != 3) { } 0 0 0
236 0 ▶ my ( $low, $high ) = $is_horizontal ? ( $vertical_low, $vertical_high ) : ( $horizontal_low, $horizontal_high );
Branch: 1 branch
BranchTrueFalseTotal
$is_horizontal ? : 0 0 0
237  
238 # A stem that ends at a line passing by (a T, as in ├ or ╢) stops
239 # at the stroke of that line nearest to it, so it does not fill
240 # the gap between the rails of a double line.
241 0 ▶ my ( $before, $after ) = $is_horizontal ? ( $vertical{up}, $vertical{down} ) : ( $horizontal{left}, $horizontal{right} );
Branch: 1 branch
BranchTrueFalseTotal
$is_horizontal ? : 0 0 0
242 0 ▶ my $opposite = $is_horizontal ? $horizontal{ $direction eq 'left' ? 'right' : 'left' } : $vertical{ $direction eq 'up' ? 'down' : 'up' };
Branch: 3 branches
BranchTrueFalseTotal
$is_horizontal ? : 0 0 0
$direction eq 'left' ? : 0 0 0
$direction eq 'up' ? : 0 0 0
243 0 ▶ if ( @$before && @$after && !@$opposite ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$before and @$after and not @$opposite) 0 0 0
Condition0%0/6
@$before and @$after and not @$opposite
!ll&&!rl&&r
000
@$before and @$after
!ll&&!rl&&r
000
MC/DC0%0/3
@$before and @$after and not @$opposite
@$before @$after not @$opposite
Truth table0%0/4
A and B and not(C)
A= @$beforeB= @$afterC= not @$opposite
ABCresult
0XX0
10X0
1100
1111
244 0 my @passing = sort { $a->[0] <=> $b->[0] } @$before, @$after;
245 0 ( $low, $high ) = ( $passing[-1][0], $passing[0][1] );
246 }
247 0 ▶ @spans = map { [ $_, $towards_end ? ( 0, $high ) : ( $low, $length ) ] } @$strokes;
Branch: 1 branch
BranchTrueFalseTotal
$towards_end ? : 0 0 0
248 }
249 else {
250 0 ▶ my ( $side_before, $side_after ) = $is_horizontal ? ( $vertical{up}, $vertical{down} ) : ( $horizontal{left}, $horizontal{right} );
Branch: 1 branch
BranchTrueFalseTotal
$is_horizontal ? : 0 0 0
251 0 ▶ my $middle = $is_horizontal ? [ $x0, $x0 + $thickness ] : [ $y0, $y0 + $thickness ];
Branch: 1 branch
BranchTrueFalseTotal
$is_horizontal ? : 0 0 0
252 0 my @rails = @$strokes;
253 0 @spans = (
254 [ $rails[0], _rail_extent( $side_before, $side_after, $towards_end, $length, $middle ) ],
255 [ $rails[1], _rail_extent( $side_after, $side_before, $towards_end, $length, $middle ) ],
256 );
257 }
258  
259 0 foreach my $span (@spans) {
260 0 my ( $across, $from, $to ) = ( $span->[0], $span->[1], $span->[2] );
261 0 ▶ push @shapes, $is_horizontal ? _rect( $from, $across->[0], $to, $across->[1] ) : _rect( $across->[0], $from, $across->[1], $to );
Branch: 1 branch
BranchTrueFalseTotal
$is_horizontal ? : 0 0 0
262 }
263 }
264 0 return @shapes;
265 }
266  
267 # Where a rail of a double arm runs, along the arm: from the cell edge to
268 # the stroke of the arm on the rail's side ($beside) that is nearest to
269 # the edge, overlapping it; without such an arm, to the far side of the
270 # arm opposite ($opposite); without either, across the middle line
271 # ($middle, where a light line would cross), so the rails of two opposite
272 # arms join.
273 0 ▶sub _rail_extent ( $beside, $opposite, $towards_end, $length, $middle ) {
Subroutine _rail_extent: not called CC=7 SCAR=40.3
274 0 ▶ if (@$beside) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$beside) 0 0 0
275 0 ▶ my $nearest = $towards_end ? $beside->[0] : $beside->[-1];
Branch: 1 branch
BranchTrueFalseTotal
$towards_end ? : 0 0 0
276 0 ▶ return $towards_end ? ( 0, $nearest->[1] ) : ( $nearest->[0], $length );
Branch: 1 branch
BranchTrueFalseTotal
$towards_end ? : 0 0 0
277 }
278 0 ▶ if (@$opposite) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$opposite) 0 0 0
279 0 ▶ return $towards_end ? ( 0, max( map { $_->[1] } @$opposite ) ) : ( min( map { $_->[0] } @$opposite ), $length );
Branch: 1 branch
BranchTrueFalseTotal
$towards_end ? : 0 0 0
280 }
281 0 ▶ return $towards_end ? ( 0, $middle->[1] ) : ( $middle->[0], $length );
Branch: 1 branch
BranchTrueFalseTotal
$towards_end ? : 0 0 0
282 }
283  
284 0 ▶sub _dash_shapes ( $is_horizontal, $weight, $dashes, $cell ) {
Subroutine _dash_shapes: not called CC=5 SCAR=34.0
285 0 my ( $width, $height, $thickness, $x0, $y0 ) = @$cell{qw(width height thickness x0 y0)};
286 0 ▶ my ($stroke) = @{ _strokes( $weight, $is_horizontal ? $y0 : $x0, $thickness ) };
Branch: 1 branch
BranchTrueFalseTotal
$is_horizontal ? : 0 0 0
287 0 ▶ my $length = $is_horizontal ? $width : $height;
Branch: 1 branch
BranchTrueFalseTotal
$is_horizontal ? : 0 0 0
288  
289 # Each dash takes its share of the cell minus a gap at its end; the gap
290 # is split around the cell edge so dashes are even across cells.
291 0 my $share = $length / $dashes;
292 0 my $gap = max( 1, int( $share / 3 + 0.5 ) );
293 0 my @shapes;
294 0 foreach my $index ( 0 .. $dashes - 1 ) {
295 0 my $from = int( $index * $share + $gap / 2 + 0.5 );
296 0 my $to = int( ( $index + 1 ) * $share - $gap / 2 + 0.5 );
297 0 ▶ push @shapes, $is_horizontal ? _rect( $from, $stroke->[0], $to, $stroke->[1] ) : _rect( $stroke->[0], $from, $stroke->[1], $to );
Branch: 1 branch
BranchTrueFalseTotal
$is_horizontal ? : 0 0 0
298 }
299 0 return @shapes;
300 }
301  
302 # A quarter circle between the middle of the vertical and the horizontal
303 # edge it connects, with straight ends, stroked as thick as a light line.
304 0 ▶sub _arc_shapes ( $vertical, $horizontal, $cell ) {
Subroutine _arc_shapes: not called CC=6 SCAR=37.4
305 0 my ( $width, $height, $thickness, $x0, $y0 ) = @$cell{qw(width height thickness x0 y0)};
306 0 my ( $center_x, $center_y ) = ( $x0 + $thickness / 2, $y0 + $thickness / 2 );
307 0 my $radius = min( $center_x, $width - $center_x, $center_y, $height - $center_y );
308  
309 0 ▶ my $edge_y = $vertical eq 'down' ? $height : 0;
Branch: 1 branch
BranchTrueFalseTotal
$vertical eq 'down' ? : 0 0 0
310 0 ▶ my $edge_x = $horizontal eq 'right' ? $width : 0;
Branch: 1 branch
BranchTrueFalseTotal
$horizontal eq 'right' ? : 0 0 0
311 0 ▶ my $sign_x = $horizontal eq 'right' ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$horizontal eq 'right' ? : 0 0 0
312 0 ▶ my $sign_y = $vertical eq 'down' ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$vertical eq 'down' ? : 0 0 0
313 0 my ( $pivot_x, $pivot_y ) = ( $center_x + $sign_x * $radius, $center_y + $sign_y * $radius );
314  
315 0 my @points = ( $center_x, $edge_y );
316 0 foreach my $step ( 0 .. ARC_SEGMENTS ) {
317 0 my $angle = ( $step / ARC_SEGMENTS ) * ( atan2( 1, 1 ) * 2 ); # 0 .. 90 degrees
318 0 push @points, $pivot_x - $sign_x * $radius * cos($angle), $pivot_y - $sign_y * $radius * sin($angle);
319 }
320 0 push @points, $edge_x, $center_y;
321 0 return { type => 'polyline', points => \@points, width => $thickness };
322 }
323  
324 0 ▶sub _diagonal_shapes ( $codepoint, $cell ) {
Subroutine _diagonal_shapes: not called CC=4 SCAR=30.0
325 0 my ( $width, $height, $thickness ) = @$cell{qw(width height thickness)};
326 0 my $rising = { type => 'polyline', points => [ $width, 0, 0, $height ], width => $thickness };
327 0 my $falling = { type => 'polyline', points => [ 0, 0, $width, $height ], width => $thickness };
328 0 ▶ return $rising if $codepoint == 0x2571;
Branch: 1 branch
BranchTrueFalseTotal
if $codepoint == 9585 0 0 0
329 0 ▶ return $falling if $codepoint == 0x2572;
Branch: 1 branch
BranchTrueFalseTotal
if $codepoint == 9586 0 0 0
330 0 ▶ return ( $rising, $falling ) if $codepoint == 0x2573;
Branch: 1 branch
BranchTrueFalseTotal
if $codepoint == 9587 0 0 0
331 0 croak sprintf 'Term::Fabulous::Screenshot::BoxDrawing: no shapes for U+%04X', $codepoint;
332 }
333  
334 2 1;
335  
336 __END__
↑ Summary blib/lib/Term/Fabulous/Render/Border.pm »