feat: add patch defaultfontsize and etc.

This commit is contained in:
doryan 2025-05-03 18:50:43 +04:00
parent c0a453cfa3
commit 6be2a4fcb5
2 changed files with 111 additions and 3 deletions

11
x.c
View File

@ -4,6 +4,7 @@
#include <limits.h> #include <limits.h>
#include <locale.h> #include <locale.h>
#include <signal.h> #include <signal.h>
#include <stdlib.h>
#include <sys/select.h> #include <sys/select.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
@ -1249,7 +1250,8 @@ xinit(int cols, int rows)
die("could not init fontconfig.\n"); die("could not init fontconfig.\n");
usedfont = (opt_font == NULL)? font : opt_font; usedfont = (opt_font == NULL)? font : opt_font;
xloadfonts(usedfont, 0);
xloadfonts(usedfont, defaultfontsize);
/* spare fonts */ /* spare fonts */
xloadsparefonts(); xloadsparefonts();
@ -1750,7 +1752,7 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og, Line line, int le
break; break;
/* FALLTHROUGH */ /* FALLTHROUGH */
case 8: /* steady st cursor */ case 8: /* steady st cursor */
g.u = xsetcursor; g.u = (unsigned int) xsetcursor;
xdrawglyph(g, cx, cy); xdrawglyph(g, cx, cy);
break; break;
} }
@ -2259,6 +2261,11 @@ main(int argc, char *argv[])
case 'v': case 'v':
die("%s " VERSION "\n", argv0); die("%s " VERSION "\n", argv0);
break; break;
case 'z':
defaultfontsize = strtod(EARGF(usage()), NULL);
if (!(defaultfontsize > 0))
usage();
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;

103
x.c.orig
View File

@ -159,6 +159,8 @@ static void xhints(void);
static int xloadcolor(int, const char *, Color *); static int xloadcolor(int, const char *, Color *);
static int xloadfont(Font *, FcPattern *); static int xloadfont(Font *, FcPattern *);
static void xloadfonts(const char *, double); static void xloadfonts(const char *, double);
static int xloadsparefont(FcPattern *, int);
static void xloadsparefonts(void);
static void xunloadfont(Font *); static void xunloadfont(Font *);
static void xunloadfonts(void); static void xunloadfonts(void);
static void xsetenv(void); static void xsetenv(void);
@ -309,6 +311,7 @@ zoomabs(const Arg *arg)
{ {
xunloadfonts(); xunloadfonts();
xloadfonts(usedfont, arg->f); xloadfonts(usedfont, arg->f);
xloadsparefonts();
cresize(0, 0); cresize(0, 0);
redraw(); redraw();
xhints(); xhints();
@ -1053,6 +1056,101 @@ xloadfonts(const char *fontstr, double fontsize)
FcPatternDestroy(pattern); 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 void
xunloadfont(Font *f) xunloadfont(Font *f)
{ {
@ -1153,6 +1251,9 @@ xinit(int cols, int rows)
usedfont = (opt_font == NULL)? font : opt_font; usedfont = (opt_font == NULL)? font : opt_font;
xloadfonts(usedfont, 0); xloadfonts(usedfont, 0);
/* spare fonts */
xloadsparefonts();
/* colors */ /* colors */
xw.cmap = XDefaultColormap(xw.dpy, xw.scr); xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
xloadcols(); xloadcols();
@ -1649,7 +1750,7 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og, Line line, int le
break; break;
/* FALLTHROUGH */ /* FALLTHROUGH */
case 8: /* steady st cursor */ case 8: /* steady st cursor */
g.u = xsetcursor; g.u = (unsigned int) xsetcursor;
xdrawglyph(g, cx, cy); xdrawglyph(g, cx, cy);
break; break;
} }