1 // PANGOFONT_WCL.CPP
2
3 // Copyright (C) 2008 Tommi Hassinen, Naosumi Yasufuku.
4
5 // This package is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9
10 // This package is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with this package; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 /*################################################################################################*/
20
21 #include "pangofont_wcl.h"
22
23 #include "local_i18n.h"
24 #include <ghemical/notice.h>
25
26 #include <gtk/gtkgl.h>
27 #include <cstdlib>
28 #include <string.h>
29
30 #include <cstring>
31 #include <cstdlib>
32 using namespace std;
33
34 /*################################################################################################*/
35
pangofont_wcl(ogl_camera * cam)36 pangofont_wcl::pangofont_wcl(ogl_camera * cam) :
37 base_wcl(cam)
38 {
39 font_string = NULL;
40 font_height = 0;
41
42 font_list_base = 0;
43 }
44
~pangofont_wcl(void)45 pangofont_wcl::~pangofont_wcl(void)
46 {
47 if (font_string != NULL)
48 {
49 g_free(font_string);
50 font_string = NULL;
51 }
52 }
53
ogl_InitPangoFont(const gchar * fs)54 void pangofont_wcl::ogl_InitPangoFont(const gchar * fs)
55 {
56 if (!fs)
57 {
58 assertion_failed(__FILE__, __LINE__, "bad font string.");
59 }
60
61 if (font_string != NULL)
62 {
63 cout << "WARNING : pangofont_wcl::ogl_InitPangoFont() is already called." << endl;
64 return;
65 }
66
67 font_string = g_strdup(fs);
68
69 // generate font display lists.
70
71 font_list_base = glGenLists(128);
72
73 PangoFontDescription * font_desc = pango_font_description_from_string(font_string);
74 PangoFont * font = gdk_gl_font_use_pango_font(font_desc, 0, 128, font_list_base);
75 if (font == NULL)
76 {
77 g_print(_("*** ERROR : Can't load font '%s'\n"), font_string);
78 exit(EXIT_FAILURE);
79 }
80
81 PangoFontMetrics * font_metrics = pango_font_get_metrics(font, NULL);
82
83 font_height = pango_font_metrics_get_ascent(font_metrics) + pango_font_metrics_get_descent(font_metrics);
84 font_height = PANGO_PIXELS(font_height);
85
86 pango_font_description_free(font_desc);
87 pango_font_metrics_unref(font_metrics);
88 }
89
ogl_GetStringWidth(const char * str)90 int pangofont_wcl::ogl_GetStringWidth(const char * str)
91 {
92 int width = 0;
93
94 unsigned int count = 0;
95 while (count < strlen(str))
96 {
97 width += font_height / 2; // how to do this correctly???
98 count++;
99 }
100
101 return width;
102 }
103
ogl_WriteString2D(const char * str,GLfloat x,GLfloat y)104 void pangofont_wcl::ogl_WriteString2D(const char * str, GLfloat x, GLfloat y)
105 {
106 glPushMatrix();
107 glLoadIdentity();
108
109 glMatrixMode(GL_PROJECTION);
110 glPushMatrix(); glLoadIdentity();
111 gluOrtho2D(0, GetWnd()->GetWidth(), 0, GetWnd()->GetHeight());
112
113 ogl_WriteString3D(str, x, y, 0.0);
114
115 glPopMatrix();
116 glMatrixMode(GL_MODELVIEW);
117
118 glPopMatrix();
119 }
120
ogl_WriteString3D(const char * str,GLfloat x,GLfloat y,GLfloat z)121 void pangofont_wcl::ogl_WriteString3D(const char * str, GLfloat x, GLfloat y, GLfloat z)
122 {
123 glDisable(GL_DEPTH_TEST);
124
125 glRasterPos3f(x, y, z);
126
127 unsigned int count = 0;
128 while (count < strlen(str))
129 {
130 glCallList(font_list_base + str[count++]);
131 }
132
133 glEnable(GL_DEPTH_TEST);
134 }
135
136 /*################################################################################################*/
137
138 // eof
139