chiark / gitweb /
ee9be882723147703c2054fff2e1151a51ea7c44
[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 parsefont () {
82     my %cellmap;
83     for (;;) {
84         $_ = <DATA> // die;
85         last if %cellmap && !m/\S/;
86         next unless m/\S/;
87         chomp;
88         s{^(.) }{};
89         $cellmap{$1} = $_;
90     }
91     my %chrpolys;
92     while (<DATA>) {
93         next unless m/\S/;
94         my @chrs = split / /, $_;
95         <DATA> !~ m/\S/ or die;
96         foreach my $row (reverse 0..4) {
97             $_ = <DATA>;
98             chomp;
99             s{^}{ };
100             $_ .= ' ' x 8;
101             m{\S/\S} and die;
102             s{/(?=\s)}{L}g;
103             s{/(?=\S)}{r}g;
104             s{\\(?=\s)}{l}g;
105             s{\\(?=\S)}{R}g;
106             p "// $_\n";
107             foreach my $chr (@chrs) {
108                 s{^ }{} or die "$chr $_ ?";
109                 foreach my $col (0..2) {
110                     my @verts;
111                     if (s{^ }{}) {
112                     } elsif (s{^\S}{}) {
113                         my $f = $cellmap{$&};
114                         die unless $f;
115                         $f =~ s/\b\d/ sprintf '%05d', $col*2000 + $&*1025 /ge;
116                         $f =~ s/\d\b/ sprintf '%05d', $row*2000 + $&*1025 /ge;
117                         push @{ $chrpolys{$chr} }, [ split / /, $f ];
118                     } else {
119                         die "$_ ?";
120                     }
121                 }
122             }
123             die "$_ ?" if m{\S};
124         }    
125     }
126
127     my @demo;
128     my $democols = 6;
129     foreach my $chr (sort keys %chrpolys) {
130         my $mod = chrmodname $chr;
131         p "module $mod () {\n";
132         foreach my $poly (@{ $chrpolys{$chr} }) {
133             p " polygon([";
134             my $delim = "";
135             foreach my $pt (@$poly) {
136                 p $delim;
137                 $pt =~ s{\d{5}}{$&,};
138                 $pt =~ s{\b\d}{$&.}g;
139                 p "[$pt]";
140                 $delim = ',';
141             }
142             p "]);\n";
143         }
144         p "}\n";
145         my $px = @demo % $democols;
146         my $py = int(@demo / $democols);
147         push @demo, " scale( Commitid__scale() ) translate([$px * 0.800, $py * 1.200]) 
148  $mod ();\n";
149     }
150     p "module Commitid_FontDemo(){\n";
151     p $_ foreach @demo;
152     p "}\n";
153
154 #    use Data::Dumper;
155 #    print Dumper(\%chrpolys);
156 }
157
158 parsefont();
159
160 #p "Commitid_FontDemo();\n";
161
162 __DATA__
163
164 # 00 20 22 02
165 l 00 20 02
166 r 00 20 22
167 L 00 22 02
168 R 20 22 02
169 > 00 20 22 02 11
170 < 00 20 11 22 02
171
172 0 1 2 3 4 5 6 7 8 9
173
174 /#\  r  /#\ ##\ # # ### //  ### /#\ /#\
175 # # /#    #   # # # #   #     # # # # #
176 # #  #  /#/ ##< \## ##\ ##\  // >#< \##
177 # #  #  #     #   #   # # #  #  # #   #
178 \#/ /#\ ### ##/   # ##/ \#/  #  \#/ ##/
179
180 a b c d e f
181
182     #         #     /##
183     #   /##   # /#\ #
184 /## ##\ #   /## #r# ###
185 # # # # #   # # #/  #
186 \## ##/ \## \## \#/ #
187