summaryrefslogtreecommitdiff
path: root/lib/glfont.cc
blob: 5cb1646f8324080d888fce9f0567e4a448758028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <stdarg.h>
#include "glbase.h"
#include "glfont.h"
#include "Input.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

Uint8 prescale2[4] = { 0, 85, 170, 255 }, prescale3[8] = { 0, 36, 72, 109, 145, 182, 218, 255 };

#define STRBUFSIZ 512

/*

font file format
================

off|siz|description
---+---+-------------------------------
 0 | 2 | Number of entries = nbentries
 2 | 1 | Flags
 3 | 1 | maxX (maximum width)
 4 | 1 | maxY (maximum height)
 5 | 1 | base (bottom line from top)
 6 | 1 | inter (size of the interline)
 7 | X | char entries
7+X| Y | char map

X = (maxX * maxY + 1) * nbentries
Y = nbentries * 4


Flags:
=====

0000000R

R = RGBA (=1) or Alpha (=0)

RGBA in 1232 format:
ABBGGGRR


Each entries:
============

off|siz|description
---+---+-------------------------------
 0 | 1 | True size of the entry
 1 | Z | Datas

Z = maxX * maxY
Datas are stored in order X first, then Y.

Char map:
========

nbentries entries, each entry = 4 bytes = 2 uint16

off|siz|description
---+---+-------------------------------
 0 | 2 | Unicode (?)
 2 | 2 | Corresponding char entry

I'm not sure about my word 'Unicode'. I write this only to say it's an attempt
to make the fonts "internationals". If the "unicode" is < 255, then it should
match only one byte in the string. Otherwise, it should match two bytes.


Variables comments
==================

nbcU = number of chars on X by texture
nbcV = number of chars on Y by texture
nbcT = number of char by texture
nbT  = number of textures

*/

mogltk::font::font(Handle * ffont) : textcolor(255, 255, 255, 255) {
    int i;
    
    nbentries = ffont->readU16();
    flags = ffont->readU8();
    maxX = ffont->readU8();
    maxY = ffont->readU8();
    base = ffont->readU8();
    inter = ffont->readU8();
    
    nbcU = 256 / maxX;
    nbcV = 256 / maxY;
    
    nbcT = nbcU * nbcV;
    
    nbT = nbentries / nbcT;
    
    if (nbentries % nbcT) {
	nbT++;
    }
    
    printm(M_INFO, "Creating font texture: %i entries, flags = 0x%02x, maxX = %i, maxY = %i\n", nbentries, flags, maxX, maxY);
    printm(M_INFO, "Which makes %i texture(s), with %i char by texture, %i on X, and %i on Y\n", nbT, nbcT, nbcU, nbcV);
    
    fonttex = (texture **) malloc(nbT * sizeof(texture *));
    
    for (i = 0; i < nbT; i++) {
	fonttex[i] = new texture(256, 256, true);
    }
    
    sizes = (Uint8 *) malloc(nbentries * sizeof(Uint8));

    Uint8 * curtex = (Uint8 *) fonttex[0]->GetSurface()->pixels;
    Uint32 curU = 0, curV = 0, curT = 0;
    for (int i = 0; i < nbentries; i++) {
	sizes[i] = ffont->readU8();
	for (int v = 0; v < maxY; v++) {
	    for (int u = 0; u < maxX; u++) {
		Uint8 f;
		f = ffont->readU8();
		if (flags & 1) {
		    Uint8 r, g, b, a;
		    r = f & 3;
		    g = (f >> 2) & 7;
		    b = (f >> 5) & 3;
		    a = (f >> 7) & 1;
		    curtex[(curU + u + (curV + v) * 256) * 4 + 0] = prescale2[r];
		    curtex[(curU + u + (curV + v) * 256) * 4 + 1] = prescale3[g];
		    curtex[(curU + u + (curV + v) * 256) * 4 + 2] = prescale2[b];
		    curtex[(curU + u + (curV + v) * 256) * 4 + 3] = a ? 255 : 0;
		} else {
		    curtex[(curU + u + (curV + v) * 256) * 4 + 0] = 255;
		    curtex[(curU + u + (curV + v) * 256) * 4 + 1] = 255;
		    curtex[(curU + u + (curV + v) * 256) * 4 + 2] = 255;
		    curtex[(curU + u + (curV + v) * 256) * 4 + 3] = f;
		}
	    }
	}
	if (((curU += maxX) + maxX) > 256) {
	    curU = 0;
	    if (((curV += maxY) + maxY) > 256) {
		curV = 0;
		if ((curT + 1) != nbT)
		    curtex = (Uint8 *) fonttex[++curT]->GetSurface()->pixels;
	    }
	}
    }
    
    corresp = (Uint16 *) malloc(nbentries * 2 * sizeof(Uint16));
    
    ffont->read(corresp, 2 * sizeof(Uint16) * nbentries);
}

