90 const int total_width,
95 return float(-start_offset);
97 return -start_offset + (total_width - line_width) / 2.0f;
99 return float(-start_offset + total_width - line_width);
103 return float(-start_offset);
112 const int line_index,
113 const int line_height,
116 return (lines_count - 1 - line_index) * line_height -
float(descender);
122 const int total_width,
127 return start_offset + total_width / 2.0f;
129 return float(start_offset);
131 return start_offset - total_width / 2.0f;
135 return float(start_offset);
141 const int line_height,
147 return -total_height / 2.0f;
149 return -total_height / 2.0f + line_height + descender;
153 return total_height / 2.0f + descender;
155 return total_height / 2.0f;
159 return float(descender);
163 const std::string
string,
168 const std::optional<int> wrap_width)
171 if (
string.empty() || !font || size <= 0.0f) {
175 const int font_identifier =
load_font(font);
176 if (font_identifier == -1) {
195 int start_offset = std::numeric_limits<int>::max();
196 for (
const int64_t i : lines.index_range()) {
197 rcti line_bounding_box;
198 BLF_boundbox(font_identifier, lines[i].
data(), lines[i].size(), &line_bounding_box);
200 total_width =
math::max(total_width, line_widths[i]);
201 start_offset =
math::min(start_offset, line_bounding_box.
xmin);
205 const int total_height = line_height * lines.size();
211 [&](
const int2 texel) { result_cpu.store_pixel(texel, 0.0f); });
224 for (
const int64_t i : lines.index_range()) {
226 lines.size(), i, line_height, descender);
228 start_offset, line_widths[i], total_width, horizontal_alignment);
229 BLF_position(font_identifier, horizontal_position, vertical_position, 0.0f);
233 BLF_buffer(font_identifier,
nullptr,
nullptr, 0, 0, 1,
nullptr);
237 start_offset, total_width, horizontal_alignment);
239 total_height, line_height, descender, vertical_alignment);
242 if (context.use_gpu()) {
266 map_.remove_if([](
auto item) {
return !item.value->needed; });
270 for (
auto &value : map_.values()) {
271 value->needed =
false;
276 const std::string
string,
281 const std::optional<int> wrap_width)
284 string, font, size, horizontal_alignment, vertical_alignment, wrap_width);
286 auto &string_image = *map_.lookup_or_add_cb(key, [&]() {
287 return std::make_unique<StringImage>(
288 context,
string, font, size, horizontal_alignment, vertical_alignment, wrap_width);
291 string_image.needed =
true;
292 return string_image.result;
#define BLI_SCOPED_DEFER(function_to_defer)
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
#define BLI_assert_unreachable()
void share_data(const Result &source)
Result upload_to_gpu(const bool from_pool) const
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
GMutableSpan cpu_data_for_write()
Result & get(Context &context, const std::string string, const VFont *font, const float size, const HorizontalAlignment horizontal_alignment, const VerticalAlignment vertical_alignment, const std::optional< int > wrap_width)
StringImageKey(const std::string string, const VFont *font, const float size, const HorizontalAlignment horizontal_alignment, const VerticalAlignment vertical_alignment, const std::optional< int > wrap_width)
const VerticalAlignment vertical_alignment
const HorizontalAlignment horizontal_alignment
const std::optional< int > wrap_width
StringImage(Context &context, const std::string string, const VFont *font, const float size, const HorizontalAlignment horizontal_alignment, const VerticalAlignment vertical_alignment, const std::optional< int > wrap_width)
static float compute_vertical_offset(const int total_height, const int line_height, const int descender, const VerticalAlignment alignment)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
static float compute_draw_vertical_position(const int lines_count, const int line_index, const int line_height, const int descender)
static float compute_horizontal_offset(const int start_offset, const int total_width, const HorizontalAlignment alignment)
void parallel_for(const int2 range, const Function &function)
static int load_font(const VFont *font)
static float compute_draw_horizontal_position(const int start_offset, const int line_width, const int total_width, const HorizontalAlignment alignment)
T min(const T &a, const T &b)
T max(const T &a, const T &b)
void BLF_boundbox(int fontid, const char *str, size_t str_len, rcti *r_box, ResultBLF *r_info=nullptr) ATTR_NONNULL(2)
unsigned __int64 uint64_t
char * STRNCPY(char(&dst)[N], const char *src)
int BLF_load_default(bool unique)
void BLF_position(int fontid, float x, float y, float z)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
bool BKE_vfont_is_builtin(const VFont *vfont)
void BLF_buffer(int fontid, float *fbuf, unsigned char *cbuf, int w, int h, int channel_count, const ColorSpace *colorspace)
void BLF_size(int fontid, float size)
BMesh const char void * data
Vector< StringRef > BLF_string_wrap(int fontid, StringRef str, const int max_pixel_width, BLFWrapMode mode=BLFWrapMode::Minimal)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
int BLF_load_mem_unique(const char *name, const unsigned char *mem, int mem_size) ATTR_NONNULL(1
void BLF_buffer_col(int fontid, const float srgb_color[4]) ATTR_NONNULL(2)
int BLF_height_max(int fontid) ATTR_WARN_UNUSED_RESULT
int BLF_descender(int fontid) ATTR_WARN_UNUSED_RESULT
int BLF_load_unique(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLF_draw_buffer(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_NONNULL(2)
void BKE_id_full_name_get(char name[MAX_ID_FULL_NAME], const ID *id, char separator_char)
bool BLF_is_builtin(int fontid)
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
void BLF_enable(int fontid, FontFlags flag)
bool BLF_unload_id(int fontid)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct PackedFile * packedfile