1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2009 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup blf
22  *
23  * Glyph rendering, texturing and caching. Wraps Freetype and OpenGL functions.
24  */
25 
26 #include <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include <ft2build.h>
32 
33 #include FT_FREETYPE_H
34 #include FT_GLYPH_H
35 #include FT_OUTLINE_H
36 #include FT_BITMAP_H
37 
38 #include "MEM_guardedalloc.h"
39 
40 #include "DNA_userdef_types.h"
41 #include "DNA_vec_types.h"
42 
43 #include "BLI_listbase.h"
44 #include "BLI_rect.h"
45 #include "BLI_threads.h"
46 
47 #include "BLF_api.h"
48 
49 #include "GPU_capabilities.h"
50 #include "GPU_immediate.h"
51 
52 #include "blf_internal.h"
53 #include "blf_internal_types.h"
54 
55 #include "BLI_math_vector.h"
56 #include "BLI_strict_flags.h"
57 
blf_kerning_cache_find(FontBLF * font)58 KerningCacheBLF *blf_kerning_cache_find(FontBLF *font)
59 {
60   KerningCacheBLF *p;
61 
62   p = (KerningCacheBLF *)font->kerning_caches.first;
63   while (p) {
64     if (p->mode == font->kerning_mode) {
65       return p;
66     }
67     p = p->next;
68   }
69   return NULL;
70 }
71 
72 /* Create a new glyph cache for the current kerning mode. */
blf_kerning_cache_new(FontBLF * font,GlyphCacheBLF * gc)73 KerningCacheBLF *blf_kerning_cache_new(FontBLF *font, GlyphCacheBLF *gc)
74 {
75   KerningCacheBLF *kc;
76 
77   kc = (KerningCacheBLF *)MEM_callocN(sizeof(KerningCacheBLF), "blf_kerning_cache_new");
78   kc->next = NULL;
79   kc->prev = NULL;
80   kc->mode = font->kerning_mode;
81 
82   unsigned int i, j;
83   for (i = 0; i < 0x80; i++) {
84     for (j = 0; j < 0x80; j++) {
85       GlyphBLF *g = blf_glyph_search(gc, i);
86       if (!g) {
87         FT_UInt glyph_index = FT_Get_Char_Index(font->face, i);
88         g = blf_glyph_add(font, gc, glyph_index, i);
89       }
90       /* Can fail on certain fonts */
91       GlyphBLF *g_prev = blf_glyph_search(gc, j);
92 
93       FT_Vector delta = {
94           .x = 0,
95           .y = 0,
96       };
97       if (g && g_prev && FT_Get_Kerning(font->face, g_prev->idx, g->idx, kc->mode, &delta) == 0) {
98         kc->table[i][j] = (int)delta.x >> 6;
99       }
100       else {
101         kc->table[i][j] = 0;
102       }
103     }
104   }
105 
106   BLI_addhead(&font->kerning_caches, kc);
107   return kc;
108 }
109 
blf_kerning_cache_clear(FontBLF * font)110 void blf_kerning_cache_clear(FontBLF *font)
111 {
112   font->kerning_cache = NULL;
113   BLI_freelistN(&font->kerning_caches);
114 }
115 
blf_glyph_cache_find(FontBLF * font,unsigned int size,unsigned int dpi)116 GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, unsigned int size, unsigned int dpi)
117 {
118   GlyphCacheBLF *p;
119 
120   p = (GlyphCacheBLF *)font->cache.first;
121   while (p) {
122     if (p->size == size && p->dpi == dpi && (p->bold == ((font->flags & BLF_BOLD) != 0)) &&
123         (p->italic == ((font->flags & BLF_ITALIC) != 0))) {
124       return p;
125     }
126     p = p->next;
127   }
128   return NULL;
129 }
130 
131 /* Create a new glyph cache for the current size, dpi, bold, italic. */
blf_glyph_cache_new(FontBLF * font)132 GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
133 {
134   GlyphCacheBLF *gc;
135 
136   gc = (GlyphCacheBLF *)MEM_callocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new");
137   gc->next = NULL;
138   gc->prev = NULL;
139   gc->size = font->size;
140   gc->dpi = font->dpi;
141   gc->bold = ((font->flags & BLF_BOLD) != 0);
142   gc->italic = ((font->flags & BLF_ITALIC) != 0);
143 
144   memset(gc->glyph_ascii_table, 0, sizeof(gc->glyph_ascii_table));
145   memset(gc->bucket, 0, sizeof(gc->bucket));
146 
147   gc->glyphs_len_max = (int)font->face->num_glyphs;
148   gc->glyphs_len_free = (int)font->face->num_glyphs;
149   gc->ascender = ((float)font->face->size->metrics.ascender) / 64.0f;
150   gc->descender = ((float)font->face->size->metrics.descender) / 64.0f;
151 
152   if (FT_IS_SCALABLE(font->face)) {
153     gc->glyph_width_max = (int)((float)(font->face->bbox.xMax - font->face->bbox.xMin) *
154                                 (((float)font->face->size->metrics.x_ppem) /
155                                  ((float)font->face->units_per_EM)));
156 
157     gc->glyph_height_max = (int)((float)(font->face->bbox.yMax - font->face->bbox.yMin) *
158                                  (((float)font->face->size->metrics.y_ppem) /
159                                   ((float)font->face->units_per_EM)));
160   }
161   else {
162     gc->glyph_width_max = (int)(((float)font->face->size->metrics.max_advance) / 64.0f);
163     gc->glyph_height_max = (int)(((float)font->face->size->metrics.height) / 64.0f);
164   }
165 
166   /* can happen with size 1 fonts */
167   CLAMP_MIN(gc->glyph_width_max, 1);
168   CLAMP_MIN(gc->glyph_height_max, 1);
169 
170   BLI_addhead(&font->cache, gc);
171   return gc;
172 }
173 
blf_glyph_cache_acquire(FontBLF * font)174 GlyphCacheBLF *blf_glyph_cache_acquire(FontBLF *font)
175 {
176   BLI_spin_lock(font->glyph_cache_mutex);
177 
178   GlyphCacheBLF *gc = blf_glyph_cache_find(font, font->size, font->dpi);
179 
180   if (!gc) {
181     gc = blf_glyph_cache_new(font);
182   }
183 
184   return gc;
185 }
186 
blf_glyph_cache_release(FontBLF * font)187 void blf_glyph_cache_release(FontBLF *font)
188 {
189   BLI_spin_unlock(font->glyph_cache_mutex);
190 }
191 
blf_glyph_cache_clear(FontBLF * font)192 void blf_glyph_cache_clear(FontBLF *font)
193 {
194   GlyphCacheBLF *gc;
195 
196   BLI_spin_lock(font->glyph_cache_mutex);
197 
198   while ((gc = BLI_pophead(&font->cache))) {
199     blf_glyph_cache_free(gc);
200   }
201 
202   BLI_spin_unlock(font->glyph_cache_mutex);
203 }
204 
blf_glyph_cache_free(GlyphCacheBLF * gc)205 void blf_glyph_cache_free(GlyphCacheBLF *gc)
206 {
207   GlyphBLF *g;
208   for (uint i = 0; i < ARRAY_SIZE(gc->bucket); i++) {
209     while ((g = BLI_pophead(&gc->bucket[i]))) {
210       blf_glyph_free(g);
211     }
212   }
213   if (gc->texture) {
214     GPU_texture_free(gc->texture);
215   }
216   if (gc->bitmap_result) {
217     MEM_freeN(gc->bitmap_result);
218   }
219   MEM_freeN(gc);
220 }
221 
blf_glyph_search(GlyphCacheBLF * gc,unsigned int c)222 GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
223 {
224   GlyphBLF *p;
225   unsigned int key;
226 
227   key = blf_hash(c);
228   p = gc->bucket[key].first;
229   while (p) {
230     if (p->c == c) {
231       return p;
232     }
233     p = p->next;
234   }
235   return NULL;
236 }
237 
blf_glyph_add(FontBLF * font,GlyphCacheBLF * gc,unsigned int index,unsigned int c)238 GlyphBLF *blf_glyph_add(FontBLF *font, GlyphCacheBLF *gc, unsigned int index, unsigned int c)
239 {
240   FT_GlyphSlot slot;
241   GlyphBLF *g;
242   FT_Error err;
243   FT_Bitmap bitmap, tempbitmap;
244   FT_BBox bbox;
245   unsigned int key;
246 
247   g = blf_glyph_search(gc, c);
248   if (g) {
249     return g;
250   }
251 
252   /* glyphs are dynamically created as needed by font rendering. this means that
253    * to make font rendering thread safe we have to do locking here. note that this
254    * must be a lock for the whole library and not just per font, because the font
255    * renderer uses a shared buffer internally */
256   BLI_spin_lock(font->ft_lib_mutex);
257 
258   /* search again after locking */
259   g = blf_glyph_search(gc, c);
260   if (g) {
261     BLI_spin_unlock(font->ft_lib_mutex);
262     return g;
263   }
264 
265   int load_flags;
266   int render_mode;
267 
268   if (font->flags & BLF_MONOCHROME) {
269     load_flags = FT_LOAD_TARGET_MONO;
270     render_mode = FT_RENDER_MODE_MONO;
271   }
272   else {
273     load_flags = FT_LOAD_NO_BITMAP;
274     render_mode = FT_RENDER_MODE_NORMAL;
275     if (font->flags & BLF_HINTING_NONE) {
276       load_flags |= FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING;
277     }
278     else if (font->flags & BLF_HINTING_SLIGHT) {
279       load_flags |= FT_LOAD_TARGET_LIGHT;
280     }
281     else if (font->flags & BLF_HINTING_FULL) {
282       load_flags |= FT_LOAD_TARGET_NORMAL;
283     }
284     else {
285       /* Default, hinting disabled until FreeType has been upgraded
286        * to give good results on all platforms. */
287       load_flags |= FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING;
288     }
289   }
290 
291   err = FT_Load_Glyph(font->face, (FT_UInt)index, load_flags);
292 
293   /* Do not oblique a font that is designed to be italic! */
294   if (((font->flags & BLF_ITALIC) != 0) && !(font->face->style_flags & FT_STYLE_FLAG_ITALIC) &&
295       (font->face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)) {
296     /* For (fake) italic: a shear transform with a 6 degree angle. */
297     FT_Matrix transform;
298     transform.xx = 0x10000L;
299     transform.yx = 0x00000L;
300     transform.xy = 0x03000L;
301     transform.yy = 0x10000L;
302     FT_Outline_Transform(&font->face->glyph->outline, &transform);
303   }
304 
305   /* Do not embolden an already bold font! */
306   if (((font->flags & BLF_BOLD) != 0) &&
307       !(font->face->style_flags & FT_STYLE_FLAG_BOLD) &
308           (font->face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)) {
309     /* Strengthen the width more than the height. */
310     const FT_Pos extra_x = FT_MulFix(font->face->units_per_EM, font->face->size->metrics.x_scale) /
311                            14;
312     const FT_Pos extra_y = FT_MulFix(font->face->units_per_EM, font->face->size->metrics.y_scale) /
313                            28;
314     FT_Outline_EmboldenXY(&font->face->glyph->outline, extra_x, extra_y);
315     if ((font->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) == 0) {
316       /* Need to increase advance, but not for fixed-width fonts. */
317       font->face->glyph->advance.x += (FT_Pos)(((float)extra_x) * 1.05f);
318       font->face->glyph->advance.y += extra_y;
319     }
320     else {
321       /* Widened fixed-pitch font gets a nudge left. */
322       FT_Outline_Translate(&font->face->glyph->outline, (extra_x / -2), 0);
323     }
324   }
325 
326   if (err) {
327     BLI_spin_unlock(font->ft_lib_mutex);
328     return NULL;
329   }
330 
331   /* get the glyph. */
332   slot = font->face->glyph;
333   err = FT_Render_Glyph(slot, render_mode);
334 
335   if (font->flags & BLF_MONOCHROME) {
336     /* Convert result from 1 bit per pixel to 8 bit per pixel */
337     /* Accum errors for later, fine if not interested beyond "ok vs any error" */
338     FT_Bitmap_New(&tempbitmap);
339 
340     /* Does Blender use Pitch 1 always? It works so far */
341     err += FT_Bitmap_Convert(font->ft_lib, &slot->bitmap, &tempbitmap, 1);
342     err += FT_Bitmap_Copy(font->ft_lib, &tempbitmap, &slot->bitmap);
343     err += FT_Bitmap_Done(font->ft_lib, &tempbitmap);
344   }
345 
346   if (err || slot->format != FT_GLYPH_FORMAT_BITMAP) {
347     BLI_spin_unlock(font->ft_lib_mutex);
348     return NULL;
349   }
350 
351   g = (GlyphBLF *)MEM_callocN(sizeof(GlyphBLF), "blf_glyph_add");
352   g->c = c;
353   g->idx = (FT_UInt)index;
354   bitmap = slot->bitmap;
355   g->dims[0] = (int)bitmap.width;
356   g->dims[1] = (int)bitmap.rows;
357 
358   const int buffer_size = g->dims[0] * g->dims[1];
359 
360   if (buffer_size != 0) {
361     if (font->flags & BLF_MONOCHROME) {
362       /* Font buffer uses only 0 or 1 values, Blender expects full 0..255 range */
363       for (int i = 0; i < buffer_size; i++) {
364         bitmap.buffer[i] = bitmap.buffer[i] ? 255 : 0;
365       }
366     }
367 
368     g->bitmap = MEM_mallocN((size_t)buffer_size, "glyph bitmap");
369     memcpy(g->bitmap, bitmap.buffer, (size_t)buffer_size);
370   }
371 
372   g->advance = ((float)slot->advance.x) / 64.0f;
373   g->advance_i = (int)g->advance;
374   g->pos[0] = slot->bitmap_left;
375   g->pos[1] = slot->bitmap_top;
376   g->pitch = slot->bitmap.pitch;
377 
378   FT_Outline_Get_CBox(&(slot->outline), &bbox);
379   g->box.xmin = ((float)bbox.xMin) / 64.0f;
380   g->box.xmax = ((float)bbox.xMax) / 64.0f;
381   g->box.ymin = ((float)bbox.yMin) / 64.0f;
382   g->box.ymax = ((float)bbox.yMax) / 64.0f;
383 
384   key = blf_hash(g->c);
385   BLI_addhead(&(gc->bucket[key]), g);
386 
387   BLI_spin_unlock(font->ft_lib_mutex);
388 
389   return g;
390 }
391 
blf_glyph_free(GlyphBLF * g)392 void blf_glyph_free(GlyphBLF *g)
393 {
394   if (g->bitmap) {
395     MEM_freeN(g->bitmap);
396   }
397   MEM_freeN(g);
398 }
399 
blf_texture_draw(const unsigned char color[4],const int glyph_size[2],const int offset,float x1,float y1,float x2,float y2)400 static void blf_texture_draw(const unsigned char color[4],
401                              const int glyph_size[2],
402                              const int offset,
403                              float x1,
404                              float y1,
405                              float x2,
406                              float y2)
407 {
408   /* Only one vertex per glyph, geometry shader expand it into a quad. */
409   /* TODO Get rid of Geom Shader because it's not optimal AT ALL for the GPU */
410   copy_v4_fl4(GPU_vertbuf_raw_step(&g_batch.pos_step),
411               x1 + g_batch.ofs[0],
412               y1 + g_batch.ofs[1],
413               x2 + g_batch.ofs[0],
414               y2 + g_batch.ofs[1]);
415   copy_v4_v4_uchar(GPU_vertbuf_raw_step(&g_batch.col_step), color);
416   copy_v2_v2_int(GPU_vertbuf_raw_step(&g_batch.glyph_size_step), glyph_size);
417   *((int *)GPU_vertbuf_raw_step(&g_batch.offset_step)) = offset;
418 
419   g_batch.glyph_len++;
420   /* Flush cache if it's full. */
421   if (g_batch.glyph_len == BLF_BATCH_DRAW_LEN_MAX) {
422     blf_batch_draw();
423   }
424 }
425 
blf_texture5_draw(const unsigned char color_in[4],const int glyph_size[2],const int offset,float x1,float y1,float x2,float y2)426 static void blf_texture5_draw(const unsigned char color_in[4],
427                               const int glyph_size[2],
428                               const int offset,
429                               float x1,
430                               float y1,
431                               float x2,
432                               float y2)
433 {
434   int glyph_size_flag[2];
435   /* flag the x and y component signs for 5x5 blurring */
436   glyph_size_flag[0] = -glyph_size[0];
437   glyph_size_flag[1] = -glyph_size[1];
438 
439   blf_texture_draw(color_in, glyph_size_flag, offset, x1, y1, x2, y2);
440 }
441 
blf_texture3_draw(const unsigned char color_in[4],const int glyph_size[2],const int offset,float x1,float y1,float x2,float y2)442 static void blf_texture3_draw(const unsigned char color_in[4],
443                               const int glyph_size[2],
444                               const int offset,
445                               float x1,
446                               float y1,
447                               float x2,
448                               float y2)
449 {
450   int glyph_size_flag[2];
451   /* flag the x component sign for 3x3 blurring */
452   glyph_size_flag[0] = -glyph_size[0];
453   glyph_size_flag[1] = glyph_size[1];
454 
455   blf_texture_draw(color_in, glyph_size_flag, offset, x1, y1, x2, y2);
456 }
457 
blf_glyph_calc_rect(rctf * rect,GlyphBLF * g,float x,float y)458 static void blf_glyph_calc_rect(rctf *rect, GlyphBLF *g, float x, float y)
459 {
460   rect->xmin = floorf(x + (float)g->pos[0]);
461   rect->xmax = rect->xmin + (float)g->dims[0];
462   rect->ymin = floorf(y + (float)g->pos[1]);
463   rect->ymax = rect->ymin - (float)g->dims[1];
464 }
465 
blf_glyph_calc_rect_test(rctf * rect,GlyphBLF * g,float x,float y)466 static void blf_glyph_calc_rect_test(rctf *rect, GlyphBLF *g, float x, float y)
467 {
468   /* Intentionally check with g->advance, because this is the
469    * width used by BLF_width. This allows that the text slightly
470    * overlaps the clipping border to achieve better alignment. */
471   rect->xmin = floorf(x);
472   rect->xmax = rect->xmin + MIN2(g->advance, (float)g->dims[0]);
473   rect->ymin = floorf(y);
474   rect->ymax = rect->ymin - (float)g->dims[1];
475 }
476 
blf_glyph_calc_rect_shadow(rctf * rect,GlyphBLF * g,float x,float y,FontBLF * font)477 static void blf_glyph_calc_rect_shadow(rctf *rect, GlyphBLF *g, float x, float y, FontBLF *font)
478 {
479   blf_glyph_calc_rect(rect, g, x + (float)font->shadow_x, y + (float)font->shadow_y);
480 }
481 
blf_glyph_render(FontBLF * font,GlyphCacheBLF * gc,GlyphBLF * g,float x,float y)482 void blf_glyph_render(FontBLF *font, GlyphCacheBLF *gc, GlyphBLF *g, float x, float y)
483 {
484   if ((!g->dims[0]) || (!g->dims[1])) {
485     return;
486   }
487 
488   if (g->glyph_cache == NULL) {
489     if (font->tex_size_max == -1) {
490       font->tex_size_max = GPU_max_texture_size();
491     }
492 
493     g->offset = gc->bitmap_len;
494 
495     int buff_size = g->dims[0] * g->dims[1];
496     int bitmap_len = gc->bitmap_len + buff_size;
497 
498     if (bitmap_len > gc->bitmap_len_alloc) {
499       int w = font->tex_size_max;
500       int h = bitmap_len / w + 1;
501 
502       gc->bitmap_len_alloc = w * h;
503       gc->bitmap_result = MEM_reallocN(gc->bitmap_result, (size_t)gc->bitmap_len_alloc);
504 
505       /* Keep in sync with the texture. */
506       if (gc->texture) {
507         GPU_texture_free(gc->texture);
508       }
509       gc->texture = GPU_texture_create_1d_array(__func__, w, h, 1, GPU_R8, NULL);
510 
511       gc->bitmap_len_landed = 0;
512     }
513 
514     memcpy(&gc->bitmap_result[gc->bitmap_len], g->bitmap, (size_t)buff_size);
515     gc->bitmap_len = bitmap_len;
516 
517     gc->glyphs_len_free--;
518     g->glyph_cache = gc;
519   }
520 
521   if (font->flags & BLF_CLIPPING) {
522     rctf rect_test;
523     blf_glyph_calc_rect_test(&rect_test, g, x, y);
524     BLI_rctf_translate(&rect_test, font->pos[0], font->pos[1]);
525 
526     if (!BLI_rctf_inside_rctf(&font->clip_rec, &rect_test)) {
527       return;
528     }
529   }
530 
531   if (g_batch.glyph_cache != g->glyph_cache) {
532     blf_batch_draw();
533     g_batch.glyph_cache = g->glyph_cache;
534   }
535 
536   if (font->flags & BLF_SHADOW) {
537     rctf rect_ofs;
538     blf_glyph_calc_rect_shadow(&rect_ofs, g, x, y, font);
539 
540     if (font->shadow == 0) {
541       blf_texture_draw(font->shadow_color,
542                        g->dims,
543                        g->offset,
544                        rect_ofs.xmin,
545                        rect_ofs.ymin,
546                        rect_ofs.xmax,
547                        rect_ofs.ymax);
548     }
549     else if (font->shadow <= 4) {
550       blf_texture3_draw(font->shadow_color,
551                         g->dims,
552                         g->offset,
553                         rect_ofs.xmin,
554                         rect_ofs.ymin,
555                         rect_ofs.xmax,
556                         rect_ofs.ymax);
557     }
558     else {
559       blf_texture5_draw(font->shadow_color,
560                         g->dims,
561                         g->offset,
562                         rect_ofs.xmin,
563                         rect_ofs.ymin,
564                         rect_ofs.xmax,
565                         rect_ofs.ymax);
566     }
567   }
568 
569   rctf rect;
570   blf_glyph_calc_rect(&rect, g, x, y);
571 
572 #if BLF_BLUR_ENABLE
573   switch (font->blur) {
574     case 3:
575       blf_texture3_draw(
576           font->color, g->dims, g->offset, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
577       break;
578     case 5:
579       blf_texture5_draw(
580           font->color, g->dims, g->offset, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
581       break;
582     default:
583       blf_texture_draw(
584           font->color, g->dims, g->offset, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
585   }
586 #else
587   blf_texture_draw(font->color, g->dims, g->offset, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
588 #endif
589 }
590