OpenShot Library | libopenshot  0.2.7
QtTextReader.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for QtTextReader 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_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 
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 "Text", and return
57  * openshot::Frame objects.
58  *
59  * All system fonts are supported, including many different font properties, such as size, color,
60  * alignment, padding, etc...
61  *
62  * @code
63  * // Any application using this class must instantiate either QGuiApplication or QApplication
64  * QApplication a(argc, argv);
65  *
66  * // Create a reader to generate an openshot::Frame containing text
67  * QtTextReader r(720, // width
68  * 480, // height
69  * 5, // x_offset
70  * 5, // y_offset
71  * GRAVITY_CENTER, // gravity
72  * "Check out this Text!", // text
73  * "Arial", // font
74  * 15.0, // font size
75  * "#fff000", // text_color
76  * "#000000" // background_color
77  * );
78  * r.Open(); // Open the reader
79  *
80  * // Get frame number 1 from the video (in fact, any frame # you request will return the same frame)
81  * std::shared_ptr<Frame> f = r.GetFrame(1);
82  *
83  * // Now that we have an openshot::Frame object, lets have some fun!
84  * f->Display(); // Display the frame on the screen
85  *
86  * // Close the reader
87  * r.Close();
88  * @endcode
89  */
90  class QtTextReader : public ReaderBase
91  {
92  private:
93  int width;
94  int height;
95  int x_offset;
96  int y_offset;
97  std::string text;
98  QFont font;
99  std::string text_color;
100  std::string background_color;
101  std::string text_background_color;
102  std::shared_ptr<QImage> image;
103  bool is_open;
104  openshot::GravityType gravity;
105 
106  public:
107 
108  /// Default constructor (blank text)
109  QtTextReader();
110 
111  /// @brief Constructor for QtTextReader with all parameters.
112  /// @param width The width of the requested openshot::Frame (not the size of the text)
113  /// @param height The height of the requested openshot::Frame (not the size of the text)
114  /// @param x_offset The number of pixels to offset the text on the X axis (horizontal)
115  /// @param y_offset The number of pixels to offset the text on the Y axis (vertical)
116  /// @param gravity The alignment / gravity of the text
117  /// @param text The text you want to generate / display
118  /// @param font The font of the text
119  /// @param text_color The color of the text (valid values are a color string in \#RRGGBB or \#AARRGGBB notation or a CSS color name)
120  /// @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')
121  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);
122 
123  /// Draw a box under rendered text using the specified color.
124  /// @param color The background color behind the text (valid values are a color string in \#RRGGBB or \#AARRGGBB notation or a CSS color name)
125  void SetTextBackgroundColor(std::string color);
126 
127  /// Close Reader
128  void Close() override;
129 
130  /// Get the cache object used by this reader (always returns NULL for this object)
131  CacheBase* GetCache() override { return NULL; };
132 
133  /// Get an openshot::Frame object for a specific frame number of this reader. All numbers
134  /// return the same Frame, since they all share the same image data.
135  ///
136  /// @returns The requested frame (containing the image)
137  /// @param requested_frame The frame number that is requested.
138  std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame) override;
139 
140  /// Determine if reader is open or closed
141  bool IsOpen() override { return is_open; };
142 
143  /// Return the type name of the class
144  std::string Name() override { return "QtTextReader"; };
145 
146  // Get and Set JSON methods
147  std::string Json() const override; ///< Generate JSON string of this object
148  void SetJson(const std::string value) override; ///< Load JSON string into this object
149  Json::Value JsonValue() const override; ///< Generate Json::Value for this object
150  void SetJsonValue(const Json::Value root) override; ///< Load Json::Value into this object
151 
152  /// Open Reader - which is called by the constructor automatically
153  void Open() override;
154  };
155 
156 }
157 
158 #endif
Header file for CacheMemory class.
Header file for TextReader class.
Header file for ReaderBase class.
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:49
This class uses Qt libraries, to create frames with "Text", and return openshot::Frame objects.
Definition: QtTextReader.h:91
void SetJson(const std::string value) override
Load JSON string into this object.
void SetTextBackgroundColor(std::string color)
void Close() override
Close Reader.
std::string Json() const override
Generate JSON string of this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
QtTextReader()
Default constructor (blank text)
std::string Name() override
Return the type name of the class.
Definition: QtTextReader.h:144
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Json::Value JsonValue() const override
Generate Json::Value for this object.
void Open() override
Open Reader - which is called by the constructor automatically.
CacheBase * GetCache() override
Get the cache object used by this reader (always returns NULL for this object)
Definition: QtTextReader.h:131
bool IsOpen() override
Determine if reader is open or closed.
Definition: QtTextReader.h:141
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:98
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition: Enums.h:39