From http://www.jwz.org/xscreensaver/xscreensaver-5.23.tar.gz
[xscreensaver] / hacks / glx / glxfonts.c
index 9197af975b2f69ecb4ab8e4e8fb7f0395ff615d1..d4af15047380cbf841b0cb92e7295947a23f2f54 100644 (file)
@@ -1,4 +1,4 @@
-/* glxfonts, Copyright (c) 2001-2009 Jamie Zawinski <jwz@jwz.org>
+/* glxfonts, Copyright (c) 2001-2012 Jamie Zawinski <jwz@jwz.org>
  * Loads X11 fonts for use with OpenGL.
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
@@ -8,6 +8,11 @@
  * documentation.  No representations are made about the suitability of this
  * software for any purpose.  It is provided "as is" without express or 
  * implied warranty.
+ *
+ * Draws 2D text over the GL scene, e.g., the FPS displays.
+ *
+ * There are two implementations here: one using glBitmap (the OpenGL 1.1 way)
+ * and one using textures via texfont.c (since OpenGLES doesn't have glBitmap).
  */
 
 #ifdef HAVE_CONFIG_H
 
 #ifdef HAVE_COCOA
 # include "jwxyz.h"
-# include <OpenGL/gl.h>
-# include <OpenGL/glu.h>
-# include <AGL/agl.h>
+/*# include <AGL/agl.h>*/
 #else
 # include <GL/glx.h>
 # include <GL/glu.h>
 #endif
 
+#ifdef HAVE_JWZGLES
+# include "jwzgles.h"
+#endif /* HAVE_JWZGLES */
+
 #include "resources.h"
 #include "glxfonts.h"
+#include "fps.h"
+
+#ifndef HAVE_GLBITMAP
+# include "texfont.h"
+#endif /* HAVE_GLBITMAP */
+
 
 /* These are in xlock-gl.c */
 extern void clear_gl_error (void);
@@ -46,6 +59,42 @@ extern char *progname;
                */
 
 
+#undef countof
+#define countof(x) (sizeof((x))/sizeof((*x)))
+
+
+/* Width (and optionally height) of the string in pixels.
+ */
+int
+string_width (XFontStruct *f, const char *c, int *height_ret)
+{
+  int x = 0;
+  int max_w = 0;
+  int h = f->ascent + f->descent;
+  while (*c)
+    {
+      int cc = *((unsigned char *) c);
+      if (*c == '\n')
+        {
+          if (x > max_w) max_w = x;
+          x = 0;
+          h += f->ascent + f->descent;
+        }
+      else
+        x += (f->per_char
+              ? f->per_char[cc-f->min_char_or_byte2].width
+              : f->min_bounds.rbearing);
+      c++;
+    }
+  if (x > max_w) max_w = x;
+  if (height_ret) *height_ret = h;
+
+  return max_w;
+}
+
+
+#ifdef HAVE_GLBITMAP
+
 /* Mostly lifted from the Mesa implementation of glXUseXFont(), since
    Mac OS 10.6 no longer supports aglUseFont() which was their analog
    of that.  This code could be in libjwxyz instead, but we might as
@@ -124,6 +173,8 @@ xscreensaver_glXUseXFont (Display *dpy, Font font,
 
   int i;
 
+  clear_gl_error ();
+
   fs = XQueryFont (dpy, font);  
   if (!fs)
     {
@@ -166,6 +217,9 @@ xscreensaver_glXUseXFont (Display *dpy, Font font,
   glPixelStorei        (GL_UNPACK_SKIP_PIXELS, 0);
   glPixelStorei        (GL_UNPACK_ALIGNMENT, 1);
 
+  clear_gl_error(); /* WTF? sometimes "invalid op" from glPixelStorei! */
+
+
   pixmap = XCreatePixmap (dpy, win, 10, 10, 1);
   values.foreground = 0;
   values.background = 1;
@@ -257,6 +311,8 @@ xscreensaver_glXUseXFont (Display *dpy, Font font,
   glPixelStorei(GL_UNPACK_SKIP_ROWS, skiprows);
   glPixelStorei(GL_UNPACK_SKIP_PIXELS, skippixels);
   glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
+
+  check_gl_error ("xscreensaver_glXUseXFont");
 }
 
 
