+ @demo = reverse $demo =~ m{.{1,$democols}}go;
+}
+
+parsefont();
+
+our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
+our $do_git_untracked = 1;
+our $argcounter;
+
+our @forms;
+
+sub rjustt ($$;$) { # right justify and truncate (ie, pad and truncate at left)
+ # always includes prefix
+ my ($sz, $whole, $prefix) = @_;
+ $prefix //= '';
+ my $lw = length $whole;
+ my $spare = $sz - $lw - (length $prefix);
+ return
+ ($spare > 0 ? (' ' x $spare) : '').
+ $prefix.
+ substr($whole, ($spare < 0 ? -$spare : 0));
+}
+
+sub ljustt ($$;$) {
+ my ($sz, $whole, $suffix) = @_;
+ $suffix //= '';
+ $sz -= length $suffix;
+ return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
+}
+
+sub genform ($@) {
+ my ($form, @lines) = @_;
+ gentextmodule($form, @lines);
+ my $f = {
+ Form => $form,
+ Chars => (length join '', @lines),
+ Lines => (scalar @lines),
+ Ambiguous => ($form =~ m/Full/ && !grep { m/\W/ } @lines),
+ };
+ push @forms, $f;
+}
+
+sub genform_q ($$$) {
+ my ($form, $s, $lines) = @_;
+ $gtm_demo_j++;
+ my $l = length $s;
+ return if $l % $lines;
+ my $e = $l/$lines;
+ return if $e < 2;
+ $gtm_demo_j--;
+ genform($form, $s =~ m/.{$e}/g);
+}
+
+sub genform_plusq ($$) {
+ my ($form, $s) = @_;
+ genform($form, $s);
+ genform_q("${form}S", $s, 2);
+ genform_q("${form}T", $s, 3);
+}
+
+our @gcmd;
+
+sub gitrun_start () {
+ open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
+}
+
+sub gitrun_done (;$) {
+ my ($errok) = @_;
+ $?=0; $!=0;
+ return if close F;
+ return if $errok;
+ die $! if $!;
+ die "@gcmd failed ($?)\n";
+}
+
+sub gitoutput (@) {
+ (@gcmd) = (qw(git), @_);
+ gitrun_start;
+ $_ = <F>;
+ gitrun_done;
+ defined or die "@gcmd produced no output";
+ chomp or die "@gcmd produced no final newline";
+ $_;
+}
+
+sub do_git () {
+ return unless $do_git;
+
+ @gcmd = qw(git status --porcelain);
+ push @gcmd, qw(--untracked=no) unless $do_git_untracked;
+
+ my $git_dirty = '';
+ gitrun_start;
+ while (<F>) {
+ if (m/^\?\?/ && $do_git_untracked) {
+ $git_dirty = '+';
+ next;
+ }
+ $git_dirty = '*';
+ last;
+ }
+ gitrun_done($git_dirty eq '*');
+
+ my $git_count;
+ my $git_object;
+
+ if ($do_git =~ m/c/) {
+ $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
+ }
+ if ($do_git =~ m/o/) {
+ $git_object = gitoutput qw(rev-parse HEAD);
+ }
+
+ foreach my $sz (2..10) {
+ gentextmodule_demo_start_batch();
+
+ if (defined($git_count)) {
+ my $smallstr = rjustt($sz, $git_count, $git_dirty);
+ if (defined($git_object) && $sz >= length($git_count) + 3) {
+ $smallstr = $git_object;
+ $smallstr .= ($git_dirty || ' ');
+ $smallstr .= $git_count;
+ $smallstr = rjustt($sz, $smallstr);
+ }
+ genform_plusq("Small$sz", $smallstr);
+ }
+
+ genform_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
+ if defined $git_object;
+
+ if (defined $git_count && defined $git_object) {
+ genform("Full".($sz*2),
+ ljustt($sz, $git_object),
+ rjustt($sz, $git_count, $git_dirty));
+
+ my $e = $sz;
+ genform("Full".($e*3)."T",
+ ljustt($e*2, $git_object, $git_dirty)
+ =~ m/.{$e}/g,
+ rjustt($e, $git_count));
+ }
+ }
+}
+
+sub do_some_best ($$) {
+ my ($modname, $formre) = @_;
+ my $fullmodname = "Commitid_${modname}_2D";
+ my @argl = qw(max_sz margin=Commitid_pixelsz());
+ p "module $fullmodname(".(join ', ', @argl).") {\n";
+ my $mbs = '$Commitid_max_best_scale';
+ p " sc_max = $mbs ? $mbs : 2;\n";
+ p " sz = max_sz - 2*[margin,margin];\n";
+ my @do;
+ foreach my $f (
+ sort {
+ $b->{Chars} <=> $a->{Chars} or
+ $a->{Lines} <=> $b->{Chars}
+ }
+ grep {
+ $_->{Form} =~ m/$formre/ &&
+ !$_->{Ambiguous}
+ }
+ @forms
+ ) {
+ my $form = $f->{Form};
+ p " sz_$form = Commitid_${form}_sz();\n";
+ foreach my $rot (qw(0 1)) {
+ my $id = "${form}_r${rot}";
+ p " sc_$id = min(sc_max";
+ foreach my $xy (qw(0 1)) {
+ p ",sz[$xy]/sz_$form","[",(($xy xor $rot)+0),"]";
+ }
+ p ");\n";
+ push @do, " if (sc_$id >= 1.0";
+ push @do, " && sc_$id >= sc_${form}_r1" if !$rot;
+ push @do, ") {\n";
+ push @do, " translate([margin,margin]) scale(sc_$id)\n";
+ push @do, " rotate(90) translate([0,-sz_$form"."[1]])\n" if $rot;
+ push @do, " Commitid_${form}_2D();\n";
+ push @do, " } else";
+ }
+ }
+ push @do, <<END;
+ {
+ echo("$fullmodname could not fit anything in", max_sz);
+ }
+END
+ p $_ foreach @do;