Style Cleanup:
[blender.git] / source / blender / blenfont / intern / blf_glyph.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/blenfont/intern/blf_glyph.c
28  *  \ingroup blf
29  */
30
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include <ft2build.h>
38
39 #include FT_FREETYPE_H
40 #include FT_GLYPH_H
41 #include FT_OUTLINE_H
42 #include FT_BITMAP_H
43
44 #include "MEM_guardedalloc.h"
45
46 #include "DNA_vec_types.h"
47 #include "DNA_userdef_types.h"
48
49 #include "BLI_blenlib.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 #define _BLF_PADDING 3
58 #define _BLF_MIPMAP_LEVELS 3
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
78         gc= (GlyphCacheBLF *)MEM_callocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new");
79         gc->next= NULL;
80         gc->prev= NULL;
81         gc->size= font->size;
82         gc->dpi= font->dpi;
83
84         memset(gc->glyph_ascii_table, 0, sizeof(gc->glyph_ascii_table));
85         memset(gc->bucket, 0, sizeof(gc->bucket));
86
87         gc->textures= (GLuint *)malloc(sizeof(GLuint)*256);
88         gc->ntex= 256;
89         gc->cur_tex= -1;
90         gc->x_offs= 0;
91         gc->y_offs= 0;
92         /* Increase padding for each mipmap level: 0->3, 1->4, 2->6, 3->10, ... */
93         if (font->flags & BLF_TEXFILTER)
94                 gc->pad= pow(2, _BLF_MIPMAP_LEVELS) + 2;
95         else
96                 gc->pad= _BLF_PADDING;
97
98         gc->num_glyphs= font->face->num_glyphs;
99         gc->rem_glyphs= font->face->num_glyphs;
100         gc->ascender= ((float)font->face->size->metrics.ascender) / 64.0f;
101         gc->descender= ((float)font->face->size->metrics.descender) / 64.0f;
102
103         if (FT_IS_SCALABLE(font->face)) {
104                 gc->max_glyph_width= (float)((font->face->bbox.xMax - font->face->bbox.xMin) *
105                                         (((float)font->face->size->metrics.x_ppem) /
106                                          ((float)font->face->units_per_EM)));
107
108                 gc->max_glyph_height= (float)((font->face->bbox.yMax - font->face->bbox.yMin) *
109                                         (((float)font->face->size->metrics.y_ppem) /
110                                          ((float)font->face->units_per_EM)));
111         }
112         else {
113                 gc->max_glyph_width= ((float)font->face->size->metrics.max_advance) / 64.0f;
114                 gc->max_glyph_height= ((float)font->face->size->metrics.height) / 64.0f;
115         }
116
117         gc->p2_width= 0;
118         gc->p2_height= 0;
119
120         BLI_addhead(&font->cache, gc);
121         return gc;
122 }
123
124 void blf_glyph_cache_clear(FontBLF *font)
125 {
126         GlyphCacheBLF *gc;
127         GlyphBLF *g;
128         int i;
129
130         for(gc=font->cache.first; gc; gc=gc->next) {
131                 for (i= 0; i < 257; i++) {
132                         while (gc->bucket[i].first) {
133                                 g= gc->bucket[i].first;
134                                 BLI_remlink(&(gc->bucket[i]), g);
135                                 blf_glyph_free(g);
136                         }
137                 }
138
139                 memset(gc->glyph_ascii_table, 0, sizeof(gc->glyph_ascii_table));
140         }
141 }
142
143 void blf_glyph_cache_free(GlyphCacheBLF *gc)
144 {
145         GlyphBLF *g;
146         int i;
147
148         for (i= 0; i < 257; i++) {
149                 while (gc->bucket[i].first) {
150                         g= gc->bucket[i].first;
151                         BLI_remlink(&(gc->bucket[i]), g);
152                         blf_glyph_free(g);
153                 }
154         }
155
156         if (gc->cur_tex+1 > 0)
157                 glDeleteTextures(gc->cur_tex+1, gc->textures);
158         free((void *)gc->textures);
159         MEM_freeN(gc);
160 }
161
162 static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc)
163 {
164         int tot_mem, i;
165         unsigned char *buf;
166
167         /* move the index. */
168         gc->cur_tex++;
169
170         if (gc->cur_tex >= gc->ntex) {
171                 gc->ntex *= 2;
172                 gc->textures= (GLuint *)realloc((void *)gc->textures, sizeof(GLuint)*gc->ntex);
173         }
174
175         gc->p2_width= blf_next_p2((gc->rem_glyphs * gc->max_glyph_width) + (gc->pad * 2));
176         if (gc->p2_width > font->max_tex_size)
177                 gc->p2_width= font->max_tex_size;
178
179         i= (int)((gc->p2_width - (gc->pad * 2)) / gc->max_glyph_width);
180         gc->p2_height= blf_next_p2(((gc->num_glyphs / i) + 1) * gc->max_glyph_height);
181
182         if (gc->p2_height > font->max_tex_size)
183                 gc->p2_height= font->max_tex_size;
184
185         tot_mem= gc->p2_width * gc->p2_height;
186         buf= (unsigned char *)malloc(tot_mem);
187         memset((void *)buf, 0, tot_mem);
188
189         glGenTextures(1, &gc->textures[gc->cur_tex]);
190         glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state= gc->textures[gc->cur_tex]));
191         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
192         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
193         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
194         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
195
196         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gc->p2_width, gc->p2_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buf);
197         free((void *)buf);
198 }
199
200 GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
201 {
202         GlyphBLF *p;
203         unsigned int key;
204
205         key= blf_hash(c);
206         p= gc->bucket[key].first;
207         while (p) {
208                 if (p->c == c)
209                         return p;
210                 p= p->next;
211         }
212         return NULL;
213 }
214
215 GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c)
216 {
217         FT_GlyphSlot slot;
218         GlyphBLF *g;
219         FT_Error err;
220         FT_Bitmap bitmap, tempbitmap;
221         int sharp = (U.text_render & USER_TEXT_DISABLE_AA);
222         FT_BBox bbox;
223         unsigned int key;
224
225         g= blf_glyph_search(font->glyph_cache, c);
226         if (g)
227                 return g;
228
229         if (sharp)
230                 err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO);
231         else
232                 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? */
233         if (err)
234                 return NULL;
235
236         /* get the glyph. */
237         slot= font->face->glyph;
238
239         if (sharp) {
240                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
241
242                 /* Convert result from 1 bit per pixel to 8 bit per pixel */
243                 /* Accum errors for later, fine if not interested beyond "ok vs any error" */
244                 FT_Bitmap_New(&tempbitmap);
245                 err += FT_Bitmap_Convert(font->ft_lib, &slot->bitmap, &tempbitmap, 1); /* Does Blender use Pitch 1 always? It works so far */
246                 err += FT_Bitmap_Copy(font->ft_lib, &tempbitmap, &slot->bitmap);
247                 err += FT_Bitmap_Done(font->ft_lib, &tempbitmap);
248         } else {
249                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
250         }
251
252         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
253                 return NULL;
254
255         g= (GlyphBLF *)MEM_callocN(sizeof(GlyphBLF), "blf_glyph_add");
256         g->c= c;
257         g->idx= (FT_UInt)index;
258         g->xoff= -1;
259         g->yoff= -1;
260         bitmap= slot->bitmap;
261         g->width= bitmap.width;
262         g->height= bitmap.rows;
263
264         if (g->width && g->height) {
265                 if (sharp) {
266                         /* Font buffer uses only 0 or 1 values, Blender expects full 0..255 range */
267                         int i;
268                         for (i=0; i < (g->width * g->height); i++) {
269                                 bitmap.buffer[i] = 255 * bitmap.buffer[i];
270                         }
271                 }
272
273                 g->bitmap= (unsigned char *)MEM_mallocN(g->width * g->height, "glyph bitmap");
274                 memcpy((void *)g->bitmap, (void *)bitmap.buffer, g->width * g->height);
275         }
276
277         g->advance= ((float)slot->advance.x) / 64.0f;
278         g->pos_x= slot->bitmap_left;
279         g->pos_y= slot->bitmap_top;
280         g->pitch= slot->bitmap.pitch;
281
282         FT_Outline_Get_CBox(&(slot->outline), &bbox);
283         g->box.xmin= ((float)bbox.xMin) / 64.0f;
284         g->box.xmax= ((float)bbox.xMax) / 64.0f;
285         g->box.ymin= ((float)bbox.yMin) / 64.0f;
286         g->box.ymax= ((float)bbox.yMax) / 64.0f;
287
288         key= blf_hash(g->c);
289         BLI_addhead(&(font->glyph_cache->bucket[key]), g);
290         return g;
291 }
292
293 void blf_glyph_free(GlyphBLF *g)
294 {
295         /* don't need free the texture, the GlyphCache already
296          * have a list of all the texture and free it.
297          */
298         if (g->bitmap)
299                 MEM_freeN(g->bitmap);
300         MEM_freeN(g);
301 }
302
303 static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
304 {
305         /* When a string is being rendered as individual glyphs (as in the game
306          * engine), the leading edge needs to be raised a fraction to prevent
307          * z-fighting for kerned characters. - z0r */
308         const float twist = (dx1 - dx) * 0.0002f;
309
310         glBegin(GL_QUADS);
311         glTexCoord2f(uv[0][0], uv[0][1]);
312         glVertex3f(dx, y1, twist);
313         
314         glTexCoord2f(uv[0][0], uv[1][1]);
315         glVertex3f(dx, y2, twist);
316         
317         glTexCoord2f(uv[1][0], uv[1][1]);
318         glVertex2f(dx1, y2);
319         
320         glTexCoord2f(uv[1][0], uv[0][1]);
321         glVertex2f(dx1, y1);
322         glEnd();
323         
324 }
325
326 static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2)
327 {
328         float soft[25]= {1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f,
329                          1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f,
330                          2/60.0f, 5/60.0f, 8/60.0f, 5/60.0f, 2/60.0f,
331                          1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f,
332                          1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f};
333         
334         float color[4], *fp= soft;
335         int dx, dy;
336
337         color[0]= shadow_col[0];
338         color[1]= shadow_col[1];
339         color[2]= shadow_col[2];
340         
341         for(dx=-2; dx<3; dx++) {
342                 for(dy=-2; dy<3; dy++, fp++) {
343                         color[3]= *(fp) * shadow_col[3];
344                         glColor4fv(color);
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(const float shadow_col[4], 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,
355                         2/16.0f,4/16.0f, 2/16.0f,
356                         1/16.0f, 2/16.0f, 1/16.0f};
357
358         float color[4], *fp= soft;
359         int dx, dy;
360
361         color[0]= shadow_col[0];
362         color[1]= shadow_col[1];
363         color[2]= shadow_col[2];
364
365         for(dx=-1; dx<2; dx++) {
366                 for(dy=-1; dy<2; dy++, fp++) {
367                         color[3]= *(fp) * shadow_col[3];
368                         glColor4fv(color);
369                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
370                 }
371         }
372         
373         glColor4fv(color);
374 }
375
376 int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
377 {
378         float dx, dx1;
379         float y1, y2;
380         float xo, yo;
381
382         if ((!g->width) || (!g->height))
383                 return 1;
384
385         if (g->build_tex == 0) {
386                 GlyphCacheBLF *gc= font->glyph_cache;
387
388                 if (font->max_tex_size == -1)
389                         glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint *)&font->max_tex_size);
390
391                 if (gc->cur_tex == -1) {
392                         blf_glyph_cache_texture(font, gc);
393                         gc->x_offs= gc->pad;
394                         gc->y_offs= gc->pad;
395                 }
396
397                 if (gc->x_offs > (gc->p2_width - gc->max_glyph_width)) {
398                         gc->x_offs= gc->pad;
399                         gc->y_offs += gc->max_glyph_height;
400
401                         if (gc->y_offs > (gc->p2_height - gc->max_glyph_height)) {
402                                 gc->y_offs= gc->pad;
403                                 blf_glyph_cache_texture(font, gc);
404                         }
405                 }
406
407                 g->tex= gc->textures[gc->cur_tex];
408                 g->xoff= gc->x_offs;
409                 g->yoff= gc->y_offs;
410
411                 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
412                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
413                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
414                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
415
416                 glBindTexture(GL_TEXTURE_2D, g->tex);
417                 glTexSubImage2D(GL_TEXTURE_2D, 0, g->xoff, g->yoff, g->width, g->height, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap);
418                 if (font->flags & BLF_TEXFILTER) {
419                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
420                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL,
421                                         _BLF_MIPMAP_LEVELS);
422                         glGenerateMipmap(GL_TEXTURE_2D);
423                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
424                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
425                                         GL_LINEAR_MIPMAP_LINEAR);
426                 }
427                 glPopClientAttrib();
428
429                 g->uv[0][0]= ((float)g->xoff) / ((float)gc->p2_width);
430                 g->uv[0][1]= ((float)g->yoff) / ((float)gc->p2_height);
431                 g->uv[1][0]= ((float)(g->xoff + g->width)) / ((float)gc->p2_width);
432                 g->uv[1][1]= ((float)(g->yoff + g->height)) / ((float)gc->p2_height);
433
434                 /* update the x offset for the next glyph. */
435                 gc->x_offs += (int)(g->box.xmax - g->box.xmin + gc->pad);
436
437                 gc->rem_glyphs--;
438                 g->build_tex= 1;
439         }
440
441         xo= 0.0f;
442         yo= 0.0f;
443
444         if (font->flags & BLF_SHADOW) {
445                 xo= x;
446                 yo= y;
447                 x += font->shadow_x;
448                 y += font->shadow_y;
449         }
450
451         dx= floor(x + g->pos_x);
452         dx1= dx + g->width;
453         y1= y + g->pos_y;
454         y2= y + g->pos_y - g->height;
455
456         if (font->flags & BLF_CLIPPING) {
457                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
458                         return 0;
459                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
460                         return 0;
461                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
462                         return 0;
463                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
464                         return 0;
465         }
466
467         if (font->tex_bind_state != g->tex) {
468                 glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state= g->tex));
469         }
470
471         if (font->flags & BLF_SHADOW) {
472
473                 switch(font->shadow) {
474                         case 3:
475                                 blf_texture3_draw(font->shadow_col, g->uv, dx, y1, dx1, y2);
476                                 break;
477                         case 5:
478                                 blf_texture5_draw(font->shadow_col, g->uv, dx, y1, dx1, y2);
479                                 break;
480                         default:
481                                 glColor4fv(font->shadow_col);
482                                 blf_texture_draw(g->uv, dx, y1, dx1, y2);
483                                 break;
484                 }
485
486                 glColor4fv(font->orig_col);
487
488                 x= xo;
489                 y= yo;
490
491                 dx= floor(x + g->pos_x);
492                 dx1= dx + g->width;
493                 y1= y + g->pos_y;
494                 y2= y + g->pos_y - g->height;
495         }
496
497         switch(font->blur) {
498                 case 3:
499                         blf_texture3_draw(font->orig_col, g->uv, dx, y1, dx1, y2);
500                         break;
501                 case 5:
502                         blf_texture5_draw(font->orig_col, g->uv, dx, y1, dx1, y2);
503                         break;
504                 default:
505                         blf_texture_draw(g->uv, dx, y1, dx1, y2);
506                         break;
507         }
508
509         return 1;
510 }