Blender  V3.3
blf_thumbs.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include <ft2build.h>
16 
17 #include FT_FREETYPE_H
18 
19 #include "BLI_listbase.h"
20 #include "BLI_rect.h"
21 #include "BLI_threads.h"
22 #include "BLI_utildefines.h"
23 
24 #include "blf_internal.h"
25 #include "blf_internal_types.h"
26 
27 #include "BLF_api.h"
28 #include "BLT_translation.h"
29 
30 #include "BLI_strict_flags.h"
31 
32 void BLF_thumb_preview(const char *filepath,
33  const char **draw_str,
34  const char **i18n_draw_str,
35  const unsigned char draw_str_lines,
36  const float font_color[4],
37  const int font_size,
38  unsigned char *buf,
39  int w,
40  int h,
41  int channels)
42 {
43  const unsigned int dpi = 72;
44  const int font_size_min = 6;
45  int font_size_curr;
46  /* shrink 1/th each line */
47  int font_shrink = 4;
48 
49  FontBLF *font;
50 
51  /* Create a new blender font obj and fill it with default values */
52  font = blf_font_new("thumb_font", filepath);
53  if (!font) {
54  printf("Info: Can't load font '%s', no preview possible\n", filepath);
55  return;
56  }
57 
58  /* Would be done via the BLF API, but we're not using a fontid here */
59  font->buf_info.cbuf = buf;
60  font->buf_info.ch = channels;
61  font->buf_info.dims[0] = w;
62  font->buf_info.dims[1] = h;
63 
64  /* Always create the image with a white font,
65  * the caller can theme how it likes */
66  memcpy(font->buf_info.col_init, font_color, sizeof(font->buf_info.col_init));
67  font->pos[1] = h;
68 
69  font_size_curr = font_size;
70 
72 
73  for (int i = 0; i < draw_str_lines; i++) {
74  const char *draw_str_i18n = i18n_draw_str[i] != NULL ? i18n_draw_str[i] : draw_str[i];
75  const size_t draw_str_i18n_len = strlen(draw_str_i18n);
76  int draw_str_i18_count = 0;
77 
78  CLAMP_MIN(font_size_curr, font_size_min);
79  if (!blf_font_size(font, (float)font_size_curr, dpi)) {
80  break;
81  }
82 
83  /* decrease font size each time */
84  font_size_curr -= (font_size_curr / font_shrink);
85  font_shrink += 1;
86 
87  font->pos[1] -= (int)((float)blf_font_ascender(font) * 1.1f);
88 
89  /* We fallback to default english strings in case not enough chars are available in current
90  * font for given translated string (useful in non-Latin i18n context, like Chinese,
91  * since many fonts will then show nothing but ugly 'missing char' in their preview).
92  * Does not handle all cases, but much better than nothing.
93  */
94  if (blf_font_count_missing_chars(font, draw_str_i18n, draw_str_i18n_len, &draw_str_i18_count) >
95  (draw_str_i18_count / 2)) {
96  blf_font_draw_buffer(font, draw_str[i], strlen(draw_str[i]), NULL);
97  }
98  else {
99  blf_font_draw_buffer(font, draw_str_i18n, draw_str_i18n_len, NULL);
100  }
101  }
102 
104  blf_font_free(font);
105 }
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
#define CLAMP_MIN(a, b)
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a producing a negative Combine Generate a color from its and blue channels(Deprecated)") DefNode(ShaderNode
void blf_draw_buffer__start(FontBLF *font)
Definition: blf.c:845
void blf_draw_buffer__end(void)
Definition: blf.c:859
int blf_font_ascender(FontBLF *font)
Definition: blf_font.c:1153
int blf_font_count_missing_chars(FontBLF *font, const char *str, const size_t str_len, int *r_tot_chars)
Definition: blf_font.c:1074
void blf_font_draw_buffer(FontBLF *font, const char *str, const size_t str_len, struct ResultBLF *r_info)
Definition: blf_font.c:557
void blf_font_free(FontBLF *font)
Definition: blf_font.c:1380
FontBLF * blf_font_new(const char *name, const char *filepath)
Definition: blf_font.c:1247
bool blf_font_size(FontBLF *font, float size, unsigned int dpi)
Definition: blf_font.c:1408
void BLF_thumb_preview(const char *filepath, 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:32
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
FontBufInfoBLF buf_info
unsigned char * cbuf