chiark / gitweb /
commitid.scad.pl: Rename Small to Full
[reprap-play.git] / commitid.scad.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 $SIG{__WARN__} = sub { die @_; };
5
6 #  xxx much of the comment below is TODO
7 #
8 # Usage:
9 #
10 #   .../commitid.scad.pl [OPTION...] [STRING...] >commitid.scad.new \
11 #     && mv -f commitid.scad.new commitid.scad
12 #
13 # Options:
14 #
15 #   --git    generate git commit indications, as shown below
16 #            (this is the default if no strings are requested with -t)
17 #
18 #   --git=object
19 #            generate git commit indication based on commit object only
20 #            (ie avoid counting commits)
21 #
22 #   -i       do not generate `+' if git-untracked files are present
23 #
24 #   [-t[FORM]] TEXT
25 #            generate a form FORM containing TEXT
26 #            TEXT can contain newlines (final newline usually undesirable)
27 #            if FORM not specified, generates Arg0 Arg1 Arg2 in sequence
28 #            character set is SPC 0-9 a-f + * (`*' glyph is `=/=').
29 #
30 # We generate a physical indication of which commit was used.
31 #
32 # We provide for scaling factors with dynamic variables:
33 #    $Commitid_pixelsz        if not set, we use 0.8 } multiplied
34 #    $Commitid_scale          if not set, we use 1.0 }  together
35 #    $Commitid_depth          if not set, we use xy pixel size from above / 2
36 #    $Commitid_depth_scale    if not set, we use 1.0 (multiplies depth above)
37 #
38 # For each form we have
39 #
40 #    module Commitid_Form_2D() { ... }
41 #    module Commitid_Form() { ... }
42 #    function Commitid_Form_sz()     => [ x, y ]
43 #
44 #  These have their origin in the bottom left corner.  The 3D model
45 #  is a positive, has its origin halfway through, and is twice the
46 #  depth in height, so it can be added or subtracted.
47 #
48 # And we provide
49 #
50 #   function Commitid_pixelsz()    // $Commitid_pixelsz * $Commitid_scale
51 #   function Commitid_depth()      // see above
52 #
53 # We can generate these forms:
54 #
55 #   Tiny3:
56 #   Tiny4:
57 #   Tiny5:
58 #   Tiny6:
59 #   Tiny7:
60 #   Tiny8:
61 #       git rev-list --first-parent --count HEAD
62 #       typically 3-4 characters but we allow for up to 6
63 #       padded with zeroes; if too long we reduce mod 10^n
64 #       eg
65 #            Tiny4    1070
66 #       If tree is dirty, + or * is suffixed, reducing number of
67 #       digits by 1.
68 #
69 #   Tiny4Q:
70 #   Tiny6Q:
71 #   Tiny9Q:
72 #       same but in two lines eg
73 #            Tiny4Q   10
74 #                     70
75 #
76 #   Git4   Git4Q
77 #   Git6   Git6Q
78 #   Git8   Git8Q
79 #       git-rev-parse HEAD   (prefix of requested length)
80 #       eg
81 #            Git6    82f2a2
82 #       If tree is dirty, + or * is suffixed to commitid,
83 #       reducing number of hex digits by 1.
84
85 #   Full3
86 #   Full4
87 #   Full5
88 #   Full6
89 #   Full7
90 #   Full8
91 #   Full9
92 #   Full10
93 #       git-rev-list --first-parent --count HEAD
94 #       git-rev-parse HEAD
95 #       eg
96 #            Full6      1070
97 #                     82f2a2
98 #       If tree is dirty, + or * is suffixed to count (but not to
99 #       commitid) reducing number of digits by 1.
100 #
101 #   FontDemo
102 #
103 #   Arg0, Arg1, ...
104 #       Strings passed on command line
105
106 sub p { print @_ or die $!; }
107
108 p <<'END';
109 // *** AUTOGENERATED - DO NOT EDIT *** //
110 function Commitid_pixelsz() =
111   ($Commitid_pixelsz       ? $Commitid_pixelsz       : 0.8) *
112   ($Commitid_scale         ? $Commitid_scale         : 1.0);
113 function Commitid_depth() =
114   ($Commitid_depth         ? $Commitid_depth         : Commitid_pixelsz()/2) *
115   ($Commitid_depth_scale   ? $Commitid_depth_scale   : 1.0);
116 function Commitid__scale() =
117   Commitid_pixelsz() / 0.2;
118 END
119
120 sub chrmodname ($) {
121     my ($chr) = @_;
122     my $chrx = sprintf '%#x', ord $chr;
123     return "Commitid__chr_$chrx";
124 }
125
126 sub gentextmodule ($@) {
127     my ($form, @lines) = @_;
128     my $modb = "Commitid_$form";
129     p "module ${modb}_2D(){\n";
130     p " // |$_|\n" foreach @lines;
131     p " scale(Commitid__scale()){\n";
132     my $y = @lines;
133     my $cols = 1;
134     foreach my $line (@lines) {
135         $y--;
136         my $x = 0;
137         foreach my $chr (split //, $line) {
138             next if $chr !~ m/\S/;
139             p sprintf "  translate([%d * 0.8, %d * 1.2]) %s();\n",
140                 $x, $y, chrmodname $chr;
141             $x++;
142         }
143         $cols = $x if $x > $cols;
144     }
145     p " }\n";
146     p "}\n";
147     p "module ${modb}(){\n";
148     p " d=Commitid_depth();\n";
149     p " translate([0,0,-d]) linear_extrude(height=d*2) ${modb}_2D();\n";
150     p "}\n";
151     p sprintf "function %s_sz() = Commitid__scale() * 0.1 * [ %d, %d ];\n",
152         $modb, 2 * ($cols * 4 - 1), 2 * (@lines * 6 - 1);
153 }
154
155 our @demo;
156
157 sub parsefont () {
158     my %cellmap;
159     for (;;) {
160         $_ = <DATA> // die;
161         last if %cellmap && !m/\S/;
162         next unless m/\S/;
163         chomp;
164         s{^(.) }{};
165         $cellmap{$1} = $_;
166     }
167     my %chrpolys;
168     while (<DATA>) {
169         next unless m/\S/;
170         chomp;
171         my @chrs = split / /, $_;
172         <DATA> !~ m/\S/ or die;
173         foreach my $row (reverse 0..4) {
174             $_ = <DATA>;
175             chomp;
176             s{^}{ };
177             $_ .= ' ' x 8;
178             m{\S/\S} and die;
179             s{/(?=\s)}{L}g;
180             s{/(?=\S)}{r}g;
181             s{\\(?=\s)}{l}g;
182             s{\\(?=\S)}{R}g;
183             p "// $_\n";
184             foreach my $chr (@chrs) {
185                 s{^ }{} or die "$chr $_ ?";
186                 foreach my $col (0..2) {
187                     my @verts;
188                     if (s{^ }{}) {
189                     } elsif (s{^\S}{}) {
190                         my $f = $cellmap{$&};
191                         die unless $f;
192                         $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
193                         $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
194                         push @{ $chrpolys{$chr} }, [ split / /, $f ];
195                     } else {
196                         die "$_ ?";
197                     }
198                 }
199             }
200             die "$_ ?" if m{\S};
201         }    
202     }
203
204     my $demo = '';
205     my $democols = 6;
206     foreach my $chr (sort keys %chrpolys) {
207         my $mod = chrmodname $chr;
208         p "module $mod () {\n";
209         foreach my $poly (@{ $chrpolys{$chr} }) {
210             p " polygon([";
211             my $delim = "";
212             foreach my $pt (@$poly) {
213                 p $delim;
214                 $pt =~ s{\d{5}}{$&,};
215                 $pt =~ s{\b\d}{$&.}g;
216                 p "[$pt]";
217                 $delim = ',';
218             }
219             p "]);\n";
220         }
221         p "}\n";
222         $demo .= $chr;
223     }
224     @demo = reverse $demo =~ m{.{1,$democols}}go;
225 }
226
227 parsefont();
228
229 our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
230 our $do_git_untracked = 1;
231 our $argcounter;
232
233 sub rjustt ($$) { # right justify and truncate (ie, pad and truncate at left)
234     my ($sz, $whole) = @_;
235     my $lw = length $whole;
236     return $lw > $sz
237         ? substr($whole, $lw-$sz)
238         : sprintf "%${sz}s", $whole;
239 }
240
241 sub ljustt ($$$) { # always includes $suffix
242     my ($sz, $whole, $suffix) = @_;
243     $sz -= length $suffix;
244     return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
245 }
246
247 sub gentextmodule_plusq ($$) {
248     my ($form, $s) = @_;
249     my $l = length $s;
250     gentextmodule($form, $s);
251     if (!($l & 1) && $l>=4) {
252         gentextmodule("${form}Q", substr($s,0,$l/2), substr($s,$l/2));
253     }
254 }
255
256 our @gcmd;
257
258 sub gitrun_start () {
259     open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
260 }
261
262 sub gitrun_done (;$) {
263     my ($errok) = @_;
264     $?=0; $!=0;
265     return if close F;
266     return if $errok;
267     die $! if $!;
268     die "@gcmd failed ($?)\n";
269 }
270
271 sub gitoutput (@) {
272     (@gcmd) = (qw(git), @_);
273     gitrun_start;
274     $_ = <F>;
275     gitrun_done;
276     defined or die "@gcmd produced no output";
277     chomp or die "@gcmd produced no final newline";
278     $_;
279 }
280
281 sub do_git () {
282     return unless $do_git;
283
284     @gcmd = qw(git status --porcelain);
285     push @gcmd, qw(--untracked=no) unless $do_git_untracked;
286
287     my $git_dirty = '';
288     gitrun_start;
289     while (<F>) {
290         if (m/^\?\?/ && $do_git_untracked) {
291             $git_dirty = '+';
292             next;
293         }
294         $git_dirty = '*';
295         last;
296     }
297     gitrun_done($git_dirty eq '*');
298
299     my $git_count;
300     my $git_object;
301
302     if ($do_git =~ m/c/) {
303         $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
304     }
305     if ($do_git =~ m/o/) {
306         $git_object = gitoutput qw(rev-parse HEAD);
307     }
308
309     foreach my $sz (3..8) {
310         gentextmodule_plusq("Tiny$sz", rjustt($sz, $git_count.$git_dirty))
311             if defined $git_count;
312
313         gentextmodule_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
314             if defined $git_object;
315
316         gentextmodule("Full$sz",
317                       rjustt($sz, $git_count.$git_dirty),
318                       ljustt($sz, $git_object, ''))
319             if defined $git_count && defined $git_object;
320     }
321 }    
322
323 while (@ARGV) {
324     $_ = shift;
325     if (m/^--(no)?-git$/) {
326         $do_git = $1 ? '' : 'co';
327     } elsif (m/^---git=object$/) {
328         $do_git = 'o';
329     } elsif (m/^-i$/) {
330         $do_git_untracked = 0;
331     } elsif (m/^-t(.*)$/) {
332         my $form = $1;
333         die "bad usage: -t needs string argument\n";
334         $_ = shift;
335         gentextmodule($form, split /\n/, $_);
336         $argcounter //= 0;
337     } elsif (m/^[^-]/) {
338         gentextmodule("Arg$argcounter", $_);
339         $argcounter++;
340     } else {
341         die "bad usage: unknown option \`$_'\n";
342     }
343 }
344
345 $do_git //= defined($argcounter) ? '' : 'co';
346
347 gentextmodule('FontDemo', @demo);
348
349 do_git();
350
351 flush STDOUT or die $!;
352 close STDOUT or die $!;
353
354 __DATA__
355
356 # 00 20 22 02
357 l 00 20 02
358 r 00 20 22
359 L 00 22 02
360 R 20 22 02
361 > 00 20 22 02 11
362 < 00 20 11 22 02
363
364 0 1 2 3 4 5 6 7 8 9
365
366 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
367 # # /#    #   # # # #   #     # # # # #
368 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
369 # #  #  #     #   #   # # #  #  # #   #
370 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
371
372 a b c d e f
373
374     #         #     /##
375     #   /##   # /#\ #
376 /## ##\ #   /## #r# ###
377 # # # # #   # # #/  #
378 \## ##/ \## \## \#/ #
379
380 + *
381
382       r
383  #  ###
384 ###  #
385  #  ###
386     L