Skip to content

Commit

Permalink
Fix fallback fonts' handling and building with -flto=auto (Fixes: #159
Browse files Browse the repository at this point in the history
, 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)
  • Loading branch information
actionless authored Nov 18, 2024
1 parent f3842ec commit 1d2f295
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/make.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
- name: make
run: make
- name: make install
run: sudo env PREFIX=/usr/ make install
run: sudo make PREFIX=/usr/ install
- name: test deps
run: sudo apt-get install -y xvfb
- name: smoke test build
run: xvfb-run ./xst bash -c exit
- name: smoke test install
run: xvfb-run xst bash -c exit
run: sync ; sleep 0.1 ; xvfb-run xst bash -c exit
35 changes: 30 additions & 5 deletions x.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ static void ttysend(const Arg *);
#include "config.h"

/* Calculate count of spare fonts */
int fonts_count;
static int fonts_count = 0;

// Declare fallback_fonts as a dynamic array
static char **fallback_fonts = NULL;

/* XEMBED messages */
#define XEMBED_FOCUS_IN 4
Expand Down Expand Up @@ -174,6 +177,7 @@ static int xloadcolor(int, const char *, Color *);
static int xloadfont(Font *, FcPattern *);
static void xloadfonts(const char *, double);
static int xloadsparefont(FcPattern *, int);
static void xinitsparefonts(void);
static void xloadsparefonts(void);
static void xunloadfont(Font *);
static void xunloadfonts(void);
Expand Down Expand Up @@ -333,9 +337,7 @@ zoomabs(const Arg *arg)
{
xunloadfonts();
xloadfonts(getusedfont(), arg->f);
fonts_count--;
xloadsparefonts();
fonts_count++;
cresize(0, 0);
redraw();
xhints();
Expand Down Expand Up @@ -1149,6 +1151,28 @@ xloadsparefont(FcPattern *pattern, int flags)
return 0;
}

void
xinitsparefonts(void)
{
// Ignore if spare fonts already loaded:
if (fonts_count) return;

// Allocate memory for initial fonts
int initial_count = sizeof(font2) / sizeof(font2[0]);
fallback_fonts = malloc((initial_count + 1) * sizeof(char *));
if (!fallback_fonts) {
die("Error initializing fallback fonts!\n");
}

// Copy the initial fonts to fallback_fonts
for (int i = 0; i < initial_count; i++) {
fallback_fonts[i] = font2[i];
}

fonts_count = initial_count;
fallback_fonts[fonts_count] = NULL; // Null-terminate the array
}

void
xloadsparefonts(void)
{
Expand All @@ -1168,8 +1192,8 @@ xloadsparefonts(void)
frc = xrealloc(frc, frccap * sizeof(Fontcache));
}

for (fp = font2; fp - font2 < fonts_count; ++fp) {
for (fp = fallback_fonts; fp < fallback_fonts + fonts_count && *fp != NULL; ++fp) {

if (**fp == '-')
pattern = XftXlfdParse(*fp, False, False);
else
Expand Down Expand Up @@ -1330,6 +1354,7 @@ xinit(int cols, int rows)
xloadfonts(getusedfont(), 0);

/* spare fonts */
xinitsparefonts();
xloadsparefonts();

/* colors */
Expand Down
46 changes: 34 additions & 12 deletions xst.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,38 @@ xrdb_load(void)
}

XRESOURCE_LOAD_META("font_fallback") {
int count = 0, endchar = fonts_count = sizeof(font2) / sizeof(*font2);
for (int i = 0; ret.addr[i]; i++) if (ret.addr[i] == ',') count++;
if (count > 0)
{
for (int i = 0; i <= count; i++)
{
if (i == 0) font2[endchar + i] = strtok(ret.addr, ",");
else font2[endchar + i] = strtok(NULL, ",");
fonts_count++;
int count = 0;
for (int i = 0; ret.addr[i]; i++) {
if (ret.addr[i] == ',') count++;
}

if (count > 0) {
// Reallocate fallback_fonts to fit additional fonts from Xresources
fallback_fonts = realloc(fallback_fonts, (fonts_count + count + 2) * sizeof(char *));
if (!fallback_fonts) {
printf("ERROR: can't load fonts from 'st.font_fallback' !\n");
return;
}

for (int i = 0; i <= count; i++) {
if (i == 0)
fallback_fonts[fonts_count + i] = strtok(ret.addr, ",");
else
fallback_fonts[fonts_count + i] = strtok(NULL, ",");
printf(" :: XRDB: adding fallback font: %s \n", fallback_fonts[fonts_count + i]);
}
font2[endchar + count + 1] = '\0';

fonts_count += count + 1;
fallback_fonts[fonts_count] = NULL; // Null-terminate
} else if (ret.addr) {
font2[endchar] = ret.addr;
fonts_count++;
fallback_fonts = realloc(fallback_fonts, (fonts_count + 2) * sizeof(char *));
if (!fallback_fonts) {
printf("ERROR: can't load fonts from 'st.font_fallback' !\n");
return;
}

fallback_fonts[fonts_count++] = ret.addr;
fallback_fonts[fonts_count] = NULL; // Null-terminate
}
}

Expand Down Expand Up @@ -139,18 +157,22 @@ reload(int sig)
if (sig == -1) {
return;
}
printf(" :: XST:: reloading config...\n");

xrdb_load();

/* colors, fonts */
xloadcols();
xunloadfonts();
xloadfonts(getusedfont(), 0);
xinitsparefonts();
xloadsparefonts();
xsetcursor(cursorshape);

/* pretend the window just got resized */
cresize(win.w, win.h);
redraw();
xhints();
/* triggers re-render if we're visible. */
ttywrite("\033[O", 3, 1);
}
Expand Down

0 comments on commit 1d2f295

Please sign in to comment.