dtext.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 uint8_t hash_get(dt_row map[DT_HASH_SIZE], wchar_t key, uint8_t def);
  13. static dt_error hash_set(dt_row map[DT_HASH_SIZE], wchar_t key, uint8_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->loaded, 0, DT_HASH_SIZE * sizeof(fnt->loaded[0]));
  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_font *fnt, dt_bbox *bbox, wchar_t const *txt)
  87. {
  88. dt_error err;
  89. size_t len;
  90. size_t i;
  91. FT_Fixed adv;
  92. if (!(len = wcslen(txt)))
  93. return -EINVAL;
  94. memset(bbox, 0, sizeof(*bbox));
  95. bbox->x = 0;
  96. bbox->y = - (fnt->face->ascender >> 6);
  97. for (i = 0; i < len; ++i) {
  98. if ((err = FT_Get_Advance(fnt->face, txt[i], 0, &adv)))
  99. return err;
  100. bbox->w += adv >> 16;
  101. }
  102. bbox->h = fnt->face->height >> 6;
  103. return 0;
  104. }
  105. dt_error
  106. dt_draw(dt_context *ctx, dt_font *fnt, dt_style const *style,
  107. uint32_t x, uint32_t y, wchar_t const *txt)
  108. {
  109. dt_error err;
  110. XRenderColor col;
  111. size_t len;
  112. size_t i;
  113. col.red = (style->red << 8) + style->red;
  114. col.green = (style->green << 8) + style->green;
  115. col.blue = (style->blue << 8) + style->blue;
  116. col.alpha = 0xFFFF - ((style->alpha << 8) + style->alpha);
  117. XRenderFillRectangle(ctx->dpy, PictOpSrc, ctx->fill, &col, 0, 0, 1, 1);
  118. len = wcslen(txt);
  119. for (i = 0; i < len; ++i)
  120. if ((err = load_char(ctx, fnt, txt[i])))
  121. return err;
  122. #define DO_COMPOSITE(Size, Type) \
  123. XRenderCompositeString ## Size(ctx->dpy, PictOpOver, ctx->fill, \
  124. ctx->pic, ctx->argb32_format, \
  125. fnt->gs, 0, 0, x, y, \
  126. (Type const *) txt, len)
  127. if (sizeof(wchar_t) == 1)
  128. DO_COMPOSITE(8, char);
  129. else if (sizeof(wchar_t) == 2)
  130. DO_COMPOSITE(16, uint16_t);
  131. else
  132. DO_COMPOSITE(32, uint32_t);
  133. #undef DO_COMPOSITE
  134. return 0;
  135. }
  136. static dt_error
  137. load_char(dt_context *ctx, dt_font *fnt, wchar_t c)
  138. {
  139. dt_error err;
  140. FT_GlyphSlot slot;
  141. Glyph gid;
  142. XGlyphInfo g;
  143. char *img;
  144. size_t x, y, i;
  145. if (hash_get(fnt->loaded, c, 0))
  146. return 0;
  147. if ((err = FT_Load_Char(fnt->face, c, FT_LOAD_RENDER)))
  148. return err;
  149. slot = fnt->face->glyph;
  150. gid = c;
  151. g.width = slot->bitmap.width;
  152. g.height = slot->bitmap.rows;
  153. g.x = - slot->bitmap_left;
  154. g.y = slot->bitmap_top;
  155. g.xOff = slot->advance.x >> 6;
  156. g.yOff = slot->advance.y >> 6;
  157. if (!(img = malloc(4 * g.width * g.height)))
  158. return -ENOMEM;
  159. for (y = 0; y < g.height; ++y)
  160. for (x = 0; x < g.width; ++x)
  161. for (i = 0; i < 4; ++i)
  162. img[4 * (y * g.width + x) + i] =
  163. slot->bitmap.buffer[y * g.width + x];
  164. XRenderAddGlyphs(ctx->dpy, fnt->gs, &gid, &g, 1,
  165. img, 4 * g.width * g.height);
  166. free(img);
  167. return hash_set(fnt->loaded, c, 1);
  168. }
  169. static uint8_t
  170. hash_get(dt_row map[DT_HASH_SIZE], wchar_t key, uint8_t def)
  171. {
  172. dt_row row;
  173. size_t i;
  174. row = map[key % DT_HASH_SIZE];
  175. for (i = 0; i < row.len; ++i)
  176. if (row.data[i].k == key)
  177. return row.data[i].v;
  178. return def;
  179. }
  180. static dt_error
  181. hash_set(dt_row map[DT_HASH_SIZE], wchar_t key, uint8_t val)
  182. {
  183. dt_row row;
  184. dt_pair *d;
  185. size_t i;
  186. row = map[key % DT_HASH_SIZE];
  187. for (i = 0; i < row.len; ++i) {
  188. if (row.data[i].k == key) {
  189. row.data[i].v = val;
  190. return 0;
  191. }
  192. }
  193. if (row.allocated == row.len) {
  194. d = row.data;
  195. if (!(d = realloc(d, (2 * row.len + 1) * sizeof(d[0]))))
  196. return -ENOMEM;
  197. row.data = d;
  198. row.allocated = 2 * row.len + 1;
  199. }
  200. ++row.len;
  201. row.data[row.len - 1].k = key;
  202. row.data[row.len - 1].v = val;
  203. map[key % DT_HASH_SIZE] = row;
  204. return 0;
  205. }