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