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