Skip to content

Commit 76725d9

Browse files
authored
Merge pull request #305 from jeffski/qt-text-html-readers
QtTextReader and QtHtmlReader
2 parents 2a1fe80 + 6c20fa4 commit 76725d9

File tree

11 files changed

+1075
-2
lines changed

11 files changed

+1075
-2
lines changed

include/OpenShot.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@
135135
#include "PlayerBase.h"
136136
#include "Point.h"
137137
#include "Profiles.h"
138+
#include "QtHtmlReader.h"
138139
#include "QtImageReader.h"
140+
#include "QtTextReader.h"
139141
#include "Timeline.h"
140142
#include "Settings.h"
141143

include/QtHtmlReader.h

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* @file
3+
* @brief Header file for QtHtmlReader class
4+
* @author Jonathan Thomas <[email protected]>
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+
#ifndef OPENSHOT_QT_HTML_READER_H
34+
#define OPENSHOT_QT_HTML_READER_H
35+
36+
#include "ReaderBase.h"
37+
38+
#include <cmath>
39+
#include <ctime>
40+
#include <iostream>
41+
#include <omp.h>
42+
#include <stdio.h>
43+
#include <memory>
44+
#include "CacheMemory.h"
45+
#include "Enums.h"
46+
#include "Exceptions.h"
47+
48+
class QImage;
49+
50+
namespace openshot
51+
{
52+
53+
/**
54+
* @brief This class uses Qt libraries, to create frames with rendered HTML, and return
55+
* openshot::Frame objects.
56+
*
57+
* Supports HTML/CSS subset available via Qt libraries, see: https://doc.qt.io/qt-5/richtext-html-subset.html
58+
*
59+
* @code
60+
* // Any application using this class must instantiate either QGuiApplication or QApplication
61+
* QApplication a(argc, argv);
62+
*
63+
* // Create a reader to generate an openshot::Frame containing text
64+
* QtHtmlReader r(720, // width
65+
* 480, // height
66+
* 5, // x_offset
67+
* 5, // y_offset
68+
* GRAVITY_CENTER, // gravity
69+
* "<b>Check out</b> this Text!", // html
70+
* "b { color: #ff0000 }", // css
71+
* "#000000" // background_color
72+
* );
73+
* r.Open(); // Open the reader
74+
*
75+
* // Get frame number 1 from the video (in fact, any frame # you request will return the same frame)
76+
* std::shared_ptr<Frame> f = r.GetFrame(1);
77+
*
78+
* // Now that we have an openshot::Frame object, lets have some fun!
79+
* f->Display(); // Display the frame on the screen
80+
*
81+
* // Close the reader
82+
* r.Close();
83+
* @endcode
84+
*/
85+
class QtHtmlReader : public ReaderBase
86+
{
87+
private:
88+
int width;
89+
int height;
90+
int x_offset;
91+
int y_offset;
92+
std::string html;
93+
std::string css;
94+
std::string background_color;
95+
std::shared_ptr<QImage> image;
96+
bool is_open;
97+
openshot::GravityType gravity;
98+
public:
99+
100+
/// Default constructor (blank text)
101+
QtHtmlReader();
102+
103+
/// @brief Constructor for QtHtmlReader with all parameters.
104+
/// @param width The width of the requested openshot::Frame (not the size of the text)
105+
/// @param height The height of the requested openshot::Frame (not the size of the text)
106+
/// @param x_offset The number of pixels to offset the text on the X axis (horizontal)
107+
/// @param y_offset The number of pixels to offset the text on the Y axis (vertical)
108+
/// @param gravity The alignment / gravity of the text
109+
/// @param html The HTML you want to render / display
110+
/// @param css The CSS you want to apply to style the HTML
111+
/// @param background_color The background color of the frame image (valid values are a color string in #RRGGBB or #AARRGGBB notation, a CSS color name, or 'transparent')
112+
QtHtmlReader(int width, int height, int x_offset, int y_offset, GravityType gravity, std::string html, std::string css, std::string background_color);
113+
114+
/// Close Reader
115+
void Close();
116+
117+
/// Get the cache object used by this reader (always returns NULL for this object)
118+
openshot::CacheMemory* GetCache() { return NULL; };
119+
120+
/// Get an openshot::Frame object for a specific frame number of this reader. All numbers
121+
/// return the same Frame, since they all share the same image data.
122+
///
123+
/// @returns The requested frame (containing the image)
124+
/// @param requested_frame The frame number that is requested.
125+
std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame);
126+
127+
/// Determine if reader is open or closed
128+
bool IsOpen() { return is_open; };
129+
130+
/// Return the type name of the class
131+
std::string Name() { return "QtHtmlReader"; };
132+
133+
/// Get and Set JSON methods
134+
std::string Json(); ///< Generate JSON string of this object
135+
void SetJson(std::string value); ///< Load JSON string into this object
136+
Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
137+
void SetJsonValue(Json::Value root); ///< Load Json::JsonValue into this object
138+
139+
/// Open Reader - which is called by the constructor automatically
140+
void Open();
141+
};
142+
143+
}
144+
145+
#endif

