選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

import.c 16 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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_ansi(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT, 0);
  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_ansi(cv, CUCUL_COLOR_DEFAULT,
  330. CUCUL_COLOR_TRANSPARENT, 0);
  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_ansi(cv, CUCUL_COLOR_DEFAULT,
  376. CUCUL_COLOR_TRANSPARENT, 0);
  377. cucul_set_canvas_size(cv, width = x + wch, height);
  378. }
  379. if((unsigned int)y >= height)
  380. {
  381. cucul_set_attr_ansi(cv, CUCUL_COLOR_DEFAULT,
  382. CUCUL_COLOR_TRANSPARENT, 0);
  383. cucul_set_canvas_size(cv, width, height = y + 1);
  384. }
  385. /* Now paste our character */
  386. cucul_set_attr_ansi(cv, grcm.efg, grcm.ebg, 0);
  387. cucul_putchar(cv, x, y, ch);
  388. x += wch;
  389. }
  390. if((unsigned int)y > height)
  391. {
  392. cucul_set_attr_ansi(cv, CUCUL_COLOR_DEFAULT,
  393. CUCUL_COLOR_TRANSPARENT, 0);
  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_COLOR_BLACK, CUCUL_COLOR_RED,
  405. CUCUL_COLOR_GREEN, CUCUL_COLOR_BROWN,
  406. CUCUL_COLOR_BLUE, CUCUL_COLOR_MAGENTA,
  407. CUCUL_COLOR_CYAN, CUCUL_COLOR_LIGHTGRAY
  408. };
  409. unsigned int j;
  410. for(j = 0; j < argc; j++)
  411. {
  412. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  413. if(argv[j] >= 30 && argv[j] <= 37)
  414. g->fg = ansi2cucul[argv[j] - 30];
  415. else if(argv[j] >= 40 && argv[j] <= 47)
  416. g->bg = ansi2cucul[argv[j] - 40];
  417. else if(argv[j] >= 90 && argv[j] <= 97)
  418. g->fg = ansi2cucul[argv[j] - 90] + 8;
  419. else if(argv[j] >= 100 && argv[j] <= 107)
  420. g->bg = ansi2cucul[argv[j] - 100] + 8;
  421. else switch(argv[j])
  422. {
  423. case 0: /* default rendition */
  424. g->fg = CUCUL_COLOR_DEFAULT;
  425. g->bg = CUCUL_COLOR_TRANSPARENT;
  426. g->bold = g->negative = g->concealed = 0;
  427. break;
  428. case 1: /* bold or increased intensity */
  429. g->bold = 1;
  430. break;
  431. case 4: /* singly underlined */
  432. break;
  433. case 5: /* slowly blinking (less then 150 per minute) */
  434. break;
  435. case 7: /* negative image */
  436. g->negative = 1;
  437. break;
  438. case 8: /* concealed characters */
  439. g->concealed = 1;
  440. break;
  441. case 22: /* normal colour or normal intensity (neither bold nor faint) */
  442. g->bold = 0;
  443. break;
  444. case 28: /* revealed characters */
  445. g->concealed = 0;
  446. break;
  447. case 39: /* default display colour (implementation-defined) */
  448. g->fg = CUCUL_COLOR_DEFAULT;
  449. break;
  450. case 49: /* default background colour (implementation-defined) */
  451. g->bg = CUCUL_COLOR_TRANSPARENT;
  452. break;
  453. default:
  454. fprintf(stderr, "unknown sgr %i\n", argv[j]);
  455. break;
  456. }
  457. }
  458. if(g->concealed)
  459. {
  460. g->efg = g->ebg = CUCUL_COLOR_TRANSPARENT;
  461. }
  462. else
  463. {
  464. g->efg = g->negative ? g->bg : g->fg;
  465. g->ebg = g->negative ? g->fg : g->bg;
  466. if(g->bold)
  467. {
  468. if(g->efg < 8)
  469. g->efg += 8;
  470. else if(g->efg == CUCUL_COLOR_DEFAULT)
  471. g->efg = CUCUL_COLOR_WHITE;
  472. }
  473. }
  474. }