From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / texfont.c
index 0262f86161427c2d54459e16b133ebab486953a6..5388efd4f75f94b3d35c28a5cba52fb832433dbd 100644 (file)
@@ -1,4 +1,4 @@
-/* texfonts, Copyright (c) 2005-2016 Jamie Zawinski <jwz@jwz.org>
+/* texfonts, Copyright (c) 2005-2017 Jamie Zawinski <jwz@jwz.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -44,6 +44,7 @@
 #endif /* HAVE_XSHM_EXTENSION */
 
 #include "xft.h"
+#include "pow2.h"
 #include "resources.h"
 #include "texfont.h"
 #include "fps.h"       /* for current_device_rotation() */
@@ -82,20 +83,6 @@ struct texture_font_data {
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
 
-/* return the next larger power of 2. */
-static int
-to_pow2 (int i)
-{
-  static const unsigned int pow2[] = { 
-    1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 
-    2048, 4096, 8192, 16384, 32768, 65536 };
-  int j;
-  for (j = 0; j < sizeof(pow2)/sizeof(*pow2); j++)
-    if (pow2[j] >= i) return pow2[j];
-  abort();  /* too big! */
-}
-
-
 /* Given a Pixmap (of screen depth), converts it to an OpenGL luminance mipmap.
    RGB are averaged to grayscale, and the resulting value is treated as alpha.
    Pass in the size of the pixmap; the size of the texture is returned
@@ -111,26 +98,13 @@ bitmap_to_texture (Display *dpy, Pixmap p, Visual *visual, int depth,
   Bool mipmap_p = True;
   int ow = *wP;
   int oh = *hP;
-  int w2 = to_pow2 (ow);
-  int h2 = to_pow2 (oh);
+  GLsizei w2 = (GLsizei) to_pow2 (ow);
+  GLsizei h2 = (GLsizei) to_pow2 (oh);
   int x, y, max, scale;
   XImage *image = 0;
   unsigned char *data = (unsigned char *) calloc (w2 * 2, (h2 + 1));
   unsigned char *out = data;
 
-  /* If either dimension is larger than the supported size, reduce.
-     We still return the old size to keep the caller's math working,
-     but the texture itself will have fewer pixels in it.
-   */
-  glGetIntegerv (GL_MAX_TEXTURE_SIZE, &max);
-  scale = 1;
-  while (w2 > max || h2 > max)
-    {
-      w2 /= 2;
-      h2 /= 2;
-      scale *= 2;
-    }
-
   /* OpenGLES doesn't support GL_INTENSITY, so instead of using a
      texture with 1 byte per pixel, the intensity value, we have
      to use 2 bytes per pixel: solid white, and an alpha value.
@@ -139,20 +113,24 @@ bitmap_to_texture (Display *dpy, Pixmap p, Visual *visual, int depth,
 #  undef GL_INTENSITY
 # endif
 
-# ifdef GL_INTENSITY
-  GLuint iformat = GL_INTENSITY;
-  GLuint format  = GL_LUMINANCE;
-# else
-  GLuint iformat = GL_LUMINANCE_ALPHA;
-  GLuint format  = GL_LUMINANCE_ALPHA;
-# endif
-  GLuint type    = GL_UNSIGNED_BYTE;
-
 # ifdef HAVE_XSHM_EXTENSION
   Bool use_shm = get_boolean_resource (dpy, "useSHM", "Boolean");
   XShmSegmentInfo shm_info;
 # endif /* HAVE_XSHM_EXTENSION */
 
+  /* If either dimension is larger than the supported size, reduce.
+     We still return the old size to keep the caller's math working,
+     but the texture itself will have fewer pixels in it.
+   */
+  glGetIntegerv (GL_MAX_TEXTURE_SIZE, &max);
+  scale = 1;
+  while (w2 > max || h2 > max)
+    {
+      w2 /= 2;
+      h2 /= 2;
+      scale *= 2;
+    }
+
 # ifdef HAVE_XSHM_EXTENSION
   if (use_shm)
     {
@@ -165,8 +143,15 @@ bitmap_to_texture (Display *dpy, Pixmap p, Visual *visual, int depth,
     }
 # endif /* HAVE_XSHM_EXTENSION */
 
-  if (!image)
-    image = XGetImage (dpy, p, 0, 0, ow, oh, ~0L, ZPixmap);
+  if (!image) {
+    /* XCreateImage fills in (red|green_blue)_mask. XGetImage only does that
+       when reading from a Window, not when it's a Pixmap.
+     */
+    image = XCreateImage (dpy, visual, depth, ZPixmap, 0, NULL, ow, oh,
+                          BitmapPad (dpy), 0);
+    image->data = malloc (image->height * image->bytes_per_line);
+    XGetSubImage (dpy, p, 0, 0, ow, oh, ~0L, ZPixmap, image, 0, 0);
+  }
 
 # ifdef HAVE_JWZGLES
   /* This would work, but it's wasteful for no benefit. */
@@ -187,7 +172,7 @@ bitmap_to_texture (Display *dpy, Pixmap p, Visual *visual, int depth,
                              XGetPixel (image, sx, sy));
       /* instead of averaging all three channels, let's just use red,
          and assume it was already grayscale. */
-      unsigned long r = pixel & visual->red_mask;
+      unsigned long r = pixel & image->red_mask;
       /* This goofy trick is to make any of RGBA/ABGR/ARGB work. */
       pixel = ((r >> 24) | (r >> 16) | (r >> 8) | r) & 0xFF;
 
@@ -226,14 +211,30 @@ bitmap_to_texture (Display *dpy, Pixmap p, Visual *visual, int depth,
     destroy_xshm_image (dpy, image, &shm_info);
   else
 # endif /* HAVE_XSHM_EXTENSION */
+  {
+    /* We malloc'd image->data, so we free it. */
+    free (image->data);
+    image->data = NULL;
     XDestroyImage (image);
+  }
 
   image = 0;
 
-  if (mipmap_p)
-    gluBuild2DMipmaps (GL_TEXTURE_2D, iformat, w2, h2, format, type, data);
-  else
-    glTexImage2D (GL_TEXTURE_2D, 0, iformat, w2, h2, 0, format, type, data);
+  {
+# ifdef GL_INTENSITY
+    GLuint iformat = GL_INTENSITY;
+    GLuint format  = GL_LUMINANCE;
+# else
+    GLuint iformat = GL_LUMINANCE_ALPHA;
+    GLuint format  = GL_LUMINANCE_ALPHA;
+# endif
+    GLuint type    = GL_UNSIGNED_BYTE;
+
+    if (mipmap_p)
+      gluBuild2DMipmaps (GL_TEXTURE_2D, iformat, w2, h2, format, type, data);
+    else
+      glTexImage2D (GL_TEXTURE_2D, 0, iformat, w2, h2, 0, format, type, data);
+  }
 
   {
     char msg[100];
@@ -647,7 +648,16 @@ print_texture_string (texture_font_data *data, const char *string)
       tex_height = data->cache->tex_height;
     }
   else
-    string_to_texture (data, string, &overall, &tex_width, &tex_height);
+    {
+# if defined HAVE_JWXYZ && defined JWXYZ_GL
+      /* TODO, JWXYZ_GL: Mixing Xlib and GL has issues. */
+      memset (&overall, 0, sizeof(overall));
+      tex_width = 8;
+      tex_height = 8;
+# else
+      string_to_texture (data, string, &overall, &tex_width, &tex_height);
+# endif
+    }
 
   {
     int ofront, oblend;