Sequencer: word-wrap support for sequencer text

Also add vertical alignment option,
default align to bottom for subtitles.
This commit is contained in:
Campbell Barton 2015-09-18 20:29:35 +10:00
parent d435b0d24d
commit 0aa0a1a966
4 changed files with 78 additions and 22 deletions

View File

@ -638,8 +638,10 @@ class SEQUENCER_PT_effect(SequencerButtonsPanel, Panel):
col.prop(strip, "text")
col.prop(strip, "font_size")
col.prop(strip, "use_shadow")
col.prop(strip, "align")
col.prop(strip, "align_x")
col.prop(strip, "align_y")
col.prop(strip, "location")
col.prop(strip, "wrap_width")
layout.operator("sequencer.export_subtitles")
col = layout.column(align=True)

View File

@ -38,6 +38,7 @@
#include "BLI_math.h" /* windows needs for M_PI */
#include "BLI_utildefines.h"
#include "BLI_rect.h"
#include "BLI_string.h"
#include "DNA_scene_types.h"
@ -2893,7 +2894,8 @@ static void init_text_effect(Sequence *seq)
BLI_strncpy(data->text, "Text", sizeof(data->text));
data->loc[0] = 0.5f;
data->align = SEQ_TEXT_ALIGN_CENTER;
data->align = SEQ_TEXT_ALIGN_X_CENTER;
data->align_y = SEQ_TEXT_ALIGN_Y_BOTTOM;
}
static int num_inputs_text(void)
@ -2920,6 +2922,7 @@ static ImBuf *do_text_effect(const SeqRenderData *context, Sequence *seq, float
struct ColorManagedDisplay *display;
const char *display_device;
const int mono = blf_mono_font_render; // XXX
int line_height;
int y_ofs, x, y;
float proxy_size_comp;
@ -2940,24 +2943,46 @@ static ImBuf *do_text_effect(const SeqRenderData *context, Sequence *seq, float
/* set before return */
BLF_size(mono, proxy_size_comp * data->text_size, 72);
BLF_enable(mono, BLF_WORD_WRAP);
/* use max width to enable newlines only */
BLF_wordwrap(mono, (data->wrap_width != 0.0f) ? data->wrap_width * width : -1);
BLF_buffer(mono, out->rect_float, (unsigned char *)out->rect, width, height, out->channels, display);
line_height = BLF_height_max(mono);
y_ofs = -BLF_descender(mono);
x = (data->loc[0] * width);
y = (data->loc[1] * height) + y_ofs;
if (data->align == SEQ_TEXT_ALIGN_LEFT) {
if ((data->align == SEQ_TEXT_ALIGN_X_LEFT) &&
(data->align_y == SEQ_TEXT_ALIGN_Y_TOP))
{
/* pass */
}
else {
const int w = BLF_width(mono, data->text, sizeof(data->text));
/* vars for calculating wordwrap */
struct {
struct ResultBLF info;
rctf rect;
} wrap;
if (data->align == SEQ_TEXT_ALIGN_RIGHT) {
x -= w;
BLF_boundbox_ex(mono, data->text, sizeof(data->text), &wrap.rect, &wrap.info);
if (data->align == SEQ_TEXT_ALIGN_X_RIGHT) {
x -= BLI_rctf_size_x(&wrap.rect);
}
else { /* SEQ_TEXT_ALIGN_CENTER */
x -= w / 2;
else if (data->align == SEQ_TEXT_ALIGN_X_CENTER) {
x -= BLI_rctf_size_x(&wrap.rect) / 2;
}
if (data->align_y == SEQ_TEXT_ALIGN_Y_BOTTOM) {
y += (wrap.info.lines - 1) * line_height;
}
else if (data->align_y == SEQ_TEXT_ALIGN_Y_CENTER) {
y += (((wrap.info.lines - 1) / 2) * line_height) - (line_height / 2);
}
}
@ -2965,7 +2990,7 @@ static ImBuf *do_text_effect(const SeqRenderData *context, Sequence *seq, float
if (data->flag & SEQ_TEXT_SHADOW) {
int fontx, fonty;
fontx = BLF_width_max(mono);
fonty = BLF_height_max(mono);
fonty = line_height;
BLF_position(mono, x + max_ii(fontx / 25, 1), y + max_ii(fonty / 25, 1), 0.0f);
BLF_buffer_col(mono, 0.0f, 0.0f, 0.0f, 1.0f);
BLF_draw_buffer(mono, data->text, BLF_DRAW_STR_DUMMY_MAX);
@ -2976,6 +3001,8 @@ static ImBuf *do_text_effect(const SeqRenderData *context, Sequence *seq, float
BLF_buffer(mono, NULL, NULL, 0, 0, 0, NULL);
BLF_disable(mono, BLF_WORD_WRAP);
return out;
}

View File

@ -275,9 +275,10 @@ typedef struct TextVars {
char text[512];
int text_size;
float loc[2];
short flag;
char align;
char pad;
float wrap_width;
char flag;
char align, align_y;
char pad[5];
} TextVars;
/* TextVars.flag */
@ -287,9 +288,16 @@ enum {
/* TextVars.align */
enum {
SEQ_TEXT_ALIGN_LEFT = 0,
SEQ_TEXT_ALIGN_CENTER = 1,
SEQ_TEXT_ALIGN_RIGHT = 2,
SEQ_TEXT_ALIGN_X_LEFT = 0,
SEQ_TEXT_ALIGN_X_CENTER = 1,
SEQ_TEXT_ALIGN_X_RIGHT = 2,
};
/* TextVars.align_y */
enum {
SEQ_TEXT_ALIGN_Y_TOP = 0,
SEQ_TEXT_ALIGN_Y_CENTER = 1,
SEQ_TEXT_ALIGN_Y_BOTTOM = 2,
};
/* ***************** Sequence modifiers ****************** */

View File

@ -2307,10 +2307,16 @@ static void rna_def_gaussian_blur(StructRNA *srna)
static void rna_def_text(StructRNA *srna)
{
static EnumPropertyItem text_align_items[] = {
{SEQ_TEXT_ALIGN_LEFT, "LEFT", 0, "Left", ""},
{SEQ_TEXT_ALIGN_CENTER, "CENTER", 0, "Center", ""},
{SEQ_TEXT_ALIGN_RIGHT, "RIGHT", 0, "Right", ""},
static EnumPropertyItem text_align_x_items[] = {
{SEQ_TEXT_ALIGN_X_LEFT, "LEFT", 0, "Left", ""},
{SEQ_TEXT_ALIGN_X_CENTER, "CENTER", 0, "Center", ""},
{SEQ_TEXT_ALIGN_X_RIGHT, "RIGHT", 0, "Right", ""},
{0, NULL, 0, NULL, NULL}
};
static EnumPropertyItem text_align_y_items[] = {
{SEQ_TEXT_ALIGN_Y_TOP, "TOP", 0, "Top", ""},
{SEQ_TEXT_ALIGN_Y_CENTER, "CENTER", 0, "Center", ""},
{SEQ_TEXT_ALIGN_Y_BOTTOM, "BOTTOM", 0, "Bottom", ""},
{0, NULL, 0, NULL, NULL}
};
@ -2331,10 +2337,23 @@ static void rna_def_text(StructRNA *srna)
RNA_def_property_ui_range(prop, 0.0, 1.0, 1, -1);
RNA_def_property_update(prop, NC_SCENE | ND_SEQUENCER, "rna_Sequence_update");
prop = RNA_def_property(srna, "align", PROP_ENUM, PROP_NONE);
prop = RNA_def_property(srna, "wrap_width", PROP_FLOAT, PROP_NONE);
RNA_def_property_float_sdna(prop, NULL, "wrap_width");
RNA_def_property_ui_text(prop, "Wrap Width", "Word wrap width as factor, zero disables");
RNA_def_property_range(prop, 0, FLT_MAX);
RNA_def_property_ui_range(prop, 0.0, 1.0, 1, -1);
RNA_def_property_update(prop, NC_SCENE | ND_SEQUENCER, "rna_Sequence_update");
prop = RNA_def_property(srna, "align_x", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "align");
RNA_def_property_enum_items(prop, text_align_items);
RNA_def_property_ui_text(prop, "Align", "");
RNA_def_property_enum_items(prop, text_align_x_items);
RNA_def_property_ui_text(prop, "Align X", "");
RNA_def_property_update(prop, NC_SCENE | ND_SEQUENCER, "rna_Sequence_update");
prop = RNA_def_property(srna, "align_y", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "align_y");
RNA_def_property_enum_items(prop, text_align_y_items);
RNA_def_property_ui_text(prop, "Align Y", "");
RNA_def_property_update(prop, NC_SCENE | ND_SEQUENCER, "rna_Sequence_update");
prop = RNA_def_property(srna, "text", PROP_STRING, PROP_NONE);