OpenShot Library | libopenshot 0.3.3
Loading...
Searching...
No Matches
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
23using namespace openshot;
24
26TextReader::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
33TextReader::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
41void 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 {
66 lines.push_back(Magick::DrawableGravity(Magick::NorthWestGravity));
67 break;
68 case GRAVITY_TOP:
69 lines.push_back(Magick::DrawableGravity(Magick::NorthGravity));
70 break;
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;
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.
152std::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
181std::string TextReader::Json() const {
182
183 // Return formatted string
184 return JsonValue().toStyledString();
185}
186
187// Generate Json::Value for this object
188Json::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
210void 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
225void 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
Header file for all Exception classes.
Header file for Frame class.
Header file for MagickUtilities (IM6/IM7 compatibility overlay)
Header file for QtUtilities (compatibiity overlay)
Header file for TextReader class.
This class represents a fraction.
Definition Fraction.h:30
int num
Numerator for the fraction.
Definition Fraction.h:32
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition Fraction.cpp:40
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition Fraction.cpp:65
int den
Denominator for the fraction.
Definition Fraction.h:33
Exception for invalid JSON.
Definition Exceptions.h:218
openshot::ReaderInfo info
Information about the current media file.
Definition ReaderBase.h:88
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
void Close() override
Close Reader.
std::string Json() const override
Generate JSON string of this object.
TextReader()
Default constructor (blank text)
void Open() override
Open Reader - which is called by the constructor automatically.
void SetTextBackgroundColor(std::string color)
void SetJson(const std::string value) override
Load JSON string into this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Json::Value JsonValue() const override
Generate Json::Value for this object.
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition Enums.h:22
@ GRAVITY_TOP_LEFT
Align clip to the top left of its parent.
Definition Enums.h:23
@ GRAVITY_LEFT
Align clip to the left of its parent (middle aligned)
Definition Enums.h:26
@ GRAVITY_TOP_RIGHT
Align clip to the top right of its parent.
Definition Enums.h:25
@ GRAVITY_RIGHT
Align clip to the right of its parent (middle aligned)
Definition Enums.h:28
@ GRAVITY_BOTTOM_LEFT
Align clip to the bottom left of its parent.
Definition Enums.h:29
@ GRAVITY_BOTTOM
Align clip to the bottom center of its parent.
Definition Enums.h:30
@ GRAVITY_TOP
Align clip to the top center of its parent.
Definition Enums.h:24
@ GRAVITY_BOTTOM_RIGHT
Align clip to the bottom right of its parent.
Definition Enums.h:31
@ GRAVITY_CENTER
Align clip to the center of its parent (middle aligned)
Definition Enums.h:27
std::shared_ptr< QImage > Magick2QImage(std::shared_ptr< Magick::Image >)
Convert Magick::Image to QImage.
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
float duration
Length of time (in seconds)
Definition ReaderBase.h:43
int width
The width of the video (in pixesl)
Definition ReaderBase.h:46
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition ReaderBase.h:48
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
int height
The height of the video (in pixels)
Definition ReaderBase.h:45
int64_t video_length
The number of frames in the video stream.
Definition ReaderBase.h:53
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition ReaderBase.h:52
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
bool has_video
Determines if this file has a video stream.
Definition ReaderBase.h:40
bool has_audio
Determines if this file has an audio stream.
Definition ReaderBase.h:41
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition ReaderBase.h:55
int64_t file_size
Size of file (in bytes)
Definition ReaderBase.h:44