OpenShot Library | libopenshot  0.3.3
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 
23 using namespace openshot;
24 
26 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)
27 {
28  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
29  Open();
30  Close();
31 }
32 
33 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)
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 
41 void 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  {
80  case GRAVITY_TOP_LEFT:
81  align_flag = Qt::AlignLeft | Qt::AlignTop;
82  break;
83  case GRAVITY_TOP:
84  align_flag = Qt::AlignHCenter | Qt::AlignTop;
85  break;
86  case GRAVITY_TOP_RIGHT:
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.has_single_image = true;
118  info.file_size = 0;
119  info.vcodec = "QImage";
120  info.width = width;
121  info.height = height;
122  info.pixel_ratio.num = 1;
123  info.pixel_ratio.den = 1;
124  info.duration = 60 * 60 * 1; // 1 hour duration
125  info.fps.num = 30;
126  info.fps.den = 1;
127  info.video_timebase.num = 1;
128  info.video_timebase.den = 30;
130 
131  // Calculate the DAR (display aspect ratio)
133 
134  // Reduce size fraction
135  font_size.Reduce();
136 
137  // Set the ratio based on the reduced fraction
138  info.display_ratio.num = font_size.num;
139  info.display_ratio.den = font_size.den;
140 
141  // Mark as "open"
142  is_open = true;
143  }
144 }
145 
146 // Close reader
148 {
149  // Close all objects, if reader is 'open'
150  if (is_open)
151  {
152  // Mark as "closed"
153  is_open = false;
154 
155  // Delete the image
156  image.reset();
157 
158  info.vcodec = "";
159  info.acodec = "";
160  }
161 }
162 
163 // Get an openshot::Frame object for a specific frame number of this reader.
164 std::shared_ptr<Frame> QtTextReader::GetFrame(int64_t requested_frame)
165 {
166  // Create a scoped lock, allowing only a single thread to run the following code at one time
167  const std::lock_guard<std::recursive_mutex> lock(getFrameMutex);
168 
169  auto sample_count = Frame::GetSamplesPerFrame(requested_frame, info.fps, info.sample_rate, info.channels);
170 
171  if (image)
172  {
173  // Create or get frame object
174  auto image_frame = std::make_shared<Frame>(
175  requested_frame, image->size().width(), image->size().height(),
176  background_color, sample_count, info.channels);
177 
178  // Add Image data to frame
179  image_frame->AddImage(image);
180 
181  // return frame object
182  return image_frame;
183  } else {
184  // return empty frame
185  auto image_frame = std::make_shared<Frame>(1, 640, 480, background_color, sample_count, info.channels);
186 
187  // return frame object
188  return image_frame;
189  }
190 }
191 
192 // Generate JSON string of this object
193 std::string QtTextReader::Json() const {
194 
195  // Return formatted string
196  return JsonValue().toStyledString();
197 }
198 
199 // Generate Json::Value for this object
200 Json::Value QtTextReader::JsonValue() const {
201 
202  // Create root json object
203  Json::Value root = ReaderBase::JsonValue(); // get parent properties
204  root["type"] = "QtTextReader";
205  root["width"] = width;
206  root["height"] = height;
207  root["x_offset"] = x_offset;
208  root["y_offset"] = y_offset;
209  root["text"] = text;
210  root["font"] = font.toString().toStdString();
211  root["text_color"] = text_color;
212  root["background_color"] = background_color;
213  root["text_background_color"] = text_background_color;
214  root["gravity"] = gravity;
215 
216  // return JsonValue
217  return root;
218 }
219 
220 // Load JSON string into this object
221 void QtTextReader::SetJson(const std::string value) {
222 
223  // Parse JSON string into JSON objects
224  try
225  {
226  const Json::Value root = openshot::stringToJson(value);
227  // Set all values that match
228  SetJsonValue(root);
229  }
230  catch (const std::exception& e)
231  {
232  // Error parsing JSON (or missing keys)
233  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
234  }
235 }
236 
237 // Load Json::Value into this object
238 void QtTextReader::SetJsonValue(const Json::Value root) {
239 
240  // Set parent data
242 
243  // Set data from Json (if key is found)
244  if (!root["width"].isNull())
245  width = root["width"].asInt();
246  if (!root["height"].isNull())
247  height = root["height"].asInt();
248  if (!root["x_offset"].isNull())
249  x_offset = root["x_offset"].asInt();
250  if (!root["y_offset"].isNull())
251  y_offset = root["y_offset"].asInt();
252  if (!root["text"].isNull())
253  text = root["text"].asString();
254  if (!root["font"].isNull())
255  font.fromString(QString::fromStdString(root["font"].asString()));
256  if (!root["text_color"].isNull())
257  text_color = root["text_color"].asString();
258  if (!root["background_color"].isNull())
259  background_color = root["background_color"].asString();
260  if (!root["text_background_color"].isNull())
261  text_background_color = root["text_background_color"].asString();
262  if (!root["gravity"].isNull())
263  gravity = (GravityType) root["gravity"].asInt();
264 
265  // Re-Open path, and re-init everything (if needed)
266  if (is_open)
267  {
268  Close();
269  Open();
270  }
271 }
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::ReaderInfo::sample_rate
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:60
openshot::QtTextReader::Json
std::string Json() const override
Generate JSON string of this object.
Definition: QtTextReader.cpp:193
openshot::ReaderBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:107
openshot::ReaderBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:162
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::QtTextReader::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: QtTextReader.cpp:221
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
QtTextReader.h
Header file for QtTextReader class.
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:88
openshot::GRAVITY_TOP_LEFT
@ GRAVITY_TOP_LEFT
Align clip to the top left of its parent.
Definition: Enums.h:23
openshot::GRAVITY_TOP_RIGHT
@ GRAVITY_TOP_RIGHT
Align clip to the top right of its parent.
Definition: Enums.h:25
openshot::GravityType
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition: Enums.h:21
openshot::ReaderInfo::duration
float duration
Length of time (in seconds)
Definition: ReaderBase.h:43
openshot::ReaderInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:40
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:46
openshot::GRAVITY_RIGHT
@ GRAVITY_RIGHT
Align clip to the right of its parent (middle aligned)
Definition: Enums.h:28
openshot::QtTextReader::Close
void Close() override
Close Reader.
Definition: QtTextReader.cpp:147
CacheBase.h
Header file for CacheBase class.
openshot::QtTextReader::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: QtTextReader.cpp:200
openshot::Fraction::ToDouble
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:40
openshot::GRAVITY_TOP
@ GRAVITY_TOP
Align clip to the top center of its parent.
Definition: Enums.h:24
openshot::ReaderInfo::video_length
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:53
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition: ReaderBase.h:45
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:32
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:33
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:65
openshot::GRAVITY_BOTTOM
@ GRAVITY_BOTTOM
Align clip to the bottom center of its parent.
Definition: Enums.h:30
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:41
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::ReaderInfo::file_size
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:44
openshot::QtTextReader::SetTextBackgroundColor
void SetTextBackgroundColor(std::string color)
Definition: QtTextReader.cpp:41
openshot::ReaderInfo::has_single_image
bool has_single_image
Determines if this file only contains a single image.
Definition: ReaderBase.h:42
openshot::ReaderInfo::video_timebase
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:55
openshot::QtTextReader::Open
void Open() override
Open Reader - which is called by the constructor automatically.
Definition: QtTextReader.cpp:50
Frame.h
Header file for Frame class.
openshot::Frame::GetSamplesPerFrame
int GetSamplesPerFrame(openshot::Fraction fps, int sample_rate, int channels)
Calculate the # of samples per video frame (for the current frame number)
Definition: Frame.cpp:484
openshot::GRAVITY_BOTTOM_LEFT
@ GRAVITY_BOTTOM_LEFT
Align clip to the bottom left of its parent.
Definition: Enums.h:29
openshot::ReaderInfo::vcodec
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:52
openshot::GRAVITY_BOTTOM_RIGHT
@ GRAVITY_BOTTOM_RIGHT
Align clip to the bottom right of its parent.
Definition: Enums.h:31
openshot::QtTextReader::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: QtTextReader.cpp:238
openshot::QtTextReader::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Definition: QtTextReader.cpp:164
openshot::GRAVITY_LEFT
@ GRAVITY_LEFT
Align clip to the left of its parent (middle aligned)
Definition: Enums.h:26
openshot::ReaderInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:48
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:50
openshot::GRAVITY_CENTER
@ GRAVITY_CENTER
Align clip to the center of its parent (middle aligned)
Definition: Enums.h:27
openshot::QtTextReader::QtTextReader
QtTextReader()
Default constructor (blank text)
Definition: QtTextReader.cpp:26
openshot::ReaderInfo::acodec
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:58
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:51
openshot::ReaderInfo::channels
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:61
Exceptions.h
Header file for all Exception classes.
openshot::ReaderBase::getFrameMutex
std::recursive_mutex getFrameMutex
Mutex for multiple threads.
Definition: ReaderBase.h:79