Debian Codegraph @

doc/scripts/makeps.pl

raw
1 #!/usr/bin/perl
2 eval 'exec perl -S $0 "$@"'
3 if $running_under_some_shell;
4 # this emulates #! processing on NIH machines.
5 # (remove #! line above if indigestible)
6
7 eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
8 # process any FOO=bar switches
9
10 # makeps - make Postscript-files of the archetypes listed in text file whose
11 # filename is passed in 'input'
12 # Variables passed in:
13 # archdir - root of crossfire-src, with a trailing slash
14 # libdir - where archetypes etc. is found
15
16 $[ = 1; # set array base to 1
17 $, = ' '; # set output field separator
18 $\ = "\n"; # set output record separator
19
20 $size=0.4;
21 $IMAGE_SIZE=32; # Size of PNG images
22
23 if ($output eq "tex") { $BG="\\#ffffff"; }
24 else { $BG="\\#ab0945"; }
25
26 # Set colour to 1 if you want colour postscript.
27 $colour = 0;
28 # IF you have giftrans installed and want transparent gifs, set
29 # appropriately. IT looks much nicer if you can do it.
30 $giftrans = 0;
31
32
33 $bmaps = $libdir . '/bmaps';
34 $bmappaths = $libdir . '/bmaps.paths';
35
36 open(BMAPS,"<".$bmappaths) || die("Can't open $bmappaths");
37 while (<BMAPS>) {
38 ($f1,$f2) = split;
39 if ($f1 ne '#') {
40 # A bit tricky. We first substitute the first .
41 # (./arch to ^/arch), so that the second substitute
42 # puts the 'base' portion in the name, and then we
43 # put the first . back in place.
44 $f2 =~ s/\./\^/;
45 $f2 =~ s/\./\.base\./;
46 $f2 =~ s/\^/\./;
47 $bmappath{$f1} = $f2;
48 }
49 }
50 close(BMAPS);
51
52 open(BMAPS,"<".$bmaps);
53 while (<BMAPS>) {
54 ($f1,$f2) = split;
55 if (defined $bmappath{("\\".$f1)}) {
56 $bmap{$f2} = $bmappath{("\\".$f1)};
57 }
58 }
59 close(BMAPS);
60
61 # An array listing which archetypes files need fixing, the value
62 # is the file where it is used. There must be at least one character
63 # between the ~~spec~~'s.
64
65
66 open(IN,"<".$input) || die("can not open $input\n");
67 while (<IN>) {
68 @flds = split(/~~/);
69 $work_todo = 1;
70 $i = 2;
71 while ($flds[$i] ne "") {
72 $makeps{$flds[$i]} = 0;
73 $i += 2;
74 }
75 }
76 close(IN);
77
78 if ($output ne "png") {
79 # An array to reduce the size of the bitmap exponentially.
80 # A 4x8 bitmap will be reduced to 60% of its full size.
81 if ($work_todo) {
82 $size_mul{1} = 1;
83 for ($i = 2; $i <= 12; $i++) {# Max input is 12x12, a *large* bitmap ;-)
84 $size_mul{$i} = $size_mul{$i - 1} * 0.9;
85 }
86 }
87 }
88
89
90 $More = 0;
91 print STDERR "starting to process $inarch\n";
92 open(IN,"<".$inarch) || die("could not open $inarch\n");
93 line: while (<IN>) {
94 chomp; # strip record separator
95 @Fld = split(/ /, $_, 2);
96 if ($Fld[1] eq 'Object') {
97 if ($interesting) {
98 $faces{$X, $Y} = $face;
99 if (!$More && $makeps{$obj} != 1) {
100 $makeps{$obj} = &assemble();
101 }
102 }
103
104 # Get ready for next archetype
105 if (!$More) {
106 $xmin = $xmax = $ymin = $ymax = 0;
107 $obj = $Fld[2];
108 $interesting = defined $makeps{$obj};
109 }
110 $X = $Y = 0;
111 $More = 0;
112 }
113
114 if ($Fld[1] eq 'face') {
115 $face = $Fld[2];
116 }
117 if ($Fld[1] eq 'x') {
118 $X = $Fld[2];
119 if ($X > $xmax) { #???
120 $xmax = $X;
121 }
122 elsif ($X < $xmin) { #???
123 $xmin = $X;
124 }
125 }
126 if ($Fld[1] eq 'y') {
127 $Y = $Fld[2];
128 if ($Y > $ymax) { #???
129 $ymax = $Y;
130 }
131 elsif ($Y < $ymin) { #???
132 $ymin = $Y;
133 }
134 }
135 if ($Fld[1] eq 'More') {
136 $More = 1;
137 }
138 if ($Fld[1] eq 'msg') {
139 do {
140 $_ = <IN>;
141 @Fld = split;
142 }
143 while ($Fld[1] ne 'endmsg');
144 }
145 }
146 close(IN);
147
148 # Remember to check the last archetype also...
149 if ($interesting) {
150 $faces{$X, $Y} = $face;
151 if ($makeps{$obj} != 1) {
152 $makeps{$obj} = &assemble();
153 }
154 }
155
156 system('rm -f work.pbm tmp.pbm empty.pbm');
157 # clean up a little
158 system("pbmmake -white $IMAGE_SIZE $IMAGE_SIZE > empty.pbm");
159
160 # We've created a number of Postscript-files - now we need to
161 # patch the filenames and sizes into the TeX-files.
162
163 $, = '';
164 open(IN,"<".$input);
165 while (<IN>) {
166 @Fld = split(/~~/);
167 if ($#Fld > 1) {
168 for ($i = 2; $i <= $#Fld; $i += 2) {
169 if (defined $makeps{$Fld[$i]}) {
170 $Fld[$i] = $makeps{$Fld[$i]};
171 }
172 }
173 }
174 print @Fld;
175 }
176 close(IN);
177
178
179 sub assemble {
180 local($w, $h, $ppm, $buff, $i, $j, $bmap_file, $ps_file) = @_;
181 my($one_image)=0;
182
183 $bmap_file = $archdir.$bmap{$faces{0,0}}.".png";
184 if ($output eq "tex") {$ps_file = $faces{0, 0} . '.ps'; }
185 elsif ($output eq "png") { $ps_file = $faces{0, 0} . '.png'; }
186 elsif ($output eq "pdf") {
187 $tmp = $faces{0, 0};
188 $tmp =~ s/\./-/gi;
189 $ps_file = $tmp . '.png';
190 } else { $ps_file = $faces{0, 0} . '.gif'; }
191
192 $ps_file =~ s/[_ ]/-/g;
193
194 # We don't need to manipulate the files, so just do hard links - much
195 # faster, and also doesn't use space.
196 if (($output eq "png") || ($output eq "pdf")) {
197 link($bmap_file, $ps_file);
198 if ($output eq "png") {
199 $ps = "<img src=$ps_file>";
200 } else {
201 $ps = "\\includegraphics[scale=0.5]{" . $ps_file . "}";
202 }
203 return $ps;
204 }
205
206 $w = $xmax - $xmin + 1;
207 $h = $ymax - $ymin + 1;
208
209 # with big image support, we don't need to assemble images. But not all
210 # images are big image - so we do a simple check - see if the face for the
211 # first and last piece are the same - if so, presume this is a big image
212 if ($archdir.$bmap{$faces{0,0}} eq $archdir.$bmap{$faces{$w-1,$h-1}}) { $one_image=1; }
213
214 if (! -e $ps_file) {
215 if ((($w == 1) && ($h == 1)) || $one_image) {
216 # Maybe ln -s instead?
217 if ($output eq "tex") {
218 if ($colour) { system("pngtopnm -mix -background $BG $bmap_file | pnmtops -noturn -nosetpage > $ps_file"); }
219 else { system("pngtopnm -mix -background $BG $bmap_file | pnmdepth 255 | ppmtopgm | pnmtops -noturn -nosetpage> $ps_file"); }
220 }
221 elsif ($giftrans) {
222 system("pngtopnm -mix -background $BG $bmap_file | ppmtogif | giftrans -t $BG $ppm > $ps_file");
223 } else {
224 system("pngtopnm -mix -background $BG $bmap_file | ppmtogif > $ps_file");
225 }
226 }
227 else {
228 $ppm = sprintf('%dx%d.ppm', $w, $h);
229 print STDERR "$ppm\n";
230 if (! -e $ppm) {
231 print STDERR
232 "pnmscale -xsc $w -ysc $h < empty.pbm | pgmtoppm white > $ppm\n";
233
234 system(sprintf('pnmscale -xsc %d -ysc %d < empty.pbm | pgmtoppm white > %s',
235 $w, $h, $ppm));
236 }
237
238 system("cp $ppm work.ppm");
239 $ppm = "work.ppm";
240
241 for ($i = $xmin; $i <= $xmax; $i++) {
242 for ($j = $ymin; $j <= $ymax; $j++) {
243 print STDERR
244 'Processing x ' . $bmap{$faces{$i, $j}};
245 $valx = ($i - $xmin) * $IMAGE_SIZE;
246 $valy = ($j - $ymin) * $IMAGE_SIZE;
247 # print STDERR "pngtopnm -background #ABCD01239876 $archdir$bmap{$faces{$i,$j}}.png > tmp.ppm\n";
248 system("pngtopnm -mix -background $BG $archdir$bmap{$faces{$i,$j}}.png > tmp.ppm");
249 system("pnmpaste tmp.ppm $valx $valy $ppm > tmp2.ppm");
250 rename("tmp2.ppm", $ppm);
251 }
252 }
253 if ($output eq "tex") {
254 if ($colour) { system("pnmtops -noturn $ppm> $ps_file"); }
255 else { system("pnmdepth 255 $ppm | ppmtopgm | pnmtops -noturn > $ps_file"); }
256 }
257 elsif ($giftrans) {
258 system("ppmtogif $ppm | giftrans -t $BG > $ps_file");
259 } else {
260 system("ppmtogif $ppm > $ps_file");
261 }
262 }
263 }
264 $mul = $size_mul{int(sqrt($w * $h))} * $size;
265 if ($output eq "tex") {
266 if ($h == 1) {
267 $ps = sprintf "\\psfig{figure=$ps_file,width=%0.2fcm,height=%0.2fcm}", $w * $mul, $h * $mul;
268 } else {
269 $ps = sprintf "\\raisebox{-%0.2fcm}{\\psfig{figure=$ps_file,width=%0.2fcm,height=%0.2fcm}}", ($h-1) * $mul, $w * $mul, $h * $mul;
270 }
271 } else {
272 $ps = "<img src=$ps_file>";
273 }
274 $ps;
275 }