Skip to content

Commit

Permalink
Merge branch 'font2-patch'
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Hall committed Apr 9, 2021
2 parents 761a9f4 + 0454eb6 commit 1ee90d3
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
* font: see http://freedesktop.org/software/fontconfig/fontconfig-user.html
*/
static char *font = "DejaVu Sans Mono:pixelsize=12:antialias=true:autohint=true";
/* Spare fonts */
static char *font2[] = {
"Noto Color Emoji:pixelsize=12:antialias=true:autohint=true"
};

static int borderpx = 2;

/*
Expand Down
167 changes: 167 additions & 0 deletions patches/st-font2-20190416-ba72400.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
From ba724004c6a368e452114f7dc147a9978fe0f3b4 Mon Sep 17 00:00:00 2001
From: Kirill Bugaev <[email protected]>
Date: Tue, 16 Apr 2019 04:31:30 +0800
Subject: [PATCH] This patch allows to add spare font besides default. Some
glyphs can be not present in default font. For this glyphs st uses
font-config and try to find them in font cache first. This patch append fonts
defined in font2 variable to the beginning of font cache. So they will be
used first for glyphs that absent in default font.

---
config.def.h | 6 +++
x.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 107 insertions(+)

diff --git a/config.def.h b/config.def.h
index 482901e..676719e 100644
--- a/config.def.h
+++ b/config.def.h
@@ -6,6 +6,12 @@
* font: see http://freedesktop.org/software/fontconfig/fontconfig-user.html
*/
static char *font = "Liberation Mono:pixelsize=12:antialias=true:autohint=true";
+/* Spare fonts */
+static char *font2[] = {
+/* "Inconsolata for Powerline:pixelsize=12:antialias=true:autohint=true", */
+/* "Hack Nerd Font Mono:pixelsize=11:antialias=true:autohint=true", */
+};
+
static int borderpx = 2;

