sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

st-font2-20190416-ba72400.diff (4495B)


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