--- ../afd_svg.v2.0.pl	2023-12-07 09:10:23
+++ ../afd_svg.v2.1.001.pl	2023-12-07 14:53:51
@@ -1,8 +1,12 @@
 #!/usr/bin/perl -w
 
 # Default parameter
-my ($asp_x, $asp_y, $par_x, $par_y, $cont_x, $cont_y, $prot_x, $prot_y, $b_top, $b_sub, $height, $stroke, $style, $shade, $margin) = 
-   ( 4,      3,      1,      1,      0,       0,       0,       0,       0,      0,      270,     4,       "DVB",  12.5,   4     );
+# Pattern
+my ($asp_x, $asp_y, $par_x, $par_y, $cont_x, $cont_y, $prot_x, $prot_y, $pad_x, $pad_y, $crop_x, $crop_y, $b_top) = 
+   ( 4,      3,      1,      1,      0,       0,       0,       0,       0,      0,      0,       0,       0    );
+# Style
+my ($b_sub, $height, $stroke, $style, $shade, $margin) = 
+   ( 0,      270,     4,       "DVB",  12.5,   4     );
 
 # Parse command line arguments
 while (my $arg = shift) {
@@ -24,8 +28,10 @@
 	-par <x> <y>: Pixel Aspect Ratio, integers. Default: 1 : 1 (square pixel)
 	  Note: Since SVG shows anamorphic pixel as square ones, the diagram will illustrate the anamorphic distortion.
 	-cont <x> <y>: Active Content Aspect Ratio, integers. Default: full frame
+	-top: Place content at top of frame. Default: centered in frame
 	-prot <x> <y>: Shoot\&Protect Aspect Ratio, integers. Default: same as Content AR
-	-t: Place content at top of frame. Default: centered in frame
+	-pad <x> <y>: Pad resulting frame to specified Aspect Ratio, integers. Default: no pad
+	-crop <x> <y>: Crop padded frame to specified Aspect Ratio, integers. Default: no crop
 	-sub: Add two lines of subtitles, 2nd in letterbox bar (if applicable)
 	SVG graphics parameter:
 	-h <height>: SVG diagram height, integer. Default: 270
@@ -55,7 +61,13 @@
         } elsif ($arg eq '-prot') {
             $prot_x = shift || die "-prot requires two values";
             $prot_y = shift || die "-prot requires two values";
-        } elsif ($arg eq '-t') {
+        } elsif ($arg eq '-pad') {
+            $pad_x = shift || die "-pad requires two values";
+            $pad_y = shift || die "-pad requires two values";
+        } elsif ($arg eq '-crop') {
+            $crop_x = shift || die "-crop requires two values";
+            $crop_y = shift || die "-crop requires two values";
+        } elsif (($arg eq '-t') || ($arg eq '-top')) {
             $b_top = 1;
         } elsif ($arg eq '-sub') {
             $b_sub = 1;
@@ -88,6 +100,18 @@
     }
 }
 
+# Returns higher of two values
+sub max 
+{
+    my ($a, $b) = @_;
+    
+    if ($a > $b) {
+        return $a;
+    } else {
+        return $b;
+    }
+}
+
 # Total Frame
 my $width = $height * $asp_x / $asp_y * $par_y / $par_x;
 
@@ -112,13 +136,13 @@
 # By default, shapes are shrunk so that the entire line width stays within the confines of the shape.
 # Optionally, the shape can be grown on selected sides so that the entire line width is just outside of the confines.
 
