UI: disable new text hinting from D3201 by default for now.
[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  * Glyph rendering, texturing and caching. Wraps Freetype and OpenGL functions.
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_listbase.h"
52 #include "BLI_rect.h"
53 #include "BLI_threads.h"
54
55 #include "BIF_gl.h"
56 #include "BLF_api.h"
57
58 #ifndef BLF_STANDALONE
59 #  include "GPU_basic_shader.h"
60 #endif
61
62 #include "blf_internal_types.h"
63 #include "blf_internal.h"
64
65 #include "BLI_strict_flags.h"
66
67 GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, unsigned int size, unsigned int dpi)
68 {
69         GlyphCacheBLF *p;
70
71         p = (GlyphCacheBLF *)font->cache.first;
72         while (p) {
73                 if (p->size == size && p->dpi == dpi)
74                         return p;
75                 p = p->next;
76         }
77         return NULL;
78 }
79
80 /* Create a new glyph cache for the current size and dpi. */
81 GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
82 {
83         GlyphCacheBLF *gc;
84
85         gc = (GlyphCacheBLF *)MEM_callocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new");
86         gc->next = NULL;
87         gc->prev = NULL;
88         gc->size = font->size;
89         gc->dpi = font->dpi;
90
91         memset(gc->glyph_ascii_table, 0, sizeof(gc->glyph_ascii_table));
92         memset(gc->bucket, 0, sizeof(gc->bucket));
93
94         gc->textures = (GLuint *)MEM_mallocN(sizeof(GLuint) * 256, __func__);
95         gc->textures_len = 256;
96         gc->texture_current = BLF_TEXTURE_UNSET;
97         gc->offset_x = 0;
98         gc->offset_y = 0;
99         gc->pad = 3;
100
101         gc->glyphs_len_max = (int)font->face->num_glyphs;
102         gc->glyphs_len_free = (int)font->face->num_glyphs;
103         gc->ascender = ((float)font->face->size->metrics.ascender) / 64.0f;
104         gc->descender = ((float)font->face->size->metrics.descender) / 64.0f;
105
106         if (FT_IS_SCALABLE(font->face)) {
107                 gc->glyph_width_max = (int)((float)(font->face->bbox.xMax - font->face->bbox.xMin) *
108                                             (((float)font->face->size->metrics.x_ppem) /
109                                              ((float)font->face->units_per_EM)));
110
111                 gc->glyph_height_max = (int)((float)(font->face->bbox.yMax - font->face->bbox.yMin) *
112                                              (((float)font->face->size->metrics.y_ppem) /
113                                               ((float)font->face->units_per_EM)));
114         }
115         else {
116                 gc->glyph_width_max = (int)(((float)font->face->size->metrics.max_advance) / 64.0f);
117                 gc->glyph_height_max = (int)(((float)font->face->size->metrics.height) / 64.0f);
118         }
119
120         /* can happen with size 1 fonts */
121         CLAMP_MIN(gc->glyph_width_max, 1);
122         CLAMP_MIN(gc->glyph_height_max, 1);
123
124         gc->p2_width = 0;
125         gc->p2_height = 0;
126
127         BLI_addhead(&font->cache, gc);
128         return gc;
129 }
130
131 void blf_glyph_cache_clear(FontBLF *font)
132 {
133         GlyphCacheBLF *gc;
134
135         while ((gc = BLI_pophead(&font->cache))) {
136                 blf_glyph_cache_free(gc);
137         }
138         font->glyph_cache = NULL;
139 }
140
141 void blf_glyph_cache_free(GlyphCacheBLF *gc)
142 {
143         GlyphBLF *g;
144         int i;
145
146         for (i = 0; i < 257; i++) {
147                 while ((g = BLI_pophead(&gc->bucket[i]))) {
148                         blf_glyph_free(g);
149                 }
150         }
151
152         if (gc->texture_current != BLF_TEXTURE_UNSET) {
153                 glDeleteTextures((int)gc->texture_current + 1, gc->textures);
154         }
155         MEM_freeN(gc->textures);
156         MEM_freeN(gc);
157 }
158
159 static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc)
160 {
161         int i;
162
163         /* move the index. */
164         gc->texture_current++;
165
166         if (UNLIKELY(gc->texture_current >= gc->textures_len)) {
167                 gc->textures_len *= 2;
168                 gc->textures = MEM_reallocN((void *)gc->textures, sizeof(GLuint) * gc->textures_len);
169         }
170
171         gc->p2_width = (int)blf_next_p2((unsigned int)((gc->glyphs_len_free * gc->glyph_width_max) + (gc->pad * 2)));
172         if (gc->p2_width > font->tex_size_max) {
173                 gc->p2_width = font->tex_size_max;
174         }
175
176         i = (int)((gc->p2_width - (gc->pad * 2)) / gc->glyph_width_max);
177         gc->p2_height = (int)blf_next_p2((unsigned int)(((gc->glyphs_len_max / i) + 1) * gc->glyph_height_max));
178
179         if (gc->p2_height > font->tex_size_max) {
180                 gc->p2_height = font->tex_size_max;
181         }
182
183         glGenTextures(1, &gc->textures[gc->texture_current]);
184         glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state = gc->textures[gc->texture_current]));
185         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
186         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
187         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
188         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
189
190 #ifndef BLF_STANDALONE
191         /* needed since basic shader doesn't support alpha-only textures,
192          * while we could add support this is only used in a few places
193          * (an alternative could be to have a simple shader for BLF). */
194         if (GLEW_ARB_texture_swizzle && GPU_basic_shader_use_glsl_get()) {
195                 GLint swizzle_mask[] = {GL_ONE, GL_ONE, GL_ONE, GL_ALPHA};
196                 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzle_mask);
197         }
198 #endif
199
200         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA8, gc->p2_width, gc->p2_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, NULL);
201 }
202
203 GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
204 {
205         GlyphBLF *p;
206         unsigned int key;
207
208         key = blf_hash(c);
209         p = gc->bucket[key].first;
210         while (p) {
211                 if (p->c == c)
212                         return p;
213                 p = p->next;
214         }
215         return NULL;
216 }
217
218 GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c)
219 {
220         FT_GlyphSlot slot;
221         GlyphBLF *g;
222         FT_Error err;
223         FT_Bitmap bitmap, tempbitmap;
224         FT_BBox bbox;
225         unsigned int key;
226
227         g = blf_glyph_search(font->glyph_cache, c);
228         if (g)
229                 return g;
230
231         /* glyphs are dynamically created as needed by font rendering. this means that
232          * to make font rendering thread safe we have to do locking here. note that this
233          * must be a lock for the whole library and not just per font, because the font
234          * renderer uses a shared buffer internally */
235         BLI_spin_lock(font->ft_lib_mutex);
236
237         /* search again after locking */
238         g = blf_glyph_search(font->glyph_cache, c);
239         if (g) {
240                 BLI_spin_unlock(font->ft_lib_mutex);
241                 return g;
242         }
243
244         if (font->flags & BLF_MONOCHROME) {
245                 err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO);
246         }
247         else {
248                 int flags = FT_LOAD_NO_BITMAP;
249
250                 if (font->flags & BLF_HINTING_NONE) {
251                         flags |= FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING;
252                 }
253                 else if (font->flags & BLF_HINTING_SLIGHT) {
254                         flags |= FT_LOAD_TARGET_LIGHT;
255                 }
256                 else if (font->flags & BLF_HINTING_FULL) {
257                         flags |= FT_LOAD_TARGET_NORMAL;
258                 }
259                 else {
260                         /* Default, hinting disabled until FreeType has been upgraded
261                          * to give good results on all platforms. */
262                         flags |= FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING;
263                 }
264
265                 err = FT_Load_Glyph(font->face, (FT_UInt)index, flags);
266         }
267
268         if (err) {
269                 BLI_spin_unlock(font->ft_lib_mutex);
270                 return NULL;
271         }
272
273         /* get the glyph. */
274         slot = font->face->glyph;
275
276         if (font->flags & BLF_MONOCHROME) {
277                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
278
279                 /* Convert result from 1 bit per pixel to 8 bit per pixel */
280                 /* Accum errors for later, fine if not interested beyond "ok vs any error" */
281                 FT_Bitmap_New(&tempbitmap);
282                 err += FT_Bitmap_Convert(font->ft_lib, &slot->bitmap, &tempbitmap, 1); /* Does Blender use Pitch 1 always? It works so far */
283                 err += FT_Bitmap_Copy(font->ft_lib, &tempbitmap, &slot->bitmap);
284                 err += FT_Bitmap_Done(font->ft_lib, &tempbitmap);
285         }
286         else {
287                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
288         }
289
290         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP) {
291                 BLI_spin_unlock(font->ft_lib_mutex);
292                 return NULL;
293         }
294
295         g = (GlyphBLF *)MEM_callocN(sizeof(GlyphBLF), "blf_glyph_add");
296         g->c = c;
297         g->idx = (FT_UInt)index;
298         g->offset_x = -1;
299         g->offset_y = -1;
300         bitmap = slot->bitmap;
301         g->width = (int)bitmap.width;
302         g->height = (int)bitmap.rows;
303
304         if (g->width && g->height) {
305                 if (font->flags & BLF_MONOCHROME) {
306                         /* Font buffer uses only 0 or 1 values, Blender expects full 0..255 range */
307                         int i;
308                         for (i = 0; i < (g->width * g->height); i++) {
309                                 bitmap.buffer[i] = bitmap.buffer[i] ? 255 : 0;
310                         }
311                 }
312
313                 g->bitmap = (unsigned char *)MEM_mallocN((size_t)(g->width * g->height), "glyph bitmap");
314                 memcpy((void *)g->bitmap, (void *)bitmap.buffer, (size_t)(g->width * g->height));
315         }
316
317         g->advance = ((float)slot->advance.x) / 64.0f;
318         g->advance_i = (int)g->advance;
319         g->pos_x = (float)slot->bitmap_left;
320         g->pos_y = (float)slot->bitmap_top;
321         g->pitch = slot->bitmap.pitch;
322
323         FT_Outline_Get_CBox(&(slot->outline), &bbox);
324         g->box.xmin = ((float)bbox.xMin) / 64.0f;
325         g->box.xmax = ((float)bbox.xMax) / 64.0f;
326         g->box.ymin = ((float)bbox.yMin) / 64.0f;
327         g->box.ymax = ((float)bbox.yMax) / 64.0f;
328
329         key = blf_hash(g->c);
330         BLI_addhead(&(font->glyph_cache->bucket[key]), g);
331
332         BLI_spin_unlock(font->ft_lib_mutex);
333
334         return g;
335 }
336
337 void blf_glyph_free(GlyphBLF *g)
338 {
339         /* don't need free the texture, the GlyphCache already
340          * have a list of all the texture and free it.
341          */
342         if (g->bitmap) {
343                 MEM_freeN(g->bitmap);
344         }
345         MEM_freeN(g);
346 }
347
348 static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
349 {
350         glBegin(GL_QUADS);
351         glTexCoord2f(uv[0][0], uv[0][1]);
352         glVertex2f(dx, y1);
353
354         glTexCoord2f(uv[0][0], uv[1][1]);
355         glVertex2f(dx, y2);
356
357         glTexCoord2f(uv[1][0], uv[1][1]);
358         glVertex2f(dx1, y2);
359
360         glTexCoord2f(uv[1][0], uv[0][1]);
361         glVertex2f(dx1, y1);
362         glEnd();
363 }
364
365 static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2)
366 {
367         const float soft[25] = {1 / 60.0f, 1 / 60.0f, 2 / 60.0f, 1 / 60.0f, 1 / 60.0f,
368                                 1 / 60.0f, 3 / 60.0f, 5 / 60.0f, 3 / 60.0f, 1 / 60.0f,
369                                 2 / 60.0f, 5 / 60.0f, 8 / 60.0f, 5 / 60.0f, 2 / 60.0f,
370                                 1 / 60.0f, 3 / 60.0f, 5 / 60.0f, 3 / 60.0f, 1 / 60.0f,
371                                 1 / 60.0f, 1 / 60.0f, 2 / 60.0f, 1 / 60.0f, 1 / 60.0f};
372
373         const float *fp = soft;
374         float color[4];
375         float dx, dy;
376
377         color[0] = shadow_col[0];
378         color[1] = shadow_col[1];
379         color[2] = shadow_col[2];
380
381         for (dx = -2; dx < 3; dx++) {
382                 for (dy = -2; dy < 3; dy++, fp++) {
383                         color[3] = *(fp) * shadow_col[3];
384                         glColor4fv(color);
385                         blf_texture_draw(uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy);
386                 }
387         }
388
389         glColor4fv(color);
390 }
391
392 static void blf_texture3_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2)
393 {
394         const float soft[9] = {1 / 16.0f, 2 / 16.0f, 1 / 16.0f,
395                                2 / 16.0f, 4 / 16.0f, 2 / 16.0f,
396                                1 / 16.0f, 2 / 16.0f, 1 / 16.0f};
397
398         const float *fp = soft;
399         float color[4];
400         float dx, dy;
401
402         color[0] = shadow_col[0];
403         color[1] = shadow_col[1];
404         color[2] = shadow_col[2];
405
406         for (dx = -1; dx < 2; dx++) {
407                 for (dy = -1; dy < 2; dy++, fp++) {
408                         color[3] = *(fp) * shadow_col[3];
409                         glColor4fv(color);
410                         blf_texture_draw(uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy);
411                 }
412         }
413
414         glColor4fv(color);
415 }
416
417 static void blf_glyph_calc_rect(rctf *rect, GlyphBLF *g, float x, float y)
418 {
419         rect->xmin = floorf(x + g->pos_x);
420         rect->xmax = rect->xmin + (float)g->width;
421         rect->ymin = y + g->pos_y;
422         rect->ymax = y + g->pos_y - (float)g->height;
423 }
424
425 void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
426 {
427         rctf rect;
428
429         if ((!g->width) || (!g->height))
430                 return;
431
432         if (g->build_tex == 0) {
433                 GlyphCacheBLF *gc = font->glyph_cache;
434
435                 if (font->tex_size_max == -1)
436                         glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint *)&font->tex_size_max);
437
438                 if (gc->texture_current == BLF_TEXTURE_UNSET) {
439                         blf_glyph_cache_texture(font, gc);
440                         gc->offset_x = gc->pad;
441                         gc->offset_y = 0;
442                 }
443
444                 if (gc->offset_x > (gc->p2_width - gc->glyph_width_max)) {
445                         gc->offset_x = gc->pad;
446                         gc->offset_y += gc->glyph_height_max;
447
448                         if (gc->offset_y > (gc->p2_height - gc->glyph_height_max)) {
449                                 gc->offset_y = 0;
450                                 blf_glyph_cache_texture(font, gc);
451                         }
452                 }
453
454                 g->tex = gc->textures[gc->texture_current];
455                 g->offset_x = gc->offset_x;
456                 g->offset_y = gc->offset_y;
457
458                 /* prevent glTexSubImage2D from failing if the character
459                  * asks for pixels out of bounds, this tends only to happen
460                  * with very small sizes (5px high or less) */
461                 if (UNLIKELY((g->offset_x + g->width)  > gc->p2_width)) {
462                         g->width  -= (g->offset_x + g->width)  - gc->p2_width;
463                         BLI_assert(g->width > 0);
464                 }
465                 if (UNLIKELY((g->offset_y + g->height) > gc->p2_height)) {
466                         g->height -= (g->offset_y + g->height) - gc->p2_height;
467                         BLI_assert(g->height > 0);
468                 }
469
470
471                 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
472                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
473                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
474                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
475
476                 glBindTexture(GL_TEXTURE_2D, g->tex);
477                 glTexSubImage2D(GL_TEXTURE_2D, 0, g->offset_x, g->offset_y, g->width, g->height, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap);
478                 glPopClientAttrib();
479
480                 g->uv[0][0] = ((float)g->offset_x) / ((float)gc->p2_width);
481                 g->uv[0][1] = ((float)g->offset_y) / ((float)gc->p2_height);
482                 g->uv[1][0] = ((float)(g->offset_x + g->width)) / ((float)gc->p2_width);
483                 g->uv[1][1] = ((float)(g->offset_y + g->height)) / ((float)gc->p2_height);
484
485                 /* update the x offset for the next glyph. */
486                 gc->offset_x += (int)BLI_rctf_size_x(&g->box) + gc->pad;
487
488                 gc->glyphs_len_free--;
489                 g->build_tex = 1;
490         }
491
492         blf_glyph_calc_rect(&rect, g, x, y);
493
494         if (font->flags & BLF_CLIPPING) {
495                 /* intentionally check clipping without shadow offset */
496                 rctf rect_test = rect;
497                 BLI_rctf_translate(&rect_test, font->pos[0], font->pos[1]);
498
499                 if (!BLI_rctf_inside_rctf(&font->clip_rec, &rect_test)) {
500                         return;
501                 }
502         }
503
504         if (font->tex_bind_state != g->tex) {
505                 glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state = g->tex));
506         }
507
508         if (font->flags & BLF_SHADOW) {
509                 rctf rect_ofs;
510                 blf_glyph_calc_rect(&rect_ofs, g,
511                                     x + (float)font->shadow_x,
512                                     y + (float)font->shadow_y);
513
514                 switch (font->shadow) {
515                         case 3:
516                                 blf_texture3_draw(font->shadow_col, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
517                                 break;
518                         case 5:
519                                 blf_texture5_draw(font->shadow_col, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
520                                 break;
521                         default:
522                                 glColor4fv(font->shadow_col);
523                                 blf_texture_draw(g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
524                                 break;
525                 }
526
527                 glColor4fv(font->orig_col);
528         }
529
530         switch (font->blur) {
531                 case 3:
532                         blf_texture3_draw(font->orig_col, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
533                         break;
534                 case 5:
535                         blf_texture5_draw(font->orig_col, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
536                         break;
537                 default:
538                         blf_texture_draw(g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
539                         break;
540         }
541
542         return;
543 }