#!/usr/bin/perl -w
use strict;

$SIG{__WARN__} = sub { die @_; };

#  xxx much of the comment below is TODO

# We generate a physical indication of which commit was used.
#
# We provide for scaling factors with dynamic variables:
#    $Commitid_pixelsz        if not set, we use 0.4 } multiplied
#    $Commitid_scale          if not set, we use 1.0 }  together
#    $Commitid_depth          if not set, we use xy pixel size from above
#    $Commitid_depth_scale    if not set, we use 1.0 (multiplies depth above)
#
# For each form we have
#
#    module Commitid_Form_2D(.4)
#    module Commitid_Form()
#
# And we provide
#
#   function Commitid_pixelsz()    // $Commitid_pixelsz * $Commitid_scale
#   function Commitid_depth()      // see above
#
# We can generate these forms:
#
#   Tiny3:
#   Tiny4:
#   Tiny5:
#   Tiny6:
#   Tiny7:
#   Tiny8:
#       git rev-list --first-parent --count HEAD
#       typically 3-4 characters but we allow for up to 6
#       eg
#            Tiny4    1070
#
#   Tiny4Q:
#   Tiny6Q:
#   Tiny9Q:
#       same but in two lines eg
#            Tiny4Q   10
#                     70
#
#   Git4
#   Git6
#   Git8
#       git-rev-parse HEAD
#       eg
#            Git6    82f2a2

#   Small4
#   Small6
#   Small8
#       git-rev-list --first-parent --count HEAD
#       git-rev-parse HEAD
#       eg
#            Small6     1070
#                     82f2a2

sub p { print @_ or die $!; }

p <<'END';
function Commitid_pixelsz() =
  ($Commitid_pixelsz       ? $Commitid_pixelsz       : 0.4) *
  ($Commitid_scale         ? $Commitid_scale         : 1.0);
function Commitid_depth() =
  ($Commitid_depth         ? $Commitid_depth         : Commitid_pixelsz()) *
  ($Commitid_depth_scale   ? $Commitid_depth_scale   : 1.0);
function Commitid__scale() =
  Commitid_pixelsz() / 0.1;
END

sub chrmodname ($) {
    my ($chr) = @_;
    my $chrx = sprintf '%#x', ord $chr;
    return "Commitid__chr_$chrx";
}

sub gentextmodule ($@) {
    my ($modulename, @lines) = @_;
    p "module $modulename(){\n";
    p " scale(Commitid__scale()){\n";
    my $y = @lines;
    foreach my $line (@lines) {
	$y--;
	my $x = 0;
	foreach my $chr (split //, $line) {
	    next if $chr !~ m/\S/;
	    p sprintf "  translate([%d * 0.8, %d * 1.2]) %s();\n",
		$x, $y, chrmodname $chr;
	    $x++;
	}
    }
    p " }\n";
    p "}\n";
}

our @demo;

sub parsefont () {
    my %cellmap;
    for (;;) {
	$_ = <DATA> // die;
	last if %cellmap && !m/\S/;
	next unless m/\S/;
	chomp;
	s{^(.) }{};
	$cellmap{$1} = $_;
    }
    my %chrpolys;
    while (<DATA>) {
	next unless m/\S/;
	chomp;
	my @chrs = split / /, $_;
	<DATA> !~ m/\S/ or die;
	foreach my $row (reverse 0..4) {
	    $_ = <DATA>;
	    chomp;
	    s{^}{ };
	    $_ .= ' ' x 8;
	    m{\S/\S} and die;
	    s{/(?=\s)}{L}g;
	    s{/(?=\S)}{r}g;
	    s{\\(?=\s)}{l}g;
	    s{\\(?=\S)}{R}g;
	    p "// $_\n";
	    foreach my $chr (@chrs) {
		s{^ }{} or die "$chr $_ ?";
		foreach my $col (0..2) {
		    my @verts;
		    if (s{^ }{}) {
		    } elsif (s{^\S}{}) {
			my $f = $cellmap{$&};
			die unless $f;
			$f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
			$f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
			push @{ $chrpolys{$chr} }, [ split / /, $f ];
		    } else {
			die "$_ ?";
		    }
		}
	    }
	    die "$_ ?" if m{\S};
	}    
    }

    my $demo = '';
    my $democols = 6;
    foreach my $chr (sort keys %chrpolys) {
	my $mod = chrmodname $chr;
	p "module $mod () {\n";
	foreach my $poly (@{ $chrpolys{$chr} }) {
	    p " polygon([";
	    my $delim = "";
	    foreach my $pt (@$poly) {
		p $delim;
		$pt =~ s{\d{5}}{$&,};
		$pt =~ s{\b\d}{$&.}g;
		p "[$pt]";
		$delim = ',';
	    }
	    p "]);\n";
	}
	p "}\n";
	$demo .= $chr;
    }
    @demo = reverse $demo =~ m{.{1,$democols}}go;
}

parsefont();

gentextmodule('Commitid_FontDemo', @demo);

__DATA__

# 00 20 22 02
l 00 20 02
r 00 20 22
L 00 22 02
R 20 22 02
> 00 20 22 02 11
< 00 20 11 22 02

0 1 2 3 4 5 6 7 8 9

/#\  r  /#\ ##\ # # ### //  ### /#\ /#\
# # /#    #   # # # #   #     # # # # #
# #  #  /#/ ##< \## ##\ ##\  // >#< \##
# #  #  #     #   #   # # #  #  # #   #
\#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/

a b c d e f

    #         #     /##
    #   /##   # /#\ #
/## ##\ #   /## #r# ###
# # # # #   # # #/  #
\## ##/ \## \## \#/ #

