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