]> icculus.org git repositories - divverent/netradiant.git/blob - libs/gtkutil/glfont.cpp
pango font stuff: remove reference to a deprectaed pango interface from the new ZeroR...
[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 <pango/pango-utils.h>
167 #include "igl.h"
168
169 class GLFontInternal
170 {
171         const char *font_string;
172         int font_height;
173         int font_ascent;
174         int font_descent;
175         int y_offset_bitmap_render_pango_units;
176         PangoContext *ft2_context;
177         PangoFontMap *fontmap;
178
179         public:
180         GLFontInternal(const char *_font_string): font_string(_font_string)
181         {
182                 PangoFontDescription *font_desc;
183                 PangoLayout *layout;
184                 PangoRectangle log_rect;
185                 int font_ascent_pango_units;
186                 int font_descent_pango_units;
187
188                 //ft2_context = pango_ft2_get_context(72, 72);
189                 fontmap = pango_ft2_font_map_new();
190                 pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(fontmap), 72, 72);
191                 ft2_context = pango_font_map_create_context(fontmap);
192
193                 font_desc = pango_font_description_from_string(font_string);
194                 //pango_font_description_set_size(font_desc, 10 * PANGO_SCALE);
195                 pango_context_set_font_description(ft2_context, font_desc);
196                 pango_font_description_free(font_desc);
197                 // TODO fallback to fixed 8, courier new 8
198
199                 layout = pango_layout_new(ft2_context);
200 #if !PANGO_VERSION_CHECK(1,22,0)
201                 PangoLayoutIter *iter;  
202                 iter = pango_layout_get_iter(layout);
203                 font_ascent_pango_units = pango_layout_iter_get_baseline(iter);
204                 pango_layout_iter_free(iter);
205 #else
206                 font_ascent_pango_units = pango_layout_get_baseline(layout);
207 #endif
208                 pango_layout_get_extents(layout, NULL, &log_rect);
209                 g_object_unref(G_OBJECT(layout));
210                 font_descent_pango_units = log_rect.height - font_ascent_pango_units;
211
212                 font_ascent = PANGO_PIXELS_CEIL(font_ascent_pango_units);
213                 font_descent = PANGO_PIXELS_CEIL(font_descent_pango_units);
214                 font_height = font_ascent + font_descent;
215                 y_offset_bitmap_render_pango_units = (font_ascent * PANGO_SCALE) - font_ascent_pango_units;
216         }
217
218         ~GLFontInternal()
219         {
220                 g_object_unref(G_OBJECT(ft2_context));
221                 g_object_unref(G_OBJECT(fontmap));
222         }
223
224         // Renders the input text at the current location with the current color.
225         // The X position of the current location is used to place the left edge of the text image,
226         // where the text image bounds are defined as the logical extents of the line of text.
227         // The Y position of the current location is used to place the bottom of the text image.
228         // You should offset the Y position by the amount returned by gtk_glwidget_font_descent()
229         // if you want to place the baseline of the text image at the current Y position.
230         // Note: A problem with this function is that if the lower left corner of the text falls
231         // just a hair outside of the viewport (meaning the current raster position is invalid),
232         // then no text will be rendered.  The solution to this is a very hacky one.  You can search
233         // Google for "glDrawPixels clipping".
234         void printString(const char *s)
235         {
236                 // The idea for this code initially came from the font-pangoft2.c example that comes with GtkGLExt.
237
238                 PangoLayout *layout;
239                 PangoRectangle log_rect;
240                 FT_Bitmap bitmap;
241                 unsigned char *begin_bitmap_buffer;
242                 GLfloat color[4];
243                 GLint previous_unpack_alignment;
244                 GLboolean previous_blend_enabled;
245                 GLint previous_blend_func_src;
246                 GLint previous_blend_func_dst;
247                 GLfloat previous_red_bias;
248                 GLfloat previous_green_bias;
249                 GLfloat previous_blue_bias;
250                 GLfloat previous_alpha_scale;
251
252                 layout = pango_layout_new(ft2_context);
253                 pango_layout_set_width(layout, -1); // -1 no wrapping.  All text on one line.
254                 pango_layout_set_text(layout, s, -1); // -1 null-terminated string.
255                 pango_layout_get_extents(layout, NULL, &log_rect);
256
257                 if (log_rect.width > 0 && log_rect.height > 0) {
258                         bitmap.rows = font_ascent + font_descent;
259                         bitmap.width = PANGO_PIXELS_CEIL(log_rect.width);
260                         bitmap.pitch = -bitmap.width; // Rendering it "upside down" for OpenGL.
261                         begin_bitmap_buffer = (unsigned char *) g_malloc(bitmap.rows * bitmap.width);
262                         memset(begin_bitmap_buffer, 0, bitmap.rows * bitmap.width);
263                         bitmap.buffer = begin_bitmap_buffer + (bitmap.rows - 1) * bitmap.width; // See pitch above.
264                         bitmap.num_grays = 0xff;
265                         bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
266                         pango_ft2_render_layout_subpixel(&bitmap, layout, -log_rect.x,
267                                         y_offset_bitmap_render_pango_units);
268                         GlobalOpenGL().m_glGetFloatv(GL_CURRENT_COLOR, color);
269
270                         // Save state.  I didn't see any OpenGL push/pop operations for these.
271                         // Question: Is saving/restoring this state necessary?  Being safe.
272                         GlobalOpenGL().m_glGetIntegerv(GL_UNPACK_ALIGNMENT, &previous_unpack_alignment);
273                         previous_blend_enabled = GlobalOpenGL().m_glIsEnabled(GL_BLEND);
274                         GlobalOpenGL().m_glGetIntegerv(GL_BLEND_SRC, &previous_blend_func_src);
275                         GlobalOpenGL().m_glGetIntegerv(GL_BLEND_DST, &previous_blend_func_dst);
276                         GlobalOpenGL().m_glGetFloatv(GL_RED_BIAS, &previous_red_bias);
277                         GlobalOpenGL().m_glGetFloatv(GL_GREEN_BIAS, &previous_green_bias);
278                         GlobalOpenGL().m_glGetFloatv(GL_BLUE_BIAS, &previous_blue_bias);
279                         GlobalOpenGL().m_glGetFloatv(GL_ALPHA_SCALE, &previous_alpha_scale);
280
281                         GlobalOpenGL().m_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
282                         GlobalOpenGL().m_glEnable(GL_BLEND);
283                         GlobalOpenGL().m_glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
284                         GlobalOpenGL().m_glPixelTransferf(GL_RED_BIAS, color[0]);
285                         GlobalOpenGL().m_glPixelTransferf(GL_GREEN_BIAS, color[1]);
286                         GlobalOpenGL().m_glPixelTransferf(GL_BLUE_BIAS, color[2]);
287                         GlobalOpenGL().m_glPixelTransferf(GL_ALPHA_SCALE, color[3]);
288
289                         GlobalOpenGL().m_glDrawPixels(bitmap.width, bitmap.rows,
290                                         GL_ALPHA, GL_UNSIGNED_BYTE, begin_bitmap_buffer);
291                         g_free(begin_bitmap_buffer);
292
293                         // Restore state in reverse order of how we set it.
294                         GlobalOpenGL().m_glPixelTransferf(GL_ALPHA_SCALE, previous_alpha_scale);
295                         GlobalOpenGL().m_glPixelTransferf(GL_BLUE_BIAS, previous_blue_bias);
296                         GlobalOpenGL().m_glPixelTransferf(GL_GREEN_BIAS, previous_green_bias);
297                         GlobalOpenGL().m_glPixelTransferf(GL_RED_BIAS, previous_red_bias);
298                         GlobalOpenGL().m_glBlendFunc(previous_blend_func_src, previous_blend_func_dst);
299                         if (!previous_blend_enabled) { GlobalOpenGL().m_glDisable(GL_BLEND); }
300                         GlobalOpenGL().m_glPixelStorei(GL_UNPACK_ALIGNMENT, previous_unpack_alignment);
301                 }
302
303                 g_object_unref(G_OBJECT(layout));
304         }
305 };