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 896 2006-04-26 11:54: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 buffer A \e libcucul buffer containing the data to be loaded |
---|
47 | * into a canvas. |
---|
48 | * \param size The length of the memory area. |
---|
49 | * \param format A string describing the input format. |
---|
50 | * \return A libcucul canvas, or NULL in case of error. |
---|
51 | */ |
---|
52 | cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *b, char const *format) |
---|
53 | { |
---|
54 | char const *buf = (char const*)b->data; |
---|
55 | |
---|
56 | if(b->size == 0 || b->data == NULL) |
---|
57 | return NULL; |
---|
58 | |
---|
59 | if(!strcasecmp("caca", format)) |
---|
60 | return import_caca(b->data, b->size); |
---|
61 | if(!strcasecmp("text", format)) |
---|
62 | return import_text(b->data, b->size); |
---|
63 | if(!strcasecmp("ansi", format)) |
---|
64 | return import_ansi(b->data, b->size); |
---|
65 | |
---|
66 | /* Autodetection */ |
---|
67 | if(!strcasecmp("", format)) |
---|
68 | { |
---|
69 | unsigned int i=0; |
---|
70 | /* if 4 first letters are CACA */ |
---|
71 | if(b->size >= 4 && |
---|
72 | buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A') |
---|
73 | return import_caca(b->data, b->size); |
---|
74 | |
---|
75 | /* If we find ESC[ argv, we guess it's an ANSI file */ |
---|
76 | while(i < b->size - 1) |
---|
77 | { |
---|
78 | if((buf[i] == 0x1b) && (buf[i+1] == '[')) |
---|
79 | return import_ansi(b->data, b->size); |
---|
80 | i++; |
---|
81 | } |
---|
82 | |
---|
83 | /* Otherwise, import it as text */ |
---|
84 | return import_text(b->data, b->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 | static void manage_modifiers(int, uint8_t *, uint8_t *, uint8_t *, |
---|
204 | uint8_t *, uint8_t *, uint8_t *); |
---|
205 | |
---|
206 | static cucul_canvas_t *import_ansi(void const *data, unsigned int size) |
---|
207 | { |
---|
208 | cucul_canvas_t *cv; |
---|
209 | unsigned char const *buffer = (unsigned char const*)data; |
---|
210 | unsigned int i, j; |
---|
211 | int x = 0, y = 0; |
---|
212 | unsigned int width = 80, height = 25; |
---|
213 | int save_x = 0, save_y = 0; |
---|
214 | unsigned int skip; |
---|
215 | uint8_t fg, bg, save_fg, save_bg, bold, reverse; |
---|
216 | |
---|
217 | fg = save_fg = CUCUL_COLOR_LIGHTGRAY; |
---|
218 | bg = save_bg = CUCUL_COLOR_BLACK; |
---|
219 | bold = reverse = 0; |
---|
220 | |
---|
221 | cv = cucul_create_canvas(width, height); |
---|
222 | cucul_set_color(cv, fg, bg); |
---|
223 | |
---|
224 | for(i = 0; i < size; i += skip) |
---|
225 | { |
---|
226 | skip = 1; |
---|
227 | |
---|
228 | if(buffer[i] == '\x1a' && size - i >= 8 |
---|
229 | && !memcmp(buffer + i + 1, "SAUCE00", 7)) |
---|
230 | break; /* End before SAUCE data */ |
---|
231 | |
---|
232 | if(buffer[i] == '\r') |
---|
233 | continue; /* DOS sucks */ |
---|
234 | |
---|
235 | if(buffer[i] == '\n') |
---|
236 | { |
---|
237 | x = 0; |
---|
238 | y++; |
---|
239 | continue; |
---|
240 | } |
---|
241 | |
---|
242 | /* Interpret escape commands, as per Standard ECMA-48 "Control |
---|
243 | * Functions for Coded Character Sets", 5.4. Control sequences. */ |
---|
244 | if(buffer[i] == '\x1b' && buffer[i + 1] == '[') |
---|
245 | { |
---|
246 | unsigned int argc = 0, argv[101]; |
---|
247 | unsigned int param, inter, final; |
---|
248 | |
---|
249 | /* Compute offsets to parameter bytes, intermediate bytes and |
---|
250 | * to the final byte. Only the final byte is mandatory, there |
---|
251 | * can be zero of the others. |
---|
252 | * |
---|
253 | * +-----+------------------+---------------------+-----------------+ |
---|
254 | * | CSI | parameter bytes | intermediate bytes | final byte | |
---|
255 | * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e | |
---|
256 | * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ | |
---|
257 | * +-----+------------------+---------------------+-----------------+ |
---|
258 | */ |
---|
259 | param = 2; |
---|
260 | |
---|
261 | for(inter = param; i + inter < size; inter++) |
---|
262 | if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f) |
---|
263 | break; |
---|
264 | |
---|
265 | for(final = inter; i + final < size; final++) |
---|
266 | if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f) |
---|
267 | break; |
---|
268 | |
---|
269 | if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e) |
---|
270 | break; /* Invalid Final Byte */ |
---|
271 | |
---|
272 | skip += final; |
---|
273 | |
---|
274 | /* Sanity checks */ |
---|
275 | if(param < inter && buffer[i + param] >= 0x3c) |
---|
276 | { |
---|
277 | //fprintf(stderr, "private sequence \"^[[%.*s\"\n", |
---|
278 | // final - param + 1, buffer + i + param); |
---|
279 | continue; /* Private sequence, skip it entirely */ |
---|
280 | } |
---|
281 | |
---|
282 | if(final - param > 100) |
---|
283 | continue; /* Suspiciously long sequence, skip it */ |
---|
284 | |
---|
285 | /* ECMA-48 5.4.2: Parameter string format */ |
---|
286 | if(param < inter) |
---|
287 | { |
---|
288 | argv[0] = 0; |
---|
289 | for(j = param; j < inter; j++) |
---|
290 | { |
---|
291 | if(buffer[i + j] == ';') |
---|
292 | argv[++argc] = 0; |
---|
293 | else if(buffer[i + j] >= '0' && buffer[i + j] <= '9') |
---|
294 | argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0'); |
---|
295 | } |
---|
296 | argc++; |
---|
297 | } |
---|
298 | |
---|
299 | /* Interpret final byte. The code representations are given in |
---|
300 | * ECMA-48 5.4: Control sequences, and the code definitions are |
---|
301 | * given in ECMA-48 8.3: Definition of control functions. */ |
---|
302 | switch(buffer[i + final]) |
---|
303 | { |
---|
304 | case 'f': /* CUP - Cursor Position */ |
---|
305 | case 'H': /* HVP - Character And Line Position */ |
---|
306 | x = (argc > 1) ? argv[1] - 1 : 0; |
---|
307 | y = (argc > 0) ? argv[0] - 1 : 0; |
---|
308 | break; |
---|
309 | case 'A': /* CUU - Cursor Up */ |
---|
310 | y -= argc ? argv[0] : 1; |
---|
311 | if(y < 0) |
---|
312 | y = 0; |
---|
313 | break; |
---|
314 | case 'B': /* CUD - Cursor Down */ |
---|
315 | y += argc ? argv[0] : 1; |
---|
316 | break; |
---|
317 | case 'C': /* CUF - Cursor Right */ |
---|
318 | x += argc ? argv[0] : 1; |
---|
319 | break; |
---|
320 | case 'D': /* CUB - Cursor Left */ |
---|
321 | x -= argc ? argv[0] : 1; |
---|
322 | if(x < 0) |
---|
323 | x = 0; |
---|
324 | break; |
---|
325 | case 's': /* Private (save cursor position) */ |
---|
326 | save_x = x; |
---|
327 | save_y = y; |
---|
328 | break; |
---|
329 | case 'u': /* Private (reload cursor positin) */ |
---|
330 | x = save_x; |
---|
331 | y = save_y; |
---|
332 | break; |
---|
333 | case 'J': /* ED - Erase In Page */ |
---|
334 | if(argv[0] == 2) |
---|
335 | x = y = 0; |
---|
336 | break; |
---|
337 | case 'K': /* EL - Erase In Line */ |
---|
338 | for(j = x; j < width; j++) |
---|
339 | _cucul_putchar32(cv, j, y, (uint32_t)' '); |
---|
340 | x = width; |
---|
341 | break; |
---|
342 | case 'm': /* SGR - Select Graphic Rendition */ |
---|
343 | for(j = 0; j < argc; j++) |
---|
344 | manage_modifiers(argv[j], &fg, &bg, |
---|
345 | &save_fg, &save_bg, &bold, &reverse); |
---|
346 | if(bold && fg < 8) |
---|
347 | fg += 8; |
---|
348 | if(bold && bg < 8) |
---|
349 | bg += 8; |
---|
350 | if(reverse) |
---|
351 | cucul_set_color(cv, bg, fg); |
---|
352 | else |
---|
353 | cucul_set_color(cv, fg, bg); |
---|
354 | break; |
---|
355 | default: |
---|
356 | break; |
---|
357 | } |
---|
358 | |
---|
359 | continue; |
---|
360 | } |
---|
361 | |
---|
362 | /* We're going to paste a character. First make sure the canvas |
---|
363 | * is big enough. */ |
---|
364 | if((unsigned int)x >= width) |
---|
365 | { |
---|
366 | x = 0; |
---|
367 | y++; |
---|
368 | } |
---|
369 | |
---|
370 | if((unsigned int)y >= height) |
---|
371 | { |
---|
372 | height = y + 1; |
---|
373 | cucul_set_canvas_size(cv, width, height); |
---|
374 | } |
---|
375 | |
---|
376 | /* Now paste our character */ |
---|
377 | _cucul_putchar32(cv, x, y,_cucul_cp437_to_utf32(buffer[i])); |
---|
378 | x++; |
---|
379 | } |
---|
380 | |
---|
381 | return cv; |
---|
382 | } |
---|
383 | |
---|
384 | /* XXX : ANSI loader helper */ |
---|
385 | |
---|
386 | static void manage_modifiers(int i, uint8_t *fg, uint8_t *bg, uint8_t *save_fg, |
---|
387 | uint8_t *save_bg, uint8_t *bold, uint8_t *reverse) |
---|
388 | { |
---|
389 | static uint8_t const ansi2cucul[] = |
---|
390 | { |
---|
391 | CUCUL_COLOR_BLACK, |
---|
392 | CUCUL_COLOR_RED, |
---|
393 | CUCUL_COLOR_GREEN, |
---|
394 | CUCUL_COLOR_BROWN, |
---|
395 | CUCUL_COLOR_BLUE, |
---|
396 | CUCUL_COLOR_MAGENTA, |
---|
397 | CUCUL_COLOR_CYAN, |
---|
398 | CUCUL_COLOR_LIGHTGRAY |
---|
399 | }; |
---|
400 | |
---|
401 | if(i >= 30 && i <= 37) |
---|
402 | *fg = ansi2cucul[i - 30]; |
---|
403 | else if(i >= 40 && i <= 47) |
---|
404 | *bg = ansi2cucul[i - 40]; |
---|
405 | else if(i >= 90 && i <= 97) |
---|
406 | *fg = ansi2cucul[i - 90] + 8; |
---|
407 | else if(i >= 100 && i <= 107) |
---|
408 | *bg = ansi2cucul[i - 100] + 8; |
---|
409 | else switch(i) |
---|
410 | { |
---|
411 | case 0: |
---|
412 | *fg = CUCUL_COLOR_DEFAULT; |
---|
413 | *bg = CUCUL_COLOR_DEFAULT; |
---|
414 | *bold = 0; |
---|
415 | *reverse = 0; |
---|
416 | break; |
---|
417 | case 1: /* BOLD */ |
---|
418 | *bold = 1; |
---|
419 | break; |
---|
420 | case 4: // Underline |
---|
421 | break; |
---|
422 | case 5: // blink |
---|
423 | break; |
---|
424 | case 7: // reverse |
---|
425 | *reverse = 1; |
---|
426 | break; |
---|
427 | case 8: // invisible |
---|
428 | *save_fg = *fg; |
---|
429 | *save_bg = *bg; |
---|
430 | *fg = CUCUL_COLOR_TRANSPARENT; |
---|
431 | *bg = CUCUL_COLOR_TRANSPARENT; |
---|
432 | break; |
---|
433 | case 28: // not invisible |
---|
434 | *fg = *save_fg; |
---|
435 | *bg = *save_bg; |
---|
436 | break; |
---|
437 | case 39: |
---|
438 | *fg = CUCUL_COLOR_DEFAULT; |
---|
439 | break; |
---|
440 | case 49: |
---|
441 | *bg = CUCUL_COLOR_DEFAULT; |
---|
442 | break; |
---|
443 | default: |
---|
444 | break; |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|