1 /* glxfonts, Copyright (c) 2001-2004 Jamie Zawinski <jwz@jwz.org>
2 * Loads X11 fonts for use with OpenGL.
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
21 #include "resources.h"
24 /* These are in xlock-gl.c */
25 extern void clear_gl_error (void);
26 extern void check_gl_error (const char *type);
29 extern char *progname;
32 /* Loads the font named by the X resource "res".
33 Returns an XFontStruct.
34 Also converts the font to a set of GL lists and returns the first list.
37 load_font (Display *dpy, char *res, XFontStruct **font_ret, GLuint *dlist_ret)
39 const char *font = get_string_resource (res, "Font");
40 const char *def1 = "-*-times-bold-r-normal-*-180-*";
41 const char *def2 = "fixed";
46 if (!res || !*res) abort();
47 if (!font_ret && !dlist_ret) abort();
49 if (!font) font = def1;
51 f = XLoadQueryFont(dpy, font);
52 if (!f && !!strcmp (font, def1))
54 fprintf (stderr, "%s: unable to load font \"%s\", using \"%s\"\n",
55 progname, font, def1);
57 f = XLoadQueryFont(dpy, font);
60 if (!f && !!strcmp (font, def2))
62 fprintf (stderr, "%s: unable to load font \"%s\", using \"%s\"\n",
63 progname, font, def2);
65 f = XLoadQueryFont(dpy, font);
70 fprintf (stderr, "%s: unable to load fallback font \"%s\" either!\n",
76 first = f->min_char_or_byte2;
77 last = f->max_char_or_byte2;
82 *dlist_ret = glGenLists ((GLuint) last+1);
83 check_gl_error ("glGenLists");
84 glXUseXFont(id, first, last-first+1, *dlist_ret + first);
85 check_gl_error ("glXUseXFont");
93 /* Width of the string in pixels.
96 string_width (XFontStruct *f, const char *c)
101 int cc = *((unsigned char *) c);
103 ? f->per_char[cc-f->min_char_or_byte2].rbearing
104 : f->min_bounds.rbearing);
111 /* Draws the string on the window at the given pixel position.
112 Newlines and tab stops are honored.
113 Any text inside [] will be rendered as a subscript.
114 Assumes the font has been loaded as with load_font().
117 print_gl_string (Display *dpy,
120 int window_width, int window_height,
121 GLfloat x, GLfloat y,
124 GLfloat line_height = font->ascent + font->descent;
125 GLfloat sub_shift = (line_height * 0.3);
126 int cw = string_width (font, "m");
131 glPushAttrib (GL_TRANSFORM_BIT | /* for matrix contents */
132 GL_ENABLE_BIT); /* for various glDisable calls */
133 glDisable (GL_LIGHTING);
134 glDisable (GL_DEPTH_TEST);
135 glDisable (GL_TEXTURE_2D);
137 glMatrixMode(GL_PROJECTION);
142 glMatrixMode(GL_MODELVIEW);
150 gluOrtho2D (0, window_width, 0, window_height);
152 glRasterPos2f (x, y);
153 for (i = 0; i < strlen(string); i++)
158 glRasterPos2f (x, (y -= line_height));
164 x2 = ((x2 + tabs) / tabs) * tabs; /* tab to tab stop */
166 glRasterPos2f (x2, y);
168 else if (c == '[' && (isdigit (string[i+1])))
171 glRasterPos2f (x2, (y -= sub_shift));
173 else if (c == ']' && sub_p)
176 glRasterPos2f (x2, (y += sub_shift));
180 glCallList (font_dlist + (int)(c));
181 x2 += (font->per_char
182 ? font->per_char[c - font->min_char_or_byte2].width
183 : font->min_bounds.width);
189 glMatrixMode(GL_PROJECTION);
194 glMatrixMode(GL_MODELVIEW);