]> icculus.org git repositories - divverent/netradiant.git/blob - libs/gtkutil/glfont.cpp
new toys :P
[divverent/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 #ifdef _WIN32
24         #include <windows.h>
25 #endif
26 #include <GL/gl.h>
27 #include "debugging/debugging.h"
28
29 // LordHavoc: this is code for direct Xlib bitmap character fetching, as an
30 // alternative to requiring gtkglarea, it was created due to a lack of this
31 // package on SuSE 9.x but this package is now commonly shipping in Linux
32 // distributions so this code may be unnecessary, feel free however to enable
33 // it when building packages for distros that do not ship with that package,
34 // or if you just prefer less dependencies...
35 #if 0
36
37 #include <X11/Xlib.h>
38 #include <gdk/gdkx.h>
39 #include <GL/glx.h>
40
41 GLFont glfont_create(const char* font_string)
42 {
43   GLuint font_list_base;
44   XFontStruct *fontInfo;
45   Display *dpy = GDK_DISPLAY ();
46   unsigned int i, first, last, firstrow, lastrow;
47   int maxchars;
48   int firstbitmap;
49
50   fontInfo = XLoadQueryFont (dpy, "-*-fixed-*-*-*-*-8-*-*-*-*-*-*-*");
51   if (fontInfo == NULL)
52   {
53     // try to load other fonts
54     fontInfo = XLoadQueryFont (dpy, "-*-fixed-*-*-*-*-*-*-*-*-*-*-*-*");
55
56     // any font will do !
57     if (fontInfo == NULL)
58       fontInfo = XLoadQueryFont(dpy, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
59
60     if (fontInfo == NULL)
61       ERROR_MESSAGE("couldn't create font");
62   }
63
64   first = (int)fontInfo->min_char_or_byte2;
65   last = (int)fontInfo->max_char_or_byte2;
66   firstrow = (int)fontInfo->min_byte1;
67   lastrow = (int)fontInfo->max_byte1;
68   /*
69    * How many chars in the charset
70    */
71   maxchars = 256 * lastrow + last;
72   font_list_base = glGenLists(maxchars+1);
73   if (font_list_base == 0)
74   {
75     ERROR_MESSAGE( "couldn't create font" );
76   }
77
78   /*
79    * Get offset to first char in the charset
80    */
81   firstbitmap = 256 * firstrow + first;
82   /*
83    * for each row of chars, call glXUseXFont to build the bitmaps.
84    */
85
86   for(i=firstrow; i<=lastrow; i++)
87   {
88     glXUseXFont(fontInfo->fid, firstbitmap, last-first+1, font_list_base+firstbitmap);
89     firstbitmap += 256;
90   }
91
92 /*    *height = fontInfo->ascent + fontInfo->descent;
93     *width = fontInfo->max_bounds.width;  */
94   return GLFont(font_list_base, fontInfo->ascent + fontInfo->descent);
95 }
96
97 void glfont_release(GLFont& font)
98 {
99   glDeleteLists(font.getDisplayList(), 256);
100   font = GLFont(0, 0);
101 }
102
103 #else
104
105 #include <gtk/gtkglwidget.h>
106
107 GLFont glfont_create(const char* font_string)
108 {
109   GLuint font_list_base = glGenLists (256);
110   gint font_height = 0;
111
112   PangoFontDescription* font_desc = pango_font_description_from_string (font_string);
113
114   PangoFont* font = gdk_gl_font_use_pango_font (font_desc, 0, 256, font_list_base);
115
116   if(font != 0)
117   {
118     PangoFontMetrics* font_metrics = pango_font_get_metrics (font, 0);
119
120     font_height = pango_font_metrics_get_ascent (font_metrics) +
121                   pango_font_metrics_get_descent (font_metrics);
122     font_height = PANGO_PIXELS (font_height);
123
124     pango_font_metrics_unref (font_metrics);
125   }
126
127   pango_font_description_free (font_desc);
128
129   // fix for pango/gtkglext metrix bug
130   if(font_height > 16)
131           font_height = 16;
132
133   return GLFont(font_list_base, font_height);
134 }
135
136 void glfont_release(GLFont& font)
137 {
138   glDeleteLists(font.getDisplayList(), 256);
139   font = GLFont(0, 0);
140 }
141 #endif