dtext.c 6.3 KB

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