Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fallback fonts' handling and building with -flto=auto (Fixes: #159, Fixes: #144, Fixes: #165) #164

Merged
merged 10 commits into from
Nov 18, 2024
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
Copy link
Collaborator Author

@actionless actionless Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(for some reason this step is starting to execute sometimes before the file actually created in the filesystem)

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
42 changes: 30 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
Loading