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 886 2006-04-26 07:57:26Z 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 "ansi": import ANSI files. |
---|
43 | * |
---|
44 | * \li \c "caca": import native libcaca files. |
---|
45 | * |
---|
46 | * \param data The memory area to be loaded into a canvas. |
---|
47 | * \param size The length of the memory area. |
---|
48 | * \param format A string describing the input format. |
---|
49 | * \return A libcucul canvas, or NULL in case of error. |
---|
50 | */ |
---|
51 | cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size, |
---|
52 | char const *format) |
---|
53 | { |
---|
54 | char const *buf = (char const*) data; |
---|
55 | |
---|
56 | if(size==0 || data==NULL) |
---|
57 | return NULL; |
---|
58 | |
---|
59 | if(!strcasecmp("caca", format)) |
---|
60 | return import_caca(data, size); |
---|
61 | if(!strcasecmp("text", format)) |
---|
62 | return import_text(data, size); |
---|
63 | if(!strcasecmp("ansi", format)) |
---|
64 | return import_ansi(data, size); |
---|
65 | |
---|
66 | /* Autodetection */ |
---|
67 | if(!strcasecmp("", format)) |
---|
68 | { |
---|
69 | unsigned int i=0; |
---|
70 | /* if 4 first letters are CACA */ |
---|
71 | if(size >= 4 && |
---|
72 | buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A') |
---|
73 | return import_caca(data, size); |
---|
74 | |
---|
75 | /* If we find ESC[ tuple, we guess it's an ANSI file */ |
---|
76 | while(i<size-1) |
---|
77 | { |
---|
78 | if((buf[i] == 0x1b) && (buf[i+1] == '[')) |
---|
79 | return import_ansi(data, size); |
---|
80 | i++; |
---|
81 | } |
---|
82 | |
---|
83 | /* Otherwise, import it as text */ |
---|
84 | return import_text(data, size); |
---|
85 | } |
---|
86 | return NULL; |
---|
87 | } |
---|
88 | |
---|
89 | /** \brief Get available import formats |
---|
90 | * |
---|
91 | * Return a list of available import formats. The list is a NULL-terminated |
---|
92 | * array of strings, interleaving a string containing the internal value for |
---|
93 | * the import format, to be used with cucul_import_canvas(), and a string |
---|
94 | * containing the natural language description for that import format. |
---|
95 | * |
---|
96 | * \return An array of strings. |
---|
97 | */ |
---|
98 | char const * const * cucul_get_import_list(void) |
---|
99 | { |
---|
100 | static char const * const list[] = |
---|
101 | { |
---|
102 | "", "autodetect", |
---|
103 | "text", "plain text", |
---|
104 | "caca", "native libcaca format", |
---|
105 | "ansi", "ANSI coloured text", |
---|
106 | NULL, NULL |
---|
107 | }; |
---|
108 | |
---|
109 | return list; |
---|
110 | } |
---|
111 | |
---|
112 | /* |
---|
113 | * XXX: the following functions are local. |
---|
114 | */ |
---|
115 | |
---|
116 | static cucul_canvas_t *import_caca(void const *data, unsigned int size) |
---|
117 | { |
---|
118 | cucul_canvas_t *cv; |
---|
119 | uint8_t const *buf = (uint8_t const *)data; |
---|
120 | unsigned int width, height, n; |
---|
121 | |
---|
122 | if(size < 16) |
---|
123 | return NULL; |
---|
124 | |
---|
125 | if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A') |
---|
126 | return NULL; |
---|
127 | |
---|
128 | if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V') |
---|
129 | return NULL; |
---|
130 | |
---|
131 | width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16) |
---|
132 | | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11]; |
---|
133 | height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16) |
---|
134 | | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15]; |
---|
135 | |
---|
136 | if(!width || !height) |
---|
137 | return NULL; |
---|
138 | |
---|
139 | if(size != 16 + width * height * 8) |
---|
140 | return NULL; |
---|
141 | |
---|
142 | cv = cucul_create_canvas(width, height); |
---|
143 | |
---|
144 | if(!cv) |
---|
145 | return NULL; |
---|
146 | |
---|
147 | for(n = height * width; n--; ) |
---|
148 | { |
---|
149 | cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24) |
---|
150 | | ((uint32_t)buf[16 + 1 + 8 * n] << 16) |
---|
151 | | ((uint32_t)buf[16 + 2 + 8 * n] << 8) |
---|
152 | | (uint32_t)buf[16 + 3 + 8 * n]; |
---|
153 | cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24) |
---|
154 | | ((uint32_t)buf[16 + 5 + 8 * n] << 16) |
---|
155 | | ((uint32_t)buf[16 + 6 + 8 * n] << 8) |
---|
156 | | (uint32_t)buf[16 + 7 + 8 * n]; |
---|
157 | } |
---|
158 | |
---|
159 | return cv; |
---|
160 | } |
---|
161 | |
---|
162 | static cucul_canvas_t *import_text(void const *data, unsigned int size) |
---|
163 | { |
---|
164 | cucul_canvas_t *cv; |
---|
165 | char const *text = (char const *)data; |
---|
166 | unsigned int width = 1, height = 1, x = 0, y = 0, i; |
---|
167 | |
---|
168 | cv = cucul_create_canvas(width, height); |
---|
169 | cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT); |
---|
170 | |
---|
171 | for(i = 0; i < size; i++) |
---|
172 | { |
---|
173 | unsigned char ch = *text++; |
---|
174 | |
---|
175 | if(ch == '\r') |
---|
176 | continue; |
---|
177 | |
---|
178 | if(ch == '\n') |
---|
179 | { |
---|
180 | x = 0; |
---|
181 | y++; |
---|
182 | continue; |
---|
183 | } |
---|
184 | |
---|
185 | if(x >= width || y >= height) |
---|
186 | { |
---|
187 | if(x >= width) |
---|
188 | width = x + 1; |
---|
189 | |
---|
190 | if(y >= height) |
---|
191 | height = y + 1; |
---|
192 | |
---|
193 | cucul_set_canvas_size(cv, width, height); |
---|
194 | } |
---|
195 | |
---|
196 | cucul_putchar(cv, x, y, ch); |
---|
197 | x++; |
---|
198 | } |
---|
199 | |
---|
200 | return cv; |
---|
201 | } |
---|
202 | |
---|
203 | #define IS_ALPHA(x) (x>='A' && x<='z') |
---|
204 | #define END_TUP 0x1337 |
---|
205 | static int parse_tuple(unsigned int *, unsigned char const *, int); |
---|
206 | static void manage_modifiers(int, uint8_t *, uint8_t *, uint8_t *, uint8_t *, uint8_t *, uint8_t *); |
---|
207 | |
---|
208 | static cucul_canvas_t *import_ansi(void const *data, unsigned int size) |
---|
209 | { |
---|
210 | cucul_canvas_t *cv; |
---|
211 | unsigned char const *buffer = (unsigned char const*)data; |
---|
212 | unsigned int i; |
---|
213 | unsigned int count = 0; |
---|
214 | unsigned int tuple[1024]; /* Should be enough. Will it be ? */ |
---|
215 | int x = 0, y = 0; |
---|
216 | unsigned int width = 80, height = 25; |
---|
217 | int save_x = 0, save_y = 0; |
---|
218 | unsigned int j, skip; |
---|
219 | uint8_t fg, bg, save_fg, save_bg, bold, reverse; |
---|
220 | |
---|
221 | fg = save_fg = CUCUL_COLOR_LIGHTGRAY; |
---|
222 | bg = save_bg = CUCUL_COLOR_BLACK; |
---|
223 | bold = reverse = 0; |
---|
224 | |
---|
225 | cv = cucul_create_canvas(width, height); |
---|
226 | cucul_set_color(cv, fg, bg); |
---|
227 | |
---|
228 | for(i = 0; i < size; i += skip) |
---|
229 | { |
---|
230 | skip = 1; |
---|
231 | |
---|
232 | if(buffer[i] == '\x1a' && size - i >= 8 |
---|
233 | && !memcmp(buffer + i + 1, "SAUCE00", 7)) |
---|
234 | break; /* End before SAUCE data */ |
---|
235 | |
---|
236 | if(buffer[i] == '\r') |
---|
237 | continue; /* DOS sucks */ |
---|
238 | |
---|
239 | if(buffer[i] == '\n') |
---|
240 | { |
---|
241 | x = 0; |
---|
242 | y++; |
---|
243 | continue; |
---|
244 | } |
---|
245 | |
---|
246 | if(buffer[i] == '\x1b' && buffer[i + 1] == '[') /* ESC code */ |
---|
247 | { |
---|
248 | unsigned char c = '\0'; |
---|
249 | |
---|
250 | i++; // ESC |
---|
251 | i++; // [ |
---|
252 | |
---|
253 | for(j = i; j < size; j++) |
---|
254 | if(IS_ALPHA(buffer[j])) |
---|
255 | { |
---|
256 | c = buffer[j]; |
---|
257 | break; |
---|
258 | } |
---|
259 | |
---|
260 | skip += parse_tuple(tuple, buffer + i, size - i); |
---|
261 | count = 0; |
---|
262 | |
---|
263 | while(tuple[count] != END_TUP) |
---|
264 | count++; /* Gruik */ |
---|
265 | |
---|
266 | switch(c) |
---|
267 | { |
---|
268 | case 'f': |
---|
269 | case 'H': |
---|
270 | if(tuple[0] == END_TUP) |
---|
271 | x = y = 0; |
---|
272 | else |
---|
273 | { |
---|
274 | y = tuple[0] - 1; |
---|
275 | x = tuple[1] == END_TUP ? 0 : tuple[1] - 1; |
---|
276 | } |
---|
277 | break; |
---|
278 | case 'A': |
---|
279 | y -= tuple[0] == END_TUP ? 1 : tuple[0]; |
---|
280 | if(y < 0) |
---|
281 | y = 0; |
---|
282 | break; |
---|
283 | case 'B': |
---|
284 | y += tuple[0] == END_TUP ? 1 : tuple[0]; |
---|
285 | break; |
---|
286 | case 'C': |
---|
287 | x += tuple[0] == END_TUP ? 1 : tuple[0]; |
---|
288 | break; |
---|
289 | case 'D': |
---|
290 | x -= tuple[0] == END_TUP ? 1 : tuple[0]; |
---|
291 | if(x < 0) |
---|
292 | x = 0; |
---|
293 | break; |
---|
294 | case 's': |
---|
295 | save_x = x; |
---|
296 | save_y = y; |
---|
297 | break; |
---|
298 | case 'u': |
---|
299 | x = save_x; |
---|
300 | y = save_y; |
---|
301 | break; |
---|
302 | case 'J': |
---|
303 | if(tuple[0] == 2) |
---|
304 | x = y = 0; |
---|
305 | break; |
---|
306 | case 'K': |
---|
307 | // CLEAR END OF LINE |
---|
308 | for(j = x; j < width; j++) |
---|
309 | _cucul_putchar32(cv, j, y, (uint32_t)' '); |
---|
310 | x = width; |
---|
311 | break; |
---|
312 | case 'm': |
---|
313 | for(j = 0; j < count; j++) |
---|
314 | manage_modifiers(tuple[j], &fg, &bg, &save_fg, &save_bg, &bold, &reverse); |
---|
315 | if(bold && fg < 8) |
---|
316 | fg += 8; |
---|
317 | if(bold && bg < 8) |
---|
318 | bg += 8; |
---|
319 | if(reverse) |
---|
320 | cucul_set_color(cv, bg, fg); |
---|
321 | else |
---|
322 | cucul_set_color(cv, fg, bg); |
---|
323 | break; |
---|
324 | default: |
---|
325 | break; |
---|
326 | } |
---|
327 | |
---|
328 | continue; |
---|
329 | } |
---|
330 | |
---|
331 | /* We're going to paste a character. First make sure the canvas |
---|
332 | * is big enough. */ |
---|
333 | if(x >= width) |
---|
334 | { |
---|
335 | x = 0; |
---|
336 | y++; |
---|
337 | } |
---|
338 | |
---|
339 | if(y >= height) |
---|
340 | { |
---|
341 | height = y + 1; |
---|
342 | cucul_set_canvas_size(cv, width, height); |
---|
343 | } |
---|
344 | |
---|
345 | /* Now paste our character */ |
---|
346 | _cucul_putchar32(cv, x, y,_cucul_cp437_to_utf32(buffer[i])); |
---|
347 | x++; |
---|
348 | } |
---|
349 | |
---|
350 | return cv; |
---|
351 | } |
---|
352 | |
---|
353 | /* XXX : ANSI loader helpers */ |
---|
354 | |
---|
355 | static int parse_tuple(unsigned int *ret, unsigned char const *buffer, int size) |
---|
356 | { |
---|
357 | int i = 0; |
---|
358 | int j = 0; |
---|
359 | int t = 0; |
---|
360 | unsigned char nbr[1024]; |
---|
361 | |
---|
362 | ret[0] = END_TUP; |
---|
363 | |
---|
364 | for(i = 0; i < size; i++) |
---|
365 | { |
---|
366 | if(IS_ALPHA(buffer[i])) |
---|
367 | { |
---|
368 | if(j != 0) |
---|
369 | { |
---|
370 | ret[t] = atoi((char*)nbr); |
---|
371 | t++; |
---|
372 | } |
---|
373 | ret[t] = END_TUP; |
---|
374 | j = 0; |
---|
375 | return i; |
---|
376 | } |
---|
377 | |
---|
378 | if(buffer[i] != ';') |
---|
379 | { |
---|
380 | nbr[j] = buffer[i]; |
---|
381 | nbr[j + 1] = 0; |
---|
382 | j++; |
---|
383 | } |
---|
384 | else |
---|
385 | { |
---|
386 | ret[t] = atoi((char*)nbr); |
---|
387 | t++; |
---|
388 | ret[t] = END_TUP; |
---|
389 | j = 0; |
---|
390 | } |
---|
391 | } |
---|
392 | return size; |
---|
393 | } |
---|
394 | |
---|
395 | static void manage_modifiers(int i, uint8_t *fg, uint8_t *bg, uint8_t *save_fg, uint8_t *save_bg, uint8_t *bold, uint8_t *reverse) |
---|
396 | { |
---|
397 | static uint8_t const ansi2cucul[] = |
---|
398 | { |
---|
399 | CUCUL_COLOR_BLACK, |
---|
400 | CUCUL_COLOR_RED, |
---|
401 | CUCUL_COLOR_GREEN, |
---|
402 | CUCUL_COLOR_BROWN, |
---|
403 | CUCUL_COLOR_BLUE, |
---|
404 | CUCUL_COLOR_MAGENTA, |
---|
405 | CUCUL_COLOR_CYAN, |
---|
406 | CUCUL_COLOR_LIGHTGRAY |
---|
407 | }; |
---|
408 | |
---|
409 | if(i >= 30 && i <= 37) |
---|
410 | *fg = ansi2cucul[i - 30]; |
---|
411 | else if(i >= 40 && i <= 47) |
---|
412 | *bg = ansi2cucul[i - 40]; |
---|
413 | else if(i >= 90 && i <= 97) |
---|
414 | *fg = ansi2cucul[i - 90] + 8; |
---|
415 | else if(i >= 100 && i <= 107) |
---|
416 | *bg = ansi2cucul[i - 100] + 8; |
---|
417 | else switch(i) |
---|
418 | { |
---|
419 | case 0: |
---|
420 | *fg = CUCUL_COLOR_DEFAULT; |
---|
421 | *bg = CUCUL_COLOR_DEFAULT; |
---|
422 | *bold = 0; |
---|
423 | *reverse = 0; |
---|
424 | break; |
---|
425 | case 1: /* BOLD */ |
---|
426 | *bold = 1; |
---|
427 | break; |
---|
428 | case 4: // Underline |
---|
429 | break; |
---|
430 | case 5: // blink |
---|
431 | break; |
---|
432 | case 7: // reverse |
---|
433 | *reverse = 1; |
---|
434 | break; |
---|
435 | case 8: // invisible |
---|
436 | *save_fg = *fg; |
---|
437 | *save_bg = *bg; |
---|
438 | *fg = CUCUL_COLOR_TRANSPARENT; |
---|
439 | *bg = CUCUL_COLOR_TRANSPARENT; |
---|
440 | break; |
---|
441 | case 28: // not invisible |
---|
442 | *fg = *save_fg; |
---|
443 | *bg = *save_bg; |
---|
444 | break; |
---|
445 | case 39: |
---|
446 | *fg = CUCUL_COLOR_DEFAULT; |
---|
447 | break; |
---|
448 | case 49: |
---|
449 | *bg = CUCUL_COLOR_DEFAULT; |
---|
450 | break; |
---|
451 | default: |
---|
452 | break; |
---|
453 | } |
---|
454 | } |
---|
455 | |
---|