253std::shared_ptr<openshot::Frame>
Timer::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
257 QSize image_size(1280, 720);
259 if (
clip &&
clip->ParentTimeline() != NULL) {
265 if (timeline != NULL) {
267 }
else if (
clip != NULL &&
clip->Reader() != NULL) {
268 image_size = QSize(
clip->Reader()->info.width,
clip->Reader()->info.height);
271 if (!frame->has_image_data)
272 frame->AddColor(image_size.width(), image_size.height(),
"#00000000");
274 std::shared_ptr<QImage> frame_image = frame->GetImage();
275 if (!frame_image || frame_image->isNull())
278 const double scale_factor = frame_image->width() / 600.0;
279 const double font_size_value = std::max(1.0,
font_size.GetValue(frame_number) * scale_factor);
280 const double stroke_width_value = std::max(0.0,
stroke_width.GetValue(frame_number) * scale_factor);
281 const double padding_value = std::max(0.0,
background_padding.GetValue(frame_number) * scale_factor);
282 const double corner_value = std::max(0.0,
background_corner.GetValue(frame_number) * scale_factor);
283 const QString timer_text = QString::fromStdString(
TimerText(frame_number));
284 const QString layout_text = QString::fromStdString(TimerLayoutText(frame_number));
286 QFont font(QString::fromStdString(
font_name),
int(font_size_value));
287 font.setPixelSize(font_size_value);
288 QFontMetricsF metrics(font);
289 QRectF text_bounds = metrics.tightBoundingRect(timer_text);
290 if (text_bounds.isEmpty())
291 text_bounds = metrics.boundingRect(timer_text);
292 QRectF layout_bounds = metrics.tightBoundingRect(layout_text);
293 if (layout_bounds.isEmpty())
294 layout_bounds = metrics.boundingRect(layout_text);
296 const double text_width = std::max(1.0, text_bounds.width());
297 const double text_height = std::max(1.0, text_bounds.height());
298 double digit_slot_width = 0.0;
299 for (
int digit = 0; digit <= 9; ++digit)
300 digit_slot_width = std::max(digit_slot_width, TextAdvance(metrics, QString::number(digit)));
302 const bool use_slot_layout = timer_text.size() == layout_text.size() &&
304 std::vector<double> slot_widths;
305 double layout_width = 0.0;
306 if (use_slot_layout) {
307 slot_widths.reserve(layout_text.size());
308 for (
int index = 0; index < layout_text.size(); ++index) {
309 const QString layout_char(layout_text[index]);
310 const double slot_width = layout_text[index].isDigit() ? digit_slot_width : TextAdvance(metrics, layout_char);
311 slot_widths.push_back(slot_width);
312 layout_width += slot_width;
315 layout_width = std::max(text_width, layout_bounds.width());
317 layout_width = std::max(1.0, layout_width);
318 const double rendered_box_width = layout_width + (padding_value * 2.0);
319 const double box_height = std::max(text_height, layout_bounds.height()) + (padding_value * 2.0);
321 double x = padding_value;
323 const int resolved_gravity = ClampGravity(
gravity);
325 x = (frame_image->width() - rendered_box_width) / 2.0;
327 x = frame_image->width() - rendered_box_width - padding_value;
329 switch (resolved_gravity) {
337 y = (frame_image->height() - box_height) / 2.0;
342 y = frame_image->height() - box_height;
347 x += frame_image->width() * (
x_offset.GetValue(frame_number) / 100.0);
348 y += frame_image->height() * (
y_offset.GetValue(frame_number) / 100.0);
352 double text_x = x + padding_value - text_bounds.left();
354 text_x = x + padding_value + ((layout_width - text_width) / 2.0) - text_bounds.left();
356 text_x = x + padding_value + layout_width - text_width - text_bounds.left();
357 text_x = std::round(text_x);
358 const double text_y = std::round(y + padding_value - text_bounds.top());
360 QPainter painter(frame_image.get());
361 painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing,
true);
362 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
365 QColor background_qcolor(QString::fromStdString(
background.GetColorHex(frame_number)));
366 background_qcolor.setAlphaF(std::max(0.0, std::min(1.0,
background_alpha.GetValue(frame_number))));
367 painter.setPen(Qt::NoPen);
368 painter.setBrush(QBrush(background_qcolor));
369 painter.drawRoundedRect(QRectF(x, y, rendered_box_width, box_height), corner_value, corner_value);
373 if (use_slot_layout) {
374 double cursor_x = x + padding_value;
375 for (
int index = 0; index < timer_text.size(); ++index) {
376 const QString text_char(timer_text[index]);
377 QRectF char_bounds = metrics.tightBoundingRect(text_char);
378 if (char_bounds.isEmpty())
379 char_bounds = metrics.boundingRect(text_char);
381 const double char_x = std::round(cursor_x);
382 path.addText(QPointF(char_x - char_bounds.left(), text_y), font, text_char);
383 cursor_x += slot_widths[index];
386 path.addText(QPointF(text_x, text_y), font, timer_text);
389 QColor stroke_qcolor(QString::fromStdString(
stroke.GetColorHex(frame_number)));
390 stroke_qcolor.setAlphaF(std::max(0.0, std::min(1.0,
font_alpha.GetValue(frame_number))));
391 QPen pen(stroke_qcolor);
392 pen.setWidthF(stroke_width_value);
393 painter.setPen(stroke_width_value <= 0.0 ? Qt::NoPen : pen);
395 QColor font_qcolor(QString::fromStdString(
color.GetColorHex(frame_number)));
396 font_qcolor.setAlphaF(std::max(0.0, std::min(1.0,
font_alpha.GetValue(frame_number))));
397 painter.setBrush(QBrush(font_qcolor));
398 painter.drawPath(
path);
530 root[
"color"] =
add_property_json(
"Text Color", 0.0,
"color",
"", &
color.red, 0, 255,
false, requested_frame);
531 root[
"color"][
"red"] =
add_property_json(
"Red",
color.red.GetValue(requested_frame),
"float",
"", &
color.red, 0, 255,
false, requested_frame);
532 root[
"color"][
"blue"] =
add_property_json(
"Blue",
color.blue.GetValue(requested_frame),
"float",
"", &
color.blue, 0, 255,
false, requested_frame);
533 root[
"color"][
"green"] =
add_property_json(
"Green",
color.green.GetValue(requested_frame),
"float",
"", &
color.green, 0, 255,
false, requested_frame);
534 root[
"stroke"] =
add_property_json(
"Stroke Color", 0.0,
"color",
"", &
stroke.red, 0, 255,
false, requested_frame);
535 root[
"stroke"][
"red"] =
add_property_json(
"Red",
stroke.red.GetValue(requested_frame),
"float",
"", &
stroke.red, 0, 255,
false, requested_frame);
536 root[
"stroke"][
"blue"] =
add_property_json(
"Blue",
stroke.blue.GetValue(requested_frame),
"float",
"", &
stroke.blue, 0, 255,
false, requested_frame);
537 root[
"stroke"][
"green"] =
add_property_json(
"Green",
stroke.green.GetValue(requested_frame),
"float",
"", &
stroke.green, 0, 255,
false, requested_frame);
539 const int resolved_gravity = ClampGravity(
gravity);
563 return root.toStyledString();