dtext.c 5.4 KB

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