Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.4 KiB

  1. From ba724004c6a368e452114f7dc147a9978fe0f3b4 Mon Sep 17 00:00:00 2001
  2. From: Kirill Bugaev <kirill.bugaev87@gmail.com>
  3. Date: Tue, 16 Apr 2019 04:31:30 +0800
  4. Subject: [PATCH] This patch allows to add spare font besides default. Some
  5. glyphs can be not present in default font. For this glyphs st uses
  6. font-config and try to find them in font cache first. This patch append fonts
  7. defined in font2 variable to the beginning of font cache. So they will be
  8. used first for glyphs that absent in default font.
  9. ---
  10. config.def.h | 6 +++
  11. x.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++
  12. 2 files changed, 107 insertions(+)
  13. diff --git a/config.def.h b/config.def.h
  14. index 482901e..676719e 100644
  15. --- a/config.def.h
  16. +++ b/config.def.h
  17. @@ -6,6 +6,12 @@
  18. * font: see http://freedesktop.org/software/fontconfig/fontconfig-user.html
  19. */
  20. static char *font = "Liberation Mono:pixelsize=12:antialias=true:autohint=true";
  21. +/* Spare fonts */
  22. +static char *font2[] = {
  23. +/* "Inconsolata for Powerline:pixelsize=12:antialias=true:autohint=true", */
  24. +/* "Hack Nerd Font Mono:pixelsize=11:antialias=true:autohint=true", */
  25. +};
  26. +
  27. static int borderpx = 2;
  28. /*
  29. diff --git a/x.c b/x.c
  30. index 5828a3b..d37e59d 100644
  31. --- a/x.c
  32. +++ b/x.c
  33. @@ -149,6 +149,8 @@ static void xhints(void);
  34. static int xloadcolor(int, const char *, Color *);
  35. static int xloadfont(Font *, FcPattern *);
  36. static void xloadfonts(char *, double);
  37. +static int xloadsparefont(FcPattern *, int);
  38. +static void xloadsparefonts(void);
  39. static void xunloadfont(Font *);
  40. static void xunloadfonts(void);
  41. static void xsetenv(void);
  42. @@ -296,6 +298,7 @@ zoomabs(const Arg *arg)
  43. {
  44. xunloadfonts();
  45. xloadfonts(usedfont, arg->f);
  46. + xloadsparefonts();
  47. cresize(0, 0);
  48. redraw();
  49. xhints();
  50. @@ -977,6 +980,101 @@ xloadfonts(char *fontstr, double fontsize)
  51. FcPatternDestroy(pattern);
  52. }
  53. +int
  54. +xloadsparefont(FcPattern *pattern, int flags)
  55. +{
  56. + FcPattern *match;
  57. + FcResult result;
  58. +
  59. + match = FcFontMatch(NULL, pattern, &result);
  60. + if (!match) {
  61. + return 1;
  62. + }
  63. +
  64. + if (!(frc[frclen].font = XftFontOpenPattern(xw.dpy, match))) {
  65. + FcPatternDestroy(match);
  66. + return 1;
  67. + }
  68. +
  69. + frc[frclen].flags = flags;
  70. + /* Believe U+0000 glyph will present in each default font */
  71. + frc[frclen].unicodep = 0;
  72. + frclen++;
  73. +
  74. + return 0;
  75. +}
  76. +
  77. +void
  78. +xloadsparefonts(void)
  79. +{
  80. + FcPattern *pattern;
  81. + double sizeshift, fontval;
  82. + int fc;
  83. + char **fp;
  84. +
  85. + if (frclen != 0)
  86. + die("can't embed spare fonts. cache isn't empty");
  87. +
  88. + /* Calculate count of spare fonts */
  89. + fc = sizeof(font2) / sizeof(*font2);
  90. + if (fc == 0)
  91. + return;
  92. +
  93. + /* Allocate memory for cache entries. */
  94. + if (frccap < 4 * fc) {
  95. + frccap += 4 * fc - frccap;
  96. + frc = xrealloc(frc, frccap * sizeof(Fontcache));
  97. + }
  98. +
  99. + for (fp = font2; fp - font2 < fc; ++fp) {
  100. +
  101. + if (**fp == '-')
  102. + pattern = XftXlfdParse(*fp, False, False);
  103. + else
  104. + pattern = FcNameParse((FcChar8 *)*fp);
  105. +
  106. + if (!pattern)
  107. + die("can't open spare font %s\n", *fp);
  108. +
  109. + if (defaultfontsize > 0) {
  110. + sizeshift = usedfontsize - defaultfontsize;
  111. + if (sizeshift != 0 &&
  112. + FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
  113. + FcResultMatch) {
  114. + fontval += sizeshift;
  115. + FcPatternDel(pattern, FC_PIXEL_SIZE);
  116. + FcPatternDel(pattern, FC_SIZE);
  117. + FcPatternAddDouble(pattern, FC_PIXEL_SIZE, fontval);
  118. + }
  119. + }
  120. +
  121. + FcPatternAddBool(pattern, FC_SCALABLE, 1);
  122. +
  123. + FcConfigSubstitute(NULL, pattern, FcMatchPattern);
  124. + XftDefaultSubstitute(xw.dpy, xw.scr, pattern);
  125. +
  126. + if (xloadsparefont(pattern, FRC_NORMAL))
  127. + die("can't open spare font %s\n", *fp);
  128. +
  129. + FcPatternDel(pattern, FC_SLANT);
  130. + FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  131. + if (xloadsparefont(pattern, FRC_ITALIC))
  132. + die("can't open spare font %s\n", *fp);
  133. +
  134. + FcPatternDel(pattern, FC_WEIGHT);
  135. + FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  136. + if (xloadsparefont(pattern, FRC_ITALICBOLD))
  137. + die("can't open spare font %s\n", *fp);
  138. +
  139. + FcPatternDel(pattern, FC_SLANT);
  140. + FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
  141. + if (xloadsparefont(pattern, FRC_BOLD))
  142. + die("can't open spare font %s\n", *fp);
  143. +
  144. + FcPatternDestroy(pattern);
  145. + }
  146. +}
  147. +
  148. void
  149. xunloadfont(Font *f)
  150. {
  151. @@ -1057,6 +1155,9 @@ xinit(int cols, int rows)
  152. usedfont = (opt_font == NULL)? font : opt_font;
  153. xloadfonts(usedfont, 0);
  154. + /* spare fonts */
  155. + xloadsparefonts();
  156. +
  157. /* colors */
  158. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  159. xloadcols();
  160. --
  161. 2.21.0