+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} };
+ $dev = 'serial' if $dev =~ m/^tty/;
+ ($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
+
+my @cols_shuffle = @cols_order;
+
+if ( $opt_edge ) {
+ # pins outside on the right
+ @cols_shuffle = ( 0,1,3,2 ) if $opt_edge;
+ @cols_align = ( '-','-','','' );
+} elsif ( $opt_middle ) {
+ # pins in middle
+ @cols_shuffle = ( 1,0,2,3 );
+ @cols_align = ( '','','-','-' );
+}
+
+sub cols_shuffle {
+ my ( $what, $order ) = @_;
+ my $new = [];
+ foreach my $i ( 0 .. $#$what ) {
+ $new->[$i] = $what->[ $order->[$i] ];
+ }
+ warn "# cols_shuffle what=",dump($what)," order=",dump($order)," new=",dump($new);
+ return @$new;
+}
+
+@cols_order = cols_shuffle( \@cols_order, \@cols_shuffle );
+@max_len = cols_shuffle( \@max_len, \@cols_shuffle );
+
+warn "# cols_order = ",dump( \@cols_order );
+warn "# cols_align = ",dump( \@cols_align );
+
+my $fmt = "%$cols_align[0]$max_len[0]s %$cols_align[1]$max_len[1]s %$cols_align[2]$max_len[2]s %$cols_align[3]$max_len[3]s\n";
+
+
+# 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 ( 0 .. $#cols_order ) {
+ my $order = $cols_order[$i];
+ next unless $line->[$order];
+ 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->[$order],$x_pos,$y,$i).sprintf( '>%' . $cols_align[$i] . $max_len[$i] . 's</tspan>', $line->[$order]);
+ $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 );
+ }
+
+ } else {
+
+ if ( $#$line == 0 ) {
+ print $line->[0], "\n";
+ } else {
+ push @$line, '' while ($#$line < 3); # fill-in single row header
+ printf $fmt, map { $line->[$_] } @cols_order;
+ }
+
+ }
+}
+
+if ( $opt_svg ) {
+ cut_mark $x,$y;
+ cut_mark $max_x,$y;
+ line $x, $y, $max_x if $opt_lines;
+
+ print qq{
+ <text
+ id="text4506"
+ y="$x"
+ x="$y"
+ style="font-size:2.34px;line-height:2.54px;font-family:'Andale Mono';stroke:none"
+ xml:space="preserve">
+
+ }; #svg
+
+ print @later, qq{</text>\n}, @cut_marks, qq{</g>\n</svg>};
+
+}