synched with trunk at revision 34793
[blender.git] / source / blender / blenfont / intern / blf_glyph.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) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33
34 #include <ft2build.h>
35
36 #include FT_FREETYPE_H
37 #include FT_GLYPH_H
38 #include FT_OUTLINE_H
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_vec_types.h"
43
44 #include "BLI_blenlib.h"
45
46 #include "BIF_gl.h"
47 #include "BLF_api.h"
48
49 #include "blf_internal_types.h"
50 #include "blf_internal.h"
51
52
53 GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, int size, int dpi)
54 {
55         GlyphCacheBLF *p;
56
57         p= (GlyphCacheBLF *)font->cache.first;
58         while (p) {
59                 if (p->size == size && p->dpi == dpi)
60                         return(p);
61                 p= p->next;
62         }
63         return(NULL);
64 }
65
66 /* Create a new glyph cache for the current size and dpi. */
67 GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
68 {
69         GlyphCacheBLF *gc;
70         int i;
71
72         gc= (GlyphCacheBLF *)MEM_mallocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new");
73         gc->next= NULL;
74         gc->prev= NULL;
75         gc->size= font->size;
76         gc->dpi= font->dpi;
77
78         for (i= 0; i < 257; i++) {
79                 gc->bucket[i].first= NULL;
80                 gc->bucket[i].last= NULL;
81         }
82
83         gc->textures= (GLuint *)malloc(sizeof(GLuint)*256);
84         gc->ntex= 256;
85         gc->cur_tex= -1;
86         gc->x_offs= 0;
87         gc->y_offs= 0;
88         gc->pad= 3;
89
90         gc->num_glyphs= font->face->num_glyphs;
91         gc->rem_glyphs= font->face->num_glyphs;
92         gc->ascender= ((float)font->face->size->metrics.ascender) / 64.0f;
93         gc->descender= ((float)font->face->size->metrics.descender) / 64.0f;
94
95         if (FT_IS_SCALABLE(font->face)) {
96                 gc->max_glyph_width= (float)((font->face->bbox.xMax - font->face->bbox.xMin) *
97                                         (((float)font->face->size->metrics.x_ppem) /
98                                          ((float)font->face->units_per_EM)));
99
100                 gc->max_glyph_height= (float)((font->face->bbox.yMax - font->face->bbox.yMin) *
101                                         (((float)font->face->size->metrics.y_ppem) /
102                                          ((float)font->face->units_per_EM)));
103         }
104         else {
105                 gc->max_glyph_width= ((float)font->face->size->metrics.max_advance) / 64.0f;
106                 gc->max_glyph_height= ((float)font->face->size->metrics.height) / 64.0f;
107         }
108
109         gc->p2_width= 0;
110         gc->p2_height= 0;
111
112         BLI_addhead(&font->cache, gc);
113         return(gc);
114 }
115
116 void blf_glyph_cache_free(GlyphCacheBLF *gc)
117 {
118         GlyphBLF *g;
119         int i;
120
121         for (i= 0; i < 257; i++) {
122                 while (gc->bucket[i].first) {
123                         g= gc->bucket[i].first;
124                         BLI_remlink(&(gc->bucket[i]), g);
125                         blf_glyph_free(g);
126                 }
127         }
128
129         if (gc->cur_tex+1 > 0)
130                 glDeleteTextures(gc->cur_tex+1, gc->textures);
131         free((void *)gc->textures);
132         MEM_freeN(gc);
133 }
134
135 static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc)
136 {
137         int tot_mem, i;
138         unsigned char *buf;
139
140         /* move the index. */
141         gc->cur_tex++;
142
143         if (gc->cur_tex >= gc->ntex) {
144                 gc->ntex *= 2;
145                 gc->textures= (GLuint *)realloc((void *)gc->textures, sizeof(GLuint)*gc->ntex);
146         }
147
148         gc->p2_width= blf_next_p2((gc->rem_glyphs * gc->max_glyph_width) + (gc->pad * 2));
149         if (gc->p2_width > font->max_tex_size)
150                 gc->p2_width= font->max_tex_size;
151
152         i= (int)((gc->p2_width - (gc->pad * 2)) / gc->max_glyph_width);
153         gc->p2_height= blf_next_p2(((gc->num_glyphs / i) + 1) * gc->max_glyph_height);
154
155         if (gc->p2_height > font->max_tex_size)
156                 gc->p2_height= font->max_tex_size;
157
158         tot_mem= gc->p2_width * gc->p2_height;
159         buf= (unsigned char *)malloc(tot_mem);
160         memset((void *)buf, 0, tot_mem);
161
162         glGenTextures(1, &gc->textures[gc->cur_tex]);
163         glBindTexture(GL_TEXTURE_2D, gc->textures[gc->cur_tex]);
164         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
165         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
166         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
167         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
168
169         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gc->p2_width, gc->p2_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buf);
170         free((void *)buf);
171 }
172
173 GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
174 {
175         GlyphBLF *p;
176         unsigned int key;
177
178         key= blf_hash(c);
179         p= gc->bucket[key].first;
180         while (p) {
181                 if (p->c == c)
182                         return(p);
183                 p= p->next;
184         }
185         return(NULL);
186 }
187
188 GlyphBLF *blf_glyph_add(FontBLF *font, FT_UInt index, unsigned int c)
189 {
190         FT_GlyphSlot slot;
191         GlyphBLF *g;
192         FT_Error err;
193         FT_Bitmap bitmap;
194         FT_BBox bbox;
195         unsigned int key;
196
197         g= blf_glyph_search(font->glyph_cache, c);
198         if (g)
199                 return(g);
200
201         err= FT_Load_Glyph(font->face, index, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP);
202         if (err)
203                 return(NULL);
204
205         /* get the glyph. */
206         slot= font->face->glyph;
207
208         err= FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
209         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
210                 return(NULL);
211
212         g= (GlyphBLF *)MEM_mallocN(sizeof(GlyphBLF), "blf_glyph_add");
213         g->next= NULL;
214         g->prev= NULL;
215         g->c= c;
216         g->idx= index;
217         g->tex= 0;
218         g->build_tex= 0;
219         g->bitmap= NULL;
220         g->xoff= -1;
221         g->yoff= -1;
222         g->uv[0][0]= 0.0f;
223         g->uv[0][1]= 0.0f;
224         g->uv[1][0]= 0.0f;
225         g->uv[1][1]= 0.0f;
226         bitmap= slot->bitmap;
227         g->width= bitmap.width;
228         g->height= bitmap.rows;
229
230         if (g->width && g->height) {
231                 g->bitmap= (unsigned char *)MEM_mallocN(g->width * g->height, "glyph bitmap");
232                 memcpy((void *)g->bitmap, (void *)bitmap.buffer, g->width * g->height);
233         }
234
235         g->advance= ((float)slot->advance.x) / 64.0f;
236         g->pos_x= slot->bitmap_left;
237         g->pos_y= slot->bitmap_top;
238         g->pitch= slot->bitmap.pitch;
239
240         FT_Outline_Get_CBox(&(slot->outline), &bbox);
241         g->box.xmin= ((float)bbox.xMin) / 64.0f;
242         g->box.xmax= ((float)bbox.xMax) / 64.0f;
243         g->box.ymin= ((float)bbox.yMin) / 64.0f;
244         g->box.ymax= ((float)bbox.yMax) / 64.0f;
245
246         key= blf_hash(g->c);
247         BLI_addhead(&(font->glyph_cache->bucket[key]), g);
248         return(g);
249 }
250
251 void blf_glyph_free(GlyphBLF *g)
252 {
253         /* don't need free the texture, the GlyphCache already
254          * have a list of all the texture and free it.
255          */
256         if (g->bitmap)
257                 MEM_freeN(g->bitmap);
258         MEM_freeN(g);
259 }
260
261 static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
262 {
263         
264         glBegin(GL_QUADS);
265         glTexCoord2f(uv[0][0], uv[0][1]);
266         glVertex2f(dx, y1);
267         
268         glTexCoord2f(uv[0][0], uv[1][1]);
269         glVertex2f(dx, y2);
270         
271         glTexCoord2f(uv[1][0], uv[1][1]);
272         glVertex2f(dx1, y2);
273         
274         glTexCoord2f(uv[1][0], uv[0][1]);
275         glVertex2f(dx1, y1);
276         glEnd();
277         
278 }
279
280 static void blf_texture5_draw(float uv[2][2], float x1, float y1, float x2, float y2)
281 {
282         float soft[25]= {
283                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f, 
284                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
285                 2/60.0f, 5/60.0f, 8/60.0f, 5/60.0f, 2/60.0f, 
286                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
287                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f};
288         
289         float color[4], *fp= soft;
290         int dx, dy;
291         
292         glGetFloatv(GL_CURRENT_COLOR, color);
293         
294         for(dx=-2; dx<3; dx++) {
295                 for(dy=-2; dy<3; dy++, fp++) {
296                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
297                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
298                 }
299         }
300         
301         glColor4fv(color);
302 }
303
304 static void blf_texture3_draw(float uv[2][2], float x1, float y1, float x2, float y2)
305 {
306         float soft[9]= {1/16.0f, 2/16.0f, 1/16.0f, 2/16.0f, 4/16.0f, 2/16.0f, 1/16.0f, 2/16.0f, 1/16.0f};
307         float color[4], *fp= soft;
308         int dx, dy;
309         
310         glGetFloatv(GL_CURRENT_COLOR, color);
311         
312         for(dx=-1; dx<2; dx++) {
313                 for(dy=-1; dy<2; dy++, fp++) {
314                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
315                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
316                 }
317         }
318         
319         glColor4fv(color);
320 }
321
322 int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
323 {
324         GlyphCacheBLF *gc;
325         GLint cur_tex;
326         float dx, dx1;
327         float y1, y2;
328         float xo, yo;
329         float color[4];
330
331         if ((!g->width) || (!g->height))
332                 return(1);
333
334         if (g->build_tex == 0) {
335                 gc= font->glyph_cache;
336
337                 if (font->max_tex_size == -1)
338                         glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint *)&font->max_tex_size);
339
340                 if (gc->cur_tex == -1) {
341                         blf_glyph_cache_texture(font, gc);
342                         gc->x_offs= gc->pad;
343                         gc->y_offs= gc->pad;
344                 }
345
346                 if (gc->x_offs > (gc->p2_width - gc->max_glyph_width)) {
347                         gc->x_offs= gc->pad;
348                         gc->y_offs += gc->max_glyph_height;
349
350                         if (gc->y_offs > (gc->p2_height - gc->max_glyph_height)) {
351                                 gc->y_offs= gc->pad;
352                                 blf_glyph_cache_texture(font, gc);
353                         }
354                 }
355
356                 g->tex= gc->textures[gc->cur_tex];
357                 g->xoff= gc->x_offs;
358                 g->yoff= gc->y_offs;
359
360                 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
361                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
362                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
363                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
364
365                 glBindTexture(GL_TEXTURE_2D, g->tex);
366                 glTexSubImage2D(GL_TEXTURE_2D, 0, g->xoff, g->yoff, g->width, g->height, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap);
367                 glPopClientAttrib();
368
369                 g->uv[0][0]= ((float)g->xoff) / ((float)gc->p2_width);
370                 g->uv[0][1]= ((float)g->yoff) / ((float)gc->p2_height);
371                 g->uv[1][0]= ((float)(g->xoff + g->width)) / ((float)gc->p2_width);
372                 g->uv[1][1]= ((float)(g->yoff + g->height)) / ((float)gc->p2_height);
373
374                 /* update the x offset for the next glyph. */
375                 gc->x_offs += (int)(g->box.xmax - g->box.xmin + gc->pad);
376
377                 gc->rem_glyphs--;
378                 g->build_tex= 1;
379         }
380
381         xo= 0.0f;
382         yo= 0.0f;
383
384         if (font->flags & BLF_SHADOW) {
385                 xo= x;
386                 yo= y;
387                 x += font->shadow_x;
388                 y += font->shadow_y;
389         }
390
391         dx= floor(x + g->pos_x);
392         dx1= dx + g->width;
393         y1= y + g->pos_y;
394         y2= y + g->pos_y - g->height;
395
396         if (font->flags & BLF_CLIPPING) {
397                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
398                         return(0);
399                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
400                         return(0);
401                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
402                         return(0);
403                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
404                         return(0);
405         }
406
407         glGetIntegerv(GL_TEXTURE_2D_BINDING_EXT, &cur_tex);
408         if (cur_tex != g->tex)
409                 glBindTexture(GL_TEXTURE_2D, g->tex);
410
411         if (font->flags & BLF_SHADOW) {
412                 glGetFloatv(GL_CURRENT_COLOR, color);
413                 glColor4fv(font->shadow_col);
414
415                 if (font->shadow == 3)
416                         blf_texture3_draw(g->uv, dx, y1, dx1, y2);
417                 else if (font->shadow == 5)
418                         blf_texture5_draw(g->uv, dx, y1, dx1, y2);
419                 else
420                         blf_texture_draw(g->uv, dx, y1, dx1, y2);
421
422                 glColor4fv(color);
423                 x= xo;
424                 y= yo;
425
426                 dx= floor(x + g->pos_x);
427                 dx1= dx + g->width;
428                 y1= y + g->pos_y;
429                 y2= y + g->pos_y - g->height;
430         }
431
432         if (font->blur==3)
433                 blf_texture3_draw(g->uv, dx, y1, dx1, y2);
434         else if (font->blur==5)
435                 blf_texture5_draw(g->uv, dx, y1, dx1, y2);
436         else
437                 blf_texture_draw(g->uv, dx, y1, dx1, y2);
438
439         return(1);
440 }