OpenShot Library | libopenshot  0.3.0
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.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;
126  info.video_timebase.num = 1;
127  info.video_timebase.den = 30;
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.
163 std::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
188 std::string QtTextReader::Json() const {
189 
190  // Return formatted string
191  return JsonValue().toStyledString();
192 }
193 
194 // Generate Json::Value for this object
195 Json::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
216 void 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
233 void 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 }
int num
Numerator for the fraction.
Definition: Fraction.h:32
Align clip to the right of its parent (middle aligned)
Definition: Enums.h:28
Header file for QtTextReader class.
Align clip to the bottom right of its parent.
Definition: Enums.h:31
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:46
void SetTextBackgroundColor(std::string color)
float duration
Length of time (in seconds)
Definition: ReaderBase.h:43
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:40
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
Json::Value JsonValue() const override
Generate Json::Value for this object.
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:40
std::string Json() const override
Generate JSON string of this object.
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:44
Align clip to the top right of its parent.
Definition: Enums.h:25
Align clip to the bottom left of its parent.
Definition: Enums.h:29
Header file for CacheBase class.
Header file for all Exception classes.
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:41
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:107
Header file for Frame class.
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:53
int height
The height of the video (in pixels)
Definition: ReaderBase.h:45
Align clip to the bottom center of its parent.
Definition: Enums.h:30
Align clip to the top left of its parent.
Definition: Enums.h:23
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:55
This class represents a fraction.
Definition: Fraction.h:30
Align clip to the left of its parent (middle aligned)
Definition: Enums.h:26
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:162
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:88
Align clip to the center of its parent (middle aligned)
Definition: Enums.h:27
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:52
void Open() override
Open Reader - which is called by the constructor automatically.
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
void Close() override
Close Reader.
Exception for invalid JSON.
Definition: Exceptions.h:217
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::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:50
QtTextReader()
Default constructor (blank text)
void SetJson(const std::string value) override
Load JSON string into this object.
Align clip to the top center of its parent.
Definition: Enums.h:24
int den
Denominator for the fraction.
Definition: Fraction.h:33
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:48
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition: Enums.h:21
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:58