include/QtTextReader.h

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/**
2+
* @file
3+
* @brief Header file for QtTextReader class
4+
* @author Jonathan Thomas <[email protected]>
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+
#ifndef OPENSHOT_QT_TEXT_READER_H
34+
#define OPENSHOT_QT_TEXT_READER_H
35+
36+
#include "ReaderBase.h"
37+
38+
#include <cmath>
39+
#include <ctime>
40+
#include <iostream>
41+
#include <omp.h>
42+
#include <stdio.h>
43+
#include <memory>
44+
#include "CacheMemory.h"
45+
#include "Enums.h"
46+
#include "Exceptions.h"
47+
48+
class QImage;
49+
50+
namespace openshot
51+
{
52+
53+
/**
54+
* @brief This class uses Qt libraries, to create frames with "Text", and return
55+
* openshot::Frame objects.
56+
*
57+
* All system fonts are supported, including many different font properties, such as size, color,
58+
* alignment, padding, etc...
59+
*
60+
* @code
61+
* // Any application using this class must instantiate either QGuiApplication or QApplication
62+
* QApplication a(argc, argv);
63+
*
64+
* // Create a reader to generate an openshot::Frame containing text
65+
* QtTextReader r(720, // width
66+
* 480, // height
67+
* 5, // x_offset
68+
* 5, // y_offset
69+
* GRAVITY_CENTER, // gravity
70+
* "Check out this Text!", // text
71+
* "Arial", // font
72+
* 15.0, // font size
73+
* "#fff000", // text_color
74+
* "#000000" // background_color
75+
* );
76+
* r.Open(); // Open the reader
77+
*
78+
* // Get frame number 1 from the video (in fact, any frame # you request will return the same frame)
79+
* std::shared_ptr<Frame> f = r.GetFrame(1);
80+
*
81+
* // Now that we have an openshot::Frame object, lets have some fun!
82+
* f->Display(); // Display the frame on the screen
83+
*
84+
* // Close the reader
85+
* r.Close();
86+
* @endcode
87+
*/
88+
class QtTextReader : public ReaderBase
89+
{
90+
private:
91+
int width;
92+
int height;
93+
int x_offset;
94+
int y_offset;
95+
std::string text;
96+
QFont font;
97+
std::string text_color;
98+
std::string background_color;
99+
std::string text_background_color;
100+
std::shared_ptr<QImage> image;
101+
bool is_open;
102+
openshot::GravityType gravity;
103+
104+
public:
105+
106+
/// Default constructor (blank text)
107+
QtTextReader();
108+
109+
/// @brief Constructor for QtTextReader with all parameters.
110+
/// @param width The width of the requested openshot::Frame (not the size of the text)
111+
/// @param height The height of the requested openshot::Frame (not the size of the text)
112+
/// @param x_offset The number of pixels to offset the text on the X axis (horizontal)
113+
/// @param y_offset The number of pixels to offset the text on the Y axis (vertical)
114+
/// @param gravity The alignment / gravity of the text
115+
/// @param text The text you want to generate / display
116+
/// @param font The font of the text
117+
/// @param font_size The size of the text
118+
/// @param is_bold Set to true to make text bold
119+
/// @param is_italic Set to true to make text italic
120+
/// @param text_color The color of the text (valid values are a color string in #RRGGBB or #AARRGGBB notation or a CSS color name)
121+
/// @param background_color The background color of the frame image (valid values are a color string in #RRGGBB or #AARRGGBB notation, a CSS color name, or 'transparent')
122+
QtTextReader(int width, int height, int x_offset, int y_offset, GravityType gravity, std::string text, QFont font, std::string text_color, std::string background_color);
123+
124+
/// Draw a box under rendered text using the specified color.
125+
/// @param color The background color behind the text (valid values are a color string in #RRGGBB or #AARRGGBB notation or a CSS color name)
126+
void SetTextBackgroundColor(std::string color);
127+
128+
/// Close Reader
129+
void Close();
130+
131+
/// Get the cache object used by this reader (always returns NULL for this object)
132+
openshot::CacheMemory* GetCache() { return NULL; };
133+
134+
/// Get an openshot::Frame object for a specific frame number of this reader. All numbers
135+
/// return the same Frame, since they all share the same image data.
136+
///
137+
/// @returns The requested frame (containing the image)
138+
/// @param requested_frame The frame number that is requested.
139+
std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame);
140+
141+
/// Determine if reader is open or closed
142+
bool IsOpen() { return is_open; };
143+
144+
/// Return the type name of the class
145+
std::string Name() { return "QtTextReader"; };
146+
147+
/// Get and Set JSON methods
148+
std::string Json(); ///< Generate JSON string of this object
149+
void SetJson(string value); ///< Load JSON string into this object
150+
Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
151+
void SetJsonValue(Json::Value root); ///< Load Json::JsonValue into this object
152+
153+
/// Open Reader - which is called by the constructor automatically
154+
void Open();
155+
};
156+
157+
}
158+
159+
#endif

