OpenShot Library | libopenshot 0.3.2
Loading...
Searching...
No Matches
QtTextReader.cpp
Go to the documentation of this file.
1
11// Copyright (c) 2008-2019 OpenShot Studios, LLC
12//
13// SPDX-License-Identifier: LGPL-3.0-or-later
14
15#include "QtTextReader.h"
16#include "CacheBase.h"
17#include "Exceptions.h"
18#include "Frame.h"
19
20#include <QImage>
21#include <QPainter>
22
23using namespace openshot;
24
26QtTextReader::QtTextReader() : width(1024), height(768), x_offset(0), y_offset(0), text(""), font(QFont("Arial", 10)), text_color("#ffffff"), background_color("#000000"), is_open(false), gravity(GRAVITY_CENTER)
27{
28 // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
29 Open();
30 Close();
31}
32
33QtTextReader::QtTextReader(int width, int height, int x_offset, int y_offset, GravityType gravity, std::string text, QFont font, std::string text_color, std::string background_color)
34: width(width), height(height), x_offset(x_offset), y_offset(y_offset), text(text), font(font), text_color(text_color), background_color(background_color), is_open(false), gravity(gravity)
35{
36 // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
37 Open();
38 Close();
39}
40
41void QtTextReader::SetTextBackgroundColor(std::string color) {
42 text_background_color = color;
43
44 // Open and Close the reader, to populate it's attributes (such as height, width, etc...) plus the text background color
45 Open();
46 Close();
47}
48
49// Open reader
51{
52 // Open reader if not already open
53 if (!is_open)
54 {
55 // create image
56 image = std::make_shared<QImage>(width, height, QImage::Format_RGBA8888_Premultiplied);
57 image->fill(QColor(background_color.c_str()));
58
59 QPainter painter;
60 if (!painter.begin(image.get())) {
61 return;
62 }
63
64 // set background
65 if (!text_background_color.empty()) {
66 painter.setBackgroundMode(Qt::OpaqueMode);
67 painter.setBackground(QBrush(text_background_color.c_str()));
68 }
69
70 // set font color
71 painter.setPen(QPen(text_color.c_str()));
72
73 // set font
74 painter.setFont(font);
75
76 // Set gravity (map between OpenShot and Qt)
77 int align_flag = 0;
78 switch (gravity)
79 {
81 align_flag = Qt::AlignLeft | Qt::AlignTop;
82 break;
83 case GRAVITY_TOP:
84 align_flag = Qt::AlignHCenter | Qt::AlignTop;
85 break;
87 align_flag = Qt::AlignRight | Qt::AlignTop;
88 break;
89 case GRAVITY_LEFT:
90 align_flag = Qt::AlignVCenter | Qt::AlignLeft;
91 break;
92 case GRAVITY_CENTER:
93 align_flag = Qt::AlignCenter;
94 break;
95 case GRAVITY_RIGHT:
96 align_flag = Qt::AlignVCenter | Qt::AlignRight;
97 break;
99 align_flag = Qt::AlignLeft | Qt::AlignBottom;
100 break;
101 case GRAVITY_BOTTOM:
102 align_flag = Qt::AlignHCenter | Qt::AlignBottom;
103 break;
105 align_flag = Qt::AlignRight | Qt::AlignBottom;
106 break;
107 }
108
109 // Draw image
110 painter.drawText(x_offset, y_offset, width, height, align_flag, text.c_str());
111
112 painter.end();
113
114 // Update image properties
115 info.has_audio = false;
116 info.has_video = true;
117 info.file_size = 0;
118 info.vcodec = "QImage";
119 info.width = width;
120 info.height = height;
121 info.pixel_ratio.num = 1;
122 info.pixel_ratio.den = 1;
123 info.duration = 60 * 60 * 1; // 1 hour duration
124 info.fps.num = 30;
125 info.fps.den = 1;
129
130 // Calculate the DAR (display aspect ratio)
132
133 // Reduce size fraction
134 font_size.Reduce();
135
136 // Set the ratio based on the reduced fraction
137 info.display_ratio.num = font_size.num;
138 info.display_ratio.den = font_size.den;
139
140 // Mark as "open"
141 is_open = true;
142 }
143}
144
145// Close reader
147{
148 // Close all objects, if reader is 'open'
149 if (is_open)
150 {
151 // Mark as "closed"
152 is_open = false;
153
154 // Delete the image
155 image.reset();
156
157 info.vcodec = "";
158 info.acodec = "";
159 }
160}
161
162// Get an openshot::Frame object for a specific frame number of this reader.
163std::shared_ptr<Frame> QtTextReader::GetFrame(int64_t requested_frame)
164{
165 if (image)
166 {
167 // Create or get frame object
168 auto image_frame = std::make_shared<Frame>(
169 requested_frame, image->size().width(), image->size().height(),
170 background_color, 0, 2);
171
172 // Add Image data to frame
173 image_frame->AddImage(image);
174
175 // return frame object
176 return image_frame;
177 } else {
178 // return empty frame
179 auto image_frame = std::make_shared<Frame>(1, 640, 480, background_color, 0, 2);
180
181 // return frame object
182 return image_frame;
183 }
184
185}
186
187// Generate JSON string of this object
188std::string QtTextReader::Json() const {
189
190 // Return formatted string
191 return JsonValue().toStyledString();
192}
193
194// Generate Json::Value for this object
195Json::Value QtTextReader::JsonValue() const {
196
197 // Create root json object
198 Json::Value root = ReaderBase::JsonValue(); // get parent properties
199 root["type"] = "QtTextReader";
200 root["width"] = width;
201 root["height"] = height;
202 root["x_offset"] = x_offset;
203 root["y_offset"] = y_offset;
204 root["text"] = text;
205 root["font"] = font.toString().toStdString();
206 root["text_color"] = text_color;
207 root["background_color"] = background_color;
208 root["text_background_color"] = text_background_color;
209 root["gravity"] = gravity;
210
211 // return JsonValue
212 return root;
213}
214
215// Load JSON string into this object
216void QtTextReader::SetJson(const std::string value) {
217
218 // Parse JSON string into JSON objects
219 try
220 {
221 const Json::Value root = openshot::stringToJson(value);
222 // Set all values that match
223 SetJsonValue(root);
224 }
225 catch (const std::exception& e)
226 {
227 // Error parsing JSON (or missing keys)
228 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
229 }
230}
231
232// Load Json::Value into this object
233void QtTextReader::SetJsonValue(const Json::Value root) {
234
235 // Set parent data
237
238 // Set data from Json (if key is found)
239 if (!root["width"].isNull())
240 width = root["width"].asInt();
241 if (!root["height"].isNull())
242 height = root["height"].asInt();
243 if (!root["x_offset"].isNull())
244 x_offset = root["x_offset"].asInt();
245 if (!root["y_offset"].isNull())
246 y_offset = root["y_offset"].asInt();
247 if (!root["text"].isNull())
248 text = root["text"].asString();
249 if (!root["font"].isNull())
250 font.fromString(QString::fromStdString(root["font"].asString()));
251 if (!root["text_color"].isNull())
252 text_color = root["text_color"].asString();
253 if (!root["background_color"].isNull())
254 background_color = root["background_color"].asString();
255 if (!root["text_background_color"].isNull())
256 text_background_color = root["text_background_color"].asString();
257 if (!root["gravity"].isNull())
258 gravity = (GravityType) root["gravity"].asInt();
259
260 // Re-Open path, and re-init everything (if needed)
261 if (is_open)
262 {
263 Close();
264 Open();
265 }
266}
Header file for CacheBase class.
Header file for all Exception classes.
Header file for Frame class.
Header file for QtTextReader class.
This class represents a fraction.
Definition Fraction.h:30
int num
Numerator for the fraction.
Definition Fraction.h:32
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition Fraction.cpp:40
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition Fraction.cpp:65
int den
Denominator for the fraction.
Definition Fraction.h:33
Exception for invalid JSON.
Definition Exceptions.h:218
void SetJson(const std::string value) override
Load JSON string into this object.
void SetTextBackgroundColor(std::string color)
void Close() override
Close Reader.
std::string Json() const override
Generate JSON string of this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
QtTextReader()
Default constructor (blank text)
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Json::Value JsonValue() const override
Generate Json::Value for this object.
void Open() override
Open Reader - which is called by the constructor automatically.
openshot::ReaderInfo info
Information about the current media file.
Definition ReaderBase.h:88
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition Enums.h:22
@ GRAVITY_TOP_LEFT
Align clip to the top left of its parent.
Definition Enums.h:23
@ GRAVITY_LEFT
Align clip to the left of its parent (middle aligned)
Definition Enums.h:26
@ GRAVITY_TOP_RIGHT
Align clip to the top right of its parent.
Definition Enums.h:25
@ GRAVITY_RIGHT
Align clip to the right of its parent (middle aligned)
Definition Enums.h:28
@ GRAVITY_BOTTOM_LEFT
Align clip to the bottom left of its parent.
Definition Enums.h:29
@ GRAVITY_BOTTOM
Align clip to the bottom center of its parent.
Definition Enums.h:30
@ GRAVITY_TOP
Align clip to the top center of its parent.
Definition Enums.h:24
@ GRAVITY_BOTTOM_RIGHT
Align clip to the bottom right of its parent.
Definition Enums.h:31
@ GRAVITY_CENTER
Align clip to the center of its parent (middle aligned)
Definition Enums.h:27
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
float duration
Length of time (in seconds)
Definition ReaderBase.h:43
int width
The width of the video (in pixesl)
Definition ReaderBase.h:46
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition ReaderBase.h:48
openshot::Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3)
Definition ReaderBase.h:51
int height
The height of the video (in pixels)
Definition ReaderBase.h:45
int64_t video_length
The number of frames in the video stream.
Definition ReaderBase.h:53
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition ReaderBase.h:58
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition ReaderBase.h:52
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
Definition ReaderBase.h:50
bool has_video
Determines if this file has a video stream.
Definition ReaderBase.h:40
bool has_audio
Determines if this file has an audio stream.
Definition ReaderBase.h:41
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition ReaderBase.h:55
int64_t file_size
Size of file (in bytes)
Definition ReaderBase.h:44