chiark / gitweb /
ae1ebb42b531626ba76a8930dd0272312ac05f1e
[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     }
279 }
280
281 our @gcmd;
282
283 sub gitrun_start () {
284     open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
285 }
286
287 sub gitrun_done (;$) {
288     my ($errok) = @_;
289     $?=0; $!=0;
290     return if close F;
291     return if $errok;
292     die $! if $!;
293     die "@gcmd failed ($?)\n";
294 }
295
296 sub gitoutput (@) {
297     (@gcmd) = (qw(git), @_);
298     gitrun_start;
299     $_ = <F>;
300     gitrun_done;
301     defined or die "@gcmd produced no output";
302     chomp or die "@gcmd produced no final newline";
303     $_;
304 }
305
306 sub do_git () {
307     return unless $do_git;
308
309     @gcmd = qw(git status --porcelain);
310     push @gcmd, qw(--untracked=no) unless $do_git_untracked;
311
312     my $git_dirty = '';
313     gitrun_start;
314     while (<F>) {
315         if (m/^\?\?/ && $do_git_untracked) {
316             $git_dirty = '+';
317             next;
318         }
319         $git_dirty = '*';
320         last;
321     }
322     gitrun_done($git_dirty eq '*');
323
324     my $git_count;
325     my $git_object;
326
327     if ($do_git =~ m/c/) {
328         $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
329     }
330     if ($do_git =~ m/o/) {
331         $git_object = gitoutput qw(rev-parse HEAD);
332     }
333
334     foreach my $sz (3..10) {
335         gentextmodule_demo_start_batch($sz-3, 0);
336
337         gentextmodule_plusq("Small$sz", rjustt($sz, $git_count.$git_dirty))
338             if defined $git_count;
339
340         gentextmodule_demo_start_batch($sz-3, 2);
341
342         gentextmodule_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
343             if defined $git_object;
344
345         gentextmodule_demo_start_batch($sz-3, 4);
346
347         gentextmodule("Full$sz",
348                       rjustt($sz, $git_count.$git_dirty),
349                       ljustt($sz, $git_object, ''))
350             if defined $git_count && defined $git_object;
351     }
352 }    
353
354 while (@ARGV) {
355     $_ = shift;
356     if (m/^--(no)?-git$/) {
357         $do_git = $1 ? '' : 'co';
358     } elsif (m/^---git=object$/) {
359         $do_git = 'o';
360     } elsif (m/^-i$/) {
361         $do_git_untracked = 0;
362     } elsif (m/^-t(.*)$/) {
363         my $form = $1;
364         die "bad usage: -t needs string argument\n";
365         $_ = shift;
366         gentextmodule($form, split /\n/, $_);
367         $argcounter //= 0;
368     } elsif (m/^[^-]/) {
369         gentextmodule("Arg$argcounter", $_);
370         $argcounter++;
371     } else {
372         die "bad usage: unknown option \`$_'\n";
373     }
374 }
375
376 $do_git //= defined($argcounter) ? '' : 'co';
377
378 gentextmodule_demo_start_batch(-1);
379 gentextmodule('FontDemo', @demo);
380
381 do_git();
382
383 p "module Commitid_2DDemo(){\n";
384 p " st = Commitid__scale() * [ 10, 5 ];\n";
385 p " e  = Commitid_pixelsz();\n";
386 p $_ foreach @gtm_demo_o;
387 p "}\n";
388
389 flush STDOUT or die $!;
390 close STDOUT or die $!;
391
392 __DATA__
393
394 # 00 20 22 02
395 l 00 20 02
396 r 00 20 22
397 L 00 22 02
398 R 20 22 02
399 > 00 20 22 02 11
400 < 00 20 11 22 02
401
402 0 1 2 3 4 5 6 7 8 9
403
404 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
405 # # /#    #   # # # #   #     # # # # #
406 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
407 # #  #  #     #   #   # # #  #  # #   #
408 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
409
410 a b c d e f
411
412     #         #     /##
413     #   /##   # /#\ #
414 /## ##\ #   /## #r# ###
415 # # # # #   # # #/  #
416 \## ##/ \## \## \#/ #
417
418 + *
419
420     # #
421  #  \#/
422 ### ###
423  #  /#\
424     # #