OpenShot Library | libopenshot  0.3.1
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.file_size = 0;
100  info.vcodec = "QImage";
101  info.width = width;
102  info.height = height;
103  info.pixel_ratio.num = 1;
104  info.pixel_ratio.den = 1;
105  info.duration = 60 * 60 * 1; // 1 hour duration
106  info.fps.num = 30;
107  info.fps.den = 1;
108  info.video_timebase.num = 1;
109  info.video_timebase.den = 30;
111 
112  // Calculate the DAR (display aspect ratio)
114 
115  // Reduce size fraction
116  size.Reduce();
117 
118  // Set the ratio based on the reduced fraction
119  info.display_ratio.num = size.num;
120  info.display_ratio.den = size.den;
121 
122  // Mark as "open"
123  is_open = true;
124  }
125 }
126 
127 // Close reader
129 {
130  // Close all objects, if reader is 'open'
131  if (is_open)
132  {
133  // Mark as "closed"
134  is_open = false;
135 
136  // Delete the image
137  image.reset();
138 
139  info.vcodec = "";
140  info.acodec = "";
141  }
142 }
143 
144 // Get an openshot::Frame object for a specific frame number of this reader.
145 std::shared_ptr<Frame> QtHtmlReader::GetFrame(int64_t requested_frame)
146 {
147  if (image)
148  {
149  // Create or get frame object
150  auto image_frame = std::make_shared<Frame>(
151  requested_frame, image->size().width(), image->size().height(),
152  background_color, 0, 2);
153 
154  // Add Image data to frame
155  image_frame->AddImage(image);
156 
157  // return frame object
158  return image_frame;
159  } else {
160  // return empty frame
161  auto image_frame = std::make_shared<Frame>(
162  1, 640, 480, background_color, 0, 2);
163 
164  // return frame object
165  return image_frame;
166  }
167 
168 }
169 
170 // Generate JSON string of this object
171 std::string QtHtmlReader::Json() const {
172 
173  // Return formatted string
174  return JsonValue().toStyledString();
175 }
176 
177 // Generate Json::Value for this object
178 Json::Value QtHtmlReader::JsonValue() const {
179 
180  // Create root json object
181  Json::Value root = ReaderBase::JsonValue(); // get parent properties
182  root["type"] = "QtHtmlReader";
183  root["width"] = width;
184  root["height"] = height;
185  root["x_offset"] = x_offset;
186  root["y_offset"] = y_offset;
187  root["html"] = html;
188  root["css"] = css;
189  root["background_color"] = background_color;
190  root["gravity"] = gravity;
191 
192  // return JsonValue
193  return root;
194 }
195 
196 // Load JSON string into this object
197 void QtHtmlReader::SetJson(const std::string value) {
198 
199  // Parse JSON string into JSON objects
200  try
201  {
202  const Json::Value root = openshot::stringToJson(value);
203  // Set all values that match
204  SetJsonValue(root);
205  }
206  catch (const std::exception& e)
207  {
208  // Error parsing JSON (or missing keys)
209  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
210  }
211 }
212 
213 // Load Json::Value into this object
214 void QtHtmlReader::SetJsonValue(const Json::Value root) {
215 
216  // Set parent data
218 
219  // Set data from Json (if key is found)
220  if (!root["width"].isNull())
221  width = root["width"].asInt();
222  if (!root["height"].isNull())
223  height = root["height"].asInt();
224  if (!root["x_offset"].isNull())
225  x_offset = root["x_offset"].asInt();
226  if (!root["y_offset"].isNull())
227  y_offset = root["y_offset"].asInt();
228  if (!root["html"].isNull())
229  html = root["html"].asString();
230  if (!root["css"].isNull())
231  css = root["css"].asString();
232  if (!root["background_color"].isNull())
233  background_color = root["background_color"].asString();
234  if (!root["gravity"].isNull())
235  gravity = (GravityType) root["gravity"].asInt();
236 
237  // Re-Open path, and re-init everything (if needed)
238  if (is_open)
239  {
240  Close();
241  Open();
242  }
243 }
Header file for all Exception classes.
Header file for Frame class.
Header file for QtHtmlReader 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
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:88
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:162
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:107
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
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 acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:58
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