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