]> git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/gtkgensurf/font.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / contrib / gtkgensurf / font.cpp
1 /*
2    GenSurf plugin for GtkRadiant
3    Copyright (C) 2001 David Hyde, Loki software and qeradiant.com
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library 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 GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 //
21 // Texture Font
22 //
23 // Taken from LeoCAD (www.leocad.org) and used in GtkGenSurf
24 // with permission from the author.
25 //
26 // Leonardo Zide (leo@lokigames.com)
27 //
28
29 #include <stdlib.h>
30 #include "gensurf.h"
31
32 static const unsigned char data[2048] = {
33         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
34         255, 207, 255, 255, 159, 255, 31, 255, 231, 159, 153, 63, 255, 255, 255, 255,
35         255, 207, 255, 255, 159, 255, 207, 255, 231, 159, 153, 63, 255, 255, 255, 255,
36         255, 207, 255, 255, 159, 255, 207, 255, 231, 255, 159, 63, 255, 255, 255, 255,
37         7, 78, 252, 240, 145, 135, 3, 71, 38, 158, 153, 51, 19, 227, 196, 255,
38         243, 140, 121, 230, 140, 51, 207, 51, 198, 156, 153, 57, 99, 204, 152, 255,
39         255, 204, 51, 111, 158, 121, 206, 121, 230, 153, 153, 60, 115, 206, 60, 255,
40         31, 204, 51, 127, 158, 121, 206, 121, 230, 153, 25, 62, 115, 206, 60, 255,
41         199, 204, 51, 127, 158, 1, 206, 121, 230, 153, 25, 62, 115, 206, 60, 255,
42         243, 204, 51, 127, 158, 249, 207, 121, 230, 153, 153, 60, 115, 206, 60, 255,
43         243, 204, 51, 111, 158, 249, 207, 121, 230, 153, 153, 57, 115, 206, 60, 255,
44         243, 140, 121, 230, 140, 115, 206, 51, 230, 153, 153, 51, 115, 206, 60, 255,
45         7, 73, 252, 240, 145, 7, 207, 71, 230, 153, 153, 39, 115, 206, 60, 255,
46         255, 255, 255, 255, 255, 255, 255, 127, 254, 255, 249, 255, 255, 255, 255, 255,
47         255, 255, 255, 255, 255, 255, 255, 127, 254, 255, 249, 255, 255, 255, 255, 255,
48         255, 255, 255, 255, 255, 255, 255, 57, 255, 255, 249, 255, 255, 255, 255, 255,
49         255, 255, 255, 255, 255, 255, 255, 131, 255, 255, 252, 255, 255, 255, 255, 255,
50         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
51         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
52         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
53         255, 255, 255, 255, 255, 255, 249, 255, 255, 255, 255, 255, 255, 255, 227, 255,
54         255, 255, 255, 255, 255, 255, 249, 255, 255, 255, 255, 255, 255, 255, 201, 255,
55         255, 255, 255, 255, 255, 255, 249, 255, 255, 255, 255, 255, 255, 255, 156, 255,
56         15, 79, 252, 200, 196, 96, 32, 79, 62, 252, 15, 15, 159, 192, 156, 255,
57         103, 142, 121, 198, 112, 206, 57, 79, 62, 60, 15, 15, 159, 207, 156, 255,
58         243, 204, 51, 207, 120, 254, 57, 207, 156, 57, 103, 102, 206, 231, 156, 255,
59         243, 204, 51, 207, 124, 252, 57, 207, 156, 25, 230, 112, 206, 231, 156, 255,
60         243, 204, 51, 207, 252, 224, 57, 207, 156, 25, 230, 121, 206, 243, 156, 255,
61         243, 204, 51, 207, 252, 199, 57, 207, 201, 211, 242, 240, 228, 249, 156, 255,
62         243, 204, 51, 207, 252, 207, 57, 207, 201, 195, 112, 230, 228, 249, 156, 255,
63         103, 142, 121, 198, 124, 206, 121, 198, 227, 231, 57, 207, 241, 252, 201, 255,
64         15, 79, 252, 200, 252, 224, 227, 200, 227, 231, 57, 207, 241, 192, 227, 255,
65         255, 207, 255, 207, 255, 255, 255, 255, 255, 255, 255, 255, 249, 255, 255, 255,
66         255, 207, 255, 207, 255, 255, 255, 255, 255, 255, 255, 255, 249, 255, 255, 255,
67         255, 207, 255, 207, 255, 255, 255, 255, 255, 255, 255, 255, 252, 255, 255, 255,
68         255, 207, 255, 207, 255, 255, 255, 255, 255, 255, 255, 127, 254, 255, 255, 255,
69         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
70         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
71         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
72         159, 15, 30, 252, 57, 224, 225, 128, 131, 7, 255, 254, 128, 127, 240, 255,
73         135, 231, 204, 249, 57, 255, 252, 159, 57, 115, 126, 252, 60, 63, 231, 255,
74         159, 247, 236, 249, 56, 127, 254, 159, 57, 115, 126, 252, 124, 158, 207, 255,
75         159, 255, 252, 249, 56, 127, 254, 207, 57, 115, 62, 249, 124, 158, 207, 255,
76         159, 255, 252, 121, 56, 112, 254, 207, 57, 115, 62, 249, 60, 207, 255, 255,
77         159, 127, 62, 60, 57, 103, 224, 231, 131, 115, 158, 243, 128, 207, 255, 255,
78         159, 63, 255, 57, 249, 103, 206, 231, 57, 7, 158, 243, 60, 207, 255, 255,
79         159, 159, 255, 153, 249, 103, 206, 231, 57, 127, 206, 231, 124, 206, 255, 255,
80         159, 207, 255, 25, 240, 103, 206, 243, 57, 127, 14, 224, 124, 158, 207, 255,
81         159, 231, 239, 249, 185, 103, 206, 243, 57, 127, 230, 207, 124, 158, 207, 255,
82         159, 231, 207, 249, 57, 103, 206, 243, 57, 63, 231, 207, 60, 63, 231, 255,
83         159, 7, 28, 252, 121, 240, 224, 243, 131, 135, 231, 207, 128, 127, 240, 255,
84         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
85         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
86         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
87         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
88         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
89         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
90         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
91         7, 126, 128, 3, 124, 240, 249, 156, 63, 231, 57, 255, 252, 57, 159, 255,
92         231, 124, 254, 243, 63, 231, 249, 156, 63, 231, 60, 255, 252, 57, 159, 255,
93         231, 121, 254, 243, 159, 207, 249, 156, 63, 103, 62, 255, 248, 56, 158, 255,
94         231, 121, 254, 243, 159, 207, 249, 156, 63, 39, 63, 255, 248, 56, 156, 255,
95         231, 115, 254, 243, 207, 255, 249, 156, 63, 135, 63, 255, 112, 56, 152, 255,
96         231, 115, 192, 3, 206, 255, 1, 156, 63, 199, 63, 255, 112, 56, 153, 255,
97         231, 115, 254, 243, 207, 193, 249, 156, 63, 135, 63, 255, 36, 57, 147, 255,
98         231, 115, 254, 243, 207, 207, 249, 156, 63, 39, 63, 255, 36, 57, 131, 255,
99         231, 121, 254, 243, 159, 207, 249, 156, 57, 103, 62, 255, 140, 57, 135, 255,
100         231, 121, 254, 243, 159, 199, 249, 156, 57, 231, 60, 255, 140, 57, 143, 255,
101         231, 124, 254, 243, 63, 199, 249, 156, 147, 231, 57, 255, 220, 57, 159, 255,
102         7, 126, 128, 243, 127, 208, 249, 156, 199, 231, 51, 192, 220, 57, 159, 255,
103         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
104         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
105         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
106         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
107         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
108         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
109         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
110         63, 252, 128, 63, 252, 128, 63, 28, 128, 249, 204, 79, 254, 39, 63, 255,
111         159, 249, 60, 159, 249, 60, 159, 249, 249, 249, 204, 79, 158, 39, 63, 255,
112         207, 243, 124, 206, 243, 124, 206, 243, 249, 249, 156, 103, 158, 103, 158, 255,
113         207, 243, 124, 206, 243, 124, 206, 255, 249, 249, 156, 231, 156, 243, 204, 255,
114         231, 231, 124, 230, 231, 124, 158, 255, 249, 249, 156, 231, 12, 243, 225, 255,
115         231, 231, 60, 231, 231, 60, 63, 252, 249, 249, 60, 243, 12, 243, 243, 255,
116         231, 231, 128, 231, 231, 128, 255, 249, 249, 249, 60, 243, 105, 249, 243, 255,
117         231, 231, 252, 103, 230, 60, 255, 243, 249, 249, 124, 251, 97, 248, 225, 255,
118         207, 243, 252, 207, 240, 124, 254, 243, 249, 249, 124, 248, 97, 248, 204, 255,
119         207, 243, 252, 207, 241, 124, 206, 243, 249, 249, 124, 248, 243, 124, 158, 255,
120         159, 249, 252, 159, 241, 124, 158, 249, 249, 115, 254, 252, 243, 60, 63, 255,
121         63, 252, 252, 63, 228, 252, 60, 252, 249, 7, 255, 252, 243, 60, 63, 255,
122         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
123         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
124         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
125         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
126         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
127         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
128         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
129         243, 19, 192, 255, 252, 255, 255, 127, 14, 127, 248, 15, 252, 247, 227, 231,
130         243, 243, 207, 255, 252, 255, 153, 127, 102, 62, 243, 227, 241, 193, 201, 243,
131         231, 249, 231, 255, 252, 255, 60, 127, 242, 156, 231, 249, 231, 148, 201, 249,
132         207, 252, 243, 255, 204, 124, 126, 126, 254, 156, 231, 57, 230, 148, 201, 252,
133         207, 252, 243, 255, 204, 60, 255, 60, 255, 156, 231, 156, 204, 244, 99, 254,
134         31, 254, 249, 255, 252, 159, 255, 57, 127, 158, 231, 204, 204, 244, 63, 255,
135         31, 254, 252, 255, 252, 207, 255, 51, 63, 159, 231, 204, 204, 193, 159, 255,
136         63, 127, 254, 255, 252, 159, 255, 57, 159, 207, 207, 204, 204, 151, 207, 248,
137         63, 127, 254, 255, 252, 63, 255, 156, 159, 159, 231, 204, 228, 151, 103, 242,
138         63, 63, 255, 255, 255, 127, 126, 158, 255, 159, 231, 25, 241, 148, 115, 242,
139         63, 159, 127, 230, 204, 252, 60, 159, 159, 159, 231, 249, 255, 148, 121, 242,
140         63, 31, 64, 230, 204, 252, 153, 159, 159, 159, 231, 227, 255, 193, 252, 248,
141         255, 255, 63, 255, 231, 255, 255, 255, 255, 159, 231, 15, 252, 247, 255, 255,
142         255, 255, 255, 255, 255, 255, 255, 255, 255, 63, 243, 255, 255, 255, 255, 255,
143         255, 255, 255, 255, 255, 255, 255, 255, 255, 127, 248, 255, 255, 255, 255, 255,
144         255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
145         63, 254, 255, 195, 255, 255, 255, 255, 3, 252, 147, 255, 255, 255, 255, 255,
146         159, 252, 255, 153, 255, 255, 255, 255, 243, 252, 147, 255, 255, 255, 255, 255,
147         159, 252, 255, 153, 255, 243, 255, 255, 243, 252, 147, 255, 255, 255, 255, 255,
148         159, 252, 204, 60, 255, 243, 255, 255, 243, 252, 0, 255, 255, 255, 255, 255,
149         63, 254, 204, 60, 255, 243, 128, 255, 243, 252, 201, 255, 255, 255, 255, 255,
150         63, 254, 225, 60, 255, 243, 255, 255, 243, 252, 201, 255, 255, 255, 255, 255,
151         31, 126, 128, 60, 127, 128, 255, 255, 243, 252, 201, 255, 255, 255, 255, 255,
152         159, 228, 225, 60, 193, 243, 128, 255, 243, 252, 201, 255, 255, 255, 255, 255,
153         207, 240, 204, 60, 255, 243, 255, 255, 243, 124, 128, 255, 255, 255, 255, 255,
154         207, 249, 204, 60, 255, 243, 255, 255, 243, 252, 228, 255, 255, 255, 255, 255,
155         207, 240, 255, 60, 255, 243, 255, 255, 243, 252, 228, 255, 255, 255, 255, 255,
156         31, 242, 255, 60, 255, 255, 255, 255, 243, 252, 228, 255, 255, 255, 255, 255,
157         255, 255, 255, 60, 255, 255, 255, 255, 243, 252, 255, 255, 255, 255, 255, 255,
158         255, 255, 255, 153, 255, 255, 255, 0, 242, 252, 255, 255, 255, 255, 255, 255,
159         255, 255, 255, 153, 255, 255, 255, 255, 243, 252, 255, 255, 255, 255, 255, 255,
160         255, 255, 255, 195, 255, 255, 255, 255, 3, 252, 255, 255, 255, 255, 255, 255
161 };
162
163 typedef struct {
164     unsigned char width;
165     float left, right, top, bottom;
166 } LC_TXFVERT;
167
168 static LC_TXFVERT glyphs[93];
169 static GLuint texture;
170
171 void texfont_init()
172 {
173     if (texture != 0) {
174         return;
175     }
176
177     int i, j, x, y;
178     float inv = 1.0f / 128;
179     const char *charlines[16] = {
180             "abcdefghijklmn", "opqrstuvwxyz0", "123456789ABC", "DEFGHIJKLMN",
181             "OPQRSTUVWX", "YZ,.!;:<>/?{}@$%", "&*()-+=_[] #"
182     };
183     unsigned char lefts[7][17] = {
184             {1, 11, 21, 30, 40, 50, 56, 66, 76, 80,  84,  93,  97,  111, 121},
185             {1, 11, 21, 31, 38, 47, 53, 63, 72, 86,  94,  103, 111, 120},
186             {1, 10, 19, 28, 37, 46, 55, 64, 73, 82,  94,  106, 118,},
187             {1, 13, 24, 34, 47, 59, 64, 73, 84, 94,  108, 120},
188             {1, 14, 25, 38, 50, 61, 71, 83, 94, 109, 120},
189             {1, 12, 22, 26, 30, 35, 39, 43, 52, 61,  65,  75,  81,  87,  103, 112, 125},
190             {3, 14, 23, 28, 33, 38, 47, 56, 65, 70,  75,  79,  88}
191     };
192     // tops = 1 20 39 58 77 96 112 (+16)
193     memset(glyphs, 0, sizeof(glyphs));
194
195     // ASCII 32-125
196     for (i = 32; i < 126; i++) {
197         for (x = 0; x < 7; x++) {
198             for (y = 0; charlines[x][y]; y++) {
199                 if (charlines[x][y] == i) {
200                     glyphs[i - 32].width = lefts[x][y + 1] - lefts[x][y];
201                     glyphs[i - 32].left = (float) lefts[x][y] * inv;
202                     glyphs[i - 32].right = (float) (lefts[x][y + 1]) * inv;
203
204                     if (x != 6) {
205                         glyphs[i - 32].top = (float) (1 + 19 * x);
206                     } else {
207                         glyphs[i - 32].top = 112;
208                     }
209                     glyphs[i - 32].bottom = glyphs[i - 32].top + 16;
210                     glyphs[i - 32].top *= inv;
211                     glyphs[i - 32].bottom *= inv;
212                 }
213             }
214         }
215     }
216
217     g_GLTable.m_pfn_qglGenTextures(1, &texture);
218     g_GLTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, texture);
219     g_GLTable.m_pfn_qglDisable(GL_TEXTURE_GEN_S);
220     g_GLTable.m_pfn_qglDisable(GL_TEXTURE_GEN_T);
221     g_GLTable.m_pfn_qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
222     g_GLTable.m_pfn_qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
223     g_GLTable.m_pfn_qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
224     g_GLTable.m_pfn_qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
225     //  g_GLTable.m_pfn_qglPixelStorei (GL_UNPACK_ALIGNMENT, 1);
226
227     unsigned char *buf = (unsigned char *) malloc(128 * 128);
228     memset(buf, 255, 128 * 128);
229
230     for (i = 0; i < 2048; i++) {
231         for (j = 0; j < 8; j++) {
232             if ((data[i] & (1 << j)) != 0) {
233                 buf[i * 8 + j] = 0;
234             }
235         }
236     }
237
238     g_GLTable.m_pfn_qglTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY4, 128, 128, 0,
239                                   GL_LUMINANCE, GL_UNSIGNED_BYTE, buf);
240     free(buf);
241 }
242
243 void texfont_write(const char *text, int l, int t)
244 {
245     if (texture == 0) {
246         return;
247     }
248
249     g_GLTable.m_pfn_qglColor3f(0, 1, 0);
250     g_GLTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, texture);
251     g_GLTable.m_pfn_qglEnable(GL_TEXTURE_2D);
252     //  g_GLTable.m_pfn_qglTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
253     g_GLTable.m_pfn_qglAlphaFunc(GL_GREATER, 0.0625);
254     g_GLTable.m_pfn_qglEnable(GL_ALPHA_TEST);
255
256     g_GLTable.m_pfn_qglBegin(GL_QUADS);
257     for (const char *p = text; *p; p++) {
258         if (*p < 32 || *p > 125) {
259             continue;
260         }
261         if (glyphs[*p - 32].width == 0) {
262             continue;
263         }
264
265         g_GLTable.m_pfn_qglTexCoord2f(glyphs[*p - 32].left, glyphs[*p - 32].top);
266         g_GLTable.m_pfn_qglVertex2i(l, t);
267         g_GLTable.m_pfn_qglTexCoord2f(glyphs[*p - 32].left, glyphs[*p - 32].bottom);
268         g_GLTable.m_pfn_qglVertex2i(l, t - 16);
269         g_GLTable.m_pfn_qglTexCoord2f(glyphs[*p - 32].right, glyphs[*p - 32].bottom);
270         g_GLTable.m_pfn_qglVertex2i(l + glyphs[*p - 32].width, t - 16);
271         g_GLTable.m_pfn_qglTexCoord2f(glyphs[*p - 32].right, glyphs[*p - 32].top);
272         g_GLTable.m_pfn_qglVertex2i(l + glyphs[*p - 32].width, t);
273         l += glyphs[*p - 32].width;
274     }
275     g_GLTable.m_pfn_qglEnd();
276
277     g_GLTable.m_pfn_qglDisable(GL_ALPHA_TEST);
278     g_GLTable.m_pfn_qglDisable(GL_TEXTURE_2D);
279     g_GLTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, 0);
280 }