OpenShot Library | libopenshot 0.3.3
Loading...
Searching...
No Matches
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
25using namespace openshot;
26
28QtHtmlReader::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
35QtHtmlReader::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;
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;
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.
146std::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
176std::string QtHtmlReader::Json() const {
177
178 // Return formatted string
179 return JsonValue().toStyledString();
180}
181
182// Generate Json::Value for this object
183Json::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
202void 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
219void 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}
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
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
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.
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
std::recursive_mutex getFrameMutex
Mutex for multiple threads.
Definition ReaderBase.h:79
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
bool has_single_image
Determines if this file only contains a single image.
Definition ReaderBase.h:42
float duration
Length of time (in seconds)
Definition ReaderBase.h:43
int width
The width of the video (in pixesl)
Definition ReaderBase.h:46
int channels
The number of audio channels used in the audio stream.
Definition ReaderBase.h:61
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
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition ReaderBase.h:60
int64_t file_size
Size of file (in bytes)
Definition ReaderBase.h:44