chiark / gitweb /
commitid.scad.pl: Provide %s_sz
[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     my $cols = 1;
94     foreach my $line (@lines) {
95         $y--;
96         my $x = 0;
97         foreach my $chr (split //, $line) {
98             next if $chr !~ m/\S/;
99             p sprintf "  translate([%d * 0.8, %d * 1.2]) %s();\n",
100                 $x, $y, chrmodname $chr;
101             $x++;
102         }
103         $cols = $x if $x > $cols;
104     }
105     p " }\n";
106     p "}\n";
107     p "module ${modb}(){\n";
108     p " d=Commitid_depth();\n";
109     p " translate([0,0,-d]) linear_extrude(height=d*2) ${modb}_2D();\n";
110     p "}\n";
111     p sprintf "function %s_sz() = Commitid__scale() * 0.1 * [ %d, %d ];\n",
112         $modb, 2 * (@lines * 6 - 1), 2 * ($cols * 4 - 1);
113 }
114
115 our @demo;
116
117 sub parsefont () {
118     my %cellmap;
119     for (;;) {
120         $_ = <DATA> // die;
121         last if %cellmap && !m/\S/;
122         next unless m/\S/;
123         chomp;
124         s{^(.) }{};
125         $cellmap{$1} = $_;
126     }
127     my %chrpolys;
128     while (<DATA>) {
129         next unless m/\S/;
130         chomp;
131         my @chrs = split / /, $_;
132         <DATA> !~ m/\S/ or die;
133         foreach my $row (reverse 0..4) {
134             $_ = <DATA>;
135             chomp;
136             s{^}{ };
137             $_ .= ' ' x 8;
138             m{\S/\S} and die;
139             s{/(?=\s)}{L}g;
140             s{/(?=\S)}{r}g;
141             s{\\(?=\s)}{l}g;
142             s{\\(?=\S)}{R}g;
143             p "// $_\n";
144             foreach my $chr (@chrs) {
145                 s{^ }{} or die "$chr $_ ?";
146                 foreach my $col (0..2) {
147                     my @verts;
148                     if (s{^ }{}) {
149                     } elsif (s{^\S}{}) {
150                         my $f = $cellmap{$&};
151                         die unless $f;
152                         $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
153                         $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
154                         push @{ $chrpolys{$chr} }, [ split / /, $f ];
155                     } else {
156                         die "$_ ?";
157                     }
158                 }
159             }
160             die "$_ ?" if m{\S};
161         }    
162     }
163
164     my $demo = '';
165     my $democols = 6;
166     foreach my $chr (sort keys %chrpolys) {
167         my $mod = chrmodname $chr;
168         p "module $mod () {\n";
169         foreach my $poly (@{ $chrpolys{$chr} }) {
170             p " polygon([";
171             my $delim = "";
172             foreach my $pt (@$poly) {
173                 p $delim;
174                 $pt =~ s{\d{5}}{$&,};
175                 $pt =~ s{\b\d}{$&.}g;
176                 p "[$pt]";
177                 $delim = ',';
178             }
179             p "]);\n";
180         }
181         p "}\n";
182         $demo .= $chr;
183     }
184     @demo = reverse $demo =~ m{.{1,$democols}}go;
185 }
186
187 parsefont();
188
189 gentextmodule('FontDemo', @demo);
190
191 __DATA__
192
193 # 00 20 22 02
194 l 00 20 02
195 r 00 20 22
196 L 00 22 02
197 R 20 22 02
198 > 00 20 22 02 11
199 < 00 20 11 22 02
200
201 0 1 2 3 4 5 6 7 8 9
202
203 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
204 # # /#    #   # # # #   #     # # # # #
205 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
206 # #  #  #     #   #   # # #  #  # #   #
207 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
208
209 a b c d e f
210
211     #         #     /##
212     #   /##   # /#\ #
213 /## ##\ #   /## #r# ###
214 # # # # #   # # #/  #
215 \## ##/ \## \## \#/ #
216