2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
27 #include "debugging/debugging.h"
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...
41 GLFont glfont_create(const char* font_string)
43 GLuint font_list_base;
44 XFontStruct *fontInfo;
45 Display *dpy = GDK_DISPLAY ();
46 unsigned int i, first, last, firstrow, lastrow;
50 fontInfo = XLoadQueryFont (dpy, "-*-fixed-*-*-*-*-8-*-*-*-*-*-*-*");
53 // try to load other fonts
54 fontInfo = XLoadQueryFont (dpy, "-*-fixed-*-*-*-*-*-*-*-*-*-*-*-*");
58 fontInfo = XLoadQueryFont(dpy, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
61 ERROR_MESSAGE("couldn't create font");
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;
69 * How many chars in the charset
71 maxchars = 256 * lastrow + last;
72 font_list_base = glGenLists(maxchars+1);
73 if (font_list_base == 0)
75 ERROR_MESSAGE( "couldn't create font" );
79 * Get offset to first char in the charset
81 firstbitmap = 256 * firstrow + first;
83 * for each row of chars, call glXUseXFont to build the bitmaps.
86 for(i=firstrow; i<=lastrow; i++)
88 glXUseXFont(fontInfo->fid, firstbitmap, last-first+1, font_list_base+firstbitmap);
92 /* *height = fontInfo->ascent + fontInfo->descent;
93 *width = fontInfo->max_bounds.width; */
94 return GLFont(font_list_base, fontInfo->ascent + fontInfo->descent);
97 void glfont_release(GLFont& font)
99 glDeleteLists(font.getDisplayList(), 256);
105 #include <gtk/gtkglwidget.h>
107 GLFont glfont_create(const char* font_string)
109 GLuint font_list_base = glGenLists (256);
110 gint font_height = 0, font_ascent = 0, font_descent = 0;
112 PangoFontDescription* font_desc = pango_font_description_from_string (font_string);
114 PangoFont* font = gdk_gl_font_use_pango_font (font_desc, 0, 256, font_list_base);
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);
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);
132 PangoFontMetrics* font_metrics = pango_font_get_metrics (font, 0);
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;
138 font_ascent = PANGO_PIXELS (font_ascent);
139 font_descent = PANGO_PIXELS (font_descent);
140 font_height = PANGO_PIXELS (font_height);
142 pango_font_metrics_unref (font_metrics);
145 pango_font_description_free (font_desc);
147 // fix for pango/gtkglext metrix bug
148 if(font_height > 256)
151 return GLFont(font_list_base, font_ascent, font_descent, font_height);
154 void glfont_release(GLFont& font)
156 glDeleteLists(font.getDisplayList(), 256);
157 font = GLFont(0, 0, 0, 0);
163 // new font code ripped from ZeroRadiant (not in use yet)
165 #include <pango/pangoft2.h>
170 const char *font_string;
174 int y_offset_bitmap_render_pango_units;
175 PangoContext *ft2_context;
178 GLFontInternal(const char *_font_string): font_string(font_string)
180 PangoFontDescription *font_desc;
182 PangoRectangle log_rect;
183 int font_ascent_pango_units;
184 int font_descent_pango_units;
186 // This call is deprecated so we'll have to fix it sometime.
187 ft2_context = pango_ft2_get_context(72, 72);
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);
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);
200 font_ascent_pango_units = pango_layout_get_baseline(layout);
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;
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;
214 g_object_unref(G_OBJECT(ft2_context));
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)
229 // The idea for this code initially came from the font-pangoft2.c example that comes with GtkGLExt.
232 PangoRectangle log_rect;
234 unsigned char *begin_bitmap_buffer;
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;
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);
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);
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);
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]);
282 GlobalOpenGL().m_glDrawPixels(bitmap.width, bitmap.rows,
283 GL_ALPHA, GL_UNSIGNED_BYTE, begin_bitmap_buffer);
284 g_free(begin_bitmap_buffer);
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);
296 g_object_unref(G_OBJECT(layout));