svn merge -r40197:40311 ^/trunk/blender
[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_mallocN(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
136         if(font->glyph_cache) {
137                 memset(font->glyph_cache->glyph_ascii_table, 0, sizeof(font->glyph_cache->glyph_ascii_table));
138         }
139 }
140
141 void blf_glyph_cache_free(GlyphCacheBLF *gc)
142 {
143         GlyphBLF *g;
144         int i;
145
146         for (i= 0; i < 257; i++) {
147                 while (gc->bucket[i].first) {
148                         g= gc->bucket[i].first;
149                         BLI_remlink(&(gc->bucket[i]), g);
150                         blf_glyph_free(g);
151                 }
152         }
153
154         if (gc->cur_tex+1 > 0)
155                 glDeleteTextures(gc->cur_tex+1, gc->textures);
156         free((void *)gc->textures);
157         MEM_freeN(gc);
158 }
159
160 static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc)
161 {
162         int tot_mem, i;
163         unsigned char *buf;
164
165         /* move the index. */
166         gc->cur_tex++;
167
168         if (gc->cur_tex >= gc->ntex) {
169                 gc->ntex *= 2;
170                 gc->textures= (GLuint *)realloc((void *)gc->textures, sizeof(GLuint)*gc->ntex);
171         }
172
173         gc->p2_width= blf_next_p2((gc->rem_glyphs * gc->max_glyph_width) + (gc->pad * 2));
174         if (gc->p2_width > font->max_tex_size)
175                 gc->p2_width= font->max_tex_size;
176
177         i= (int)((gc->p2_width - (gc->pad * 2)) / gc->max_glyph_width);
178         gc->p2_height= blf_next_p2(((gc->num_glyphs / i) + 1) * gc->max_glyph_height);
179
180         if (gc->p2_height > font->max_tex_size)
181                 gc->p2_height= font->max_tex_size;
182
183         tot_mem= gc->p2_width * gc->p2_height;
184         buf= (unsigned char *)malloc(tot_mem);
185         memset((void *)buf, 0, tot_mem);
186
187         glGenTextures(1, &gc->textures[gc->cur_tex]);
188         glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state= gc->textures[gc->cur_tex]));
189         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
190         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
191         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
192         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
193
194         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gc->p2_width, gc->p2_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buf);
195         free((void *)buf);
196 }
197
198 GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
199 {
200         GlyphBLF *p;
201         unsigned int key;
202
203         key= blf_hash(c);
204         p= gc->bucket[key].first;
205         while (p) {
206                 if (p->c == c)
207                         return(p);
208                 p= p->next;
209         }
210         return(NULL);
211 }
212
213 GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c)
214 {
215         FT_GlyphSlot slot;
216         GlyphBLF *g;
217         FT_Error err;
218         FT_Bitmap bitmap, tempbitmap;
219         int sharp = (U.text_render & USER_TEXT_DISABLE_AA);
220         FT_BBox bbox;
221         unsigned int key;
222
223         g= blf_glyph_search(font->glyph_cache, c);
224         if (g)
225                 return(g);
226
227         if (sharp)
228                 err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO);
229         else
230                 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? */
231         if (err)
232                 return(NULL);
233
234         /* get the glyph. */
235         slot= font->face->glyph;
236
237         if (sharp) {
238                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
239
240                 /* Convert result from 1 bit per pixel to 8 bit per pixel */
241                 /* Accum errors for later, fine if not interested beyond "ok vs any error" */
242                 FT_Bitmap_New(&tempbitmap);
243                 err += FT_Bitmap_Convert(font->ft_lib, &slot->bitmap, &tempbitmap, 1); /* Does Blender use Pitch 1 always? It works so far */
244                 err += FT_Bitmap_Copy(font->ft_lib, &tempbitmap, &slot->bitmap);
245                 err += FT_Bitmap_Done(font->ft_lib, &tempbitmap);
246         } else {
247                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
248         }
249
250         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
251                 return(NULL);
252
253         g= (GlyphBLF *)MEM_mallocN(sizeof(GlyphBLF), "blf_glyph_add");
254         g->next= NULL;
255         g->prev= NULL;
256         g->c= c;
257         g->idx= (FT_UInt)index;
258         g->tex= 0;
259         g->build_tex= 0;
260         g->bitmap= NULL;
261         g->xoff= -1;
262         g->yoff= -1;
263         g->uv[0][0]= 0.0f;
264         g->uv[0][1]= 0.0f;
265         g->uv[1][0]= 0.0f;
266         g->uv[1][1]= 0.0f;
267         bitmap= slot->bitmap;
268         g->width= bitmap.width;
269         g->height= bitmap.rows;
270
271         if (g->width && g->height) {
272                 if (sharp) {
273                         /* Font buffer uses only 0 or 1 values, Blender expects full 0..255 range */
274                         int i;
275                         for (i=0; i < (g->width * g->height); i++) {
276                                 bitmap.buffer[i] = 255 * bitmap.buffer[i];
277                         }
278                 }
279
280                 g->bitmap= (unsigned char *)MEM_mallocN(g->width * g->height, "glyph bitmap");
281                 memcpy((void *)g->bitmap, (void *)bitmap.buffer, g->width * g->height);
282         }
283
284         g->advance= ((float)slot->advance.x) / 64.0f;
285         g->pos_x= slot->bitmap_left;
286         g->pos_y= slot->bitmap_top;
287         g->pitch= slot->bitmap.pitch;
288
289         FT_Outline_Get_CBox(&(slot->outline), &bbox);
290         g->box.xmin= ((float)bbox.xMin) / 64.0f;
291         g->box.xmax= ((float)bbox.xMax) / 64.0f;
292         g->box.ymin= ((float)bbox.yMin) / 64.0f;
293         g->box.ymax= ((float)bbox.yMax) / 64.0f;
294
295         key= blf_hash(g->c);
296         BLI_addhead(&(font->glyph_cache->bucket[key]), g);
297         return(g);
298 }
299
300 void blf_glyph_free(GlyphBLF *g)
301 {
302         /* don't need free the texture, the GlyphCache already
303          * have a list of all the texture and free it.
304          */
305         if (g->bitmap)
306                 MEM_freeN(g->bitmap);
307         MEM_freeN(g);
308 }
309
310 static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
311 {
312         
313         glBegin(GL_QUADS);
314         glTexCoord2f(uv[0][0], uv[0][1]);
315         glVertex2f(dx, y1);
316         
317         glTexCoord2f(uv[0][0], uv[1][1]);
318         glVertex2f(dx, y2);
319         
320         glTexCoord2f(uv[1][0], uv[1][1]);
321         glVertex2f(dx1, y2);
322         
323         glTexCoord2f(uv[1][0], uv[0][1]);
324         glVertex2f(dx1, y1);
325         glEnd();
326         
327 }
328
329 static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2)
330 {
331         float soft[25]= {1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f,
332                          1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f,
333                          2/60.0f, 5/60.0f, 8/60.0f, 5/60.0f, 2/60.0f,
334                          1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f,
335                          1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f};
336         
337         float color[4], *fp= soft;
338         int dx, dy;
339
340         color[0]= shadow_col[0];
341         color[1]= shadow_col[1];
342         color[2]= shadow_col[2];
343         
344         for(dx=-2; dx<3; dx++) {
345                 for(dy=-2; dy<3; dy++, fp++) {
346                         color[3]= *(fp) * shadow_col[3];
347                         glColor4fv(color);
348                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
349                 }
350         }
351         
352         glColor4fv(color);
353 }
354
355 static void blf_texture3_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2)
356 {
357         float soft[9]= {1/16.0f, 2/16.0f, 1/16.0f,
358                         2/16.0f,4/16.0f, 2/16.0f,
359                         1/16.0f, 2/16.0f, 1/16.0f};
360
361         float color[4], *fp= soft;
362         int dx, dy;
363
364         color[0]= shadow_col[0];
365         color[1]= shadow_col[1];
366         color[2]= shadow_col[2];
367
368         for(dx=-1; dx<2; dx++) {
369                 for(dy=-1; dy<2; dy++, fp++) {
370                         color[3]= *(fp) * shadow_col[3];
371                         glColor4fv(color);
372                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
373                 }
374         }
375         
376         glColor4fv(color);
377 }
378
379 int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
380 {
381         float dx, dx1;
382         float y1, y2;
383         float xo, yo;
384
385         if ((!g->width) || (!g->height))
386                 return(1);
387
388         if (g->build_tex == 0) {
389                 GlyphCacheBLF *gc= font->glyph_cache;
390
391                 if (font->max_tex_size == -1)
392                         glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint *)&font->max_tex_size);
393
394                 if (gc->cur_tex == -1) {
395                         blf_glyph_cache_texture(font, gc);
396                         gc->x_offs= gc->pad;
397                         gc->y_offs= gc->pad;
398                 }
399
400                 if (gc->x_offs > (gc->p2_width - gc->max_glyph_width)) {
401                         gc->x_offs= gc->pad;
402                         gc->y_offs += gc->max_glyph_height;
403
404                         if (gc->y_offs > (gc->p2_height - gc->max_glyph_height)) {
405                                 gc->y_offs= gc->pad;
406                                 blf_glyph_cache_texture(font, gc);
407                         }
408                 }
409
410                 g->tex= gc->textures[gc->cur_tex];
411                 g->xoff= gc->x_offs;
412                 g->yoff= gc->y_offs;
413
414                 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
415                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
416                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
417                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
418
419                 glBindTexture(GL_TEXTURE_2D, g->tex);
420                 glTexSubImage2D(GL_TEXTURE_2D, 0, g->xoff, g->yoff, g->width, g->height, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap);
421                 glPopClientAttrib();
422
423                 g->uv[0][0]= ((float)g->xoff) / ((float)gc->p2_width);
424                 g->uv[0][1]= ((float)g->yoff) / ((float)gc->p2_height);
425                 g->uv[1][0]= ((float)(g->xoff + g->width)) / ((float)gc->p2_width);
426                 g->uv[1][1]= ((float)(g->yoff + g->height)) / ((float)gc->p2_height);
427
428                 /* update the x offset for the next glyph. */
429                 gc->x_offs += (int)(g->box.xmax - g->box.xmin + gc->pad);
430
431                 gc->rem_glyphs--;
432                 g->build_tex= 1;
433         }
434
435         xo= 0.0f;
436         yo= 0.0f;
437
438         if (font->flags & BLF_SHADOW) {
439                 xo= x;
440                 yo= y;
441                 x += font->shadow_x;
442                 y += font->shadow_y;
443         }
444
445         dx= floor(x + g->pos_x);
446         dx1= dx + g->width;
447         y1= y + g->pos_y;
448         y2= y + g->pos_y - g->height;
449
450         if (font->flags & BLF_CLIPPING) {
451                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
452                         return(0);
453                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
454                         return(0);
455                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
456                         return(0);
457                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
458                         return(0);
459         }
460
461         if (font->tex_bind_state != g->tex) {
462                 glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state= g->tex));
463         }
464
465         if (font->flags & BLF_SHADOW) {
466
467                 switch(font->shadow) {
468                         case 3:
469                                 blf_texture3_draw(font->shadow_col, g->uv, dx, y1, dx1, y2);
470                                 break;
471                         case 5:
472                                 blf_texture5_draw(font->shadow_col, g->uv, dx, y1, dx1, y2);
473                                 break;
474                         default:
475                                 glColor4fv(font->shadow_col);
476                                 blf_texture_draw(g->uv, dx, y1, dx1, y2);
477                                 break;
478                 }
479
480                 glColor4fv(font->orig_col);
481
482                 x= xo;
483                 y= yo;
484
485                 dx= floor(x + g->pos_x);
486                 dx1= dx + g->width;
487                 y1= y + g->pos_y;
488                 y2= y + g->pos_y - g->height;
489         }
490
491         switch(font->blur) {
492                 case 3:
493                         blf_texture3_draw(font->orig_col, g->uv, dx, y1, dx1, y2);
494                         break;
495                 case 5:
496                         blf_texture5_draw(font->orig_col, g->uv, dx, y1, dx1, y2);
497                         break;
498                 default:
499                         blf_texture_draw(g->uv, dx, y1, dx1, y2);
500                         break;
501         }
502
503         return(1);
504 }