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
21 #include "font-retry.h"
23 extern const char *progname;
26 #define countof(x) (sizeof((x))/sizeof((*x)))
29 load_font_retry_1 (Display *dpy, int screen, const char *xlfd, Bool xft_p)
33 # define LOADFONT() (xft_p \
34 ? (void *) XftFontOpenXlfd (dpy, screen, xlfd) \
35 : (void *) XLoadQueryFont (dpy, xlfd))
37 # define LOADFONT() ((void *) XLoadQueryFont (dpy, xlfd))
48 # else /* !HAVE_JWXYZ */
53 Bool bold_p = (!!strcasestr (xlfd, "-bold-"));
54 Bool italic_p = (!!strcasestr (xlfd, "-i-") ||
55 !!strcasestr (xlfd, "-o-"));
56 Bool fixed_p = (!!strcasestr (xlfd, "courier") ||
57 !!strcasestr (xlfd, "-ocr") ||
58 !!strcasestr (xlfd, "-m-") ||
59 !!strcasestr (xlfd, "-c-"));
62 if (!strcmp (xlfd, "vga")) /* BSOD uses this: it has no XLFD name. */
63 fixed_p = True, size = 120;
65 /* Look for the first number in the string like "-180-" */
69 for (s = xlfd; *s; s++)
70 if (s[0] == '-' && s[1] >= '0' && s[1] <= '9')
74 while (*s2 >= '0' && *s2 <= '9')
76 i = i * 10 + *s2 - '0';
79 if (*s2 != '-') continue; /* Number ends with dash */
80 if (i < 60 || i >= 2000) continue; /* In range 6pt - 200pt */
81 if (i % 10) continue; /* Multiple of 10 */
90 fprintf (stderr, "%s: unloadable, unparsable font: \"%s\"\n",
97 const char *fixed[] = { "courier",
101 "american typewriter",
105 const char *variable[] = { "helvetica",
107 "bitstream vera sans",
111 "new century schoolbook",
117 const char *charsets[] = { "iso10646-1", "iso8859-1", "*-*" };
118 const char *weights[] = { "bold", "medium" };
119 const char *slants[] = { "o", "i", "r" };
120 const char *spacings[] = { "m", "c", "p" };
121 int a, b, c, d, e, g;
124 for (a = 0; a < countof(charsets); a++)
125 for (b = (bold_p ? 0 : 1); b < countof(weights); b++)
126 for (c = (italic_p ? 0 : 2); c < countof(slants); c++)
128 d < (fixed_p ? countof(fixed) : countof(variable));
130 for (g = size; g >= 60; g -= 10)
131 for (e = (fixed_p ? 0 : 2); e < countof(spacings); e++)
134 "-%s-%s-%s-%s-%s-%s-%s-%d-%s-%s-%s-%s-%s",
136 (fixed_p ? fixed[d] : variable[d]),
141 "*", /* pixel size */
143 "*", /* x resolution */
144 "*", /* y resolution */
146 "*", /* average width */
148 /* fprintf(stderr, "%s: trying %s\n", progname, buf);*/
153 "%s: substituted \"%s\" for \"%s\"\n",
154 progname, buf, xlfd); */
159 fprintf (stderr, "%s: unable to find any alternatives to \"%s\"\n",
165 # endif /* !HAVE_JWXYZ */
169 load_font_retry (Display *dpy, const char *xlfd)
171 return (XFontStruct *) load_font_retry_1 (dpy, 0, xlfd, 0);
176 load_xft_font_retry (Display *dpy, int screen, const char *xlfd)
178 return (XftFont *) load_font_retry_1 (dpy, screen, xlfd, 1);
181 #elif defined(HAVE_JWXYZ)
184 load_xft_font_retry (Display *dpy, int screen, const char *xlfd)
186 return XftFontOpenXlfd (dpy, screen, xlfd);
189 #endif /* !HAVE_JWXYZ */