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