OpenShot Library | libopenshot  0.2.7
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 "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)
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 
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
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;
127 
128  // Calculate the DAR (display aspect ratio)
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
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.
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
187 std::string QtHtmlReader::Json() const {
188 
189  // Return formatted string
190  return JsonValue().toStyledString();
191 }
192 
193 // Generate Json::Value for this object
194 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
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
230 void QtHtmlReader::SetJsonValue(const Json::Value root) {
231 
232  // Set parent data
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 }
Header file for all Exception classes.
Header file for QtHtmlReader class.
This class represents a fraction.
Definition: Fraction.h:48
int num
Numerator for the fraction.
Definition: Fraction.h:50
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:59
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:84
int den
Denominator for the fraction.
Definition: Fraction.h:51
Exception for invalid JSON.
Definition: Exceptions.h:206
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Json::Value JsonValue() const override
Generate Json::Value for this object.
std::string Json() const override
Generate JSON string of this object.
void Close() override
Close Reader.
QtHtmlReader()
Default constructor (blank text)
void Open() override
Open Reader - which is called by the constructor automatically.
void SetJson(const std::string value) override
Load JSON string into this object.
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
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
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:34
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 acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:80
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