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