hb.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <X11/Xft/Xft.h>
  5. #include <hb.h>
  6. #include <hb-ft.h>
  7. #include "st.h"
  8. void hbtransformsegment(XftFont *xfont, const Glyph *string, hb_codepoint_t *codepoints, int start, int length);
  9. hb_font_t *hbfindfont(XftFont *match);
  10. typedef struct {
  11. XftFont *match;
  12. hb_font_t *font;
  13. } HbFontMatch;
  14. static int hbfontslen = 0;
  15. static HbFontMatch *hbfontcache = NULL;
  16. void
  17. hbunloadfonts()
  18. {
  19. for (int i = 0; i < hbfontslen; i++) {
  20. hb_font_destroy(hbfontcache[i].font);
  21. XftUnlockFace(hbfontcache[i].match);
  22. }
  23. if (hbfontcache != NULL) {
  24. free(hbfontcache);
  25. hbfontcache = NULL;
  26. }
  27. hbfontslen = 0;
  28. }
  29. hb_font_t *
  30. hbfindfont(XftFont *match)
  31. {
  32. for (int i = 0; i < hbfontslen; i++) {
  33. if (hbfontcache[i].match == match)
  34. return hbfontcache[i].font;
  35. }
  36. /* Font not found in cache, caching it now. */
  37. hbfontcache = realloc(hbfontcache, sizeof(HbFontMatch) * (hbfontslen + 1));
  38. FT_Face face = XftLockFace(match);
  39. hb_font_t *font = hb_ft_font_create(face, NULL);
  40. if (font == NULL)
  41. die("Failed to load Harfbuzz font.");
  42. hbfontcache[hbfontslen].match = match;
  43. hbfontcache[hbfontslen].font = font;
  44. hbfontslen += 1;
  45. return font;
  46. }
  47. void
  48. hbtransform(XftGlyphFontSpec *specs, const Glyph *glyphs, size_t len, int x, int y)
  49. {
  50. int start = 0, length = 1, gstart = 0;
  51. hb_codepoint_t *codepoints = calloc(len, sizeof(hb_codepoint_t));
  52. for (int idx = 1, specidx = 1; idx < len; idx++) {
  53. if (glyphs[idx].mode & ATTR_WDUMMY) {
  54. length += 1;
  55. continue;
  56. }
  57. if (specs[specidx].font != specs[start].font || ATTRCMP(glyphs[gstart], glyphs[idx]) || selected(x + idx, y) != selected(x + gstart, y)) {
  58. hbtransformsegment(specs[start].font, glyphs, codepoints, gstart, length);
  59. /* Reset the sequence. */
  60. length = 1;
  61. start = specidx;
  62. gstart = idx;
  63. } else {
  64. length += 1;
  65. }
  66. specidx++;
  67. }
  68. /* EOL. */
  69. hbtransformsegment(specs[start].font, glyphs, codepoints, gstart, length);
  70. /* Apply the transformation to glyph specs. */
  71. for (int i = 0, specidx = 0; i < len; i++) {
  72. if (glyphs[i].mode & ATTR_WDUMMY)
  73. continue;
  74. if (glyphs[i].mode & ATTR_BOXDRAW) {
  75. specidx++;
  76. continue;
  77. }
  78. if (codepoints[i] != specs[specidx].glyph)
  79. ((Glyph *)glyphs)[i].mode |= ATTR_LIGA;
  80. specs[specidx++].glyph = codepoints[i];
  81. }
  82. free(codepoints);
  83. }
  84. void
  85. hbtransformsegment(XftFont *xfont, const Glyph *string, hb_codepoint_t *codepoints, int start, int length)
  86. {
  87. hb_font_t *font = hbfindfont(xfont);
  88. if (font == NULL)
  89. return;
  90. Rune rune;
  91. ushort mode = USHRT_MAX;
  92. hb_buffer_t *buffer = hb_buffer_create();
  93. hb_buffer_set_direction(buffer, HB_DIRECTION_LTR);
  94. /* Fill buffer with codepoints. */
  95. for (int i = start; i < (start+length); i++) {
  96. rune = string[i].u;
  97. mode = string[i].mode;
  98. if (mode & ATTR_WDUMMY)
  99. rune = 0x0020;
  100. hb_buffer_add_codepoints(buffer, &rune, 1, 0, 1);
  101. }
  102. /* Shape the segment. */
  103. hb_shape(font, buffer, NULL, 0);
  104. /* Get new glyph info. */
  105. hb_glyph_info_t *info = hb_buffer_get_glyph_infos(buffer, NULL);
  106. /* Write new codepoints. */
  107. for (int i = 0; i < length; i++) {
  108. hb_codepoint_t gid = info[i].codepoint;
  109. codepoints[start+i] = gid;
  110. }
  111. /* Cleanup. */
  112. hb_buffer_destroy(buffer);
  113. }