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