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