OpenShot Library | libopenshot  0.2.5
QtHtmlReader.cpp
Go to the documentation of this file.
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 "../include/QtHtmlReader.h"
34 #include <QImage>
35 #include <QPainter>
36 #include <QTextDocument>
37 #include <QGuiApplication>
38 #include <QAbstractTextDocumentLayout>
39 
40 using namespace openshot;
41 
42 /// Default constructor (blank text)
43 QtHtmlReader::QtHtmlReader() : width(1024), height(768), x_offset(0), y_offset(0), html(""), css(""), background_color("#000000"), is_open(false), gravity(GRAVITY_CENTER)
44 {
45  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
46  Open();
47  Close();
48 }
49 
50 QtHtmlReader::QtHtmlReader(int width, int height, int x_offset, int y_offset, GravityType gravity, std::string html, std::string css, std::string background_color)
51 : 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)
52 {
53  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
54  Open();
55  Close();
56 }
57 
58 // Open reader
60 {
61  // Open reader if not already open
62  if (!is_open)
63  {
64  // create image
65  image = std::shared_ptr<QImage>(new QImage(width, height, QImage::Format_RGBA8888));
66  image->fill(QColor(background_color.c_str()));
67 
68  //start painting
69  QPainter painter;
70  if (!painter.begin(image.get())) {
71  return;
72  }
73 
74  //set background
75  painter.setBackground(QBrush(background_color.c_str()));
76 
77  //draw text
78  QTextDocument text_document;
79 
80  //disable redo/undo stack as not needed
81  text_document.setUndoRedoEnabled(false);
82 
83  //create the HTML/CSS document
84  text_document.setTextWidth(width);
85  text_document.setDefaultStyleSheet(css.c_str());
86  text_document.setHtml(html.c_str());
87 
88  int td_height = text_document.documentLayout()->documentSize().height();
89 
90  if (gravity == GRAVITY_TOP_LEFT || gravity == GRAVITY_TOP || gravity == GRAVITY_TOP_RIGHT) {
91  painter.translate(x_offset, y_offset);
92  } else if (gravity == GRAVITY_LEFT || gravity == GRAVITY_CENTER || gravity == GRAVITY_RIGHT) {
93  painter.translate(x_offset, (height - td_height) / 2 + y_offset);
94  } else if (gravity == GRAVITY_BOTTOM_LEFT || gravity == GRAVITY_BOTTOM_RIGHT || gravity == GRAVITY_BOTTOM) {
95  painter.translate(x_offset, height - td_height + y_offset);
96  }
97 
98  if (gravity == GRAVITY_TOP_LEFT || gravity == GRAVITY_LEFT || gravity == GRAVITY_BOTTOM_LEFT) {
99  text_document.setDefaultTextOption(QTextOption(Qt::AlignLeft));
100  } else if (gravity == GRAVITY_CENTER || gravity == GRAVITY_TOP || gravity == GRAVITY_BOTTOM) {
101  text_document.setDefaultTextOption(QTextOption(Qt::AlignHCenter));
102  } else if (gravity == GRAVITY_TOP_RIGHT || gravity == GRAVITY_RIGHT|| gravity == GRAVITY_BOTTOM_RIGHT) {
103  text_document.setDefaultTextOption(QTextOption(Qt::AlignRight));
104  }
105 
106  // Draw image
107  text_document.drawContents(&painter);
108 
109  painter.end();
110 
111  // Update image properties
112  info.has_audio = false;
113  info.has_video = true;
114  info.file_size = 0;
115  info.vcodec = "QImage";
116  info.width = width;
117  info.height = height;
118  info.pixel_ratio.num = 1;
119  info.pixel_ratio.den = 1;
120  info.duration = 60 * 60 * 1; // 1 hour duration
121  info.fps.num = 30;
122  info.fps.den = 1;
123  info.video_timebase.num = 1;
124  info.video_timebase.den = 30;
126 
127  // Calculate the DAR (display aspect ratio)
129 
130  // Reduce size fraction
131  size.Reduce();
132 
133  // Set the ratio based on the reduced fraction
134  info.display_ratio.num = size.num;
135  info.display_ratio.den = size.den;
136 
137  // Mark as "open"
138  is_open = true;
139  }
140 }
141 
142 // Close reader
144 {
145  // Close all objects, if reader is 'open'
146  if (is_open)
147  {
148  // Mark as "closed"
149  is_open = false;
150 
151  // Delete the image
152  image.reset();
153 
154  info.vcodec = "";
155  info.acodec = "";
156  }
157 }
158 
159 // Get an openshot::Frame object for a specific frame number of this reader.
160 std::shared_ptr<Frame> QtHtmlReader::GetFrame(int64_t requested_frame)
161 {
162  if (image)
163  {
164  // Create or get frame object
165  std::shared_ptr<Frame> image_frame(new Frame(requested_frame, image->size().width(), image->size().height(), background_color, 0, 2));
166 
167  // Add Image data to frame
168  image_frame->AddImage(image);
169 
170  // return frame object
171  return image_frame;
172  } else {
173  // return empty frame
174  std::shared_ptr<Frame> image_frame(new Frame(1, 640, 480, background_color, 0, 2));
175 
176  // return frame object
177  return image_frame;
178  }
179 
180 }
181 
182 // Generate JSON string of this object
183 std::string QtHtmlReader::Json() const {
184 
185  // Return formatted string
186  return JsonValue().toStyledString();
187 }
188 
189 // Generate Json::Value for this object
190 Json::Value QtHtmlReader::JsonValue() const {
191 
192  // Create root json object
193  Json::Value root = ReaderBase::JsonValue(); // get parent properties
194  root["type"] = "QtHtmlReader";
195  root["width"] = width;
196  root["height"] = height;
197  root["x_offset"] = x_offset;
198  root["y_offset"] = y_offset;
199  root["html"] = html;
200  root["css"] = css;
201  root["background_color"] = background_color;
202  root["gravity"] = gravity;
203 
204  // return JsonValue
205  return root;
206 }
207 
208 // Load JSON string into this object
209 void QtHtmlReader::SetJson(const std::string value) {
210 
211  // Parse JSON string into JSON objects
212  try
213  {
214  const Json::Value root = openshot::stringToJson(value);
215  // Set all values that match
216  SetJsonValue(root);
217  }
218  catch (const std::exception& e)
219  {
220  // Error parsing JSON (or missing keys)
221  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
222  }
223 }
224 
225 // Load Json::Value into this object
226 void QtHtmlReader::SetJsonValue(const Json::Value root) {
227 
228  // Set parent data
230 
231  // Set data from Json (if key is found)
232  if (!root["width"].isNull())
233  width = root["width"].asInt();
234  if (!root["height"].isNull())
235  height = root["height"].asInt();
236  if (!root["x_offset"].isNull())
237  x_offset = root["x_offset"].asInt();
238  if (!root["y_offset"].isNull())
239  y_offset = root["y_offset"].asInt();
240  if (!root["html"].isNull())
241  html = root["html"].asString();
242  if (!root["css"].isNull())
243  css = root["css"].asString();
244  if (!root["background_color"].isNull())
245  background_color = root["background_color"].asString();
246  if (!root["gravity"].isNull())
247  gravity = (GravityType) root["gravity"].asInt();
248 
249  // Re-Open path, and re-init everything (if needed)
250  if (is_open)
251  {
252  Close();
253  Open();
254  }
255 }
int num
Numerator for the fraction.
Definition: Fraction.h:47
Align clip to the right of its parent (middle aligned)
Definition: Enums.h:45
Json::Value JsonValue() const override
Generate Json::Value for this object.
Align clip to the bottom right of its parent.
Definition: Enums.h:48
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:68
This class represents a single frame of video (i.e. image & audio data)
Definition: Frame.h:106
float duration
Length of time (in seconds)
Definition: ReaderBase.h:65
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:62
std::string Json() const override
Get and Set JSON methods.
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:66
Align clip to the top right of its parent.
Definition: Enums.h:42
Align clip to the bottom left of its parent.
Definition: Enums.h:46
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:63
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:116
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:75
int height
The height of the video (in pixels)
Definition: ReaderBase.h:67
Align clip to the bottom center of its parent.
Definition: Enums.h:47
Align clip to the top left of its parent.
Definition: Enums.h:40
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:77
This class represents a fraction.
Definition: Fraction.h:45
Align clip to the left of its parent (middle aligned)
Definition: Enums.h:43
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:171
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
QtHtmlReader()
Default constructor (blank text)
Align clip to the center of its parent (middle aligned)
Definition: Enums.h:44
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:74
void Close()
Close Reader.
This namespace is the default namespace for all code in the openshot library.
Exception for invalid JSON.
Definition: Exceptions.h:205
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
void SetJson(const std::string value)
Load JSON string into this object.
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
void Open()
Open Reader - which is called by the constructor automatically.
Align clip to the top center of its parent.
Definition: Enums.h:41
int den
Denominator for the fraction.
Definition: Fraction.h:48
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:70
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition: Enums.h:38
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:80
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:49
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame)