You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

446 lines
13 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. # include <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "cucul_internals.h"
  25. static cucul_canvas_t *import_caca(void const *, unsigned int);
  26. static cucul_canvas_t *import_text(void const *, unsigned int);
  27. static cucul_canvas_t *import_ansi(void const *, unsigned int);
  28. /** \brief Import a buffer into a canvas
  29. *
  30. * This function imports a libcucul buffer (cucul_load_memory()/cucul_load_file())
  31. * into an internal libcucul canvas.
  32. *
  33. * Valid values for \c format are:
  34. *
  35. * \li \c "": attempt to autodetect the file format.
  36. *
  37. * \li \c "ansi": import ANSI files.
  38. *
  39. * \li \c "caca": import native libcaca files.
  40. *
  41. * \param buffer A \e libcucul buffer containing the data to be loaded
  42. * into a canvas.
  43. * \param format A string describing the input format.
  44. * \return A libcucul canvas, or NULL in case of error.
  45. */
  46. cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buffer, char const *format)
  47. {
  48. char const *buf = (char const*)buffer->data;
  49. if(buffer->size == 0 || buffer->data == NULL)
  50. return NULL;
  51. if(!strcasecmp("caca", format))
  52. return import_caca(buffer->data, buffer->size);
  53. if(!strcasecmp("text", format))
  54. return import_text(buffer->data, buffer->size);
  55. if(!strcasecmp("ansi", format))
  56. return import_ansi(buffer->data, buffer->size);
  57. /* Autodetection */
  58. if(!strcasecmp("", format))
  59. {
  60. unsigned int i=0;
  61. /* if 4 first letters are CACA */
  62. if(buffer->size >= 4 &&
  63. buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A')
  64. return import_caca(buffer->data, buffer->size);
  65. /* If we find ESC[ argv, we guess it's an ANSI file */
  66. while(i < buffer->size - 1)
  67. {
  68. if((buf[i] == 0x1b) && (buf[i+1] == '['))
  69. return import_ansi(buffer->data, buffer->size);
  70. i++;
  71. }
  72. /* Otherwise, import it as text */
  73. return import_text(buffer->data, buffer->size);
  74. }
  75. return NULL;
  76. }
  77. /** \brief Get available import formats
  78. *
  79. * Return a list of available import formats. The list is a NULL-terminated
  80. * array of strings, interleaving a string containing the internal value for
  81. * the import format, to be used with cucul_import_canvas(), and a string
  82. * containing the natural language description for that import format.
  83. *
  84. * \return An array of strings.
  85. */
  86. char const * const * cucul_get_import_list(void)
  87. {
  88. static char const * const list[] =
  89. {
  90. "", "autodetect",
  91. "text", "plain text",
  92. "caca", "native libcaca format",
  93. "ansi", "ANSI coloured text",
  94. NULL, NULL
  95. };
  96. return list;
  97. }
  98. /*
  99. * XXX: the following functions are local.
  100. */
  101. static cucul_canvas_t *import_caca(void const *data, unsigned int size)
  102. {
  103. cucul_canvas_t *cv;
  104. uint8_t const *buf = (uint8_t const *)data;
  105. unsigned int width, height, n;
  106. if(size < 16)
  107. return NULL;
  108. if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
  109. return NULL;
  110. if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
  111. return NULL;
  112. width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
  113. | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
  114. height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
  115. | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
  116. if(!width || !height)
  117. return NULL;
  118. if(size != 16 + width * height * 8)
  119. return NULL;
  120. cv = cucul_create_canvas(width, height);
  121. if(!cv)
  122. return NULL;
  123. for(n = height * width; n--; )
  124. {
  125. cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
  126. | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
  127. | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
  128. | (uint32_t)buf[16 + 3 + 8 * n];
  129. cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
  130. | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
  131. | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
  132. | (uint32_t)buf[16 + 7 + 8 * n];
  133. }
  134. return cv;
  135. }
  136. static cucul_canvas_t *import_text(void const *data, unsigned int size)
  137. {
  138. cucul_canvas_t *cv;
  139. char const *text = (char const *)data;
  140. unsigned int width = 1, height = 1, x = 0, y = 0, i;
  141. cv = cucul_create_canvas(width, height);
  142. cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT);
  143. for(i = 0; i < size; i++)
  144. {
  145. unsigned char ch = *text++;
  146. if(ch == '\r')
  147. continue;
  148. if(ch == '\n')
  149. {
  150. x = 0;
  151. y++;
  152. continue;
  153. }
  154. if(x >= width || y >= height)
  155. {
  156. if(x >= width)
  157. width = x + 1;
  158. if(y >= height)
  159. height = y + 1;
  160. cucul_set_canvas_size(cv, width, height);
  161. }
  162. cucul_putchar(cv, x, y, ch);
  163. x++;
  164. }
  165. return cv;
  166. }
  167. /* Graphic Rendition Combination Mode */
  168. struct grcm
  169. {
  170. uint8_t fg, bg;
  171. uint8_t bold, negative, concealed;
  172. };
  173. static void manage_modifiers(struct grcm *, int);
  174. static cucul_canvas_t *import_ansi(void const *data, unsigned int size)
  175. {
  176. struct grcm grcm;
  177. unsigned char const *buffer = (unsigned char const*)data;
  178. cucul_canvas_t *cv;
  179. unsigned int i, j, skip;
  180. unsigned int width = 80, height = 25;
  181. int x = 0, y = 0, save_x = 0, save_y = 0;
  182. manage_modifiers(&grcm, 0);
  183. cv = cucul_create_canvas(width, height);
  184. cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_DEFAULT);
  185. for(i = 0; i < size; i += skip)
  186. {
  187. skip = 1;
  188. if(buffer[i] == '\x1a' && size - i >= 8
  189. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  190. break; /* End before SAUCE data */
  191. if(buffer[i] == '\r')
  192. continue; /* DOS sucks */
  193. if(buffer[i] == '\n')
  194. {
  195. x = 0;
  196. y++;
  197. continue;
  198. }
  199. /* Interpret escape commands, as per Standard ECMA-48 "Control
  200. * Functions for Coded Character Sets", 5.4. Control sequences. */
  201. if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  202. {
  203. unsigned int argc = 0, argv[101];
  204. unsigned int param, inter, final;
  205. /* Compute offsets to parameter bytes, intermediate bytes and
  206. * to the final byte. Only the final byte is mandatory, there
  207. * can be zero of the others.
  208. * 0 param=2 inter final final+1
  209. * +-----+------------------+---------------------+-----------------+
  210. * | CSI | parameter bytes | intermediate bytes | final byte |
  211. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  212. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  213. * +-----+------------------+---------------------+-----------------+
  214. */
  215. param = 2;
  216. for(inter = param; i + inter < size; inter++)
  217. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  218. break;
  219. for(final = inter; i + final < size; final++)
  220. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  221. break;
  222. if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  223. break; /* Invalid Final Byte */
  224. skip += final;
  225. /* Sanity checks */
  226. if(param < inter && buffer[i + param] >= 0x3c)
  227. {
  228. //fprintf(stderr, "private sequence \"^[[%.*s\"\n",
  229. // final - param + 1, buffer + i + param);
  230. continue; /* Private sequence, skip it entirely */
  231. }
  232. if(final - param > 100)
  233. continue; /* Suspiciously long sequence, skip it */
  234. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  235. * format */
  236. if(param < inter)
  237. {
  238. argv[0] = 0;
  239. for(j = param; j < inter; j++)
  240. {
  241. if(buffer[i + j] == ';')
  242. argv[++argc] = 0;
  243. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  244. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  245. }
  246. argc++;
  247. }
  248. /* Interpret final byte. The code representations are given in
  249. * ECMA-48 5.4: Control sequences, and the code definitions are
  250. * given in ECMA-48 8.3: Definition of control functions. */
  251. switch(buffer[i + final])
  252. {
  253. case 'f': /* CUP - Cursor Position */
  254. case 'H': /* HVP - Character And Line Position */
  255. x = (argc > 1) ? argv[1] - 1 : 0;
  256. y = (argc > 0) ? argv[0] - 1 : 0;
  257. break;
  258. case 'A': /* CUU - Cursor Up */
  259. y -= argc ? argv[0] : 1;
  260. if(y < 0)
  261. y = 0;
  262. break;
  263. case 'B': /* CUD - Cursor Down */
  264. y += argc ? argv[0] : 1;
  265. break;
  266. case 'C': /* CUF - Cursor Right */
  267. x += argc ? argv[0] : 1;
  268. break;
  269. case 'D': /* CUB - Cursor Left */
  270. x -= argc ? argv[0] : 1;
  271. if(x < 0)
  272. x = 0;
  273. break;
  274. case 's': /* Private (save cursor position) */
  275. save_x = x;
  276. save_y = y;
  277. break;
  278. case 'u': /* Private (reload cursor positin) */
  279. x = save_x;
  280. y = save_y;
  281. break;
  282. case 'J': /* ED - Erase In Page */
  283. if(argv[0] == 2)
  284. x = y = 0;
  285. break;
  286. case 'K': /* EL - Erase In Line */
  287. for(j = x; j < width; j++)
  288. _cucul_putchar32(cv, j, y, (uint32_t)' ');
  289. x = width;
  290. break;
  291. case 'm': /* SGR - Select Graphic Rendition */
  292. for(j = 0; j < argc; j++)
  293. manage_modifiers(&grcm, argv[j]);
  294. if(grcm.concealed)
  295. cucul_set_color(cv, CUCUL_COLOR_TRANSPARENT,
  296. CUCUL_COLOR_TRANSPARENT);
  297. else if(grcm.negative)
  298. cucul_set_color(cv, (grcm.bold && grcm.bg < 8) ?
  299. grcm.bg + 8 : grcm.bg, grcm.fg);
  300. else
  301. cucul_set_color(cv, (grcm.bold && grcm.fg < 8) ?
  302. grcm.fg + 8 : grcm.fg, grcm.bg);
  303. break;
  304. default:
  305. break;
  306. }
  307. continue;
  308. }
  309. /* We're going to paste a character. First make sure the canvas
  310. * is big enough. */
  311. if((unsigned int)x >= width)
  312. {
  313. x = 0;
  314. y++;
  315. }
  316. if((unsigned int)y >= height)
  317. {
  318. height = y + 1;
  319. cucul_set_canvas_size(cv, width, height);
  320. }
  321. /* Now paste our character */
  322. _cucul_putchar32(cv, x, y,_cucul_cp437_to_utf32(buffer[i]));
  323. x++;
  324. }
  325. return cv;
  326. }
  327. /* XXX : ANSI loader helper */
  328. static void manage_modifiers(struct grcm *g, int i)
  329. {
  330. static uint8_t const ansi2cucul[] =
  331. {
  332. CUCUL_COLOR_BLACK, CUCUL_COLOR_RED,
  333. CUCUL_COLOR_GREEN, CUCUL_COLOR_BROWN,
  334. CUCUL_COLOR_BLUE, CUCUL_COLOR_MAGENTA,
  335. CUCUL_COLOR_CYAN, CUCUL_COLOR_LIGHTGRAY
  336. };
  337. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  338. if(i >= 30 && i <= 37)
  339. g->fg = ansi2cucul[i - 30];
  340. else if(i >= 40 && i <= 47)
  341. g->bg = ansi2cucul[i - 40];
  342. else if(i >= 90 && i <= 97)
  343. g->fg = ansi2cucul[i - 90] + 8;
  344. else if(i >= 100 && i <= 107)
  345. g->bg = ansi2cucul[i - 100] + 8;
  346. else switch(i)
  347. {
  348. case 0: /* default rendition */
  349. g->fg = CUCUL_COLOR_DEFAULT;
  350. g->bg = CUCUL_COLOR_DEFAULT;
  351. g->bold = g->negative = g->concealed = 0;
  352. break;
  353. case 1: /* bold or increased intensity */
  354. g->bold = 1;
  355. break;
  356. case 4: /* singly underlined */
  357. break;
  358. case 5: /* slowly blinking (less then 150 per minute) */
  359. break;
  360. case 7: /* negative image */
  361. g->negative = 1;
  362. break;
  363. case 8: /* concealed characters */
  364. g->concealed = 1;
  365. break;
  366. case 22: /* normal colour or normal intensity (neither bold nor faint) */
  367. g->bold = 0;
  368. break;
  369. case 28: /* revealed characters */
  370. g->concealed = 0;
  371. break;
  372. case 39: /* default display colour (implementation-defined) */
  373. g->fg = CUCUL_COLOR_DEFAULT;
  374. break;
  375. case 49: /* default background colour (implementation-defined) */
  376. g->bg = CUCUL_COLOR_DEFAULT;
  377. break;
  378. default:
  379. //fprintf(stderr, "unknown sgr %i\n", i);
  380. break;
  381. }
  382. }