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

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

# We generate a physical indication of which commit was used.
#
# We can generate three 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
#
#   Small4
#   Small6
#   Small8
#       git-rev-list --first-parent --count HEAD
#       git-rev-parse HEAD
#       eg
#            Small6     1070
#                     82f2a2

sub parsefont () {
    my %cellmap;
    while (<DATA>) {
	last if %cellmap && !m/\S/;
	next unless m/\S/;
	chomp;
	s{^(.) }{};
	$cellmap{$1} = $_;
    }
    my %chrpolys;
    while (<DATA>) {
	next unless m/\S/;
	my @chrs = split / /, $_;
	<DATA> !~ m/\S/ or die;
	foreach my $row (0..4) {
	    $_ = <DATA>;
	    chomp;
	    s{^}{ };
	    $_ .= ' ' x (@chrs * 4);
	    m{\S/\S} and die;
	    s{/(?=\s)}{L}g;
	    s{/(?=\S)}{r}g;
	    s{\\(?=\s)}{l}g;
	    s{\\(?=\S)}{R}g;
	    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 '%x', $col*2 + $& /ge;
			$f =~ s/\d\b/ sprintf '%x', $row*2 + $& /ge;
			push @{ $chrpolys{$chr} }, [ split / /, $f ];
		    } else {
			die "$_ ?";
		    }
		}
	    }
	    die "$_ ?" if m{\S};
	}    
    }
		    
    use Data::Dumper;
    print Dumper(\%chrpolys);
}

parsefont();

__DATA__

# 00 20 22 02
l 00 20 22
r 00 20 02
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

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

a b c d e f

    #         #     /##
/## ##\ /## /## /## #
# # # # #   # # # # ###
# # # # #   # # #/  #
\## ##/ \## \## ### #

