OpenShot Library | libopenshot  0.2.5
TextReader.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for TextReader class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #ifndef OPENSHOT_TEXT_READER_H
32 #define OPENSHOT_TEXT_READER_H
33 
34 // Require ImageMagick support
35 #ifdef USE_IMAGEMAGICK
36 
37 #include "ReaderBase.h"
38 
39 #include <cmath>
40 #include <ctime>
41 #include <iostream>
42 #include <omp.h>
43 #include <stdio.h>
44 #include <memory>
45 #include "CacheMemory.h"
46 #include "Enums.h"
47 #include "Exceptions.h"
48 #include "MagickUtilities.h"
49 
50 namespace openshot
51 {
52 
53  /**
54  * @brief This class uses the ImageMagick++ 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  * // Create a reader to generate an openshot::Frame containing text
62  * TextReader r(720, // width
63  * 480, // height
64  * 5, // x_offset
65  * 5, // y_offset
66  * GRAVITY_CENTER, // gravity
67  * "Check out this Text!", // text
68  * "Arial", // font
69  * 15.0, // size
70  * "#fff000", // text_color
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 TextReader : public ReaderBase
86  {
87  private:
88  int width;
89  int height;
90  int x_offset;
91  int y_offset;
92  std::string text;
93  std::string font;
94  double size;
95  std::string text_color;
96  std::string background_color;
97  std::string text_background_color;
98  std::shared_ptr<Magick::Image> image;
99  MAGICK_DRAWABLE lines;
100  bool is_open;
101  openshot::GravityType gravity;
102 
103  public:
104 
105  /// Default constructor (blank text)
106  TextReader();
107 
108  /// @brief Constructor for TextReader with all parameters.
109  /// @param width The width of the requested openshot::Frame (not the size of the text)
110  /// @param height The height of the requested openshot::Frame (not the size of the text)
111  /// @param x_offset The number of pixels to offset the text on the X axis (horizontal)
112  /// @param y_offset The number of pixels to offset the text on the Y axis (vertical)
113  /// @param gravity The alignment / gravity of the text
114  /// @param text The text you want to generate / display
115  /// @param font The font of the text
116  /// @param size The size of the text
117  /// @param text_color The color of the text
118  /// @param background_color The background color of the text frame image (also supports Transparent)
119  TextReader(int width, int height, int x_offset, int y_offset, GravityType gravity, std::string text, std::string font, double size, std::string text_color, std::string background_color);
120 
121  /// Draw a box under rendered text using the specified color.
122  /// @param color The background color behind the text
123  void SetTextBackgroundColor(std::string color);
124 
125  /// Close Reader
126  void Close();
127 
128  /// Get the cache object used by this reader (always returns NULL for this object)
129  openshot::CacheMemory* GetCache() { return NULL; };
130 
131  /// Get an openshot::Frame object for a specific frame number of this reader. All numbers
132  /// return the same Frame, since they all share the same image data.
133  ///
134  /// @returns The requested frame (containing the image)
135  /// @param requested_frame The frame number that is requested.
136  std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame);
137 
138  /// Determine if reader is open or closed
139  bool IsOpen() { return is_open; };
140 
141  /// Return the type name of the class
142  std::string Name() { return "TextReader"; };
143 
144  /// Get and Set JSON methods
145  std::string Json() const override; ///< Generate JSON string of this object
146  void SetJson(const std::string value); ///< Load JSON string into this object
147  Json::Value JsonValue() const override; ///< Generate Json::Value for this object
148  void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object
149 
150  /// Open Reader - which is called by the constructor automatically
151  void Open();
152  };
153 
154 }
155 
156 #endif //USE_IMAGEMAGICK
157 #endif //OPENSHOT_TEXT_READER_H
Header file for CacheMemory class.
Header file for TextReader class.
Header file for all Exception classes.
Header file for MagickUtilities (IM6/IM7 compatibility overlay)
#define MAGICK_DRAWABLE
Header file for ReaderBase class.
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:51
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:98
This class uses the ImageMagick++ libraries, to create frames with "Text", and return openshot::Frame...
Definition: TextReader.h:86
std::string Json() const override
Get and Set JSON methods.
Definition: TextReader.cpp:190
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame)
Definition: TextReader.cpp:164
TextReader()
Default constructor (blank text)
Definition: TextReader.cpp:39
void SetTextBackgroundColor(std::string color)
Definition: TextReader.cpp:54
openshot::CacheMemory * GetCache()
Get the cache object used by this reader (always returns NULL for this object)
Definition: TextReader.h:129
void Open()
Open Reader - which is called by the constructor automatically.
Definition: TextReader.cpp:63
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: TextReader.cpp:197
std::string Name()
Return the type name of the class.
Definition: TextReader.h:142
void Close()
Close Reader.
Definition: TextReader.cpp:153
bool IsOpen()
Determine if reader is open or closed.
Definition: TextReader.h:139
void SetJson(const std::string value)
Load JSON string into this object.
Definition: TextReader.cpp:219
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: TextReader.cpp:234
This namespace is the default namespace for all code in the openshot library.
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition: Enums.h:39