OpenShot Library | libopenshot 0.2.7
QtTextReader.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Source file for QtTextReader class
4 * @author Jonathan Thomas <jonathan@openshot.org>
5 * @author Sergei Kolesov (jediserg)
6 * @author Jeff Shillitto (jeffski)
7 *
8 * @ref License
9 */
10
11/* LICENSE
12 *
13 * Copyright (c) 2008-2019 OpenShot Studios, LLC
14 * <http://www.openshotstudios.com/>. This file is part of
15 * OpenShot Library (libopenshot), an open-source project dedicated to
16 * delivering high quality video editing and animation solutions to the
17 * world. For more information visit <http://www.openshot.org/>.
18 *
19 * OpenShot Library (libopenshot) is free software: you can redistribute it
20 * and/or modify it under the terms of the GNU Lesser General Public License
21 * as published by the Free Software Foundation, either version 3 of the
22 * License, or (at your option) any later version.
23 *
24 * OpenShot Library (libopenshot) is distributed in the hope that it will be
25 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU Lesser General Public License for more details.
28 *
29 * You should have received a copy of the GNU Lesser General Public License
30 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
31 */
32
33#include "QtTextReader.h"
34#include "Exceptions.h"
35#include <QImage>
36#include <QPainter>
37
38using namespace openshot;
39
40/// Default constructor (blank text)
41QtTextReader::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)
42{
43 // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
44 Open();
45 Close();
46}
47
48QtTextReader::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)
49: 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)
50{
51 // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
52 Open();
53 Close();
54}
55
56void QtTextReader::SetTextBackgroundColor(std::string color) {
57 text_background_color = color;
58
59 // Open and Close the reader, to populate it's attributes (such as height, width, etc...) plus the text background color
60 Open();
61 Close();
62}
63
64// Open reader
66{
67 // Open reader if not already open
68 if (!is_open)
69 {
70 // create image
71 image = std::make_shared<QImage>(width, height, QImage::Format_RGBA8888_Premultiplied);
72 image->fill(QColor(background_color.c_str()));
73
74 QPainter painter;
75 if (!painter.begin(image.get())) {
76 return;
77 }
78
79 // set background
80 if (!text_background_color.empty()) {
81 painter.setBackgroundMode(Qt::OpaqueMode);
82 painter.setBackground(QBrush(text_background_color.c_str()));
83 }
84
85 // set font color
86 painter.setPen(QPen(text_color.c_str()));
87
88 // set font
89 painter.setFont(font);
90
91 // Set gravity (map between OpenShot and Qt)
92 int align_flag = 0;
93 switch (gravity)
94 {
96 align_flag = Qt::AlignLeft | Qt::AlignTop;
97 break;
98 case GRAVITY_TOP:
99 align_flag = Qt::AlignHCenter | Qt::AlignTop;
100 break;
102 align_flag = Qt::AlignRight | Qt::AlignTop;
103 break;
104 case GRAVITY_LEFT:
105 align_flag = Qt::AlignVCenter | Qt::AlignLeft;
106 break;
107 case GRAVITY_CENTER:
108 align_flag = Qt::AlignCenter;
109 break;
110 case GRAVITY_RIGHT:
111 align_flag = Qt::AlignVCenter | Qt::AlignRight;
112 break;
114 align_flag = Qt::AlignLeft | Qt::AlignBottom;
115 break;
116 case GRAVITY_BOTTOM:
117 align_flag = Qt::AlignHCenter | Qt::AlignBottom;
118 break;
120 align_flag = Qt::AlignRight | Qt::AlignBottom;
121 break;
122 }
123
124 // Draw image
125 painter.drawText(x_offset, y_offset, width, height, align_flag, text.c_str());
126
127 painter.end();
128
129 // Update image properties
130 info.has_audio = false;
131 info.has_video = true;
132 info.file_size = 0;
133 info.vcodec = "QImage";
134 info.width = width;
135 info.height = height;
136 info.pixel_ratio.num = 1;
137 info.pixel_ratio.den = 1;
138 info.duration = 60 * 60 * 1; // 1 hour duration
139 info.fps.num = 30;
140 info.fps.den = 1;
144
145 // Calculate the DAR (display aspect ratio)
147
148 // Reduce size fraction
149 font_size.Reduce();
150
151 // Set the ratio based on the reduced fraction
152 info.display_ratio.num = font_size.num;
153 info.display_ratio.den = font_size.den;
154
155 // Mark as "open"
156 is_open = true;
157 }
158}
159
160// Close reader
162{
163 // Close all objects, if reader is 'open'
164 if (is_open)
165 {
166 // Mark as "closed"
167 is_open = false;
168
169 // Delete the image
170 image.reset();
171
172 info.vcodec = "";
173 info.acodec = "";
174 }
175}
176
177// Get an openshot::Frame object for a specific frame number of this reader.
178std::shared_ptr<Frame> QtTextReader::GetFrame(int64_t requested_frame)
179{
180 if (image)
181 {
182 // Create or get frame object
183 auto image_frame = std::make_shared<Frame>(
184 requested_frame, image->size().width(), image->size().height(),
185 background_color, 0, 2);
186
187 // Add Image data to frame
188 image_frame->AddImage(image);
189
190 // return frame object
191 return image_frame;
192 } else {
193 // return empty frame
194 auto image_frame = std::make_shared<Frame>(1, 640, 480, background_color, 0, 2);
195
196 // return frame object
197 return image_frame;
198 }
199
200}
201
202// Generate JSON string of this object
203std::string QtTextReader::Json() const {
204
205 // Return formatted string
206 return JsonValue().toStyledString();
207}
208
209// Generate Json::Value for this object
210Json::Value QtTextReader::JsonValue() const {
211
212 // Create root json object
213 Json::Value root = ReaderBase::JsonValue(); // get parent properties
214 root["type"] = "QtTextReader";
215 root["width"] = width;
216 root["height"] = height;
217 root["x_offset"] = x_offset;
218 root["y_offset"] = y_offset;
219 root["text"] = text;
220 root["font"] = font.toString().toStdString();
221 root["text_color"] = text_color;
222 root["background_color"] = background_color;
223 root["text_background_color"] = text_background_color;
224 root["gravity"] = gravity;
225
226 // return JsonValue
227 return root;
228}
229
230// Load JSON string into this object
231void QtTextReader::SetJson(const std::string value) {
232
233 // Parse JSON string into JSON objects
234 try
235 {
236 const Json::Value root = openshot::stringToJson(value);
237 // Set all values that match
238 SetJsonValue(root);
239 }
240 catch (const std::exception& e)
241 {
242 // Error parsing JSON (or missing keys)
243 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
244 }
245}
246
247// Load Json::Value into this object
248void QtTextReader::SetJsonValue(const Json::Value root) {
249
250 // Set parent data
252
253 // Set data from Json (if key is found)
254 if (!root["width"].isNull())
255 width = root["width"].asInt();
256 if (!root["height"].isNull())
257 height = root["height"].asInt();
258 if (!root["x_offset"].isNull())
259 x_offset = root["x_offset"].asInt();
260 if (!root["y_offset"].isNull())
261 y_offset = root["y_offset"].asInt();
262 if (!root["text"].isNull())
263 text = root["text"].asString();
264 if (!root["font"].isNull())
265 font.fromString(QString::fromStdString(root["font"].asString()));
266 if (!root["text_color"].isNull())
267 text_color = root["text_color"].asString();
268 if (!root["background_color"].isNull())
269 background_color = root["background_color"].asString();
270 if (!root["text_background_color"].isNull())
271 text_background_color = root["text_background_color"].asString();
272 if (!root["gravity"].isNull())
273 gravity = (GravityType) root["gravity"].asInt();
274
275 // Re-Open path, and re-init everything (if needed)
276 if (is_open)
277 {
278 Close();
279 Open();
280 }
281}
Header file for all Exception classes.
Header file for QtTextReader class.
This class represents a fraction.
Definition: Fraction.h:48
int num
Numerator for the fraction.
Definition: Fraction.h:50
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:59
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:84
int den
Denominator for the fraction.
Definition: Fraction.h:51
Exception for invalid JSON.
Definition: Exceptions.h:206
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:111
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:171
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:116
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition: Enums.h:39
@ GRAVITY_TOP_LEFT
Align clip to the top left of its parent.
Definition: Enums.h:40
@ GRAVITY_LEFT
Align clip to the left of its parent (middle aligned)
Definition: Enums.h:43
@ GRAVITY_TOP_RIGHT
Align clip to the top right of its parent.
Definition: Enums.h:42
@ GRAVITY_RIGHT
Align clip to the right of its parent (middle aligned)
Definition: Enums.h:45
@ GRAVITY_BOTTOM_LEFT
Align clip to the bottom left of its parent.
Definition: Enums.h:46
@ GRAVITY_BOTTOM
Align clip to the bottom center of its parent.
Definition: Enums.h:47
@ GRAVITY_TOP
Align clip to the top center of its parent.
Definition: Enums.h:41
@ GRAVITY_BOTTOM_RIGHT
Align clip to the bottom right of its parent.
Definition: Enums.h:48
@ GRAVITY_CENTER
Align clip to the center of its parent (middle aligned)
Definition: Enums.h:44
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
float duration
Length of time (in seconds)
Definition: ReaderBase.h:65
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:68
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:70
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:73
int height
The height of the video (in pixels)
Definition: ReaderBase.h:67
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:75
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:80
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:74
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
Definition: ReaderBase.h:72
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:62
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:63
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:77
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:66