+my $x = 30.00; # mm
+my $y = 30.00; # mm
+
+if ( $opt_svg ) {
+ print qq{<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<svg
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
+ xmlns:cc="http://creativecommons.org/ns#"
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:svg="http://www.w3.org/2000/svg"
+ xmlns="http://www.w3.org/2000/svg"
+ xmlns:xlink="http://www.w3.org/1999/xlink"
+ id="svg8"
+ version="1.1"
+ viewBox="0 0 210 297"
+ height="297mm"
+ width="210mm">
+
+
+<g id="layer1">
+
+ }; # svg, insert rest of rect
+
+ print qq{<rect x="0" y="0" width="210" height="297" style="fill:#000000" id="high-contrast"/>} if $opt_invert;
+}
+
+my @later;
+
+my $cols = { # foreground background
+ txt => [ '#000000', '#ffffff' ],
+ pins => [ '#ffffff', '#ff00ff' ],
+ vcc => [ '#ff0000', '#ffff00' ],
+ gnd => [ '#000000', '#00ffff' ],
+ i2c => [ '#008800', '#ffcccc' ],
+ serial=>[ '#000088', '#ccffcc' ],
+ spi => [ '#880000', '#ccccff' ],
+};
+
+sub swap_cols {
+ my $swap = shift;
+ die "$swap not found in ",dump($cols) unless $cols->{$swap};
+ my ( $c1, $c2 ) = @{ $cols->{$swap} };
+ $cols->{$swap} = [ $c2, $c1 ];
+}
+
+swap_cols 'txt' if $opt_invert;
+
+
+sub svg_style {
+ my ($name,$x,$y,$col) = @_;
+ $y -= $font_b; # shift box overlay to right vertical position based on font baseline
+
+ sub rect {
+ my ($x,$y,$col,$fill) = @_;
+ print qq{<rect x="$x" y="$y" height="2.54" width="}, $max_len[$col] * $font_w, qq{" style="fill:$fill;stroke:#ffffff;stroke-width:0.10" />\n};
+
+ }
+
+ if ( $name =~ m/^(\d+)$/ ) { # pins
+ my $pin = $1;
+ my ( $fg, $bg ) = @{ $cols->{pins} };
+ if ( $pin == 1 ) {
+ my $w = $max_len[$col]*$font_w - 0.1;
+ my $cx = $x + $w;
+ my $cy = $y + 2.54;
+ #print qq{<polygon points="$x,$y $cx,$y $x,$cy $x,$y" stroke="$fg" stroke-width="0.25" fill="$bg" />};
+ #print qq{<polygon points="$x,$cy $cx,$cy $cx,$y $x,$cy" stroke="$bg" stroke-width="0.25" fill="$fg" />};
+ print qq{<rect x="$x" y="$y" width="$w" height="2.54" stroke="$fg" stroke-width="0.3" fill="$bg" />};
+ my ( $fg, $bg ) = @{ $cols->{txt} };
+ print qq{<rect x="$x" y="$y" width="$w" height="2.54" rx="1" ry="1" stroke="$fg" stroke-width="0.3" fill="$bg" />};
+ } else {
+ rect $x,$y,$col,$fg;
+ }
+ return qq{ style="fill:$bg"};
+ }
+
+ if ( $name =~ m/(VCC|3V3|3.3V|5v)/i ) {
+ my ($fg,$bg) = @{ $cols->{vcc} };
+ rect $x,$y,$col,$bg;
+ return qq{ style="fill:$fg"};
+ } elsif ( $name =~ m/(G(ND|Round)|VSS|0v)/i ) {
+ my ($fg,$bg) = @{ $cols->{gnd} };
+ rect $x,$y,$col,$bg;
+ return qq{ style="fill:$fg"};
+ } elsif ( $name =~ m/\[(\w+)/ ) { # kernel
+ my $dev = $1;
+ my ($fg,$bg) = @{ $cols->{txt} };
+ ($fg,$bg) = @{ $cols->{$dev} } if exists $cols->{$dev};
+ rect $x,$y,$col,$bg;
+ return qq{ style="fill:$fg"};
+ } else {
+ my ( $fg, $bg ) = @{ $cols->{txt} };
+ rect $x,$y,$col,$bg;
+ #return qq{ style="fill:$fg"};
+ return '';
+ }
+}
+
+my $alt_col = 0;
+
+my @cols_order = ( 0,1,2,3 );
+my @cols_align = ( '','-','','-' ); # sprintf prefix
+
+@cols_order = ( 0,1,3,2 ); # pins outside on the right
+@cols_align = ( '','-','-','' );
+
+
+# cut marks
+my ($fg,$bg) = @{ $cols->{txt} };
+my $line_fmt = qq{<line x1="%s" y1="%s" x2="%s" y2="%s" style="stroke:$fg;stroke-width:0.10;fill:$bg" />\n};
+
+my @cut_marks;
+sub cut_mark {
+ my ($x,$y) = @_;
+ return unless $opt_svg;
+ push @cut_marks, sprintf($line_fmt, $x-5, $y-$font_b, $x+5, $y-$font_b);
+ push @cut_marks, sprintf($line_fmt, $x, $y-$font_b-5, $x, $y-$font_b+5);
+}
+cut_mark $x, $y;
+my $max_x = $x;
+$max_x += $max_len[$_] * $font_w foreach ( 0 .. 3 );
+cut_mark $max_x, $y;
+
+sub line {
+ my ($x,$y,$max_x) = @_;
+ push @cut_marks, sprintf($line_fmt, $x, $y-$font_b, $max_x, $y-$font_b);
+}
+
+
+my $last_cut_mark = 0;
+
+foreach my $i ( 0 .. $#line_parts ) {
+ $i = $#line_parts - $i if $opt_vertical;
+ my $line = $line_parts[$i];
+
+ if ( $opt_svg ) {
+
+ if ( ! exists $line->[0] ) {
+ # before first empty line
+ if ( $last_cut_mark == 0 ) {
+ cut_mark $x, $y;
+ cut_mark $max_x, $y;
+ $last_cut_mark = 1;
+ line $x, $y, $max_x if $opt_lines;
+ $y += 15; # make spacing between pinouts
+ }
+ } elsif ( $last_cut_mark ) {
+ # first full line
+ cut_mark $x, $y;
+ cut_mark $max_x, $y;
+ $last_cut_mark = 0;
+ } else {
+ #warn "CUTMARK no magic";
+ }
+
+ line $x, $y, $max_x if $opt_lines && exists $line->[1];
+
+ my ($fg,$bg) = @{ $cols->{txt} };
+ my $tspan = qq{<tspan x="$x" y="$y" style="line-height:2.54;fill:$fg;stroke:none;">\n};
+
+ my $x_pos = $x;
+ foreach my $i ( @cols_order ) {
+ next unless $line->[$i];
+ my $text_anchor = 'middle';
+ my $x2 = $x_pos + ( $max_len[$i] * $font_w ) / 2;
+ $tspan .= qq{<tspan x="$x2" text-anchor="$text_anchor"}.svg_style($line->[$i],$x_pos,$y,$i).sprintf( '>%' . $cols_align[$i] . $max_len[$i] . 's</tspan>', $line->[$i]);
+ $x_pos += $max_len[$i] * $font_w;
+ }
+
+ $tspan .= qq{</tspan>\n};
+ push @later,sprintf $tspan, @$line;
+ $y += 2.54;
+
+ # swap pin colors for line stripe
+ if ( $opt_zebra ) {
+ swap_cols $_ foreach qw( pins txt );
+ }
+