mogltk::font::~font() {
    int i;
    for (i = 0; i < nbT; i++) {
	delete fonttex[i];
    }
    
    free((void *&) fonttex);
    free(sizes);
}

void mogltk::font::drawentry(Uint16 entry, int x, int y, Color c) {
    bool was2D;
    int trueentry, cx, cy, px, py;
    
    was2D = mogltk::glbase::is2D();
    
    if (!was2D)
	mogltk::glbase::Enter2DMode();
    
    if (shadow) {
	int os = shadow;
	shadow = 0;
	
	drawentry(entry, x + os, y + os, BLACK);
	
	shadow = os;
    }
    
    y -= base;
    
    Bind(entry / nbcT);
    c.Bind();
    trueentry = entry % nbcT;
    cx = trueentry % nbcU;
    cy = trueentry / nbcU;
    px = cx * maxX;
    py = cy * maxY;
    
    glBegin(GL_TRIANGLE_STRIP);
    glTexCoord2i(px           , py           ); glVertex2i(x           , y           );
    glTexCoord2i(px + maxX - 1, py           ); glVertex2i(x + maxX - 1, y           );
    glTexCoord2i(px           , py + maxY - 1); glVertex2i(x           , y + maxY - 1);
    glTexCoord2i(px + maxX - 1, py + maxY - 1); glVertex2i(x + maxX - 1, y + maxY - 1);
    glEnd();
    
    if (!was2D)
	mogltk::glbase::Leave2DMode();
}

void mogltk::font::Bind(int index) {
    fonttex[index]->Bind();
}

void mogltk::font::putcursor(int x, int y) {
    cx = ox = x;
    cy = y;
}

void mogltk::font::putentry(Uint16 entry, Color c) {
    drawentry(entry, cx, cy, c);
    cx += sizes[entry];
}

void mogltk::font::putchar(char ch, Color c) {
    Uint16 * p;
    int i;

    for (i = 0, p = corresp; i < nbentries; i++, p++) {
	if (*(p++) == ch) {
	    putentry(*p, c);
	    return;
	}
    }
}

int mogltk::font::getchar(char ch) const {
    Uint16 * p;
    int i;

    for (i = 0, p = corresp; i < nbentries; i++, p++) {
	if (*(p++) == ch) {
	    return *p;
	}
    }
    
    return -1;
}

void mogltk::font::newline(void) {
    cx = ox;
    cy += inter;
}

int mogltk::font::printf(const ugly_string & m, va_list ap) {
    char * p;
    static char buffer[STRBUFSIZ + 1];
    int r;

#ifdef HAVE_VSNPRINTF
    r = vsnprintf(buffer, STRBUFSIZ, m.p, ap);
#else
    r = vsprintf(buffer, m.p, ap);
#endif
    
    for (p = buffer; *p; p++) {
	if (*p == '\n') {
	    newline();
	} else {
	    putchar(*p, textcolor);
	}
    }
    
    return r;
}

int mogltk::font::printf(const ugly_string & m, ...) {
    va_list ap;
    int r;
    
    va_start(ap, m);
    r = printf(m, ap);
    va_end(ap);
    
    return r;
}

int mogltk::font::printf(const char * p, ...) {
    ugly_string m;
    va_list ap;
    int r;
    
    m.p = p;
    
    va_start(ap, p);
    r = printf(m, ap);
    va_end(ap);
    
    return r;
}

void mogltk::font::setcolor(Color c) {
    textcolor = c;
}

void mogltk::font::setshadow(int s) {
    shadow = s;
}

int mogltk::font::singletextsize(const String & s) const {
    unsigned int i;
    int r = 0;
    
    for (i = 0; i < s.strlen(); i++) {
	r += sizes[getchar(s[i])];
    }
    
    return r;
}

mogltk::font * mogltk::SystemFont;