doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / gameengine / GamePlayer / common / bmfont.cpp
1 /*
2  * bmfont.c
3  *
4  * 04-10-2000 frank
5  *
6  * $Id$
7  *
8  * ***** BEGIN GPL LICENSE BLOCK *****
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
25  * All rights reserved.
26  *
27  * The Original Code is: all of this file.
28  *
29  * Contributor(s): none yet.
30  *
31  * ***** END GPL LICENSE BLOCK *****
32  *
33  */
34
35 /**
36  * Two external functions:
37  *
38  * void detectBitmapFont(ImBuf *ibuf)
39  *   detects if an image buffer contains a bitmap font. It makes the
40  *   specific bitmap data which is stored in the bitmap invisible to blender.
41  *
42  * void matrixGlyph(ImBuf * ibuf, unsigned short unicode, *float x 7)
43  *   returns all the information about the character (unicode) in the floats
44  *
45  * Room for improvement:
46  *   add kerning data in the bitmap
47  *   all calculations in matrixGlyph() are static and could be done during
48  *     initialization
49  */
50
51 #include <stdio.h>
52
53 #include "MEM_guardedalloc.h"
54 #include "BLI_blenlib.h"
55 #include "BKE_global.h"
56 #include "IMB_imbuf_types.h"
57
58 #include "BKE_bmfont.h"
59 #include "BKE_bmfont_types.h"
60
61 /*MAART:
62 void printfGlyph(bmGlyph * glyph)
63 {
64         printf("unicode: %d '%c'\n", glyph->unicode, glyph->unicode);
65         printf(" locx: %4d locy: %4d\n", glyph->locx, glyph->locy);
66         printf(" sizex: %3d sizey: %3d\n", glyph->sizex, glyph->sizey);
67         printf(" ofsx:  %3d ofsy:  %3d\n", glyph->ofsx, glyph->ofsy);
68         printf(" advan: %3d reser: %3d\n", glyph->advance, glyph->reserved);
69 }
70 */
71
72 #define MAX2(x,y)          ( (x)>(y) ? (x) : (y) )
73 #define MAX3(x,y,z)                MAX2( MAX2((x),(y)) , (z) )  
74
75 void calcAlpha(ImBuf * ibuf)
76 {
77         int i;
78         char * rect;
79         
80         if (ibuf) {
81                 rect = (char *) ibuf->rect;
82                 for (i = ibuf->x * ibuf->y ; i > 0 ; i--) {
83                         rect[3] = MAX3(rect[0], rect[1], rect[2]);
84                         rect += 4;
85                 }
86         }
87 }
88
89 void readBitmapFontVersion0(ImBuf * ibuf, unsigned char * rect, int step)
90 {
91         int glyphcount, bytes, i, index, linelength, ysize;
92         unsigned char * buffer;
93         bmFont * bmfont;
94         
95         linelength = ibuf->x * step;
96         
97         glyphcount = (rect[6 * step] << 8) | rect[7 * step];
98         bytes = ((glyphcount - 1) * sizeof(bmGlyph)) + sizeof(bmFont);
99         
100         ysize = (bytes + (ibuf->x - 1)) / ibuf->x;
101         
102         if (ysize < ibuf->y) {
103                 // we're first going to copy all data into a liniar buffer.
104                 // step can be 4 or 1 bytes, and the data is not sequential because
105                 // the bitmap was flipped vertically.
106                 
107                 buffer = (unsigned char*)MEM_mallocN(bytes, "readBitmapFontVersion0:buffer");
108                 
109                 index = 0;      
110                 for (i = 0; i < bytes; i++) {
111                         buffer[i] = rect[index];
112                         index += step;
113                         if (index >= linelength) {
114                                 // we've read one line, no skip to the line *before* that
115                                 rect -= linelength;
116                                 index -= linelength;
117                         }
118                 }
119                 
120                 // we're now going to endian convert the data
121                 
122                 bmfont = (bmFont*)MEM_mallocN(bytes, "readBitmapFontVersion0:bmfont");
123                 index = 0;
124                 
125                 // first read the header
126                 bmfont->magic[0]    = buffer[index++];
127                 bmfont->magic[1]    = buffer[index++];
128                 bmfont->magic[2]    = buffer[index++];
129                 bmfont->magic[3]    = buffer[index++];
130                 bmfont->version     = (buffer[index] << 8) | buffer[index + 1]; index += 2;
131                 bmfont->glyphcount  = (buffer[index] << 8) | buffer[index + 1]; index += 2;
132                 bmfont->xsize       = (buffer[index] << 8) | buffer[index + 1]; index += 2;
133                 bmfont->ysize       = (buffer[index] << 8) | buffer[index + 1]; index += 2;
134                 
135                 for (i = 0; i < bmfont->glyphcount; i++) {
136                         bmfont->glyphs[i].unicode  = (buffer[index] << 8) | buffer[index + 1]; index += 2;
137                         bmfont->glyphs[i].locx     = (buffer[index] << 8) | buffer[index + 1]; index += 2;
138                         bmfont->glyphs[i].locy     = (buffer[index] << 8) | buffer[index + 1]; index += 2;
139                         bmfont->glyphs[i].ofsx     = buffer[index++];
140                         bmfont->glyphs[i].ofsy     = buffer[index++];
141                         bmfont->glyphs[i].sizex    = buffer[index++];
142                         bmfont->glyphs[i].sizey    = buffer[index++];
143                         bmfont->glyphs[i].advance  = buffer[index++];
144                         bmfont->glyphs[i].reserved = buffer[index++];
145                         /* MAART:
146                         if (G.f & G_DEBUG) {
147                                 printfGlyph(&bmfont->glyphs[i]);
148                         }
149                         */
150                 }
151                 
152                 MEM_freeN(buffer);
153                 
154                 /* MAART:
155                 if (G.f & G_DEBUG) {
156                         printf("Oldy = %d Newy = %d\n", ibuf->y, ibuf->y - ysize);
157                         printf("glyphcount = %d\n", glyphcount);
158                         printf("bytes = %d\n", bytes);
159                 }
160                 */
161
162                 // we've read the data from the image. Now we're going
163                 // to crop the image vertically so only the bitmap data
164                 // remains visible
165                 
166                 ibuf->y -= ysize;
167                 ibuf->userdata = bmfont;
168                 ibuf->userflags |= IB_BITMAPFONT;
169
170                 if (ibuf->depth < 32) {
171                         // we're going to fake alpha here:
172                         calcAlpha(ibuf);
173                 }
174         } else {
175                 /* MAART:
176                 printf("readBitmapFontVersion0: corrupted bitmapfont\n");
177                 */
178         }
179 }
180
181 void detectBitmapFont(ImBuf *ibuf)
182 {
183         unsigned char * rect;
184         unsigned short version;
185         long i;
186         
187         if (ibuf != NULL) {
188                 // bitmap must have an x size that is a power of two
189                 if (is_power_of_two(ibuf->x)) {
190                         rect = (unsigned char *) (ibuf->rect + (ibuf->x * (ibuf->y - 1)));
191                         // printf ("starts with: %s %c %c %c %c\n", rect, rect[0], rect[1], rect[2], rect[3]);
192                         if (rect[0] == 'B' && rect[1] == 'F' && rect[2] == 'N' && rect[3] == 'T') {
193                                 // printf("found 8bit font !\n");
194                                 // round y size down
195                                 // do the 8 bit font stuff. (not yet)
196                         } else {
197                                 // we try all 4 possible combinations
198                                 for (i = 0; i < 4; i++) {
199                                         if (rect[0] == 'B' && rect[4] == 'F' && rect[8] == 'N' && rect[12] == 'T') {
200                                                 // printf("found 24bit font !\n");
201                                                 // We're going to parse the file:
202                                                 
203                                                 version = (rect[16] << 8) | rect[20];
204                                                 
205                                                 if (version == 0) {
206                                                         readBitmapFontVersion0(ibuf, rect, 4);
207                                                 } else {
208                                                         //printf("detectBitmapFont :Unsupported version %d\n", version);
209                                                 }
210                                                 
211                                                 // on succes ibuf->userdata points to the bitmapfont
212                                                 if (ibuf->userdata) {
213                                                         break;
214                                                 }
215                                         }
216                                         rect++;
217                                 }
218                         }
219                 }
220         }
221 }
222
223 int locateGlyph(bmFont *bmfont, unsigned short unicode)
224 {
225         int min, max, current = 0;
226         
227         if (bmfont) {
228                 min = 0;
229                 max = bmfont->glyphcount;
230                 while (1) {
231                         // look halfway for glyph
232                         current = (min + max) >> 1;
233
234                         if (bmfont->glyphs[current].unicode == unicode) {
235                                 break;
236                         } else if (bmfont->glyphs[current].unicode < unicode) {
237                                 // have to move up
238                                 min = current;
239                         } else {
240                                 // have to move down
241                                 max = current;
242                         }
243                         
244                         if (max - min <= 1) {
245                                 // unable to locate glyph
246                                 current = 0;
247                                 break;
248                         }
249                 }
250         }
251         
252         return(current);
253 }
254
255 void matrixGlyph(ImBuf * ibuf, unsigned short unicode,
256                 float *centerx, float *centery,
257                 float *sizex,   float *sizey,
258                 float *transx,  float *transy,
259                 float *movex,   float *movey,
260                 float *advance)
261 {
262         int index;
263         bmFont *bmfont;
264         
265         *centerx = *centery = 0.0;
266         *sizex = *sizey = 1.0;
267         *transx = *transy = 0.0;
268         *movex = *movey = 0.0;
269         *advance = 1.0;
270                 
271         if (ibuf) {
272                 bmfont = (bmFont*)ibuf->userdata;
273                 if (bmfont && (ibuf->userflags & IB_BITMAPFONT)) {
274                         index = locateGlyph(bmfont, unicode);
275                         if (index) {
276                                                                 
277                                 *sizex = (bmfont->glyphs[index].sizex) / (float) (bmfont->glyphs[0].sizex);
278                                 *sizey = (bmfont->glyphs[index].sizey) / (float) (bmfont->glyphs[0].sizey);
279
280                                 *transx = bmfont->glyphs[index].locx / (float) ibuf->x;
281                                 *transy = (ibuf->y - bmfont->glyphs[index].locy) / (float) ibuf->y;
282
283                                 *centerx = bmfont->glyphs[0].locx / (float) ibuf->x;
284                                 *centery = (ibuf->y - bmfont->glyphs[0].locy) / (float) ibuf->y;
285
286                                 // 2.0 units is the default size of an object
287                                 
288                                 *movey = (float)(1.0 - *sizey + 2.0 * (bmfont->glyphs[index].ofsy - bmfont->glyphs[0].ofsy) / (float) bmfont->glyphs[0].sizey);
289                                 *movex = (float)(*sizex - 1.0 + 2.0 * (bmfont->glyphs[index].ofsx - bmfont->glyphs[0].ofsx) / (float) bmfont->glyphs[0].sizex);
290                                 
291                                 *advance = (float)(2.0 * bmfont->glyphs[index].advance / (float) bmfont->glyphs[0].advance);
292
293                                 // printfGlyph(&bmfont->glyphs[index]);
294                                 // printf("%c %d %0.5f %0.5f %0.5f %0.5f %0.5f \n", unicode, index, *sizex, *sizey, *transx, *transy, *advance);
295                         }
296                 }
297         }
298 }