Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

19 роки тому
19 роки тому
19 роки тому
19 роки тому
19 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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->attr[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_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_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. return cv;
  198. }
  199. static cucul_canvas_t *import_ansi(void const *data, unsigned int size,
  200. int utf8)
  201. {
  202. struct ansi_grcm grcm;
  203. unsigned char const *buffer = (unsigned char const*)data;
  204. cucul_canvas_t *cv;
  205. unsigned int i, j, skip, dummy = 0;
  206. unsigned int width = 0, height = 0, wch = 1;
  207. unsigned long int ch;
  208. int x = 0, y = 0, save_x = 0, save_y = 0;
  209. cv = cucul_create_canvas(width, height);
  210. if(!cv)
  211. {
  212. #if defined(HAVE_ERRNO_H)
  213. errno = ENOMEM;
  214. #endif
  215. return NULL;
  216. }
  217. ansi_parse_grcm(cv, &grcm, 1, &dummy);
  218. for(i = 0; i < size; i += skip)
  219. {
  220. skip = 1;
  221. /* Wrap long lines */
  222. if((unsigned int)x >= 80)
  223. {
  224. x = 0;
  225. y++;
  226. }
  227. if(buffer[i] == '\x1a' && size - i >= 8
  228. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  229. break; /* End before SAUCE data */
  230. if(buffer[i] == '\r')
  231. continue; /* DOS sucks */
  232. if(buffer[i] == '\n')
  233. {
  234. x = 0;
  235. y++;
  236. continue;
  237. }
  238. /* Interpret escape commands, as per Standard ECMA-48 "Control
  239. * Functions for Coded Character Sets", 5.4. Control sequences. */
  240. if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  241. {
  242. unsigned int argc = 0, argv[101];
  243. unsigned int param, inter, final;
  244. /* Compute offsets to parameter bytes, intermediate bytes and
  245. * to the final byte. Only the final byte is mandatory, there
  246. * can be zero of the others.
  247. * 0 param=2 inter final final+1
  248. * +-----+------------------+---------------------+-----------------+
  249. * | CSI | parameter bytes | intermediate bytes | final byte |
  250. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  251. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  252. * +-----+------------------+---------------------+-----------------+
  253. */
  254. param = 2;
  255. for(inter = param; i + inter < size; inter++)
  256. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  257. break;
  258. for(final = inter; i + final < size; final++)
  259. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  260. break;
  261. if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  262. break; /* Invalid Final Byte */
  263. skip += final;
  264. /* Sanity checks */
  265. if(param < inter && buffer[i + param] >= 0x3c)
  266. {
  267. fprintf(stderr, "private sequence \"^[[%.*s\"\n",
  268. final - param + 1, buffer + i + param);
  269. continue; /* Private sequence, skip it entirely */
  270. }
  271. if(final - param > 100)
  272. continue; /* Suspiciously long sequence, skip it */
  273. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  274. * format */
  275. if(param < inter)
  276. {
  277. argv[0] = 0;
  278. for(j = param; j < inter; j++)
  279. {
  280. if(buffer[i + j] == ';')
  281. argv[++argc] = 0;
  282. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  283. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  284. }
  285. argc++;
  286. }
  287. /* Interpret final byte. The code representations are given in
  288. * ECMA-48 5.4: Control sequences, and the code definitions are
  289. * given in ECMA-48 8.3: Definition of control functions. */
  290. switch(buffer[i + final])
  291. {
  292. case 'f': /* CUP - Cursor Position */
  293. case 'H': /* HVP - Character And Line Position */
  294. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  295. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  296. break;
  297. case 'A': /* CUU - Cursor Up */
  298. y -= argc ? argv[0] : 1;
  299. if(y < 0)
  300. y = 0;
  301. break;
  302. case 'B': /* CUD - Cursor Down */
  303. y += argc ? argv[0] : 1;
  304. break;
  305. case 'C': /* CUF - Cursor Right */
  306. x += argc ? argv[0] : 1;
  307. break;
  308. case 'D': /* CUB - Cursor Left */
  309. x -= argc ? argv[0] : 1;
  310. if(x < 0)
  311. x = 0;
  312. break;
  313. case 's': /* Private (save cursor position) */
  314. save_x = x;
  315. save_y = y;
  316. break;
  317. case 'u': /* Private (reload cursor position) */
  318. x = save_x;
  319. y = save_y;
  320. break;
  321. case 'J': /* ED - Erase In Page */
  322. if(argv[0] == 2)
  323. x = y = 0;
  324. break;
  325. case 'K': /* EL - Erase In Line */
  326. if(width < 80)
  327. cucul_set_color(cv, CUCUL_COLOR_DEFAULT,
  328. CUCUL_COLOR_TRANSPARENT);
  329. cucul_set_canvas_size(cv, width = 80, height);
  330. for(j = x; j < 80; j++)
  331. cucul_putchar(cv, j, y, ' ');
  332. x = 80;
  333. break;
  334. case 'm': /* SGR - Select Graphic Rendition */
  335. ansi_parse_grcm(cv, &grcm, argc, argv);
  336. break;
  337. default:
  338. fprintf(stderr, "unknown command %c\n", buffer[i + final]);
  339. break;
  340. }
  341. continue;
  342. }
  343. /* Get the character we’re going to paste */
  344. if(utf8)
  345. {
  346. unsigned int bytes;
  347. ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes);
  348. wch = cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  349. skip += bytes - 1;
  350. }
  351. else
  352. {
  353. ch = cucul_cp437_to_utf32(buffer[i]);
  354. }
  355. /* Make sure the canvas is big enough. */
  356. if((unsigned int)x + wch > width)
  357. {
  358. cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT);
  359. cucul_set_canvas_size(cv, width = x + wch, height);
  360. }
  361. if((unsigned int)y >= height)
  362. {
  363. cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT);
  364. cucul_set_canvas_size(cv, width, height = y + 1);
  365. }
  366. /* Now paste our character */
  367. cucul_set_color(cv, grcm.efg, grcm.ebg);
  368. cucul_putchar(cv, x, y, ch);
  369. x += wch;
  370. }
  371. return cv;
  372. }
  373. /* XXX : ANSI loader helper */
  374. static void ansi_parse_grcm(cucul_canvas_t *cv, struct ansi_grcm *g,
  375. unsigned int argc, unsigned int const *argv)
  376. {
  377. static uint8_t const ansi2cucul[] =
  378. {
  379. CUCUL_COLOR_BLACK, CUCUL_COLOR_RED,
  380. CUCUL_COLOR_GREEN, CUCUL_COLOR_BROWN,
  381. CUCUL_COLOR_BLUE, CUCUL_COLOR_MAGENTA,
  382. CUCUL_COLOR_CYAN, CUCUL_COLOR_LIGHTGRAY
  383. };
  384. unsigned int j;
  385. for(j = 0; j < argc; j++)
  386. {
  387. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  388. if(argv[j] >= 30 && argv[j] <= 37)
  389. g->fg = ansi2cucul[argv[j] - 30];
  390. else if(argv[j] >= 40 && argv[j] <= 47)
  391. g->bg = ansi2cucul[argv[j] - 40];
  392. else if(argv[j] >= 90 && argv[j] <= 97)
  393. g->fg = ansi2cucul[argv[j] - 90] + 8;
  394. else if(argv[j] >= 100 && argv[j] <= 107)
  395. g->bg = ansi2cucul[argv[j] - 100] + 8;
  396. else switch(argv[j])
  397. {
  398. case 0: /* default rendition */
  399. g->fg = CUCUL_COLOR_DEFAULT;
  400. g->bg = CUCUL_COLOR_TRANSPARENT;
  401. g->bold = g->negative = g->concealed = 0;
  402. break;
  403. case 1: /* bold or increased intensity */
  404. g->bold = 1;
  405. break;
  406. case 4: /* singly underlined */
  407. break;
  408. case 5: /* slowly blinking (less then 150 per minute) */
  409. break;
  410. case 7: /* negative image */
  411. g->negative = 1;
  412. break;
  413. case 8: /* concealed characters */
  414. g->concealed = 1;
  415. break;
  416. case 22: /* normal colour or normal intensity (neither bold nor faint) */
  417. g->bold = 0;
  418. break;
  419. case 28: /* revealed characters */
  420. g->concealed = 0;
  421. break;
  422. case 39: /* default display colour (implementation-defined) */
  423. g->fg = CUCUL_COLOR_DEFAULT;
  424. break;
  425. case 49: /* default background colour (implementation-defined) */
  426. g->bg = CUCUL_COLOR_TRANSPARENT;
  427. break;
  428. default:
  429. fprintf(stderr, "unknown sgr %i\n", argv[j]);
  430. break;
  431. }
  432. }
  433. if(g->concealed)
  434. {
  435. g->efg = g->ebg = CUCUL_COLOR_TRANSPARENT;
  436. }
  437. else
  438. {
  439. g->efg = g->negative ? g->bg : g->fg;
  440. g->ebg = g->negative ? g->fg : g->bg;
  441. if(g->bold)
  442. {
  443. if(g->efg < 8)
  444. g->efg += 8;
  445. else if(g->efg == CUCUL_COLOR_DEFAULT)
  446. g->efg = CUCUL_COLOR_WHITE;
  447. }
  448. }
  449. }