1 /**
2 * @file
3 * @brief Source file for QtHtmlReader class
4 * @author Jonathan Thomas <jonathan@openshot.org>
5 * @author Sergei Kolesov (jediserg)
6 * @author Jeff Shillitto (jeffski)
7 *
8 * @ref License
9 */
10
11 /* LICENSE
12 *
13 * Copyright (c) 2008-2019 OpenShot Studios, LLC
14 * <http://www.openshotstudios.com/>. This file is part of
15 * OpenShot Library (libopenshot), an open-source project dedicated to
16 * delivering high quality video editing and animation solutions to the
17 * world. For more information visit <http://www.openshot.org/>.
18 *
19 * OpenShot Library (libopenshot) is free software: you can redistribute it
20 * and/or modify it under the terms of the GNU Lesser General Public License
21 * as published by the Free Software Foundation, either version 3 of the
22 * License, or (at your option) any later version.
23 *
24 * OpenShot Library (libopenshot) is distributed in the hope that it will be
25 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU Lesser General Public License for more details.
28 *
29 * You should have received a copy of the GNU Lesser General Public License
30 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
31 */
32
33 #include "QtHtmlReader.h"
34 #include "Exceptions.h"
35 #include <QImage>
36 #include <QPainter>
37 #include <QTextDocument>
38 #include <QGuiApplication>
39 #include <QAbstractTextDocumentLayout>
40
41 using namespace openshot;
42
43 /// Default constructor (blank text)
QtHtmlReader()44 QtHtmlReader::QtHtmlReader() : width(1024), height(768), x_offset(0), y_offset(0), html(""), css(""), background_color("#000000"), is_open(false), gravity(GRAVITY_CENTER)
45 {
46 // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
47 Open();
48 Close();
49 }
50
QtHtmlReader(int width,int height,int x_offset,int y_offset,GravityType gravity,std::string html,std::string css,std::string background_color)51 QtHtmlReader::QtHtmlReader(int width, int height, int x_offset, int y_offset, GravityType gravity, std::string html, std::string css, std::string background_color)
52 : width(width), height(height), x_offset(x_offset), y_offset(y_offset), gravity(gravity), html(html), css(css), background_color(background_color), is_open(false)
53 {
54 // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
55 Open();
56 Close();
57 }
58
59 // Open reader
Open()60 void QtHtmlReader::Open()
61 {
62 // Open reader if not already open
63 if (!is_open)
64 {
65 // create image
66 image = std::make_shared<QImage>(width, height, QImage::Format_RGBA8888_Premultiplied);
67 image->fill(QColor(background_color.c_str()));
68
69 //start painting
70 QPainter painter;
71 if (!painter.begin(image.get())) {
72 return;
73 }
74
75 //set background
76 painter.setBackground(QBrush(background_color.c_str()));
77
78 //draw text
79 QTextDocument text_document;
80
81 //disable redo/undo stack as not needed
82 text_document.setUndoRedoEnabled(false);
83
84 //create the HTML/CSS document
85 text_document.setTextWidth(width);
86 text_document.setDefaultStyleSheet(css.c_str());
87 text_document.setHtml(html.c_str());
88
89 int td_height = text_document.documentLayout()->documentSize().height();
90
91 if (gravity == GRAVITY_TOP_LEFT || gravity == GRAVITY_TOP || gravity == GRAVITY_TOP_RIGHT) {
92 painter.translate(x_offset, y_offset);
93 } else if (gravity == GRAVITY_LEFT || gravity == GRAVITY_CENTER || gravity == GRAVITY_RIGHT) {
94 painter.translate(x_offset, (height - td_height) / 2 + y_offset);
95 } else if (gravity == GRAVITY_BOTTOM_LEFT || gravity == GRAVITY_BOTTOM_RIGHT || gravity == GRAVITY_BOTTOM) {
96 painter.translate(x_offset, height - td_height + y_offset);
97 }
98
99 if (gravity == GRAVITY_TOP_LEFT || gravity == GRAVITY_LEFT || gravity == GRAVITY_BOTTOM_LEFT) {
100 text_document.setDefaultTextOption(QTextOption(Qt::AlignLeft));
101 } else if (gravity == GRAVITY_CENTER || gravity == GRAVITY_TOP || gravity == GRAVITY_BOTTOM) {
102 text_document.setDefaultTextOption(QTextOption(Qt::AlignHCenter));
103 } else if (gravity == GRAVITY_TOP_RIGHT || gravity == GRAVITY_RIGHT|| gravity == GRAVITY_BOTTOM_RIGHT) {
104 text_document.setDefaultTextOption(QTextOption(Qt::AlignRight));
105 }
106
107 // Draw image
108 text_document.drawContents(&painter);
109
110 painter.end();
111
112 // Update image properties
113 info.has_audio = false;
114 info.has_video = true;
115 info.file_size = 0;
116 info.vcodec = "QImage";
117 info.width = width;
118 info.height = height;
119 info.pixel_ratio.num = 1;
120 info.pixel_ratio.den = 1;
121 info.duration = 60 * 60 * 1; // 1 hour duration
122 info.fps.num = 30;
123 info.fps.den = 1;
124 info.video_timebase.num = 1;
125 info.video_timebase.den = 30;
126 info.video_length = round(info.duration * info.fps.ToDouble());
127
128 // Calculate the DAR (display aspect ratio)
129 Fraction size(info.width * info.pixel_ratio.num, info.height * info.pixel_ratio.den);
130
131 // Reduce size fraction
132 size.Reduce();
133
134 // Set the ratio based on the reduced fraction
135 info.display_ratio.num = size.num;
136 info.display_ratio.den = size.den;
137
138 // Mark as "open"
139 is_open = true;
140 }
141 }
142
143 // Close reader
Close()144 void QtHtmlReader::Close()
145 {
146 // Close all objects, if reader is 'open'
147 if (is_open)
148 {
149 // Mark as "closed"
150 is_open = false;
151
152 // Delete the image
153 image.reset();
154
155 info.vcodec = "";
156 info.acodec = "";
157 }
158 }
159
160 // Get an openshot::Frame object for a specific frame number of this reader.
GetFrame(int64_t requested_frame)161 std::shared_ptr<Frame> QtHtmlReader::GetFrame(int64_t requested_frame)
162 {
163 if (image)
164 {
165 // Create or get frame object
166 auto image_frame = std::make_shared<Frame>(
167 requested_frame, image->size().width(), image->size().height(),
168 background_color, 0, 2);
169
170 // Add Image data to frame
171 image_frame->AddImage(image);
172
173 // return frame object
174 return image_frame;
175 } else {
176 // return empty frame
177 auto image_frame = std::make_shared<Frame>(
178 1, 640, 480, background_color, 0, 2);
179
180 // return frame object
181 return image_frame;
182 }
183
184 }
185
186 // Generate JSON string of this object
Json() const187 std::string QtHtmlReader::Json() const {
188
189 // Return formatted string
190 return JsonValue().toStyledString();
191 }
192
193 // Generate Json::Value for this object
JsonValue() const194 Json::Value QtHtmlReader::JsonValue() const {
195
196 // Create root json object
197 Json::Value root = ReaderBase::JsonValue(); // get parent properties
198 root["type"] = "QtHtmlReader";
199 root["width"] = width;
200 root["height"] = height;
201 root["x_offset"] = x_offset;
202 root["y_offset"] = y_offset;
203 root["html"] = html;
204 root["css"] = css;
205 root["background_color"] = background_color;
206 root["gravity"] = gravity;
207
208 // return JsonValue
209 return root;
210 }
211
212 // Load JSON string into this object
SetJson(const std::string value)213 void QtHtmlReader::SetJson(const std::string value) {
214
215 // Parse JSON string into JSON objects
216 try
217 {
218 const Json::Value root = openshot::stringToJson(value);
219 // Set all values that match
220 SetJsonValue(root);
221 }
222 catch (const std::exception& e)
223 {
224 // Error parsing JSON (or missing keys)
225 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
226 }
227 }
228
229 // Load Json::Value into this object
SetJsonValue(const Json::Value root)230 void QtHtmlReader::SetJsonValue(const Json::Value root) {
231
232 // Set parent data
233 ReaderBase::SetJsonValue(root);
234
235 // Set data from Json (if key is found)
236 if (!root["width"].isNull())
237 width = root["width"].asInt();
238 if (!root["height"].isNull())
239 height = root["height"].asInt();
240 if (!root["x_offset"].isNull())
241 x_offset = root["x_offset"].asInt();
242 if (!root["y_offset"].isNull())
243 y_offset = root["y_offset"].asInt();
244 if (!root["html"].isNull())
245 html = root["html"].asString();
246 if (!root["css"].isNull())
247 css = root["css"].asString();
248 if (!root["background_color"].isNull())
249 background_color = root["background_color"].asString();
250 if (!root["gravity"].isNull())
251 gravity = (GravityType) root["gravity"].asInt();
252
253 // Re-Open path, and re-init everything (if needed)
254 if (is_open)
255 {
256 Close();
257 Open();
258 }
259 }
260