Blender V5.0
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
19/* Only change if we need to update the previews in the on-disk cache. */
20#define FONT_THUMB_VERSION "1.0.1"
21
22ImBuf *IMB_thumb_load_font(const char *filepath, uint x, uint y)
23{
25
26 /* fill with white and zero alpha */
27 const float col[4] = {1.0f, 1.0f, 1.0f, 0.0f};
28 IMB_rectfill(ibuf, col);
29
30 if (!BLF_thumb_preview(filepath, ibuf->byte_buffer.data, ibuf->x, ibuf->y, ibuf->channels)) {
31 IMB_freeImBuf(ibuf);
32 ibuf = nullptr;
33 }
34
35 return ibuf;
36}
37
39{
40 uchar digest[16];
42 r_hash[0] = '\0';
43 BLI_hash_md5_to_hexdigest(digest, r_hash);
44
45 return true;
46}
47
48ImBuf *IMB_font_preview(const char *filepath,
49 uint width,
50 const float color[4],
51 const char *sample_text)
52{
53 int font_id = (filepath[0] != '<') ? BLF_load(filepath) : 0;
54 if (font_id == -1) {
55 return nullptr;
56 }
57 const char default_sample[] = "ABCDabefg&0123";
58 const char *sample = sample_text ? sample_text : default_sample;
59
60 BLF_buffer_col(font_id, color);
61
62 BLF_size(font_id, 50.0f);
63 float name_w;
64 float name_h;
65 BLF_width_and_height(font_id, sample, strlen(sample), &name_w, &name_h);
66 const float scale = float(width) / name_w * 0.98f;
67 BLF_size(font_id, scale * 50.0f);
68 name_w *= scale;
69 name_h *= scale;
70
71 const int height = int(name_h * 1.8f);
72 ImBuf *ibuf = IMB_allocImBuf(width, height, 32, IB_byte_data);
73 /* fill with white and zero alpha */
74 const float col[4] = {1.0f, 1.0f, 1.0f, 0.0f};
75 IMB_rectfill(ibuf, col);
76
77 BLF_buffer(font_id, ibuf->float_buffer.data, ibuf->byte_buffer.data, width, height, nullptr);
78
79 BLF_position(font_id, 0.0f, height * 0.3f, 0.0f);
80 BLF_draw_buffer(font_id, sample, 1024);
81
82 BLF_buffer(font_id, nullptr, nullptr, 0, 0, nullptr);
83
84 if (font_id != 0) {
85 BLF_unload_id(font_id);
86 }
87
88 return ibuf;
89}
void BLF_size(int fontid, float size)
Definition blf.cc:443
void BLF_buffer(int fontid, float *fbuf, unsigned char *cbuf, int w, int h, const ColorSpace *colorspace)
Definition blf.cc:956
void BLF_draw_buffer(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_NONNULL(2)
Definition blf.cc:1045
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:789
int BLF_load(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition blf.cc:175
void BLF_buffer_col(int fontid, const float srgb_color[4]) ATTR_NONNULL(2)
Definition blf.cc:1015
bool BLF_unload_id(int fontid)
Definition blf.cc:280
bool BLF_thumb_preview(const char *filepath, unsigned char *buf, int w, int h, int channels) ATTR_NONNULL()
void BLF_position(int fontid, float x, float y, float z)
Definition blf.cc:388
char * BLI_hash_md5_to_hexdigest(const void *resblock, char r_hex_digest[33])
Definition hash_md5.cc:379
void * BLI_hash_md5_buffer(const char *buffer, size_t len, void *resblock)
Definition hash_md5.cc:343
unsigned char uchar
unsigned int uint
void IMB_freeImBuf(ImBuf *ibuf)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
void IMB_rectfill(ImBuf *drect, const float col[4])
Definition rectop.cc:978
@ IB_byte_data
@ IB_metadata
nullptr float
uint col
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
ImBuf * IMB_thumb_load_font(const char *filepath, uint x, uint y)
bool IMB_thumb_load_font_get_hash(char *r_hash)
ImBuf * IMB_font_preview(const char *filepath, uint width, const float color[4], const char *sample_text)
#define FONT_THUMB_VERSION