108std::shared_ptr<openshot::Frame>
Caption::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
117 QSize image_size(1, 1);
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);
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 for (
auto match = matchedCaptions.begin(); match != matchedCaptions.end(); match++) {
192 int64_t start_frame = ((match->captured(1).toFloat() * 60.0 * 60.0 ) + (match->captured(2).toFloat() * 60.0 ) +
193 match->captured(3).toFloat() + (match->captured(4).toFloat() / 1000.0)) * fps.
ToFloat();
194 int64_t end_frame = ((match->captured(5).toFloat() * 60.0 * 60.0 ) + (match->captured(6).toFloat() * 60.0 ) +
195 match->captured(7).toFloat() + (match->captured(8).toFloat() / 1000.0)) * fps.
ToFloat();
198 QStringList lines = match->captured(9).split(
"\n");
199 for(
int index = 0; index < lines.length(); index++) {
201 QString line = lines[index];
203 if (!line.startsWith(QStringLiteral(
"NOTE")) &&
204 !line.isEmpty() && frame_number >= start_frame && frame_number <= end_frame && line.length() > 1) {
207 fade_in_percentage = ((float) frame_number - (
float) start_frame) / fade_in_value;
208 fade_out_percentage = 1.0 - (((float) frame_number - ((
float) end_frame - fade_out_value)) / fade_out_value);
211 QStringList words = line.split(
" ");
214 bool use_spaces =
true;
215 if (line.length() > 20 && words.length() == 1) {
216 words = line.split(
"");
219 int words_remaining = words.length();
220 while (words_remaining > 0) {
221 bool words_displayed =
false;
222 for(
int word_index = words.length(); word_index > 0; word_index--) {
224 QString fitting_line = words.mid(0, word_index).join(
" ");
227 QRectF textRect = metrics.boundingRect(caption_area, Qt::TextSingleLine, fitting_line);
228 if (textRect.width() <= caption_area.width()) {
230 QPoint p(left_margin_x, current_y);
234 QString fitting_line;
236 fitting_line = words.mid(0, word_index).join(
" ");
238 fitting_line = words.mid(0, word_index).join(
"");
240 path1.addText(p, font, fitting_line);
241 text_paths.push_back(path1);
244 words = words.mid(word_index, words.length());
245 words_remaining = words.length();
246 words_displayed =
true;
249 current_y += line_height;
252 if (path1.boundingRect().width() > max_text_width) {
253 max_text_width = path1.boundingRect().width();
256 if (path1.boundingRect().top() < top_y) {
257 top_y = path1.boundingRect().top();
260 if (path1.boundingRect().bottom() > bottom_y) {
261 bottom_y = path1.boundingRect().bottom();
267 if (!words_displayed) {
278 QRectF caption_area_with_padding = QRectF(left_margin_x - (padding_value / 2.0),
279 top_y - (padding_value / 2.0),
280 max_text_width + padding_value,
281 (bottom_y - top_y) + padding_value);
284 double alignment_offset = std::max((caption_area_width - max_text_width) / 2.0, 0.0);
287 QBrush background_brush;
290 caption_area_with_padding.translate(alignment_offset, 0.0);
291 if (fade_in_percentage < 1.0) {
294 }
else if (fade_out_percentage >= 0.0 && fade_out_percentage <= 1.0) {
300 background_brush.setColor(background_qcolor);
301 background_brush.setStyle(Qt::SolidPattern);
302 painter.setBrush(background_brush);
303 painter.setPen(Qt::NoPen);
304 painter.drawRoundedRect(caption_area_with_padding, background_corner_value, background_corner_value);
308 QColor font_qcolor = QColor(QString(
color.
GetColorHex(frame_number).c_str()));
310 font_brush.setStyle(Qt::SolidPattern);
314 QColor stroke_qcolor;
317 pen.setColor(stroke_qcolor);
318 pen.setWidthF(std::max(stroke_width_value, 0.0));
322 for(QPainterPath
path : text_paths) {
324 path.translate(alignment_offset, 0.0);
325 if (fade_in_percentage < 1.0) {
329 }
else if (fade_out_percentage >= 0.0 && fade_out_percentage <= 1.0) {
334 pen.setColor(stroke_qcolor);
335 font_brush.setColor(font_qcolor);
338 if (stroke_width_value <= 0.0) {
339 painter.setPen(Qt::NoPen);
344 painter.setBrush(font_brush);
345 painter.drawPath(
path);