16 #include "../Timeline.h"
18 #include <QGuiApplication>
25 #include <QPainterPath>
30 Caption::Caption() : color(
"#ffffff"), stroke(
"#a9a9a9"), background(
"#ff000000"), background_alpha(0.0), left(0.1), top(0.75), right(0.1), bottom(0.0),
31 stroke_width(0.5), font_size(30.0), font_alpha(1.0), is_dirty(true), font_name(
"sans"), font(NULL), metrics(NULL),
32 fade_in(0.0), fade_out(0.0), background_corner(10.0), background_padding(20.0), line_spacing(1.0)
35 init_effect_details();
40 color(
"#ffffff"), stroke(
"#a9a9a9"), background(
"#ff000000"), background_alpha(0.0), left(0.1), top(0.75), right(0.1), bottom(0.0),
41 stroke_width(0.5), font_size(30.0), font_alpha(1.0), is_dirty(true), font_name(
"sans"), font(NULL), metrics(NULL),
42 fade_in(0.0), fade_out(0.0), background_corner(10.0), background_padding(20.0), line_spacing(1.0),
43 caption_text(captions)
46 init_effect_details();
50 void Caption::init_effect_details()
63 if (caption_text.length() == 0) {
64 caption_text =
"00:00:00:000 --> 00:10:00:000\nEdit this caption with our caption editor";
75 caption_text = new_caption_text;
80 void Caption::process_regex() {
85 matchedCaptions.clear();
87 QString caption_prepared = QString(caption_text.c_str());
88 if (caption_prepared.endsWith(
"\n\n") ==
false) {
90 caption_prepared.append(
"\n\n");
94 QRegularExpression allPathsRegex(QStringLiteral(
"(\\d{2})?:*(\\d{2}):(\\d{2}).(\\d{2,3})\\s*-->\\s*(\\d{2})?:*(\\d{2}):(\\d{2}).(\\d{2,3})([\\s\\S]*?)(.*?)(?=\\d{2}:\\d{2,3}|\\Z)"), QRegularExpression::MultilineOption);
95 QRegularExpressionMatchIterator i = allPathsRegex.globalMatch(caption_prepared);
97 QRegularExpressionMatch match = i.next();
98 if (match.hasMatch()) {
100 matchedCaptions.push_back(match);
108 std::shared_ptr<openshot::Frame>
Caption::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
117 QSize image_size(1, 1);
119 if (
clip &&
clip->ParentTimeline() != NULL) {
121 }
else if (this->ParentTimeline() != NULL) {
129 }
else if (
clip != NULL &&
clip->Reader() != NULL) {
130 fps =
clip->Reader()->info.fps;
131 image_size = QSize(
clip->Reader()->info.width,
clip->Reader()->info.height);
134 if (!frame->has_image_data) {
136 frame->AddColor(image_size.width(), image_size.height(),
"#000000");
140 std::shared_ptr<QImage> frame_image = frame->GetImage();
144 double timeline_scale_factor = frame_image->width() / 600.0;
147 QPainter painter(frame_image.get());
148 painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing,
true);
151 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
154 double font_size_value =
font_size.
GetValue(frame_number) * timeline_scale_factor;
155 QFont font(QString(
font_name.c_str()),
int(font_size_value));
156 font.setPixelSize(std::max(font_size_value, 1.0));
157 QFontMetricsF metrics = QFontMetricsF(font);
170 double metrics_line_spacing = metrics.lineSpacing();
173 double left_margin_x = frame_image->width() * left_value;
174 double top_margin_y = frame_image->height() * top_value;
175 double starting_y = top_margin_y + metrics_line_spacing;
176 double current_y = starting_y;
177 double bottom_y = starting_y;
178 double top_y = starting_y;
179 double max_text_width = 0.0;
180 double right_margin_x = frame_image->width() - (frame_image->width() * right_value);
181 double caption_area_width = right_margin_x - left_margin_x;
182 double bottom_margin_y = frame_image->height() - (frame_image->height() * bottom_value);
183 double caption_area_height = std::max(bottom_margin_y - starting_y, 0.0);
184 QRectF caption_area = QRectF(left_margin_x, starting_y, caption_area_width, caption_area_height);
185 QRectF caption_clip_area = QRectF(left_margin_x, top_margin_y, caption_area_width,
186 std::max(bottom_margin_y - top_margin_y, 0.0));
189 std::vector<QPainterPath> text_paths;
190 double fade_in_percentage = 0.0;
191 double fade_out_percentage = 0.0;
192 double line_height = metrics_line_spacing * line_spacing_value;
195 auto fracToSeconds = [&](
const QString &f){
196 QString ms = f.leftJustified(3, QChar(
'0'));
197 return ms.toInt() / 1000.0;
201 for (
auto match = matchedCaptions.begin(); match != matchedCaptions.end(); match++) {
204 double startSeconds =
205 match->captured(1).toFloat() * 3600.0 +
206 match->captured(2).toFloat() * 60.0 +
207 match->captured(3).toFloat() +
208 fracToSeconds(match->captured(4));
211 match->captured(5).toFloat() * 3600.0 +
212 match->captured(6).toFloat() * 60.0 +
213 match->captured(7).toFloat() +
214 fracToSeconds(match->captured(8));
216 auto start_frame = int64_t(startSeconds * fps.
ToFloat()) + 1;
217 auto end_frame = int64_t(endSeconds * fps.
ToFloat());
220 QStringList lines = match->captured(9).split(
"\n");
221 for(
int index = 0; index < lines.length(); index++) {
223 QString line = lines[index];
225 if (!line.startsWith(QStringLiteral(
"NOTE")) &&
226 !line.isEmpty() && frame_number >= start_frame && frame_number <= end_frame && line.length() > 1) {
229 fade_in_percentage = fade_in_value > 0.0
230 ? ((float) frame_number - (
float) start_frame) / fade_in_value
232 fade_out_percentage = fade_out_value > 0.0
233 ? 1.0 - (((float) frame_number - ((
float) end_frame - fade_out_value)) / fade_out_value)
237 QStringList words = line.split(
" ");
240 bool use_spaces =
true;
241 if (line.length() > 20 && words.length() == 1) {
242 words = line.split(
"");
245 int words_remaining = words.length();
246 while (words_remaining > 0) {
247 bool words_displayed =
false;
248 for(
int word_index = words.length(); word_index > 0; word_index--) {
250 QString fitting_line = words.mid(0, word_index).join(
" ");
253 QRectF textRect = metrics.boundingRect(caption_area, Qt::TextSingleLine, fitting_line);
254 if (textRect.width() <= caption_area.width()) {
256 QPoint p(left_margin_x, current_y);
260 QString fitting_line;
262 fitting_line = words.mid(0, word_index).join(
" ");
264 fitting_line = words.mid(0, word_index).join(
"");
266 path1.addText(p, font, fitting_line);
267 text_paths.push_back(path1);
270 words = words.mid(word_index, words.length());
271 words_remaining = words.length();
272 words_displayed =
true;
275 current_y += line_height;
278 if (path1.boundingRect().width() > max_text_width) {
279 max_text_width = path1.boundingRect().width();
282 if (path1.boundingRect().top() < top_y) {
283 top_y = path1.boundingRect().top();
286 if (path1.boundingRect().bottom() > bottom_y) {
287 bottom_y = path1.boundingRect().bottom();
293 if (!words_displayed) {
304 QRectF caption_area_with_padding = QRectF(left_margin_x - (padding_value / 2.0),
305 top_y - (padding_value / 2.0),
306 max_text_width + padding_value,
307 (bottom_y - top_y) + padding_value);
310 double alignment_offset = std::max((caption_area_width - max_text_width) / 2.0, 0.0);
313 QBrush background_brush;
316 caption_area_with_padding.translate(alignment_offset, 0.0);
317 if (fade_in_percentage < 1.0) {
320 }
else if (fade_out_percentage >= 0.0 && fade_out_percentage <= 1.0) {
326 background_brush.setColor(background_qcolor);
327 background_brush.setStyle(Qt::SolidPattern);
328 painter.setBrush(background_brush);
329 painter.setPen(Qt::NoPen);
331 painter.setClipRect(caption_clip_area);
332 painter.drawRoundedRect(caption_area_with_padding, background_corner_value, background_corner_value);
336 QColor font_qcolor = QColor(QString(
color.
GetColorHex(frame_number).c_str()));
338 font_brush.setStyle(Qt::SolidPattern);
342 QColor stroke_qcolor;
345 pen.setColor(stroke_qcolor);
346 pen.setWidthF(std::max(stroke_width_value, 0.0));
350 for(QPainterPath
path : text_paths) {
352 path.translate(alignment_offset, 0.0);
353 if (fade_in_percentage < 1.0) {
357 }
else if (fade_out_percentage >= 0.0 && fade_out_percentage <= 1.0) {
362 pen.setColor(stroke_qcolor);
363 font_brush.setColor(font_qcolor);
366 if (stroke_width_value <= 0.0) {
367 painter.setPen(Qt::NoPen);
372 painter.setBrush(font_brush);
373 painter.drawPath(
path);
413 root[
"caption_text"] = caption_text;
430 catch (
const std::exception& e)
433 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
444 if (!root[
"color"].isNull())
446 if (!root[
"stroke"].isNull())
448 if (!root[
"background"].isNull())
450 if (!root[
"background_alpha"].isNull())
452 if (!root[
"background_corner"].isNull())
454 if (!root[
"background_padding"].isNull())
456 if (!root[
"stroke_width"].isNull())
458 if (!root[
"font_size"].isNull())
460 if (!root[
"font_alpha"].isNull())
462 if (!root[
"fade_in"].isNull())
464 if (!root[
"fade_out"].isNull())
466 if (!root[
"line_spacing"].isNull())
468 if (!root[
"left"].isNull())
470 if (!root[
"top"].isNull())
472 if (!root[
"right"].isNull())
474 if (!root[
"bottom"].isNull())
476 if (!root[
"caption_text"].isNull())
477 caption_text = root[
"caption_text"].asString();
478 if (!root[
"caption_font"].isNull())
479 font_name = root[
"caption_font"].asString();
517 root[
"caption_text"] =
add_property_json(
"Captions", 0.0,
"caption", caption_text, NULL, -1, -1,
false, requested_frame);
521 return root.toStyledString();