chiark / gitweb /
de1efa87586485c334880cd624607565d332ede4
[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 # We generate a physical indication of which commit was used.
9 #
10 # We provide for scaling factors with dynamic variables:
11 #    $Commitid_pixelsz        if not set, we use 0.4 } multiplied
12 #    $Commitid_scale          if not set, we use 1.0 }  together
13 #    $Commitid_depth          if not set, we use xy pixel size from above
14 #    $Commitid_depth_scale    if not set, we use 1.0 (multiplies depth above)
15 #
16 # For each form we have
17 #
18 #    module Commitid_Form_2D() { ... }
19 #    module Commitid_Form() { ... }
20 #
21 #  These have their origin in the bottom left corner.  The 3D model
22 #  is a positive, has its origin halfway through, and is twice the
23 #  depth in height, so it can be added or subtracted.
24 #
25 # And we provide
26 #
27 #   function Commitid_pixelsz()    // $Commitid_pixelsz * $Commitid_scale
28 #   function Commitid_depth()      // see above
29 #
30 # We can generate these forms:
31 #
32 #   Tiny3:
33 #   Tiny4:
34 #   Tiny5:
35 #   Tiny6:
36 #   Tiny7:
37 #   Tiny8:
38 #       git rev-list --first-parent --count HEAD
39 #       typically 3-4 characters but we allow for up to 6
40 #       padded with zeroes; if too long we reduce mod 10^n
41 #       eg
42 #            Tiny4    1070
43 #
44 #   Tiny4Q:
45 #   Tiny6Q:
46 #   Tiny9Q:
47 #       same but in two lines eg
48 #            Tiny4Q   10
49 #                     70
50 #
51 #   Git4  Git4Q
52 #   Git6  Git6Q
53 #   Git8  Git8Q
54 #       git-rev-parse HEAD   (prefix of requested length)
55 #       eg
56 #            Git6    82f2a2
57
58 #   Small4
59 #   Small6
60 #   Small8
61 #       git-rev-list --first-parent --count HEAD
62 #       git-rev-parse HEAD
63 #       eg
64 #            Small6     1070
65 #                     82f2a2
66
67 sub p { print @_ or die $!; }
68
69 p <<'END';
70 // *** AUTOGENERATED - DO NOT EDIT *** //
71 function Commitid_pixelsz() =
72   ($Commitid_pixelsz       ? $Commitid_pixelsz       : 0.4) *
73   ($Commitid_scale         ? $Commitid_scale         : 1.0);
74 function Commitid_depth() =
75   ($Commitid_depth         ? $Commitid_depth         : Commitid_pixelsz()) *
76   ($Commitid_depth_scale   ? $Commitid_depth_scale   : 1.0);
77 function Commitid__scale() =
78   Commitid_pixelsz() / 0.1;
79 END
80
81 sub chrmodname ($) {
82     my ($chr) = @_;
83     my $chrx = sprintf '%#x', ord $chr;
84     return "Commitid__chr_$chrx";
85 }
86
87 sub gentextmodule ($@) {
88     my ($form, @lines) = @_;
89     my $modb = "Commitid_$form";
90     p "module ${modb}_2D(){\n";
91     p " scale(Commitid__scale()){\n";
92     my $y = @lines;
93     foreach my $line (@lines) {
94         $y--;
95         my $x = 0;
96         foreach my $chr (split //, $line) {
97             next if $chr !~ m/\S/;
98             p sprintf "  translate([%d * 0.8, %d * 1.2]) %s();\n",
99                 $x, $y, chrmodname $chr;
100             $x++;
101         }
102     }
103     p " }\n";
104     p "}\n";
105     p "module ${modb}(){\n";
106     p " d=Commitid_depth();\n";
107     p " translate([0,0,-d]) linear_extrude(height=d*2) ${modb}_2D();\n";
108     p "}\n";
109 }
110
111 our @demo;
112
113 sub parsefont () {
114     my %cellmap;
115     for (;;) {
116         $_ = <DATA> // die;
117         last if %cellmap && !m/\S/;
118         next unless m/\S/;
119         chomp;
120         s{^(.) }{};
121         $cellmap{$1} = $_;
122     }
123     my %chrpolys;
124     while (<DATA>) {
125         next unless m/\S/;
126         chomp;
127         my @chrs = split / /, $_;
128         <DATA> !~ m/\S/ or die;
129         foreach my $row (reverse 0..4) {
130             $_ = <DATA>;
131             chomp;
132             s{^}{ };
133             $_ .= ' ' x 8;
134             m{\S/\S} and die;
135             s{/(?=\s)}{L}g;
136             s{/(?=\S)}{r}g;
137             s{\\(?=\s)}{l}g;
138             s{\\(?=\S)}{R}g;
139             p "// $_\n";
140             foreach my $chr (@chrs) {
141                 s{^ }{} or die "$chr $_ ?";
142                 foreach my $col (0..2) {
143                     my @verts;
144                     if (s{^ }{}) {
145                     } elsif (s{^\S}{}) {
146                         my $f = $cellmap{$&};
147                         die unless $f;
148                         $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
149                         $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
150                         push @{ $chrpolys{$chr} }, [ split / /, $f ];
151                     } else {
152                         die "$_ ?";
153                     }
154                 }
155             }
156             die "$_ ?" if m{\S};
157         }    
158     }
159
160     my $demo = '';
161     my $democols = 6;
162     foreach my $chr (sort keys %chrpolys) {
163         my $mod = chrmodname $chr;
164         p "module $mod () {\n";
165         foreach my $poly (@{ $chrpolys{$chr} }) {
166             p " polygon([";
167             my $delim = "";
168             foreach my $pt (@$poly) {
169                 p $delim;
170                 $pt =~ s{\d{5}}{$&,};
171                 $pt =~ s{\b\d}{$&.}g;
172                 p "[$pt]";
173                 $delim = ',';
174             }
175             p "]);\n";
176         }
177         p "}\n";
178         $demo .= $chr;
179     }
180     @demo = reverse $demo =~ m{.{1,$democols}}go;
181 }
182
183 parsefont();
184
185 gentextmodule('FontDemo', @demo);
186
187 __DATA__
188
189 # 00 20 22 02
190 l 00 20 02
191 r 00 20 22
192 L 00 22 02
193 R 20 22 02
194 > 00 20 22 02 11
195 < 00 20 11 22 02
196
197 0 1 2 3 4 5 6 7 8 9
198
199 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
200 # # /#    #   # # # #   #     # # # # #
201 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
202 # #  #  #     #   #   # # #  #  # #   #
203 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
204
205 a b c d e f
206
207     #         #     /##
208     #   /##   # /#\ #
209 /## ##\ #   /## #r# ###
210 # # # # #   # # #/  #
211 \## ##/ \## \## \#/ #
212