Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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