OpenShot Library | libopenshot  0.2.5
TextReader.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for TextReader class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 // Require ImageMagick support
32 #ifdef USE_IMAGEMAGICK
33 
34 #include "../include/TextReader.h"
35 
36 using namespace openshot;
37 
38 /// Default constructor (blank text)
39 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) {
40 
41  // Open and Close the reader, to populate its attributes (such as height, width, etc...)
42  Open();
43  Close();
44 }
45 
46 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)
47 : 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)
48 {
49  // Open and Close the reader, to populate its attributes (such as height, width, etc...)
50  Open();
51  Close();
52 }
53 
54 void TextReader::SetTextBackgroundColor(std::string color) {
55  text_background_color = color;
56 
57  // Open and Close the reader, to populate it's attributes (such as height, width, etc...) plus the text background color
58  Open();
59  Close();
60 }
61 
62 // Open reader
64 {
65  // Open reader if not already open
66  if (!is_open)
67  {
68  // create image
69  image = std::shared_ptr<Magick::Image>(new Magick::Image(Magick::Geometry(width,height), Magick::Color(background_color)));
70 
71  // Give image a transparent background color
72  image->backgroundColor(Magick::Color("none"));
73 
74  // Set gravity (map between OpenShot and ImageMagick)
75  switch (gravity)
76  {
77  case GRAVITY_TOP_LEFT:
78  lines.push_back(Magick::DrawableGravity(Magick::NorthWestGravity));
79  break;
80  case GRAVITY_TOP:
81  lines.push_back(Magick::DrawableGravity(Magick::NorthGravity));
82  break;
83  case GRAVITY_TOP_RIGHT:
84  lines.push_back(Magick::DrawableGravity(Magick::NorthEastGravity));
85  break;
86  case GRAVITY_LEFT:
87  lines.push_back(Magick::DrawableGravity(Magick::WestGravity));
88  break;
89  case GRAVITY_CENTER:
90  lines.push_back(Magick::DrawableGravity(Magick::CenterGravity));
91  break;
92  case GRAVITY_RIGHT:
93  lines.push_back(Magick::DrawableGravity(Magick::EastGravity));
94  break;
96  lines.push_back(Magick::DrawableGravity(Magick::SouthWestGravity));
97  break;
98  case GRAVITY_BOTTOM:
99  lines.push_back(Magick::DrawableGravity(Magick::SouthGravity));
100  break;
102  lines.push_back(Magick::DrawableGravity(Magick::SouthEastGravity));
103  break;
104  }
105 
106  // Set stroke properties
107  lines.push_back(Magick::DrawableStrokeColor(Magick::Color("none")));
108  lines.push_back(Magick::DrawableStrokeWidth(0.0));
109  lines.push_back(Magick::DrawableFillColor(text_color));
110  lines.push_back(Magick::DrawableFont(font));
111  lines.push_back(Magick::DrawablePointSize(size));
112  lines.push_back(Magick::DrawableText(x_offset, y_offset, text));
113 
114  if (!text_background_color.empty()) {
115  lines.push_back(Magick::DrawableTextUnderColor(Magick::Color(text_background_color)));
116  }
117 
118  // Draw image
119  image->draw(lines);
120 
121  // Update image properties
122  info.has_audio = false;
123  info.has_video = true;
124  info.file_size = image->fileSize();
125  info.vcodec = image->format();
126  info.width = image->size().width();
127  info.height = image->size().height();
128  info.pixel_ratio.num = 1;
129  info.pixel_ratio.den = 1;
130  info.duration = 60 * 60 * 1; // 1 hour duration
131  info.fps.num = 30;
132  info.fps.den = 1;
133  info.video_timebase.num = 1;
134  info.video_timebase.den = 30;
136 
137  // Calculate the DAR (display aspect ratio)
139 
140  // Reduce size fraction
141  size.Reduce();
142 
143  // Set the ratio based on the reduced fraction
144  info.display_ratio.num = size.num;
145  info.display_ratio.den = size.den;
146 
147  // Mark as "open"
148  is_open = true;
149  }
150 }
151 
152 // Close reader
154 {
155  // Close all objects, if reader is 'open'
156  if (is_open)
157  {
158  // Mark as "closed"
159  is_open = false;
160  }
161 }
162 
163 // Get an openshot::Frame object for a specific frame number of this reader.
164 std::shared_ptr<Frame> TextReader::GetFrame(int64_t requested_frame)
165 {
166  if (image)
167  {
168  // Create or get frame object
169  std::shared_ptr<Frame> image_frame(new Frame(requested_frame, image->size().width(), image->size().height(), "#000000", 0, 2));
170 
171  // Add Image data to frame
172  std::shared_ptr<Magick::Image> copy_image(new Magick::Image(*image.get()));
173  copy_image->modifyImage(); // actually copy the image data to this object
174  //TODO: Reimplement this with QImage
175  image_frame->AddMagickImage(copy_image);
176 
177  // return frame object
178  return image_frame;
179  } else {
180  // return empty frame
181  std::shared_ptr<Frame> image_frame(new Frame(1, 640, 480, "#000000", 0, 2));
182 
183  // return frame object
184  return image_frame;
185  }
186 
187 }
188 
189 // Generate JSON string of this object
190 std::string TextReader::Json() const {
191 
192  // Return formatted string
193  return JsonValue().toStyledString();
194 }
195 
196 // Generate Json::Value for this object
197 Json::Value TextReader::JsonValue() const {
198 
199  // Create root json object
200  Json::Value root = ReaderBase::JsonValue(); // get parent properties
201  root["type"] = "TextReader";
202  root["width"] = width;
203  root["height"] = height;
204  root["x_offset"] = x_offset;
205  root["y_offset"] = y_offset;
206  root["text"] = text;
207  root["font"] = font;
208  root["size"] = size;
209  root["text_color"] = text_color;
210  root["background_color"] = background_color;
211  root["text_background_color"] = text_background_color;
212  root["gravity"] = gravity;
213 
214  // return JsonValue
215  return root;
216 }
217 
218 // Load JSON string into this object
219 void TextReader::SetJson(const std::string value) {
220  try
221  {
222  Json::Value root = openshot::stringToJson(value);
223  // Set all values that match
224  SetJsonValue(root);
225  }
226  catch (const std::exception& e)
227  {
228  // Error parsing JSON (or missing keys)
229  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
230  }
231 }
232 
233 // Load Json::Value into this object
234 void TextReader::SetJsonValue(const Json::Value root) {
235 
236  // Set parent data
238 
239  // Set data from Json (if key is found)
240  if (!root["width"].isNull())
241  width = root["width"].asInt();
242  if (!root["height"].isNull())
243  height = root["height"].asInt();
244  if (!root["x_offset"].isNull())
245  x_offset = root["x_offset"].asInt();
246  if (!root["y_offset"].isNull())
247  y_offset = root["y_offset"].asInt();
248  if (!root["text"].isNull())
249  text = root["text"].asString();
250  if (!root["font"].isNull())
251  font = root["font"].asString();
252  if (!root["size"].isNull())
253  size = root["size"].asDouble();
254  if (!root["text_color"].isNull())
255  text_color = root["text_color"].asString();
256  if (!root["background_color"].isNull())
257  background_color = root["background_color"].asString();
258  if (!root["text_background_color"].isNull())
259  text_background_color = root["text_background_color"].asString();
260  if (!root["gravity"].isNull())
261  gravity = (GravityType) root["gravity"].asInt();
262 
263  // Re-Open path, and re-init everything (if needed)
264  if (is_open)
265  {
266  Close();
267  Open();
268  }
269 }
270 
271 #endif //USE_IMAGEMAGICK
This class represents a fraction.
Definition: Fraction.h:45
int num
Numerator for the fraction.
Definition: Fraction.h:47
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:49
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:74
int den
Denominator for the fraction.
Definition: Fraction.h:48
This class represents a single frame of video (i.e. image & audio data)
Definition: Frame.h:107
Exception for invalid JSON.
Definition: Exceptions.h:206
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
std::string Json() const override
Get and Set JSON methods.
Definition: TextReader.cpp:190
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame)
Definition: TextReader.cpp:164
TextReader()
Default constructor (blank text)
Definition: TextReader.cpp:39
void SetTextBackgroundColor(std::string color)
Definition: TextReader.cpp:54
void Open()
Open Reader - which is called by the constructor automatically.
Definition: TextReader.cpp:63
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: TextReader.cpp:197
void Close()
Close Reader.
Definition: TextReader.cpp:153
void SetJson(const std::string value)
Load JSON string into this object.
Definition: TextReader.cpp:219
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: TextReader.cpp:234
This namespace is the default namespace for all code in the openshot library.
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:33
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 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