]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/glfont.cpp
reformat code! now the code is only ugly on the *inside*
[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 "globaldefs.h"
24 #include "igl.h"
25
26 // generic string printing with call lists
27 class GLFontCallList : public GLFont {
28     GLuint m_displayList;
29     int m_pixelHeight;
30     int m_pixelAscent;
31     int m_pixelDescent;
32 public:
33     GLFontCallList(GLuint displayList, int asc, int desc, int pixelHeight) : m_displayList(displayList),
34                                                                              m_pixelHeight(pixelHeight),
35                                                                              m_pixelAscent(asc), m_pixelDescent(desc)
36     {
37     }
38
39     virtual ~GLFontCallList()
40     {
41         glDeleteLists(m_displayList, 256);
42     }
43
44     void printString(const char *s)
45     {
46         GlobalOpenGL().m_glListBase(m_displayList);
47         GlobalOpenGL().m_glCallLists(GLsizei(strlen(s)), GL_UNSIGNED_BYTE, reinterpret_cast<const GLubyte *>( s ));
48     }
49
50     virtual int getPixelAscent() const
51     {
52         return m_pixelAscent;
53     }
54
55     virtual int getPixelDescent() const
56     {
57         return m_pixelDescent;
58     }
59
60     virtual int getPixelHeight() const
61     {
62         return m_pixelHeight;
63     }
64 };
65
66 #if GDEF_OS_WINDOWS
67 #include <windows.h>
68 #endif
69
70 #include "debugging/debugging.h"
71
72 // LordHavoc: this is code for direct Xlib bitmap character fetching, as an
73 // alternative to requiring gtkglarea, it was created due to a lack of this
74 // package on SuSE 9.x but this package is now commonly shipping in Linux
75 // distributions so this code may be unnecessary, feel free however to enable
76 // it when building packages for distros that do not ship with that package,
77 // or if you just prefer less dependencies...
78 #if 0
79
80 #include <X11/Xlib.h>
81 #include <gdk/gdkx.h>
82 #include <GL/glx.h>
83
84 GLFont *glfont_create( const char* font_string ){
85     GLuint font_list_base;
86     XFontStruct *fontInfo;
87     Display *dpy = GDK_DISPLAY();
88     unsigned int i, first, last, firstrow, lastrow;
89     int maxchars;
90     int firstbitmap;
91
92     fontInfo = XLoadQueryFont( dpy, "-*-fixed-*-*-*-*-8-*-*-*-*-*-*-*" );
93     if ( fontInfo == NULL ) {
94         // try to load other fonts
95         fontInfo = XLoadQueryFont( dpy, "-*-fixed-*-*-*-*-*-*-*-*-*-*-*-*" );
96
97         // any font will do !
98         if ( fontInfo == NULL ) {
99             fontInfo = XLoadQueryFont( dpy, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
100         }
101
102         if ( fontInfo == NULL ) {
103             ERROR_MESSAGE( "couldn't create font" );
104         }
105     }
106
107     first = (int)fontInfo->min_char_or_byte2;
108     last = (int)fontInfo->max_char_or_byte2;
109     firstrow = (int)fontInfo->min_byte1;
110     lastrow = (int)fontInfo->max_byte1;
111     /*
112      * How many chars in the charset
113      */
114     maxchars = 256 * lastrow + last;
115     font_list_base = glGenLists( maxchars + 1 );
116     if ( font_list_base == 0 ) {
117         ERROR_MESSAGE( "couldn't create font" );
118     }
119
120     /*
121      * Get offset to first char in the charset
122      */
123     firstbitmap = 256 * firstrow + first;
124     /*
125      * for each row of chars, call glXUseXFont to build the bitmaps.
126      */
127
128     for ( i = firstrow; i <= lastrow; i++ )
129     {
130         glXUseXFont( fontInfo->fid, firstbitmap, last - first + 1, font_list_base + firstbitmap );
131         firstbitmap += 256;
132     }
133
134 /*    *height = fontInfo->ascent + fontInfo->descent;
135  *width = fontInfo->max_bounds.width;  */
136     return new GLFontCallList( font_list_base, fontInfo->ascent, fontInfo->descent, fontInfo->ascent + fontInfo->descent );
137 }
138
139 #elif 0
140
141 #include <gtk/gtkglwidget.h>
142
143 GLFont *glfont_create( const char* font_string ){
144     GLuint font_list_base = glGenLists( 256 );
145     gint font_height = 0, font_ascent = 0, font_descent = 0;
146
147     PangoFontDescription* font_desc = pango_font_description_from_string( font_string );
148
149     PangoFont* font = gdk_gl_font_use_pango_font( font_desc, 0, 256, font_list_base );
150
151     if ( font == 0 ) {
152         pango_font_description_free( font_desc );
153         font_desc = pango_font_description_from_string( "fixed 8" );
154         font = gdk_gl_font_use_pango_font( font_desc, 0, 256, font_list_base );
155     }
156
157     if ( font == 0 ) {
158         pango_font_description_free( font_desc );
159         font_desc = pango_font_description_from_string( "courier new 8" );
160         font = gdk_gl_font_use_pango_font( font_desc, 0, 256, font_list_base );
161     }
162
163     if ( font != 0 ) {
164         PangoFontMetrics* font_metrics = pango_font_get_metrics( font, 0 );
165
166         font_ascent = pango_font_metrics_get_ascent( font_metrics );
167         font_descent = pango_font_metrics_get_descent( font_metrics );
168         font_height = font_ascent + font_descent;
169
170         font_ascent = PANGO_PIXELS( font_ascent );
171         font_descent = PANGO_PIXELS( font_descent );
172         font_height = PANGO_PIXELS( font_height );
173
174         pango_font_metrics_unref( font_metrics );
175     }
176
177     pango_font_description_free( font_desc );
178
179     // fix for pango/gtkglext metrix bug
180     if ( font_height > 256 ) {
181         font_height = 16;
182     }
183
184     return new GLFontCallList( font_list_base, font_ascent, font_descent, font_height );
185 }
186 #else
187
188 // new font code ripped from ZeroRadiant
189
190 #include <pango/pangoft2.h>
191 #include <pango/pango-features.h>
192 #include <pango/pango-utils.h>
193
194 class GLFontInternal : public GLFont {
195     const char *font_string;
196     int font_height;
197     int font_ascent;
198     int font_descent;
199     int y_offset_bitmap_render_pango_units;
200     PangoContext *ft2_context;
201     PangoFontMap *fontmap;
202
203 public:
204     GLFontInternal(const char *_font_string) : font_string(_font_string)
205     {
206         PangoFontDescription *font_desc;
207         PangoLayout *layout;
208         PangoRectangle log_rect;
209         int font_ascent_pango_units;
210         int font_descent_pango_units;
211
212 #if !PANGO_VERSION_CHECK(1, 22, 0)
213         ft2_context = pango_ft2_get_context( 72, 72 );
214 #else
215         fontmap = pango_ft2_font_map_new();
216         pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(fontmap), 72, 72);
217         ft2_context = pango_font_map_create_context(fontmap);
218 #endif
219
220         font_desc = pango_font_description_from_string(font_string);
221         //pango_font_description_set_size(font_desc, 10 * PANGO_SCALE);
222         pango_context_set_font_description(ft2_context, font_desc);
223         pango_font_description_free(font_desc);
224         // TODO fallback to fixed 8, courier new 8
225
226         layout = pango_layout_new(ft2_context);
227
228 #ifdef FONT_SIZE_WORKAROUND
229         pango_layout_set_width( layout, -1 );   // -1 no wrapping.  All text on one line.
230         pango_layout_set_text( layout, "The quick brown fox jumped over the lazy sleeping dog's back then sat on a tack.", -1 );   // -1 null-terminated string.
231 #endif
232
233 #if !PANGO_VERSION_CHECK(1, 22, 0)
234         PangoLayoutIter *iter;
235         iter = pango_layout_get_iter( layout );
236         font_ascent_pango_units = pango_layout_iter_get_baseline( iter );
237         pango_layout_iter_free( iter );
238 #else
239         font_ascent_pango_units = pango_layout_get_baseline(layout);
240 #endif
241         pango_layout_get_extents(layout, NULL, &log_rect);
242         g_object_unref(G_OBJECT(layout));
243         font_descent_pango_units = log_rect.height - font_ascent_pango_units;
244
245         font_ascent = PANGO_PIXELS_CEIL(font_ascent_pango_units);
246         font_descent = PANGO_PIXELS_CEIL(font_descent_pango_units);
247         font_height = font_ascent + font_descent;
248         y_offset_bitmap_render_pango_units = (font_ascent * PANGO_SCALE) - font_ascent_pango_units;
249     }
250
251     virtual ~GLFontInternal()
252     {
253         g_object_unref(G_OBJECT(ft2_context));
254         g_object_unref(G_OBJECT(fontmap));
255     }
256
257 // Renders the input text at the current location with the current color.
258 // The X position of the current location is used to place the left edge of the text image,
259 // where the text image bounds are defined as the logical extents of the line of text.
260 // The Y position of the current location is used to place the bottom of the text image.
261 // You should offset the Y position by the amount returned by gtk_glwidget_font_descent()
262 // if you want to place the baseline of the text image at the current Y position.
263 // Note: A problem with this function is that if the lower left corner of the text falls
264 // just a hair outside of the viewport (meaning the current raster position is invalid),
265 // then no text will be rendered.  The solution to this is a very hacky one.  You can search
266 // Google for "glDrawPixels clipping".
267     virtual void printString(const char *s)
268     {
269         // The idea for this code initially came from the font-pangoft2.c example that comes with GtkGLExt.
270
271         PangoLayout *layout;
272         PangoRectangle log_rect;
273         FT_Bitmap bitmap;
274         unsigned char *begin_bitmap_buffer;
275         GLfloat color[4];
276         GLint previous_unpack_alignment;
277         GLboolean previous_blend_enabled;
278         GLint previous_blend_func_src;
279         GLint previous_blend_func_dst;
280         GLfloat previous_red_bias;
281         GLfloat previous_green_bias;
282         GLfloat previous_blue_bias;
283         GLfloat previous_alpha_scale;
284
285         layout = pango_layout_new(ft2_context);
286         pango_layout_set_width(layout, -1);   // -1 no wrapping.  All text on one line.
287         pango_layout_set_text(layout, s, -1);   // -1 null-terminated string.
288         pango_layout_get_extents(layout, NULL, &log_rect);
289
290         if (log_rect.width > 0 && log_rect.height > 0) {
291             bitmap.rows = font_ascent + font_descent;
292             bitmap.width = PANGO_PIXELS_CEIL(log_rect.width);
293             bitmap.pitch = -bitmap.width;     // Rendering it "upside down" for OpenGL.
294             begin_bitmap_buffer = (unsigned char *) g_malloc(bitmap.rows * bitmap.width);
295             memset(begin_bitmap_buffer, 0, bitmap.rows * bitmap.width);
296             bitmap.buffer = begin_bitmap_buffer + (bitmap.rows - 1) * bitmap.width;   // See pitch above.
297             bitmap.num_grays = 0xff;
298             bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
299             pango_ft2_render_layout_subpixel(&bitmap, layout, -log_rect.x,
300                                              y_offset_bitmap_render_pango_units);
301             GlobalOpenGL().m_glGetFloatv(GL_CURRENT_COLOR, color);
302
303             // Save state.  I didn't see any OpenGL push/pop operations for these.
304             // Question: Is saving/restoring this state necessary?  Being safe.
305             GlobalOpenGL().m_glGetIntegerv(GL_UNPACK_ALIGNMENT, &previous_unpack_alignment);
306             previous_blend_enabled = GlobalOpenGL().m_glIsEnabled(GL_BLEND);
307             GlobalOpenGL().m_glGetIntegerv(GL_BLEND_SRC, &previous_blend_func_src);
308             GlobalOpenGL().m_glGetIntegerv(GL_BLEND_DST, &previous_blend_func_dst);
309             GlobalOpenGL().m_glGetFloatv(GL_RED_BIAS, &previous_red_bias);
310             GlobalOpenGL().m_glGetFloatv(GL_GREEN_BIAS, &previous_green_bias);
311             GlobalOpenGL().m_glGetFloatv(GL_BLUE_BIAS, &previous_blue_bias);
312             GlobalOpenGL().m_glGetFloatv(GL_ALPHA_SCALE, &previous_alpha_scale);
313
314             GlobalOpenGL().m_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
315             GlobalOpenGL().m_glEnable(GL_BLEND);
316             GlobalOpenGL().m_glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
317             GlobalOpenGL().m_glPixelTransferf(GL_RED_BIAS, color[0]);
318             GlobalOpenGL().m_glPixelTransferf(GL_GREEN_BIAS, color[1]);
319             GlobalOpenGL().m_glPixelTransferf(GL_BLUE_BIAS, color[2]);
320             GlobalOpenGL().m_glPixelTransferf(GL_ALPHA_SCALE, color[3]);
321
322             GlobalOpenGL().m_glDrawPixels(bitmap.width, bitmap.rows,
323                                           GL_ALPHA, GL_UNSIGNED_BYTE, begin_bitmap_buffer);
324             g_free(begin_bitmap_buffer);
325
326             // Restore state in reverse order of how we set it.
327             GlobalOpenGL().m_glPixelTransferf(GL_ALPHA_SCALE, previous_alpha_scale);
328             GlobalOpenGL().m_glPixelTransferf(GL_BLUE_BIAS, previous_blue_bias);
329             GlobalOpenGL().m_glPixelTransferf(GL_GREEN_BIAS, previous_green_bias);
330             GlobalOpenGL().m_glPixelTransferf(GL_RED_BIAS, previous_red_bias);
331             GlobalOpenGL().m_glBlendFunc(previous_blend_func_src, previous_blend_func_dst);
332             if (!previous_blend_enabled) {
333                 GlobalOpenGL().m_glDisable(GL_BLEND);
334             }
335             GlobalOpenGL().m_glPixelStorei(GL_UNPACK_ALIGNMENT, previous_unpack_alignment);
336         }
337
338         g_object_unref(G_OBJECT(layout));
339     }
340
341     virtual int getPixelAscent() const
342     {
343         return font_ascent;
344     }
345
346     virtual int getPixelDescent() const
347     {
348         return font_descent;
349     }
350
351     virtual int getPixelHeight() const
352     {
353         return font_height;
354     }
355 };
356
357 GLFont *glfont_create(const char *font_string)
358 {
359     return new GLFontInternal(font_string);
360 }
361
362 #endif