1 | /* |
---|
2 | * libcucul Canvas for ultrafast compositing of Unicode letters |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: import.c 875 2006-04-25 14:12:31Z sam $ |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * This file contains various import functions. |
---|
16 | */ |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | #include "common.h" |
---|
20 | |
---|
21 | #if !defined(__KERNEL__) |
---|
22 | # include <stdio.h> |
---|
23 | # include <stdlib.h> |
---|
24 | # include <string.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | #include "cucul.h" |
---|
28 | #include "cucul_internals.h" |
---|
29 | |
---|
30 | static cucul_canvas_t *import_caca(void const *, unsigned int); |
---|
31 | static cucul_canvas_t *import_text(void const *, unsigned int); |
---|
32 | static cucul_canvas_t *import_ansi(void const *, unsigned int); |
---|
33 | |
---|
34 | /** \brief Import a buffer into a canvas |
---|
35 | * |
---|
36 | * This function imports a memory area into an internal libcucul canvas. |
---|
37 | * |
---|
38 | * Valid values for \c format are: |
---|
39 | * |
---|
40 | * \li \c "": attempt to autodetect the file format. |
---|
41 | * |
---|
42 | * \li \c "caca": import native libcaca files. |
---|
43 | * |
---|
44 | * \param data The memory area to be loaded into a canvas. |
---|
45 | * \param size The length of the memory area. |
---|
46 | * \param format A string describing the input format. |
---|
47 | * \return A libcucul canvas, or NULL in case of error. |
---|
48 | */ |
---|
49 | cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size, |
---|
50 | char const *format) |
---|
51 | { |
---|
52 | char const *buf = (char const*) data; |
---|
53 | |
---|
54 | if(size==0 || data==NULL) |
---|
55 | return NULL; |
---|
56 | |
---|
57 | if(!strcasecmp("caca", format)) |
---|
58 | return import_caca(data, size); |
---|
59 | if(!strcasecmp("text", format)) |
---|
60 | return import_text(data, size); |
---|
61 | if(!strcasecmp("ansi", format)) |
---|
62 | return import_ansi(data, size); |
---|
63 | |
---|
64 | /* Autodetection */ |
---|
65 | if(!strcasecmp("", format)) |
---|
66 | { |
---|
67 | /* if 4 first letters are CACA */ |
---|
68 | if(size >= 4 && |
---|
69 | buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A') |
---|
70 | return import_caca(data, size); |
---|
71 | |
---|
72 | /* If 2 first characters are ESC[ (not reliable at all) */ |
---|
73 | if(size >= 2 && buf[0] == 0x1b && buf[1] == '[') |
---|
74 | return import_ansi(data, size); |
---|
75 | |
---|
76 | /* Otherwise, import it as text */ |
---|
77 | return import_text(data, size); |
---|
78 | } |
---|
79 | return NULL; |
---|
80 | } |
---|
81 | |
---|
82 | /** \brief Get available import formats |
---|
83 | * |
---|
84 | * Return a list of available import formats. The list is a NULL-terminated |
---|
85 | * array of strings, interleaving a string containing the internal value for |
---|
86 | * the import format, to be used with cucul_import_canvas(), and a string |
---|
87 | * containing the natural language description for that import format. |
---|
88 | * |
---|
89 | * \return An array of strings. |
---|
90 | */ |
---|
91 | char const * const * cucul_get_import_list(void) |
---|
92 | { |
---|
93 | static char const * const list[] = |
---|
94 | { |
---|
95 | "", "autodetect", |
---|
96 | "text", "plain text", |
---|
97 | "caca", "native libcaca format", |
---|
98 | "ansi", "ANSI coloured text", |
---|
99 | NULL, NULL |
---|
100 | }; |
---|
101 | |
---|
102 | return list; |
---|
103 | } |
---|
104 | |
---|
105 | /* |
---|
106 | * XXX: the following functions are local. |
---|
107 | */ |
---|
108 | |
---|
109 | static cucul_canvas_t *import_caca(void const *data, unsigned int size) |
---|
110 | { |
---|
111 | cucul_canvas_t *cv; |
---|
112 | uint8_t const *buf = (uint8_t const *)data; |
---|
113 | unsigned int width, height, n; |
---|
114 | |
---|
115 | if(size < 16) |
---|
116 | return NULL; |
---|
117 | |
---|
118 | if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A') |
---|
119 | return NULL; |
---|
120 | |
---|
121 | if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V') |
---|
122 | return NULL; |
---|
123 | |
---|
124 | width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16) |
---|
125 | | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11]; |
---|
126 | height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16) |
---|
127 | | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15]; |
---|
128 | |
---|
129 | if(!width || !height) |
---|
130 | return NULL; |
---|
131 | |
---|
132 | if(size != 16 + width * height * 8) |
---|
133 | return NULL; |
---|
134 | |
---|
135 | cv = cucul_create_canvas(width, height); |
---|
136 | |
---|
137 | if(!cv) |
---|
138 | return NULL; |
---|
139 | |
---|
140 | for(n = height * width; n--; ) |
---|
141 | { |
---|
142 | cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24) |
---|
143 | | ((uint32_t)buf[16 + 1 + 8 * n] << 16) |
---|
144 | | ((uint32_t)buf[16 + 2 + 8 * n] << 8) |
---|
145 | | (uint32_t)buf[16 + 3 + 8 * n]; |
---|
146 | cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24) |
---|
147 | | ((uint32_t)buf[16 + 5 + 8 * n] << 16) |
---|
148 | | ((uint32_t)buf[16 + 6 + 8 * n] << 8) |
---|
149 | | (uint32_t)buf[16 + 7 + 8 * n]; |
---|
150 | } |
---|
151 | |
---|
152 | return cv; |
---|
153 | } |
---|
154 | |
---|
155 | static cucul_canvas_t *import_text(void const *data, unsigned int size) |
---|
156 | { |
---|
157 | cucul_canvas_t *cv; |
---|
158 | char const *text = (char const *)data; |
---|
159 | unsigned int width = 1, height = 1, x = 0, y = 0, i; |
---|
160 | |
---|
161 | cv = cucul_create_canvas(width, height); |
---|
162 | cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT); |
---|
163 | |
---|
164 | for(i = 0; i < size; i++) |
---|
165 | { |
---|
166 | unsigned char ch = *text++; |
---|
167 | |
---|
168 | if(ch == '\r') |
---|
169 | continue; |
---|
170 | |
---|
171 | if(ch == '\n') |
---|
172 | { |
---|
173 | x = 0; |
---|
174 | y++; |
---|
175 | continue; |
---|
176 | } |
---|
177 | |
---|
178 | while(x >= width) |
---|
179 | { |
---|
180 | width++; |
---|
181 | cucul_set_canvas_size(cv, width, height); |
---|
182 | } |
---|
183 | |
---|
184 | while(y >= height) |
---|
185 | { |
---|
186 | height++; |
---|
187 | cucul_set_canvas_size(cv, width, height); |
---|
188 | } |
---|
189 | |
---|
190 | cucul_putchar(cv, x, y, ch); |
---|
191 | x++; |
---|
192 | } |
---|
193 | |
---|
194 | return cv; |
---|
195 | } |
---|
196 | |
---|
197 | #define IS_ALPHA(x) (x>='A' && x<='z') |
---|
198 | void updateCanvasSize(cucul_canvas_t *cv, int x, int y, int *max_x, int *max_y); |
---|
199 | unsigned char get_ansi_command(unsigned char const *buffer, int size); |
---|
200 | int parse_tuple(unsigned int *ret, unsigned char const *buffer, int size); |
---|
201 | void manage_modifiers(char c, int *fg, int *bg, int *old_fg, int *old_bg); |
---|
202 | |
---|
203 | static cucul_canvas_t *import_ansi(void const *data, unsigned int size) |
---|
204 | { |
---|
205 | cucul_canvas_t *cv; |
---|
206 | unsigned char const *buffer = (unsigned char const*)data; |
---|
207 | unsigned int i, sent_size = 0; |
---|
208 | unsigned char c; |
---|
209 | unsigned int count = 0; |
---|
210 | unsigned int tuple[1024]; /* Should be enough. Will it be ? */ |
---|
211 | int x = 0, y = 0, max_x = 80, max_y = 25; |
---|
212 | int save_x = 0, save_y = 0; |
---|
213 | unsigned int j, add = 0; |
---|
214 | int fg, bg, old_fg, old_bg; |
---|
215 | |
---|
216 | fg = old_fg = CUCUL_COLOR_LIGHTGRAY; |
---|
217 | bg = old_bg = CUCUL_COLOR_BLACK; |
---|
218 | |
---|
219 | cv = cucul_create_canvas(max_x, max_y); |
---|
220 | |
---|
221 | for(i = 0; i < size; i++) |
---|
222 | { |
---|
223 | if((buffer[i] == 0x1b) && (buffer[i + 1] == '[')) /* ESC code */ |
---|
224 | { |
---|
225 | i++; // ESC |
---|
226 | i++; // [ |
---|
227 | sent_size = size - i; |
---|
228 | c = get_ansi_command(&buffer[i], sent_size); |
---|
229 | add = parse_tuple(tuple, &buffer[i], sent_size); |
---|
230 | count = 0; |
---|
231 | |
---|
232 | while(tuple[count] != 0x1337) |
---|
233 | count++; /* Gruik */ |
---|
234 | |
---|
235 | switch(c) |
---|
236 | { |
---|
237 | case 'f': |
---|
238 | case 'H': |
---|
239 | if(tuple[0] != 0x1337) |
---|
240 | { |
---|
241 | x = tuple[0]; |
---|
242 | if(tuple[1] != 0x1337) |
---|
243 | y = tuple[1]; |
---|
244 | } |
---|
245 | else |
---|
246 | { |
---|
247 | x = 0; |
---|
248 | y = 0; |
---|
249 | } |
---|
250 | updateCanvasSize(cv, x, y, &max_x, &max_y); |
---|
251 | break; |
---|
252 | case 'A': |
---|
253 | if(tuple[0] == 0x1337) |
---|
254 | y -= 1; |
---|
255 | else |
---|
256 | y -= tuple[0]; |
---|
257 | if(y < 0) y = 0; |
---|
258 | updateCanvasSize(cv, x, y, &max_x, &max_y); |
---|
259 | break; |
---|
260 | case 'B': |
---|
261 | if(tuple[0] == 0x1337) |
---|
262 | y++; |
---|
263 | else |
---|
264 | y += tuple[0]; |
---|
265 | updateCanvasSize(cv, x, y, &max_x, &max_y); |
---|
266 | break; |
---|
267 | case 'C': |
---|
268 | if(tuple[0] == 0x1337) |
---|
269 | x++; |
---|
270 | else |
---|
271 | x += tuple[0]; |
---|
272 | updateCanvasSize(cv, x, y, &max_x, &max_y); |
---|
273 | break; |
---|
274 | case 'D': |
---|
275 | if(tuple[0] == 0x1337) |
---|
276 | x--; |
---|
277 | else |
---|
278 | x -= tuple[0]; |
---|
279 | if(x < 0) x = 0; |
---|
280 | updateCanvasSize(cv, x, y, &max_x, &max_y); |
---|
281 | break; |
---|
282 | case 's': |
---|
283 | save_x = x; |
---|
284 | save_y = y; |
---|
285 | break; |
---|
286 | case 'u': |
---|
287 | x = save_x; |
---|
288 | y = save_y; |
---|
289 | updateCanvasSize(cv, x, y, &max_x, &max_y); |
---|
290 | break; |
---|
291 | case 'J': |
---|
292 | if(tuple[0] == 2) |
---|
293 | { |
---|
294 | x = 0; |
---|
295 | y = 0; |
---|
296 | updateCanvasSize(cv, x, y, &max_x, &max_y); |
---|
297 | } |
---|
298 | break; |
---|
299 | case 'K': |
---|
300 | // CLEAR END OF LINE |
---|
301 | break; |
---|
302 | case 'm': |
---|
303 | for(j = 0; j < count; j++) |
---|
304 | manage_modifiers(tuple[j], &fg, &bg, &old_fg, &old_bg); |
---|
305 | cucul_set_color(cv, fg, bg); |
---|
306 | break; |
---|
307 | default: |
---|
308 | /*printf("Unknow command %c (%c)\n", c, IS_ALPHA(c)?c:'.');*/ |
---|
309 | break; |
---|
310 | } |
---|
311 | } |
---|
312 | else |
---|
313 | { |
---|
314 | if(buffer[i] == '\n') |
---|
315 | { |
---|
316 | x = 0; |
---|
317 | y++; |
---|
318 | updateCanvasSize(cv, x, y, &max_x, &max_y); |
---|
319 | } |
---|
320 | else if(buffer[i] == '\r') |
---|
321 | { |
---|
322 | // DOS sucks. |
---|
323 | } |
---|
324 | else |
---|
325 | { |
---|
326 | if((buffer[i] >= 0x20) || (buffer[i] <= 0x7E)) |
---|
327 | { |
---|
328 | _cucul_putchar32(cv, x, y,_cucul_cp437_to_utf32(buffer[i])); |
---|
329 | |
---|
330 | // cucul_putchar(cv, x, y, buffer[i]); |
---|
331 | } |
---|
332 | else |
---|
333 | cucul_putchar(cv, x, y, '?'); |
---|
334 | x++; |
---|
335 | updateCanvasSize(cv, x, y, &max_x, &max_y); |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | i += add; // add is tuple char count, then +[ +command |
---|
340 | add = 0; |
---|
341 | } |
---|
342 | |
---|
343 | return cv; |
---|
344 | } |
---|
345 | |
---|
346 | /* XXX : ANSI loader helpers */ |
---|
347 | |
---|
348 | unsigned char get_ansi_command(unsigned char const *buffer, int size) |
---|
349 | { |
---|
350 | int i; |
---|
351 | |
---|
352 | for(i = 0; i < size; i++) |
---|
353 | if(IS_ALPHA(buffer[i])) |
---|
354 | return buffer[i]; |
---|
355 | |
---|
356 | return 0; |
---|
357 | } |
---|
358 | |
---|
359 | int parse_tuple(unsigned int *ret, unsigned char const *buffer, int size) |
---|
360 | { |
---|
361 | int i = 0; |
---|
362 | int j = 0; |
---|
363 | int t = 0; |
---|
364 | unsigned char nbr[1024]; |
---|
365 | |
---|
366 | ret[0] = 0x1337; |
---|
367 | |
---|
368 | for(i = 0; i < size; i++) |
---|
369 | { |
---|
370 | if(IS_ALPHA(buffer[i])) |
---|
371 | { |
---|
372 | if(j != 0) |
---|
373 | { |
---|
374 | ret[t] = atoi((char*)nbr); |
---|
375 | t++; |
---|
376 | } |
---|
377 | ret[t] = 0x1337; |
---|
378 | j = 0; |
---|
379 | return i; |
---|
380 | } |
---|
381 | |
---|
382 | if(buffer[i] != ';') |
---|
383 | { |
---|
384 | nbr[j] = buffer[i]; |
---|
385 | nbr[j + 1] = 0; |
---|
386 | j++; |
---|
387 | } |
---|
388 | else |
---|
389 | { |
---|
390 | ret[t] = atoi((char*)nbr); |
---|
391 | t++; |
---|
392 | ret[t] = 0x1337; |
---|
393 | j = 0; |
---|
394 | } |
---|
395 | } |
---|
396 | return size; |
---|
397 | } |
---|
398 | |
---|
399 | |
---|
400 | |
---|
401 | void manage_modifiers(char c, int *fg, int *bg, int *old_fg, int *old_bg) |
---|
402 | { |
---|
403 | switch(c) |
---|
404 | { |
---|
405 | case 0: |
---|
406 | *fg = CUCUL_COLOR_DEFAULT; |
---|
407 | *bg = CUCUL_COLOR_DEFAULT; |
---|
408 | break; |
---|
409 | case 1: // BOLD |
---|
410 | break; |
---|
411 | case 4: // Underline |
---|
412 | break; |
---|
413 | case 5: // blink |
---|
414 | break; |
---|
415 | case 7: // reverse |
---|
416 | *fg = 15 - *fg; |
---|
417 | *bg = 15 - *bg; |
---|
418 | break; |
---|
419 | case 8: // invisible |
---|
420 | *old_fg = *fg; |
---|
421 | *old_bg = *bg; |
---|
422 | *fg = CUCUL_COLOR_TRANSPARENT; |
---|
423 | *bg = CUCUL_COLOR_TRANSPARENT; |
---|
424 | break; |
---|
425 | case 28: // not invisible |
---|
426 | *fg = *old_fg; |
---|
427 | *bg = *old_bg; |
---|
428 | break; |
---|
429 | case 30: *fg = CUCUL_COLOR_BLACK; break; |
---|
430 | case 31: *fg = CUCUL_COLOR_RED; break; |
---|
431 | case 32: *fg = CUCUL_COLOR_GREEN; break; |
---|
432 | case 33: *fg = CUCUL_COLOR_BROWN; break; |
---|
433 | case 34: *fg = CUCUL_COLOR_BLUE; break; |
---|
434 | case 35: *fg = CUCUL_COLOR_MAGENTA; break; |
---|
435 | case 36: *fg = CUCUL_COLOR_CYAN; break; |
---|
436 | case 37: *fg = CUCUL_COLOR_WHITE; break; |
---|
437 | case 39: *fg = CUCUL_COLOR_LIGHTGRAY; break; |
---|
438 | case 40: *bg = CUCUL_COLOR_BLACK; break; |
---|
439 | case 41: *bg = CUCUL_COLOR_RED; break; |
---|
440 | case 42: *bg = CUCUL_COLOR_GREEN; break; |
---|
441 | case 43: *bg = CUCUL_COLOR_BROWN; break; |
---|
442 | case 44: *bg = CUCUL_COLOR_BLUE; break; |
---|
443 | case 45: *bg = CUCUL_COLOR_MAGENTA; break; |
---|
444 | case 46: *bg = CUCUL_COLOR_CYAN; break; |
---|
445 | case 47: *bg = CUCUL_COLOR_WHITE; break; |
---|
446 | case 49: *bg = CUCUL_COLOR_BLACK; break; |
---|
447 | |
---|
448 | case 90: *fg = CUCUL_COLOR_DARKGRAY; break; |
---|
449 | case 91: *fg = CUCUL_COLOR_LIGHTRED; break; |
---|
450 | case 92: *fg = CUCUL_COLOR_LIGHTGREEN; break; |
---|
451 | case 93: *fg = CUCUL_COLOR_YELLOW; break; |
---|
452 | case 94: *fg = CUCUL_COLOR_LIGHTBLUE; break; |
---|
453 | case 95: *fg = CUCUL_COLOR_LIGHTMAGENTA; break; |
---|
454 | case 96: *fg = CUCUL_COLOR_LIGHTCYAN; break; |
---|
455 | case 97: *fg = CUCUL_COLOR_WHITE; break; |
---|
456 | case 100: *bg = CUCUL_COLOR_DARKGRAY; break; |
---|
457 | case 101: *bg = CUCUL_COLOR_LIGHTRED; break; |
---|
458 | case 102: *bg = CUCUL_COLOR_LIGHTGREEN; break; |
---|
459 | case 103: *bg = CUCUL_COLOR_YELLOW; break; |
---|
460 | case 104: *bg = CUCUL_COLOR_LIGHTBLUE; break; |
---|
461 | case 105: *bg = CUCUL_COLOR_LIGHTMAGENTA; break; |
---|
462 | case 106: *bg = CUCUL_COLOR_LIGHTCYAN; break; |
---|
463 | case 107: *bg = CUCUL_COLOR_WHITE; break; |
---|
464 | |
---|
465 | default: |
---|
466 | /* printf("Unknow option to 'm' %d (%c)\n", c, IS_ALPHA(c)?c:'.'); */ |
---|
467 | break; |
---|
468 | } |
---|
469 | } |
---|
470 | |
---|
471 | void updateCanvasSize(cucul_canvas_t *cv, int x, int y, int *max_x, int *max_y) |
---|
472 | { |
---|
473 | if(x > *max_x) |
---|
474 | *max_x = x; |
---|
475 | |
---|
476 | if(y > *max_y) |
---|
477 | *max_y = y; |
---|
478 | |
---|
479 | cucul_set_canvas_size(cv, *max_x, *max_y); |
---|
480 | } |
---|
481 | |
---|