include/TextReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ namespace openshot
121121
TextReader(int width, int height, int x_offset, int y_offset, GravityType gravity, string text, string font, double size, string text_color, string background_color);
122122

123123
/// Draw a box under rendered text using the specified color.
124-
/// @param text_background_color The background color behind the text
124+
/// @param color The background color behind the text
125125
void SetTextBackgroundColor(string color);
126126

127127
/// Close Reader

src/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ SET ( OPENSHOT_SOURCE_FILES
223223
QtPlayer.cpp
224224
Settings.cpp
225225
Timeline.cpp
226+
QtTextReader.cpp
227+
QtHtmlReader.cpp
228+
226229

227230
# Qt Video Player
228231
${QT_PLAYER_FILES}
@@ -335,7 +338,7 @@ target_link_libraries(openshot PUBLIC ${REQUIRED_LIBRARIES})
335338
# Pick up parameters from OpenMP target and propagate
336339
target_link_libraries(openshot PUBLIC OpenMP::OpenMP_CXX)
337340

338-
############### CLI EXECUTABLE ################
341+
############### CLI EXECUTABLES ################
339342
# Create test executable
340343
add_executable(openshot-example examples/Example.cpp)
341344

@@ -350,6 +353,9 @@ target_compile_definitions(openshot-example PRIVATE
350353
# Link test executable to the new library
351354
target_link_libraries(openshot-example openshot)
352355

356+
add_executable(openshot-html-test examples/ExampleHtml.cpp)
357+
target_link_libraries(openshot-html-test openshot Qt5::Gui)
358+
353359
############### PLAYER EXECUTABLE ################
354360
# Create test executable
355361
add_executable(openshot-player Qt/demo/main.cpp)

0 commit comments

Comments
 (0)