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