ufont_ttf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /* Minimal fixed-point TrueType parser and rasterizer.
  2. *
  3. * No floats, no hinting, no CFF. Supports the tables and glyph forms that the
  4. * Latin-1 Arimo/Liberation subsets use: simple and composite glyphs, cmap
  5. * format 4, short/long loca, hmtx advances. Coordinates are scaled to 26.6
  6. * pixels; coverage is 4x vertical supersampling with exact horizontal spans.
  7. */
  8. #include "ufont_internal.h"
  9. #include <string.h>
  10. #define TTF_MAX_EDGES 2048
  11. struct ttf_point {
  12. int x, y;
  13. unsigned char on;
  14. };
  15. struct ttf_outline {
  16. int npoints;
  17. int ncontours;
  18. unsigned short ends[UFONT_MAX_CONTOURS];
  19. struct ttf_point points[UFONT_MAX_POINTS];
  20. };
  21. struct ttf_seq_point {
  22. int x, y;
  23. unsigned char on;
  24. };
  25. struct ttf_edge {
  26. int x0, y0, x1, y1;
  27. signed char dir;
  28. };
  29. static struct ttf_outline comp_outline[4];
  30. static struct ttf_seq_point seq_points[UFONT_MAX_POINTS * 2];
  31. static struct ttf_edge edges[TTF_MAX_EDGES];
  32. static int n_edges;
  33. static unsigned int be16(const unsigned char *p)
  34. {
  35. return ((unsigned int)p[0] << 8) | p[1];
  36. }
  37. static int sbe16(const unsigned char *p)
  38. {
  39. return (int)(short)be16(p);
  40. }
  41. static unsigned int be32(const unsigned char *p)
  42. {
  43. return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
  44. ((unsigned int)p[2] << 8) | p[3];
  45. }
  46. static int tag_eq(const unsigned char *p, const char *tag)
  47. {
  48. return p[0] == (unsigned char)tag[0] &&
  49. p[1] == (unsigned char)tag[1] &&
  50. p[2] == (unsigned char)tag[2] &&
  51. p[3] == (unsigned char)tag[3];
  52. }
  53. static const unsigned char *find_table(const unsigned char *data,
  54. unsigned int len, const char *tag,
  55. unsigned int *out_len)
  56. {
  57. unsigned int n, i;
  58. const unsigned char *dir;
  59. if (len < 12)
  60. return 0;
  61. n = be16(data + 4);
  62. dir = data + 12;
  63. if (n > 64 || 12u + n * 16u > len)
  64. return 0;
  65. for (i = 0; i < n; i++) {
  66. const unsigned char *r = dir + i * 16;
  67. unsigned int off = be32(r + 8);
  68. unsigned int l = be32(r + 12);
  69. if (!tag_eq(r, tag))
  70. continue;
  71. if (off > len || l > len - off)
  72. return 0;
  73. if (out_len)
  74. *out_len = l;
  75. return data + off;
  76. }
  77. return 0;
  78. }
  79. int ufont_ttf_load(struct ufont_face *f, const unsigned char *data,
  80. unsigned int len)
  81. {
  82. const unsigned char *head, *maxp, *hhea, *hmtx, *loca, *glyf, *cmap;
  83. unsigned int head_len, maxp_len, hhea_len, hmtx_len, loca_len;
  84. unsigned int glyf_len, cmap_len, i, nrec;
  85. unsigned int cmap4_off = 0;
  86. if (len < 12)
  87. return -1;
  88. /* 0x00010000 is TrueType; "true" also appears in older files. */
  89. if (!((data[0] == 0 && data[1] == 1 && data[2] == 0 && data[3] == 0) ||
  90. (data[0] == 't' && data[1] == 'r' && data[2] == 'u' &&
  91. data[3] == 'e')))
  92. return -1;
  93. head = find_table(data, len, "head", &head_len);
  94. maxp = find_table(data, len, "maxp", &maxp_len);
  95. hhea = find_table(data, len, "hhea", &hhea_len);
  96. hmtx = find_table(data, len, "hmtx", &hmtx_len);
  97. loca = find_table(data, len, "loca", &loca_len);
  98. glyf = find_table(data, len, "glyf", &glyf_len);
  99. cmap = find_table(data, len, "cmap", &cmap_len);
  100. if (!head || !maxp || !hhea || !hmtx || !loca || !glyf || !cmap)
  101. return -1;
  102. if (head_len < 54 || maxp_len < 6 || hhea_len < 36)
  103. return -1;
  104. if (be32(head + 12) != 0x5F0F3CF5u) /* head.magicNumber */
  105. return -1;
  106. f->data = data;
  107. f->len = len;
  108. f->upem = be16(head + 18);
  109. f->loca_short = sbe16(head + 50) == 0;
  110. f->num_glyphs = be16(maxp + 4);
  111. f->ascent = sbe16(hhea + 4);
  112. f->descent = sbe16(hhea + 6);
  113. f->line_gap = sbe16(hhea + 8);
  114. f->num_hmetrics = be16(hhea + 34);
  115. if (f->upem < 16 || f->upem > 16384 || f->num_glyphs < 1 ||
  116. f->num_glyphs > 65535)
  117. return -1;
  118. if (f->num_hmetrics < 1 || f->num_hmetrics > f->num_glyphs)
  119. return -1;
  120. if (loca_len < (f->loca_short ? (unsigned int)f->num_glyphs * 2u + 2u
  121. : (unsigned int)f->num_glyphs * 4u + 4u))
  122. return -1;
  123. if (hmtx_len < (unsigned int)f->num_hmetrics * 4u)
  124. return -1;
  125. /* Pick a format 4 Unicode cmap. */
  126. if (cmap_len < 4)
  127. return -1;
  128. nrec = be16(cmap + 2);
  129. if (nrec > 64 || 4u + nrec * 8u > cmap_len)
  130. return -1;
  131. for (i = 0; i < nrec; i++) {
  132. const unsigned char *r = cmap + 4 + i * 8;
  133. unsigned int off = be32(r + 4);
  134. unsigned int plat = be16(r);
  135. unsigned int enc = be16(r + 2);
  136. if (off + 2 > cmap_len)
  137. continue;
  138. if (be16(cmap + off) != 4)
  139. continue;
  140. if (plat == 3 && (enc == 0 || enc == 1)) {
  141. cmap4_off = off;
  142. break;
  143. }
  144. if (plat == 0 && enc == 3 && !cmap4_off)
  145. cmap4_off = off;
  146. }
  147. if (!cmap4_off || cmap4_off + 16 > cmap_len)
  148. return -1;
  149. if (be16(cmap + cmap4_off + 2) > cmap_len - cmap4_off)
  150. return -1;
  151. f->loca = loca;
  152. f->glyf = glyf;
  153. f->glyf_len = glyf_len;
  154. f->hmtx = hmtx;
  155. f->cmap4 = cmap + cmap4_off;
  156. f->cmap4_len = be16(cmap + cmap4_off + 2);
  157. return 0;
  158. }
  159. int ufont_ttf_glyph_index(const struct ufont_face *f, unsigned int cp)
  160. {
  161. const unsigned char *c = f->cmap4;
  162. unsigned int seg, i, end, start, delta, range;
  163. if (cp > 0xFFFFu || !c || f->cmap4_len < 16)
  164. return 0;
  165. seg = be16(c + 6) / 2u;
  166. if (seg == 0 || 16u + seg * 8u > f->cmap4_len)
  167. return 0;
  168. for (i = 0; i < seg; i++) {
  169. end = be16(c + 14 + i * 2);
  170. if (cp > end)
  171. continue;
  172. start = be16(c + 16 + seg * 2 + i * 2);
  173. if (cp < start)
  174. return 0;
  175. delta = be16(c + 16 + seg * 4 + i * 2);
  176. range = be16(c + 16 + seg * 6 + i * 2);
  177. if (range == 0)
  178. return (int)((cp + delta) & 0xFFFFu);
  179. {
  180. unsigned int ro = 16u + seg * 6u + i * 2u;
  181. unsigned int addr = ro + range + (cp - start) * 2u;
  182. unsigned int g;
  183. if (addr + 2u > f->cmap4_len)
  184. return 0;
  185. g = be16(c + addr);
  186. if (g)
  187. g = (g + delta) & 0xFFFFu;
  188. return (int)g;
  189. }
  190. }
  191. return 0;
  192. }
  193. static const unsigned char *glyph_data(const struct ufont_face *f, int glyph,
  194. unsigned int *size)
  195. {
  196. unsigned int a, b;
  197. if (glyph < 0 || glyph >= f->num_glyphs)
  198. return 0;
  199. if (f->loca_short) {
  200. a = be16(f->loca + glyph * 2u) * 2u;
  201. b = be16(f->loca + glyph * 2u + 2u) * 2u;
  202. } else {
  203. a = be32(f->loca + glyph * 4u);
  204. b = be32(f->loca + glyph * 4u + 4u);
  205. }
  206. if (b <= a || b > f->glyf_len)
  207. return 0;
  208. if (size)
  209. *size = b - a;
  210. return f->glyf + a;
  211. }
  212. int ufont_ttf_advance(const struct ufont_face *f, int glyph, int px)
  213. {
  214. unsigned int units;
  215. if (glyph < 0 || glyph >= f->num_glyphs)
  216. glyph = 0;
  217. if (glyph >= f->num_hmetrics)
  218. glyph = f->num_hmetrics - 1;
  219. units = be16(f->hmtx + glyph * 4u);
  220. return (int)((units * (unsigned int)px * 64u + (unsigned int)f->upem / 2u)
  221. / (unsigned int)f->upem);
  222. }
  223. static int load_simple(const struct ufont_face *f, const unsigned char *g,
  224. unsigned int glen, struct ttf_outline *out)
  225. {
  226. int ncontours = sbe16(g);
  227. (void)f;
  228. int i, j, npoints, flags, repeats;
  229. unsigned int off;
  230. unsigned char flag;
  231. int x, y;
  232. if (ncontours < 0 || ncontours > UFONT_MAX_CONTOURS)
  233. return -1;
  234. if (glen < 12u + (unsigned int)ncontours * 2u)
  235. return -1;
  236. out->ncontours = ncontours;
  237. for (i = 0; i < ncontours; i++)
  238. out->ends[i] = be16(g + 10 + i * 2);
  239. npoints = ncontours ? (int)out->ends[ncontours - 1] + 1 : 0;
  240. if (npoints < 0 || npoints > UFONT_MAX_POINTS)
  241. return -1;
  242. off = 10u + (unsigned int)ncontours * 2u;
  243. if (off + 2u > glen)
  244. return -1;
  245. off += 2u + be16(g + off); /* skip instructions */
  246. if (off > glen)
  247. return -1;
  248. /* Flags, with the repeat bit expanded. */
  249. {
  250. unsigned char flags_buf[UFONT_MAX_POINTS];
  251. int n = 0;
  252. while (n < npoints) {
  253. if (off >= glen)
  254. return -1;
  255. flag = g[off++];
  256. flags_buf[n++] = flag;
  257. if (flag & 0x08) {
  258. if (off >= glen)
  259. return -1;
  260. repeats = g[off++];
  261. while (repeats-- > 0 && n < npoints)
  262. flags_buf[n++] = flag;
  263. }
  264. }
  265. for (i = 0; i < npoints; i++) {
  266. flags = flags_buf[i];
  267. out->points[i].on = (flags & 0x01) != 0;
  268. out->points[i].x = 0;
  269. out->points[i].y = 0;
  270. }
  271. /* X deltas. */
  272. x = 0;
  273. for (i = 0; i < npoints; i++) {
  274. flags = flags_buf[i];
  275. if (flags & 0x02) {
  276. if (off >= glen)
  277. return -1;
  278. j = g[off++];
  279. x += (flags & 0x10) ? j : -j;
  280. } else if (!(flags & 0x10)) {
  281. if (off + 2u > glen)
  282. return -1;
  283. x += sbe16(g + off);
  284. off += 2;
  285. }
  286. out->points[i].x = x;
  287. }
  288. /* Y deltas. */
  289. y = 0;
  290. for (i = 0; i < npoints; i++) {
  291. flags = flags_buf[i];
  292. if (flags & 0x04) {
  293. if (off >= glen)
  294. return -1;
  295. j = g[off++];
  296. y += (flags & 0x20) ? j : -j;
  297. } else if (!(flags & 0x20)) {
  298. if (off + 2u > glen)
  299. return -1;
  300. y += sbe16(g + off);
  301. off += 2;
  302. }
  303. out->points[i].y = y;
  304. }
  305. }
  306. out->npoints = npoints;
  307. return 0;
  308. }
  309. static int load_glyph(const struct ufont_face *f, int glyph,
  310. struct ttf_outline *out, int depth);
  311. static int load_composite(const struct ufont_face *f, const unsigned char *g,
  312. unsigned int glen, struct ttf_outline *out, int depth)
  313. {
  314. unsigned int off = 10;
  315. int any = 0;
  316. out->npoints = 0;
  317. out->ncontours = 0;
  318. if (depth >= 4)
  319. return -1;
  320. for (;;) {
  321. unsigned int flags, comp, args[2];
  322. int dx, dy, i, a, b, c, d;
  323. struct ttf_outline *co;
  324. if (off + 4u > glen)
  325. return -1;
  326. flags = be16(g + off);
  327. comp = be16(g + off + 2);
  328. off += 4;
  329. if (flags & 0x0001) {
  330. if (off + 4u > glen)
  331. return -1;
  332. args[0] = (unsigned int)sbe16(g + off);
  333. args[1] = (unsigned int)sbe16(g + off + 2);
  334. off += 4;
  335. } else {
  336. if (off + 2u > glen)
  337. return -1;
  338. args[0] = (unsigned int)(signed char)g[off];
  339. args[1] = (unsigned int)(signed char)g[off + 1];
  340. off += 2;
  341. }
  342. a = 0x10000; b = 0; c = 0; d = 0x10000;
  343. if (flags & 0x0008) {
  344. if (off + 2u > glen)
  345. return -1;
  346. a = d = sbe16(g + off) * 4; /* F2Dot14 -> 16.16 */
  347. off += 2;
  348. } else if (flags & 0x0040) {
  349. if (off + 4u > glen)
  350. return -1;
  351. a = sbe16(g + off) * 4;
  352. d = sbe16(g + off + 2) * 4;
  353. off += 4;
  354. } else if (flags & 0x0080) {
  355. if (off + 8u > glen)
  356. return -1;
  357. a = sbe16(g + off) * 4;
  358. b = sbe16(g + off + 2) * 4;
  359. c = sbe16(g + off + 4) * 4;
  360. d = sbe16(g + off + 6) * 4;
  361. off += 8;
  362. }
  363. if (comp >= (unsigned int)f->num_glyphs ||
  364. depth + 1 >= 4)
  365. return -1;
  366. co = &comp_outline[depth + 1];
  367. if (load_glyph(f, (int)comp, co, depth + 1) != 0)
  368. return -1;
  369. /* Apply the component transform in font units. */
  370. for (i = 0; i < co->npoints; i++) {
  371. int x = co->points[i].x;
  372. int y = co->points[i].y;
  373. co->points[i].x = (a * x + c * y) >> 16;
  374. co->points[i].y = (b * x + d * y) >> 16;
  375. }
  376. dx = 0;
  377. dy = 0;
  378. if (flags & 0x0002) {
  379. dx = (int)args[0];
  380. dy = (int)args[1];
  381. if ((flags & 0x0800) && !(flags & 0x1000)) {
  382. int tx = (a * dx + c * dy) >> 16;
  383. int ty = (b * dx + d * dy) >> 16;
  384. dx = tx;
  385. dy = ty;
  386. }
  387. } else {
  388. int p1 = (int)args[0];
  389. int p2 = (int)args[1];
  390. if (p1 >= 0 && p1 < out->npoints &&
  391. p2 >= 0 && p2 < co->npoints) {
  392. dx = out->points[p1].x - co->points[p2].x;
  393. dy = out->points[p1].y - co->points[p2].y;
  394. }
  395. }
  396. if (out->npoints + co->npoints > UFONT_MAX_POINTS ||
  397. out->ncontours + co->ncontours > UFONT_MAX_CONTOURS)
  398. return -1;
  399. for (i = 0; i < co->npoints; i++) {
  400. out->points[out->npoints + i] = co->points[i];
  401. out->points[out->npoints + i].x += dx;
  402. out->points[out->npoints + i].y += dy;
  403. }
  404. for (i = 0; i < co->ncontours; i++)
  405. out->ends[out->ncontours + i] =
  406. (unsigned short)(co->ends[i] + out->npoints);
  407. out->npoints += co->npoints;
  408. out->ncontours += co->ncontours;
  409. any = 1;
  410. if (!(flags & 0x0020))
  411. break;
  412. }
  413. return any ? 0 : -1;
  414. }
  415. static int load_glyph(const struct ufont_face *f, int glyph,
  416. struct ttf_outline *out, int depth)
  417. {
  418. const unsigned char *g;
  419. unsigned int glen;
  420. int ncontours;
  421. g = glyph_data(f, glyph, &glen);
  422. if (!g)
  423. return -1;
  424. if (glen < 10)
  425. return -1;
  426. ncontours = sbe16(g);
  427. if (ncontours >= 0)
  428. return load_simple(f, g, glen, out);
  429. return load_composite(f, g, glen, out, depth);
  430. }
  431. static int scale_coord(const struct ufont_face *f, int v, int px)
  432. {
  433. return (int)(((long)v * (long)px * 64L +
  434. (long)f->upem / 2L) / (long)f->upem);
  435. }
  436. static int floor_div64(int v)
  437. {
  438. return v >= 0 ? v / 64 : -((-v + 63) / 64);
  439. }
  440. static int ceil_div64(int v)
  441. {
  442. return v >= 0 ? (v + 63) / 64 : -((-v) / 64);
  443. }
  444. int ufont_ttf_bbox(const struct ufont_face *f, int glyph, int px,
  445. int *xoff, int *yoff, int *w, int *h)
  446. {
  447. static struct ttf_outline out;
  448. int i, minx, miny, maxx, maxy, x0, y0, x1, y1;
  449. *xoff = 0;
  450. *yoff = 0;
  451. *w = 0;
  452. *h = 0;
  453. if (load_glyph(f, glyph, &out, 0) != 0 || out.npoints == 0)
  454. return 0;
  455. minx = maxx = scale_coord(f, out.points[0].x, px);
  456. miny = maxy = -scale_coord(f, out.points[0].y, px);
  457. for (i = 1; i < out.npoints; i++) {
  458. int x = scale_coord(f, out.points[i].x, px);
  459. int y = -scale_coord(f, out.points[i].y, px);
  460. if (x < minx) minx = x;
  461. if (x > maxx) maxx = x;
  462. if (y < miny) miny = y;
  463. if (y > maxy) maxy = y;
  464. }
  465. x0 = floor_div64(minx);
  466. y0 = floor_div64(miny);
  467. x1 = ceil_div64(maxx);
  468. y1 = ceil_div64(maxy);
  469. if (x1 <= x0) x1 = x0 + 1;
  470. if (y1 <= y0) y1 = y0 + 1;
  471. *xoff = x0;
  472. *yoff = y0;
  473. *w = x1 - x0;
  474. *h = y1 - y0;
  475. if (*w > 512 || *h > 512) {
  476. *w = *h = 0;
  477. return 0;
  478. }
  479. return 0;
  480. }
  481. static void add_edge(int x0, int y0, int x1, int y1)
  482. {
  483. signed char dir;
  484. if (y0 == y1 || n_edges >= TTF_MAX_EDGES)
  485. return;
  486. dir = 1;
  487. if (y0 > y1) {
  488. int t;
  489. t = x0; x0 = x1; x1 = t;
  490. t = y0; y0 = y1; y1 = t;
  491. dir = -1;
  492. }
  493. edges[n_edges].x0 = x0;
  494. edges[n_edges].y0 = y0;
  495. edges[n_edges].x1 = x1;
  496. edges[n_edges].y1 = y1;
  497. edges[n_edges].dir = dir;
  498. n_edges++;
  499. }
  500. static void flatten_quad(int x0, int y0, int cx, int cy, int x1, int y1,
  501. int depth)
  502. {
  503. long dx = (long)x1 - x0;
  504. long dy = (long)y1 - y0;
  505. long cross = dx * ((long)cy - y0) - dy * ((long)cx - x0);
  506. long lim = (dx < 0 ? -dx : dx) + (dy < 0 ? -dy : dy);
  507. if (lim < 1)
  508. lim = 1;
  509. if (cross < 0)
  510. cross = -cross;
  511. if (depth >= 8 || cross <= lim * 2L) {
  512. add_edge(x0, y0, x1, y1);
  513. return;
  514. }
  515. {
  516. int q0x = (x0 + cx) / 2, q0y = (y0 + cy) / 2;
  517. int q1x = (cx + x1) / 2, q1y = (cy + y1) / 2;
  518. int mx = (q0x + q1x) / 2, my = (q0y + q1y) / 2;
  519. flatten_quad(x0, y0, q0x, q0y, mx, my, depth + 1);
  520. flatten_quad(mx, my, q1x, q1y, x1, y1, depth + 1);
  521. }
  522. }
  523. static void build_edges(const struct ufont_face *f, int px,
  524. const struct ttf_outline *out)
  525. {
  526. int c, start = 0;
  527. n_edges = 0;
  528. for (c = 0; c < out->ncontours; c++) {
  529. int end = out->ends[c];
  530. int count = end - start + 1;
  531. int first_on = -1, i, n = 0;
  532. if (count <= 1) {
  533. start = end + 1;
  534. continue;
  535. }
  536. for (i = 0; i < count; i++)
  537. if (out->points[start + i].on) {
  538. first_on = i;
  539. break;
  540. }
  541. if (first_on < 0) {
  542. /* All off-curve: insert a midpoint between every pair
  543. * and start at the midpoint before the first point. */
  544. for (i = 0; i < count; i++) {
  545. int a = start + i;
  546. int b = start + ((i + 1) % count);
  547. if (n < (int)(sizeof(seq_points) /
  548. sizeof(seq_points[0]))) {
  549. seq_points[n].x =
  550. (out->points[a].x +
  551. out->points[b].x) / 2;
  552. seq_points[n].y =
  553. (out->points[a].y +
  554. out->points[b].y) / 2;
  555. seq_points[n].on = 1;
  556. n++;
  557. }
  558. if (n < (int)(sizeof(seq_points) /
  559. sizeof(seq_points[0]))) {
  560. seq_points[n] = (struct ttf_seq_point){
  561. out->points[b].x,
  562. out->points[b].y,
  563. out->points[b].on
  564. };
  565. n++;
  566. }
  567. }
  568. } else {
  569. /* Rotate to the first on-curve point and insert
  570. * midpoints between consecutive off-curve points. */
  571. for (i = 0; i < count; i++) {
  572. int idx = start + (first_on + i) % count;
  573. struct ttf_seq_point p;
  574. p.x = out->points[idx].x;
  575. p.y = out->points[idx].y;
  576. p.on = out->points[idx].on;
  577. if (n > 0 && !seq_points[n - 1].on && !p.on) {
  578. seq_points[n].x =
  579. (seq_points[n - 1].x + p.x) / 2;
  580. seq_points[n].y =
  581. (seq_points[n - 1].y + p.y) / 2;
  582. seq_points[n].on = 1;
  583. n++;
  584. }
  585. seq_points[n++] = p;
  586. if (n >= (int)(sizeof(seq_points) /
  587. sizeof(seq_points[0])))
  588. break;
  589. }
  590. }
  591. for (i = 0; i < n; i++) {
  592. struct ttf_seq_point a = seq_points[i];
  593. struct ttf_seq_point b = seq_points[(i + 1) % n];
  594. int ax = scale_coord(f, a.x, px);
  595. int ay = -scale_coord(f, a.y, px);
  596. int bx = scale_coord(f, b.x, px);
  597. int by = -scale_coord(f, b.y, px);
  598. if (b.on) {
  599. add_edge(ax, ay, bx, by);
  600. } else {
  601. struct ttf_seq_point d =
  602. seq_points[(i + 2) % n];
  603. int cx = bx, cy = by;
  604. int dx = scale_coord(f, d.x, px);
  605. int dy = -scale_coord(f, d.y, px);
  606. flatten_quad(ax, ay, cx, cy, dx, dy, 0);
  607. i++; /* the next point is consumed */
  608. }
  609. }
  610. start = end + 1;
  611. }
  612. }
  613. /* Add horizontal coverage of [xa, xb] (26.6, relative to xoff) to mask. */
  614. static void add_span(unsigned char *mask, int w, int h, int row,
  615. int xa, int xb)
  616. {
  617. int pa, pb, p;
  618. (void)h;
  619. if (xa < 0)
  620. xa = 0;
  621. if (xb > w * 64)
  622. xb = w * 64;
  623. if (xb <= xa)
  624. return;
  625. pa = xa / 64;
  626. pb = (xb + 63) / 64;
  627. if (pa < 0)
  628. pa = 0;
  629. if (pb > w)
  630. pb = w;
  631. for (p = pa; p < pb; p++) {
  632. int l = p * 64;
  633. int r = (p + 1) * 64;
  634. int cov;
  635. int v;
  636. if (l < xa) l = xa;
  637. if (r > xb) r = xb;
  638. cov = r - l;
  639. if (cov <= 0)
  640. continue;
  641. v = mask[row * w + p] + cov;
  642. if (v > 255)
  643. v = 255;
  644. mask[row * w + p] = (unsigned char)v;
  645. }
  646. }
  647. int ufont_ttf_raster(const struct ufont_face *f, int glyph, int px,
  648. int xoff, int yoff, int w, int h, unsigned char *mask)
  649. {
  650. static struct ttf_outline out;
  651. int row, sub;
  652. if (w <= 0 || h <= 0 || !mask)
  653. return 0;
  654. if (load_glyph(f, glyph, &out, 0) != 0 || out.npoints == 0)
  655. return 0;
  656. build_edges(f, px, &out);
  657. if (n_edges == 0)
  658. return 0;
  659. for (row = 0; row < h; row++) {
  660. for (sub = 0; sub < 4; sub++) {
  661. int sy = ((yoff + row) * 64) + sub * 16 + 8;
  662. static int xs[TTF_MAX_EDGES];
  663. static signed char dirs[TTF_MAX_EDGES];
  664. int n = 0, i, j, wind = 0, span = 0, span_x = 0;
  665. for (i = 0; i < n_edges; i++) {
  666. const struct ttf_edge *e = &edges[i];
  667. long dy, num, x;
  668. if (sy < e->y0 || sy >= e->y1)
  669. continue;
  670. dy = (long)e->y1 - e->y0;
  671. if (dy == 0)
  672. continue;
  673. num = (long)(sy - e->y0) *
  674. ((long)e->x1 - e->x0);
  675. x = e->x0 + num / dy;
  676. if (n < TTF_MAX_EDGES) {
  677. xs[n] = (int)x;
  678. dirs[n] = e->dir;
  679. n++;
  680. }
  681. }
  682. /* Insertion sort by x. */
  683. for (i = 1; i < n; i++) {
  684. int xv = xs[i];
  685. signed char dv = dirs[i];
  686. j = i - 1;
  687. while (j >= 0 && xs[j] > xv) {
  688. xs[j + 1] = xs[j];
  689. dirs[j + 1] = dirs[j];
  690. j--;
  691. }
  692. xs[j + 1] = xv;
  693. dirs[j + 1] = dv;
  694. }
  695. wind = 0;
  696. span = 0;
  697. span_x = 0;
  698. for (i = 0; i < n; i++) {
  699. if (wind == 0) {
  700. span_x = xs[i];
  701. span = 1;
  702. }
  703. wind += dirs[i];
  704. if (wind == 0 && span) {
  705. int xa = span_x - (xoff * 64);
  706. int xb = xs[i] - (xoff * 64);
  707. if (xb > xa)
  708. add_span(mask, w, h, row,
  709. xa, xb);
  710. span = 0;
  711. }
  712. }
  713. }
  714. }
  715. return 0;
  716. }