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