chiark / gitweb /
commitid: 2d test explicit placements
[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 #   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             next if $chr !~ m/\S/;
152             p sprintf "  translate([%d * 0.8, %d * 1.2]) %s();\n",
153                 $x, $y, chrmodname $chr;
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,
169         " translate([$gtm_demo_i * st[0], $gtm_demo_j * st[1]])".
170         " ${modb}_2D();\n";
171     $gtm_demo_j++;
172 }
173
174 our @demo;
175
176 sub parsefont () {
177     my %cellmap;
178     for (;;) {
179         $_ = <DATA> // die;
180         last if %cellmap && !m/\S/;
181         next unless m/\S/;
182         chomp;
183         s{^(.) }{};
184         $cellmap{$1} = $_;
185     }
186     my %chrpolys;
187     while (<DATA>) {
188         next unless m/\S/;
189         chomp;
190         my @chrs = split / /, $_;
191         <DATA> !~ m/\S/ or die;
192         foreach my $row (reverse 0..4) {
193             $_ = <DATA>;
194             chomp;
195             s{^}{ };
196             $_ .= ' ' x 8;
197             m{\S/\S} and die;
198             s{/(?=\s)}{L}g;
199             s{/(?=\S)}{r}g;
200             s{\\(?=\s)}{l}g;
201             s{\\(?=\S)}{R}g;
202             p "// $_\n";
203             foreach my $chr (@chrs) {
204                 s{^ }{} or die "$chr $_ ?";
205                 foreach my $col (0..2) {
206                     my @verts;
207                     if (s{^ }{}) {
208                     } elsif (s{^\S}{}) {
209                         my $f = $cellmap{$&};
210                         die unless $f;
211                         $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
212                         $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
213                         push @{ $chrpolys{$chr} }, [ split / /, $f ];
214                     } else {
215                         die "$_ ?";
216                     }
217                 }
218             }
219             die "$_ ?" if m{\S};
220         }    
221     }
222
223     my $demo = '';
224     my $democols = 6;
225     foreach my $chr (sort keys %chrpolys) {
226         my $mod = chrmodname $chr;
227         p "module $mod () {\n";
228         foreach my $poly (@{ $chrpolys{$chr} }) {
229             p " polygon([";
230             my $delim = "";
231             foreach my $pt (@$poly) {
232                 p $delim;
233                 $pt =~ s{\d{5}}{$&,};
234                 $pt =~ s{\b\d}{$&.}g;
235                 p "[$pt]";
236                 $delim = ',';
237             }
238             p "]);\n";
239         }
240         p "}\n";
241         $demo .= $chr;
242     }
243     @demo = reverse $demo =~ m{.{1,$democols}}go;
244 }
245
246 parsefont();
247
248 our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
249 our $do_git_untracked = 1;
250 our $argcounter;
251
252 sub rjustt ($$) { # right justify and truncate (ie, pad and truncate at left)
253     my ($sz, $whole) = @_;
254     my $lw = length $whole;
255     return $lw > $sz
256         ? substr($whole, $lw-$sz)
257         : sprintf "%${sz}s", $whole;
258 }
259
260 sub ljustt ($$$) { # always includes $suffix
261     my ($sz, $whole, $suffix) = @_;
262     $sz -= length $suffix;
263     return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
264 }
265
266 sub gentextmodule_plusq ($$) {
267     my ($form, $s) = @_;
268     my $l = length $s;
269     gentextmodule($form, $s);
270     if (!($l & 1) && $l>=4) {
271         gentextmodule("${form}S", substr($s,0,$l/2), substr($s,$l/2));
272     }
273 }
274
275 our @gcmd;
276
277 sub gitrun_start () {
278     open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
279 }
280
281 sub gitrun_done (;$) {
282     my ($errok) = @_;
283     $?=0; $!=0;
284     return if close F;
285     return if $errok;
286     die $! if $!;
287     die "@gcmd failed ($?)\n";
288 }
289
290 sub gitoutput (@) {
291     (@gcmd) = (qw(git), @_);
292     gitrun_start;
293     $_ = <F>;
294     gitrun_done;
295     defined or die "@gcmd produced no output";
296     chomp or die "@gcmd produced no final newline";
297     $_;
298 }
299
300 sub do_git () {
301     return unless $do_git;
302
303     @gcmd = qw(git status --porcelain);
304     push @gcmd, qw(--untracked=no) unless $do_git_untracked;
305
306     my $git_dirty = '';
307     gitrun_start;
308     while (<F>) {
309         if (m/^\?\?/ && $do_git_untracked) {
310             $git_dirty = '+';
311             next;
312         }
313         $git_dirty = '*';
314         last;
315     }
316     gitrun_done($git_dirty eq '*');
317
318     my $git_count;
319     my $git_object;
320
321     if ($do_git =~ m/c/) {
322         $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
323     }
324     if ($do_git =~ m/o/) {
325         $git_object = gitoutput qw(rev-parse HEAD);
326     }
327
328     foreach my $sz (3..10) {
329         gentextmodule_demo_start_batch($sz-3, 0);
330
331         gentextmodule_plusq("Small$sz", rjustt($sz, $git_count.$git_dirty))
332             if defined $git_count;
333
334         gentextmodule_demo_start_batch($sz-3, 2);
335
336         gentextmodule_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
337             if defined $git_object;
338
339         gentextmodule_demo_start_batch($sz-3, 4);
340
341         gentextmodule("Full$sz",
342                       rjustt($sz, $git_count.$git_dirty),
343                       ljustt($sz, $git_object, ''))
344             if defined $git_count && defined $git_object;
345     }
346 }    
347
348 while (@ARGV) {
349     $_ = shift;
350     if (m/^--(no)?-git$/) {
351         $do_git = $1 ? '' : 'co';
352     } elsif (m/^---git=object$/) {
353         $do_git = 'o';
354     } elsif (m/^-i$/) {
355         $do_git_untracked = 0;
356     } elsif (m/^-t(.*)$/) {
357         my $form = $1;
358         die "bad usage: -t needs string argument\n";
359         $_ = shift;
360         gentextmodule($form, split /\n/, $_);
361         $argcounter //= 0;
362     } elsif (m/^[^-]/) {
363         gentextmodule("Arg$argcounter", $_);
364         $argcounter++;
365     } else {
366         die "bad usage: unknown option \`$_'\n";
367     }
368 }
369
370 $do_git //= defined($argcounter) ? '' : 'co';
371
372 gentextmodule_demo_start_batch(-1);
373 gentextmodule('FontDemo', @demo);
374
375 do_git();
376
377 p "module Commitid_2DDemo(){\n";
378 p " st = Commitid__scale() * [ 10, 5 ];\n";
379 p $_ foreach @gtm_demo_o;
380 p "}\n";
381
382 flush STDOUT or die $!;
383 close STDOUT or die $!;
384
385 __DATA__
386
387 # 00 20 22 02
388 l 00 20 02
389 r 00 20 22
390 L 00 22 02
391 R 20 22 02
392 > 00 20 22 02 11
393 < 00 20 11 22 02
394
395 0 1 2 3 4 5 6 7 8 9
396
397 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
398 # # /#    #   # # # #   #     # # # # #
399 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
400 # #  #  #     #   #   # # #  #  # #   #
401 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
402
403 a b c d e f
404
405     #         #     /##
406     #   /##   # /#\ #
407 /## ##\ #   /## #r# ###
408 # # # # #   # # #/  #
409 \## ##/ \## \## \#/ #
410
411 + *
412
413       r
414  #  ###
415 ###  #
416  #  ###
417     L