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