dtext.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* See LICENSE file for copyright and license details. */
  2. #include <errno.h>
  3. #include <stdint.h>
  4. #include <ft2build.h>
  5. #include FT_FREETYPE_H
  6. #include FT_ADVANCES_H
  7. #include <X11/Xlib.h>
  8. #include <X11/extensions/Xrender.h>
  9. #include "dtext.h"
  10. #define max(a, b) ((a) > (b) ? (a) : (b))
  11. #define min(a, b) ((a) < (b) ? (a) : (b))
  12. static dt_error load_face(dt_context *ctx, FT_Face *face, char const *name);
  13. static dt_error load_char(dt_context *ctx, dt_font *fnt, wchar_t c);
  14. static dt_pair hash_unavailable = { 0 };
  15. static dt_pair const *hash_get(dt_row map[DT_HASH_SIZE], wchar_t key);
  16. static dt_error hash_set(dt_row map[DT_HASH_SIZE], dt_pair val);
  17. dt_error
  18. dt_init(dt_context **res, Display *dpy, Window win)
  19. {
  20. dt_error err;
  21. dt_context *ctx;
  22. Visual *visual;
  23. Pixmap pix;
  24. XRenderPictureAttributes attrs;
  25. if (!(ctx = malloc(sizeof(*ctx))))
  26. return -ENOMEM;
  27. if ((err = FT_Init_FreeType(&ctx->ft_lib))) {
  28. free(ctx);
  29. return err;
  30. }
  31. visual = XDefaultVisual(dpy, XDefaultScreen(dpy));
  32. ctx->win_format = XRenderFindVisualFormat(dpy, visual);
  33. XFree(visual);
  34. ctx->argb32_format = XRenderFindStandardFormat(dpy, PictStandardARGB32);
  35. ctx->dpy = dpy;
  36. ctx->pic = XRenderCreatePicture(dpy, win, ctx->win_format, 0, &attrs);
  37. pix = XCreatePixmap(dpy, DefaultRootWindow(dpy), 1, 1, 32);
  38. attrs.repeat = 1;
  39. ctx->fill = XRenderCreatePicture(ctx->dpy, pix, ctx->argb32_format,
  40. CPRepeat, &attrs);
  41. XFreePixmap(dpy, pix);
  42. *res = ctx;
  43. return 0;
  44. }
  45. void
  46. dt_quit(dt_context *ctx)
  47. {
  48. XRenderFreePicture(ctx->dpy, ctx->fill);
  49. XRenderFreePicture(ctx->dpy, ctx->pic);
  50. XFree(ctx->argb32_format);
  51. XFree(ctx->win_format);
  52. FT_Done_FreeType(ctx->ft_lib);
  53. free(ctx);
  54. }
  55. dt_error
  56. dt_load(dt_context *ctx, dt_font **res, char const *name)
  57. {
  58. dt_error err;
  59. dt_font *fnt;
  60. size_t i;
  61. size_t len;
  62. char *face;
  63. int16_t descent;
  64. if (!(fnt = malloc(sizeof(*fnt))))
  65. return -ENOMEM;
  66. fnt->num_faces = 1;
  67. for (i = 0; name[i]; ++i)
  68. fnt->num_faces += (name[i] == ';');
  69. if (!(fnt->faces = malloc(fnt->num_faces * sizeof(fnt->faces[0])))) {
  70. free(fnt);
  71. return -ENOMEM;
  72. }
  73. for (i = 0; i < fnt->num_faces; ++i) {
  74. len = strchr(name, ';') - name;
  75. if (!(face = strndup(name, len)))
  76. return -ENOMEM;
  77. if ((err = load_face(ctx, &fnt->faces[i], face))) {
  78. free(face);
  79. while (--i != (size_t) -1)
  80. FT_Done_Face(fnt->faces[i]);
  81. free(fnt->faces);
  82. free(fnt);
  83. return err;
  84. }
  85. free(face);
  86. name += len + 1;
  87. }
  88. fnt->gs = XRenderCreateGlyphSet(ctx->dpy, ctx->argb32_format);
  89. memset(fnt->advance, 0, sizeof(fnt->advance));
  90. fnt->ascent = descent = 0;
  91. for (i = 0; i < fnt->num_faces; ++i) {
  92. fnt->ascent = max(fnt->ascent, fnt->faces[i]->ascender >> 6);
  93. descent = min(descent, fnt->faces[i]->descender >> 6);
  94. }
  95. fnt->height = fnt->ascent - descent;
  96. *res = fnt;
  97. return 0;
  98. }
  99. void
  100. dt_free(dt_context *ctx, dt_font *fnt)
  101. {
  102. size_t i;
  103. XRenderFreeGlyphSet(ctx->dpy, fnt->gs);
  104. for (i = 0; i < fnt->num_faces; ++i)
  105. FT_Done_Face(fnt->faces[i]);
  106. free(fnt);
  107. }
  108. dt_error
  109. dt_box(dt_context *ctx, dt_font *fnt, dt_bbox *bbox,
  110. wchar_t const *txt, size_t len)
  111. {
  112. dt_error err;
  113. size_t i;
  114. dt_pair const *p;
  115. memset(bbox, 0, sizeof(*bbox));
  116. for (i = 0; i < len; ++i) {
  117. if ((err = load_char(ctx, fnt, txt[i])))
  118. return err;
  119. p = hash_get(fnt->advance, txt[i]);
  120. bbox->w += p->adv;
  121. bbox->h = max(p->h, bbox->h + max(0, bbox->y - p->asc));
  122. bbox->y = min(p->asc, bbox->y);
  123. }
  124. return 0;
  125. }
  126. dt_error
  127. dt_draw(dt_context *ctx, dt_font *fnt, dt_color const *color,
  128. uint32_t x, uint32_t y, wchar_t const *txt, size_t len)
  129. {
  130. dt_error err;
  131. XRenderColor col;
  132. size_t i;
  133. col.red = (color->red << 8) + color->red;
  134. col.green = (color->green << 8) + color->green;
  135. col.blue = (color->blue << 8) + color->blue;
  136. col.alpha = (color->alpha << 8) + color->alpha;
  137. XRenderFillRectangle(ctx->dpy, PictOpSrc, ctx->fill, &col, 0, 0, 1, 1);
  138. for (i = 0; i < len; ++i)
  139. if ((err = load_char(ctx, fnt, txt[i])))
  140. return err;
  141. #define DO_COMPOSITE(Size, Type) \
  142. XRenderCompositeString ## Size(ctx->dpy, PictOpOver, ctx->fill, \
  143. ctx->pic, ctx->argb32_format, \
  144. fnt->gs, 0, 0, x, y, \
  145. (Type const *) txt, len)
  146. if (sizeof(wchar_t) == 1)
  147. DO_COMPOSITE(8, char);
  148. else if (sizeof(wchar_t) == 2)
  149. DO_COMPOSITE(16, uint16_t);
  150. else
  151. DO_COMPOSITE(32, uint32_t);
  152. #undef DO_COMPOSITE
  153. return 0;
  154. }
  155. static dt_error
  156. load_face(dt_context *ctx, FT_Face *face, char const *name) {
  157. dt_error err;
  158. char *file;
  159. char *colon;
  160. size_t size;
  161. if (!(colon = strchr(name, ':')))
  162. return -EINVAL;
  163. if (!(file = strndup(name, colon - name)))
  164. return -ENOMEM;
  165. err = FT_New_Face(ctx->ft_lib, file, 0, face);
  166. free(file);
  167. if (err)
  168. return err;
  169. name = colon + 1;
  170. size = strtoul(name, 0, 10);
  171. if ((err = FT_Set_Char_Size(*face, size << 6, 0, 0, 0))) {
  172. FT_Done_Face(*face);
  173. return err;
  174. }
  175. return 0;
  176. }
  177. static dt_error
  178. load_char(dt_context *ctx, dt_font *fnt, wchar_t c)
  179. {
  180. dt_error err;
  181. FT_UInt code;
  182. FT_GlyphSlot slot;
  183. Glyph gid;
  184. XGlyphInfo g;
  185. char *img;
  186. size_t x, y, i;
  187. if (hash_get(fnt->advance, c) != &hash_unavailable)
  188. return 0;
  189. slot = 0;
  190. for (i = 0; i < fnt->num_faces; ++i) {
  191. code = FT_Get_Char_Index(fnt->faces[i], c);
  192. if (!code)
  193. continue;
  194. if ((err = FT_Load_Glyph(fnt->faces[i], code, FT_LOAD_RENDER)))
  195. continue;
  196. slot = fnt->faces[i]->glyph;
  197. break;
  198. }
  199. if (!slot) {
  200. if ((err = FT_Load_Char(fnt->faces[0], c, FT_LOAD_RENDER)))
  201. return err;
  202. slot = fnt->faces[0]->glyph;
  203. }
  204. gid = c;
  205. g.width = slot->bitmap.width;
  206. g.height = slot->bitmap.rows;
  207. g.x = - slot->bitmap_left;
  208. g.y = slot->bitmap_top;
  209. g.xOff = slot->advance.x >> 6;
  210. g.yOff = slot->advance.y >> 6;
  211. if (!(img = malloc(4 * g.width * g.height)))
  212. return -ENOMEM;
  213. for (y = 0; y < g.height; ++y)
  214. for (x = 0; x < g.width; ++x)
  215. for (i = 0; i < 4; ++i)
  216. img[4 * (y * g.width + x) + i] =
  217. slot->bitmap.buffer[y * g.width + x];
  218. XRenderAddGlyphs(ctx->dpy, fnt->gs, &gid, &g, 1,
  219. img, 4 * g.width * g.height);
  220. free(img);
  221. return hash_set(fnt->advance, (dt_pair) {
  222. .c = c,
  223. .adv = slot->advance.x >> 6,
  224. .asc = - slot->metrics.horiBearingY >> 6,
  225. .h = slot->metrics.height >> 6
  226. });
  227. }
  228. static dt_pair const *
  229. hash_get(dt_row map[DT_HASH_SIZE], wchar_t key)
  230. {
  231. dt_row row;
  232. size_t i;
  233. row = map[key % DT_HASH_SIZE];
  234. for (i = 0; i < row.len; ++i)
  235. if (row.data[i].c == key)
  236. return &row.data[i];
  237. return &hash_unavailable;
  238. }
  239. static dt_error
  240. hash_set(dt_row map[DT_HASH_SIZE], dt_pair val)
  241. {
  242. dt_row row;
  243. dt_pair *d;
  244. size_t i;
  245. row = map[val.c % DT_HASH_SIZE];
  246. for (i = 0; i < row.len; ++i) {
  247. if (row.data[i].c == val.c) {
  248. row.data[i] = val;
  249. return 0;
  250. }
  251. }
  252. if (row.allocated == row.len) {
  253. d = row.data;
  254. if (!(d = realloc(d, (2 * row.len + 1) * sizeof(d[0]))))
  255. return -ENOMEM;
  256. row.data = d;
  257. row.allocated = 2 * row.len + 1;
  258. }
  259. ++row.len;
  260. row.data[row.len - 1] = val;
  261. map[val.c % DT_HASH_SIZE] = row;
  262. return 0;
  263. }