chiark / gitweb /
5a61f415fdc353002336ed71a73645bf7bc8c88a
[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 + *
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 #   Small3:
56 #   Small4:
57 #   Small5:
58 #   Small6:
59 #   Small7:
60 #   Small8:
61 #   Small9:
62 #   Small10:
63 #       git rev-list --first-parent --count HEAD
64 #       typically 3-4 characters but we allow for up to 6
65 #       padded with zeroes; if too long we reduce mod 10^n
66 #       eg
67 #            Small4    1070
68 #       If tree is dirty, + or * is suffixed, reducing number of
69 #       digits by 1.
70 #
71 #   Small4S:
72 #   Small6S:
73 #   Small8S:
74 #   Small10S:
75 #       same but split into two lines eg
76 #            Small4S  10
77 #                     70
78 #
79 #   Git4   Git4S
80 #   Git6   Git6S
81 #   Git8   Git8S
82 #   Git10  Git10S
83 #       git-rev-parse HEAD   (prefix of requested length)
84 #       eg
85 #            Git6    82f2a2
86 #       If tree is dirty, + or * is suffixed to commitid,
87 #       reducing number of hex digits by 1.
88
89 #   Full3
90 #   Full4
91 #   Full5
92 #   Full6
93 #   Full7
94 #   Full8
95 #   Full9
96 #   Full10
97 #       git-rev-list --first-parent --count HEAD
98 #       git-rev-parse HEAD
99 #       eg
100 #            Full6      1070
101 #                     82f2a2
102 #       If tree is dirty, + or * is suffixed to count (but not to
103 #       commitid) reducing number of digits by 1.
104 #
105 #   FontDemo
106 #
107 #   Arg0, Arg1, ...
108 #       Strings passed on command line
109
110 sub p { print @_ or die $!; }
111
112 p <<'END';
113 // *** AUTOGENERATED - DO NOT EDIT *** //
114 function Commitid_pixelsz() =
115   ($Commitid_pixelsz       ? $Commitid_pixelsz       : 0.8) *
116   ($Commitid_scale         ? $Commitid_scale         : 1.0);
117 function Commitid_depth() =
118   ($Commitid_depth         ? $Commitid_depth         : Commitid_pixelsz()/2) *
119   ($Commitid_depth_scale   ? $Commitid_depth_scale   : 1.0);
120 function Commitid__scale() =
121   Commitid_pixelsz() / 0.2;
122 END
123
124 sub chrmodname ($) {
125     my ($chr) = @_;
126     my $chrx = sprintf '%#x', ord $chr;
127     return "Commitid__chr_$chrx";
128 }
129
130 our $gtm_demo_i = -1;
131 our $gtm_demo_j;
132 our @gtm_demo_o;
133
134 sub gentextmodule_demo_start_batch ($;$) {
135     ($gtm_demo_i, $gtm_demo_j) = @_;
136     $gtm_demo_j //= 0;
137 }
138
139 sub gentextmodule ($@) {
140     my ($form, @lines) = @_;
141     my $modb = "Commitid_$form";
142     p "module ${modb}_2D(){\n";
143     p " // |$_|\n" foreach @lines;
144     p " scale(Commitid__scale()){\n";
145     my $y = @lines;
146     my $cols = 1;
147     foreach my $line (@lines) {
148         $y--;
149         my $x = 0;
150         foreach my $chr (split //, $line) {
151             p sprintf "  translate([%d * 0.8, %d * 1.2]) %s();\n",
152                 $x, $y, chrmodname $chr
153                 if $chr =~ m/\S/;
154             $x++;
155         }
156         $cols = $x if $x > $cols;
157     }
158     p " }\n";
159     p "}\n";
160     p "module ${modb}(){\n";
161     p " d=Commitid_depth();\n";
162     p " translate([0,0,-d]) linear_extrude(height=d*2) ${modb}_2D();\n";
163     p "}\n";
164
165     p sprintf "function %s_sz() = Commitid__scale() * 0.1 * [ %d, %d ];\n",
166         $modb, 2 * ($cols * 4 - 1), 2 * (@lines * 6 - 1);
167
168     push @gtm_demo_o, <<END;
169  translate([$gtm_demo_i * st[0], $gtm_demo_j * st[1]]) {
170   difference(){
171    color("blue") translate([-e,-e]) square(${modb}_sz() + 2*[e,e]);
172    square(${modb}_sz());
173   }
174   ${modb}_2D();
175 }
176 END
177     $gtm_demo_j++;
178 }
179
180 our @demo;
181
182 sub parsefont () {
183     my %cellmap;
184     for (;;) {
185         $_ = <DATA> // die;
186         last if %cellmap && !m/\S/;
187         next unless m/\S/;
188         chomp;
189         s{^(.) }{};
190         $cellmap{$1} = $_;
191     }
192     my %chrpolys;
193     while (<DATA>) {
194         next unless m/\S/;
195         chomp;
196         my @chrs = split / /, $_;
197         <DATA> !~ m/\S/ or die;
198         foreach my $row (reverse 0..4) {
199             $_ = <DATA>;
200             chomp;
201             s{^}{ };
202             $_ .= ' ' x 8;
203             m{\S/\S} and die;
204             s{/(?=\s)}{L}g;
205             s{/(?=\S)}{r}g;
206             s{\\(?=\s)}{l}g;
207             s{\\(?=\S)}{R}g;
208             p "// $_\n";
209             foreach my $chr (@chrs) {
210                 s{^ }{} or die "$chr $_ ?";
211                 foreach my $col (0..2) {
212                     my @verts;
213                     if (s{^ }{}) {
214                     } elsif (s{^\S}{}) {
215                         my $f = $cellmap{$&};
216                         die unless $f;
217                         $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
218                         $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
219                         push @{ $chrpolys{$chr} }, [ split / /, $f ];
220                     } else {
221                         die "$_ ?";
222                     }
223                 }
224             }
225             die "$_ ?" if m{\S};
226         }    
227     }
228
229     my $demo = '';
230     my $democols = 6;
231     foreach my $chr (sort keys %chrpolys) {
232         my $mod = chrmodname $chr;
233         p "module $mod () {\n";
234         foreach my $poly (@{ $chrpolys{$chr} }) {
235             p " polygon([";
236             my $delim = "";
237             foreach my $pt (@$poly) {
238                 p $delim;
239                 $pt =~ s{\d{5}}{$&,};
240                 $pt =~ s{\b\d}{$&.}g;
241                 p "[$pt]";
242                 $delim = ',';
243             }
244             p "]);\n";
245         }
246         p "}\n";
247         $demo .= $chr;
248     }
249     @demo = reverse $demo =~ m{.{1,$democols}}go;
250 }
251
252 parsefont();
253
254 our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
255 our $do_git_untracked = 1;
256 our $argcounter;
257
258 sub rjustt ($$) { # right justify and truncate (ie, pad and truncate at left)
259     my ($sz, $whole) = @_;
260     my $lw = length $whole;
261     return $lw > $sz
262         ? substr($whole, $lw-$sz)
263         : sprintf "%${sz}s", $whole;
264 }
265
266 sub ljustt ($$$) { # always includes $suffix
267     my ($sz, $whole, $suffix) = @_;
268     $sz -= length $suffix;
269     return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
270 }
271
272 sub gentextmodule_plusq ($$) {
273     my ($form, $s) = @_;
274     my $l = length $s;
275     gentextmodule($form, $s);
276     if (!($l & 1) && $l>=4) {
277         gentextmodule("${form}S", substr($s,0,$l/2), substr($s,$l/2));
278     } else {
279         $gtm_demo_j++;
280     }
281 }
282
283 our @gcmd;
284
285 sub gitrun_start () {
286     open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
287 }
288
289 sub gitrun_done (;$) {
290     my ($errok) = @_;
291     $?=0; $!=0;
292     return if close F;
293     return if $errok;
294     die $! if $!;
295     die "@gcmd failed ($?)\n";
296 }
297
298 sub gitoutput (@) {
299     (@gcmd) = (qw(git), @_);
300     gitrun_start;
301     $_ = <F>;
302     gitrun_done;
303     defined or die "@gcmd produced no output";
304     chomp or die "@gcmd produced no final newline";
305     $_;
306 }
307
308 sub do_git () {
309     return unless $do_git;
310
311     @gcmd = qw(git status --porcelain);
312     push @gcmd, qw(--untracked=no) unless $do_git_untracked;
313
314     my $git_dirty = '';
315     gitrun_start;
316     while (<F>) {
317         if (m/^\?\?/ && $do_git_untracked) {
318             $git_dirty = '+';
319             next;
320         }
321         $git_dirty = '*';
322         last;
323     }
324     gitrun_done($git_dirty eq '*');
325
326     my $git_count;
327     my $git_object;
328
329     if ($do_git =~ m/c/) {
330         $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
331     }
332     if ($do_git =~ m/o/) {
333         $git_object = gitoutput qw(rev-parse HEAD);
334     }
335
336     foreach my $sz (3..10) {
337         gentextmodule_demo_start_batch($sz-3);
338
339         gentextmodule_plusq("Small$sz", rjustt($sz, $git_count.$git_dirty))
340             if defined $git_count;
341
342         gentextmodule_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
343             if defined $git_object;
344
345         gentextmodule("Full$sz",
346                       rjustt($sz, $git_count.$git_dirty),
347                       ljustt($sz, $git_object, ''))
348             if defined $git_count && defined $git_object;
349     }
350 }    
351
352 while (@ARGV) {
353     $_ = shift;
354     if (m/^--(no)?-git$/) {
355         $do_git = $1 ? '' : 'co';
356     } elsif (m/^---git=object$/) {
357         $do_git = 'o';
358     } elsif (m/^-i$/) {
359         $do_git_untracked = 0;
360     } elsif (m/^-t(.*)$/) {
361         my $form = $1;
362         die "bad usage: -t needs string argument\n";
363         $_ = shift;
364         gentextmodule($form, split /\n/, $_);
365         $argcounter //= 0;
366     } elsif (m/^[^-]/) {
367         gentextmodule("Arg$argcounter", $_);
368         $argcounter++;
369     } else {
370         die "bad usage: unknown option \`$_'\n";
371     }
372 }
373
374 $do_git //= defined($argcounter) ? '' : 'co';
375
376 gentextmodule_demo_start_batch(-1);
377 gentextmodule('FontDemo', @demo);
378
379 do_git();
380
381 p "module Commitid_2DDemo(){\n";
382 p " st = Commitid__scale() * [ 10, 5 ];\n";
383 p " e  = Commitid_pixelsz();\n";
384 p $_ foreach @gtm_demo_o;
385 p "}\n";
386
387 flush STDOUT or die $!;
388 close STDOUT or die $!;
389
390 __DATA__
391
392 # 00 20 22 02
393 l 00 20 02
394 r 00 20 22
395 L 00 22 02
396 R 20 22 02
397 > 00 20 22 02 11
398 < 00 20 11 22 02
399
400 0 1 2 3 4 5 6 7 8 9
401
402 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
403 # # /#    #   # # # #   #     # # # # #
404 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
405 # #  #  #     #   #   # # #  #  # #   #
406 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
407
408 a b c d e f
409
410     #         #     /##
411     #   /##   # /#\ #
412 /## ##\ #   /## #r# ###
413 # # # # #   # # #/  #
414 \## ##/ \## \## \#/ #
415
416 + *
417
418     # #
419  #  \#/
420 ### ###
421  #  /#\
422     # #