chiark / gitweb /
commitid.scad.pl: Generate up to size 10
[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 #   Small4Q:
72 #   Small6Q:
73 #   Small8Q:
74 #   Small10Q:
75 #       same but in two lines eg
76 #            Small4Q  10
77 #                     70
78 #
79 #   Git4   Git4Q
80 #   Git6   Git6Q
81 #   Git8   Git8Q
82 #   Git10  Git10Q
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 sub gentextmodule ($@) {
131     my ($form, @lines) = @_;
132     my $modb = "Commitid_$form";
133     p "module ${modb}_2D(){\n";
134     p " // |$_|\n" foreach @lines;
135     p " scale(Commitid__scale()){\n";
136     my $y = @lines;
137     my $cols = 1;
138     foreach my $line (@lines) {
139         $y--;
140         my $x = 0;
141         foreach my $chr (split //, $line) {
142             next if $chr !~ m/\S/;
143             p sprintf "  translate([%d * 0.8, %d * 1.2]) %s();\n",
144                 $x, $y, chrmodname $chr;
145             $x++;
146         }
147         $cols = $x if $x > $cols;
148     }
149     p " }\n";
150     p "}\n";
151     p "module ${modb}(){\n";
152     p " d=Commitid_depth();\n";
153     p " translate([0,0,-d]) linear_extrude(height=d*2) ${modb}_2D();\n";
154     p "}\n";
155     p sprintf "function %s_sz() = Commitid__scale() * 0.1 * [ %d, %d ];\n",
156         $modb, 2 * ($cols * 4 - 1), 2 * (@lines * 6 - 1);
157 }
158
159 our @demo;
160
161 sub parsefont () {
162     my %cellmap;
163     for (;;) {
164         $_ = <DATA> // die;
165         last if %cellmap && !m/\S/;
166         next unless m/\S/;
167         chomp;
168         s{^(.) }{};
169         $cellmap{$1} = $_;
170     }
171     my %chrpolys;
172     while (<DATA>) {
173         next unless m/\S/;
174         chomp;
175         my @chrs = split / /, $_;
176         <DATA> !~ m/\S/ or die;
177         foreach my $row (reverse 0..4) {
178             $_ = <DATA>;
179             chomp;
180             s{^}{ };
181             $_ .= ' ' x 8;
182             m{\S/\S} and die;
183             s{/(?=\s)}{L}g;
184             s{/(?=\S)}{r}g;
185             s{\\(?=\s)}{l}g;
186             s{\\(?=\S)}{R}g;
187             p "// $_\n";
188             foreach my $chr (@chrs) {
189                 s{^ }{} or die "$chr $_ ?";
190                 foreach my $col (0..2) {
191                     my @verts;
192                     if (s{^ }{}) {
193                     } elsif (s{^\S}{}) {
194                         my $f = $cellmap{$&};
195                         die unless $f;
196                         $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
197                         $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
198                         push @{ $chrpolys{$chr} }, [ split / /, $f ];
199                     } else {
200                         die "$_ ?";
201                     }
202                 }
203             }
204             die "$_ ?" if m{\S};
205         }    
206     }
207
208     my $demo = '';
209     my $democols = 6;
210     foreach my $chr (sort keys %chrpolys) {
211         my $mod = chrmodname $chr;
212         p "module $mod () {\n";
213         foreach my $poly (@{ $chrpolys{$chr} }) {
214             p " polygon([";
215             my $delim = "";
216             foreach my $pt (@$poly) {
217                 p $delim;
218                 $pt =~ s{\d{5}}{$&,};
219                 $pt =~ s{\b\d}{$&.}g;
220                 p "[$pt]";
221                 $delim = ',';
222             }
223             p "]);\n";
224         }
225         p "}\n";
226         $demo .= $chr;
227     }
228     @demo = reverse $demo =~ m{.{1,$democols}}go;
229 }
230
231 parsefont();
232
233 our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
234 our $do_git_untracked = 1;
235 our $argcounter;
236
237 sub rjustt ($$) { # right justify and truncate (ie, pad and truncate at left)
238     my ($sz, $whole) = @_;
239     my $lw = length $whole;
240     return $lw > $sz
241         ? substr($whole, $lw-$sz)
242         : sprintf "%${sz}s", $whole;
243 }
244
245 sub ljustt ($$$) { # always includes $suffix
246     my ($sz, $whole, $suffix) = @_;
247     $sz -= length $suffix;
248     return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
249 }
250
251 sub gentextmodule_plusq ($$) {
252     my ($form, $s) = @_;
253     my $l = length $s;
254     gentextmodule($form, $s);
255     if (!($l & 1) && $l>=4) {
256         gentextmodule("${form}Q", substr($s,0,$l/2), substr($s,$l/2));
257     }
258 }
259
260 our @gcmd;
261
262 sub gitrun_start () {
263     open F, "-|", @gcmd or die "$gcmd[0]: start: $!";
264 }
265
266 sub gitrun_done (;$) {
267     my ($errok) = @_;
268     $?=0; $!=0;
269     return if close F;
270     return if $errok;
271     die $! if $!;
272     die "@gcmd failed ($?)\n";
273 }
274
275 sub gitoutput (@) {
276     (@gcmd) = (qw(git), @_);
277     gitrun_start;
278     $_ = <F>;
279     gitrun_done;
280     defined or die "@gcmd produced no output";
281     chomp or die "@gcmd produced no final newline";
282     $_;
283 }
284
285 sub do_git () {
286     return unless $do_git;
287
288     @gcmd = qw(git status --porcelain);
289     push @gcmd, qw(--untracked=no) unless $do_git_untracked;
290
291     my $git_dirty = '';
292     gitrun_start;
293     while (<F>) {
294         if (m/^\?\?/ && $do_git_untracked) {
295             $git_dirty = '+';
296             next;
297         }
298         $git_dirty = '*';
299         last;
300     }
301     gitrun_done($git_dirty eq '*');
302
303     my $git_count;
304     my $git_object;
305
306     if ($do_git =~ m/c/) {
307         $git_count = gitoutput qw(rev-list --first-parent --count HEAD);
308     }
309     if ($do_git =~ m/o/) {
310         $git_object = gitoutput qw(rev-parse HEAD);
311     }
312
313     foreach my $sz (3..10) {
314         gentextmodule_plusq("Small$sz", rjustt($sz, $git_count.$git_dirty))
315             if defined $git_count;
316
317         gentextmodule_plusq("Git$sz", ljustt($sz, $git_object, $git_dirty))
318             if defined $git_object;
319
320         gentextmodule("Full$sz",
321                       rjustt($sz, $git_count.$git_dirty),
322                       ljustt($sz, $git_object, ''))
323             if defined $git_count && defined $git_object;
324     }
325 }    
326
327 while (@ARGV) {
328     $_ = shift;
329     if (m/^--(no)?-git$/) {
330         $do_git = $1 ? '' : 'co';
331     } elsif (m/^---git=object$/) {
332         $do_git = 'o';
333     } elsif (m/^-i$/) {
334         $do_git_untracked = 0;
335     } elsif (m/^-t(.*)$/) {
336         my $form = $1;
337         die "bad usage: -t needs string argument\n";
338         $_ = shift;
339         gentextmodule($form, split /\n/, $_);
340         $argcounter //= 0;
341     } elsif (m/^[^-]/) {
342         gentextmodule("Arg$argcounter", $_);
343         $argcounter++;
344     } else {
345         die "bad usage: unknown option \`$_'\n";
346     }
347 }
348
349 $do_git //= defined($argcounter) ? '' : 'co';
350
351 gentextmodule('FontDemo', @demo);
352
353 do_git();
354
355 flush STDOUT or die $!;
356 close STDOUT or die $!;
357
358 __DATA__
359
360 # 00 20 22 02
361 l 00 20 02
362 r 00 20 22
363 L 00 22 02
364 R 20 22 02
365 > 00 20 22 02 11
366 < 00 20 11 22 02
367
368 0 1 2 3 4 5 6 7 8 9
369
370 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
371 # # /#    #   # # # #   #     # # # # #
372 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
373 # #  #  #     #   #   # # #  #  # #   #
374 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
375
376 a b c d e f
377
378     #         #     /##
379     #   /##   # /#\ #
380 /## ##\ #   /## #r# ###
381 # # # # #   # # #/  #
382 \## ##/ \## \## \#/ #
383
384 + *
385
386       r
387  #  ###
388 ###  #
389  #  ###
390     L