]> icculus.org git repositories - divverent/netradiant.git/blob - libs/gtkutil/glfont.cpp
glfont.cpp: add an internal class that uses Pango
[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, font_ascent = 0, font_descent = 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           pango_font_description_free (font_desc);
119           font_desc = pango_font_description_from_string ("fixed 8");
120           font = gdk_gl_font_use_pango_font (font_desc, 0, 256, font_list_base);
121   }
122
123   if(font == 0)
124   {
125           pango_font_description_free (font_desc);
126           font_desc = pango_font_description_from_string ("courier new 8");
127           font = gdk_gl_font_use_pango_font (font_desc, 0, 256, font_list_base);
128   }
129
130   if(font != 0)
131   {
132     PangoFontMetrics* font_metrics = pango_font_get_metrics (font, 0);
133
134     font_ascent = pango_font_metrics_get_ascent (font_metrics);
135         font_descent = pango_font_metrics_get_descent (font_metrics);
136         font_height = font_ascent + font_descent;
137
138     font_ascent = PANGO_PIXELS (font_ascent);
139     font_descent = PANGO_PIXELS (font_descent);
140     font_height = PANGO_PIXELS (font_height);
141
142     pango_font_metrics_unref (font_metrics);
143   }
144
145   pango_font_description_free (font_desc);
146
147   // fix for pango/gtkglext metrix bug
148   if(font_height > 256)
149           font_height = 16;
150
151   return GLFont(font_list_base, font_ascent, font_descent, font_height);
152 }
153
154 void glfont_release(GLFont& font)
155 {
156   glDeleteLists(font.getDisplayList(), 256);
157   font = GLFont(0, 0, 0, 0);
158 }
159 #endif
160
161
162
163 // new font code ripped from ZeroRadiant (not in use yet)
164
165 #include <pango/pangoft2.h>
166 #include "igl.h"
167
168 class GLFontInternal
169 {
170         const char *font_string;
171         int font_height;
172         int font_ascent;
173         int font_descent;
174         int y_offset_bitmap_render_pango_units;
175         PangoContext *ft2_context;
176
177         public:
178         GLFontInternal(const char *_font_string): font_string(font_string)
179         {
180                 PangoFontDescription *font_desc;
181                 PangoLayout *layout;
182                 PangoRectangle log_rect;
183                 int font_ascent_pango_units;
184                 int font_descent_pango_units;
185
186                 // This call is deprecated so we'll have to fix it sometime.
187                 ft2_context = pango_ft2_get_context(72, 72);
188
189                 font_desc = pango_font_description_from_string(font_string);
190                 pango_context_set_font_description(ft2_context, font_desc);
191                 pango_font_description_free(font_desc);
192
193                 layout = pango_layout_new(ft2_context);
194 #if !PANGO_VERSION_CHECK(1,22,0)
195                 PangoLayoutIter *iter;  
196                 iter = pango_layout_get_iter(layout);
197                 font_ascent_pango_units = pango_layout_iter_get_baseline(iter);
198                 pango_layout_iter_free(iter);
199 #else
200                 font_ascent_pango_units = pango_layout_get_baseline(layout);
201 #endif
202                 pango_layout_get_extents(layout, NULL, &log_rect);
203                 g_object_unref(G_OBJECT(layout));
204                 font_descent_pango_units = log_rect.height - font_ascent_pango_units;
205
206                 font_ascent = PANGO_PIXELS_CEIL(font_ascent_pango_units);
207                 font_descent = PANGO_PIXELS_CEIL(font_descent_pango_units);
208                 font_height = font_ascent + font_descent;
209                 y_offset_bitmap_render_pango_units = (font_ascent * PANGO_SCALE) - font_ascent_pango_units;
210         }
211
212         ~GLFontInternal()
213         {
214                 g_object_unref(G_OBJECT(ft2_context));
215         }
216
217         // Renders the input text at the current location with the current color.
218         // The X position of the current location is used to place the left edge of the text image,
219         // where the text image bounds are defined as the logical extents of the line of text.
220         // The Y position of the current location is used to place the bottom of the text image.
221         // You should offset the Y position by the amount returned by gtk_glwidget_font_descent()
222         // if you want to place the baseline of the text image at the current Y position.
223         // Note: A problem with this function is that if the lower left corner of the text falls
224         // just a hair outside of the viewport (meaning the current raster position is invalid),
225         // then no text will be rendered.  The solution to this is a very hacky one.  You can search
226         // Google for "glDrawPixels clipping".
227         void printString(const char *s)
228         {
229                 // The idea for this code initially came from the font-pangoft2.c example that comes with GtkGLExt.
230
231                 PangoLayout *layout;
232                 PangoRectangle log_rect;
233                 FT_Bitmap bitmap;
234                 unsigned char *begin_bitmap_buffer;
235                 GLfloat color[4];
236                 GLint previous_unpack_alignment;
237                 GLboolean previous_blend_enabled;
238                 GLint previous_blend_func_src;
239                 GLint previous_blend_func_dst;
240                 GLfloat previous_red_bias;
241                 GLfloat previous_green_bias;
242                 GLfloat previous_blue_bias;
243                 GLfloat previous_alpha_scale;
244
245                 layout = pango_layout_new(ft2_context);
246                 pango_layout_set_width(layout, -1); // -1 no wrapping.  All text on one line.
247                 pango_layout_set_text(layout, s, -1); // -1 null-terminated string.
248                 pango_layout_get_extents(layout, NULL, &log_rect);
249
250                 if (log_rect.width > 0 && log_rect.height > 0) {
251                         bitmap.rows = font_ascent + font_descent;
252                         bitmap.width = PANGO_PIXELS_CEIL(log_rect.width);
253                         bitmap.pitch = -bitmap.width; // Rendering it "upside down" for OpenGL.
254                         begin_bitmap_buffer = (unsigned char *) g_malloc(bitmap.rows * bitmap.width);
255                         memset(begin_bitmap_buffer, 0, bitmap.rows * bitmap.width);
256                         bitmap.buffer = begin_bitmap_buffer + (bitmap.rows - 1) * bitmap.width; // See pitch above.
257                         bitmap.num_grays = 0xff;
258                         bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
259                         pango_ft2_render_layout_subpixel(&bitmap, layout, -log_rect.x,
260                                         y_offset_bitmap_render_pango_units);
261                         GlobalOpenGL().m_glGetFloatv(GL_CURRENT_COLOR, color);
262
263                         // Save state.  I didn't see any OpenGL push/pop operations for these.
264                         // Question: Is saving/restoring this state necessary?  Being safe.
265                         GlobalOpenGL().m_glGetIntegerv(GL_UNPACK_ALIGNMENT, &previous_unpack_alignment);
266                         previous_blend_enabled = GlobalOpenGL().m_glIsEnabled(GL_BLEND);
267                         GlobalOpenGL().m_glGetIntegerv(GL_BLEND_SRC, &previous_blend_func_src);
268                         GlobalOpenGL().m_glGetIntegerv(GL_BLEND_DST, &previous_blend_func_dst);
269                         GlobalOpenGL().m_glGetFloatv(GL_RED_BIAS, &previous_red_bias);
270                         GlobalOpenGL().m_glGetFloatv(GL_GREEN_BIAS, &previous_green_bias);
271                         GlobalOpenGL().m_glGetFloatv(GL_BLUE_BIAS, &previous_blue_bias);
272                         GlobalOpenGL().m_glGetFloatv(GL_ALPHA_SCALE, &previous_alpha_scale);
273
274                         GlobalOpenGL().m_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
275                         GlobalOpenGL().m_glEnable(GL_BLEND);
276                         GlobalOpenGL().m_glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
277                         GlobalOpenGL().m_glPixelTransferf(GL_RED_BIAS, color[0]);
278                         GlobalOpenGL().m_glPixelTransferf(GL_GREEN_BIAS, color[1]);
279                         GlobalOpenGL().m_glPixelTransferf(GL_BLUE_BIAS, color[2]);
280                         GlobalOpenGL().m_glPixelTransferf(GL_ALPHA_SCALE, color[3]);
281
282                         GlobalOpenGL().m_glDrawPixels(bitmap.width, bitmap.rows,
283                                         GL_ALPHA, GL_UNSIGNED_BYTE, begin_bitmap_buffer);
284                         g_free(begin_bitmap_buffer);
285
286                         // Restore state in reverse order of how we set it.
287                         GlobalOpenGL().m_glPixelTransferf(GL_ALPHA_SCALE, previous_alpha_scale);
288                         GlobalOpenGL().m_glPixelTransferf(GL_BLUE_BIAS, previous_blue_bias);
289                         GlobalOpenGL().m_glPixelTransferf(GL_GREEN_BIAS, previous_green_bias);
290                         GlobalOpenGL().m_glPixelTransferf(GL_RED_BIAS, previous_red_bias);
291                         GlobalOpenGL().m_glBlendFunc(previous_blend_func_src, previous_blend_func_dst);
292                         if (!previous_blend_enabled) { GlobalOpenGL().m_glDisable(GL_BLEND); }
293                         GlobalOpenGL().m_glPixelStorei(GL_UNPACK_ALIGNMENT, previous_unpack_alignment);
294                 }
295
296                 g_object_unref(G_OBJECT(layout));
297         }
298 };