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