Blender  V2.93
blf_thumbs.c
Go to the documentation of this file.
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 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include <ft2build.h>
30 
31 #include FT_FREETYPE_H
32 
33 #include "BLI_listbase.h"
34 #include "BLI_rect.h"
35 #include "BLI_threads.h"
36 #include "BLI_utildefines.h"
37 
38 #include "blf_internal.h"
39 #include "blf_internal_types.h"
40 
41 #include "BLF_api.h"
42 #include "BLT_translation.h"
43 
44 #include "BLI_strict_flags.h"
45 
51 void BLF_thumb_preview(const char *filename,
52  const char **draw_str,
53  const char **i18n_draw_str,
54  const unsigned char draw_str_lines,
55  const float font_color[4],
56  const int font_size,
57  unsigned char *buf,
58  int w,
59  int h,
60  int channels)
61 {
62  const unsigned int dpi = 72;
63  const int font_size_min = 6;
64  int font_size_curr;
65  /* shrink 1/th each line */
66  int font_shrink = 4;
67 
68  FontBLF *font;
69  GlyphCacheBLF *gc;
70 
71  /* Create a new blender font obj and fill it with default values */
72  font = blf_font_new("thumb_font", filename);
73  if (!font) {
74  printf("Info: Can't load font '%s', no preview possible\n", filename);
75  return;
76  }
77 
78  /* Would be done via the BLF API, but we're not using a fontid here */
79  font->buf_info.cbuf = buf;
80  font->buf_info.ch = channels;
81  font->buf_info.dims[0] = w;
82  font->buf_info.dims[1] = h;
83 
84  /* Always create the image with a white font,
85  * the caller can theme how it likes */
86  memcpy(font->buf_info.col_init, font_color, sizeof(font->buf_info.col_init));
87  font->pos[1] = (float)h;
88 
89  font_size_curr = font_size;
90 
92 
93  for (int i = 0; i < draw_str_lines; i++) {
94  const char *draw_str_i18n = i18n_draw_str[i] != NULL ? i18n_draw_str[i] : draw_str[i];
95  const size_t draw_str_i18n_len = strlen(draw_str_i18n);
96  int draw_str_i18n_nbr = 0;
97 
98  blf_font_size(font, (unsigned int)MAX2(font_size_min, font_size_curr), dpi);
99  gc = blf_glyph_cache_find(font, font->size, font->dpi);
100  /* There will be no matching glyph cache if blf_font_size() failed to set font size. */
101  if (!gc) {
102  break;
103  }
104 
105  /* decrease font size each time */
106  font_size_curr -= (font_size_curr / font_shrink);
107  font_shrink += 1;
108 
109  font->pos[1] -= gc->ascender * 1.1f;
110 
111  /* We fallback to default english strings in case not enough chars are available in current
112  * font for given translated string (useful in non-latin i18n context, like Chinese,
113  * since many fonts will then show nothing but ugly 'missing char' in their preview).
114  * Does not handle all cases, but much better than nothing.
115  */
116  if (blf_font_count_missing_chars(font, draw_str_i18n, draw_str_i18n_len, &draw_str_i18n_nbr) >
117  (draw_str_i18n_nbr / 2)) {
118  blf_font_draw_buffer(font, draw_str[i], strlen(draw_str[i]), NULL);
119  }
120  else {
121  blf_font_draw_buffer(font, draw_str_i18n, draw_str_i18n_len, NULL);
122  }
123  }
124 
126  blf_font_free(font);
127 }
typedef float(TangentPoint)[2]
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
#define MAX2(a, b)
void blf_draw_buffer__start(FontBLF *font)
Definition: blf.c:880
void blf_draw_buffer__end(void)
Definition: blf.c:894
void blf_font_draw_buffer(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
Definition: blf_font.c:705
FontBLF * blf_font_new(const char *name, const char *filename)
Definition: blf_font.c:1351
void blf_font_size(FontBLF *font, unsigned int size, unsigned int dpi)
Definition: blf_font.c:277
void blf_font_free(FontBLF *font)
Definition: blf_font.c:1279
int blf_font_count_missing_chars(FontBLF *font, const char *str, const size_t len, int *r_tot_chars)
Definition: blf_font.c:1254
GlyphCacheBLF * blf_glyph_cache_find(FontBLF *font, unsigned int size, unsigned int dpi)
Definition: blf_glyph.c:116
void BLF_thumb_preview(const char *filename, const char **draw_str, const char **i18n_draw_str, const unsigned char draw_str_lines, const float font_color[4], const int font_size, unsigned char *buf, int w, int h, int channels)
Definition: blf_thumbs.c:51
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
unsigned int dpi
unsigned int size
float pos[3]
FontBufInfoBLF buf_info
unsigned char * cbuf