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