@@ -315,67 +371,55 @@ load_font (Display *dpy, char *res, XFontStruct **font_ret, GLuint *dlist_ret)
       clear_gl_error ();
       *dlist_ret = glGenLists ((GLuint) last+1);
       check_gl_error ("glGenLists");
-      xscreensaver_glXUseXFont(dpy, id, first, last-first+1,
-                               *dlist_ret + first);
-      check_gl_error ("xscreensaver_glXUseXFont");
+      xscreensaver_glXUseXFont (dpy, id, first, last-first+1,
+                                *dlist_ret + first);
     }
 
   if (font_ret)
     *font_ret = f;
 }
 
-
-/* Width (and optionally height) of the string in pixels.
- */
-int
-string_width (XFontStruct *f, const char *c, int *height_ret)
-{
-  int x = 0;
-  int max_w = 0;
-  int h = f->ascent + f->descent;
-  while (*c)
-    {
-      int cc = *((unsigned char *) c);
-      if (*c == '\n')
-        {
-          if (x > max_w) max_w = x;
-          x = 0;
-          h += f->ascent + f->descent;
-        }
-      else
-        x += (f->per_char
-              ? f->per_char[cc-f->min_char_or_byte2].width
-              : f->min_bounds.rbearing);
-      c++;
-    }
-  if (x > max_w) max_w = x;
-  if (height_ret) *height_ret = h;
-
-  return max_w;
-}
+#endif /* HAVE_GLBITMAP */
 
 
 /* Draws the string on the window at the given pixel position.
    Newlines and tab stops are honored.
-   Any text inside [] will be rendered as a subscript.
+   Any numbers inside [] will be rendered as a subscript.
    Assumes the font has been loaded as with load_font().
  */
 void
 print_gl_string (Display *dpy,
-                 XFontStruct *font,
-                 GLuint font_dlist,
+# ifdef HAVE_GLBITMAP
+                 XFontStruct *font, GLuint font_dlist,
+# else
+                 texture_font_data *font_data,
+# endif
                  int window_width, int window_height,
                  GLfloat x, GLfloat y,
                  const char *string,
                  Bool clear_background_p)
 {
+
+  /* If window_width was specified, we're drawing ortho in pixel coordinates.
+     Otherwise, we're just dropping the text at the current position in the
+     scene, billboarded. */
+  Bool in_scene_p = (window_width == 0);
+
+# ifdef HAVE_GLBITMAP
   GLfloat line_height = font->ascent + font->descent;
-  GLfloat sub_shift = (line_height * 0.3);
   int cw = string_width (font, "m", 0);
-  int tabs = cw * 7;
+# else /* !HAVE_GLBITMAP */
+  int line_height = 0;
+  int cw = texture_string_width (font_data, "m", &line_height);
+# endif /* !HAVE_GLBITMAP */
 
-  y -= line_height;
+  GLfloat sub_shift = (line_height * 0.3);
+  int tabs = cw * 7;
+  int lines = 0;
+  const char *c;
+  GLfloat color[4];
 
+# ifdef HAVE_GLBITMAP
   /* Sadly, this causes a stall of the graphics pipeline (as would the
      equivalent calls to glGet*.)  But there's no way around this, short
      of having each caller set up the specific display matrix we need
@@ -386,19 +430,56 @@ print_gl_string (Display *dpy,
                 GL_ENABLE_BIT |     /* for various glDisable calls */
                 GL_CURRENT_BIT |    /* for glColor3f() */
                 GL_LIST_BIT);       /* for glListBase() */
