108std::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) {
126 if (timeline != 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);
160 double left_value =
left.GetValue(frame_number);
161 double top_value =
top.GetValue(frame_number);
162 double fade_in_value =
fade_in.GetValue(frame_number) * fps.
ToDouble();
164 double right_value =
right.GetValue(frame_number);
165 double background_corner_value =
background_corner.GetValue(frame_number) * timeline_scale_factor;
166 double padding_value =
background_padding.GetValue(frame_number) * timeline_scale_factor;
167 double stroke_width_value =
stroke_width.GetValue(frame_number) * timeline_scale_factor;
168 double line_spacing_value =
line_spacing.GetValue(frame_number);
169 double metrics_line_spacing = metrics.lineSpacing();
172 double left_margin_x = frame_image->width() * left_value;
173 double starting_y = (frame_image->height() * top_value) + metrics_line_spacing;
174 double current_y = starting_y;
175 double bottom_y = starting_y;
176 double top_y = starting_y;
177 double max_text_width = 0.0;
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());
183 std::vector<QPainterPath> text_paths;
184 double fade_in_percentage = 0.0;
185 double fade_out_percentage = 0.0;
186 double line_height = metrics_line_spacing * line_spacing_value;
189 auto fracToSeconds = [&](
const QString &f){
190 QString ms = f.leftJustified(3, QChar(
'0'));
191 return ms.toInt() / 1000.0;
195 for (
auto match = matchedCaptions.begin(); match != matchedCaptions.end(); match++) {
198 double startSeconds =
199 match->captured(1).toFloat() * 3600.0 +
200 match->captured(2).toFloat() * 60.0 +
201 match->captured(3).toFloat() +
202 fracToSeconds(match->captured(4));
205 match->captured(5).toFloat() * 3600.0 +
206 match->captured(6).toFloat() * 60.0 +
207 match->captured(7).toFloat() +
208 fracToSeconds(match->captured(8));
210 auto start_frame = int64_t(startSeconds * fps.
ToFloat()) + 1;
211 auto end_frame = int64_t(endSeconds * fps.
ToFloat());
214 QStringList lines = match->captured(9).split(
"\n");
215 for(
int index = 0; index < lines.length(); index++) {
217 QString line = lines[index];
219 if (!line.startsWith(QStringLiteral(
"NOTE")) &&
220 !line.isEmpty() && frame_number >= start_frame && frame_number <= end_frame && line.length() > 1) {
223 fade_in_percentage = ((float) frame_number - (float) start_frame) / fade_in_value;
224 fade_out_percentage = 1.0 - (((float) frame_number - ((float) end_frame - fade_out_value)) / fade_out_value);
227 QStringList words = line.split(
" ");
230 bool use_spaces =
true;
231 if (line.length() > 20 && words.length() == 1) {
232 words = line.split(
"");
235 int words_remaining = words.length();
236 while (words_remaining > 0) {
237 bool words_displayed =
false;
238 for(
int word_index = words.length(); word_index > 0; word_index--) {
240 QString fitting_line = words.mid(0, word_index).join(
" ");
243 QRectF textRect = metrics.boundingRect(caption_area, Qt::TextSingleLine, fitting_line);
244 if (textRect.width() <= caption_area.width()) {
246 QPoint p(left_margin_x, current_y);
250 QString fitting_line;
252 fitting_line = words.mid(0, word_index).join(
" ");
254 fitting_line = words.mid(0, word_index).join(
"");
256 path1.addText(p, font, fitting_line);
257 text_paths.push_back(path1);
260 words = words.mid(word_index, words.length());
261 words_remaining = words.length();
262 words_displayed =
true;
265 current_y += line_height;
268 if (path1.boundingRect().width() > max_text_width) {
269 max_text_width = path1.boundingRect().width();
272 if (path1.boundingRect().top() < top_y) {
273 top_y = path1.boundingRect().top();
276 if (path1.boundingRect().bottom() > bottom_y) {
277 bottom_y = path1.boundingRect().bottom();
283 if (!words_displayed) {
294 QRectF caption_area_with_padding = QRectF(left_margin_x - (padding_value / 2.0),
295 top_y - (padding_value / 2.0),
296 max_text_width + padding_value,
297 (bottom_y - top_y) + padding_value);
300 double alignment_offset = std::max((caption_area_width - max_text_width) / 2.0, 0.0);
303 QBrush background_brush;
304 QColor background_qcolor = QColor(QString(
background.GetColorHex(frame_number).c_str()));
306 caption_area_with_padding.translate(alignment_offset, 0.0);
307 if (fade_in_percentage < 1.0) {
309 background_qcolor.setAlphaF(fade_in_percentage *
background_alpha.GetValue(frame_number));
310 }
else if (fade_out_percentage >= 0.0 && fade_out_percentage <= 1.0) {
312 background_qcolor.setAlphaF(fade_out_percentage *
background_alpha.GetValue(frame_number));
316 background_brush.setColor(background_qcolor);
317 background_brush.setStyle(Qt::SolidPattern);
318 painter.setBrush(background_brush);
319 painter.setPen(Qt::NoPen);
320 painter.drawRoundedRect(caption_area_with_padding, background_corner_value, background_corner_value);
324 QColor font_qcolor = QColor(QString(
color.GetColorHex(frame_number).c_str()));
325 font_qcolor.setAlphaF(
font_alpha.GetValue(frame_number));
326 font_brush.setStyle(Qt::SolidPattern);
330 QColor stroke_qcolor;
331 stroke_qcolor = QColor(QString(
stroke.GetColorHex(frame_number).c_str()));
332 stroke_qcolor.setAlphaF(
font_alpha.GetValue(frame_number));
333 pen.setColor(stroke_qcolor);
334 pen.setWidthF(std::max(stroke_width_value, 0.0));
338 for(QPainterPath
path : text_paths) {
340 path.translate(alignment_offset, 0.0);
341 if (fade_in_percentage < 1.0) {
343 font_qcolor.setAlphaF(fade_in_percentage *
font_alpha.GetValue(frame_number));
344 stroke_qcolor.setAlphaF(fade_in_percentage *
font_alpha.GetValue(frame_number));
345 }
else if (fade_out_percentage >= 0.0 && fade_out_percentage <= 1.0) {
347 font_qcolor.setAlphaF(fade_out_percentage *
font_alpha.GetValue(frame_number));
348 stroke_qcolor.setAlphaF(fade_out_percentage *
font_alpha.GetValue(frame_number));
350 pen.setColor(stroke_qcolor);
351 font_brush.setColor(font_qcolor);
354 if (stroke_width_value <= 0.0) {
355 painter.setPen(Qt::NoPen);
360 painter.setBrush(font_brush);
361 painter.drawPath(
path);
477 root[
"color"][
"red"] =
add_property_json(
"Red",
color.red.GetValue(requested_frame),
"float",
"", &
color.red, 0, 255,
false, requested_frame);
478 root[
"color"][
"blue"] =
add_property_json(
"Blue",
color.blue.GetValue(requested_frame),
"float",
"", &
color.blue, 0, 255,
false, requested_frame);
479 root[
"color"][
"green"] =
add_property_json(
"Green",
color.green.GetValue(requested_frame),
"float",
"", &
color.green, 0, 255,
false, requested_frame);
481 root[
"stroke"][
"red"] =
add_property_json(
"Red",
stroke.red.GetValue(requested_frame),
"float",
"", &
stroke.red, 0, 255,
false, requested_frame);
482 root[
"stroke"][
"blue"] =
add_property_json(
"Blue",
stroke.blue.GetValue(requested_frame),
"float",
"", &
stroke.blue, 0, 255,
false, requested_frame);
483 root[
"stroke"][
"green"] =
add_property_json(
"Green",
stroke.green.GetValue(requested_frame),
"float",
"", &
stroke.green, 0, 255,
false, requested_frame);
497 root[
"left"] =
add_property_json(
"Left Size",
left.GetValue(requested_frame),
"float",
"", &
left, 0.0, 0.5,
false, requested_frame);
498 root[
"top"] =
add_property_json(
"Top Size",
top.GetValue(requested_frame),
"float",
"", &
top, 0.0, 1.0,
false, requested_frame);
499 root[
"right"] =
add_property_json(
"Right Size",
right.GetValue(requested_frame),
"float",
"", &
right, 0.0, 0.5,
false, requested_frame);
500 root[
"caption_text"] =
add_property_json(
"Captions", 0.0,
"caption", caption_text, NULL, -1, -1,
false, requested_frame);
504 return root.toStyledString();