| 1 | package Term::Fabulous::Screenshot::Render::SVG; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use v5.32; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use feature 'signatures'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Exporter qw(import); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | our @EXPORT_OK = qw(render_svg); | |||||||||||||
| 13 | |||||||||||||||
| 14 | # Draws a Term::Fabulous::Screenshot::Scene as an SVG document (a | ||||||||||||||
| 15 | # character string). The image is self-contained: no external fonts or | ||||||||||||||
| 16 | # files, so it shows the same in an <img> element, where browsers load | ||||||||||||||
| 17 | # nothing an SVG refers to. | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 0 | ▶ | sub render_svg ($scene) { | ||||||||||||
|
Subroutine
render_svg: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 20 | 0 | my $layout = $scene->layout; | |||||||||||||
| 21 | 0 | my $theme = $scene->theme; | |||||||||||||
| 22 | 0 | my ( $width, $height ) = @$layout{qw(image_width image_height)}; | |||||||||||||
| 23 | |||||||||||||||
| 24 | my @svg = ( | ||||||||||||||
| 25 | sprintf( | ||||||||||||||
| 26 | '<svg xmlns="http://www.w3.org/2000/svg" width="%s" height="%s" viewBox="0 0 %s %s" role="img" aria-label="%s">', _n($width), _n($height), _n($width), _n($height), _escape( $scene->title ) | ||||||||||||||
| 27 | ), | ||||||||||||||
| 28 | sprintf( '<title>%s</title>', _escape( $scene->title ) ), | ||||||||||||||
| 29 | _style($scene), | ||||||||||||||
| 30 | _definitions($scene), | ||||||||||||||
| 31 | _window($scene), | ||||||||||||||
| 32 | '<g shape-rendering="crispEdges">', | ||||||||||||||
| 33 | 0 | ( map { _rect( $_, $_->{color} ) } $scene->backgrounds ), | |||||||||||||
| 34 | 0 | ( map { _rect( $_, $_->{color} ) } grep { $_->{type} eq 'rect' } $scene->shapes ), | |||||||||||||
| 35 | '</g>', | ||||||||||||||
| 36 | 0 | ( map { _polyline($_) } grep { $_->{type} eq 'polyline' } $scene->shapes ), | |||||||||||||
| 37 | 0 | ( map { _circle($_) } grep { $_->{type} eq 'circle' } $scene->shapes ), | |||||||||||||
| 38 | '<g class="terminal">', | ||||||||||||||
| 39 | 0 | ( map { _text( $_, $scene ) } $scene->texts ), | |||||||||||||
| 40 | '</g>', | ||||||||||||||
| 41 | '<g shape-rendering="crispEdges">', | ||||||||||||||
| 42 | 0 | ( map { _rect( $_, $_->{color} ) } $scene->lines ), | |||||||||||||
| 43 | '</g>', | ||||||||||||||
| 44 | 0 | ( map { _image($_) } $scene->pictures ), | |||||||||||||
| 45 | '</svg>', | ||||||||||||||
| 46 | ); | ||||||||||||||
| 47 | 0 | return join( "\n", @svg ) . "\n"; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | ▶ | sub _style ($scene) { | ||||||||||||
|
Subroutine
_style: not called CC=1 SCAR=6.9
| |||||||||||||||
| 51 | 0 | my $theme = $scene->theme; | |||||||||||||
| 52 | 0 | return join "\n", | |||||||||||||
| 53 | '<style>', | ||||||||||||||
| 54 | sprintf( '.terminal text { font-family: %s; font-size: %spx; white-space: pre; }', $theme->font_family, _n( $scene->font_size ) ), | ||||||||||||||
| 55 | '.terminal .b { font-weight: bold; }', | ||||||||||||||
| 56 | '.terminal .i { font-style: italic; }', | ||||||||||||||
| 57 | sprintf( '.title { font-family: %s; font-size: %spx; }', $theme->font_family, _n( $theme->title_font_size * $scene->scale ) ), | ||||||||||||||
| 58 | '</style>'; | ||||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | ▶ | sub _definitions ($scene) { | ||||||||||||
|
Subroutine
_definitions: not called CC=1 SCAR=6.9
| |||||||||||||||
| 62 | 0 | my $layout = $scene->layout; | |||||||||||||
| 63 | 0 | my $theme = $scene->theme; | |||||||||||||
| 64 | 0 | my $scale = $scene->scale; | |||||||||||||
| 65 | return join "\n", | ||||||||||||||
| 66 | '<defs>', | ||||||||||||||
| 67 | sprintf( | ||||||||||||||
| 68 | '<filter id="shadow" x="-10%%" y="-10%%" width="120%%" height="130%%"><feDropShadow dx="0" dy="%s" stdDeviation="%s" flood-color="#000000" flood-opacity="%s"/></filter>', | ||||||||||||||
| 69 | _n( $theme->shadow_offset * $scale ), _n( $theme->shadow_blur * $scale ), _n( $theme->shadow_opacity ) | ||||||||||||||
| 70 | ), | ||||||||||||||
| 71 | sprintf( | ||||||||||||||
| 72 | '<clipPath id="window"><rect x="%s" y="%s" width="%s" height="%s" rx="%s"/></clipPath>', | ||||||||||||||
| 73 | 0 | map { _n($_) } @$layout{qw(window_x window_y window_width window_height)}, $theme->corner_radius * $scale | |||||||||||||
| 74 | ), | ||||||||||||||
| 75 | '</defs>'; | ||||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | # The window: a rounded rectangle with a shadow, the title bar with the | ||||||||||||||
| 79 | # three buttons and the title, and a faint outline. | ||||||||||||||
| 80 | 0 | ▶ | sub _window ($scene) { | ||||||||||||
|
Subroutine
_window: not called CC=3 SCAR=24.8
| |||||||||||||||
| 81 | 0 | my $layout = $scene->layout; | |||||||||||||
| 82 | 0 | my $theme = $scene->theme; | |||||||||||||
| 83 | 0 | my $scale = $scene->scale; | |||||||||||||
| 84 | 0 | my ( $x, $y, $width, $height ) = @$layout{qw(window_x window_y window_width window_height)}; | |||||||||||||
| 85 | 0 | my $radius = $theme->corner_radius * $scale; | |||||||||||||
| 86 | 0 | my $bar_height = $theme->title_bar_height * $scale; | |||||||||||||
| 87 | 0 | my $center_y = $y + $bar_height / 2; | |||||||||||||
| 88 | |||||||||||||||
| 89 | my @parts = ( | ||||||||||||||
| 90 | 0 | sprintf( '<rect x="%s" y="%s" width="%s" height="%s" rx="%s" fill="%s" filter="url(#shadow)"/>', map( { _n($_) } $x, $y, $width, $height, $radius ), _color( $theme->default_background ) ), | |||||||||||||
| 91 | 0 | sprintf( '<rect x="%s" y="%s" width="%s" height="%s" fill="%s" clip-path="url(#window)"/>', map( { _n($_) } $x, $y, $width, $bar_height ), _color( $theme->title_bar_color ) ), | |||||||||||||
| 92 | ); | ||||||||||||||
| 93 | 0 | my $button_x = $x + ( $theme->button_spacing ) * $scale; | |||||||||||||
| 94 | 0 | foreach my $color ( @{ $theme->button_colors } ) { | |||||||||||||
| 95 | 0 | push @parts, sprintf( '<circle cx="%s" cy="%s" r="%s" fill="%s"/>', _n($button_x), _n($center_y), _n( $theme->button_radius * $scale ), _color($color) ); | |||||||||||||
| 96 | 0 | $button_x += $theme->button_spacing * $scale; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | 0 | ▶ | push @parts, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | sprintf( | ||||||||||||||
| 100 | '<text class="title" x="%s" y="%s" text-anchor="middle" dominant-baseline="central" fill="%s">%s</text>', | ||||||||||||||
| 101 | _n( $x + $width / 2 ), _n($center_y), _color( $theme->title_color ), _escape( $scene->shown_title ) | ||||||||||||||
| 102 | ) if length $scene->shown_title; | ||||||||||||||
| 103 | push @parts, | ||||||||||||||
| 104 | sprintf( | ||||||||||||||
| 105 | '<rect x="%s" y="%s" width="%s" height="%s" rx="%s" fill="none" stroke="%s" stroke-opacity="%s" stroke-width="%s"/>', | ||||||||||||||
| 106 | 0 | map( { _n($_) } $x + 0.5 * $scale, $y + 0.5 * $scale, $width - $scale, $height - $scale, $radius ), | |||||||||||||
| 107 | _color( $theme->outline_color ), _n( $theme->outline_opacity ), _n($scale) | ||||||||||||||
| 108 | ); | ||||||||||||||
| 109 | 0 | return join "\n", @parts; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 0 | ▶ | sub _rect ( $rect, $color ) { | ||||||||||||
|
Subroutine
_rect: not called CC=1 SCAR=6.9
| |||||||||||||||
| 113 | 0 | return sprintf '<rect x="%s" y="%s" width="%s" height="%s" fill="%s"/>', ( map { _n($_) } @$rect{qw(x y width height)} ), _color($color); | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 0 | ▶ | sub _polyline ($line) { | ||||||||||||
|
Subroutine
_polyline: not called CC=1 SCAR=6.9
| |||||||||||||||
| 117 | 0 | return sprintf '<polyline points="%s" fill="none" stroke="%s" stroke-width="%s" stroke-linejoin="round"/>', join( ' ', map { _n($_) } @{ $line->{points} } ), | |||||||||||||
| 118 | 0 | _color( $line->{color} ), _n( $line->{width} ); | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 0 | ▶ | sub _circle ($circle) { | ||||||||||||
|
Subroutine
_circle: not called CC=1 SCAR=6.9
| |||||||||||||||
| 122 | 0 | return sprintf '<circle cx="%s" cy="%s" r="%s" fill="%s"/>', ( map { _n($_) } @$circle{qw(cx cy r)} ), _color( $circle->{color} ); | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | # A run is stretched to exactly the width of its cells (textLength), so | ||||||||||||||
| 126 | # the columns line up with any monospace font; a single glyph is centered | ||||||||||||||
| 127 | # in its cells. | ||||||||||||||
| 128 | 0 | ▶ | sub _text ( $text, $scene ) { | ||||||||||||
|
Subroutine
_text: not called CC=5 SCAR=34.0
| |||||||||||||||
| 129 | 0 | ▶ | my @classes = ( $text->{bold} ? 'b' : (), $text->{italic} ? 'i' : () ); | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 130 | 0 | ▶ | my $class = @classes ? sprintf( ' class="%s"', join ' ', @classes ) : ''; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 0 | my $fill = _color( $text->{color} ); | |||||||||||||
| 132 | 0 | ▶ | if ( $text->{anchor} eq 'middle' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 0 | return sprintf '<text x="%s" y="%s" text-anchor="middle"%s fill="%s">%s</text>', _n( $text->{x} + $text->{width} / 2 ), _n( $text->{y} ), $class, $fill, _escape( $text->{text} ); | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | 0 | return sprintf '<text x="%s" y="%s" textLength="%s"%s fill="%s">%s</text>', _n( $text->{x} ), _n( $text->{y} ), _n( $text->{width} ), $class, $fill, _escape( $text->{text} ); | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | # The picture is embedded as a data URL: an SVG shown in an <img> | ||||||||||||||
| 139 | # element may load nothing else. | ||||||||||||||
| 140 | 0 | ▶ | sub _image ($picture) { | ||||||||||||
|
Subroutine
_image: not called CC=1 SCAR=6.9
| |||||||||||||||
| 141 | 0 | return sprintf '<image x="%s" y="%s" width="%s" height="%s" preserveAspectRatio="none" href="data:image/png;base64,%s"/>', ( map { _n($_) } @$picture{qw(x y width height)} ), | |||||||||||||
| 142 | 0 | encode_base64( $picture->{png}, '' ); | |||||||||||||
| 143 | } | ||||||||||||||
| 144 | |||||||||||||||
| 145 | 0 | ▶ | sub _color ($rgb) { | ||||||||||||
|
Subroutine
_color: not called CC=1 SCAR=6.9
| |||||||||||||||
| 146 | 0 | return sprintf '#%06x', $rgb; | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | # Numbers without trailing zeros, so the same scene always gives the same | ||||||||||||||
| 150 | # bytes. | ||||||||||||||
| 151 | 0 | ▶ | sub _n ($number) { | ||||||||||||
|
Subroutine
_n: not called CC=2 SCAR=17.9
| |||||||||||||||
| 152 | 0 | my $text = sprintf '%.3f', $number; | |||||||||||||
| 153 | 0 | $text =~ s/\.?0+\z//; | |||||||||||||
| 154 | 0 | ▶ | return $text eq '-0' ? '0' : $text; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 155 | } | ||||||||||||||
| 156 | |||||||||||||||
| 157 | # XML has no way to write control characters other than tab and line | ||||||||||||||
| 158 | # breaks; text with one cannot be shown. | ||||||||||||||
| 159 | 0 | ▶ | sub _escape ($text) { | ||||||||||||
|
Subroutine
_escape: not called CC=2 SCAR=17.9
| |||||||||||||||
| 160 | 0 | ▶ | croak sprintf 'Term::Fabulous::Screenshot::Render::SVG: cannot write the control character U+%04X in SVG', ord $1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 161 | if $text =~ /([\x00-\x08\x0B\x0C\x0E-\x1F\x{FFFE}\x{FFFF}])/; | ||||||||||||||
| 162 | 0 | return $text =~ s/&/&/gr =~ s/</</gr =~ s/>/>/gr =~ s/"/"/gr; | |||||||||||||
| 163 | } | ||||||||||||||
| 164 | |||||||||||||||
| 165 | 1 | 1; | |||||||||||||
| 166 | |||||||||||||||
| 167 | __END__ | ||||||||||||||