Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/post/src/fttext.cpp
3196 views
1
//-----------------------------------------------------------------------------------
2
// Filename: fttext.cpp
3
// Description: Provides FTGL text rendering functionality for ElmerPost
4
// Usage: fttext string [x y]
5
// ftfont font [size r g b]
6
// Compilation: export CFLAGS="-DHAVE_FTGL -I/usr/include/freetype2 -I/usr/include/FTGL"
7
// export CXXFLAGS="-DHAVE_FTGL -I/usr/include/freetype2 -I/usr/include/FTGL"
8
// export LIBS="-lfreetype -lftgl"
9
// Written by: Mikko Lyly
10
// Date: 15. Jan 2008
11
//-----------------------------------------------------------------------------------
12
#if defined(HAVE_CONFIG_H)
13
#include "../config.h"
14
#endif
15
16
#if defined(HAVE_FTGL_NEW) || defined(HAVE_FTGL_OLD)
17
18
#include <stdlib.h>
19
#include <stdio.h>
20
#include <GL/gl.h>
21
#include <tcl.h>
22
23
#if defined(WIN32) || defined(win32)
24
#include <windows.h>
25
#include "FTGL/FTGL.h"
26
#include "FTGL/FTGLPixmapFont.h"
27
#else
28
#if defined(HAVE_FTGL_NEW)
29
#include "FTGL/ftgl.h"
30
#else
31
#include "FTGL/FTGL.h"
32
#include "FTGL/FTGLPixmapFont.h"
33
#endif // HAVE_FTGL_NEW
34
#endif // WIN32 || win32
35
36
#define FTGLSTRLEN 4096
37
38
typedef struct {
39
double x, y;
40
int size;
41
double r, g, b;
42
int init_ok;
43
FTGLPixmapFont *Font;
44
char txt[FTGLSTRLEN];
45
char ttf[FTGLSTRLEN];
46
char current_ttf[FTGLSTRLEN];
47
char ttffile[FTGLSTRLEN];
48
} ftgl_t;
49
50
static ftgl_t ftgl;
51
52
extern "C" void (*user_hook_before_all)();
53
extern "C" void (*user_hook_after_all)();
54
55
static void FtInit() {
56
strcpy(ftgl.txt, "");
57
ftgl.x = -0.9;
58
ftgl.y = -0.9;
59
strcpy(ftgl.ttf, "FreeSans");
60
ftgl.size = 30;
61
ftgl.r = 1.0;
62
ftgl.g = 1.0;
63
ftgl.b = 1.0;
64
ftgl.init_ok = 1;
65
return;
66
}
67
68
extern "C" void FtRender() {
69
unsigned int r = strlen(ftgl.current_ttf);
70
unsigned int s = strlen(ftgl.ttf);
71
int t = strcmp(ftgl.ttf, ftgl.current_ttf);
72
73
if( (r!=s) || (t!=0) || ftgl.Font->Error() ) {
74
char *elmer_post_home = getenv("ELMER_POST_HOME");
75
fprintf(stdout, "fttext: getenv: ELMER_POST_HOME=%s\n",
76
elmer_post_home);
77
fflush(stdout);
78
79
#if defined(WIN32) || defined(win32)
80
sprintf(ftgl.ttffile, "%s\\fonts\\TrueType\\%s.ttf",
81
elmer_post_home, ftgl.ttf);
82
#else
83
sprintf(ftgl.ttffile, "%s/fonts/TrueType/%s.ttf",
84
elmer_post_home, ftgl.ttf);
85
#endif
86
87
fprintf(stdout, "fttext: load: %s\n", ftgl.ttffile);
88
fflush(stdout);
89
90
delete ftgl.Font;
91
ftgl.Font = new FTGLPixmapFont(ftgl.ttffile);
92
93
if(ftgl.Font->Error()) {
94
fprintf(stderr, "fttext: error: load font failed!\n");
95
fflush(stderr);
96
return;
97
}
98
memset(ftgl.current_ttf, 0, FTGLSTRLEN);
99
strncpy(ftgl.current_ttf, ftgl.ttf, strlen(ftgl.ttf));
100
}
101
102
if( ftgl.Font->Error() ) {
103
fprintf(stderr, "fttext: error: no font loaded!\n");
104
fflush(stderr);
105
return;
106
}
107
108
glMatrixMode(GL_MODELVIEW);
109
glPushMatrix();
110
glLoadIdentity();
111
glMatrixMode(GL_PROJECTION);
112
glPushMatrix();
113
glLoadIdentity();
114
115
glColor3f(ftgl.r, ftgl.g, ftgl.b);
116
glRasterPos3f(ftgl.x, ftgl.y, 0.0);
117
118
glDisable(GL_LIGHTING);
119
glDisable(GL_TEXTURE_1D);
120
121
ftgl.Font->FaceSize(ftgl.size);
122
ftgl.Font->Render(ftgl.txt);
123
124
glEnable(GL_TEXTURE_1D);
125
glEnable(GL_LIGHTING);
126
127
glPopMatrix();
128
glMatrixMode(GL_MODELVIEW);
129
glPopMatrix();
130
131
return;
132
}
133
134
extern "C" int FtFont(ClientData cl, Tcl_Interp *interp,
135
int argc, char **argv) {
136
if(!ftgl.init_ok)
137
FtInit();
138
139
if(argc > 1)
140
strcpy( ftgl.ttf, argv[1] );
141
142
if(argc > 2)
143
ftgl.size = atoi(argv[2]);
144
145
if(argc > 3)
146
ftgl.r = atof(argv[3]);
147
148
if(argc > 4)
149
ftgl.g = atof(argv[4]);
150
151
if(argc > 5)
152
ftgl.b = atof(argv[5]);
153
154
Tcl_Eval(interp, "display");
155
156
return TCL_OK;
157
}
158
159
extern "C" int FtText(ClientData cl, Tcl_Interp *interp,
160
int argc, char **argv) {
161
162
if(!ftgl.init_ok)
163
FtInit();
164
165
strcpy(ftgl.txt, "");
166
ftgl.x = -0.9;
167
ftgl.y = -0.9;
168
169
if(argc > 1) {
170
171
// TCL uses internally UTF-8. We want
172
// text in system default for FTGL:
173
//---------------------------------------
174
Tcl_DString ds;
175
Tcl_DStringInit(&ds);
176
char *res = Tcl_UtfToExternalDString(NULL, argv[1], -1, &ds);
177
strcpy(ftgl.txt, res);
178
Tcl_DStringFree(&ds);
179
}
180
181
if(argc > 2)
182
ftgl.x = atof(argv[2]);
183
184
if(argc > 3)
185
ftgl.y = atof(argv[3]);
186
187
user_hook_after_all = FtRender;
188
189
Tcl_Eval(interp, "display");
190
191
return TCL_OK;
192
}
193
194
#endif // HAVE_FTGL_NEW || HAVE_FTGL_OLD
195
196