svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r20937:21899
[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_texture_add(FontBLF *font, FT_UInt index, unsigned int c)
193 {
194         FT_GlyphSlot slot;
195         GlyphCacheBLF *gc;
196         GlyphBLF *g;
197         GlyphTextureBLF *gt;
198         FT_Error err;
199         FT_Bitmap bitmap;
200         FT_BBox bbox;
201         unsigned int key;
202         int do_new;
203
204         g= blf_glyph_search(font->glyph_cache, c);
205
206         /* The glyph can be add on Bitmap mode, so we have the
207          * glyph, but not the texture data.
208          */
209         if (g && g->tex_data)
210                 return(g);
211         else if (g)
212                 do_new= 0;
213         else
214                 do_new= 1;
215
216         err= FT_Load_Glyph(font->face, index, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP);
217         if (err)
218                 return(NULL);
219
220         /* get the glyph. */
221         slot= font->face->glyph;
222
223         err= FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
224         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
225                 return(NULL);
226
227         if (do_new) {
228                 g= (GlyphBLF *)MEM_mallocN(sizeof(GlyphBLF), "blf_glyph_add");
229                 g->next= NULL;
230                 g->prev= NULL;
231                 g->tex_data= NULL;
232                 g->bitmap_data= NULL;
233                 g->c= c;
234         }
235
236         gt= (GlyphTextureBLF *)MEM_mallocN(sizeof(GlyphTextureBLF), "blf_glyph_texture_add");
237         gc= font->glyph_cache;
238
239         if (gc->cur_tex == -1) {
240                 blf_glyph_cache_texture(font, gc);
241                 gc->x_offs= gc->pad;
242                 gc->y_offs= gc->pad;
243         }
244
245         if (gc->x_offs > (gc->p2_width - gc->max_glyph_width)) {
246                 gc->x_offs= gc->pad;
247                 gc->y_offs += gc->max_glyph_height;
248
249                 if (gc->y_offs > (gc->p2_height - gc->max_glyph_height)) {
250                         gc->y_offs= gc->pad;
251                         blf_glyph_cache_texture(font, gc);
252                 }
253         }
254
255         bitmap= slot->bitmap;
256         gt->tex= gc->textures[gc->cur_tex];
257
258         gt->xoff= gc->x_offs;
259         gt->yoff= gc->y_offs;
260         gt->width= bitmap.width;
261         gt->height= bitmap.rows;
262
263         if (gt->width && gt->height) {
264                 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
265                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
266                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
267                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
268
269                 glBindTexture(GL_TEXTURE_2D, gt->tex);
270                 glTexSubImage2D(GL_TEXTURE_2D, 0, gt->xoff, gt->yoff, gt->width, gt->height, GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.buffer);
271                 glPopClientAttrib();
272         }
273
274         g->advance= ((float)slot->advance.x) / 64.0f;
275         gt->pos_x= slot->bitmap_left;
276         gt->pos_y= slot->bitmap_top;
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         gt->uv[0][0]= ((float)gt->xoff) / ((float)gc->p2_width);
285         gt->uv[0][1]= ((float)gt->yoff) / ((float)gc->p2_height);
286         gt->uv[1][0]= ((float)(gt->xoff + gt->width)) / ((float)gc->p2_width);
287         gt->uv[1][1]= ((float)(gt->yoff + gt->height)) / ((float)gc->p2_height);
288
289         /* update the x offset for the next glyph. */
290         gc->x_offs += (int)(g->box.xmax - g->box.xmin + gc->pad);
291
292         if (do_new) {
293                 key= blf_hash(g->c);
294                 BLI_addhead(&(gc->bucket[key]), g);
295                 gc->rem_glyphs--;
296         }
297
298         /* and attach the texture information. */
299         g->tex_data= gt;
300
301         return(g);
302 }
303
304 GlyphBLF *blf_glyph_bitmap_add(FontBLF *font, FT_UInt index, unsigned int c)
305 {
306         FT_GlyphSlot slot;
307         GlyphCacheBLF *gc;
308         GlyphBLF *g;
309         GlyphBitmapBLF *gt;
310         FT_Error err;
311         FT_Bitmap bitmap;
312         FT_BBox bbox;
313         unsigned char *dest, *src;
314         unsigned int key, y;
315         unsigned int src_width, src_height, src_pitch;
316         int do_new;
317
318         g= blf_glyph_search(font->glyph_cache, c);
319
320         /*
321          * The glyph can be add on Texture mode, so we have the
322          * glyph, but not the bitmap data.
323          */
324         if (g && g->bitmap_data)
325                 return(g);
326         else if (g)
327                 do_new= 0;
328         else
329                 do_new= 1;
330
331         err= FT_Load_Glyph(font->face, index, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP);
332         if (err)
333                 return(NULL);
334
335         /* get the glyph. */
336         slot= font->face->glyph;
337
338         err= FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
339         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
340                 return(NULL);
341
342         if (do_new) {
343                 g= (GlyphBLF *)MEM_mallocN(sizeof(GlyphBLF), "blf_glyph_add");
344                 g->next= NULL;
345                 g->prev= NULL;
346                 g->tex_data= NULL;
347                 g->bitmap_data= NULL;
348                 g->c= c;
349         }
350
351         gt= (GlyphBitmapBLF *)MEM_mallocN(sizeof(GlyphBitmapBLF), "blf_glyph_bitmap_add");
352         gc= font->glyph_cache;
353
354         bitmap= slot->bitmap;
355
356         src_width= bitmap.width;
357         src_height= bitmap.rows;
358         src_pitch= bitmap.pitch;
359
360         gt->width= src_width;
361         gt->height= src_height;
362         gt->pitch= src_pitch;
363         gt->image= NULL;
364
365         if (gt->width && gt->height) {
366                 gt->image= (unsigned char *)malloc(gt->pitch * gt->height);
367
368                 dest= gt->image + ((gt->height - 1) * gt->pitch);
369                 src= bitmap.buffer;
370
371                 for (y= 0; y < src_height; ++y) {
372                         memcpy((void *)dest, (void *)src, src_pitch);
373                         dest -= gt->pitch;
374                         src += src_pitch;
375                 }
376         }
377
378         g->advance= ((float)slot->advance.x) / 64.0f;
379         gt->pos_x= slot->bitmap_left;
380         gt->pos_y= ((int)src_height) - slot->bitmap_top;
381
382         FT_Outline_Get_CBox(&(slot->outline), &bbox);
383         g->box.xmin= ((float)bbox.xMin) / 64.0f;
384         g->box.xmax= ((float)bbox.xMax) / 64.0f;
385         g->box.ymin= ((float)bbox.yMin) / 64.0f;
386         g->box.ymax= ((float)bbox.yMax) / 64.0f;
387
388         if (do_new) {
389                 key= blf_hash(g->c);
390                 BLI_addhead(&(gc->bucket[key]), g);
391                 gc->rem_glyphs--;
392         }
393
394         /* and attach the bitmap information. */
395         g->bitmap_data= gt;
396
397         return(g);
398 }
399
400 GlyphBLF *blf_glyph_add(FontBLF *font, FT_UInt index, unsigned int c)
401 {
402         if (font->mode == BLF_MODE_BITMAP)
403                 return(blf_glyph_bitmap_add(font, index, c));
404         return(blf_glyph_texture_add(font, index, c));
405 }
406
407 void blf_glyph_free(GlyphBLF *g)
408 {
409         if (g->tex_data)
410                 MEM_freeN(g->tex_data);
411
412         if (g->bitmap_data) {
413                 if (g->bitmap_data->image)
414                         free((void *)g->bitmap_data->image);
415                 MEM_freeN(g->bitmap_data);
416         }
417
418         /* don't need free the texture, the GlyphCache already
419          * have a list of all the texture and free it.
420          */
421         MEM_freeN(g);
422 }
423
424 static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
425 {
426         
427         glBegin(GL_QUADS);
428         glTexCoord2f(uv[0][0], uv[0][1]);
429         glVertex2f(dx, y1);
430         
431         glTexCoord2f(uv[0][0], uv[1][1]);
432         glVertex2f(dx, y2);
433         
434         glTexCoord2f(uv[1][0], uv[1][1]);
435         glVertex2f(dx1, y2);
436         
437         glTexCoord2f(uv[1][0], uv[0][1]);
438         glVertex2f(dx1, y1);
439         glEnd();
440         
441 }
442
443 static void blf_texture5_draw(float uv[2][2], float x1, float y1, float x2, float y2)
444 {
445         float soft[25]= {
446                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f, 
447                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
448                 2/60.0f, 5/60.0f, 8/60.0f, 5/60.0f, 2/60.0f, 
449                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
450                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f};
451         
452         float color[4], *fp= soft;
453         int dx, dy;
454         
455         glGetFloatv(GL_CURRENT_COLOR, color);
456         
457         for(dx=-2; dx<3; dx++) {
458                 for(dy=-2; dy<3; dy++, fp++) {
459                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
460                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
461                 }
462         }
463         
464         glColor4fv(color);
465 }
466
467 static void blf_texture3_draw(float uv[2][2], float x1, float y1, float x2, float y2)
468 {
469         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};
470         float color[4], *fp= soft;
471         int dx, dy;
472         
473         glGetFloatv(GL_CURRENT_COLOR, color);
474         
475         for(dx=-1; dx<2; dx++) {
476                 for(dy=-1; dy<2; dy++, fp++) {
477                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
478                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
479                 }
480         }
481         
482         glColor4fv(color);
483 }
484
485 int blf_glyph_texture_render(FontBLF *font, GlyphBLF *g, float x, float y)
486 {
487         GlyphTextureBLF *gt;
488         GLint cur_tex;
489         float dx, dx1;
490         float y1, y2;
491         float xo, yo;
492         float color[4];
493
494         gt= g->tex_data;
495         xo= 0.0f;
496         yo= 0.0f;
497
498         if (font->flags & BLF_SHADOW) {
499                 xo= x;
500                 yo= y;
501                 x += font->shadow_x;
502                 y += font->shadow_y;
503         }
504
505         dx= floor(x + gt->pos_x);
506         dx1= dx + gt->width;
507         y1= y + gt->pos_y;
508         y2= y + gt->pos_y - gt->height;
509
510         if (font->flags & BLF_CLIPPING) {
511                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
512                         return(0);
513                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
514                         return(0);
515                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
516                         return(0);
517                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
518                         return(0);
519         }
520
521         glGetIntegerv(GL_TEXTURE_2D_BINDING_EXT, &cur_tex);
522         if (cur_tex != gt->tex)
523                 glBindTexture(GL_TEXTURE_2D, gt->tex);
524
525         if (font->flags & BLF_SHADOW) {
526                 glGetFloatv(GL_CURRENT_COLOR, color);
527                 glColor4fv(font->shadow_col);
528
529                 if (font->shadow == 3)
530                         blf_texture3_draw(gt->uv, dx, y1, dx1, y2);
531                 else if (font->shadow == 5)
532                         blf_texture5_draw(gt->uv, dx, y1, dx1, y2);
533                 else
534                         blf_texture_draw(gt->uv, dx, y1, dx1, y2);
535
536                 glColor4fv(color);
537                 x= xo;
538                 y= yo;
539
540                 dx= floor(x + gt->pos_x);
541                 dx1= dx + gt->width;
542                 y1= y + gt->pos_y;
543                 y2= y + gt->pos_y - gt->height;
544         }
545
546         if (font->blur==3)
547                 blf_texture3_draw(gt->uv, dx, y1, dx1, y2);
548         else if (font->blur==5)
549                 blf_texture5_draw(gt->uv, dx, y1, dx1, y2);
550         else
551                 blf_texture_draw(gt->uv, dx, y1, dx1, y2);
552         
553         return(1);
554 }
555
556 int blf_glyph_bitmap_render(FontBLF *font, GlyphBLF *g, float x, float y)
557 {
558         GlyphBitmapBLF *gt;
559         GLubyte null_bitmap= 0;
560
561         gt= g->bitmap_data;
562         if (!gt->image)
563                 return(1);
564
565         if (font->flags & BLF_CLIPPING) {
566                 if (!BLI_in_rctf(&font->clip_rec, x + font->pos[0], y + font->pos[1]))
567                         return(0);
568                 if (!BLI_in_rctf(&font->clip_rec, x + font->pos[0], y + gt->height + font->pos[1]))
569                         return(0);
570                 if (!BLI_in_rctf(&font->clip_rec, x + gt->width + font->pos[0], y + gt->height + font->pos[1]))
571                         return(0);
572                 if (!BLI_in_rctf(&font->clip_rec, x + gt->width + font->pos[0], y + font->pos[1]))
573                         return(0);
574         }
575
576         glBitmap(0, 0, 0.0, 0.0, x + font->pos[0], y - font->pos[1], (const GLubyte *)&null_bitmap);
577         glPixelStorei(GL_UNPACK_ROW_LENGTH, gt->pitch * 8);
578         glBitmap(gt->width, gt->height, 0.0, gt->pos_y, 0.0, 0.0, (const GLubyte *)gt->image);
579         glBitmap(0, 0, 0.0, 0.0, -x - font->pos[0], -y + font->pos[1], (const GLubyte *)&null_bitmap);
580         return(1);
581 }
582
583 int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
584 {
585         if (font->mode == BLF_MODE_BITMAP)
586                 return(blf_glyph_bitmap_render(font, g, x, y));
587         return(blf_glyph_texture_render(font, g, x, y));
588 }