Blender V5.3
thumbs_font.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_hash_md5.hh"
10
11#include "IMB_imbuf.hh"
12#include "IMB_imbuf_types.hh"
13
14#include "IMB_thumbs.hh" /* own include. */
15
16/* XXX, bad level call */
18
19namespace blender {
20
21/* Only change if we need to update the previews in the on-disk cache. */
22#define FONT_THUMB_VERSION "1.0.1"
23
24ImBuf *IMB_thumb_load_font(const char *filepath, uint x, uint y)
25{
27
28 /* fill with white and zero alpha */
29 const float col[4] = {1.0f, 1.0f, 1.0f, 0.0f};
30 IMB_rectfill(ibuf, col);
31
32 if (!BLF_thumb_preview(filepath, ibuf->byte_data_for_write(), ibuf->x, ibuf->y, ibuf->channels))
33 {
34 IMB_freeImBuf(ibuf);
35 ibuf = nullptr;
36 }
37
38 return ibuf;
39}
40
42{
43 uchar digest[16];
45 r_hash[0] = '\0';
46 BLI_hash_md5_to_hexdigest(digest, r_hash);
47
48 return true;
49}
50
51ImBuf *IMB_font_preview(const char *filepath,
52 uint width,
53 const float color[4],
54 const char *sample_text)
55{
56 int font_id = (filepath[0] != '<') ? BLF_load(filepath) : 0;
57 if (font_id == -1) {
58 return nullptr;
59 }
60 const char default_sample[] = "ABCDabefg&0123";
61 const char *sample = sample_text ? sample_text : default_sample;
62
63 BLF_buffer_col(font_id, color);
64
65 BLF_size(font_id, 50.0f);
66 float name_w;
67 float name_h;
68 BLF_width_and_height(font_id, sample, strlen(sample), &name_w, &name_h);
69 const float scale = float(width) / name_w * 0.98f;
70 BLF_size(font_id, scale * 50.0f);
71 name_w *= scale;
72 name_h *= scale;
73
74 const int height = int(name_h * 1.8f);
75 ImBuf *ibuf = IMB_allocImBuf(width, height, ImBufFlags::ByteData);
76 /* fill with white and zero alpha */
77 const float col[4] = {1.0f, 1.0f, 1.0f, 0.0f};
78 IMB_rectfill(ibuf, col);
79
80 BLF_buffer(font_id, nullptr, ibuf->byte_data_for_write(), width, height, 4, nullptr);
81
82 BLF_position(font_id, 0.0f, height * 0.3f, 0.0f);
83 BLF_draw_buffer(font_id, sample, 1024);
84
85 BLF_buffer(font_id, nullptr, nullptr, 0, 0, 4, nullptr);
86
87 if (font_id != 0) {
88 BLF_unload_id(font_id);
89 }
90
91 return ibuf;
92}
93
94} // namespace blender
float3x3 scale(float3x3 mat, float2 scale)
BMesh Iterators.
ImBuf * IMB_font_preview(const char *filepath, unsigned int width, const float color[4], const char *sample_text=nullptr)
void * BLI_hash_md5_buffer(const char *buffer, size_t len, void *resblock)
Definition hash_md5.cc:345
void BLF_position(int fontid, float x, float y, float z)
Definition blf.cc:390
bool BLF_thumb_preview(const char *filepath, unsigned char *buf, int w, int h, int channels) ATTR_NONNULL()
unsigned int uint
Definition BKE_text.h:55
void BLF_buffer(int fontid, float *fbuf, unsigned char *cbuf, int w, int h, int channel_count, const ColorSpace *colorspace)
Definition blf.cc:989
void IMB_rectfill(ImBuf *drect, const float col[4])
Definition rectop.cc:1165
bool IMB_thumb_load_font_get_hash(char *r_hash)
void BLF_size(int fontid, float size)
Definition blf.cc:445
void IMB_freeImBuf(ImBuf *ibuf)
int BLF_load(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition blf.cc:177
unsigned char uchar
Definition BKE_text.h:58
ImBuf * IMB_thumb_load_font(const char *filepath, unsigned int x, unsigned int y)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
void BLF_buffer_col(int fontid, const float srgb_color[4]) ATTR_NONNULL(2)
Definition blf.cc:1057
char * BLI_hash_md5_to_hexdigest(const void *resblock, char r_hex_digest[33])
Definition hash_md5.cc:381
void BLF_draw_buffer(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_NONNULL(2)
Definition blf.cc:1087
void BLF_width_and_height(int fontid, const char *str, size_t str_len, float *r_width, float *r_height) ATTR_NONNULL()
Definition blf.cc:822
bool BLF_unload_id(int fontid)
Definition blf.cc:282
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
#define FONT_THUMB_VERSION