Skip to content

Commit 7095e4f

Browse files
committed
Adjust caption scaling logic, to keep different resolution projects from having dramatically different font sizes
1 parent c250151 commit 7095e4f

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/effects/Caption.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
using namespace openshot;
2828

2929
/// Blank constructor, useful when using Json to load the effect properties
30-
Caption::Caption() : color("#ffffff"), stroke("#a9a9a9"), background("#ff000000"), background_alpha(0.0), left(0.1), top(0.70), right(0.1),
30+
Caption::Caption() : color("#ffffff"), stroke("#a9a9a9"), background("#ff000000"), background_alpha(0.0), left(0.1), top(0.72), right(0.1),
3131
stroke_width(0.5), font_size(20.0), font_alpha(1.0), is_dirty(true), font_name("sans"), font(NULL), metrics(NULL),
3232
fade_in(0.35), fade_out(0.35), background_corner(10.0), background_padding(20.0), line_spacing(1.0)
3333
{
@@ -37,9 +37,10 @@ Caption::Caption() : color("#ffffff"), stroke("#a9a9a9"), background("#ff000000"
3737

3838
// Default constructor
3939
Caption::Caption(std::string captions) :
40-
color("#ffffff"), caption_text(captions), stroke("#a9a9a9"), background("#ff000000"), background_alpha(0.0),
41-
left(0.1), top(0.70), right(0.1), stroke_width(0.5), font_size(20.0), font_alpha(1.0), is_dirty(true), font_name("sans"),
42-
font(NULL), metrics(NULL), fade_in(0.35), fade_out(0.35), background_corner(10.0), background_padding(20.0), line_spacing(1.0)
40+
color("#ffffff"), stroke("#a9a9a9"), background("#ff000000"), background_alpha(0.0), left(0.1), top(0.72), right(0.1),
41+
stroke_width(0.5), font_size(20.0), font_alpha(1.0), is_dirty(true), font_name("sans"), font(NULL), metrics(NULL),
42+
fade_in(0.35), fade_out(0.35), background_corner(10.0), background_padding(20.0), line_spacing(1.0),
43+
caption_text(captions)
4344
{
4445
// Init effect properties
4546
init_effect_details();
@@ -116,7 +117,7 @@ std::shared_ptr<openshot::Frame> Caption::GetFrame(std::shared_ptr<openshot::Fra
116117
Clip* clip = (Clip*) ParentClip();
117118
Timeline* timeline = NULL;
118119
Fraction fps;
119-
double scale_factor = 1.0; // amount of scaling needed for text (based on preview window size)
120+
120121
if (clip && clip->ParentTimeline() != NULL) {
121122
timeline = (Timeline*) clip->ParentTimeline();
122123
} else if (this->ParentTimeline() != NULL) {
@@ -125,19 +126,18 @@ std::shared_ptr<openshot::Frame> Caption::GetFrame(std::shared_ptr<openshot::Fra
125126

126127
// Get the FPS from the parent object (Timeline or Clip's Reader)
127128
if (timeline != NULL) {
128-
fps.num = timeline->info.fps.num;
129-
fps.den = timeline->info.fps.den;
130-
// preview window is sometimes smaller/larger than the timeline size
131-
scale_factor = (double) timeline->preview_width / (double) timeline->info.width;
129+
fps = timeline->info.fps;
132130
} else if (clip != NULL && clip->Reader() != NULL) {
133-
fps.num = clip->Reader()->info.fps.num;
134-
fps.den = clip->Reader()->info.fps.den;
135-
scale_factor = 1.0;
131+
fps = clip->Reader()->info.fps;
136132
}
137133

138134
// Get the frame's image
139135
std::shared_ptr<QImage> frame_image = frame->GetImage();
140136

137+
// Calculate scale factor, to keep different resolutions from
138+
// having dramatically different font sizes
139+
double timeline_scale_factor = frame->GetImage()->width() / 1280.0;
140+
141141
// Load timeline's new frame image into a QPainter
142142
QPainter painter(frame_image.get());
143143
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing, true);
@@ -146,7 +146,7 @@ std::shared_ptr<openshot::Frame> Caption::GetFrame(std::shared_ptr<openshot::Fra
146146
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
147147

148148
// Font options and metrics for caption text
149-
double font_size_value = font_size.GetValue(frame_number) * device_pixel_ratio;
149+
double font_size_value = font_size.GetValue(frame_number) * device_pixel_ratio * timeline_scale_factor;
150150
QFont font(QString(font_name.c_str()), int(font_size_value));
151151
font.setPointSizeF(std::max(font_size_value, 1.0));
152152
QFontMetricsF metrics = QFontMetricsF(font);
@@ -157,9 +157,9 @@ std::shared_ptr<openshot::Frame> Caption::GetFrame(std::shared_ptr<openshot::Fra
157157
double fade_in_value = fade_in.GetValue(frame_number) * fps.ToDouble();
158158
double fade_out_value = fade_out.GetValue(frame_number) * fps.ToDouble();
159159
double right_value = right.GetValue(frame_number);
160-
double background_corner_value = background_corner.GetValue(frame_number) * scale_factor;
161-
double padding_value = background_padding.GetValue(frame_number) * scale_factor;
162-
double stroke_width_value = stroke_width.GetValue(frame_number) * scale_factor;
160+
double background_corner_value = background_corner.GetValue(frame_number) * timeline_scale_factor;
161+
double padding_value = background_padding.GetValue(frame_number) * timeline_scale_factor;
162+
double stroke_width_value = stroke_width.GetValue(frame_number) * timeline_scale_factor;
163163
double line_spacing_value = line_spacing.GetValue(frame_number);
164164
double metrics_line_spacing = metrics.lineSpacing();
165165

0 commit comments

Comments
 (0)