Skip to content

Commit 1d2f295

Browse files
authored
Fix fallback fonts' handling and building with -flto=auto (Fixes: #159, Fixes: #144, Fixes: #165) (#164)
* fix building with -flto=auto * fix: rename back default_fallback_fonts to font2 for backwards-compatibility with local users' config.h * fix(x: zoomabs): fix fallback fonts' handling when zooming (Fixes: #144) * fix(xst: xtdb_load: font_fallback): add missing return statements on font loading error * chore: fix GH CI * fix(xst: reload): init and load spare fonts (aka font2), call xhints after resize (fixes: #165)
1 parent f3842ec commit 1d2f295

File tree

3 files changed

+66
-19
lines changed

3 files changed

+66
-19
lines changed

.github/workflows/make.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ jobs:
1616
- name: make
1717
run: make
1818
- name: make install
19-
run: sudo env PREFIX=/usr/ make install
19+
run: sudo make PREFIX=/usr/ install
2020
- name: test deps
2121
run: sudo apt-get install -y xvfb
2222
- name: smoke test build
2323
run: xvfb-run ./xst bash -c exit
2424
- name: smoke test install
25-
run: xvfb-run xst bash -c exit
25+
run: sync ; sleep 0.1 ; xvfb-run xst bash -c exit

x.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ static void ttysend(const Arg *);
7575
#include "config.h"
7676

7777
/* Calculate count of spare fonts */
78-
int fonts_count;
78+
static int fonts_count = 0;
79+
80+
// Declare fallback_fonts as a dynamic array
81+
static char **fallback_fonts = NULL;
7982

8083
/* XEMBED messages */
8184
#define XEMBED_FOCUS_IN 4
@@ -174,6 +177,7 @@ static int xloadcolor(int, const char *, Color *);
174177
static int xloadfont(Font *, FcPattern *);
175178
static void xloadfonts(const char *, double);
176179
static int xloadsparefont(FcPattern *, int);
180+
static void xinitsparefonts(void);
177181
static void xloadsparefonts(void);
178182
static void xunloadfont(Font *);
179183
static void xunloadfonts(void);
@@ -333,9 +337,7 @@ zoomabs(const Arg *arg)
333337
{
334338
xunloadfonts();
335339
xloadfonts(getusedfont(), arg->f);
336-
fonts_count--;
337340
xloadsparefonts();
338-
fonts_count++;
339341
cresize(0, 0);
340342
redraw();
341343
xhints();
@@ -1149,6 +1151,28 @@ xloadsparefont(FcPattern *pattern, int flags)
11491151
return 0;
11501152
}
11511153

1154+
void
1155+
xinitsparefonts(void)
1156+
{
1157+
// Ignore if spare fonts already loaded:
1158+
if (fonts_count) return;
1159+
1160+
// Allocate memory for initial fonts
1161+
int initial_count = sizeof(font2) / sizeof(font2[0]);
1162+
fallback_fonts = malloc((initial_count + 1) * sizeof(char *));
1163+
if (!fallback_fonts) {
1164+
die("Error initializing fallback fonts!\n");
1165+
}
1166+
1167+
// Copy the initial fonts to fallback_fonts
1168+
for (int i = 0; i < initial_count; i++) {
1169+
fallback_fonts[i] = font2[i];
1170+
}
1171+
1172+
fonts_count = initial_count;
1173+
fallback_fonts[fonts_count] = NULL; // Null-terminate the array
1174+
}
1175+
11521176
void
11531177
xloadsparefonts(void)
11541178
{
@@ -1168,8 +1192,8 @@ xloadsparefonts(void)
11681192
frc = xrealloc(frc, frccap * sizeof(Fontcache));
11691193
}
11701194

1171-
for (fp = font2; fp - font2 < fonts_count; ++fp) {
1172-
1195+
for (fp = fallback_fonts; fp < fallback_fonts + fonts_count && *fp != NULL; ++fp) {
1196+
11731197
if (**fp == '-')
11741198
pattern = XftXlfdParse(*fp, False, False);
11751199
else
@@ -1330,6 +1354,7 @@ xinit(int cols, int rows)
13301354
xloadfonts(getusedfont(), 0);
13311355

13321356
/* spare fonts */
1357+
xinitsparefonts();
13331358
xloadsparefonts();
13341359

13351360
/* colors */

xst.c

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,38 @@ xrdb_load(void)
6363
}
6464

6565
XRESOURCE_LOAD_META("font_fallback") {
66-
int count = 0, endchar = fonts_count = sizeof(font2) / sizeof(*font2);
67-
for (int i = 0; ret.addr[i]; i++) if (ret.addr[i] == ',') count++;
68-
if (count > 0)
69-
{
70-
for (int i = 0; i <= count; i++)
71-
{
72-
if (i == 0) font2[endchar + i] = strtok(ret.addr, ",");
73-
else font2[endchar + i] = strtok(NULL, ",");
74-
fonts_count++;
66+
int count = 0;
67+
for (int i = 0; ret.addr[i]; i++) {
68+
if (ret.addr[i] == ',') count++;
69+
}
70+
71+
if (count > 0) {
72+
// Reallocate fallback_fonts to fit additional fonts from Xresources
73+
fallback_fonts = realloc(fallback_fonts, (fonts_count + count + 2) * sizeof(char *));
74+
if (!fallback_fonts) {
75+
printf("ERROR: can't load fonts from 'st.font_fallback' !\n");
76+
return;
77+
}
78+
79+
for (int i = 0; i <= count; i++) {
80+
if (i == 0)
81+
fallback_fonts[fonts_count + i] = strtok(ret.addr, ",");
82+
else
83+
fallback_fonts[fonts_count + i] = strtok(NULL, ",");
84+
printf(" :: XRDB: adding fallback font: %s \n", fallback_fonts[fonts_count + i]);
7585
}
76-
font2[endchar + count + 1] = '\0';
86+
87+
fonts_count += count + 1;
88+
fallback_fonts[fonts_count] = NULL; // Null-terminate
7789
} else if (ret.addr) {
78-
font2[endchar] = ret.addr;
79-
fonts_count++;
90+
fallback_fonts = realloc(fallback_fonts, (fonts_count + 2) * sizeof(char *));
91+
if (!fallback_fonts) {
92+
printf("ERROR: can't load fonts from 'st.font_fallback' !\n");
93+
return;
94+
}
95+
96+
fallback_fonts[fonts_count++] = ret.addr;
97+
fallback_fonts[fonts_count] = NULL; // Null-terminate
8098
}
8199
}
82100

@@ -139,18 +157,22 @@ reload(int sig)
139157
if (sig == -1) {
140158
return;
141159
}
160+
printf(" :: XST:: reloading config...\n");
142161

143162
xrdb_load();
144163

145164
/* colors, fonts */
146165
xloadcols();
147166
xunloadfonts();
148167
xloadfonts(getusedfont(), 0);
168+
xinitsparefonts();
169+
xloadsparefonts();
149170
xsetcursor(cursorshape);
150171

151172
/* pretend the window just got resized */
152173
cresize(win.w, win.h);
153174
redraw();
175+
xhints();
154176
/* triggers re-render if we're visible. */
155177
ttywrite("\033[O", 3, 1);
156178
}

0 commit comments

Comments
 (0)