176 if (
data->text_size < 1.0f ||
177 ((
data->color[3] == 0.0f) &&
179 (
data->outline_color[3] == 0.0f ||
data->outline_width <= 0.0f ||
190 strip->effectdata =
data;
192 data->text_font =
nullptr;
193 data->text_blf_id = -1;
194 data->text_size = 60.0f;
195 data->space_line = 1.0f;
196 data->abs_space_line = 60.0f;
199 data->shadow_color[3] = 0.7f;
201 data->shadow_offset = 0.04f;
202 data->shadow_blur = 0.0f;
203 data->box_color[0] = 0.2f;
204 data->box_color[1] = 0.2f;
205 data->box_color[2] = 0.2f;
206 data->box_color[3] = 0.7f;
207 data->box_margin = 0.01f;
208 data->box_roundness = 0.0f;
209 data->outline_color[3] = 0.7f;
210 data->outline_width = 0.05f;
213 data->text_len_bytes = strlen(
data->text_ptr);
214 data->cursor_offset =
data->text_len_bytes;
221 data->wrap_width = 1.0f;
226 if (
data ==
nullptr) {
231 if (do_id_user &&
data->text_font !=
nullptr) {
233 data->text_font =
nullptr;
237 if (
data->text_blf_id >= 0) {
239 data->text_blf_id = -1;
253 data->text_font = font;
259 if (vfont ==
nullptr) {
295 MEM_SAFE_DELETE(
data->text_ptr);
296 MEM_delete(
data->runtime);
298 strip->effectdata =
nullptr;
307 data->runtime =
nullptr;
308 data->text_blf_id = -1;
311 dst->effectdata =
data;
332 const rcti &shadow_rect)
334 dst +=
int64_t(start_line) * width;
335 for (
int y = start_line;
y < start_line + height;
y++) {
336 for (
int x = 0;
x < width;
x++) {
338 if (
x >= shadow_rect.
xmin &&
x <= shadow_rect.
xmax) {
339 float accum_weight = 0.0f;
342 for (
int nx = xmin,
index = (xmin -
x) + half_size; nx <= xmax; nx++,
index++) {
343 float weight = gaussian[
index];
344 int offset =
y * width + nx;
345 accum += rect[offset] * weight;
346 accum_weight += weight;
348 accum *= (1.0f / accum_weight);
364 const rcti &shadow_rect)
366 dst +=
int64_t(start_line) * width;
367 for (
int y = start_line;
y < start_line + height;
y++) {
368 for (
int x = 0;
x < width;
x++) {
370 if (
x >= shadow_rect.
xmin &&
x <= shadow_rect.
xmax) {
371 float accum_weight = 0.0f;
374 for (
int ny = ymin,
index = (ymin -
y) + half_size; ny <= ymax; ny++,
index++) {
375 float weight = gaussian[
index];
376 int offset = ny * width +
x;
377 accum += rect[offset] * weight;
378 accum_weight += weight;
380 accum *= (1.0f / accum_weight);
399 const rcti &shadow_rect,
407 for (
int x = shadow_rect.
xmin;
x <= shadow_rect.
xmax;
x++) {
409 size_t src_offset = width * src_y + src_x;
410 size_t dst_offset = width *
y +
x;
411 r_shadow_mask[dst_offset] = input[src_offset * 4 + 3];
418 const rcti &shadow_rect,
419 const float4 &shadow_color,
426 size_t offset =
y * width + shadow_rect.
xmin;
428 for (
int x = shadow_rect.
xmin;
x <= shadow_rect.
xmax;
x++, offset++, dst += 4) {
429 uchar a = shadow_mask[offset];
435 float4 col2 = shadow_color * (a * (1.0f / 255.0f));
437 float fac = 1.0f - col1.w;
438 float4 col = col1 + fac * col2;
448 const int width = context->rectx;
449 const int height = context->recty;
451 const float blur_amount = line_height * 0.5f *
data->shadow_blur;
452 bool do_blur = blur_amount >= 1.0f;
457 sinf(
data->shadow_angle) * line_height *
data->shadow_offset);
459 rcti shadow_rect = rect;
469 const int half_size = int(blur_amount + 0.5f);
472 BLI_rcti_pad(&shadow_rect, half_size + 1, half_size + 1);
479 const int y_first = y_range.
first();
480 const int y_size = y_range.
size();
493 const int y_first = y_range.
first();
494 const int y_size = y_range.
size();
534 for (
const int64_t y : sub_y_range) {
535 size_t index =
y * size.x;
542 float minimum_squared_distance = std::numeric_limits<float>::max();
543 for (
int dy = -step_size; dy <= step_size; dy += step_size) {
545 if (yy < 0 || yy >= size.y) {
548 for (
int dx = -step_size; dx <= step_size; dx += step_size) {
550 if (xx < 0 || xx >= size.x) {
553 JFACoord val = input[size_t(yy) * size.x + xx];
559 if (squared_distance < minimum_squared_distance) {
560 minimum_squared_distance = squared_distance;
595 const int outline_width = int(
data->runtime->line_height * 0.5f *
data->outline_width);
596 if (outline_width < 1 || data->outline_color[3] <= 0.0f ||
599 return data->runtime->text_boundbox;
602 const int2 size =
int2(context->rectx, context->recty);
605 const size_t pixel_count = size_t(size.x) * size.y;
609 reinterpret_cast<uchar *
>(tmp_buf.
data()),
613 out->byte_buffer.colorspace);
617 rcti outline_rect =
data->runtime->text_boundbox;
618 BLI_rcti_pad(&outline_rect, outline_width + 1, outline_width + 1);
630 for (const int y : y_range) {
631 size_t index = size_t(y) * size.x;
632 for (int x = 0; x < size.x; x++, index++) {
633 bool is_opaque = tmp_buf[index].w >= 128;
635 coord.x = is_opaque ? x : JFA_INVALID;
636 coord.y = is_opaque ? y : JFA_INVALID;
637 boundary[index] = coord;
644 Array<JFACoord> initial_flooded_result(pixel_count, invalid_coord);
647 Array<JFACoord> *result_to_flood = &initial_flooded_result;
648 Array<JFACoord> intermediate_result(pixel_count, invalid_coord);
649 Array<JFACoord> *result_after_flooding = &intermediate_result;
653 while (step_size != 0) {
655 *result_to_flood, *result_after_flooding,
size, rect_x_range, rect_y_range, step_size);
656 std::swap(result_to_flood, result_after_flooding);
666 const float text_color_alpha =
data->color[3];
671 uchar *byte_data =
out->byte_data_for_write();
673 for (
const int y : y_range) {
674 size_t index = size_t(
y) *
size.x + rect_x_range.start();
676 for (
int x = rect_x_range.start();
x < rect_x_range.one_after_last();
x++, index++, dst += 4)
697 float text_alpha = tmp_buf[
index].w * (1.0f / 255.0f);
698 float mul_opaque_text = text_alpha >= 1.0f ? 0.0f : 1.0f;
699 float mul_transparent_text = 1.0f - text_alpha;
707 float mfac = 1.0f - col1.
w;
709 float4 col = col1 + mfac * col2;
715 data->runtime->font,
nullptr, byte_data,
size.x,
size.y, 4,
out->byte_buffer.colorspace);
723 ImBuf *ibuf,
const float col[4],
int x1,
int y1,
int x2,
int y2,
float corner_radius)
725 const int width = ibuf->
x;
726 const int height = ibuf->
y;
737 if (x1 == x2 || y1 == y2) {
748 for (
const int y : y_range) {
749 uchar *dst = byte_data + (size_t(width) *
y + x1) * 4;
750 float origin_x = 0.0f, origin_y = 0.0f;
751 for (
int x = x1;
x < x2;
x++) {
753 float fac = 1.0f - pix.w;
755 float4 premul_col = premul_col_base;
756 bool is_corner =
false;
757 if (
x < x1 + corner_radius &&
y < y1 + corner_radius) {
759 origin_x = x1 + corner_radius - 1;
760 origin_y = y1 + corner_radius - 1;
762 else if (
x >= x2 - corner_radius &&
y < y1 + corner_radius) {
764 origin_x = x2 - corner_radius;
765 origin_y = y1 + corner_radius - 1;
769 origin_x = x1 + corner_radius - 1;
770 origin_y = y2 - corner_radius;
772 else if (
x >= x2 - corner_radius &&
y >= y2 - corner_radius) {
774 origin_x = x2 - corner_radius;
775 origin_y = y2 - corner_radius;
782 constexpr float curve_pow = 2.1f;
785 float alpha =
math::clamp(corner_radius - r, 0.0f, 1.0f);
789 float4 dst_fl = fac * premul_col + pix;
800 if (context ==
nullptr) {
860 data->text_len_bytes,
861 [&](
int index,
size_t byte_offset,
int byte_len,
int advance_x) {
864 info.offset = byte_offset;
865 info.byte_length = byte_len;
866 info.advance_x = advance_x;
867 characters.append(info);
879 if (
data->wrap_width == 0.0f) {
880 return std::numeric_limits<int>::max();
882 return data->wrap_width * image_size.x;
888 const int2 image_size,
891 runtime->
lines.clear();
895 float cur_pixel_x = 0.0f;
900 for (
CharInfo &character : characters) {
901 char chr =
data->text_ptr[character.offset];
907 character.position.x = cur_pixel_x;
908 last_space = &character;
910 else if (chr ==
'\n') {
911 character.do_wrap =
true;
913 last_space =
nullptr;
915 else if ((cur_pixel_x + character.advance_x) > wrap_width && last_space !=
nullptr) {
918 last_space =
nullptr;
920 cur_pixel_x += character.advance_x;
924 float2 cur_pixel_pos = {0.0f, 0.0f};
926 for (
CharInfo &character : characters) {
928 character.position = cur_pixel_pos;
931 cur_pixel_pos.x += character.advance_x;
934 const bool line_end =
data->text_ptr[character.offset] ==
'\0' || character.do_wrap;
937 const int num_visible_chars = line.
characters.size() - 1;
938 if (num_visible_chars <= 0) {
951 if (character.do_wrap) {
953 data->abs_space_line :
957 cur_pixel_pos.y -= line_spacing;
966 for (
const LineInfo &line : lines) {
967 width_max = std::max(width_max, line.width);
976 const float line_offset = (max_width - line_width);
979 return {line_offset, 0.0f};
982 return {line_offset / 2.0f, 0.0f};
992 switch (
data->anchor_x) {
997 anchor_offset.x = -width_max / 2.0f;
1000 anchor_offset.x = -width_max;
1003 switch (
data->anchor_y) {
1005 anchor_offset.y = 0;
1008 anchor_offset.y = text_height / 2.0f;
1011 anchor_offset.y = text_height;
1015 return anchor_offset;
1021 rctf glyph_bounds_max;
1024 data->abs_space_line :
1026 const int text_height = (runtime->
lines.size() - 1) * line_spacing +
1032 if (width_max == 0) {
1033 width_max = text_height * 2;
1036 const float2 image_center{
data->loc[0] * image_size.x,
data->loc[1] * image_size.y};
1047 const int2 image_size)
1051 data->abs_space_line :
1053 const int box_height = runtime->
line_height + (runtime->
lines.size() - 1) * line_spacing;
1055 const float2 image_center{
data->loc[0] * image_size.x,
data->loc[1] * image_size.y};
1056 const float2 line_height_offset{0.0f,
1062 const float2 offset =
math::round(image_center + line_height_offset + align_offset +
1073 if (text.
runtime ==
nullptr) {
1074 text.
runtime = MEM_new<TextVarsRuntime>(__func__);
1083 runtime.
font = font;
1130 const int font =
data->runtime->font;
1135 out.image->byte_data_for_write(),
1139 out.image->byte_buffer.colorspace);
1141 BLF_buffer(font,
nullptr,
nullptr, 0, 0, 4,
nullptr);
1151 if (
out.image->byte_data()) {
1152 const int margin =
data->box_margin *
out.image->x;
1153 const int minx =
data->runtime->text_boundbox.xmin - margin;
1154 const int maxx =
data->runtime->text_boundbox.xmax + margin;
1155 const int miny =
data->runtime->text_boundbox.ymin - margin;
1156 const int maxy =
data->runtime->text_boundbox.ymax + margin;
1157 float corner_radius =
data->box_roundness * (maxy - miny) / 2.0f;
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
#define STRIP_FONT_NOT_LOADED
#define PRF_scope_with_name(ui_name, category)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
constexpr int64_t first() const
constexpr int64_t size() const
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
constexpr IndexRange()=default
auto add_or_modify(const Key &key, const CreateValueF &create_value, const ModifyValueF &modify_value) -> decltype(create_value(nullptr))
int64_t remove_if(Predicate &&predicate)
ItemIterator items() const &
void reserve(const int64_t min_capacity)
#define pf(_x, _i)
Prefetch 64.
float distance(float, float) RET
int size(const AssetLibraryReference *library_reference)
T clamp(const T &a, const T &min, const T &max)
T distance(const T &a, const T &b)
T min(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T max(const T &a, const T &b)
void effect_text_font_set(Strip *strip, VFont *font)
static int wrap_width_get(const TextVars *data, const int2 image_size)
std::recursive_mutex & text_runtime_mutex_get()
static int text_effect_line_size_get(const RenderData *context, const TextVars &text)
static int g_fallback_font_id
float4 load_premul_pixel(const uchar *ptr)
static void fill_rect_alpha_under(ImBuf *ibuf, const float col[4], int x1, int y1, int x2, int y2, float corner_radius)
static void calc_boundbox(const TextVars *data, TextVarsRuntime *runtime, const int2 image_size)
static void initialize_shadow_alpha(int width, int height, int2 offset, const rcti &shadow_rect, const uchar *input, Array< uchar > &r_shadow_mask)
static void text_draw(const char *text_ptr, const TextVarsRuntime *runtime, float color[4])
void text_effect_get_handle(EffectHandle &rval)
float get_render_scale_factor(eSpaceSeq_Proxy_RenderSize render_size, short scene_render_scale)
Array< float > make_gaussian_blur_kernel(float rad, int size)
void text_effect_update_runtime(const RenderData *context, TextVars &text, const int2 image_size)
SeqResult prepare_effect_imbufs(const RenderData *context, const SeqResult &ibuf1, const SeqResult &ibuf2, bool uninitialized_pixels)
static Vector< CharInfo > build_character_info(const TextVars *data, int font)
static float2 anchor_offset_get(const TextVars *data, int width_max, int text_height)
static void text_font_unload(TextVars *data, const bool do_id_user)
int text_effect_font_get(TextVars &text)
static SeqFontMap g_font_map
void text_effect_adjust_relative(TextVars &text, const int2 old_size, const int2 new_size)
static void init_text_effect(Strip *strip)
static StripEarlyOut early_out_text(const Strip *strip, float)
bool effects_can_render_text(const Strip *strip)
static void jump_flooding_pass(Span< JFACoord > input, MutableSpan< JFACoord > output, int2 size, IndexRange x_range, IndexRange y_range, int step_size)
static void apply_word_wrapping(const TextVars *data, TextVarsRuntime *runtime, const int2 image_size, Vector< CharInfo > &characters)
static int text_effect_font_init(const RenderData *context, TextVars &text, FontFlags font_flags)
static std::recursive_mutex text_runtime_mutex
static void composite_shadow(int width, const rcti &shadow_rect, const float4 &shadow_color, const Array< uchar > &shadow_mask, uchar *output)
static void apply_text_alignment(const TextVars *data, TextVarsRuntime *runtime, const int2 image_size)
static SeqResult do_text_effect(const RenderData *context, SeqRenderState *, Strip *strip, float, float, const SeqResult &, const SeqResult &)
static void text_gaussian_blur_y(const Span< float > gaussian, int half_size, int start_line, int width, int height, const uchar *rect, uchar *dst, const rcti &shadow_rect)
static int text_box_width_get(const Vector< LineInfo > &lines)
static void draw_text_shadow(const RenderData *context, const TextVars *data, int line_height, const rcti &rect, ImBuf *out)
static float2 horizontal_alignment_offset_get(const TextVars *data, float line_width, int max_width)
static rcti draw_text_outline(const RenderData *context, const TextVars *data, ImBuf *out)
static void text_gaussian_blur_x(const Span< float > gaussian, int half_size, int start_line, int width, int height, const uchar *rect, uchar *dst, const rcti &shadow_rect)
static void free_text_effect(Strip *strip, const bool do_id_user)
static int strip_load_font_mem(const std::string &name, const uchar *data, int data_size)
static void text_font_load(TextVars *data, const bool do_id_user)
static void copy_text_effect(Strip *dst, const Strip *src, const int flag)
static void strip_unload_font(int fontid)
void store_premul_pixel(const float4 &pix, uchar *dst)
static int strip_load_font_file(const std::string &path)
constexpr uint16_t JFA_INVALID
static void clamp_rect(int width, int height, rcti &r_rect)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
VecBase< uint8_t, 4 > uchar4
bool BLF_is_loaded_id(int fontid) ATTR_WARN_UNUSED_RESULT
char * STRNCPY(char(&dst)[N], const char *src)
void BLF_position(int fontid, float x, float y, float z)
float BLF_width(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void BLF_buffer(int fontid, float *fbuf, unsigned char *cbuf, int w, int h, int channel_count, const ColorSpace *colorspace)
int BLF_load_mono_default(bool unique)
MINLINE int power_of_2_max_i(int n)
MINLINE void copy_v4_fl(float r[4], float f)
MINLINE void straight_to_premul_v4_v4(float premul[4], const float straight[4])
void BLF_addref_id(int fontid)
bool BLF_bounds_max(int fontid, rctf *r_bounds) ATTR_NONNULL(2)
void BLF_disable(int fontid, FontFlags flag)
@ LIB_ID_CREATE_NO_USER_REFCOUNT
VecBase< float, 4 > float4
void BLF_size(int fontid, float size)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
BMesh const char void * data
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
void BLI_rcti_translate(struct rcti *rect, int x, int y)
int BLF_load_mem_unique(const char *name, const unsigned char *mem, int mem_size) ATTR_NONNULL(1
void IMB_colormanagement_assign_byte_colorspace(ImBuf *ibuf, const char *name)
@ SEQ_TEXT_USE_ABSOLUTE_LINE_SPACING
const char * IMB_colormanagement_srgb_colorspace_name_get()
void BLF_buffer_col(int fontid, const float srgb_color[4]) ATTR_NONNULL(2)
@ SEQ_TEXT_ANCHOR_X_CENTER
@ SEQ_TEXT_ANCHOR_X_RIGHT
int BLF_height_max(int fontid) ATTR_WARN_UNUSED_RESULT
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
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)
char * BLI_strdup_null(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_MALLOC
void BLF_info_foreach_glyph(int fontid, const char *str, size_t str_len, FunctionRef< void(int index, size_t byte_offset, int byte_len, int advance_x)> callback)
void BLI_rcti_pad(struct rcti *rect, int pad_x, int pad_y)
bool BLF_is_builtin(int fontid)
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
@ SEQ_TEXT_ALIGN_X_CENTER
@ SEQ_TEXT_ANCHOR_Y_BOTTOM
@ SEQ_TEXT_ANCHOR_Y_CENTER
MINLINE int clamp_i(int value, int min, int max)
void BLF_enable(int fontid, FontFlags flag)
bool BLF_unload_id(int fontid)
SphericalHarmonicL1< float4 > mul(SphericalHarmonicL1< float4 > a, float b)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
uint8_t * byte_data_for_write()
seq::TextVarsRuntime * runtime
struct PackedFile * packedfile
void(*) copy(Strip *dst, const Strip *src, int flag)
void(*) free(Strip *strip, bool do_id_user)
void(*) init(Strip *strip)
StripEarlyOut(*) early_out(const Strip *strip, float fac)
SeqResult(*) execute(const RenderData *context, SeqRenderState *state, Strip *strip, float timeline_frame, float fac, const SeqResult &input1, const SeqResult &input2)
Vector< CharInfo > characters
Map< std::string, int > path_to_file_font_id
std::recursive_mutex mutex
Map< std::string, int > name_to_mem_font_id