1 /*   EXTRAITS DE LA LICENCE
2 	Copyright CEA, contributeurs : Luc BILLARD et Damien
3 	CALISTE, laboratoire L_Sim, (2001-2005)
4 
5 	Adresse mèl :
6 	BILLARD, non joignable par mèl ;
7 	CALISTE, damien P caliste AT cea P fr.
8 
9 	Ce logiciel est un programme informatique servant à visualiser des
10 	structures atomiques dans un rendu pseudo-3D.
11 
12 	Ce logiciel est régi par la licence CeCILL soumise au droit français et
13 	respectant les principes de diffusion des logiciels libres. Vous pouvez
14 	utiliser, modifier et/ou redistribuer ce programme sous les conditions
15 	de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
16 	sur le site "http://www.cecill.info".
17 
18 	Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
19 	pris connaissance de la licence CeCILL, et que vous en avez accepté les
20 	termes (cf. le fichier Documentation/licence.fr.txt fourni avec ce logiciel).
21 */
22 
23 /*   LICENCE SUM UP
24 	Copyright CEA, contributors : Luc BILLARD et Damien
25 	CALISTE, laboratoire L_Sim, (2001-2005)
26 
27 	E-mail address:
28 	BILLARD, not reachable any more ;
29 	CALISTE, damien P caliste AT cea P fr.
30 
31 	This software is a computer program whose purpose is to visualize atomic
32 	configurations in 3D.
33 
34 	This software is governed by the CeCILL  license under French law and
35 	abiding by the rules of distribution of free software.  You can  use,
36 	modify and/ or redistribute the software under the terms of the CeCILL
37 	license as circulated by CEA, CNRS and INRIA at the following URL
38 	"http://www.cecill.info".
39 
40 	The fact that you are presently reading this means that you have had
41 	knowledge of the CeCILL license and that you accept its terms. You can
42 	find a copy of this licence shipped with this software at Documentation/licence.en.txt.
43 */
44 #include "text.h"
45 
46 #include <string.h>
47 
48 #include <visu_tools.h>
49 #include <OSOpenGL/visu_openGL.h>
50 
51 #ifdef HAVE_FTGL
52 #include <FTGL/ftgl.h>
53 #endif
54 
55 /**
56  * SECTION:text
57  * @short_description: Enables capabilities to write some text on rendering screen.
58  *
59  * <para>For the moment, this module is very basic and the only fonts
60  * available is the helvetica 12 one. This module is currently broken
61  * under Windows.</para>
62  */
63 
64 static GLuint BASE;  /* for the font display list */
65 static GLuint SMALL; /* for the small font display list */
66 static gboolean textListHaveBeenBuilt = FALSE;
67 
68 #ifdef HAVE_FTGL
69 static FTGLfont *font = NULL;
70 #endif
71 static float fontSize = 32.f; /* only used in the case of FTGL. */
72 
_putGlText(const gchar * text,VisuGlTextSize size)73 static void _putGlText(const gchar *text, VisuGlTextSize size)
74 {
75   g_return_if_fail(textListHaveBeenBuilt);
76 
77   glPushAttrib(GL_LIST_BIT);
78   if (size == VISU_GL_TEXT_SMALL && SMALL > 0)
79     glListBase(SMALL);
80   else
81     glListBase(BASE);
82   glCallLists((int)strlen(text), GL_UNSIGNED_BYTE, (GLubyte *)text);
83   glPopAttrib();
84 }
85 static VisuGlTextFunc renderText = _putGlText;
86 
87 /**
88  * visu_gl_text_putTextWithFTGL:
89  * @text: the text to write.
90  * @size: the size.
91  *
92  * A #VisuGlTextFunc routine using FTGL to render text with Pixmap
93  * lists, see visu_gl_text_setFunc().
94  *
95  * Since: 3.7
96  **/
visu_gl_text_putTextWithFTGL(const gchar * text,VisuGlTextSize size)97 void visu_gl_text_putTextWithFTGL(const gchar *text, VisuGlTextSize size)
98 {
99   g_return_if_fail(textListHaveBeenBuilt);
100 
101 #ifdef HAVE_FTGL
102   if (size == VISU_GL_TEXT_NORMAL)
103     ftglSetFontFaceSize(font, fontSize, fontSize);
104   else
105     ftglSetFontFaceSize(font, fontSize * 0.75f, fontSize * 0.75f);
106   /* glGetFloatv(GL_CURRENT_COLOR, color); */
107   /* glPixelTransferf(GL_RED_BIAS, 255.f * color[0]); */
108   /* glPixelTransferf(GL_GREEN_BIAS, 255.f * color[1]); */
109   /* glPixelTransferf(GL_BLUE_BIAS, 255.f * color[2]); */
110   ftglRenderFont(font, text, FTGL_RENDER_ALL);
111   /* glPixelTransferf(GL_RED_BIAS, 0.f); */
112   /* glPixelTransferf(GL_GREEN_BIAS, 0.f); */
113   /* glPixelTransferf(GL_BLUE_BIAS, 0.f); */
114 #else
115   g_warning("FTGL backend not compiled, unable to write '%s' at size %d.", text, size);
116 #endif
117 }
118 
119 /**
120  * visu_gl_text_setFunc:
121  * @func: (scope call) (allow-none): a #VisuGlTextFunc function
122  *
123  * Set the function to render text at the raster position.
124  *
125  * Since: 3.7
126  *
127  * Returns: TRUE if the function is indeed changed.
128  **/
visu_gl_text_setFunc(VisuGlTextFunc func)129 gboolean visu_gl_text_setFunc(VisuGlTextFunc func)
130 {
131   if (func == renderText)
132     return FALSE;
133 
134   renderText = (func)?func:_putGlText;
135   return TRUE;
136 }
137 /**
138  * visu_gl_text_setFontSize:
139  * @size: a new size.
140  *
141  * Change the normal font size used by V_Sim (see
142  * #VISU_GL_TEXT_NORMAL). The small font is scaled accordingly. This is working only
143  * with the FTGL backend.
144  *
145  * Since: 3.7
146  *
147  * Returns: TRUE if font size is indeed changed.
148  **/
visu_gl_text_setFontSize(float size)149 gboolean visu_gl_text_setFontSize(float size)
150 {
151   if (size == fontSize)
152     return FALSE;
153 
154   fontSize = CLAMP(size, 12.f, 999.f);
155   return TRUE;
156 }
157 
158 /**
159  * visu_gl_text_drawChars:
160  * @s: a string.
161  * @size: the size of the text to render.
162  *
163  * Draw the given string on the current raster position with default
164  * font.
165  */
visu_gl_text_drawChars(const gchar * s,VisuGlTextSize size)166 void visu_gl_text_drawChars(const gchar *s, VisuGlTextSize size)
167 {
168   gsize nread, nwritten;
169   GError *error;
170   gchar *text;
171 
172   g_return_if_fail(s);
173 
174   DBG_fprintf(stderr, "OpenGL Text: put text '%s'.\n", s);
175   error = (GError*)0;
176   text = g_convert_with_fallback(s, -1, "iso-8859-1", "utf-8", "<?>",
177                                  &nread, &nwritten, &error);
178   if (text)
179     {
180       renderText(text, size);
181       g_free(text);
182     }
183   else
184     {
185       g_warning("%s", error->message);
186       g_error_free(error);
187     }
188 }
189 /**
190  * visu_gl_text_initFontList:
191  *
192  * Initialise the font drawing with default font (depending on system).
193  * It must be called before visu_gl_text_drawChars() and not in a glNewList().
194  * Can be called several times, fonts are initialized once only. Use
195  * visu_gl_text_rebuildFontList() to force to build a new font list.
196  */
visu_gl_text_initFontList()197 void visu_gl_text_initFontList()
198 {
199   if (textListHaveBeenBuilt)
200     return;
201 
202   BASE  = visu_gl_initFontList(18);
203   SMALL = visu_gl_initFontList(14);
204   textListHaveBeenBuilt = (BASE > 0);
205   DBG_fprintf(stderr, "OpenGL Text: creating font list: %d %d.\n", BASE, SMALL);
206   g_return_if_fail(BASE > 0 && SMALL > 0);
207 
208 #ifdef HAVE_FTGL
209   font = ftglCreatePixmapFont("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf");
210 #endif
211 }
212 /**
213  * visu_gl_text_rebuildFontList:
214  *
215  * Force to buid a new font list (for example new context has changed.
216  */
visu_gl_text_rebuildFontList()217 void visu_gl_text_rebuildFontList()
218 {
219   DBG_fprintf(stderr, "OpenGL Text: rebuilding font list.\n");
220   visu_gl_text_initFontList();
221 }
222 /**
223  * visu_gl_text_onNewContext:
224  *
225  * Set the flag for text list build to FALSE. It will force to rebuild
226  * the text lists at next call of visu_gl_text_initFontList().
227  *
228  * Since: 3.6
229  */
visu_gl_text_onNewContext()230 void visu_gl_text_onNewContext()
231 {
232   DBG_fprintf(stderr, "OpenGL Text: new context, forgetting fonts.\n");
233   textListHaveBeenBuilt = FALSE;
234 }
235