Debian Codegraph @

common/shell_spool.cc

raw
1 /*****************************************************************************
2 * Free42 -- an HP-42S calculator simulator
3 * Copyright (C) 2004-2026 Thomas Okken
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/licenses/.
16 *****************************************************************************/
17
18 #ifndef ANDROID
19
20 #include <stdlib.h>
21
22 #include "shell_spool.h"
23 #include "core_main.h"
24
25 #endif
26
27 #include "core_helpers.h"
28
29 int hp2ascii(char *dst, const char *src, int srclen) {
30 const char *esc;
31 unsigned char c;
32 int s, d = 0;
33 for (s = 0; s < srclen; s++) {
34 c = src[s];
35 if (undefined_char(c)) {
36 // Escape sequence: euro sign plus two hex digits.
37 // In hp2ascii(), only used for undefined characters;
38 // in ascii2hp(), accepted for everything.
39 dst[d++] = (char) 0xe2;
40 dst[d++] = (char) 0x82;
41 dst[d++] = (char) 0xac;
42 dst[d++] = "0123456789abcdef"[c >> 4];
43 dst[d++] = "0123456789abcdef"[c & 15];
44 continue;
45 }
46 switch (c) {
47 case 0: esc = "\303\267"; break; // division sign
48 case 1: esc = "\303\227"; break; // multiplication sign
49 case 2: esc = "\342\210\232"; break; // square root sign
50 case 3: esc = "\342\210\253"; break; // integral sign
51 case 4: esc = "\342\226\222"; break; // gray rectangle
52 case 5: esc = "\316\243"; break; // uppercase sigma
53 case 6: esc = "\342\226\270"; break; // small right-pointing triangle
54 case 7: esc = "\317\200"; break; // lowercase pi
55 case 8: esc = "\302\277"; break; // upside-down question mark
56 case 9: esc = "\342\211\244"; break; // less-than-or-equals sign
57 case 11: esc = "\342\211\245"; break; // greater-than-or-equals sign
58 case 12: esc = "\342\211\240"; break; // not-equals sign
59 case 13: esc = "\342\206\265"; break; // down-then-left arrow
60 case 14: esc = "\342\206\223"; break; // downward-pointing arrow
61 case 15: esc = "\342\206\222"; break; // right-pointing arrow
62 case 16: esc = "\342\206\220"; break; // left-pointing arrow
63 case 17: esc = "\316\274"; break; // lowercase mu
64 case 18: esc = "\302\243"; break; // pound sterling sign
65 case 19: esc = "\302\260"; break; // degree symbol
66 case 20: esc = "\303\205"; break; // uppercase a with ring
67 case 21: esc = "\303\221"; break; // uppercase n with tilde
68 case 22: esc = "\303\204"; break; // uppercase a with umlaut
69 case 23: esc = "\342\210\241"; break; // measured angle symbol
70 case 24: esc = "\341\264\207"; break; // small-caps e
71 case 25: esc = "\303\206"; break; // uppercase ae ligature
72 case 26: esc = "\342\200\246"; break; // ellipsis
73 case 27: esc = "[ESC]"; break; // EC symbol
74 case 28: esc = "\303\226"; break; // uppercase o with umlaut
75 case 29: esc = "\303\234"; break; // uppercase u with umlaut
76 case 30: esc = "^"; break; // caret
77 case 31: esc = "\342\200\242"; break; // bullet
78 case 94: esc = "\342\206\221"; break; // upward-pointing arrow
79 case 127: esc = "\342\224\234"; break; // append sign
80 case 128: esc = "\342\210\266"; break; // thin colon
81 case 129: esc = "\312\217"; break; // small-caps y
82 case 130: esc = "\316\251"; break; // uppercase omega
83 case 131: esc = "\341\264\233"; break; // small-caps t
84 case 132: esc = "\312\237"; break; // small-caps l
85 case 133: esc = "\341\264\200"; break; // small-caps a
86 case 134: esc = "\342\226\271"; break; // hollow right-pointing triangle
87 case 135: esc = "\342\227\254"; break; // gray right-pointing triangle
88 case 136: esc = "\302\253"; break; // opening guillemet
89 case 137: esc = "\302\273"; break; // closing guillemet
90 case 138: esc = "[LF]"; break; // LF symbol
91 default: dst[d++] = c; continue;
92 }
93 while (*esc != 0)
94 dst[d++] = *esc++;
95 }
96 return d;
97 }
98
99 #ifndef ANDROID
100
101 void shell_spool_txt(const char *text, int length,
102 file_writer writer, file_newliner newliner) {
103 char buf[1000];
104 int d = hp2ascii(buf, text, length);
105 writer(buf, d);
106 newliner();
107 }
108
109 void shell_spool_bitmap_to_txt(const char *bits, int bytesperline,
110 int x, int y, int width, int height,
111 file_writer writer, file_newliner newliner) {
112 for (int v = 0; v < height; v += 2) {
113 for (int h = 0; h < width; h += 2) {
114 int k = 0;
115 for (int vv = 0; vv < 2; vv++) {
116 int vvv = v + vv + y;
117 if (vvv >= height)
118 break;
119 for (int hh = 0; hh < 2; hh++) {
120 int hhh = h + hh + x;
121 if (hhh >= width)
122 break;
123 if ((bits[vvv * bytesperline + (hhh >> 3)] & (1 << (hhh & 7))) != 0)
124 k += 1 << (hh + 2 * vv);
125 }
126 }
127 switch (k) {
128 case 0: writer("\302\240", 2); break;
129 case 1: writer("\342\226\230", 3); break;
130 case 2: writer("\342\226\235", 3); break;
131 case 3: writer("\342\226\200", 3); break;
132 case 4: writer("\342\226\226", 3); break;
133 case 5: writer("\342\226\214", 3); break;
134 case 6: writer("\342\226\236", 3); break;
135 case 7: writer("\342\226\233", 3); break;
136 case 8: writer("\342\226\227", 3); break;
137 case 9: writer("\342\226\232", 3); break;
138 case 10: writer("\342\226\220", 3); break;
139 case 11: writer("\342\226\234", 3); break;
140 case 12: writer("\342\226\204", 3); break;
141 case 13: writer("\342\226\231", 3); break;
142 case 14: writer("\342\226\237", 3); break;
143 case 15: writer("\342\226\210", 3); break;
144 }
145 }
146 newliner();
147 }
148 }
149
150 struct gif_data {
151 int codesize;
152 int bytecount;
153 char buf[255];
154
155 short prefix_table[4096];
156 short code_table[4096];
157 short hash_next[4096];
158 short hash_head[256];
159
160 int maxcode;
161 int clear_code;
162 int end_code;
163
164 int curr_code_size;
165 int prefix;
166 int currbyte;
167 int bits_needed;
168 int initial_clear;
169 int really_done;
170
171 int width;
172 int height;
173 };
174
175 static gif_data *g;
176
177
178 bool shell_start_gif(file_writer writer, int width, int provisional_height) {
179 char buf[29];
180 char *p = buf, c;
181 int height = provisional_height;
182 int i;
183
184 /* NOTE: the height will be set to the *actual* height once we know
185 * what that is, i.e., when shell_finish_gif() is called. We populate
186 * it using the maximum height (as set in the preferences dialog) so
187 * that even incomplete GIF files will be viewable, just in case the
188 * user is impatient and wants to take a peek.
189 */
190
191
192 /* GIF Header */
193
194 *p++ = 'G';
195 *p++ = 'I';
196 *p++ = 'F';
197 *p++ = '8';
198 *p++ = '7';
199 *p++ = 'a';
200
201 /* Screen descriptor */
202
203 *p++ = width & 255;
204 *p++ = width >> 8;
205 *p++ = height & 255;
206 *p++ = height >> 8;
207 *p++ = (char) 0xf0;
208 *p++ = 0;
209 *p++ = 0;
210
211 /* Global color map */
212
213 *p++ = (char) 255;
214 *p++ = (char) 255;
215 *p++ = (char) 255;
216 *p++ = 0;
217 *p++ = 0;
218 *p++ = 0;
219
220 /* Image Descriptor */
221
222 *p++ = ',';
223 *p++ = 0;
224 *p++ = 0;
225 *p++ = 0;
226 *p++ = 0;
227 *p++ = width & 255;
228 *p++ = width >> 8;
229 *p++ = height & 255;
230 *p++ = height >> 8;
231 *p++ = 0x00;
232
233 /* Write GIF header & descriptors */
234
235 writer(buf, 29);
236
237
238 /* Initialize GIF encoder */
239
240 if (g == NULL) {
241 g = (gif_data *) malloc(sizeof(gif_data));
242 if (g == NULL)
243 return false;
244 }
245
246 g->codesize = 2;
247 g->bytecount = 0;
248 g->maxcode = 1 << g->codesize;
249 for (i = 0; i < g->maxcode; i++) {
250 g->prefix_table[i] = -1;
251 g->code_table[i] = i;
252 g->hash_next[i] = -1;
253 }
254 for (i = 0; i < 256; i++)
255 g->hash_head[i] = -1;
256
257 g->clear_code = g->maxcode++;
258 g->end_code = g->maxcode++;
259
260 g->curr_code_size = g->codesize + 1;
261 g->prefix = -1;
262 g->currbyte = 0;
263 g->bits_needed = 8;
264 g->initial_clear = 1;
265 g->really_done = 0;
266
267 g->width = width;
268 g->height = 0;
269
270 c = g->codesize;
271 writer(&c, 1);
272
273 return true;
274 }
275
276 void shell_spool_gif(const char *bits, int bytesperline,
277 int x, int y, int width, int height,
278 file_writer writer) {
279 int v, h;
280 g->height += height;
281
282 /* Encode Image Data */
283
284 for (v = y; v < y + height || (v == y && height == 0); v++) {
285 int done = v == y && height == 0;
286 for (h = 0; h < g->width; h++) {
287 int new_code;
288 unsigned char hash_code;
289 int hash_index;
290 int pixel;
291
292 if (g->really_done) {
293 new_code = g->end_code;
294 goto emit;
295 } else if (done) {
296 new_code = g->prefix;
297 goto emit;
298 }
299
300 if (h < width)
301 pixel = ((bits[bytesperline * v + (h >> 3)]) >> (h & 7)) & 1;
302 else
303 pixel = 0;
304
305 /* Look for concat(prefix, pixel) in string table */
306 if (g->prefix == -1) {
307 g->prefix = pixel;
308 goto no_emit;
309 }
310
311 /* Compute hash code
312 * TODO: There's a lot of room for improvement here!
313 * I'm getting search percentages of over 30%; looking for
314 * something in single digits.
315 */
316 {
317 unsigned long x = (((long) g->prefix) << 20)
318 + (((long) pixel) << 12);
319 unsigned char b1, b2, b3;
320 x /= 997;
321 b1 = (unsigned char) (x >> 16);
322 b2 = (unsigned char) (x >> 8);
323 b3 = (unsigned char) x;
324 hash_code = b1 ^ b2 ^ b3;
325 }
326 hash_index = g->hash_head[hash_code];
327 while (hash_index != -1) {
328 if (g->prefix_table[hash_index] == g->prefix
329 && g->code_table[hash_index] == pixel) {
330 g->prefix = hash_index;
331 goto no_emit;
332 }
333 hash_index = g->hash_next[hash_index];
334 }
335
336 /* Not found: */
337 if (g->maxcode < 4096) {
338 g->prefix_table[g->maxcode] = g->prefix;
339 g->code_table[g->maxcode] = pixel;
340 g->hash_next[g->maxcode] = g->hash_head[hash_code];
341 g->hash_head[hash_code] = g->maxcode;
342 g->maxcode++;
343 }
344 new_code = g->prefix;
345 g->prefix = pixel;
346
347 emit: {
348 int outcode = g->initial_clear ? g->clear_code
349 : g->really_done ? g->end_code : new_code;
350 int bits_available = g->curr_code_size;
351 while (bits_available != 0) {
352 int bits_copied = g->bits_needed < bits_available ?
353 g->bits_needed : bits_available;
354 int bits = outcode >> (g->curr_code_size - bits_available);
355 bits &= 255 >> (8 - bits_copied);
356 g->currbyte |= bits << (8 - g->bits_needed);
357 bits_available -= bits_copied;
358 g->bits_needed -= bits_copied;
359 if (g->bits_needed == 0 ||
360 (bits_available == 0 && g->really_done)) {
361 g->buf[g->bytecount++] = g->currbyte;
362 if (g->bytecount == 255) {
363 char c = g->bytecount;
364 writer(&c, 1);
365 writer(g->buf, g->bytecount);
366 g->bytecount = 0;
367 }
368 if (bits_available == 0 && g->really_done)
369 goto data_done;
370 g->currbyte = 0;
371 g->bits_needed = 8;
372 }
373 }
374
375 if (done) {
376 done = 0;
377 g->really_done = 1;
378 goto emit;
379 }
380 if (g->initial_clear) {
381 g->initial_clear = 0;
382 goto emit;
383 } else {
384 if (g->maxcode > (1 << g->curr_code_size)) {
385 g->curr_code_size++;
386 } else if (new_code == g->clear_code) {
387 int i;
388 g->maxcode = (1 << g->codesize) + 2;
389 g->curr_code_size = g->codesize + 1;
390 for (i = 0; i < 256; i++)
391 g->hash_head[i] = -1;
392 } else if (g->maxcode == 4096) {
393 new_code = g->clear_code;
394 goto emit;
395 }
396 }
397 }
398
399 no_emit:;
400 }
401 }
402
403 data_done:
404 ;
405 }
406
407 void shell_finish_gif(file_seeker seeker, file_writer writer) {
408 char c;
409
410 /* Flush the encoder and write any remaining data */
411
412 shell_spool_gif(NULL, 0, 0, 0, 0, 0, writer);
413
414 if (g->bytecount > 0) {
415 c = g->bytecount;
416 writer(&c, 1);
417 writer(g->buf, g->bytecount);
418 }
419 c = 0;
420 writer(&c, 1);
421
422 /* GIF Trailer */
423
424 c = ';';
425 writer(&c, 1);
426
427 /* Update the 'height' fields in the header, now that at last
428 * we know what the final height is */
429
430 seeker(8);
431 c = g->height & 255;
432 writer(&c, 1);
433 c = g->height >> 8;
434 writer(&c, 1);
435
436 seeker(26);
437 c = g->height & 255;
438 writer(&c, 1);
439 c = g->height >> 8;
440 writer(&c, 1);
441
442 /* All done! */
443 }
444
445 void shell_spool_exit() {
446 if (g != NULL) {
447 free(g);
448 g = NULL;
449 }
450 }
451
452 #endif