chiark / gitweb /
commitid.scad.pl: Introduce gentextmodule and use it for Commitid_FontDemo (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 function Commitid_pixelsz() =
66   ($Commitid_pixelsz       ? $Commitid_pixelsz       : 0.4) *
67   ($Commitid_scale         ? $Commitid_scale         : 1.0);
68 function Commitid_depth() =
69   ($Commitid_depth         ? $Commitid_depth         : Commitid_pixelsz()) *
70   ($Commitid_depth_scale   ? $Commitid_depth_scale   : 1.0);
71 function Commitid__scale() =
72   Commitid_pixelsz() / 0.1;
73 END
74
75 sub chrmodname ($) {
76     my ($chr) = @_;
77     my $chrx = sprintf '%#x', ord $chr;
78     return "Commitid__chr_$chrx";
79 }
80
81 sub gentextmodule ($@) {
82     my ($modulename, @lines) = @_;
83     p "module $modulename(){\n";
84     p " scale(Commitid__scale()){\n";
85     my $y = @lines;
86     foreach my $line (@lines) {
87         $y--;
88         my $x = 0;
89         foreach my $chr (split //, $line) {
90             next if $chr !~ m/\S/;
91             p sprintf "  translate([%d * 0.8, %d * 1.2]) %s();\n",
92                 $x, $y, chrmodname $chr;
93             $x++;
94         }
95     }
96     p " }\n";
97     p "}\n";
98 }
99
100 our @demo;
101
102 sub parsefont () {
103     my %cellmap;
104     for (;;) {
105         $_ = <DATA> // die;
106         last if %cellmap && !m/\S/;
107         next unless m/\S/;
108         chomp;
109         s{^(.) }{};
110         $cellmap{$1} = $_;
111     }
112     my %chrpolys;
113     while (<DATA>) {
114         next unless m/\S/;
115         chomp;
116         my @chrs = split / /, $_;
117         <DATA> !~ m/\S/ or die;
118         foreach my $row (reverse 0..4) {
119             $_ = <DATA>;
120             chomp;
121             s{^}{ };
122             $_ .= ' ' x 8;
123             m{\S/\S} and die;
124             s{/(?=\s)}{L}g;
125             s{/(?=\S)}{r}g;
126             s{\\(?=\s)}{l}g;
127             s{\\(?=\S)}{R}g;
128             p "// $_\n";
129             foreach my $chr (@chrs) {
130                 s{^ }{} or die "$chr $_ ?";
131                 foreach my $col (0..2) {
132                     my @verts;
133                     if (s{^ }{}) {
134                     } elsif (s{^\S}{}) {
135                         my $f = $cellmap{$&};
136                         die unless $f;
137                         $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
138                         $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
139                         push @{ $chrpolys{$chr} }, [ split / /, $f ];
140                     } else {
141                         die "$_ ?";
142                     }
143                 }
144             }
145             die "$_ ?" if m{\S};
146         }    
147     }
148
149     my $demo = '';
150     my $democols = 6;
151     foreach my $chr (sort keys %chrpolys) {
152         my $mod = chrmodname $chr;
153         p "module $mod () {\n";
154         foreach my $poly (@{ $chrpolys{$chr} }) {
155             p " polygon([";
156             my $delim = "";
157             foreach my $pt (@$poly) {
158                 p $delim;
159                 $pt =~ s{\d{5}}{$&,};
160                 $pt =~ s{\b\d}{$&.}g;
161                 p "[$pt]";
162                 $delim = ',';
163             }
164             p "]);\n";
165         }
166         p "}\n";
167         $demo .= $chr;
168     }
169     @demo = reverse $demo =~ m{.{1,$democols}}go;
170 }
171
172 parsefont();
173
174 gentextmodule('Commitid_FontDemo', @demo);
175
176 __DATA__
177
178 # 00 20 22 02
179 l 00 20 02
180 r 00 20 22
181 L 00 22 02
182 R 20 22 02
183 > 00 20 22 02 11
184 < 00 20 11 22 02
185
186 0 1 2 3 4 5 6 7 8 9
187
188 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
189 # # /#    #   # # # #   #     # # # # #
190 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
191 # #  #  #     #   #   # # #  #  # #   #
192 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
193
194 a b c d e f
195
196     #         #     /##
197     #   /##   # /#\ #
198 /## ##\ #   /## #r# ###
199 # # # # #   # # #/  #
200 \## ##/ \## \## \#/ #
201