hb.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <X11/Xft/Xft.h>
  5. #include <X11/cursorfont.h>
  6. #include <hb.h>
  7. #include <hb-ft.h>
  8. #include "st.h"
  9. #include "hb.h"
  10. #define FEATURE(c1,c2,c3,c4) { .tag = HB_TAG(c1,c2,c3,c4), .value = 1, .start = HB_FEATURE_GLOBAL_START, .end = HB_FEATURE_GLOBAL_END }
  11. hb_font_t *hbfindfont(XftFont *match);
  12. typedef struct {
  13. XftFont *match;
  14. hb_font_t *font;
  15. } HbFontMatch;
  16. static int hbfontslen = 0;
  17. static HbFontMatch *hbfontcache = NULL;
  18. /*
  19. * Poplulate the array with a list of font features, wrapped in FEATURE macro,
  20. * e. g.
  21. * FEATURE('c', 'a', 'l', 't'), FEATURE('d', 'l', 'i', 'g')
  22. */
  23. hb_feature_t features[] = { };
  24. void
  25. hbunloadfonts()
  26. {
  27. for (int i = 0; i < hbfontslen; i++) {
  28. hb_font_destroy(hbfontcache[i].font);
  29. XftUnlockFace(hbfontcache[i].match);
  30. }
  31. if (hbfontcache != NULL) {
  32. free(hbfontcache);
  33. hbfontcache = NULL;
  34. }
  35. hbfontslen = 0;
  36. }
  37. hb_font_t *
  38. hbfindfont(XftFont *match)
  39. {
  40. for (int i = 0; i < hbfontslen; i++) {
  41. if (hbfontcache[i].match == match)
  42. return hbfontcache[i].font;
  43. }
  44. /* Font not found in cache, caching it now. */
  45. hbfontcache = realloc(hbfontcache, sizeof(HbFontMatch) * (hbfontslen + 1));
  46. FT_Face face = XftLockFace(match);
  47. hb_font_t *font = hb_ft_font_create(face, NULL);
  48. if (font == NULL)
  49. die("Failed to load Harfbuzz font.");
  50. hbfontcache[hbfontslen].match = match;
  51. hbfontcache[hbfontslen].font = font;
  52. hbfontslen += 1;
  53. return font;
  54. }
  55. void hbtransform(HbTransformData *data, XftFont *xfont, const Glyph *glyphs, int start, int length) {
  56. Rune rune;
  57. ushort mode = USHRT_MAX;
  58. unsigned int glyph_count;
  59. int i, end = start + length;
  60. hb_font_t *font = hbfindfont(xfont);
  61. if (font == NULL)
  62. return;
  63. hb_buffer_t *buffer = hb_buffer_create();
  64. hb_buffer_set_direction(buffer, HB_DIRECTION_LTR);
  65. /* Fill buffer with codepoints. */
  66. for (i = start; i < end; i++) {
  67. rune = glyphs[i].u;
  68. mode = glyphs[i].mode;
  69. if (mode & ATTR_WDUMMY)
  70. rune = 0x0020;
  71. hb_buffer_add_codepoints(buffer, &rune, 1, 0, 1);
  72. }
  73. /* Shape the segment. */
  74. hb_shape(font, buffer, features, sizeof(features)/sizeof(hb_feature_t));
  75. /* Get new glyph info. */
  76. hb_glyph_info_t *info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
  77. hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(buffer, &glyph_count);
  78. /** Fill the output. */
  79. data->buffer = buffer;
  80. data->glyphs = info;
  81. data->positions = pos;
  82. data->count = glyph_count;
  83. }
  84. void hbcleanup(HbTransformData *data) {
  85. hb_buffer_destroy(data->buffer);
  86. memset(data, 0, sizeof(HbTransformData));
  87. }