Debian Codegraph @

ThirdParty/GL/glh/glh_text.h

raw
1 /*
2 glh - is a platform-indepenedent C++ OpenGL helper library
3
4
5 Copyright (c) 2000 Cass Everitt
6 Copyright (c) 2000 NVIDIA Corporation
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or
10 without modification, are permitted provided that the following
11 conditions are met:
12
13 * Redistributions of source code must retain the above
14 copyright notice, this list of conditions and the following
15 disclaimer.
16
17 * Redistributions in binary form must reproduce the above
18 copyright notice, this list of conditions and the following
19 disclaimer in the documentation and/or other materials
20 provided with the distribution.
21
22 * The names of contributors to this software may not be used
23 to endorse or promote products derived from this software
24 without specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
38
39
40 Cass Everitt - [email protected]
41 */
42
43 #ifndef GLH_TEXT_H
44 #define GLH_TEXT_H
45
46 #include <string>
47
48 #ifdef _WIN32
49 # include <windows.h>
50 #endif
51
52 #ifdef MACOS
53 #include <OpenGL/gl.h>
54 #else
55 #include <GL/gl.h>
56 #endif
57
58 #include <glh/glh_obs.h>
59
60 namespace glh
61 {
62
63 struct font
64 {
65 // build display lists and the like
66 virtual void initialize() = 0;
67 // get font metrics
68 virtual float get_ascent() = 0;
69 virtual float get_descent() = 0;
70 virtual float get_width(int i) = 0;
71
72 // draw
73 virtual void render(int i) = 0;
74 };
75
76 inline float string_width(font * f, string text)
77 {
78 float w = 0;
79 for(unsigned int i=0; i < text.size(); i++)
80 {
81 w += f->get_width(text[i]);
82 }
83 return w;
84 }
85 // skip a line
86 inline void next_line(font * f)
87 {
88 glTranslatef(0, - (f->get_ascent() + f->get_descent()), 0);
89 }
90
91 // renders text horizontally
92 inline void render_single_line(font * f, string text)
93 {
94 glPushMatrix();
95 for(unsigned int i=0; i < text.size(); i++)
96 {
97 f->render(text[i]);
98 }
99 glPopMatrix();
100 }
101
102
103 // render text on multiple lines (based on newlines)
104 struct simple_multi_line_text
105 {
106 public:
107 simple_multi_line_text() : f(0), line_spacing(1.1f) {}
108
109
110 void render()
111 {
112 if(!f) return;
113 if(dirty)
114 {
115 dirty = false;
116 dl.new_list(GL_COMPILE);
117 make_render_calls();
118 dl.end_list();
119 }
120 dl.call_list();
121 }
122 void get_dimensions(float & width, float & height)
123 {
124 if(!f) return;
125 if(dirty)
126 {
127 dirty = false;
128 dl.new_list(GL_COMPILE);
129 make_render_calls();
130 dl.end_list();
131 }
132 width = w;
133 height = h;
134 }
135 void set_font(font * new_font)
136 {
137 dirty = true;
138 f = new_font;
139 }
140 void set_text(const string & new_text)
141 {
142 dirty = true;
143 text = new_text;
144 }
145 font * get_font() { return f; }
146 const string & get_text() { return text; }
147 private:
148 // skip a line
149 void next_line()
150 {
151 glTranslatef(0, - (f->get_ascent() + f->get_descent()) * line_spacing, 0);
152 }
153
154 void make_render_calls()
155 {
156 w = h = 0;
157 float curr_width = 0;
158 float font_height = f->get_ascent() + f->get_descent();
159 int lines = 0;
160
161 if(text.size() != 0) lines++;
162
163 glPushMatrix(); // vertical translation
164 glPushMatrix(); // horizontal translation
165 for(unsigned int i=0; i < text.size(); i++)
166 {
167 if(text[i] == '\n')
168 {
169 lines++;
170 if(curr_width > w) w = curr_width;
171 curr_width = 0;
172
173 glPopMatrix(); // back to beginning of line
174 next_line(); // translate futher down
175 glPushMatrix();
176 }
177 f->render(text[i]);
178 curr_width += f->get_width(text[i]);
179 }
180 glPopMatrix();
181 glPopMatrix();
182 if(curr_width > w) w = curr_width;
183 h = lines * font_height * line_spacing;
184 }
185
186 font * f;
187 string text;
188 display_list dl;
189 float w, h;
190 bool dirty;
191 float line_spacing;
192 };
193
194
195 }
196
197 #endif