]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/glfont.cpp
added a direct Xlib implementation of font bitmap generation, disabled
[xonotic/netradiant.git] / libs / gtkutil / glfont.cpp
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "glfont.h"
23 #include <GL/gl.h>
24 #include "debugging/debugging.h"
25
26 // LordHavoc: this is code for direct Xlib bitmap character fetching, as an
27 // alternative to requiring gtkglarea, it was created due to a lack of this
28 // package on SuSE 9.x but this package is now commonly shipping in Linux
29 // distributions so this code may be unnecessary, feel free however to enable
30 // it when building packages for distros that do not ship with that package,
31 // or if you just prefer less dependencies...
32 #if 0
33
34 #include <X11/Xlib.h>
35 #include <gdk/gdkx.h>
36 #include <GL/glx.h>
37
38 GLFont glfont_create(const char* font_string)
39 {
40   GLuint font_list_base;
41   XFontStruct *fontInfo;
42   Display *dpy = GDK_DISPLAY ();
43   unsigned int i, first, last, firstrow, lastrow;
44   int maxchars;
45   int firstbitmap;
46
47   fontInfo = XLoadQueryFont (dpy, "-*-fixed-*-*-*-*-8-*-*-*-*-*-*-*");
48   if (fontInfo == NULL)
49   {
50     // try to load other fonts
51     fontInfo = XLoadQueryFont (dpy, "-*-fixed-*-*-*-*-*-*-*-*-*-*-*-*");
52
53     // any font will do !
54     if (fontInfo == NULL)
55       fontInfo = XLoadQueryFont(dpy, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
56
57     if (fontInfo == NULL)
58       ERROR_MESSAGE("couldn't create font");
59   }
60
61   first = (int)fontInfo->min_char_or_byte2;
62   last = (int)fontInfo->max_char_or_byte2;
63   firstrow = (int)fontInfo->min_byte1;
64   lastrow = (int)fontInfo->max_byte1;
65   /*
66    * How many chars in the charset
67    */
68   maxchars = 256 * lastrow + last;
69   font_list_base = glGenLists(maxchars+1);
70   if (font_list_base == 0)
71   {
72     ERROR_MESSAGE( "couldn't create font" );
73   }
74
75   /*
76    * Get offset to first char in the charset
77    */
78   firstbitmap = 256 * firstrow + first;
79   /*
80    * for each row of chars, call glXUseXFont to build the bitmaps.
81    */
82
83   for(i=firstrow; i<=lastrow; i++)
84   {
85     glXUseXFont(fontInfo->fid, firstbitmap, last-first+1, font_list_base+firstbitmap);
86     firstbitmap += 256;
87   }
88
89 /*    *height = fontInfo->ascent + fontInfo->descent;
90     *width = fontInfo->max_bounds.width;  */
91   return GLFont(font_list_base, fontInfo->ascent + fontInfo->descent);
92 }
93
94 void glfont_release(GLFont& font)
95 {
96   glDeleteLists(font.getDisplayList(), 256);
97   font = GLFont(0, 0);
98 }
99
100 #else
101
102 #include <gtk/gtkglwidget.h>
103
104 GLFont glfont_create(const char* font_string)
105 {
106   GLuint font_list_base = glGenLists (256);
107   gint font_height = 0;
108
109   PangoFontDescription* font_desc = pango_font_description_from_string (font_string);
110
111   PangoFont* font = gdk_gl_font_use_pango_font (font_desc, 0, 256, font_list_base);
112
113   if(font != 0)
114   {
115     PangoFontMetrics* font_metrics = pango_font_get_metrics (font, 0);
116
117     font_height = pango_font_metrics_get_ascent (font_metrics) +
118                   pango_font_metrics_get_descent (font_metrics);
119     font_height = PANGO_PIXELS (font_height);
120
121     pango_font_metrics_unref (font_metrics);
122   }
123
124   pango_font_description_free (font_desc);
125
126   // fix for pango/gtkglext metrix bug
127   if(font_height > 16)
128           font_height = 16;
129
130   return GLFont(font_list_base, font_height);
131 }
132
133 void glfont_release(GLFont& font)
134 {
135   glDeleteLists(font.getDisplayList(), 256);
136   font = GLFont(0, 0);
137 }
138 #endif