Recreating my GSoC branch.
[blender.git] / source / blender / blenkernel / intern / bmfont.c
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 "BKE_global.h"
55 #include "IMB_imbuf_types.h"
56
57 #include "BKE_bmfont_types.h"
58
59 void printfGlyph(bmGlyph * glyph)
60 {
61         printf("unicode: %d '%c'\n", glyph->unicode, glyph->unicode);
62         printf(" locx: %4d locy: %4d\n", glyph->locx, glyph->locy);
63         printf(" sizex: %3d sizey: %3d\n", glyph->sizex, glyph->sizey);
64         printf(" ofsx:  %3d ofsy:  %3d\n", glyph->ofsx, glyph->ofsy);
65         printf(" advan: %3d reser: %3d\n", glyph->advance, glyph->reserved);
66 }
67
68 #define MAX2(x,y)          ( (x)>(y) ? (x) : (y) )
69 #define MAX3(x,y,z)                MAX2( MAX2((x),(y)) , (z) )  
70
71 void calcAlpha(ImBuf * ibuf)
72 {
73         int i;
74         char * rect;
75         
76         if (ibuf) {
77                 rect = (char *) ibuf->rect;
78                 for (i = ibuf->x * ibuf->y ; i > 0 ; i--) {
79                         rect[3] = MAX3(rect[0], rect[1], rect[2]);
80                         rect += 4;
81                 }
82         }
83 }
84
85 void readBitmapFontVersion0(ImBuf * ibuf, unsigned char * rect, int step)
86 {
87         int glyphcount, bytes, i, index, linelength, ysize;
88         unsigned char * buffer;
89         bmFont * bmfont;
90         
91         linelength = ibuf->x * step;
92         
93         glyphcount = (rect[6 * step] << 8) | rect[7 * step];
94         bytes = ((glyphcount - 1) * sizeof(bmGlyph)) + sizeof(bmFont);
95         
96         ysize = (bytes + (ibuf->x - 1)) / ibuf->x;
97         
98         if (ysize < ibuf->y) {
99                 // we're first going to copy all data into a liniar buffer.
100                 // step can be 4 or 1 bytes, and the data is not sequential because
101                 // the bitmap was flipped vertically.
102                 
103                 buffer = MEM_mallocN(bytes, "readBitmapFontVersion0:buffer");
104                 
105                 index = 0;      
106                 for (i = 0; i < bytes; i++) {
107                         buffer[i] = rect[index];
108                         index += step;
109                         if (index >= linelength) {
110                                 // we've read one line, no skip to the line *before* that
111                                 rect -= linelength;
112                                 index -= linelength;
113                         }
114                 }
115                 
116                 // we're now going to endian convert the data
117                 
118                 bmfont = MEM_mallocN(bytes, "readBitmapFontVersion0:bmfont");
119                 index = 0;
120                 
121                 // first read the header
122                 bmfont->magic[0]    = buffer[index++];
123                 bmfont->magic[1]    = buffer[index++];
124                 bmfont->magic[2]    = buffer[index++];
125                 bmfont->magic[3]    = buffer[index++];
126                 bmfont->version     = (buffer[index] << 8) | buffer[index + 1]; index += 2;
127                 bmfont->glyphcount  = (buffer[index] << 8) | buffer[index + 1]; index += 2;
128                 bmfont->xsize       = (buffer[index] << 8) | buffer[index + 1]; index += 2;
129                 bmfont->ysize       = (buffer[index] << 8) | buffer[index + 1]; index += 2;
130                 
131                 for (i = 0; i < bmfont->glyphcount; i++) {
132                         bmfont->glyphs[i].unicode  = (buffer[index] << 8) | buffer[index + 1]; index += 2;
133                         bmfont->glyphs[i].locx     = (buffer[index] << 8) | buffer[index + 1]; index += 2;
134                         bmfont->glyphs[i].locy     = (buffer[index] << 8) | buffer[index + 1]; index += 2;
135                         bmfont->glyphs[i].ofsx     = buffer[index++];
136                         bmfont->glyphs[i].ofsy     = buffer[index++];
137                         bmfont->glyphs[i].sizex    = buffer[index++];
138                         bmfont->glyphs[i].sizey    = buffer[index++];
139                         bmfont->glyphs[i].advance  = buffer[index++];
140                         bmfont->glyphs[i].reserved = buffer[index++];
141                         if (G.f & G_DEBUG) {
142                                 printfGlyph(&bmfont->glyphs[i]);
143                         }
144                 }
145                 
146                 MEM_freeN(buffer);
147                 
148                 if (G.f & G_DEBUG) {
149                         printf("Oldy = %d Newy = %d\n", ibuf->y, ibuf->y - ysize);
150                         printf("glyphcount = %d\n", glyphcount);
151                         printf("bytes = %d\n", bytes);
152                 }
153
154                 // we've read the data from the image. Now we're going
155                 // to crop the image vertically so only the bitmap data
156                 // remains visible
157                 
158                 ibuf->y -= ysize;
159                 ibuf->userdata = bmfont;
160                 ibuf->userflags |= IB_BITMAPFONT;
161
162                 if (ibuf->depth < 32) {
163                         // we're going to fake alpha here:
164                         calcAlpha(ibuf);
165                 }
166         } else {
167                 printf("readBitmapFontVersion0: corrupted bitmapfont\n");
168         }
169 }
170
171 void detectBitmapFont(ImBuf *ibuf)
172 {
173         unsigned char * rect;
174         unsigned short version;
175         int i;
176         
177         if (ibuf != NULL && ibuf->rect != NULL) {
178                         // bitmap must have an x size that is a power of two
179                 if (is_power_of_two(ibuf->x)) {
180                         rect = (unsigned char *) (ibuf->rect + (ibuf->x * (ibuf->y - 1)));
181                         // printf ("starts with: %s %c %c %c %c\n", rect, rect[0], rect[1], rect[2], rect[3]);
182                         if (rect[0] == 'B' && rect[1] == 'F' && rect[2] == 'N' && rect[3] == 'T') {
183                                 // printf("found 8bit font !\n");
184                                 // round y size down
185                                 // do the 8 bit font stuff. (not yet)
186                         } else {
187                                 // we try all 4 possible combinations
188                                 for (i = 0; i < 4; i++) {
189                                         if (rect[0] == 'B' && rect[4] == 'F' && rect[8] == 'N' && rect[12] == 'T') {
190                                                 // printf("found 24bit font !\n");
191                                                 // We're going to parse the file:
192                                                 
193                                                 version = (rect[16] << 8) | rect[20];
194                                                 
195                                                 if (version == 0) {
196                                                         readBitmapFontVersion0(ibuf, rect, 4);
197                                                 } else {
198                                                         printf("detectBitmapFont :Unsupported version %d\n", version);
199                                                 }
200                                                 
201                                                 // on succes ibuf->userdata points to the bitmapfont
202                                                 if (ibuf->userdata) {
203                                                         break;
204                                                 }
205                                         }
206                                         rect++;
207                                 }
208                         }
209                 }
210         }
211 }
212
213 int locateGlyph(bmFont *bmfont, unsigned short unicode)
214 {
215         int min, max, current = 0;
216         
217         if (bmfont) {
218                 min = 0;
219                 max = bmfont->glyphcount;
220                 while (1) {
221                         // look halfway for glyph
222                         current = (min + max) >> 1;
223
224                         if (bmfont->glyphs[current].unicode == unicode) {
225                                 break;
226                         } else if (bmfont->glyphs[current].unicode < unicode) {
227                                 // have to move up
228                                 min = current;
229                         } else {
230                                 // have to move down
231                                 max = current;
232                         }
233                         
234                         if (max - min <= 1) {
235                                 // unable to locate glyph
236                                 current = 0;
237                                 break;
238                         }
239                 }
240         }
241         
242         return(current);
243 }
244
245 void matrixGlyph(ImBuf * ibuf, unsigned short unicode,
246                 float *centerx, float *centery,
247                 float *sizex,   float *sizey,
248                 float *transx,  float *transy,
249                 float *movex,   float *movey,
250                 float *advance)
251 {
252         int index;
253         bmFont *bmfont;
254         
255         *centerx = *centery = 0.0;
256         *sizex = *sizey = 1.0;
257         *transx = *transy = 0.0;
258         *movex = *movey = 0.0;
259         *advance = 1.0;
260                 
261         if (ibuf) {
262                 bmfont = ibuf->userdata;
263                 if (bmfont && (ibuf->userflags & IB_BITMAPFONT)) {
264                         index = locateGlyph(bmfont, unicode);
265                         if (index) {
266                                                                 
267                                 *sizex = (bmfont->glyphs[index].sizex) / (float) (bmfont->glyphs[0].sizex);
268                                 *sizey = (bmfont->glyphs[index].sizey) / (float) (bmfont->glyphs[0].sizey);
269
270                                 *transx = bmfont->glyphs[index].locx / (float) ibuf->x;
271                                 *transy = (ibuf->y - bmfont->glyphs[index].locy) / (float) ibuf->y;
272
273                                 *centerx = bmfont->glyphs[0].locx / (float) ibuf->x;
274                                 *centery = (ibuf->y - bmfont->glyphs[0].locy) / (float) ibuf->y;
275
276                                 // 2.0 units is the default size of an object
277                                 
278                                 *movey = 1.0f - *sizey + 2.0f * (bmfont->glyphs[index].ofsy - bmfont->glyphs[0].ofsy) / (float) bmfont->glyphs[0].sizey;
279                                 *movex = *sizex - 1.0f + 2.0f * (bmfont->glyphs[index].ofsx - bmfont->glyphs[0].ofsx) / (float) bmfont->glyphs[0].sizex;
280                                 
281                                 *advance = 2.0f * bmfont->glyphs[index].advance / (float) bmfont->glyphs[0].advance;
282
283                                 // printfGlyph(&bmfont->glyphs[index]);
284                                 // printf("%c %d %0.5f %0.5f %0.5f %0.5f %0.5f \n", unicode, index, *sizex, *sizey, *transx, *transy, *advance);
285                         }
286                 }
287         }
288 }