+
+ if (metrics_ret)
+ *metrics_ret = overall;
+}
+
+
+/* Bounding box of the multi-line string, in pixels,
+ and overall ascent/descent of the font.
+ */
+void
+texture_string_metrics (texture_font_data *data, const char *s,
+ XCharStruct *metrics_ret,
+ int *ascent_ret, int *descent_ret)
+{
+ if (metrics_ret)
+ iterate_texture_string (data, s, 0, 0, 0, 0, metrics_ret);
+ if (ascent_ret) *ascent_ret = data->xftfont->ascent;
+ if (descent_ret) *descent_ret = data->xftfont->descent;
+}
+
+
+/* Returns a cache entry for this string, with a valid texid.
+ If the returned entry has a string in it, the texture is valid.
+ Otherwise it is an empty entry waiting to be rendered.
+ */
+static struct texfont_cache *
+get_cache (texture_font_data *data, const char *string)
+{
+ int count = 0;
+ texfont_cache *prev = 0, *prev2 = 0, *curr = 0, *next = 0;
+
+ if (data->cache)
+ for (prev2 = 0, prev = 0, curr = data->cache, next = curr->next;
+ curr;
+ prev2 = prev, prev = curr, curr = next,
+ next = (curr ? curr->next : 0), count++)
+ {
+ if (!strcmp (string, curr->string))
+ {
+ if (prev)
+ prev->next = next; /* Unlink from list */
+ if (curr != data->cache)
+ {
+ curr->next = data->cache; /* Move to front */
+ data->cache = curr;
+ }
+ return curr;
+ }
+ }
+
+ /* Made it to the end of the list without a hit.
+ If the cache is full, empty out the last one on the list,
+ and move it to the front. Keep the texid.
+ */
+ if (count > data->cache_size)
+ {
+ free (prev->string);
+ prev->string = 0;
+ prev->tex_width = 0;
+ prev->tex_height = 0;
+ memset (&prev->extents, 0, sizeof(prev->extents));
+ if (prev2)
+ prev2->next = 0;
+ if (prev != data->cache)
+ prev->next = data->cache;
+ data->cache = prev;
+ return prev;
+ }
+
+ /* Not cached, and cache not full. Add a new entry at the front,
+ and allocate a new texture for it.
+ */
+ curr = (struct texfont_cache *) calloc (1, sizeof(*prev));
+ glGenTextures (1, &curr->texid);
+ curr->string = 0;
+ curr->next = data->cache;
+ data->cache = curr;
+
+ return curr;
+}
+
+
+/* Renders the given string into the prevailing texture.
+ Returns the metrics of the text, and size of the texture.
+ */
+void
+string_to_texture (texture_font_data *data, const char *string,
+ XCharStruct *extents_ret,
+ int *tex_width_ret, int *tex_height_ret)
+{
+ Window window = RootWindow (data->dpy, 0);
+ Pixmap p;
+ XGCValues gcv;
+ GC gc;
+ XWindowAttributes xgwa;
+ XRenderColor rcolor;
+ XftColor xftcolor;
+ XftDraw *xftdraw;
+ int width, height;
+ XCharStruct overall;
+
+ /* Measure the string and create a Pixmap of the proper size.
+ */
+ XGetWindowAttributes (data->dpy, window, &xgwa);
+ iterate_texture_string (data, string, 0, 0, 0, 0, &overall);
+ width = overall.rbearing - overall.lbearing;
+ height = overall.ascent + overall.descent;
+ if (width <= 0) width = 1;
+ if (height <= 0) height = 1;
+ p = XCreatePixmap (data->dpy, window, width, height, xgwa.depth);
+
+ gcv.foreground = BlackPixelOfScreen (xgwa.screen);
+ gc = XCreateGC (data->dpy, p, GCForeground, &gcv);
+ XFillRectangle (data->dpy, p, gc, 0, 0, width, height);
+ XFreeGC (data->dpy, gc);
+
+ /* Render the string into the pixmap.
+ */
+ rcolor.red = rcolor.green = rcolor.blue = rcolor.alpha = 0xFFFF;
+ XftColorAllocValue (data->dpy, xgwa.visual, xgwa.colormap,
+ &rcolor, &xftcolor);
+ xftdraw = XftDrawCreate (data->dpy, p, xgwa.visual, xgwa.colormap);
+ iterate_texture_string (data, string,
+ -overall.lbearing, overall.ascent,
+ xftdraw, &xftcolor, 0);
+ XftDrawDestroy (xftdraw);
+ XftColorFree (data->dpy, xgwa.visual, xgwa.colormap, &xftcolor);
+
+ /* Copy the bits from the Pixmap into a texture, unless it's cached.
+ */
+ bitmap_to_texture (data->dpy, p, xgwa.visual, xgwa.depth,
+ &width, &height);
+ XFreePixmap (data->dpy, p);
+
+ if (extents_ret) *extents_ret = overall;
+ if (tex_width_ret) *tex_width_ret = width;
+ if (tex_height_ret) *tex_height_ret = height;
+}
+
+
+/* Set the various OpenGL parameters for properly rendering things
+ with a texture generated by string_to_texture().
+ */
+void
+enable_texture_string_parameters (void)
+{
+ glEnable (GL_TEXTURE_2D);
+
+ /* Texture-rendering parameters to make font pixmaps tolerable to look at.
+ */
+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
+ GL_LINEAR_MIPMAP_LINEAR);
+
+ /* LOD bias is part of OpenGL 1.4.
+ GL_EXT_texture_lod_bias has been present since the original iPhone.
+ */
+# if !defined(GL_TEXTURE_LOD_BIAS) && defined(GL_TEXTURE_LOD_BIAS_EXT)
+# define GL_TEXTURE_LOD_BIAS GL_TEXTURE_LOD_BIAS_EXT
+# endif
+# ifdef GL_TEXTURE_LOD_BIAS
+ glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0.25);
+# endif
+ clear_gl_error(); /* invalid enum on iPad 3 */
+
+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+
+ /* Don't write the transparent parts of the quad into the depth buffer. */
+ glAlphaFunc (GL_GREATER, 0.01);
+ glEnable (GL_ALPHA_TEST);
+ glEnable (GL_BLEND);
+ glDisable (GL_LIGHTING);
+ glDisable (GL_TEXTURE_GEN_S);
+ glDisable (GL_TEXTURE_GEN_T);
+ glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);