ufont.c 6.5 KB

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