Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

572 řádky
15 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains FIGlet and TOIlet font handling functions.
  16. */
  17. /*
  18. * FIXME: this file needs huge cleanup to be usable
  19. */
  20. #include "config.h"
  21. #if !defined(__KERNEL__)
  22. # include <stdio.h>
  23. # include <stdlib.h>
  24. # include <string.h>
  25. #endif
  26. #include "caca.h"
  27. #include "caca_internals.h"
  28. struct caca_figfont
  29. {
  30. int term_width;
  31. int x, y, w, h, lines;
  32. enum { H_DEFAULT, H_KERN, H_SMUSH, H_NONE, H_OVERLAP } hmode;
  33. int hsmushrule;
  34. uint32_t hardblank;
  35. int height, baseline, max_length;
  36. int old_layout;
  37. int print_direction, full_layout, codetag_count;
  38. int glyphs;
  39. caca_canvas_t *fontcv, *charcv;
  40. int *left, *right; /* Unused yet */
  41. uint32_t *lookup;
  42. };
  43. static uint32_t hsmush(uint32_t ch1, uint32_t ch2, int rule);
  44. static caca_figfont_t * open_figfont(char const *);
  45. static int free_figfont(caca_figfont_t *);
  46. int caca_canvas_set_figfont(caca_canvas_t *cv, char const *path)
  47. {
  48. caca_figfont_t *ff = NULL;
  49. if(path)
  50. {
  51. ff = open_figfont(path);
  52. if(!ff)
  53. return -1;
  54. }
  55. if(cv->ff)
  56. {
  57. caca_free_canvas(cv->ff->charcv);
  58. free(cv->ff->left);
  59. free(cv->ff->right);
  60. free_figfont(cv->ff);
  61. }
  62. cv->ff = ff;
  63. if(!path)
  64. return 0;
  65. /* from TOIlet’s main.c */
  66. ff->term_width = 80;
  67. ff->hmode = H_DEFAULT;
  68. /* from TOIlet’s render.c */
  69. ff->x = ff->y = 0;
  70. ff->w = ff->h = 0;
  71. ff->lines = 0;
  72. caca_set_canvas_size(cv, 0, 0); /* XXX */
  73. /* from TOIlet’s figlet.c */
  74. if(ff->full_layout & 0x3f)
  75. ff->hsmushrule = ff->full_layout & 0x3f;
  76. else if(ff->old_layout > 0)
  77. ff->hsmushrule = ff->old_layout;
  78. switch(ff->hmode)
  79. {
  80. case H_DEFAULT:
  81. if(ff->old_layout == -1)
  82. ff->hmode = H_NONE;
  83. else if(ff->old_layout == 0 && (ff->full_layout & 0xc0) == 0x40)
  84. ff->hmode = H_KERN;
  85. else if((ff->old_layout & 0x3f) && (ff->full_layout & 0x3f)
  86. && (ff->full_layout & 0x80))
  87. {
  88. ff->hmode = H_SMUSH;
  89. ff->hsmushrule = ff->full_layout & 0x3f;
  90. }
  91. else if(ff->old_layout == 0 && (ff->full_layout & 0xbf) == 0x80)
  92. {
  93. ff->hmode = H_SMUSH;
  94. ff->hsmushrule = 0x3f;
  95. }
  96. else
  97. ff->hmode = H_OVERLAP;
  98. break;
  99. default:
  100. break;
  101. }
  102. ff->charcv = caca_create_canvas(ff->max_length - 2, ff->height);
  103. ff->left = malloc(ff->height * sizeof(int));
  104. ff->right = malloc(ff->height * sizeof(int));
  105. cv->ff = ff;
  106. return 0;
  107. }
  108. int caca_put_figchar(caca_canvas_t *cv, uint32_t ch)
  109. {
  110. caca_figfont_t *ff = cv->ff;
  111. int c, w, h, x, y, overlap, extra, xleft, xright;
  112. if (!ff)
  113. return -1;
  114. switch(ch)
  115. {
  116. case (uint32_t)'\r':
  117. return 0;
  118. case (uint32_t)'\n':
  119. ff->x = 0;
  120. ff->y += ff->height;
  121. return 0;
  122. /* FIXME: handle '\t' */
  123. }
  124. /* Look whether our glyph is available */
  125. for(c = 0; c < ff->glyphs; c++)
  126. if(ff->lookup[c * 2] == ch)
  127. break;
  128. if(c == ff->glyphs)
  129. return 0;
  130. w = ff->lookup[c * 2 + 1];
  131. h = ff->height;
  132. caca_set_canvas_handle(ff->fontcv, 0, c * ff->height);
  133. caca_blit(ff->charcv, 0, 0, ff->fontcv, NULL);
  134. /* Check whether we reached the end of the screen */
  135. if(ff->x && ff->x + w > ff->term_width)
  136. {
  137. ff->x = 0;
  138. ff->y += h;
  139. }
  140. /* Compute how much the next character will overlap */
  141. switch(ff->hmode)
  142. {
  143. case H_SMUSH:
  144. case H_KERN:
  145. case H_OVERLAP:
  146. extra = (ff->hmode == H_OVERLAP);
  147. overlap = w;
  148. for(y = 0; y < h; y++)
  149. {
  150. /* Compute how much spaces we can eat from the new glyph */
  151. for(xright = 0; xright < overlap; xright++)
  152. if(caca_get_char(ff->charcv, xright, y) != ' ')
  153. break;
  154. /* Compute how much spaces we can eat from the previous glyph */
  155. for(xleft = 0; xright + xleft < overlap && xleft < ff->x; xleft++)
  156. if(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y) != ' ')
  157. break;
  158. /* Handle overlapping */
  159. if(ff->hmode == H_OVERLAP && xleft < ff->x)
  160. xleft++;
  161. /* Handle smushing */
  162. if(ff->hmode == H_SMUSH)
  163. {
  164. if(xleft < ff->x &&
  165. hsmush(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y),
  166. caca_get_char(ff->charcv, xright, y),
  167. ff->hsmushrule))
  168. xleft++;
  169. }
  170. if(xleft + xright < overlap)
  171. overlap = xleft + xright;
  172. }
  173. break;
  174. case H_NONE:
  175. overlap = 0;
  176. break;
  177. default:
  178. return -1;
  179. }
  180. /* Check whether the current canvas is large enough */
  181. if(ff->x + w - overlap > ff->w)
  182. ff->w = ff->x + w - overlap < ff->term_width
  183. ? ff->x + w - overlap : ff->term_width;
  184. if(ff->y + h > ff->h)
  185. ff->h = ff->y + h;
  186. #if 0 /* deactivated for libcaca insertion */
  187. if(attr)
  188. caca_set_attr(cv, attr);
  189. #endif
  190. caca_set_canvas_size(cv, ff->w, ff->h);
  191. /* Render our char (FIXME: create a rect-aware caca_blit_canvas?) */
  192. for(y = 0; y < h; y++)
  193. for(x = 0; x < w; x++)
  194. {
  195. uint32_t ch1, ch2;
  196. //uint32_t tmpat = caca_get_attr(ff->fontcv, x, y + c * ff->height);
  197. ch2 = caca_get_char(ff->charcv, x, y);
  198. if(ch2 == ' ')
  199. continue;
  200. ch1 = caca_get_char(cv, ff->x + x - overlap, ff->y + y);
  201. /* FIXME: this could be changed to caca_put_attr() when the
  202. * function is fixed in libcaca */
  203. //caca_set_attr(cv, tmpat);
  204. if(ch1 == ' ' || ff->hmode != H_SMUSH)
  205. caca_put_char(cv, ff->x + x - overlap, ff->y + y, ch2);
  206. else
  207. caca_put_char(cv, ff->x + x - overlap, ff->y + y,
  208. hsmush(ch1, ch2, ff->hsmushrule));
  209. //caca_put_attr(cv, ff->x + x, ff->y + y, tmpat);
  210. }
  211. /* Advance cursor */
  212. ff->x += w - overlap;
  213. return 0;
  214. }
  215. int caca_flush_figlet(caca_canvas_t *cv)
  216. {
  217. caca_figfont_t *ff = cv->ff;
  218. int x, y;
  219. if (!ff)
  220. return -1;
  221. //ff->torender = cv;
  222. //caca_set_canvas_size(ff->torender, ff->w, ff->h);
  223. caca_set_canvas_size(cv, ff->w, ff->h);
  224. /* FIXME: do this somewhere else, or record hardblank positions */
  225. for(y = 0; y < ff->h; y++)
  226. for(x = 0; x < ff->w; x++)
  227. if(caca_get_char(cv, x, y) == 0xa0)
  228. {
  229. uint32_t attr = caca_get_attr(cv, x, y);
  230. caca_put_char(cv, x, y, ' ');
  231. caca_put_attr(cv, x, y, attr);
  232. }
  233. ff->x = ff->y = 0;
  234. ff->w = ff->h = 0;
  235. //cv = caca_create_canvas(1, 1); /* XXX */
  236. /* from render.c */
  237. ff->lines += caca_get_canvas_height(cv);
  238. return 0;
  239. }
  240. #define STD_GLYPHS (127 - 32)
  241. #define EXT_GLYPHS (STD_GLYPHS + 7)
  242. static caca_figfont_t * open_figfont(char const *path)
  243. {
  244. #if !defined __KERNEL__ && defined HAVE_SNPRINTF
  245. char altpath[2048];
  246. #endif
  247. char buf[2048];
  248. char hardblank[10];
  249. caca_figfont_t *ff;
  250. char *data = NULL;
  251. caca_file_t *f;
  252. int i, j, size, comment_lines;
  253. ff = malloc(sizeof(caca_figfont_t));
  254. if(!ff)
  255. {
  256. seterrno(ENOMEM);
  257. return NULL;
  258. }
  259. /* Open font: if not found, try .tlf, then .flf */
  260. f = caca_file_open(path, "r");
  261. #if !defined __KERNEL__ && defined HAVE_SNPRINTF
  262. #if (! defined(snprintf)) && ( defined(_WIN32) || defined(WIN32) ) && (! defined(__CYGWIN__))
  263. #define snprintf _snprintf
  264. #endif
  265. if(!f)
  266. {
  267. snprintf(altpath, 2047, "%s.tlf", path);
  268. altpath[2047] = '\0';
  269. f = caca_file_open(altpath, "r");
  270. }
  271. if(!f)
  272. {
  273. snprintf(altpath, 2047, "%s.flf", path);
  274. altpath[2047] = '\0';
  275. f = caca_file_open(altpath, "r");
  276. }
  277. #endif
  278. if(!f)
  279. {
  280. free(ff);
  281. seterrno(ENOENT);
  282. return NULL;
  283. }
  284. /* Read header */
  285. ff->print_direction = 0;
  286. ff->full_layout = 0;
  287. ff->codetag_count = 0;
  288. caca_file_gets(f, buf, 2048);
  289. if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank,
  290. &ff->height, &ff->baseline, &ff->max_length,
  291. &ff->old_layout, &comment_lines, &ff->print_direction,
  292. &ff->full_layout, &ff->codetag_count) < 6)
  293. {
  294. debug("figfont error: `%s' has invalid header: %s", path, buf);
  295. caca_file_close(f);
  296. free(ff);
  297. seterrno(EINVAL);
  298. return NULL;
  299. }
  300. if(ff->old_layout < -1 || ff->old_layout > 63 || ff->full_layout > 32767
  301. || ((ff->full_layout & 0x80) && (ff->full_layout & 0x3f) == 0
  302. && ff->old_layout))
  303. {
  304. debug("figfont error: `%s' has invalid layout %i/%u",
  305. path, ff->old_layout, ff->full_layout);
  306. caca_file_close(f);
  307. free(ff);
  308. seterrno(EINVAL);
  309. return NULL;
  310. }
  311. ff->hardblank = caca_utf8_to_utf32(hardblank, NULL);
  312. /* Skip comment lines */
  313. for(i = 0; i < comment_lines; i++)
  314. caca_file_gets(f, buf, 2048);
  315. /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223)
  316. * then read additional characters. */
  317. ff->glyphs = 0;
  318. ff->lookup = NULL;
  319. for(i = 0, size = 0; !caca_file_eof(f); ff->glyphs++)
  320. {
  321. if((ff->glyphs % 2048) == 0)
  322. ff->lookup = realloc(ff->lookup,
  323. (ff->glyphs + 2048) * 2 * sizeof(int));
  324. if(ff->glyphs < STD_GLYPHS)
  325. {
  326. ff->lookup[ff->glyphs * 2] = 32 + ff->glyphs;
  327. }
  328. else if(ff->glyphs < EXT_GLYPHS)
  329. {
  330. static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 };
  331. ff->lookup[ff->glyphs * 2] = tab[ff->glyphs - STD_GLYPHS];
  332. }
  333. else
  334. {
  335. unsigned int tmp;
  336. if(caca_file_gets(f, buf, 2048) == NULL)
  337. break;
  338. /* Ignore blank lines, as in jacky.flf */
  339. if(buf[0] == '\n' || buf[0] == '\r')
  340. continue;
  341. /* Ignore negative indices for now, as in ivrit.flf */
  342. if(buf[0] == '-')
  343. {
  344. for(j = 0; j < ff->height; j++)
  345. caca_file_gets(f, buf, 2048);
  346. continue;
  347. }
  348. if(!buf[0] || buf[0] < '0' || buf[0] > '9')
  349. {
  350. debug("figfont error: glyph #%u in `%s'", ff->glyphs, path);
  351. free(data);
  352. free(ff->lookup);
  353. free(ff);
  354. seterrno(EINVAL);
  355. return NULL;
  356. }
  357. sscanf(buf, buf[1] == 'x' ? "%x" : "%u", &tmp);
  358. ff->lookup[ff->glyphs * 2] = tmp;
  359. }
  360. ff->lookup[ff->glyphs * 2 + 1] = 0;
  361. for(j = 0; j < ff->height; j++)
  362. {
  363. if(i + 2048 >= size)
  364. data = realloc(data, size += 2048);
  365. caca_file_gets(f, data + i, 2048);
  366. i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
  367. }
  368. }
  369. caca_file_close(f);
  370. if(ff->glyphs < EXT_GLYPHS)
  371. {
  372. debug("figfont error: only %u glyphs in `%s', expected at least %u",
  373. ff->glyphs, path, EXT_GLYPHS);
  374. free(data);
  375. free(ff->lookup);
  376. free(ff);
  377. seterrno(EINVAL);
  378. return NULL;
  379. }
  380. /* Import buffer into canvas */
  381. ff->fontcv = caca_create_canvas(0, 0);
  382. caca_import_canvas_from_memory(ff->fontcv, data, i, "utf8");
  383. free(data);
  384. /* Remove EOL characters. For now we ignore hardblanks, don’t do any
  385. * smushing, nor any kind of error checking. */
  386. for(j = 0; j < ff->height * ff->glyphs; j++)
  387. {
  388. uint32_t ch, oldch = 0;
  389. for(i = ff->max_length; i--;)
  390. {
  391. ch = caca_get_char(ff->fontcv, i, j);
  392. /* Replace hardblanks with U+00A0 NO-BREAK SPACE */
  393. if(ch == ff->hardblank)
  394. caca_put_char(ff->fontcv, i, j, ch = 0xa0);
  395. if(oldch && ch != oldch)
  396. {
  397. if(!ff->lookup[j / ff->height * 2 + 1])
  398. ff->lookup[j / ff->height * 2 + 1] = i + 1;
  399. }
  400. else if(oldch && ch == oldch)
  401. caca_put_char(ff->fontcv, i, j, ' ');
  402. else if(ch != ' ')
  403. {
  404. oldch = ch;
  405. caca_put_char(ff->fontcv, i, j, ' ');
  406. }
  407. }
  408. }
  409. return ff;
  410. }
  411. int free_figfont(caca_figfont_t *ff)
  412. {
  413. caca_free_canvas(ff->fontcv);
  414. free(ff->lookup);
  415. free(ff);
  416. return 0;
  417. }
  418. static uint32_t hsmush(uint32_t ch1, uint32_t ch2, int rule)
  419. {
  420. /* Rule 1 */
  421. if((rule & 0x01) && ch1 == ch2 && ch1 != 0xa0)
  422. return ch2;
  423. if(ch1 < 0x80 && ch2 < 0x80)
  424. {
  425. char const charlist[] = "|/\\[]{}()<>";
  426. char *tmp1, *tmp2;
  427. /* Rule 2 */
  428. if(rule & 0x02)
  429. {
  430. if(ch1 == '_' && strchr(charlist, ch2))
  431. return ch2;
  432. if(ch2 == '_' && strchr(charlist, ch1))
  433. return ch1;
  434. }
  435. /* Rule 3 */
  436. if((rule & 0x04) &&
  437. (tmp1 = strchr(charlist, ch1)) && (tmp2 = strchr(charlist, ch2)))
  438. {
  439. int cl1 = (tmp1 + 1 - charlist) / 2;
  440. int cl2 = (tmp2 + 1 - charlist) / 2;
  441. if(cl1 < cl2)
  442. return ch2;
  443. if(cl1 > cl2)
  444. return ch1;
  445. }
  446. /* Rule 4 */
  447. if(rule & 0x08)
  448. {
  449. uint16_t s = ch1 + ch2;
  450. uint16_t p = ch1 * ch2;
  451. if(p == 15375 /* '{' * '}' */
  452. || p == 8463 /* '[' * ']' */
  453. || (p == 1640 && s == 81)) /* '(' *|+ ')' */
  454. return '|';
  455. }
  456. /* Rule 5 */
  457. if(rule & 0x10)
  458. {
  459. switch((ch1 << 8) | ch2)
  460. {
  461. case 0x2f5c: return '|'; /* /\ */
  462. case 0x5c2f: return 'Y'; /* \/ */
  463. case 0x3e3c: return 'X'; /* >< */
  464. }
  465. }
  466. /* Rule 6 */
  467. if((rule & 0x20) && ch1 == ch2 && ch1 == 0xa0)
  468. return 0xa0;
  469. }
  470. return 0;
  471. }
  472. /*
  473. * XXX: The following functions are aliases.
  474. */
  475. int cucul_canvas_set_figfont(cucul_canvas_t *, char const *)
  476. CACA_ALIAS(caca_canvas_set_figfont);
  477. int cucul_put_figchar(cucul_canvas_t *, uint32_t) CACA_ALIAS(caca_put_figchar);
  478. int cucul_flush_figlet(cucul_canvas_t *) CACA_ALIAS(caca_flush_figlet);