-# Returns a vertical SVG Line. Uses global $stroke variable.
+# Returns a vertical SVG Line.
 sub svg_vertline 
 {
     # adj is either 'l' or 'r', to shift to resp. side.
     # length is shrunk to keep line within confines.
     
-    my ($x, $y1, $y2, $adj) = @_;
+    my ($x, $y1, $y2, $adj, $sw) = @_;
     
     # Shift horizontally
     if ($adj=~/l/) {
@@ -132,13 +156,14 @@
     $y2 -= $s2;
     
     return 
-        '<line ' . 
-        'x1="' . $x . '" y1="' . $y1 . '" ' . 
-        'x2="' . $x . '" y2="' . $y2 . '" ' . 
-        '/>';
+        '<line' . 
+        ' x1="' . $x . '" y1="' . $y1 . '"' . 
+        ' x2="' . $x . '" y2="' . $y2 . '"' . 
+        ' stroke-width="' . $sw . '"' . 
+        ' />';
 }
 
-# Returns a SVG Rectangle. Uses global $stroke variable.
+# Returns a SVG Rectangle.
 sub svg_rect 
 {
     # adj is combo of l,r,t,b chars, to grow on resp. side to keep line outside of confines.
@@ -146,7 +171,7 @@
     # fill is grey percentage, 0 (white) to 100 (black), or -1 for no fill
     # b_stroke_fill, if not zero, sets stroke color to same as fill color, or white, if no fill
     
-    my ($x, $y, $w, $h, $adj, $fill, $b_stroke_fill) = @_;
+    my ($x, $y, $w, $h, $adj, $fill, $b_stroke_fill, $sw) = @_;
     
     # Shrink / grow horizontally
     if ($adj=~/l/) {
@@ -178,22 +203,23 @@
     
     my $fillrgb = int((255 * (100 - ($fill >= 0 ? $fill : 0)) + 99) / 100);
     return 
-        '<rect ' . 
-        'x="' . $x . '" y="' . $y . '" ' . 
-        'width="' . $w . '" height="' . $h . '" ' . 
-        ($fill >= 0 ? 'fill="rgb(' . "${fillrgb},${fillrgb},${fillrgb}" . ')" ' : '') . 
-        ($b_stroke_fill ? 'stroke="rgb(' . "${fillrgb},${fillrgb},${fillrgb}" . ')" ' : '') . 
-        '/>';
+        '<rect' . 
+        ' x="' . $x . '" y="' . $y . '"' . 
+        ' width="' . $w . '" height="' . $h . '"' . 
+        ($fill >= 0 ? ' fill="rgb(' . "${fillrgb},${fillrgb},${fillrgb}" . ')"' : '') . 
+        ($b_stroke_fill ? ' stroke="rgb(' . "${fillrgb},${fillrgb},${fillrgb}" . ')"' : '') . 
+        ' stroke-width="' . $sw . '"' . 
+        ' />';
 }
 
-# Returns a SVG Oval (Circle or Ellipse). Uses global $stroke variable.
+# Returns a SVG Oval (Circle or Ellipse).
 sub svg_oval 
 {
     # adj is combo of l,r,t,b chars, to grow on resp. side to keep line outside of confines.
     # default is to shrink to keep line within confines.
     # fill is grey percentage, 0 (white) to 100 (black), or -1 for no fill
     
-    my ($x, $y, $rx, $ry, $adj, $fill) = @_;
+    my ($x, $y, $rx, $ry, $adj, $fill, $sw) = @_;
     
     # Default shrink
     $rx -= $s2;
@@ -237,13 +263,14 @@
     
     my $fillrgb = int((255 * (100 - $fill) + 99) / 100);
     return 
-        '<' . ($rx == $ry ? 'circle' : 'ellipse') . ' ' . 
-        'cx="' . $x . '" cy="' . $y . '" ' . 
-        ($rx == $ry ? 
-            'r="' . $rx . '" ' : 
-            'rx="' . $rx . '" ry="' . $ry . '" '
+        '<' . ($rx == $ry ? 'circle' : 'ellipse') . 
+        ' cx="' . $x . '" cy="' . $y . '"' . 
+        ($rx == $ry ? 
+            ' r="' . $rx . '"' : 
+            ' rx="' . $rx . '" ry="' . $ry . '"'
         ) . 
-        ($fill >= 0 ? 'fill="rgb(' . "${fillrgb},${fillrgb},${fillrgb}" . ')" ' : '') . 
+        ($fill >= 0 ? ' fill="rgb(' . "${fillrgb},${fillrgb},${fillrgb}" . ')"' : '') . 
+        ' stroke-width="' . $sw . '"' . 
         '/>';
 }
 
@@ -301,18 +328,18 @@
 {
     # fill is grey percentage, 0 (white) to 100 (black), or -1 for no fill
     
-    my $fill = shift;
+    my ($view_x, $view_y, $view_w, $view_h, $fill) = @_;
     
     my $fillrgb = int((255 * (100 - $fill) + 99) / 100);
     return 
         '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n" .
         '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n" .
         '<svg width="' . $width . '" height="' . $height . '" ' . 
-        ($margin > 0 ? 'viewBox="-' . $margin . ' -' . $margin . ' ' . ($width + 2 * $margin) . ' ' . ($height + 2 * $margin) . '" ' : '') . 
+        'viewBox="' . $view_x . ' ' . $view_y . ' ' . $view_w . ' ' . $view_h . '" ' . 
         'xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' . "\n" .
         "\t<g fill=" . ($fill >= 0 ? '"rgb(' . "${fillrgb},${fillrgb},${fillrgb}" . ')"' : '"none"') . 
             ' stroke="rgb(0,0,0)" stroke-width="' . $stroke . '" stroke-linecap="round" stroke-linejoin="round">' . "\n" . 
-        ($margin > 0 ? "\t\t" . svg_rect(-$margin, -$margin, $width + 2 * $margin, $height + 2 * $margin, '', -1, 1) . "\n" : '');
+        ($margin > 0 ? "\t\t" . svg_rect(-$margin, -$margin, $width + 2 * $margin, $height + 2 * $margin, '', -1, 1, $stroke) . "\n" : '');
 }
 
 # Returns the SVG footer
@@ -329,12 +356,28 @@
 # Area Dimensions
 #
 
+# Full (Total) Frame
+my $frame_w = $width;
+my $frame_h = $height;
+
+# SVG canvas
+my $view_x = 0;
+my $view_y = 0;
+my $view_w = $width;
+my $view_h = $height;
+if ($margin > 0) {
+    $view_x -= $margin;
+    $view_y -= $margin;
+    $view_w += (2 * $margin);
+    $view_h += (2 * $margin);
+}
+
 # Active Content (relative to total frame)
-my $cont_w = $width;
-my $cont_h = $height;
+my $cont_w = $frame_w;
+my $cont_h = $frame_h;
 if (($cont_x > 0) && ($cont_y > 0)) {
-    $cont_w = min($width,  $height * $cont_x / $cont_y * $par_y / $par_x);
-    $cont_h = min($height, $width  * $cont_y / $cont_x * $par_x / $par_y);
+    $cont_w = min($frame_w, $frame_h * $cont_x / $cont_y * $par_y / $par_x);
+    $cont_h = min($frame_h, $frame_w * $cont_y / $cont_x * $par_x / $par_y);
 }
 
 # Shoot & Protect (relative to active content)
@@ -346,8 +389,8 @@
 }
 
 # Bars (Outside of active content)
-my $bar_w = ($width - $cont_w) / 2;
-my $bar_h = ($height - $cont_h) / 2;
+my $bar_w = ($frame_w - $cont_w) / 2;
+my $bar_h = ($frame_h - $cont_h) / 2;
 
 # Optional Crop (Outside of "Shoot & Protect", but inside active content)
 my $opt_w = ($cont_w - $prot_w) / 2;
@@ -369,8 +412,46 @@
 my $cont6427_w = min($cont_w, $cont_h * 64 / 27);
 my $cont6427_h = $cont_h;
 
+# Padding
+my $pad_w = $frame_w;
+my $pad_h = $frame_h;
+if (($pad_x > 0) && ($pad_y > 0)) {
+    $pad_w = max($frame_w, $frame_w * $pad_x / $pad_y * $asp_y / $asp_x);
+    $pad_h = max($frame_h, $frame_h * $pad_y / $pad_x * $asp_x / $asp_y);
+}
 
+# Pad Bars (Outside of total frame)
+my $pad_bar_w = ($pad_w - $frame_w) / 2;
+my $pad_bar_h = ($pad_h - $frame_h) / 2;
+print "PAD: $frame_w x $frame_h --> $pad_w x $pad_h x=$pad_bar_w+$frame_w+$pad_bar_w, y=$pad_bar_h+$frame_h+$pad_bar_h\n";
 
+# Adjust SVG Canvas
+$view_x -= $pad_bar_w;
+$view_y -= $pad_bar_h;
+$view_w += (2 * $pad_bar_w);
+$view_h += (2 * $pad_bar_h);
+
+
+# Cropping
+my $crop_w = $frame_w;
+my $crop_h = $frame_h;
+if (($crop_x > 0) && ($crop_y > 0)) {
+    $crop_w = min($frame_w, $frame_h * $crop_x / $crop_y * $par_y / $par_x);
+    $crop_h = min($frame_h, $frame_w * $crop_y / $crop_x * $par_x / $par_y);
+}
+
+# Crop Bars (Cutting into total frame)
+my $crop_bar_w = ($frame_w - $crop_w) / 2;
+my $crop_bar_h = ($frame_h - $crop_h) / 2;
+print "CROP: $frame_w x $frame_h --> $crop_w x $crop_h x=$crop_bar_w-$frame_w-$crop_bar_w, y=$crop_bar_h-$frame_h-$crop_bar_h\n";
+
+# Adjust SVG Canvas
+$view_x += $crop_bar_w;
+$view_y += $crop_bar_h;
+$view_w -= (2 * $crop_bar_w);
+$view_h -= (2 * $crop_bar_h);
+
+
 #
 # DVB Style
 #
@@ -385,8 +466,8 @@
     my $dvb_active_rect_adj = ($bar_w > 0 ? 'lr' : '') . ($bar_h > 0 ? ($b_top ? 'b' : 'tb') : '');
     
     # Main Circle
-    my $dvb_main_circ_cx = $width / 2;
-    my $dvb_main_circ_cy = $height / 2 - ($b_top ? $bar_h : 0);
+    my $dvb_main_circ_cx = $frame_w / 2;
+    my $dvb_main_circ_cy = $frame_h / 2 - ($b_top ? $bar_h : 0);
     my $dvb_main_circ_ry = $prot_h / 2;
     my $dvb_main_circ_rx = $dvb_main_circ_ry * $par_y / $par_x;
     my $dvb_main_circ_adj = '';
@@ -417,38 +498,54 @@
     # Left / Top Bar
     my $dvb_lt_bar_x = 0;
     my $dvb_lt_bar_y = 0;
-    my $dvb_lt_bar_w = ($bar_h > 0 ? ($b_top ? 0 : $width) : $bar_w);
-    my $dvb_lt_bar_h = ($bar_w > 0 ? $height : ($b_top ? 0 : $bar_h));
+    my $dvb_lt_bar_w = ($bar_h > 0 ? ($b_top ? 0 : $frame_w) : $bar_w);
+    my $dvb_lt_bar_h = ($bar_w > 0 ? $frame_h : ($b_top ? 0 : $bar_h));
     my $dvb_lt_bar_adj = '';
     
     # Right / Bottom Bar
     my $dvb_rb_bar_x = ($bar_w > 0 ? $bar_w + $cont_w : 0);
     my $dvb_rb_bar_y = ($bar_h > 0 ? ($b_top ? $cont_h : $bar_h + $cont_h) : 0);
-    my $dvb_rb_bar_w = ($bar_h > 0 ? $width : $bar_w);
-    my $dvb_rb_bar_h = ($bar_w > 0 ? $height : ($b_top ? $bar_h * 2 : $bar_h));
+    my $dvb_rb_bar_w = ($bar_h > 0 ? $frame_w : $bar_w);
+    my $dvb_rb_bar_h = ($bar_w > 0 ? $frame_h : ($b_top ? $bar_h * 2 : $bar_h));
     my $dvb_rb_bar_adj = '';
     
     # DVB File Content
-    $svg = svg_header(0);
-    $svg .= "\t\t" . svg_rect($dvb_active_rect_x, $dvb_active_rect_y, $dvb_active_rect_w, $dvb_active_rect_h, $dvb_active_rect_adj, $shade) . "\n";
-    $svg .= "\t\t" . svg_oval($dvb_main_circ_cx, $dvb_main_circ_cy, $dvb_main_circ_rx, $dvb_main_circ_ry, $dvb_main_circ_adj, -1) . "\n";
+    $svg = svg_header($view_x, $view_y, $view_w, $view_h, 0);
+    $svg .= "\t\t" . svg_rect($dvb_active_rect_x, $dvb_active_rect_y, $dvb_active_rect_w, $dvb_active_rect_h, $dvb_active_rect_adj, $shade, 0, $stroke) . "\n";
+    $svg .= "\t\t" . svg_oval($dvb_main_circ_cx, $dvb_main_circ_cy, $dvb_main_circ_rx, $dvb_main_circ_ry, $dvb_main_circ_adj, -1, $stroke) . "\n";
     for (my $i = 0; $i < $dvb_main_circ_rep; $i++) {
-        $svg .= "\t\t" . svg_oval($dvb_main_circ_cx - ($i + 1) * 2 * $dvb_main_circ_rx, $dvb_main_circ_cy, $dvb_main_circ_rx, $dvb_main_circ_ry - $s2, $dvb_left_circ_adj, -1) . "\n";
-        $svg .= "\t\t" . svg_oval($dvb_main_circ_cx + ($i + 1) * 2 * $dvb_main_circ_rx, $dvb_main_circ_cy, $dvb_main_circ_rx, $dvb_main_circ_ry - $s2, $dvb_right_circ_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_oval($dvb_main_circ_cx - ($i + 1) * 2 * $dvb_main_circ_rx, $dvb_main_circ_cy, $dvb_main_circ_rx, $dvb_main_circ_ry - $s2, $dvb_left_circ_adj, -1, $stroke) . "\n";
+        $svg .= "\t\t" . svg_oval($dvb_main_circ_cx + ($i + 1) * 2 * $dvb_main_circ_rx, $dvb_main_circ_cy, $dvb_main_circ_rx, $dvb_main_circ_ry - $s2, $dvb_right_circ_adj, -1, $stroke) . "\n";
     }
-    $svg .= "\t\t" . svg_oval($dvb_left_circ_cx, $dvb_left_circ_cy, $dvb_left_circ_rx, $dvb_left_circ_ry, $dvb_left_circ_adj, -1) . "\n";
-    $svg .= "\t\t" . svg_oval($dvb_right_circ_cx, $dvb_right_circ_cy, $dvb_right_circ_rx, $dvb_right_circ_ry, $dvb_right_circ_adj, -1) . "\n";
+    $svg .= "\t\t" . svg_oval($dvb_left_circ_cx, $dvb_left_circ_cy, $dvb_left_circ_rx, $dvb_left_circ_ry, $dvb_left_circ_adj, -1, $stroke) . "\n";
+    $svg .= "\t\t" . svg_oval($dvb_right_circ_cx, $dvb_right_circ_cy, $dvb_right_circ_rx, $dvb_right_circ_ry, $dvb_right_circ_adj, -1, $stroke) . "\n";
     if (($dvb_lt_bar_w > 0) && ($dvb_lt_bar_h > 0)) {
-        $svg .= "\t\t" . svg_rect($dvb_lt_bar_x, $dvb_lt_bar_y, $dvb_lt_bar_w, $dvb_lt_bar_h, $dvb_lt_bar_adj, 100) . "\n";
+        $svg .= "\t\t" . svg_rect($dvb_lt_bar_x, $dvb_lt_bar_y, $dvb_lt_bar_w, $dvb_lt_bar_h, $dvb_lt_bar_adj, 100, 0, $stroke) . "\n";
     }
     if (($dvb_rb_bar_w > 0) && ($dvb_rb_bar_h > 0)) {
-        $svg .= "\t\t" . svg_rect($dvb_rb_bar_x, $dvb_rb_bar_y, $dvb_rb_bar_w, $dvb_rb_bar_h, $dvb_rb_bar_adj, 100) . "\n";
+        $svg .= "\t\t" . svg_rect($dvb_rb_bar_x, $dvb_rb_bar_y, $dvb_rb_bar_w, $dvb_rb_bar_h, $dvb_rb_bar_adj, 100, 0, $stroke) . "\n";
     }
     if ($b_sub) {
-        my $sub_area_h = $dvb_active_rect_h + (($bar_h < $height / 8) ? $bar_h : $height / 8);
+        my $sub_area_h = $dvb_active_rect_h + min($bar_h, $frame_h / 8);
         $svg .= "\t\t" . svg_text('Subtitles First Line', $dvb_active_rect_x, $dvb_active_rect_y + $sub_area_h * 6 / 8, $dvb_active_rect_w, $sub_area_h / 8, $dvb_active_rect_adj, $shade * 3) . "\n";
         $svg .= "\t\t" . svg_text('Subtitles Second Line', $dvb_active_rect_x, $dvb_active_rect_y + $sub_area_h * 7 / 8, $dvb_active_rect_w, $sub_area_h / 8, $dvb_active_rect_adj, $shade * 3) . "\n";
     }
+    if ($pad_bar_w > 0) {
+        $svg .= "\t\t" . svg_rect($pad_bar_w * -1, 0, $pad_bar_w, $frame_h, '', 75, 0, 0) . "\n";
+        $svg .= "\t\t" . svg_rect($frame_w, 0, $pad_bar_w, $frame_h, '', 75, 0, 0) . "\n";
+    }
+    if ($pad_bar_h > 0) {
+        $svg .= "\t\t" . svg_rect(0, $pad_bar_h * -1, $frame_w, $pad_bar_h, '', 75, 0, 0) . "\n";
+        $svg .= "\t\t" . svg_rect(0, $frame_h, $frame_w, $pad_bar_h, '', 75, 0, 0) . "\n";
+    }
+    if ($crop_bar_w > 0) {
+        $svg .= "\t\t" . svg_rect(0, 0, $crop_bar_w, $frame_h, '', 0, 0, 0) . "\n";
+        $svg .= "\t\t" . svg_rect($frame_w - $crop_bar_w, 0, $crop_bar_w, $frame_h, '', 0, 0, 0) . "\n";
+    }
+    if ($crop_bar_h > 0) {
+        $svg .= "\t\t" . svg_rect(0, 0, $frame_w, $crop_bar_h, '', 0, 0, 0) . "\n";
+        $svg .= "\t\t" . svg_rect(0, $frame_h - $crop_bar_h, $frame_w, $crop_bar_h, '', 0, 0, 0) . "\n";
+    }
     $svg .= svg_footer();
 }
 
@@ -488,8 +585,8 @@
     # Main Circle
     my $atsc_main_circ_ry = $cont_h / 2;
     my $atsc_main_circ_rx = $atsc_main_circ_ry * $par_y / $par_x;
-    my $atsc_main_circ_cx = $width / 2;
-    my $atsc_main_circ_cy = $height / 2 - ($b_top ? $bar_h : 0);
+    my $atsc_main_circ_cx = $frame_w / 2;
+    my $atsc_main_circ_cy = $frame_h / 2 - ($b_top ? $bar_h : 0);
     my $atsc_main_circ_adj = '';
     my $atsc_main_circ_rep = 0;
     
@@ -622,89 +719,89 @@
     # Left / Top Bar
     my $atsc_lt_bar_x = 0;
     my $atsc_lt_bar_y = 0;
-    my $atsc_lt_bar_w = ($bar_h > 0 ? ($b_top ? 0 : $width) : $bar_w);
-    my $atsc_lt_bar_h = ($bar_w > 0 ? $height : ($b_top ? 0 : $bar_h));
+    my $atsc_lt_bar_w = ($bar_h > 0 ? ($b_top ? 0 : $frame_w) : $bar_w);
+    my $atsc_lt_bar_h = ($bar_w > 0 ? $frame_h : ($b_top ? 0 : $bar_h));
     my $atsc_lt_bar_adj = '';
     
     # Right / Bottom Bar
     my $atsc_rb_bar_x = ($bar_w > 0 ? $bar_w + $cont_w : 0);
     my $atsc_rb_bar_y = ($bar_h > 0 ? ($b_top ? $cont_h : $bar_h + $cont_h) : 0);
-    my $atsc_rb_bar_w = ($bar_h > 0 ? $width : $bar_w);
-    my $atsc_rb_bar_h = ($bar_w > 0 ? $height : ($b_top ? $bar_h * 2 : $bar_h));
+    my $atsc_rb_bar_w = ($bar_h > 0 ? $frame_w : $bar_w);
+    my $atsc_rb_bar_h = ($bar_w > 0 ? $frame_h : ($b_top ? $bar_h * 2 : $bar_h));
     my $atsc_rb_bar_adj = '';
     
     # ATSC File Content
-    $svg = svg_header(-1);
-    $svg .= "\t\t" . svg_rect($atsc_active_rect_x, $atsc_active_rect_y, $atsc_active_rect_w, $atsc_active_rect_h, $atsc_active_rect_adj, 0) . "\n";
+    $svg = svg_header($view_x, $view_y, $view_w, $view_h, -1);
+    $svg .= "\t\t" . svg_rect($atsc_active_rect_x, $atsc_active_rect_y, $atsc_active_rect_w, $atsc_active_rect_h, $atsc_active_rect_adj, 0, 0, $stroke) . "\n";
     if (($atsc_lt_shade_w > 0) && ($atsc_lt_shade_h > 0)) {
-        $svg .= "\t\t" . svg_rect($atsc_lt_shade_x, $atsc_lt_shade_y, $atsc_lt_shade_w, $atsc_lt_shade_h, $atsc_lt_shade_adj, $shade) . "\n";
+        $svg .= "\t\t" . svg_rect($atsc_lt_shade_x, $atsc_lt_shade_y, $atsc_lt_shade_w, $atsc_lt_shade_h, $atsc_lt_shade_adj, $shade, 0, $stroke) . "\n";
     }
     if (($atsc_rb_shade_w > 0) && ($atsc_rb_shade_h > 0)) {
-        $svg .= "\t\t" . svg_rect($atsc_rb_shade_x, $atsc_rb_shade_y, $atsc_rb_shade_w, $atsc_rb_shade_h, $atsc_rb_shade_adj, $shade) . "\n";
+        $svg .= "\t\t" . svg_rect($atsc_rb_shade_x, $atsc_rb_shade_y, $atsc_rb_shade_w, $atsc_rb_shade_h, $atsc_rb_shade_adj, $shade, 0, $stroke) . "\n";
     }
-    $svg .= "\t\t" . svg_oval($atsc_main_circ_cx, $atsc_main_circ_cy, $atsc_main_circ_rx, $atsc_main_circ_ry, $atsc_main_circ_adj, -1) . "\n";
-    $svg .= "\t\t" . svg_oval($atsc_left_circ_cx, $atsc_left_circ_cy, $atsc_left_circ_rx, $atsc_left_circ_ry, $atsc_left_circ_adj, -1) . "\n";
-    $svg .= "\t\t" . svg_oval($atsc_right_circ_cx, $atsc_right_circ_cy, $atsc_right_circ_rx, $atsc_right_circ_ry, $atsc_right_circ_adj, -1) . "\n";
+    $svg .= "\t\t" . svg_oval($atsc_main_circ_cx, $atsc_main_circ_cy, $atsc_main_circ_rx, $atsc_main_circ_ry, $atsc_main_circ_adj, -1, $stroke) . "\n";
+    $svg .= "\t\t" . svg_oval($atsc_left_circ_cx, $atsc_left_circ_cy, $atsc_left_circ_rx, $atsc_left_circ_ry, $atsc_left_circ_adj, -1, $stroke) . "\n";
+    $svg .= "\t\t" . svg_oval($atsc_right_circ_cx, $atsc_right_circ_cy, $atsc_right_circ_rx, $atsc_right_circ_ry, $atsc_right_circ_adj, -1, $stroke) . "\n";
     if ($atsc_left149_line_y2 > 0) {
-        $svg .= "\t\t" . svg_vertline($atsc_left149_line_x, $atsc_left149_line_y1, $atsc_left149_line_y2, $atsc_left149_line_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_vertline($atsc_left149_line_x, $atsc_left149_line_y1, $atsc_left149_line_y2, $atsc_left149_line_adj, $stroke) . "\n";
     }
     if ($atsc_left149_circ_rx > 0) {
-        $svg .= "\t\t" . svg_oval($atsc_left149_circ_cx, $atsc_left149_circ_cy, $atsc_left149_circ_rx, $atsc_left149_circ_ry, $atsc_left149_circ_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_oval($atsc_left149_circ_cx, $atsc_left149_circ_cy, $atsc_left149_circ_rx, $atsc_left149_circ_ry, $atsc_left149_circ_adj, -1, $stroke) . "\n";
     }
     if ($atsc_right149_line_y2 > 0) {
-        $svg .= "\t\t" . svg_vertline($atsc_right149_line_x, $atsc_right149_line_y1, $atsc_right149_line_y2, $atsc_right149_line_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_vertline($atsc_right149_line_x, $atsc_right149_line_y1, $atsc_right149_line_y2, $atsc_right149_line_adj, $stroke) . "\n";
     }
     if ($atsc_right149_circ_rx > 0) {
-        $svg .= "\t\t" . svg_oval($atsc_right149_circ_cx, $atsc_right149_circ_cy, $atsc_right149_circ_rx, $atsc_right149_circ_ry, $atsc_right149_circ_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_oval($atsc_right149_circ_cx, $atsc_right149_circ_cy, $atsc_right149_circ_rx, $atsc_right149_circ_ry, $atsc_right149_circ_adj, -1, $stroke) . "\n";
     }
     if ($atsc_left169_line_y2 > 0) {
-        $svg .= "\t\t" . svg_vertline($atsc_left169_line_x, $atsc_left169_line_y1, $atsc_left169_line_y2, $atsc_left169_line_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_vertline($atsc_left169_line_x, $atsc_left169_line_y1, $atsc_left169_line_y2, $atsc_left169_line_adj, $stroke) . "\n";
     }
     if ($atsc_left169_circ_rx > 0) {
-        $svg .= "\t\t" . svg_oval($atsc_left169_circ_cx, $atsc_left169_circ_cy, $atsc_left169_circ_rx, $atsc_left169_circ_ry, $atsc_left169_circ_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_oval($atsc_left169_circ_cx, $atsc_left169_circ_cy, $atsc_left169_circ_rx, $atsc_left169_circ_ry, $atsc_left169_circ_adj, -1, $stroke) . "\n";
     }
     if ($atsc_right169_line_y2 > 0) {
-        $svg .= "\t\t" . svg_vertline($atsc_right169_line_x, $atsc_right169_line_y1, $atsc_right169_line_y2, $atsc_right169_line_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_vertline($atsc_right169_line_x, $atsc_right169_line_y1, $atsc_right169_line_y2, $atsc_right169_line_adj, $stroke) . "\n";
     }
     if ($atsc_right169_circ_rx > 0) {
-        $svg .= "\t\t" . svg_oval($atsc_right169_circ_cx, $atsc_right169_circ_cy, $atsc_right169_circ_rx, $atsc_right169_circ_ry, $atsc_right169_circ_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_oval($atsc_right169_circ_cx, $atsc_right169_circ_cy, $atsc_right169_circ_rx, $atsc_right169_circ_ry, $atsc_right169_circ_adj, -1, $stroke) . "\n";
     }
     if ($atsc_left6427_line_y2 > 0) {
-        $svg .= "\t\t" . svg_vertline($atsc_left6427_line_x, $atsc_left6427_line_y1, $atsc_left6427_line_y2, $atsc_left6427_line_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_vertline($atsc_left6427_line_x, $atsc_left6427_line_y1, $atsc_left6427_line_y2, $atsc_left6427_line_adj, $stroke) . "\n";
     }
     if ($atsc_left6427_circ_rx > 0) {
-        $svg .= "\t\t" . svg_oval($atsc_left6427_circ_cx, $atsc_left6427_circ_cy, $atsc_left6427_circ_rx, $atsc_left6427_circ_ry, $atsc_left6427_circ_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_oval($atsc_left6427_circ_cx, $atsc_left6427_circ_cy, $atsc_left6427_circ_rx, $atsc_left6427_circ_ry, $atsc_left6427_circ_adj, -1, $stroke) . "\n";
     }
     if ($atsc_right6427_line_y2 > 0) {
-        $svg .= "\t\t" . svg_vertline($atsc_right6427_line_x, $atsc_right6427_line_y1, $atsc_right6427_line_y2, $atsc_right6427_line_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_vertline($atsc_right6427_line_x, $atsc_right6427_line_y1, $atsc_right6427_line_y2, $atsc_right6427_line_adj, $stroke) . "\n";
     }
     if ($atsc_right6427_circ_rx > 0) {
-        $svg .= "\t\t" . svg_oval($atsc_right6427_circ_cx, $atsc_right6427_circ_cy, $atsc_right6427_circ_rx, $atsc_right6427_circ_ry, $atsc_right6427_circ_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_oval($atsc_right6427_circ_cx, $atsc_right6427_circ_cy, $atsc_right6427_circ_rx, $atsc_right6427_circ_ry, $atsc_right6427_circ_adj, -1, $stroke) . "\n";
     }
     if ($atsc_leftfull_line_y2 > 0) {
-        $svg .= "\t\t" . svg_vertline($atsc_leftfull_line_x, $atsc_leftfull_line_y1, $atsc_leftfull_line_y2, $atsc_leftfull_line_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_vertline($atsc_leftfull_line_x, $atsc_leftfull_line_y1, $atsc_leftfull_line_y2, $atsc_leftfull_line_adj, $stroke) . "\n";
     }
     if ($atsc_leftfull_circ_rx > 0) {
-        $svg .= "\t\t" . svg_oval($atsc_leftfull_circ_cx, $atsc_leftfull_circ_cy, $atsc_leftfull_circ_rx, $atsc_leftfull_circ_ry, $atsc_leftfull_circ_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_oval($atsc_leftfull_circ_cx, $atsc_leftfull_circ_cy, $atsc_leftfull_circ_rx, $atsc_leftfull_circ_ry, $atsc_leftfull_circ_adj, -1, $stroke) . "\n";
     }
     if ($atsc_rightfull_line_y2 > 0) {
-        $svg .= "\t\t" . svg_vertline($atsc_rightfull_line_x, $atsc_rightfull_line_y1, $atsc_rightfull_line_y2, $atsc_rightfull_line_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_vertline($atsc_rightfull_line_x, $atsc_rightfull_line_y1, $atsc_rightfull_line_y2, $atsc_rightfull_line_adj, $stroke) . "\n";
     }
     if ($atsc_rightfull_circ_rx > 0) {
-        $svg .= "\t\t" . svg_oval($atsc_rightfull_circ_cx, $atsc_rightfull_circ_cy, $atsc_rightfull_circ_rx, $atsc_rightfull_circ_ry, $atsc_rightfull_circ_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_oval($atsc_rightfull_circ_cx, $atsc_rightfull_circ_cy, $atsc_rightfull_circ_rx, $atsc_rightfull_circ_ry, $atsc_rightfull_circ_adj, -1, $stroke) . "\n";
     }
     for (my $i = 0; $i < $atsc_main_circ_rep; $i++) {
-        $svg .= "\t\t" . svg_oval($atsc_leftfull_circ_cx + $atsc_leftfull_circ_rx + ($i * 2 + 1) * $atsc_main_circ_rx, $atsc_main_circ_cy, $atsc_main_circ_rx, $atsc_main_circ_ry - $s2, $atsc_leftfull_circ_adj, -1) . "\n";
-        $svg .= "\t\t" . svg_oval($atsc_rightfull_circ_cx - $atsc_rightfull_circ_rx - ($i * 2 + 1) * $atsc_main_circ_rx, $atsc_main_circ_cy, $atsc_main_circ_rx, $atsc_main_circ_ry - $s2, $atsc_rightfull_circ_adj, -1) . "\n";
+        $svg .= "\t\t" . svg_oval($atsc_leftfull_circ_cx + $atsc_leftfull_circ_rx + ($i * 2 + 1) * $atsc_main_circ_rx, $atsc_main_circ_cy, $atsc_main_circ_rx, $atsc_main_circ_ry - $s2, $atsc_leftfull_circ_adj, -1, $stroke) . "\n";
+        $svg .= "\t\t" . svg_oval($atsc_rightfull_circ_cx - $atsc_rightfull_circ_rx - ($i * 2 + 1) * $atsc_main_circ_rx, $atsc_main_circ_cy, $atsc_main_circ_rx, $atsc_main_circ_ry - $s2, $atsc_rightfull_circ_adj, -1, $stroke) . "\n";
     }
     if (($atsc_lt_bar_w > 0) && ($atsc_lt_bar_h > 0)) {
-        $svg .= "\t\t" . svg_rect($atsc_lt_bar_x, $atsc_lt_bar_y, $atsc_lt_bar_w, $atsc_lt_bar_h, $atsc_lt_bar_adj, 100) . "\n";
+        $svg .= "\t\t" . svg_rect($atsc_lt_bar_x, $atsc_lt_bar_y, $atsc_lt_bar_w, $atsc_lt_bar_h, $atsc_lt_bar_adj, 100, 0, $stroke) . "\n";
     }
     if (($atsc_rb_bar_w > 0) && ($atsc_rb_bar_h > 0)) {
-        $svg .= "\t\t" . svg_rect($atsc_rb_bar_x, $atsc_rb_bar_y, $atsc_rb_bar_w, $atsc_rb_bar_h, $atsc_rb_bar_adj, 100) . "\n";
+        $svg .= "\t\t" . svg_rect($atsc_rb_bar_x, $atsc_rb_bar_y, $atsc_rb_bar_w, $atsc_rb_bar_h, $atsc_rb_bar_adj, 100, 0, $stroke) . "\n";
     }
     if ($b_sub) {
-        my $sub_area_h = $atsc_active_rect_h + (($bar_h < $height / 8) ? $bar_h : $height / 8);
+        my $sub_area_h = $atsc_active_rect_h + min($bar_h, $frame_h / 8);
         $svg .= "\t\t" . svg_text('Subtitles First Line', $atsc_active_rect_x, $atsc_active_rect_y + $sub_area_h * 6 / 8, $atsc_active_rect_w, $sub_area_h / 8, $atsc_active_rect_adj, $shade * 3) . "\n";
         $svg .= "\t\t" . svg_text('Subtitles Second Line', $atsc_active_rect_x, $atsc_active_rect_y + $sub_area_h * 7 / 8, $atsc_active_rect_w, $sub_area_h / 8, $atsc_active_rect_adj, $shade * 3) . "\n";
     }
