25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

547 lines
16 KiB

  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$
  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. * This file contains various import functions.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined(__KERNEL__)
  19. # if defined(HAVE_ERRNO_H)
  20. # include <errno.h>
  21. # endif
  22. # include <stdio.h>
  23. # include <stdlib.h>
  24. # include <string.h>
  25. #endif
  26. #include "cucul.h"
  27. #include "cucul_internals.h"
  28. /* ANSI Graphic Rendition Combination Mode */
  29. struct ansi_grcm
  30. {
  31. uint8_t fg, bg; /* ANSI-context fg/bg */
  32. uint8_t efg, ebg; /* Effective (libcucul) fg/bg */
  33. uint8_t bold, negative, concealed;
  34. };
  35. static cucul_canvas_t *import_caca(void const *, unsigned int);
  36. static cucul_canvas_t *import_text(void const *, unsigned int);
  37. static cucul_canvas_t *import_ansi(void const *, unsigned int, int);
  38. static void ansi_parse_grcm(cucul_canvas_t *, struct ansi_grcm *,
  39. unsigned int, unsigned int const *);
  40. /** \brief Import a buffer into a canvas
  41. *
  42. * Import a libcucul buffer as returned by cucul_load_memory()
  43. * or cucul_load_file() into an internal libcucul canvas.
  44. *
  45. * Valid values for \c format are:
  46. * - \c "": attempt to autodetect the file format.
  47. * - \c "text": import ASCII text files.
  48. * - \c "ansi": import ANSI files.
  49. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  50. * - \c "caca": import native libcaca files.
  51. *
  52. * If an error occurs, NULL is returned and \b errno is set accordingly:
  53. * - \c ENOMEM Not enough memory to allocate canvas.
  54. * - \c EINVAL Invalid format requested.
  55. *
  56. * \param buffer A \e libcucul buffer containing the data to be loaded
  57. * into a canvas.
  58. * \param format A string describing the input format.
  59. * \return A libcucul canvas, or NULL in case of error.
  60. */
  61. cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buffer, char const *format)
  62. {
  63. char const *buf = (char const*)buffer->data;
  64. if(!strcasecmp("caca", format))
  65. return import_caca(buffer->data, buffer->size);
  66. if(!strcasecmp("utf8", format))
  67. return import_ansi(buffer->data, buffer->size, 1);
  68. if(!strcasecmp("text", format))
  69. return import_text(buffer->data, buffer->size);
  70. if(!strcasecmp("ansi", format))
  71. return import_ansi(buffer->data, buffer->size, 0);
  72. /* Autodetection */
  73. if(!strcasecmp("", format))
  74. {
  75. unsigned int i;
  76. /* If 4 first letters are CACA */
  77. if(buffer->size >= 4 &&
  78. buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A')
  79. return import_caca(buffer->data, buffer->size);
  80. /* If we find ESC[ argv, we guess it's an ANSI file */
  81. for(i = 0; i + 1 < buffer->size; i++)
  82. if((buf[i] == 0x1b) && (buf[i + 1] == '['))
  83. return import_ansi(buffer->data, buffer->size, 0);
  84. /* Otherwise, import it as text */
  85. return import_text(buffer->data, buffer->size);
  86. }
  87. #if defined(HAVE_ERRNO_H)
  88. errno = EINVAL;
  89. #endif
  90. return NULL;
  91. }
  92. /** \brief Get available import formats
  93. *
  94. * Return a list of available import formats. The list is a NULL-terminated
  95. * array of strings, interleaving a string containing the internal value for
  96. * the import format, to be used with cucul_import_canvas(), and a string
  97. * containing the natural language description for that import format.
  98. *
  99. * This function never fails.
  100. *
  101. * \return An array of strings.
  102. */
  103. char const * const * cucul_get_import_list(void)
  104. {
  105. static char const * const list[] =
  106. {
  107. "", "autodetect",
  108. "text", "plain text",
  109. "caca", "native libcaca format",
  110. "ansi", "ANSI coloured text",
  111. NULL, NULL
  112. };
  113. return list;
  114. }
  115. /*
  116. * XXX: the following functions are local.
  117. */
  118. static cucul_canvas_t *import_caca(void const *data, unsigned int size)
  119. {
  120. cucul_canvas_t *cv;
  121. uint8_t const *buf = (uint8_t const *)data;
  122. unsigned int width, height, n;
  123. if(size < 16)
  124. goto invalid_caca;
  125. if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
  126. goto invalid_caca;
  127. if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
  128. goto invalid_caca;
  129. width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
  130. | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
  131. height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
  132. | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
  133. if(size != 16 + width * height * 8)
  134. goto invalid_caca;
  135. cv = cucul_create_canvas(width, height);
  136. if(!cv)
  137. {
  138. #if defined(HAVE_ERRNO_H)
  139. errno = ENOMEM;
  140. #endif
  141. return NULL;
  142. }
  143. for(n = height * width; n--; )
  144. {
  145. cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
  146. | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
  147. | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
  148. | (uint32_t)buf[16 + 3 + 8 * n];
  149. cv->attrs[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
  150. | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
  151. | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
  152. | (uint32_t)buf[16 + 7 + 8 * n];
  153. }
  154. return cv;
  155. invalid_caca:
  156. #if defined(HAVE_ERRNO_H)
  157. errno = EINVAL;
  158. #endif
  159. return NULL;
  160. }
  161. static cucul_canvas_t *import_text(void const *data, unsigned int size)
  162. {
  163. cucul_canvas_t *cv;
  164. char const *text = (char const *)data;
  165. unsigned int width = 0, height = 0, x = 0, y = 0, i;
  166. cv = cucul_create_canvas(width, height);
  167. if(!cv)
  168. {
  169. #if defined(HAVE_ERRNO_H)
  170. errno = ENOMEM;
  171. #endif
  172. return NULL;
  173. }
  174. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_DEFAULT, CUCUL_TRANSPARENT));
  175. for(i = 0; i < size; i++)
  176. {
  177. unsigned char ch = *text++;
  178. if(ch == '\r')
  179. continue;
  180. if(ch == '\n')
  181. {
  182. x = 0;
  183. y++;
  184. continue;
  185. }
  186. if(x >= width || y >= height)
  187. {
  188. if(x >= width)
  189. width = x + 1;
  190. if(y >= height)
  191. height = y + 1;
  192. cucul_set_canvas_size(cv, width, height);
  193. }
  194. cucul_putchar(cv, x, y, ch);
  195. x++;
  196. }
  197. if(y > height)
  198. cucul_set_canvas_size(cv, width, height = y);
  199. return cv;
  200. }
  201. static cucul_canvas_t *import_ansi(void const *data, unsigned int size,
  202. int utf8)
  203. {
  204. struct ansi_grcm grcm;
  205. unsigned char const *buffer = (unsigned char const*)data;
  206. cucul_canvas_t *cv;
  207. unsigned int i, j, skip, dummy = 0;
  208. unsigned int width = 0, height = 0, wch = 1;
  209. unsigned long int ch;
  210. int x = 0, y = 0, save_x = 0, save_y = 0;
  211. cv = cucul_create_canvas(width, height);
  212. if(!cv)
  213. {
  214. #if defined(HAVE_ERRNO_H)
  215. errno = ENOMEM;
  216. #endif
  217. return NULL;
  218. }
  219. ansi_parse_grcm(cv, &grcm, 1, &dummy);
  220. for(i = 0; i < size; i += skip)
  221. {
  222. skip = 1;
  223. /* Wrap long lines */
  224. if((unsigned int)x >= 80)
  225. {
  226. x = 0;
  227. y++;
  228. }
  229. if(buffer[i] == '\x1a' && size - i >= 8
  230. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  231. break; /* End before SAUCE data */
  232. if(buffer[i] == '\r')
  233. continue; /* DOS sucks */
  234. if(buffer[i] == '\n')
  235. {
  236. x = 0;
  237. y++;
  238. continue;
  239. }
  240. /* Interpret escape commands, as per Standard ECMA-48 "Control
  241. * Functions for Coded Character Sets", 5.4. Control sequences. */
  242. if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  243. {
  244. unsigned int argc = 0, argv[101];
  245. unsigned int param, inter, final;
  246. /* Compute offsets to parameter bytes, intermediate bytes and
  247. * to the final byte. Only the final byte is mandatory, there
  248. * can be zero of the others.
  249. * 0 param=2 inter final final+1
  250. * +-----+------------------+---------------------+-----------------+
  251. * | CSI | parameter bytes | intermediate bytes | final byte |
  252. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  253. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  254. * +-----+------------------+---------------------+-----------------+
  255. */
  256. param = 2;
  257. for(inter = param; i + inter < size; inter++)
  258. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  259. break;
  260. for(final = inter; i + final < size; final++)
  261. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  262. break;
  263. if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  264. break; /* Invalid Final Byte */
  265. skip += final;
  266. /* Sanity checks */
  267. if(param < inter && buffer[i + param] >= 0x3c)
  268. {
  269. fprintf(stderr, "private sequence \"^[[%.*s\"\n",
  270. final - param + 1, buffer + i + param);
  271. continue; /* Private sequence, skip it entirely */
  272. }
  273. if(final - param > 100)
  274. continue; /* Suspiciously long sequence, skip it */
  275. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  276. * format */
  277. if(param < inter)
  278. {
  279. argv[0] = 0;
  280. for(j = param; j < inter; j++)
  281. {
  282. if(buffer[i + j] == ';')
  283. argv[++argc] = 0;
  284. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  285. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  286. }
  287. argc++;
  288. }
  289. /* Interpret final byte. The code representations are given in
  290. * ECMA-48 5.4: Control sequences, and the code definitions are
  291. * given in ECMA-48 8.3: Definition of control functions. */
  292. switch(buffer[i + final])
  293. {
  294. case 'f': /* CUP - Cursor Position */
  295. case 'H': /* HVP - Character And Line Position */
  296. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  297. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  298. break;
  299. case 'A': /* CUU - Cursor Up */
  300. y -= argc ? argv[0] : 1;
  301. if(y < 0)
  302. y = 0;
  303. break;
  304. case 'B': /* CUD - Cursor Down */
  305. y += argc ? argv[0] : 1;
  306. break;
  307. case 'C': /* CUF - Cursor Right */
  308. x += argc ? argv[0] : 1;
  309. break;
  310. case 'D': /* CUB - Cursor Left */
  311. x -= argc ? argv[0] : 1;
  312. if(x < 0)
  313. x = 0;
  314. break;
  315. case 's': /* Private (save cursor position) */
  316. save_x = x;
  317. save_y = y;
  318. break;
  319. case 'u': /* Private (reload cursor position) */
  320. x = save_x;
  321. y = save_y;
  322. break;
  323. case 'J': /* ED - Erase In Page */
  324. if(argv[0] == 2)
  325. x = y = 0;
  326. break;
  327. case 'K': /* EL - Erase In Line */
  328. if(width < 80)
  329. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_DEFAULT,
  330. CUCUL_TRANSPARENT));
  331. cucul_set_canvas_size(cv, width = 80, height);
  332. for(j = x; j < 80; j++)
  333. cucul_putchar(cv, j, y, ' ');
  334. x = 80;
  335. break;
  336. case 'm': /* SGR - Select Graphic Rendition */
  337. ansi_parse_grcm(cv, &grcm, argc, argv);
  338. break;
  339. default:
  340. fprintf(stderr, "unknown command %c\n", buffer[i + final]);
  341. break;
  342. }
  343. continue;
  344. }
  345. /* Get the character we’re going to paste */
  346. if(utf8)
  347. {
  348. unsigned int bytes;
  349. if(i + 6 < size)
  350. ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes);
  351. else
  352. {
  353. /* Add a trailing zero to what we're going to read */
  354. char tmp[7];
  355. memcpy(tmp, buffer + i, size - i);
  356. tmp[size - i] = '\0';
  357. ch = cucul_utf8_to_utf32(tmp, &bytes);
  358. }
  359. if(!bytes)
  360. {
  361. /* If the Unicode is invalid, assume it was latin1. */
  362. ch = buffer[i];
  363. bytes = 1;
  364. }
  365. wch = cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  366. skip += bytes - 1;
  367. }
  368. else
  369. {
  370. ch = cucul_cp437_to_utf32(buffer[i]);
  371. }
  372. /* Make sure the canvas is big enough. */
  373. if((unsigned int)x + wch > width)
  374. {
  375. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_DEFAULT,
  376. CUCUL_TRANSPARENT));
  377. cucul_set_canvas_size(cv, width = x + wch, height);
  378. }
  379. if((unsigned int)y >= height)
  380. {
  381. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_DEFAULT,
  382. CUCUL_TRANSPARENT));
  383. cucul_set_canvas_size(cv, width, height = y + 1);
  384. }
  385. /* Now paste our character */
  386. cucul_set_attr(cv, cucul_ansi_to_attr(grcm.efg, grcm.ebg));
  387. cucul_putchar(cv, x, y, ch);
  388. x += wch;
  389. }
  390. if((unsigned int)y > height)
  391. {
  392. cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_DEFAULT,
  393. CUCUL_TRANSPARENT));
  394. cucul_set_canvas_size(cv, width, height = y);
  395. }
  396. return cv;
  397. }
  398. /* XXX : ANSI loader helper */
  399. static void ansi_parse_grcm(cucul_canvas_t *cv, struct ansi_grcm *g,
  400. unsigned int argc, unsigned int const *argv)
  401. {
  402. static uint8_t const ansi2cucul[] =
  403. {
  404. CUCUL_BLACK, CUCUL_RED, CUCUL_GREEN, CUCUL_BROWN,
  405. CUCUL_BLUE, CUCUL_MAGENTA, CUCUL_CYAN, CUCUL_LIGHTGRAY
  406. };
  407. unsigned int j;
  408. for(j = 0; j < argc; j++)
  409. {
  410. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  411. if(argv[j] >= 30 && argv[j] <= 37)
  412. g->fg = ansi2cucul[argv[j] - 30];
  413. else if(argv[j] >= 40 && argv[j] <= 47)
  414. g->bg = ansi2cucul[argv[j] - 40];
  415. else if(argv[j] >= 90 && argv[j] <= 97)
  416. g->fg = ansi2cucul[argv[j] - 90] + 8;
  417. else if(argv[j] >= 100 && argv[j] <= 107)
  418. g->bg = ansi2cucul[argv[j] - 100] + 8;
  419. else switch(argv[j])
  420. {
  421. case 0: /* default rendition */
  422. g->fg = CUCUL_DEFAULT;
  423. g->bg = CUCUL_TRANSPARENT;
  424. g->bold = g->negative = g->concealed = 0;
  425. break;
  426. case 1: /* bold or increased intensity */
  427. g->bold = 1;
  428. break;
  429. case 4: /* singly underlined */
  430. break;
  431. case 5: /* slowly blinking (less then 150 per minute) */
  432. break;
  433. case 7: /* negative image */
  434. g->negative = 1;
  435. break;
  436. case 8: /* concealed characters */
  437. g->concealed = 1;
  438. break;
  439. case 22: /* normal colour or normal intensity (neither bold nor faint) */
  440. g->bold = 0;
  441. break;
  442. case 28: /* revealed characters */
  443. g->concealed = 0;
  444. break;
  445. case 39: /* default display colour (implementation-defined) */
  446. g->fg = CUCUL_DEFAULT;
  447. break;
  448. case 49: /* default background colour (implementation-defined) */
  449. g->bg = CUCUL_TRANSPARENT;
  450. break;
  451. default:
  452. fprintf(stderr, "unknown sgr %i\n", argv[j]);
  453. break;
  454. }
  455. }
  456. if(g->concealed)
  457. {
  458. g->efg = g->ebg = CUCUL_TRANSPARENT;
  459. }
  460. else
  461. {
  462. g->efg = g->negative ? g->bg : g->fg;
  463. g->ebg = g->negative ? g->fg : g->bg;
  464. if(g->bold)
  465. {
  466. if(g->efg < 8)
  467. g->efg += 8;
  468. else if(g->efg == CUCUL_DEFAULT)
  469. g->efg = CUCUL_WHITE;
  470. }
  471. }
  472. }