Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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