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