File: texteffect.cpp

package info (click to toggle)
olive-editor 20181223-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,844 kB
  • sloc: cpp: 20,147; xml: 315; ansic: 16; makefile: 11
file content (201 lines) | stat: -rw-r--r-- 6,555 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "texteffect.h"

#include <QGridLayout>
#include <QLabel>
#include <QOpenGLTexture>
#include <QTextEdit>
#include <QPainter>
#include <QPushButton>
#include <QColorDialog>
#include <QFontDatabase>
#include <QComboBox>
#include <QWidget>
#include <QtMath>

#include "ui/labelslider.h"
#include "ui/collapsiblewidget.h"
#include "project/clip.h"
#include "project/sequence.h"
#include "ui/comboboxex.h"
#include "ui/colorbutton.h"
#include "ui/fontcombobox.h"

TextEffect::TextEffect(Clip *c, const EffectMeta* em) :
	Effect(c, em)
{
	enable_superimpose = true;
    enable_shader = true;

    text_val = add_row("Text:")->add_field(EFFECT_FIELD_STRING, "text", 2);

    set_font_combobox = add_row("Font:")->add_field(EFFECT_FIELD_FONT, "font", 2);

    size_val = add_row("Size:")->add_field(EFFECT_FIELD_DOUBLE, "size", 2);
    size_val->set_double_minimum_value(0);

    set_color_button = add_row("Color:")->add_field(EFFECT_FIELD_COLOR, "color", 2);

	EffectRow* alignment_row = add_row("Alignment:");
    halign_field = alignment_row->add_field(EFFECT_FIELD_COMBO, "halign");
	halign_field->add_combo_item("Left", Qt::AlignLeft);
	halign_field->add_combo_item("Center", Qt::AlignHCenter);
	halign_field->add_combo_item("Right", Qt::AlignRight);
    halign_field->add_combo_item("Justify", Qt::AlignJustify);

    valign_field = alignment_row->add_field(EFFECT_FIELD_COMBO, "valign");
	valign_field->add_combo_item("Top", Qt::AlignTop);
	valign_field->add_combo_item("Center", Qt::AlignVCenter);
    valign_field->add_combo_item("Bottom", Qt::AlignBottom);

    word_wrap_field = add_row("Word Wrap:")->add_field(EFFECT_FIELD_BOOL, "wordwrap", 2);

    outline_bool = add_row("Outline:")->add_field(EFFECT_FIELD_BOOL, "outline", 2);
    outline_color = add_row("Outline Color:")->add_field(EFFECT_FIELD_COLOR, "outlinecolor", 2);
    outline_width = add_row("Outline Width:")->add_field(EFFECT_FIELD_DOUBLE, "outlinewidth", 2);
	outline_width->set_double_minimum_value(0);

    shadow_bool = add_row("Shadow:")->add_field(EFFECT_FIELD_BOOL, "shadow", 2);
    shadow_color = add_row("Shadow Color:")->add_field(EFFECT_FIELD_COLOR, "shadowcolor", 2);
    shadow_distance = add_row("Shadow Distance:")->add_field(EFFECT_FIELD_DOUBLE, "shadowdistance", 2);
	shadow_distance->set_double_minimum_value(0);
    shadow_softness = add_row("Shadow Softness:")->add_field(EFFECT_FIELD_DOUBLE, "shadowsoftness", 2);
	shadow_softness->set_double_minimum_value(0);
    shadow_opacity = add_row("Shadow Opacity:")->add_field(EFFECT_FIELD_DOUBLE, "shadowopacity", 2);
	shadow_opacity->set_double_minimum_value(0);
	shadow_opacity->set_double_maximum_value(100);

	size_val->set_double_default_value(48);
	text_val->set_string_value("Sample Text");
	halign_field->set_combo_index(1);
	valign_field->set_combo_index(1);
	word_wrap_field->set_bool_value(true);
	outline_color->set_color_value(Qt::black);
	shadow_color->set_color_value(Qt::black);
	shadow_opacity->set_double_default_value(100);
	shadow_softness->set_double_default_value(5);
	shadow_distance->set_double_default_value(5);
	shadow_opacity->set_double_default_value(80);
	outline_width->set_double_default_value(20);

	outline_enable(false);
    shadow_enable(false);

	connect(shadow_bool, SIGNAL(toggled(bool)), this, SLOT(shadow_enable(bool)));
	connect(outline_bool, SIGNAL(toggled(bool)), this, SLOT(outline_enable(bool)));

    vertPath = "common.vert";
    fragPath = "dropshadow.frag";
}

