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