svn merge ^/trunk/blender -r48749:48754
[blender.git] / source / blender / blenkernel / intern / bmfont.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): 04-10-2000 frank.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  */
28
29 /** \file blender/blenkernel/intern/bmfont.c
30  *  \ingroup bke
31  */
32
33
34 /**
35  * Two external functions:
36  *
37  * void detectBitmapFont(ImBuf *ibuf)
38  *   detects if an image buffer contains a bitmap font. It makes the
39  *   specific bitmap data which is stored in the bitmap invisible to blender.
40  *
41  * void matrixGlyph(ImBuf * ibuf, unsigned short unicode, *float x 7)
42  *   returns all the information about the character (unicode) in the floats
43  *
44  * Room for improvement:
45  *   add kerning data in the bitmap
46  *   all calculations in matrixGlyph() are static and could be done during
47  *     initialization
48  */
49
50 #include <stdio.h>
51
52 #include "MEM_guardedalloc.h"
53 #include "BKE_global.h"
54 #include "IMB_imbuf_types.h"
55
56 #include "BKE_bmfont.h"
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.debug & G_DEBUG) {
142                                 printfGlyph(&bmfont->glyphs[i]);
143                         }
144                 }
145                 
146                 MEM_freeN(buffer);
147                 
148                 if (G.debug & 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->planes < 32) {
163                         /* we're going to fake alpha here: */
164                         calcAlpha(ibuf);
165                 }
166         }
167         else {
168                 printf("readBitmapFontVersion0: corrupted bitmapfont\n");
169         }
170 }
171
172 void detectBitmapFont(ImBuf *ibuf)
173 {
174         unsigned char * rect;
175         unsigned short version;
176         int i;
177         
178         if (ibuf != NULL && ibuf->rect != NULL) {
179                         /* bitmap must have an x size that is a power of two */
180                 if (is_power_of_two(ibuf->x)) {
181                         rect = (unsigned char *) (ibuf->rect + (ibuf->x * (ibuf->y - 1)));
182                         /* printf ("starts with: %s %c %c %c %c\n", rect, rect[0], rect[1], rect[2], rect[3]); */
183                         if (rect[0] == 'B' && rect[1] == 'F' && rect[2] == 'N' && rect[3] == 'T') {
184                                 /* printf("found 8bit font !\n");
185                                  * round y size down
186                                  * do the 8 bit font stuff. (not yet) */
187                         }
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                                                 }
200                                                 else {
201                                                         printf("detectBitmapFont :Unsupported version %d\n", version);
202                                                 }
203
204                                                 /* on succes ibuf->userdata points to the bitmapfont */
205                                                 if (ibuf->userdata) {
206                                                         break;
207                                                 }
208                                         }
209                                         rect++;
210                                 }
211                         }
212                 }
213         }
214 }
215
216 int locateGlyph(bmFont *bmfont, unsigned short unicode)
217 {
218         int min, max, current = 0;
219         
220         if (bmfont) {
221                 min = 0;
222                 max = bmfont->glyphcount;
223                 while (1) {
224                         /* look halfway for glyph */
225                         current = (min + max) >> 1;
226
227                         if (bmfont->glyphs[current].unicode == unicode) {
228                                 break;
229                         }
230                         else if (bmfont->glyphs[current].unicode < unicode) {
231                                 /* have to move up */
232                                 min = current;
233                         }
234                         else {
235                                 /* have to move down */
236                                 max = current;
237                         }
238
239                         if (max - min <= 1) {
240                                 /* unable to locate glyph */
241                                 current = 0;
242                                 break;
243                         }
244                 }
245         }
246         
247         return(current);
248 }
249
250 void matrixGlyph(ImBuf * ibuf, unsigned short unicode,
251                 float *centerx, float *centery,
252                 float *sizex,   float *sizey,
253                 float *transx,  float *transy,
254                 float *movex,   float *movey,
255                 float *advance)
256 {
257         int index;
258         bmFont *bmfont;
259         
260         *centerx = *centery = 0.0;
261         *sizex = *sizey = 1.0;
262         *transx = *transy = 0.0;
263         *movex = *movey = 0.0;
264         *advance = 1.0;
265                 
266         if (ibuf) {
267                 bmfont = ibuf->userdata;
268                 if (bmfont && (ibuf->userflags & IB_BITMAPFONT)) {
269                         index = locateGlyph(bmfont, unicode);
270                         if (index) {
271                                                                 
272                                 *sizex = (bmfont->glyphs[index].sizex) / (float) (bmfont->glyphs[0].sizex);
273                                 *sizey = (bmfont->glyphs[index].sizey) / (float) (bmfont->glyphs[0].sizey);
274
275                                 *transx = bmfont->glyphs[index].locx / (float) ibuf->x;
276                                 *transy = (ibuf->y - bmfont->glyphs[index].locy) / (float) ibuf->y;
277
278                                 *centerx = bmfont->glyphs[0].locx / (float) ibuf->x;
279                                 *centery = (ibuf->y - bmfont->glyphs[0].locy) / (float) ibuf->y;
280
281                                 /* 2.0 units is the default size of an object */
282                                 
283                                 *movey = 1.0f - *sizey + 2.0f * (bmfont->glyphs[index].ofsy - bmfont->glyphs[0].ofsy) / (float) bmfont->glyphs[0].sizey;
284                                 *movex = *sizex - 1.0f + 2.0f * (bmfont->glyphs[index].ofsx - bmfont->glyphs[0].ofsx) / (float) bmfont->glyphs[0].sizex;
285                                 
286                                 *advance = 2.0f * bmfont->glyphs[index].advance / (float) bmfont->glyphs[0].advance;
287
288                                 // printfGlyph(&bmfont->glyphs[index]);
289                                 // printf("%c %d %0.5f %0.5f %0.5f %0.5f %0.5f\n", unicode, index, *sizex, *sizey, *transx, *transy, *advance);
290                         }
291                 }
292         }
293 }