dtext.c 6.5 KB

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