1 /* xscreensaver, Copyright (c) 2018 by Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 /* Like XLoadQueryFont, but if it fails, it tries some heuristics to
19 #include "font-retry.h"
21 extern const char *progname;
24 #define countof(x) (sizeof((x))/sizeof((*x)))
27 load_font_retry (Display *dpy, const char *xlfd)
29 XFontStruct *f = XLoadQueryFont (dpy, xlfd);
37 Bool bold_p = (!!strcasestr (xlfd, "-bold-"));
38 Bool italic_p = (!!strcasestr (xlfd, "-i-") ||
39 !!strcasestr (xlfd, "-o-"));
40 Bool fixed_p = (!!strcasestr (xlfd, "courier") ||
41 !!strcasestr (xlfd, "-ocr") ||
42 !!strcasestr (xlfd, "-m-") ||
43 !!strcasestr (xlfd, "-c-"));
46 if (!strcmp (xlfd, "vga")) /* BSOD uses this: it has no XLFD name. */
47 fixed_p = True, size = 120;
49 /* Look for the first number in the string like "-180-" */
53 for (s = xlfd; *s; s++)
54 if (s[0] == '-' && s[1] >= '0' && s[1] <= '9')
58 while (*s2 >= '0' && *s2 <= '9')
60 i = i * 10 + *s2 - '0';
63 if (*s2 != '-') continue; /* Number ends with dash */
64 if (i < 60 || i >= 2000) continue; /* In range 6pt - 200pt */
65 if (i % 10) continue; /* Multiple of 10 */
74 fprintf (stderr, "%s: unloadable, unparsable font: \"%s\"\n",
76 return XLoadQueryFont (dpy, "fixed");
80 const char *fixed[] = { "courier",
84 "american typewriter",
88 const char *variable[] = { "helvetica",
90 "bitstream vera sans",
94 "new century schoolbook",
100 const char *charsets[] = { "iso10646-1", "iso8859-1", "*-*" };
101 const char *weights[] = { "bold", "medium" };
102 const char *slants[] = { "o", "i", "r" };
103 const char *spacings[] = { "m", "c", "p" };
104 int a, b, c, d, e, g;
107 for (a = 0; a < countof(charsets); a++)
108 for (b = (bold_p ? 0 : 1); b < countof(weights); b++)
109 for (c = (italic_p ? 0 : 2); c < countof(slants); c++)
111 d < (fixed_p ? countof(fixed) : countof(variable));
113 for (g = size; g >= 60; g -= 10)
114 for (e = (fixed_p ? 0 : 2); e < countof(spacings); e++)
117 "-%s-%s-%s-%s-%s-%s-%s-%d-%s-%s-%s-%s-%s",
119 (fixed_p ? fixed[d] : variable[d]),
124 "*", /* pixel size */
126 "*", /* x resolution */
127 "*", /* y resolution */
129 "*", /* average width */
131 /* fprintf(stderr, "%s: trying %s\n", progname, buf);*/
132 f = XLoadQueryFont (dpy, buf);
136 "%s: substituted \"%s\" for \"%s\"\n",
137 progname, buf, xlfd); */
142 fprintf (stderr, "%s: unable to find any alternatives to \"%s\"\n",
144 return XLoadQueryFont (dpy, "fixed");