您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2006-2007 Sam Hocevar <sam@zoy.org>
  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. switch(ch)
  113. {
  114. case (uint32_t)'\r':
  115. return 0;
  116. case (uint32_t)'\n':
  117. ff->x = 0;
  118. ff->y += ff->height;
  119. return 0;
  120. /* FIXME: handle '\t' */
  121. }
  122. /* Look whether our glyph is available */
  123. for(c = 0; c < ff->glyphs; c++)
  124. if(ff->lookup[c * 2] == ch)
  125. break;
  126. if(c == ff->glyphs)
  127. return 0;
  128. w = ff->lookup[c * 2 + 1];
  129. h = ff->height;
  130. caca_set_canvas_handle(ff->fontcv, 0, c * ff->height);
  131. caca_blit(ff->charcv, 0, 0, ff->fontcv, NULL);
  132. /* Check whether we reached the end of the screen */
  133. if(ff->x && ff->x + w > ff->term_width)
  134. {
  135. ff->x = 0;
  136. ff->y += h;
  137. }
  138. /* Compute how much the next character will overlap */
  139. switch(ff->hmode)
  140. {
  141. case H_SMUSH:
  142. case H_KERN:
  143. case H_OVERLAP:
  144. extra = (ff->hmode == H_OVERLAP);
  145. overlap = w;
  146. for(y = 0; y < h; y++)
  147. {
  148. /* Compute how much spaces we can eat from the new glyph */
  149. for(xright = 0; xright < overlap; xright++)
  150. if(caca_get_char(ff->charcv, xright, y) != ' ')
  151. break;
  152. /* Compute how much spaces we can eat from the previous glyph */
  153. for(xleft = 0; xright + xleft < overlap && xleft < ff->x; xleft++)
  154. if(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y) != ' ')
  155. break;
  156. /* Handle overlapping */
  157. if(ff->hmode == H_OVERLAP && xleft < ff->x)
  158. xleft++;
  159. /* Handle smushing */
  160. if(ff->hmode == H_SMUSH)
  161. {
  162. if(xleft < ff->x &&
  163. hsmush(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y),
  164. caca_get_char(ff->charcv, xright, y),
  165. ff->hsmushrule))
  166. xleft++;
  167. }
  168. if(xleft + xright < overlap)
  169. overlap = xleft + xright;
  170. }
  171. break;
  172. case H_NONE:
  173. overlap = 0;
  174. break;
  175. default:
  176. return -1;
  177. }
  178. /* Check whether the current canvas is large enough */
  179. if(ff->x + w - overlap > ff->w)
  180. ff->w = ff->x + w - overlap < ff->term_width
  181. ? ff->x + w - overlap : ff->term_width;
  182. if(ff->y + h > ff->h)
  183. ff->h = ff->y + h;
  184. #if 0 /* deactivated for libcaca insertion */
  185. if(attr)
  186. caca_set_attr(cv, attr);
  187. #endif
  188. caca_set_canvas_size(cv, ff->w, ff->h);
  189. /* Render our char (FIXME: create a rect-aware caca_blit_canvas?) */
  190. for(y = 0; y < h; y++)
  191. for(x = 0; x < w; x++)
  192. {
  193. uint32_t ch1, ch2;
  194. //uint32_t tmpat = caca_get_attr(ff->fontcv, x, y + c * ff->height);
  195. ch2 = caca_get_char(ff->charcv, x, y);
  196. if(ch2 == ' ')
  197. continue;
  198. ch1 = caca_get_char(cv, ff->x + x - overlap, ff->y + y);
  199. /* FIXME: this could be changed to caca_put_attr() when the
  200. * function is fixed in libcaca */
  201. //caca_set_attr(cv, tmpat);
  202. if(ch1 == ' ' || ff->hmode != H_SMUSH)
  203. caca_put_char(cv, ff->x + x - overlap, ff->y + y, ch2);
  204. else
  205. caca_put_char(cv, ff->x + x - overlap, ff->y + y,
  206. hsmush(ch1, ch2, ff->hsmushrule));
  207. //caca_put_attr(cv, ff->x + x, ff->y + y, tmpat);
  208. }
  209. /* Advance cursor */
  210. ff->x += w - overlap;
  211. return 0;
  212. }
  213. int caca_flush_figlet(caca_canvas_t *cv)
  214. {
  215. caca_figfont_t *ff = cv->ff;
  216. int x, y;
  217. //ff->torender = cv;
  218. //caca_set_canvas_size(ff->torender, ff->w, ff->h);
  219. caca_set_canvas_size(cv, ff->w, ff->h);
  220. /* FIXME: do this somewhere else, or record hardblank positions */
  221. for(y = 0; y < ff->h; y++)
  222. for(x = 0; x < ff->w; x++)
  223. if(caca_get_char(cv, x, y) == 0xa0)
  224. {
  225. uint32_t attr = caca_get_attr(cv, x, y);
  226. caca_put_char(cv, x, y, ' ');
  227. caca_put_attr(cv, x, y, attr);
  228. }
  229. ff->x = ff->y = 0;
  230. ff->w = ff->h = 0;
  231. //cv = caca_create_canvas(1, 1); /* XXX */
  232. /* from render.c */
  233. ff->lines += caca_get_canvas_height(cv);
  234. return 0;
  235. }
  236. #define STD_GLYPHS (127 - 32)
  237. #define EXT_GLYPHS (STD_GLYPHS + 7)
  238. static caca_figfont_t * open_figfont(char const *path)
  239. {
  240. char altpath[2048];
  241. char buf[2048];
  242. char hardblank[10];
  243. caca_figfont_t *ff;
  244. char *data = NULL;
  245. caca_file_t *f;
  246. int i, j, size, comment_lines;
  247. ff = malloc(sizeof(caca_figfont_t));
  248. if(!ff)
  249. {
  250. seterrno(ENOMEM);
  251. return NULL;
  252. }
  253. /* Open font: if not found, try .tlf, then .flf */
  254. f = caca_file_open(path, "r");
  255. #if !defined __KERNEL__ && defined HAVE_SNPRINTF
  256. #if (! defined(snprintf)) && ( defined(_WIN32) || defined(WIN32) ) && (! defined(__CYGWIN__))
  257. #define snprintf _snprintf
  258. #endif
  259. if(!f)
  260. {
  261. snprintf(altpath, 2047, "%s.tlf", path);
  262. altpath[2047] = '\0';
  263. f = caca_file_open(altpath, "r");
  264. }
  265. if(!f)
  266. {
  267. snprintf(altpath, 2047, "%s.flf", path);
  268. altpath[2047] = '\0';
  269. f = caca_file_open(altpath, "r");
  270. }
  271. #endif
  272. if(!f)
  273. {
  274. free(ff);
  275. seterrno(ENOENT);
  276. return NULL;
  277. }
  278. /* Read header */
  279. ff->print_direction = 0;
  280. ff->full_layout = 0;
  281. ff->codetag_count = 0;
  282. caca_file_gets(f, buf, 2048);
  283. if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank,
  284. &ff->height, &ff->baseline, &ff->max_length,
  285. &ff->old_layout, &comment_lines, &ff->print_direction,
  286. &ff->full_layout, &ff->codetag_count) < 6)
  287. {
  288. debug("figfont error: `%s' has invalid header: %s", path, buf);
  289. caca_file_close(f);
  290. free(ff);
  291. seterrno(EINVAL);
  292. return NULL;
  293. }
  294. if(ff->old_layout < -1 || ff->old_layout > 63 || ff->full_layout > 32767
  295. || ((ff->full_layout & 0x80) && (ff->full_layout & 0x3f) == 0
  296. && ff->old_layout))
  297. {
  298. debug("figfont error: `%s' has invalid layout %i/%u",
  299. path, ff->old_layout, ff->full_layout);
  300. caca_file_close(f);
  301. free(ff);
  302. seterrno(EINVAL);
  303. return NULL;
  304. }
  305. ff->hardblank = caca_utf8_to_utf32(hardblank, NULL);
  306. /* Skip comment lines */
  307. for(i = 0; i < comment_lines; i++)
  308. caca_file_gets(f, buf, 2048);
  309. /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223)
  310. * then read additional characters. */
  311. ff->glyphs = 0;
  312. ff->lookup = NULL;
  313. for(i = 0, size = 0; !caca_file_eof(f); ff->glyphs++)
  314. {
  315. if((ff->glyphs % 2048) == 0)
  316. ff->lookup = realloc(ff->lookup,
  317. (ff->glyphs + 2048) * 2 * sizeof(int));
  318. if(ff->glyphs < STD_GLYPHS)
  319. {
  320. ff->lookup[ff->glyphs * 2] = 32 + ff->glyphs;
  321. }
  322. else if(ff->glyphs < EXT_GLYPHS)
  323. {
  324. static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 };
  325. ff->lookup[ff->glyphs * 2] = tab[ff->glyphs - STD_GLYPHS];
  326. }
  327. else
  328. {
  329. if(caca_file_gets(f, buf, 2048) == NULL)
  330. break;
  331. /* Ignore blank lines, as in jacky.flf */
  332. if(buf[0] == '\n' || buf[0] == '\r')
  333. continue;
  334. /* Ignore negative indices for now, as in ivrit.flf */
  335. if(buf[0] == '-')
  336. {
  337. for(j = 0; j < ff->height; j++)
  338. caca_file_gets(f, buf, 2048);
  339. continue;
  340. }
  341. if(!buf[0] || buf[0] < '0' || buf[0] > '9')
  342. {
  343. debug("figfont error: glyph #%u in `%s'", ff->glyphs, path);
  344. free(data);
  345. free(ff->lookup);
  346. free(ff);
  347. seterrno(EINVAL);
  348. return NULL;
  349. }
  350. if(buf[1] == 'x')
  351. sscanf(buf, "%x", &ff->lookup[ff->glyphs * 2]);
  352. else
  353. sscanf(buf, "%u", &ff->lookup[ff->glyphs * 2]);
  354. }
  355. ff->lookup[ff->glyphs * 2 + 1] = 0;
  356. for(j = 0; j < ff->height; j++)
  357. {
  358. if(i + 2048 >= size)
  359. data = realloc(data, size += 2048);
  360. caca_file_gets(f, data + i, 2048);
  361. i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
  362. }
  363. }
  364. caca_file_close(f);
  365. if(ff->glyphs < EXT_GLYPHS)
  366. {
  367. debug("figfont error: only %u glyphs in `%s', expected at least %u",
  368. ff->glyphs, path, EXT_GLYPHS);
  369. free(data);
  370. free(ff->lookup);
  371. free(ff);
  372. seterrno(EINVAL);
  373. return NULL;
  374. }
  375. /* Import buffer into canvas */
  376. ff->fontcv = caca_create_canvas(0, 0);
  377. caca_import_memory(ff->fontcv, data, i, "utf8");
  378. free(data);
  379. /* Remove EOL characters. For now we ignore hardblanks, don’t do any
  380. * smushing, nor any kind of error checking. */
  381. for(j = 0; j < ff->height * ff->glyphs; j++)
  382. {
  383. uint32_t ch, oldch = 0;
  384. for(i = ff->max_length; i--;)
  385. {
  386. ch = caca_get_char(ff->fontcv, i, j);
  387. /* Replace hardblanks with U+00A0 NO-BREAK SPACE */
  388. if(ch == ff->hardblank)
  389. caca_put_char(ff->fontcv, i, j, ch = 0xa0);
  390. if(oldch && ch != oldch)
  391. {
  392. if(!ff->lookup[j / ff->height * 2 + 1])
  393. ff->lookup[j / ff->height * 2 + 1] = i + 1;
  394. }
  395. else if(oldch && ch == oldch)
  396. caca_put_char(ff->fontcv, i, j, ' ');
  397. else if(ch != ' ')
  398. {
  399. oldch = ch;
  400. caca_put_char(ff->fontcv, i, j, ' ');
  401. }
  402. }
  403. }
  404. return ff;
  405. }
  406. int free_figfont(caca_figfont_t *ff)
  407. {
  408. caca_free_canvas(ff->fontcv);
  409. free(ff->lookup);
  410. free(ff);
  411. return 0;
  412. }
  413. static uint32_t hsmush(uint32_t ch1, uint32_t ch2, int rule)
  414. {
  415. /* Rule 1 */
  416. if((rule & 0x01) && ch1 == ch2 && ch1 != 0xa0)
  417. return ch2;
  418. if(ch1 < 0x80 && ch2 < 0x80)
  419. {
  420. char const charlist[] = "|/\\[]{}()<>";
  421. char *tmp1, *tmp2;
  422. /* Rule 2 */
  423. if(rule & 0x02)
  424. {
  425. if(ch1 == '_' && strchr(charlist, ch2))
  426. return ch2;
  427. if(ch2 == '_' && strchr(charlist, ch1))
  428. return ch1;
  429. }
  430. /* Rule 3 */
  431. if((rule & 0x04) &&
  432. (tmp1 = strchr(charlist, ch1)) && (tmp2 = strchr(charlist, ch2)))
  433. {
  434. int cl1 = (tmp1 + 1 - charlist) / 2;
  435. int cl2 = (tmp2 + 1 - charlist) / 2;
  436. if(cl1 < cl2)
  437. return ch2;
  438. if(cl1 > cl2)
  439. return ch1;
  440. }
  441. /* Rule 4 */
  442. if(rule & 0x08)
  443. {
  444. uint16_t s = ch1 + ch2;
  445. uint16_t p = ch1 * ch2;
  446. if(p == 15375 /* '{' * '}' */
  447. || p == 8463 /* '[' * ']' */
  448. || (p == 1640 && s == 81)) /* '(' *|+ ')' */
  449. return '|';
  450. }
  451. /* Rule 5 */
  452. if(rule & 0x10)
  453. {
  454. switch((ch1 << 8) | ch2)
  455. {
  456. case 0x2f5c: return '|'; /* /\ */
  457. case 0x5c2f: return 'Y'; /* \/ */
  458. case 0x3e3c: return 'X'; /* >< */
  459. }
  460. }
  461. /* Rule 6 */
  462. if((rule & 0x20) && ch1 == ch2 && ch1 == 0xa0)
  463. return 0xa0;
  464. }
  465. return 0;
  466. }
  467. /*
  468. * XXX: The following functions are aliases.
  469. */
  470. int cucul_canvas_set_figfont(cucul_canvas_t *, char const *)
  471. CACA_ALIAS(caca_canvas_set_figfont);
  472. int cucul_put_figchar(cucul_canvas_t *, uint32_t) CACA_ALIAS(caca_put_figchar);
  473. int cucul_flush_figlet(cucul_canvas_t *) CACA_ALIAS(caca_flush_figlet);