/*
diff --git a/x.c b/x.c
index 5828a3b..d37e59d 100644
--- a/x.c
+++ b/x.c
@@ -149,6 +149,8 @@ static void xhints(void);
static int xloadcolor(int, const char *, Color *);
static int xloadfont(Font *, FcPattern *);
static void xloadfonts(char *, double);
+static int xloadsparefont(FcPattern *, int);
+static void xloadsparefonts(void);
static void xunloadfont(Font *);
static void xunloadfonts(void);
static void xsetenv(void);
@@ -296,6 +298,7 @@ zoomabs(const Arg *arg)
{
xunloadfonts();
xloadfonts(usedfont, arg->f);
+ xloadsparefonts();
cresize(0, 0);
redraw();
xhints();
@@ -977,6 +980,101 @@ xloadfonts(char *fontstr, double fontsize)
FcPatternDestroy(pattern);
}

+int
+xloadsparefont(FcPattern *pattern, int flags)
+{
+ FcPattern *match;
+ FcResult result;
+
+ match = FcFontMatch(NULL, pattern, &result);
+ if (!match) {
+ return 1;
+ }
+
+ if (!(frc[frclen].font = XftFontOpenPattern(xw.dpy, match))) {
+ FcPatternDestroy(match);
+ return 1;
+ }
+
+ frc[frclen].flags = flags;
+ /* Believe U+0000 glyph will present in each default font */
+ frc[frclen].unicodep = 0;
+ frclen++;
+
+ return 0;
+}
+
+void
+xloadsparefonts(void)
+{
+ FcPattern *pattern;
+ double sizeshift, fontval;
+ int fc;
+ char **fp;
+
+ if (frclen != 0)
+ die("can't embed spare fonts. cache isn't empty");
+
+ /* Calculate count of spare fonts */
+ fc = sizeof(font2) / sizeof(*font2);
+ if (fc == 0)
+ return;
+
+ /* Allocate memory for cache entries. */
+ if (frccap < 4 * fc) {
+ frccap += 4 * fc - frccap;
+ frc = xrealloc(frc, frccap * sizeof(Fontcache));
+ }
+
+ for (fp = font2; fp - font2 < fc; ++fp) {
+
+ if (**fp == '-')
+ pattern = XftXlfdParse(*fp, False, False);
+ else
+ pattern = FcNameParse((FcChar8 *)*fp);
+
+ if (!pattern)
+ die("can't open spare font %s\n", *fp);
+
+ if (defaultfontsize > 0) {
+ sizeshift = usedfontsize - defaultfontsize;
+ if (sizeshift != 0 &&
+ FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
+ FcResultMatch) {
+ fontval += sizeshift;
+ FcPatternDel(pattern, FC_PIXEL_SIZE);
+ FcPatternDel(pattern, FC_SIZE);
+ FcPatternAddDouble(pattern, FC_PIXEL_SIZE, fontval);
+ }
+ }
+
+ FcPatternAddBool(pattern, FC_SCALABLE, 1);
+
+ FcConfigSubstitute(NULL, pattern, FcMatchPattern);
+ XftDefaultSubstitute(xw.dpy, xw.scr, pattern);
+
+ if (xloadsparefont(pattern, FRC_NORMAL))
+ die("can't open spare font %s\n", *fp);
+
+ FcPatternDel(pattern, FC_SLANT);
+ FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
+ if (xloadsparefont(pattern, FRC_ITALIC))
+ die("can't open spare font %s\n", *fp);
+
+ FcPatternDel(pattern, FC_WEIGHT);
+ FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
+ if (xloadsparefont(pattern, FRC_ITALICBOLD))
+ die("can't open spare font %s\n", *fp);
+
+ FcPatternDel(pattern, FC_SLANT);
+ FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
+ if (xloadsparefont(pattern, FRC_BOLD))
+ die("can't open spare font %s\n", *fp);
+
+ FcPatternDestroy(pattern);
+ }
+}
+
void
xunloadfont(Font *f)
{
@@ -1057,6 +1155,9 @@ xinit(int cols, int rows)
usedfont = (opt_font == NULL)? font : opt_font;
xloadfonts(usedfont, 0);

+ /* spare fonts */
+ xloadsparefonts();
+
/* colors */
xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
xloadcols();
--
2.21.0

101 changes: 101 additions & 0 deletions x.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ static void xhints(void);
static int xloadcolor(int, const char *, Color *);
static int xloadfont(Font *, FcPattern *);
static void xloadfonts(char *, double);
static int xloadsparefont(FcPattern *, int);
static void xloadsparefonts(void);
static void xunloadfont(Font *);
static void xunloadfonts(void);
static void xsetenv(void);
Expand Down Expand Up @@ -305,6 +307,7 @@ zoomabs(const Arg *arg)
{
xunloadfonts();
xloadfonts(usedfont, arg->f);
xloadsparefonts();
cresize(0, 0);
redraw();
xhints();
Expand Down Expand Up @@ -1027,6 +1030,101 @@ xloadfonts(char *fontstr, double fontsize)
FcPatternDestroy(pattern);
}

int
xloadsparefont(FcPattern *pattern, int flags)
{
FcPattern *match;
FcResult result;

match = FcFontMatch(NULL, pattern, &result);
if (!match) {
return 1;
}

if (!(frc[frclen].font = XftFontOpenPattern(xw.dpy, match))) {
FcPatternDestroy(match);
return 1;
}

frc[frclen].flags = flags;
/* Believe U+0000 glyph will present in each default font */
frc[frclen].unicodep = 0;
frclen++;

return 0;
}

void
xloadsparefonts(void)
{
FcPattern *pattern;
double sizeshift, fontval;
int fc;
char **fp;

if (frclen != 0)
die("can't embed spare fonts. cache isn't empty");

/* Calculate count of spare fonts */
fc = sizeof(font2) / sizeof(*font2);
if (fc == 0)
return;

/* Allocate memory for cache entries. */
if (frccap < 4 * fc) {
frccap += 4 * fc - frccap;
frc = xrealloc(frc, frccap * sizeof(Fontcache));
}

for (fp = font2; fp - font2 < fc; ++fp) {

if (**fp == '-')
pattern = XftXlfdParse(*fp, False, False);
else
pattern = FcNameParse((FcChar8 *)*fp);

if (!pattern)
die("can't open spare font %s\n", *fp);

if (defaultfontsize > 0) {
sizeshift = usedfontsize - defaultfontsize;
if (sizeshift != 0 &&
FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
FcResultMatch) {
fontval += sizeshift;
FcPatternDel(pattern, FC_PIXEL_SIZE);
FcPatternDel(pattern, FC_SIZE);
FcPatternAddDouble(pattern, FC_PIXEL_SIZE, fontval);
}
}

FcPatternAddBool(pattern, FC_SCALABLE, 1);

FcConfigSubstitute(NULL, pattern, FcMatchPattern);
XftDefaultSubstitute(xw.dpy, xw.scr, pattern);

if (xloadsparefont(pattern, FRC_NORMAL))
die("can't open spare font %s\n", *fp);

FcPatternDel(pattern, FC_SLANT);
FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
if (xloadsparefont(pattern, FRC_ITALIC))
die("can't open spare font %s\n", *fp);

FcPatternDel(pattern, FC_WEIGHT);
FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
if (xloadsparefont(pattern, FRC_ITALICBOLD))
die("can't open spare font %s\n", *fp);

FcPatternDel(pattern, FC_SLANT);
FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
if (xloadsparefont(pattern, FRC_BOLD))
die("can't open spare font %s\n", *fp);

FcPatternDestroy(pattern);
}
}

void
xunloadfont(Font *f)
{
Expand Down Expand Up @@ -1124,6 +1222,9 @@ xinit(int cols, int rows)
usedfont = (opt_font == NULL)? font : opt_font;
xloadfonts(usedfont, 0);

/* spare fonts */
xloadsparefonts();

/* colors */
xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
xloadcols();
Expand Down

0 comments on commit 1ee90d3

Please sign in to comment.