-  {
-# ifdef DEBUG
+#  ifdef DEBUG
     check_gl_error ("glPushAttrib");
-# endif
+#  endif
+# else /* !HAVE_GLBITMAP */
+    Bool tex_p   = glIsEnabled (GL_TEXTURE_2D);
+    Bool texs_p  = glIsEnabled (GL_TEXTURE_GEN_S);
+    Bool text_p  = glIsEnabled (GL_TEXTURE_GEN_T);
+    Bool light_p = glIsEnabled (GL_LIGHTING);
+    Bool blend_p = glIsEnabled (GL_BLEND);
+    Bool depth_p = glIsEnabled (GL_DEPTH_TEST);
+    Bool cull_p  = glIsEnabled (GL_CULL_FACE);
+    Bool fog_p   = glIsEnabled (GL_FOG);
+    GLint oblend;
+#  ifndef HAVE_JWZGLES
+    GLint opoly[2];
+    glGetIntegerv (GL_POLYGON_MODE, opoly);
+#  endif
+    glGetIntegerv (GL_BLEND_DST, &oblend);
+# endif /* !HAVE_GLBITMAP */
+
+    glGetFloatv (GL_CURRENT_COLOR, color);
+
+  for (c = string; *c; c++)
+    if (*c == '\n') lines++;
+
+  y -= line_height;
+
+  {
 
     /* disable lighting and texturing when drawing bitmaps!
        (glPopAttrib() restores these.)
      */
+# ifdef HAVE_GLBITMAP
     glDisable (GL_TEXTURE_2D);
+# else /* !HAVE_GLBITMAP */
+    glEnable (GL_TEXTURE_2D);
+    glDisable (GL_TEXTURE_GEN_S);
+    glDisable (GL_TEXTURE_GEN_T);
+    glPolygonMode (GL_FRONT, GL_FILL);
+    glEnable (GL_BLEND);
+    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+# endif /* !HAVE_GLBITMAP */
+
     glDisable (GL_LIGHTING);
-    glDisable (GL_BLEND);
-    glDisable (GL_DEPTH_TEST);
+
+
+      if (!in_scene_p)
+        glDisable (GL_DEPTH_TEST);
     glDisable (GL_CULL_FACE);
+    glDisable (GL_FOG);
 
     /* glPopAttrib() does not restore matrix changes, so we must
        push/pop the matrix stacks to be non-intrusive there.
@@ -409,7 +490,8 @@ print_gl_string (Display *dpy,
 # ifdef DEBUG
       check_gl_error ("glPushMatrix");
 # endif
-      glLoadIdentity();
+      if (!in_scene_p)
+        glLoadIdentity();
 
       /* Each matrix mode has its own stack, so we need to push/pop
          them separately. */
@@ -424,34 +506,78 @@ print_gl_string (Display *dpy,
         check_gl_error ("glPushMatrix");
 # endif
 
-        glLoadIdentity();
-        gluOrtho2D (0, window_width, 0, window_height);
+        if (!in_scene_p)
+          {
+            double rot = current_device_rotation();
+
+            glLoadIdentity();
+            glOrtho (0, window_width, 0, window_height, -1, 1);
+
+            if (rot > 135 || rot < -135)
+              {
+                glTranslatef (window_width, window_height, 0);
+                glRotatef (180, 0, 0, 1);
+              }
+            else if (rot > 45)
+              {
+                glTranslatef (window_width, 0, 0);
+                glRotatef (90, 0, 0, 1);
+                y -= (window_height - window_width);
+                if (y < line_height * lines + 10)
+                  y = line_height * lines + 10;
+              }
+            else if (rot < -45)
+              {
+                glTranslatef(0, window_height, 0);
+                glRotatef (-90, 0, 0, 1);
+                y -= (window_height - window_width);
+                if (y < line_height * lines + 10)
+                  y = line_height * lines + 10;
+              }
+          }
 # ifdef DEBUG
-        check_gl_error ("gluOrtho2D");
+        check_gl_error ("glOrtho");
 # endif
 
+        /* Let's always dropshadow the FPS and Title text. */
+        if (! in_scene_p)
+          clear_background_p = True;
+
+#if 0
         if (clear_background_p)
           {
             int w, h;
-            int lh = font->ascent + font->descent;
-            w = string_width (font, string, &h);
             glColor3f (0, 0, 0);
+# ifdef HAVE_GLBITMAP
+            w = string_width (font, string, &h);
             glRecti (x - font->descent,
-                     y + lh
+                     y + line_height
                      x + w + 2*font->descent,
-                     y + lh - h - font->descent);
+                     y + line_height - h - font->descent);
+# else /* !HAVE_GLBITMAP */
+            {
+              int descent = line_height * 0.2;
+              if (descent < 2) descent = 2;
+              w = texture_string_width (font_data, string, &h);
+              glRecti (x - descent,
+                       y + line_height, 
+                       x + w + 2*descent,
+                       y + line_height - h - descent);
+            }
+# endif /* !HAVE_GLBITMAP */
             glColor3f (1, 1, 1);
           }
+#endif /* 0 */
+
 
         /* draw the text */
-        glRasterPos2f (x, y);
-/*        glListBase (font_dlist);*/
+
         for (i = 0; i < strlen(string); i++)
           {
             unsigned char c = (unsigned char) string[i];
             if (c == '\n')
               {
-                glRasterPos2f (x, (y -= line_height));
+                y -= line_height;
                 x2 = x;
               }
             else if (c == '\t')
@@ -459,25 +585,57 @@ print_gl_string (Display *dpy,
                 x2 -= x;
                 x2 = ((x2 + tabs) / tabs) * tabs;  /* tab to tab stop */
                 x2 += x;
-                glRasterPos2f (x2, y);
               }
             else if (c == '[' && (isdigit (string[i+1])))
               {
                 sub_p = True;
-                glRasterPos2f (x2, (y -= sub_shift));
+                y -= sub_shift;
               }
             else if (c == ']' && sub_p)
               {
                 sub_p = False;
-                glRasterPos2f (x2, (y += sub_shift));
+                y += sub_shift;
               }
             else
               {
-/*            glCallLists (s - string, GL_UNSIGNED_BYTE, string);*/
-                glCallList (font_dlist + (int)(c));
+                /* outline font in black */
+                const XPoint offsets[] = {{ -1, -1 },
+                                          { -1,  1 },
+                                          {  1,  1 },
+                                          {  1, -1 },
+                                          {  0,  0 }};
+                int j;
+# ifndef HAVE_GLBITMAP
+                char s[2];
+                s[0] = c;
+                s[1] = 0;
+# endif /* !HAVE_GLBITMAP */
+
+                glColor3f (0, 0, 0);
+                for (j = 0; j < countof(offsets); j++)
+                  {
+                    if (! clear_background_p)
+                      j = countof(offsets)-1;
+                    if (offsets[j].x == 0)
+                      glColor4fv (color);
+
+# ifdef HAVE_GLBITMAP
+                    glRasterPos2f (x2 + offsets[j].x, y + offsets[j].y);
+                    glCallList (font_dlist + (int)(c));
+# else /* !HAVE_GLBITMAP */
+                    glPushMatrix();
+                    glTranslatef (x2 + offsets[j].x, y + offsets[j].y, 0);
+                    print_texture_string (font_data, s);
+                    glPopMatrix();
+# endif /* !HAVE_GLBITMAP */
+                  }
+# ifdef HAVE_GLBITMAP
                 x2 += (font->per_char
                        ? font->per_char[c - font->min_char_or_byte2].width
                        : font->min_bounds.width);
+# else /* !HAVE_GLBITMAP */
+                x2 += texture_string_width (font_data, s, 0);
+# endif /* !HAVE_GLBITMAP */
               }
           }
 # ifdef DEBUG
@@ -489,10 +647,25 @@ print_gl_string (Display *dpy,
     glMatrixMode(GL_PROJECTION);
     glPopMatrix();
   }
+# ifdef HAVE_GLBITMAP
   glPopAttrib();
-# ifdef DEBUG
+#  ifdef DEBUG
   check_gl_error ("glPopAttrib");
-# endif
+#  endif
+# else  /* !HAVE_GLBITMAP */
+  if (tex_p)   glEnable (GL_TEXTURE_2D); else glDisable (GL_TEXTURE_2D);
+  if (texs_p)  glEnable (GL_TEXTURE_GEN_S);/*else glDisable (GL_TEXTURE_GEN_S);*/
+  if (text_p)  glEnable (GL_TEXTURE_GEN_T);/*else glDisable (GL_TEXTURE_GEN_T);*/
+  if (blend_p) glEnable (GL_BLEND); else glDisable (GL_BLEND);
+  if (light_p) glEnable (GL_LIGHTING); /*else glDisable (GL_LIGHTING);*/
+  if (depth_p) glEnable (GL_DEPTH_TEST); else glDisable (GL_DEPTH_TEST);
+  if (cull_p)  glEnable (GL_CULL_FACE); /*else glDisable (GL_CULL_FACE);*/
+  if (fog_p)   glEnable (GL_FOG); /*else glDisable (GL_FOG);*/
+#  ifndef HAVE_JWZGLES
+  glPolygonMode (GL_FRONT, opoly[0]);
+#  endif
+  glBlendFunc (GL_SRC_ALPHA, oblend);
+# endif /* !HAVE_GLBITMAP */
 
   glMatrixMode(GL_MODELVIEW);
 }