OpenShot Library | libopenshot  0.3.2
TextReader.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 // Require ImageMagick support
14 #ifdef USE_IMAGEMAGICK
15 
16 #include "QtUtilities.h"
17 #include "MagickUtilities.h"
18 
19 #include "TextReader.h"
20 #include "Exceptions.h"
21 #include "Frame.h"
22 
23 using namespace openshot;
24 
26 TextReader::TextReader() : width(1024), height(768), x_offset(0), y_offset(0), text(""), font("Arial"), size(10.0), text_color("#ffffff"), background_color("#000000"), is_open(false), gravity(GRAVITY_CENTER) {
27 
28  // Open and Close the reader, to populate its attributes (such as height, width, etc...)
29  Open();
30  Close();
31 }
32 
33 TextReader::TextReader(int width, int height, int x_offset, int y_offset, GravityType gravity, std::string text, std::string font, double size, 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), size(size), text_color(text_color), background_color(background_color), is_open(false), gravity(gravity)
35 {
36  // Open and Close the reader, to populate its attributes (such as height, width, etc...)
37  Open();
38  Close();
39 }
40 
41 void TextReader::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<Magick::Image>(
57  Magick::Geometry(width,height), Magick::Color(background_color));
58 
59  // Give image a transparent background color
60  image->backgroundColor(Magick::Color("none"));
61 
62  // Set gravity (map between OpenShot and ImageMagick)
63  switch (gravity)
64  {
65  case GRAVITY_TOP_LEFT:
66  lines.push_back(Magick::DrawableGravity(Magick::NorthWestGravity));
67  break;
68  case GRAVITY_TOP:
69  lines.push_back(Magick::DrawableGravity(Magick::NorthGravity));
70  break;
71  case GRAVITY_TOP_RIGHT:
72  lines.push_back(Magick::DrawableGravity(Magick::NorthEastGravity));
73  break;
74  case GRAVITY_LEFT:
75  lines.push_back(Magick::DrawableGravity(Magick::WestGravity));
76  break;
77  case GRAVITY_CENTER:
78  lines.push_back(Magick::DrawableGravity(Magick::CenterGravity));
79  break;
80  case GRAVITY_RIGHT:
81  lines.push_back(Magick::DrawableGravity(Magick::EastGravity));
82  break;
84  lines.push_back(Magick::DrawableGravity(Magick::SouthWestGravity));
85  break;
86  case GRAVITY_BOTTOM:
87  lines.push_back(Magick::DrawableGravity(Magick::SouthGravity));
88  break;
90  lines.push_back(Magick::DrawableGravity(Magick::SouthEastGravity));
91  break;
92  }
93 
94  // Set stroke properties
95  lines.push_back(Magick::DrawableStrokeColor(Magick::Color("none")));
96  lines.push_back(Magick::DrawableStrokeWidth(0.0));
97  lines.push_back(Magick::DrawableFillColor(text_color));
98  lines.push_back(Magick::DrawableFont(font));
99  lines.push_back(Magick::DrawablePointSize(size));
100  lines.push_back(Magick::DrawableText(x_offset, y_offset, text));
101 
102  if (!text_background_color.empty()) {
103  lines.push_back(Magick::DrawableTextUnderColor(Magick::Color(text_background_color)));
104  }
105 
106  // Draw image
107  image->draw(lines);
108 
109  // Update image properties
110  info.has_audio = false;
111  info.has_video = true;
112  info.file_size = image->fileSize();
113  info.vcodec = image->format();
114  info.width = image->size().width();
115  info.height = image->size().height();
116  info.pixel_ratio.num = 1;
117  info.pixel_ratio.den = 1;
118  info.duration = 60 * 60 * 1; // 1 hour duration
119  info.fps.num = 30;
120  info.fps.den = 1;
121  info.video_timebase.num = 1;
122  info.video_timebase.den = 30;
124 
125  // Calculate the DAR (display aspect ratio)
127 
128  // Reduce size fraction
129  size.Reduce();
130 
131  // Set the ratio based on the reduced fraction
132  info.display_ratio.num = size.num;
133  info.display_ratio.den = size.den;
134 
135  // Mark as "open"
136  is_open = true;
137  }
138 }
139 
140 // Close reader
142 {
143  // Close all objects, if reader is 'open'
144  if (is_open)
145  {
146  // Mark as "closed"
147  is_open = false;
148  }
149 }
150 
151 // Get an openshot::Frame object for a specific frame number of this reader.
152 std::shared_ptr<Frame> TextReader::GetFrame(int64_t requested_frame)
153 {
154  if (image)
155  {
156  // Create or get frame object
157  auto image_frame = std::make_shared<Frame>(
158  requested_frame,
159  image->size().width(), image->size().height(),
160  "#000000", 0, 2);
161 
162  // Add Image data to frame
163  auto copy_image = std::make_shared<Magick::Image>(*image.get());
164  copy_image->modifyImage(); // actually copy the image data to this object
165  auto qimage = openshot::Magick2QImage(copy_image);
166  image_frame->AddImage(qimage);
167 
168  // return frame object
169  return image_frame;
170  } else {
171  // return empty frame
172  auto image_frame = std::make_shared<Frame>(1, 640, 480, "#000000", 0, 2);
173 
174  // return frame object
175  return image_frame;
176  }
177 
178 }
179 
180 // Generate JSON string of this object
181 std::string TextReader::Json() const {
182 
183  // Return formatted string
184  return JsonValue().toStyledString();
185 }
186 
187 // Generate Json::Value for this object
188 Json::Value TextReader::JsonValue() const {
189 
190  // Create root json object
191  Json::Value root = ReaderBase::JsonValue(); // get parent properties
192  root["type"] = "TextReader";
193  root["width"] = width;
194  root["height"] = height;
195  root["x_offset"] = x_offset;
196  root["y_offset"] = y_offset;
197  root["text"] = text;
198  root["font"] = font;
199  root["size"] = size;
200  root["text_color"] = text_color;
201  root["background_color"] = background_color;
202  root["text_background_color"] = text_background_color;
203  root["gravity"] = gravity;
204 
205  // return JsonValue
206  return root;
207 }
208 
209 // Load JSON string into this object
210 void TextReader::SetJson(const std::string value) {
211  try
212  {
213  Json::Value root = openshot::stringToJson(value);
214  // Set all values that match
215  SetJsonValue(root);
216  }
217  catch (const std::exception& e)
218  {
219  // Error parsing JSON (or missing keys)
220  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
221  }
222 }
223 
224 // Load Json::Value into this object
225 void TextReader::SetJsonValue(const Json::Value root) {
226 
227  // Set parent data
229 
230  // Set data from Json (if key is found)
231  if (!root["width"].isNull())
232  width = root["width"].asInt();
233  if (!root["height"].isNull())
234  height = root["height"].asInt();
235  if (!root["x_offset"].isNull())
236  x_offset = root["x_offset"].asInt();
237  if (!root["y_offset"].isNull())
238  y_offset = root["y_offset"].asInt();
239  if (!root["text"].isNull())
240  text = root["text"].asString();
241  if (!root["font"].isNull())
242  font = root["font"].asString();
243  if (!root["size"].isNull())
244  size = root["size"].asDouble();
245  if (!root["text_color"].isNull())
246  text_color = root["text_color"].asString();
247  if (!root["background_color"].isNull())
248  background_color = root["background_color"].asString();
249  if (!root["text_background_color"].isNull())
250  text_background_color = root["text_background_color"].asString();
251  if (!root["gravity"].isNull())
252  gravity = (GravityType) root["gravity"].asInt();
253 
254  // Re-Open path, and re-init everything (if needed)
255  if (is_open)
256  {
257  Close();
258  Open();
259  }
260 }
261 
262 #endif //USE_IMAGEMAGICK
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
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
TextReader.h
Header file for TextReader class.
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
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::TextReader::Close
void Close() override
Close Reader.
Definition: TextReader.cpp:141
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
QtUtilities.h
Header file for QtUtilities (compatibiity overlay)
openshot::TextReader::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: TextReader.cpp:188
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::TextReader::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: TextReader.cpp:210
openshot::GRAVITY_RIGHT
@ GRAVITY_RIGHT
Align clip to the right of its parent (middle aligned)
Definition: Enums.h:28
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::TextReader::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Definition: TextReader.cpp:152
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::Magick2QImage
std::shared_ptr< QImage > Magick2QImage(std::shared_ptr< Magick::Image >)
Convert Magick::Image to QImage.
Definition: MagickUtilities.cpp:46
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::TextReader::Json
std::string Json() const override
Generate JSON string of this object.
Definition: TextReader.cpp:181
Frame.h
Header file for Frame class.
openshot::TextReader::SetTextBackgroundColor
void SetTextBackgroundColor(std::string color)
Definition: TextReader.cpp:41
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::TextReader::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: TextReader.cpp:225
openshot::TextReader::Open
void Open() override
Open Reader - which is called by the constructor automatically.
Definition: TextReader.cpp:50
MagickUtilities.h
Header file for MagickUtilities (IM6/IM7 compatibility overlay)
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::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
Exceptions.h
Header file for all Exception classes.
openshot::TextReader::TextReader
TextReader()
Default constructor (blank text)
Definition: TextReader.cpp:26