1 /**
2 * @file
3 * @brief Source file for Caption effect class
4 * @author Jonathan Thomas <jonathan@openshot.org>
5 *
6 * @ref License
7 */
8
9 /* LICENSE
10 *
11 * Copyright (c) 2008-2019 OpenShot Studios, LLC
12 * <http://www.openshotstudios.com/>. This file is part of
13 * OpenShot Library (libopenshot), an open-source project dedicated to
14 * delivering high quality video editing and animation solutions to the
15 * world. For more information visit <http://www.openshot.org/>.
16 *
17 * OpenShot Library (libopenshot) is free software: you can redistribute it
18 * and/or modify it under the terms of the GNU Lesser General Public License
19 * as published by the Free Software Foundation, either version 3 of the
20 * License, or (at your option) any later version.
21 *
22 * OpenShot Library (libopenshot) is distributed in the hope that it will be
23 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public License
28 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29 */
30
31 #include "Caption.h"
32 #include "Exceptions.h"
33 #include "../Clip.h"
34 #include "../Timeline.h"
35
36 #include <QString>
37 #include <QPoint>
38 #include <QRect>
39 #include <QPen>
40 #include <QBrush>
41
42 using namespace openshot;
43
44 /// Blank constructor, useful when using Json to load the effect properties
Caption()45 Caption::Caption() : color("#ffffff"), stroke("#a9a9a9"), background("#ff000000"), background_alpha(0.0), left(0.25), top(0.7), right(0.1),
46 stroke_width(0.5), font_size(30.0), font_alpha(1.0), is_dirty(true), font_name("sans"), font(NULL), metrics(NULL),
47 fade_in(0.35), fade_out(0.35), background_corner(10.0), background_padding(20.0)
48 {
49 // Init effect properties
50 init_effect_details();
51 }
52
53 // Default constructor
Caption(std::string captions)54 Caption::Caption(std::string captions) :
55 color("#ffffff"), caption_text(captions), stroke("#a9a9a9"), background("#ff000000"), background_alpha(0.0),
56 left(0.25), top(0.7), right(0.1), stroke_width(0.5), font_size(30.0), font_alpha(1.0), is_dirty(true), font_name("sans"),
57 font(NULL), metrics(NULL), fade_in(0.35), fade_out(0.35), background_corner(10.0), background_padding(20.0)
58 {
59 // Init effect properties
60 init_effect_details();
61 }
62
63 // Init effect settings
init_effect_details()64 void Caption::init_effect_details()
65 {
66 /// Initialize the values of the EffectInfo struct.
67 InitEffectInfo();
68
69 /// Set the effect info
70 info.class_name = "Caption";
71 info.name = "Caption";
72 info.description = "Add text captions on top of your video.";
73 info.has_audio = false;
74 info.has_video = true;
75
76 // Init placeholder caption (for demo)
77 if (caption_text.length() == 0) {
78 caption_text = "00:00:00:000 --> 00:10:00:000\nEdit this caption with our caption editor";
79 }
80 }
81
82 // Set the caption string to use (see VTT format)
CaptionText()83 std::string Caption::CaptionText() {
84 return caption_text;
85 }
86
87 // Get the caption string
CaptionText(std::string new_caption_text)88 void Caption::CaptionText(std::string new_caption_text) {
89 caption_text = new_caption_text;
90 is_dirty = true;
91 }
92
93 // Process regex string only when dirty
process_regex()94 void Caption::process_regex() {
95 if (is_dirty) {
96 is_dirty = false;
97
98 // Clear existing matches
99 matchedCaptions.clear();
100
101 QString caption_prepared = QString(caption_text.c_str());
102 if (caption_prepared.endsWith("\n\n") == false) {
103 // We need a couple line ends at the end of the caption string (for our regex to work correctly)
104 caption_prepared.append("\n\n");
105 }
106
107 // Parse regex and find all matches
108 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]*?)\\n(.*?)(?=\\n\\d{2,3}|\\Z)"), QRegularExpression::MultilineOption);
109 QRegularExpressionMatchIterator i = allPathsRegex.globalMatch(caption_prepared);
110 while (i.hasNext()) {
111 QRegularExpressionMatch match = i.next();
112 if (match.hasMatch()) {
113 // Push all match objects into a vector (so we can reverse them later)
114 matchedCaptions.push_back(match);
115 }
116 }
117 }
118 }
119
120 // This method is required for all derived classes of EffectBase, and returns a
121 // modified openshot::Frame object
GetFrame(std::shared_ptr<openshot::Frame> frame,int64_t frame_number)122 std::shared_ptr<openshot::Frame> Caption::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
123 {
124 // Process regex (if needed)
125 process_regex();
126
127 // Get the Clip and Timeline pointers (if available)
128 Clip* clip = (Clip*) ParentClip();
129 Timeline* timeline = NULL;
130 Fraction fps;
131 double scale_factor = 1.0; // amount of scaling needed for text (based on preview window size)
132 if (clip->ParentTimeline() != NULL) {
133 timeline = (Timeline*) clip->ParentTimeline();
134 } else if (this->ParentTimeline() != NULL) {
135 timeline = (Timeline*) this->ParentTimeline();
136 }
137
138 // Get the FPS from the parent object (Timeline or Clip's Reader)
139 if (timeline != NULL) {
140 fps.num = timeline->info.fps.num;
141 fps.den = timeline->info.fps.den;
142 // preview window is sometimes smaller/larger than the timeline size
143 scale_factor = (double) timeline->preview_width / (double) timeline->info.width;
144 } else if (clip != NULL && clip->Reader() != NULL) {
145 fps.num = clip->Reader()->info.fps.num;
146 fps.den = clip->Reader()->info.fps.den;
147 scale_factor = 1.0;
148 }
149
150 // Get the frame's image
151 std::shared_ptr<QImage> frame_image = frame->GetImage();
152
153 // Load timeline's new frame image into a QPainter
154 QPainter painter(frame_image.get());
155 painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing, true);
156
157 // Composite a new layer onto the image
158 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
159
160 // Font options and metrics for caption text
161 double font_size_value = font_size.GetValue(frame_number) * scale_factor;
162 QFont font(QString(font_name.c_str()), int(font_size_value));
163 font.setPointSizeF(std::max(font_size_value, 1.0));
164 QFontMetricsF metrics = QFontMetricsF(font);
165
166 // Get current keyframe values
167 double left_value = left.GetValue(frame_number);
168 double top_value = top.GetValue(frame_number);
169 double fade_in_value = fade_in.GetValue(frame_number) * fps.ToDouble();
170 double fade_out_value = fade_out.GetValue(frame_number) * fps.ToDouble();
171 double right_value = right.GetValue(frame_number);
172 double background_corner_value = background_corner.GetValue(frame_number);
173 double padding_value = background_padding.GetValue(frame_number);
174
175 // Calculate caption area (based on left, top, and right margin)
176 double left_margin_x = frame_image->width() * left_value;
177 double starting_y = (frame_image->height() * top_value) + (metrics.lineSpacing() * scale_factor);
178 double right_margin_x = frame_image->width() - (frame_image->width() * right_value);
179 double caption_area_width = right_margin_x - left_margin_x;
180 QRectF caption_area = QRectF(left_margin_x, starting_y, caption_area_width, frame_image->height());
181 QRectF caption_area_with_padding = QRectF(left_margin_x - (padding_value / 2.0), starting_y - (padding_value / 2.0), caption_area_width + padding_value, frame_image->height() + padding_value);
182
183 // Set background color of caption
184 QBrush brush;
185 QColor background_qcolor = QColor(QString(background.GetColorHex(frame_number).c_str()));
186 background_qcolor.setAlphaF(background_alpha.GetValue(frame_number));
187 brush.setColor(background_qcolor);
188 brush.setStyle(Qt::SolidPattern);
189 painter.setBrush(brush);
190 painter.setPen(Qt::NoPen);
191 painter.drawRoundedRect(caption_area_with_padding, background_corner_value, background_corner_value);
192
193 // Set text color of caption
194 QPen pen;
195 QColor stroke_qcolor;
196 if (stroke_width.GetValue(frame_number) <= 0.0) {
197 // No stroke
198 painter.setPen(Qt::NoPen);
199 } else {
200 // Stroke color
201 stroke_qcolor = QColor(QString(stroke.GetColorHex(frame_number).c_str()));
202 stroke_qcolor.setAlphaF(font_alpha.GetValue(frame_number));
203 pen.setColor(stroke_qcolor);
204 pen.setWidthF(stroke_width.GetValue(frame_number) * scale_factor);
205 painter.setPen(pen);
206 }
207 // Fill color of text
208 QColor font_qcolor = QColor(QString(color.GetColorHex(frame_number).c_str()));
209 font_qcolor.setAlphaF(font_alpha.GetValue(frame_number));
210 brush.setColor(font_qcolor);
211 painter.setBrush(brush);
212
213 // Loop through matches and find text to display (if any)
214 for (auto match = matchedCaptions.begin(); match != matchedCaptions.end(); match++) {
215
216 // Build timestamp (00:00:04.000 --> 00:00:06.500)
217 int64_t start_frame = ((match->captured(1).toFloat() * 60.0 * 60.0 ) + (match->captured(2).toFloat() * 60.0 ) +
218 match->captured(3).toFloat() + (match->captured(4).toFloat() / 1000.0)) * fps.ToFloat();
219 int64_t end_frame = ((match->captured(5).toFloat() * 60.0 * 60.0 ) + (match->captured(6).toFloat() * 60.0 ) +
220 match->captured(7).toFloat() + (match->captured(8).toFloat() / 1000.0)) * fps.ToFloat();
221
222 // Split multiple lines into separate paths
223 QStringList lines = match->captured(9).split("\n");
224 for(int index = 0; index < lines.length(); index++) {
225 // Multi-line
226 QString line = lines[index];
227 // Ignore lines that start with NOTE, or are <= 1 char long
228 if (!line.startsWith(QStringLiteral("NOTE")) &&
229 !line.isEmpty() && frame_number >= start_frame && frame_number <= end_frame &&
230 line.length() > 1) {
231
232 // Calculate fade in/out ranges
233 double fade_in_percentage = ((float) frame_number - (float) start_frame) / fade_in_value;
234 double fade_out_percentage = 1.0 - (((float) frame_number - ((float) end_frame - fade_out_value)) / fade_out_value);
235 if (fade_in_percentage < 1.0) {
236 // Fade in
237 font_qcolor.setAlphaF(fade_in_percentage * font_alpha.GetValue(frame_number));
238 stroke_qcolor.setAlphaF(fade_in_percentage * font_alpha.GetValue(frame_number));
239 } else if (fade_out_percentage >= 0.0 && fade_out_percentage <= 1.0) {
240 // Fade out
241 font_qcolor.setAlphaF(fade_out_percentage * font_alpha.GetValue(frame_number));
242 stroke_qcolor.setAlphaF(fade_out_percentage * font_alpha.GetValue(frame_number));
243 }
244 pen.setColor(stroke_qcolor);
245 brush.setColor(font_qcolor);
246 painter.setPen(pen);
247 painter.setBrush(brush);
248
249 // Loop through words, and find word-wrap boundaries
250 QStringList words = line.split(" ");
251 int words_remaining = words.length();
252 while (words_remaining > 0) {
253 bool words_displayed = false;
254 for(int word_index = words.length(); word_index > 0; word_index--) {
255 // Current matched caption string (from the beginning to the current word index)
256 QString fitting_line = words.mid(0, word_index).join(" ");
257
258 // Calculate size of text
259 QRectF textRect = metrics.boundingRect(caption_area, Qt::TextSingleLine, fitting_line);
260 if (textRect.width() <= caption_area.width()) {
261 // Location for text
262 QPoint p(left_margin_x, starting_y);
263
264 // Draw text onto path (for correct border and fill)
265 QPainterPath path1;
266 QString fitting_line = words.mid(0, word_index).join(" ");
267 path1.addText(p, font, fitting_line);
268 painter.drawPath(path1);
269
270 // Increment QPoint to height of text (for next line) + padding
271 starting_y += path1.boundingRect().height() + (metrics.lineSpacing() * scale_factor);
272
273 // Update line (to remove words already drawn
274 words = words.mid(word_index, words.length());
275 words_remaining = words.length();
276 words_displayed = true;
277 break;
278 }
279 }
280
281 if (words_displayed == false) {
282 // Exit loop if no words displayed
283 words_remaining = 0;
284 }
285 }
286
287 }
288 }
289 }
290
291 // End painter
292 painter.end();
293
294 // return the modified frame
295 return frame;
296 }
297
298 // Generate JSON string of this object
Json() const299 std::string Caption::Json() const {
300
301 // Return formatted string
302 return JsonValue().toStyledString();
303 }
304
305 // Generate Json::Value for this object
JsonValue() const306 Json::Value Caption::JsonValue() const {
307
308 // Create root json object
309 Json::Value root = EffectBase::JsonValue(); // get parent properties
310 root["type"] = info.class_name;
311 root["color"] = color.JsonValue();
312 root["stroke"] = stroke.JsonValue();
313 root["background"] = background.JsonValue();
314 root["background_alpha"] = background_alpha.JsonValue();
315 root["background_corner"] = background_corner.JsonValue();
316 root["background_padding"] = background_padding.JsonValue();
317 root["stroke_width"] = stroke_width.JsonValue();
318 root["font_size"] = font_size.JsonValue();
319 root["font_alpha"] = font_alpha.JsonValue();
320 root["fade_in"] = fade_in.JsonValue();
321 root["fade_out"] = fade_out.JsonValue();
322 root["left"] = left.JsonValue();
323 root["top"] = top.JsonValue();
324 root["right"] = right.JsonValue();
325 root["caption_text"] = caption_text;
326 root["caption_font"] = font_name;
327
328 // return JsonValue
329 return root;
330 }
331
332 // Load JSON string into this object
SetJson(const std::string value)333 void Caption::SetJson(const std::string value) {
334
335 // Parse JSON string into JSON objects
336 try
337 {
338 const Json::Value root = openshot::stringToJson(value);
339 // Set all values that match
340 SetJsonValue(root);
341 }
342 catch (const std::exception& e)
343 {
344 // Error parsing JSON (or missing keys)
345 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
346 }
347 }
348
349 // Load Json::Value into this object
SetJsonValue(const Json::Value root)350 void Caption::SetJsonValue(const Json::Value root) {
351
352 // Set parent data
353 EffectBase::SetJsonValue(root);
354
355 // Set data from Json (if key is found)
356 if (!root["color"].isNull())
357 color.SetJsonValue(root["color"]);
358 if (!root["stroke"].isNull())
359 stroke.SetJsonValue(root["stroke"]);
360 if (!root["background"].isNull())
361 background.SetJsonValue(root["background"]);
362 if (!root["background_alpha"].isNull())
363 background_alpha.SetJsonValue(root["background_alpha"]);
364 if (!root["background_corner"].isNull())
365 background_corner.SetJsonValue(root["background_corner"]);
366 if (!root["background_padding"].isNull())
367 background_padding.SetJsonValue(root["background_padding"]);
368 if (!root["stroke_width"].isNull())
369 stroke_width.SetJsonValue(root["stroke_width"]);
370 if (!root["font_size"].isNull())
371 font_size.SetJsonValue(root["font_size"]);
372 if (!root["font_alpha"].isNull())
373 font_alpha.SetJsonValue(root["font_alpha"]);
374 if (!root["fade_in"].isNull())
375 fade_in.SetJsonValue(root["fade_in"]);
376 if (!root["fade_out"].isNull())
377 fade_out.SetJsonValue(root["fade_out"]);
378 if (!root["left"].isNull())
379 left.SetJsonValue(root["left"]);
380 if (!root["top"].isNull())
381 top.SetJsonValue(root["top"]);
382 if (!root["right"].isNull())
383 right.SetJsonValue(root["right"]);
384 if (!root["caption_text"].isNull())
385 caption_text = root["caption_text"].asString();
386 if (!root["caption_font"].isNull())
387 font_name = root["caption_font"].asString();
388
389 // Mark effect as dirty to reparse Regex
390 is_dirty = true;
391 }
392
393 // Get all properties for a specific frame
PropertiesJSON(int64_t requested_frame) const394 std::string Caption::PropertiesJSON(int64_t requested_frame) const {
395
396 // Generate JSON properties list
397 Json::Value root;
398 root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
399 root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
400 root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame);
401 root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
402 root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame);
403 root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame);
404
405 // Keyframes
406 root["color"] = add_property_json("Color", 0.0, "color", "", &color.red, 0, 255, false, requested_frame);
407 root["color"]["red"] = add_property_json("Red", color.red.GetValue(requested_frame), "float", "", &color.red, 0, 255, false, requested_frame);
408 root["color"]["blue"] = add_property_json("Blue", color.blue.GetValue(requested_frame), "float", "", &color.blue, 0, 255, false, requested_frame);
409 root["color"]["green"] = add_property_json("Green", color.green.GetValue(requested_frame), "float", "", &color.green, 0, 255, false, requested_frame);
410 root["stroke"] = add_property_json("Border", 0.0, "color", "", &stroke.red, 0, 255, false, requested_frame);
411 root["stroke"]["red"] = add_property_json("Red", stroke.red.GetValue(requested_frame), "float", "", &stroke.red, 0, 255, false, requested_frame);
412 root["stroke"]["blue"] = add_property_json("Blue", stroke.blue.GetValue(requested_frame), "float", "", &stroke.blue, 0, 255, false, requested_frame);
413 root["stroke"]["green"] = add_property_json("Green", stroke.green.GetValue(requested_frame), "float", "", &stroke.green, 0, 255, false, requested_frame);
414 root["background_alpha"] = add_property_json("Background Alpha", background_alpha.GetValue(requested_frame), "float", "", &background_alpha, 0.0, 1.0, false, requested_frame);
415 root["background_corner"] = add_property_json("Background Corner Radius", background_corner.GetValue(requested_frame), "float", "", &background_corner, 0.0, 60.0, false, requested_frame);
416 root["background_padding"] = add_property_json("Background Padding", background_padding.GetValue(requested_frame), "float", "", &background_padding, 0.0, 60.0, false, requested_frame);
417 root["background"] = add_property_json("Background", 0.0, "color", "", &background.red, 0, 255, false, requested_frame);
418 root["background"]["red"] = add_property_json("Red", background.red.GetValue(requested_frame), "float", "", &background.red, 0, 255, false, requested_frame);
419 root["background"]["blue"] = add_property_json("Blue", background.blue.GetValue(requested_frame), "float", "", &background.blue, 0, 255, false, requested_frame);
420 root["background"]["green"] = add_property_json("Green", background.green.GetValue(requested_frame), "float", "", &background.green, 0, 255, false, requested_frame);
421 root["stroke_width"] = add_property_json("Stroke Width", stroke_width.GetValue(requested_frame), "float", "", &stroke_width, 0, 10.0, false, requested_frame);
422 root["font_size"] = add_property_json("Font Size", font_size.GetValue(requested_frame), "float", "", &font_size, 0, 200.0, false, requested_frame);
423 root["font_alpha"] = add_property_json("Font Alpha", font_alpha.GetValue(requested_frame), "float", "", &font_alpha, 0.0, 1.0, false, requested_frame);
424 root["fade_in"] = add_property_json("Fade In (Seconds)", fade_in.GetValue(requested_frame), "float", "", &fade_in, 0.0, 3.0, false, requested_frame);
425 root["fade_out"] = add_property_json("Fade Out (Seconds)", fade_out.GetValue(requested_frame), "float", "", &fade_out, 0.0, 3.0, false, requested_frame);
426 root["left"] = add_property_json("Left Size", left.GetValue(requested_frame), "float", "", &left, 0.0, 0.5, false, requested_frame);
427 root["top"] = add_property_json("Top Size", top.GetValue(requested_frame), "float", "", &top, 0.0, 1.0, false, requested_frame);
428 root["right"] = add_property_json("Right Size", right.GetValue(requested_frame), "float", "", &right, 0.0, 0.5, false, requested_frame);
429 root["caption_text"] = add_property_json("Captions", 0.0, "caption", caption_text, NULL, -1, -1, false, requested_frame);
430 root["caption_font"] = add_property_json("Font", 0.0, "font", font_name, NULL, -1, -1, false, requested_frame);
431
432 // Set the parent effect which properties this effect will inherit
433 root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
434
435 // Return formatted string
436 return root.toStyledString();
437 }
438