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.

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