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