dtext.c 6.9 KB

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