void TextEffect::redraw(double timecode) {
	img.fill(Qt::transparent);

	QPainter p(&img);
	p.setRenderHint(QPainter::Antialiasing);
	int width = img.width();
	int height = img.height();

	// set font
	font.setStyleHint(QFont::Helvetica, QFont::PreferAntialias);
	font.setFamily(set_font_combobox->get_font_name(timecode));
	font.setPointSize(size_val->get_double_value(timecode));
	p.setFont(font);
	QFontMetrics fm(font);

	QStringList lines = text_val->get_string_value(timecode).split('\n');

	// word wrap function
	if (word_wrap_field->get_bool_value(timecode)) {
		for (int i=0;i<lines.size();i++) {
			QString s(lines.at(i));
			if (fm.width(s) > width) {
				int last_space_index = 0;
				for (int j=0;j<s.length();j++) {
					if (s.at(j) == ' ') {
						if (fm.width(s.left(j)) > width) {
							break;
						} else {
							last_space_index = j;
						}
					}
				}
				if (last_space_index > 0) {
					lines.insert(i+1, s.mid(last_space_index + 1));
					lines[i] = s.left(last_space_index);
				}
			}
		}
	}

	QPainterPath path;

	int text_height = fm.height()*lines.size();

	for (int i=0;i<lines.size();i++) {
		int text_x, text_y;

		switch (halign_field->get_combo_data(timecode).toInt()) {
		case Qt::AlignLeft: text_x = 0; break;
		case Qt::AlignHCenter: text_x = (width/2) - (fm.width(lines.at(i))/2); break;
		case Qt::AlignRight: text_x = width - fm.width(lines.at(i)); break;
		case Qt::AlignJustify:
			// add spaces until the string is too big
			text_x = 0;
			while (fm.width(lines.at(i)) < width) {
				bool space = false;
				QString spaced(lines.at(i));
				for (int i=0;i<spaced.length();i++) {
					if (spaced.at(i) == ' ') {
						// insert a space
						spaced.insert(i, ' ');
						space = true;

						// scan to next non-space
						while (i < spaced.length() && spaced.at(i) == ' ') i++;
					}
				}
				if (fm.width(spaced) > width || !space) {
					break;
				} else {
					lines[i] = spaced;
				}
			}
			break;
		}

		switch (valign_field->get_combo_data(timecode).toInt()) {
		case Qt::AlignTop: text_y = (fm.height()*i)+fm.ascent(); break;
		case Qt::AlignVCenter: text_y = ((height/2) - (text_height/2) - fm.descent()) + (fm.height()*(i+1)); break;
		case Qt::AlignBottom: text_y = (height - text_height - fm.descent()) + (fm.height()*(i+1)); break;
		}

		path.addText(text_x, text_y, font, lines.at(i));
    }

	// draw outline
	int outline_width_val = outline_width->get_double_value(timecode);
	if (outline_bool->get_bool_value(timecode) && outline_width_val > 0) {
		QPen outline(outline_color->get_color_value(timecode));
		outline.setWidth(outline_width_val);
		p.setPen(outline);
		p.setBrush(Qt::NoBrush);
		p.drawPath(path);
	}

	// draw "master" text
	p.setPen(Qt::NoPen);
	p.setBrush(set_color_button->get_color_value(timecode));
    p.drawPath(path);
}

void TextEffect::shadow_enable(bool e) {
	shadow_color->set_enabled(e);
	shadow_distance->set_enabled(e);
	shadow_softness->set_enabled(e);
	shadow_opacity->set_enabled(e);
}

void TextEffect::outline_enable(bool e) {
	outline_color->set_enabled(e);
	outline_width->set_enabled(e);
}