Blender V4.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#include "BLI_utildefines.h"
11
12#include "IMB_imbuf.hh"
13#include "IMB_imbuf_types.hh"
14
15#include "IMB_thumbs.hh" /* own include. */
16
17/* XXX, bad level call */
19
20/* Only change if we need to update the previews in the on-disk cache. */
21#define FONT_THUMB_VERSION "1.0.1"
22
23ImBuf *IMB_thumb_load_font(const char *filepath, uint x, uint y)
24{
25 ImBuf *ibuf = IMB_allocImBuf(x, y, 32, IB_rect | IB_metadata);
26
27 /* fill with white and zero alpha */
28 const float col[4] = {1.0f, 1.0f, 1.0f, 0.0f};
29 IMB_rectfill(ibuf, col);
30
31 if (!BLF_thumb_preview(filepath, ibuf->byte_buffer.data, ibuf->x, ibuf->y, ibuf->channels)) {
32 IMB_freeImBuf(ibuf);
33 ibuf = nullptr;
34 }
35
36 return ibuf;
37}
38
40{
41 uchar digest[16];
43 r_hash[0] = '\0';
44 BLI_hash_md5_to_hexdigest(digest, r_hash);
45
46 return true;
47}
48
49ImBuf *IMB_font_preview(const char *filepath, uint width, const float color[4])
50{
51 int font_id = (filepath[0] != '<') ? BLF_load(filepath) : 0;
52 if (font_id == -1) {
53 return nullptr;
54 }
55 const char sample[] = "ABCDEFGH\nabcdefg123";
56
57 BLF_buffer_col(font_id, color);
58
59 BLF_size(font_id, 50.0f);
60 BLF_enable(font_id, BLF_WORD_WRAP);
61 float name_w;
62 float name_h;
63 BLF_width_and_height(font_id, sample, sizeof(sample), &name_w, &name_h);
64 float scale = float(width) / name_w;
65 BLF_size(font_id, scale * 50.0f);
66 name_w *= scale;
67 name_h *= scale;
68
69 int height = int(name_h * 1.3f);
70 ImBuf *ibuf = IMB_allocImBuf(width, height, 32, IB_rect);
71 /* fill with white and zero alpha */
72 const float col[4] = {1.0f, 1.0f, 1.0f, 0.0f};
73 IMB_rectfill(ibuf, col);
74
75 BLF_buffer(font_id, ibuf->float_buffer.data, ibuf->byte_buffer.data, width, height, nullptr);
76
77 BLF_position(font_id, 0.0f, name_h * 0.8f, 0.0f);
78 BLF_draw_buffer(font_id, sample, 1024);
79
80 BLF_buffer(font_id, nullptr, nullptr, 0, 0, nullptr);
81
82 if (font_id != 0) {
83 BLF_unload_id(font_id);
84 }
85
86 return ibuf;
87}
@ BLF_WORD_WRAP
Definition BLF_api.hh:367
void BLF_size(int fontid, float size)
Definition blf.cc:426
void BLF_draw_buffer(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_NONNULL(2)
Definition blf.cc:962
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:778
void BLF_buffer_col(int fontid, const float rgba[4]) ATTR_NONNULL(2)
Definition blf.cc:937
void BLF_buffer(int fontid, float *fbuf, unsigned char *cbuf, int w, int h, ColorManagedDisplay *display)
Definition blf.cc:924
void BLF_unload_id(int fontid)
Definition blf.cc:284
int BLF_load(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition blf.cc:174
void BLF_enable(int fontid, int option)
Definition blf.cc:312
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:371
char * BLI_hash_md5_to_hexdigest(const void *resblock, char r_hex_digest[33])
Definition hash_md5.cc:382
void * BLI_hash_md5_buffer(const char *buffer, size_t len, void *resblock)
Definition hash_md5.cc:346
unsigned char uchar
unsigned int uint
void IMB_rectfill(ImBuf *drect, const float col[4])
Definition rectop.cc:1051
Contains defines and structs used throughout the imbuf module.
@ IB_metadata
@ IB_rect
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 Brightness Control the brightness and contrast of the input color Vector Map input vector components with curves 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 Invert a color
draw_view in_light_buf[] float
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
uint col
struct ImBuf * IMB_allocImBuf(unsigned int, unsigned int, unsigned char, unsigned int)
void IMB_freeImBuf(ImBuf *)
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)
#define FONT_THUMB_VERSION
ImBuf * IMB_font_preview(const char *filepath, uint width, const float color[4])