chiark / gitweb /
be58d4cd67ee8e5e1ef89a8db505b8ad4cbfba60
[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 #       eg
41 #            Tiny4    1070
42 #
43 #   Tiny4Q:
44 #   Tiny6Q:
45 #   Tiny9Q:
46 #       same but in two lines eg
47 #            Tiny4Q   10
48 #                     70
49 #
50 #   Git4
51 #   Git6
52 #   Git8
53 #       git-rev-parse HEAD
54 #       eg
55 #            Git6    82f2a2
56
57 #   Small4
58 #   Small6
59 #   Small8
60 #       git-rev-list --first-parent --count HEAD
61 #       git-rev-parse HEAD
62 #       eg
63 #            Small6     1070
64 #                     82f2a2
65
66 sub p { print @_ or die $!; }
67
68 p <<'END';
69 // *** AUTOGENERATED - DO NOT EDIT *** //
70 function Commitid_pixelsz() =
71   ($Commitid_pixelsz       ? $Commitid_pixelsz       : 0.4) *
72   ($Commitid_scale         ? $Commitid_scale         : 1.0);
73 function Commitid_depth() =
74   ($Commitid_depth         ? $Commitid_depth         : Commitid_pixelsz()) *
75   ($Commitid_depth_scale   ? $Commitid_depth_scale   : 1.0);
76 function Commitid__scale() =
77   Commitid_pixelsz() / 0.1;
78 END
79
80 sub chrmodname ($) {
81     my ($chr) = @_;
82     my $chrx = sprintf '%#x', ord $chr;
83     return "Commitid__chr_$chrx";
84 }
85
86 sub gentextmodule ($@) {
87     my ($form, @lines) = @_;
88     my $modb = "Commitid_$form";
89     p "module ${modb}_2D(){\n";
90     p " scale(Commitid__scale()){\n";
91     my $y = @lines;
92     foreach my $line (@lines) {
93         $y--;
94         my $x = 0;
95         foreach my $chr (split //, $line) {
96             next if $chr !~ m/\S/;
97             p sprintf "  translate([%d * 0.8, %d * 1.2]) %s();\n",
98                 $x, $y, chrmodname $chr;
99             $x++;
100         }
101     }
102     p " }\n";
103     p "}\n";
104     p "module ${modb}(){\n";
105     p " d=Commitid_depth();\n";
106     p " translate([0,0,-d]) linear_extrude(height=d*2) ${modb}_2D();\n";
107     p "}\n";
108 }
109
110 our @demo;
111
112 sub parsefont () {
113     my %cellmap;
114     for (;;) {
115         $_ = <DATA> // die;
116         last if %cellmap && !m/\S/;
117         next unless m/\S/;
118         chomp;
119         s{^(.) }{};
120         $cellmap{$1} = $_;
121     }
122     my %chrpolys;
123     while (<DATA>) {
124         next unless m/\S/;
125         chomp;
126         my @chrs = split / /, $_;
127         <DATA> !~ m/\S/ or die;
128         foreach my $row (reverse 0..4) {
129             $_ = <DATA>;
130             chomp;
131             s{^}{ };
132             $_ .= ' ' x 8;
133             m{\S/\S} and die;
134             s{/(?=\s)}{L}g;
135             s{/(?=\S)}{r}g;
136             s{\\(?=\s)}{l}g;
137             s{\\(?=\S)}{R}g;
138             p "// $_\n";
139             foreach my $chr (@chrs) {
140                 s{^ }{} or die "$chr $_ ?";
141                 foreach my $col (0..2) {
142                     my @verts;
143                     if (s{^ }{}) {
144                     } elsif (s{^\S}{}) {
145                         my $f = $cellmap{$&};
146                         die unless $f;
147                         $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
148                         $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
149                         push @{ $chrpolys{$chr} }, [ split / /, $f ];
150                     } else {
151                         die "$_ ?";
152                     }
153                 }
154             }
155             die "$_ ?" if m{\S};
156         }    
157     }
158
159     my $demo = '';
160     my $democols = 6;
161     foreach my $chr (sort keys %chrpolys) {
162         my $mod = chrmodname $chr;
163         p "module $mod () {\n";
164         foreach my $poly (@{ $chrpolys{$chr} }) {
165             p " polygon([";
166             my $delim = "";
167             foreach my $pt (@$poly) {
168                 p $delim;
169                 $pt =~ s{\d{5}}{$&,};
170                 $pt =~ s{\b\d}{$&.}g;
171                 p "[$pt]";
172                 $delim = ',';
173             }
174             p "]);\n";
175         }
176         p "}\n";
177         $demo .= $chr;
178     }
179     @demo = reverse $demo =~ m{.{1,$democols}}go;
180 }
181
182 parsefont();
183
184 gentextmodule('FontDemo', @demo);
185
186 __DATA__
187
188 # 00 20 22 02
189 l 00 20 02
190 r 00 20 22
191 L 00 22 02
192 R 20 22 02
193 > 00 20 22 02 11
194 < 00 20 11 22 02
195
196 0 1 2 3 4 5 6 7 8 9
197
198 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
199 # # /#    #   # # # #   #     # # # # #
200 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
201 # #  #  #     #   #   # # #  #  # #   #
202 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
203
204 a b c d e f
205
206     #         #     /##
207     #   /##   # /#\ #
208 /## ##\ #   /## #r# ###
209 # # # # #   # # #/  #
210 \## ##/ \## \## \#/ #
211