ufont.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* ufont: face registry, glyph cache and text metrics on top of the
  2. * fixed-point TrueType backend in ufont_ttf.c. */
  3. #include "ufont.h"
  4. #include "ufont_internal.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. struct ufont_cache_ent {
  8. const struct ufont_face *face;
  9. unsigned int cp;
  10. unsigned char px;
  11. unsigned char used;
  12. struct ufont_glyph g;
  13. };
  14. static unsigned char *arena;
  15. static unsigned int arena_cap;
  16. static unsigned int arena_req = UFONT_ARENA_BYTES;
  17. static unsigned int arena_used;
  18. static unsigned int glyph_base;
  19. static struct ufont_face *faces[UFONT_MAX_FACES];
  20. static int nfaces;
  21. static struct ufont_cache_ent cache[UFONT_CACHE_SLOTS];
  22. void *ufont_arena_alloc(unsigned int bytes, unsigned int align)
  23. {
  24. unsigned int p;
  25. if (!arena || align == 0)
  26. return 0;
  27. p = (arena_used + align - 1u) & ~(align - 1u);
  28. if (p > arena_cap || bytes > arena_cap - p)
  29. return 0;
  30. arena_used = p + bytes;
  31. return arena + p;
  32. }
  33. void ufont_arena_mark(unsigned int mark)
  34. {
  35. if (mark <= arena_cap)
  36. arena_used = mark;
  37. }
  38. unsigned int ufont_arena_used(void)
  39. {
  40. return arena_used;
  41. }
  42. static void cache_clear(void)
  43. {
  44. unsigned int i;
  45. for (i = 0; i < UFONT_CACHE_SLOTS; i++)
  46. cache[i].used = 0;
  47. if (glyph_base)
  48. arena_used = glyph_base;
  49. }
  50. void ufont_set_arena_bytes(unsigned int bytes)
  51. {
  52. arena_req = bytes ? bytes : UFONT_ARENA_BYTES;
  53. }
  54. int ufont_arena_init(void)
  55. {
  56. if (arena)
  57. return 0;
  58. arena = (unsigned char *)malloc(arena_req);
  59. if (!arena)
  60. return -1;
  61. arena_cap = arena_req;
  62. arena_used = 0;
  63. glyph_base = 0;
  64. nfaces = 0;
  65. memset(faces, 0, sizeof(faces));
  66. memset(cache, 0, sizeof(cache));
  67. return 0;
  68. }
  69. void ufont_reset(void)
  70. {
  71. if (arena)
  72. free(arena);
  73. arena = 0;
  74. arena_cap = 0;
  75. arena_used = 0;
  76. glyph_base = 0;
  77. nfaces = 0;
  78. memset(faces, 0, sizeof(faces));
  79. memset(cache, 0, sizeof(cache));
  80. }
  81. struct ufont_face *ufont_face_open_mem(const void *data, unsigned int len)
  82. {
  83. struct ufont_face *f;
  84. unsigned char *copy;
  85. unsigned int mark;
  86. if (!data || len == 0 || len > UFONT_MAX_FILE)
  87. return 0;
  88. if (ufont_arena_init() != 0)
  89. return 0;
  90. if (nfaces >= UFONT_MAX_FACES)
  91. return 0;
  92. if (glyph_base)
  93. cache_clear();
  94. mark = arena_used;
  95. copy = (unsigned char *)ufont_arena_alloc(len, 4);
  96. if (!copy) {
  97. ufont_arena_mark(mark);
  98. return 0;
  99. }
  100. memcpy(copy, data, len);
  101. f = (struct ufont_face *)ufont_arena_alloc(sizeof(*f), 8);
  102. if (!f) {
  103. ufont_arena_mark(mark);
  104. return 0;
  105. }
  106. memset(f, 0, sizeof(*f));
  107. f->id = (unsigned int)nfaces;
  108. if (ufont_ttf_load(f, copy, len) != 0) {
  109. ufont_arena_mark(mark);
  110. return 0;
  111. }
  112. faces[nfaces++] = f;
  113. glyph_base = arena_used;
  114. return f;
  115. }
  116. void ufont_metrics(const struct ufont_face *f, int px, struct ufont_metrics *m)
  117. {
  118. int asc, desc, gap;
  119. if (!m)
  120. return;
  121. memset(m, 0, sizeof(*m));
  122. if (!f || px < 4)
  123. return;
  124. asc = (int)(((long)f->ascent * px + f->upem / 2) / f->upem);
  125. desc = (int)(((long)-f->descent * px + f->upem / 2) / f->upem);
  126. gap = (int)(((long)f->line_gap * px + f->upem / 2) / f->upem);
  127. if (asc < 1)
  128. asc = px - 2;
  129. if (desc < 1)
  130. desc = 2;
  131. if (gap < 0)
  132. gap = 0;
  133. m->ascent = asc;
  134. m->descent = desc;
  135. m->line_gap = gap;
  136. m->line_height = asc + desc + gap;
  137. }
  138. static unsigned int next_cp(const char *s, int *i, int n, int utf8)
  139. {
  140. unsigned int c;
  141. int p = *i;
  142. if (!utf8) {
  143. c = (unsigned char)s[p];
  144. *i = p + 1;
  145. return c;
  146. }
  147. c = (unsigned char)s[p++];
  148. if (c < 0x80) {
  149. *i = p;
  150. return c;
  151. }
  152. if ((c & 0xE0) == 0xC0 && (n < 0 || p < n)) {
  153. unsigned int c2 = (unsigned char)s[p];
  154. if ((c2 & 0xC0) == 0x80) {
  155. *i = p + 1;
  156. return ((c & 0x1F) << 6) | (c2 & 0x3F);
  157. }
  158. } else if ((c & 0xF0) == 0xE0 && (n < 0 || p + 1 < n)) {
  159. unsigned int c2 = (unsigned char)s[p];
  160. unsigned int c3 = (unsigned char)s[p + 1];
  161. if ((c2 & 0xC0) == 0x80 && (c3 & 0xC0) == 0x80) {
  162. *i = p + 2;
  163. return ((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) |
  164. (c3 & 0x3F);
  165. }
  166. } else if ((c & 0xF8) == 0xF0 && (n < 0 || p + 2 < n)) {
  167. unsigned int c2 = (unsigned char)s[p];
  168. unsigned int c3 = (unsigned char)s[p + 1];
  169. unsigned int c4 = (unsigned char)s[p + 2];
  170. if ((c2 & 0xC0) == 0x80 && (c3 & 0xC0) == 0x80 &&
  171. (c4 & 0xC0) == 0x80) {
  172. *i = p + 3;
  173. return ((c & 0x07) << 18) | ((c2 & 0x3F) << 12) |
  174. ((c3 & 0x3F) << 6) | (c4 & 0x3F);
  175. }
  176. }
  177. *i = p;
  178. return 0xFFFD;
  179. }
  180. int ufont_text_width(const struct ufont_face *f, int px,
  181. const char *s, int n, int utf8)
  182. {
  183. int i = 0, total = 0;
  184. if (!f || !s || px < 4)
  185. return 0;
  186. if (n < 0) {
  187. n = 0;
  188. while (s[n])
  189. n++;
  190. }
  191. while (i < n) {
  192. unsigned int cp = next_cp(s, &i, n, utf8);
  193. int glyph = ufont_ttf_glyph_index(f, cp);
  194. total += ufont_ttf_advance(f, glyph, px);
  195. }
  196. return (total + 32) >> 6;
  197. }
  198. int ufont_has_glyph(const struct ufont_face *f, unsigned int cp)
  199. {
  200. return f && ufont_ttf_glyph_index(f, cp) != 0;
  201. }
  202. int ufont_advance_cp(const struct ufont_face *f, int px, unsigned int cp)
  203. {
  204. if (!f)
  205. return 0;
  206. return ufont_ttf_advance(f, ufont_ttf_glyph_index(f, cp), px);
  207. }
  208. unsigned int ufont_utf8_next(const char *s, int *i, int n)
  209. {
  210. if (!s || !i)
  211. return 0xFFFD;
  212. return next_cp(s, i, n, 1);
  213. }
  214. static struct ufont_cache_ent *cache_lookup(const struct ufont_face *f,
  215. int px, unsigned int cp)
  216. {
  217. unsigned int start, i;
  218. start = (cp * 2654435761u) ^ ((unsigned int)px << 16) ^
  219. (f->id * 0x9E3779B1u);
  220. start &= UFONT_CACHE_SLOTS - 1u;
  221. for (i = 0; i < UFONT_CACHE_SLOTS; i++) {
  222. struct ufont_cache_ent *e =
  223. &cache[(start + i) & (UFONT_CACHE_SLOTS - 1u)];
  224. if (!e->used)
  225. return 0;
  226. if (e->face == f && e->px == (unsigned char)px && e->cp == cp)
  227. return e;
  228. }
  229. return 0;
  230. }
  231. static struct ufont_cache_ent *cache_free_slot(const struct ufont_face *f,
  232. int px, unsigned int cp)
  233. {
  234. unsigned int start, i;
  235. start = (cp * 2654435761u) ^ ((unsigned int)px << 16) ^
  236. (f->id * 0x9E3779B1u);
  237. start &= UFONT_CACHE_SLOTS - 1u;
  238. for (i = 0; i < UFONT_CACHE_SLOTS; i++) {
  239. struct ufont_cache_ent *e =
  240. &cache[(start + i) & (UFONT_CACHE_SLOTS - 1u)];
  241. if (!e->used) {
  242. e->used = 1;
  243. e->face = f;
  244. e->px = (unsigned char)px;
  245. e->cp = cp;
  246. memset(&e->g, 0, sizeof(e->g));
  247. return e;
  248. }
  249. }
  250. return 0;
  251. }
  252. const struct ufont_glyph *ufont_glyph(const struct ufont_face *f, int px,
  253. unsigned int cp)
  254. {
  255. struct ufont_cache_ent *e;
  256. int glyph, advance, xoff, yoff, w, h;
  257. unsigned char *mask;
  258. int attempt;
  259. if (!f || px < 4)
  260. return 0;
  261. if (px > 96)
  262. px = 96;
  263. e = cache_lookup(f, px, cp);
  264. if (e)
  265. return &e->g;
  266. for (attempt = 0; attempt < 2; attempt++) {
  267. e = cache_free_slot(f, px, cp);
  268. if (!e) {
  269. cache_clear();
  270. continue;
  271. }
  272. glyph = ufont_ttf_glyph_index(f, cp);
  273. advance = ufont_ttf_advance(f, glyph, px);
  274. xoff = yoff = w = h = 0;
  275. ufont_ttf_bbox(f, glyph, px, &xoff, &yoff, &w, &h);
  276. e->g.advance = advance;
  277. e->g.xoff = (short)xoff;
  278. e->g.yoff = (short)yoff;
  279. e->g.w = (unsigned short)w;
  280. e->g.h = (unsigned short)h;
  281. if (w <= 0 || h <= 0)
  282. return &e->g;
  283. mask = (unsigned char *)ufont_arena_alloc(
  284. (unsigned int)w * (unsigned int)h, 1);
  285. if (!mask) {
  286. e->used = 0;
  287. cache_clear();
  288. continue;
  289. }
  290. memset(mask, 0, (unsigned int)w * (unsigned int)h);
  291. ufont_ttf_raster(f, glyph, px, xoff, yoff, w, h, mask);
  292. e->g.mask = mask;
  293. return &e->g;
  294. }
  295. return 0;
  296. }