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