Ocean
GLESText.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_RENDERING_GLES_TEXT_H
9 #define META_OCEAN_RENDERING_GLES_TEXT_H
10 
14 
15 #include "ocean/base/Singleton.h"
16 
18 
19 #include "ocean/cv/fonts/Font.h"
20 
21 #include "ocean/rendering/Text.h"
23 
24 namespace Ocean
25 {
26 
27 namespace Rendering
28 {
29 
30 namespace GLESceneGraph
31 {
32 
33 /**
34  * This class implements a GLESceneGraph text object.
35  * @ingroup renderinggles
36  */
37 class OCEAN_RENDERING_GLES_EXPORT GLESText :
38  virtual public GLESShape,
39  virtual protected GLESTriangles,
40  virtual public Text
41 {
42  friend class GLESFactory;
43 
44  protected:
45 
46  /**
47  * The manager providing access to the texture containing the font's characters and some associated information.
48  */
49  class FontManager : public Singleton<FontManager>
50  {
51  friend class Singleton<FontManager>;
52 
53  public:
54 
55  /**
56  * This class stores the relevant information necessary to render the text using one specific font.
57  */
58  class FontData
59  {
60  public:
61 
62  /**
63  * Default constructor creating an invalid object.
64  */
65  FontData() = default;
66 
67  /**
68  * Creates a new object.
69  * @param font The font of this object, must be valid
70  * @param textures The textures object holding the texture with the font's characters, must be valid
71  * @param characters The layout information for all characters
72  * @param textureWidth The width of the texture, in pixel, with range [1, infinity)
73  * @param textureHeight The height of the texture, in pixel, with range [1, infinity)
74  */
75  inline FontData(const CV::Fonts::SharedFont& font, const TexturesRef& textures, const CV::Fonts::Font::SharedCharacters& characters, const unsigned int textureWidth, const unsigned int textureHeight);
76 
77  /**
78  * Returns whether this object hold valid information.
79  * @return True, if so
80  */
81  inline bool isValid() const;
82 
83  public:
84 
85  /// The font of this object.
87 
88  /// The textures object holding the texture with the font's characters.
90 
91  /// The layout information for all characters.
93 
94  /// The width of the texture, in pixel, with range [1, infinity), 0 if this object is invalid.
95  unsigned int textureWidth_ = 0u;
96 
97  /// The height of the texture, in pixel, with range [1, infinity), 0 if this object is invalid.
98  unsigned int textureHeight_ = 0u;
99  };
100 
101  protected:
102 
103  /**
104  * Definition of a pair combining the font's family name and style name.
105  */
106  typedef std::pair<std::string, std::string> FontPair;
107 
108  /**
109  * Definition of a map mapping the font's name pair to FontData objects.
110  */
111  typedef std::map<FontPair, FontData> FontMap;
112 
113  public:
114 
115  /**
116  * Returns the relevant information necessary to render the text using a specific font.
117  * @param engine The rendering engine to be used
118  * @param fontFamily The family identifying the font, must be valid
119  * @param styleName The optional style name of the font, empty to use the default style
120  * @return The FontData object containing the relevant information, can be invalid
121  */
122  FontData fontData(Rendering::Engine& engine, const std::string& fontFamily, const std::string& styleName = std::string());
123 
124  /**
125  * Returns the font (with correct font size) which is used to create the relevant information for the manager.
126  * @param fontFamily The family identifying the font, must be valid
127  * @param styleName The optional style name of the font, empty to use the default style
128  * @return The font, nullptr if unknown
129  */
130  CV::Fonts::SharedFont font(const std::string& fontFamily, const std::string& styleName = std::string());
131 
132  /**
133  * Releases all text texture resources.
134  * The font manager holds textures even if the owning text objects are disposed.<br>
135  * Therefore, an explicit releasing of all text resources is necessary before the rendering engine is disposed.
136  */
137  void release();
138 
139  protected:
140 
141  /// The map mapping font names to FontData objects.
143 
144  /// The manager's lock.
146  };
147 
148  public:
149 
150  /**
151  * Returns the text to be rendered.
152  * @see Text::text().
153  */
154  std::string text() const override;
155 
156  /**
157  * Returns the actual width and height of the text.
158  * @see Text::size().
159  */
160  Vector2 size() const override;
161 
162  /**
163  * Returns the font which is used to render the text.
164  * @see Text::fontFamily().
165  */
166  std::string fontFamily(std::string* styleName = nullptr) const override;
167 
168  /**
169  * Returns the material which is used to render the background of the text.
170  * @see Text::backgroundMaterial().
171  */
172  MaterialRef backgroundMaterial() const override;
173 
174  /**
175  * Returns the alignment mode of the text.
176  * @see Text::alignmentMode().
177  */
178  AlignmentMode alignmentMode() const override;
179 
180  /**
181  * Returns the horizontal anchor of the text.
182  * @see Text::horizontalAnchor().
183  */
185 
186  /**
187  * Returns the vertical anchor of the text.
188  * @return Text::verticalAnchor().
189  */
190  VerticalAnchor verticalAnchor() const override;
191 
192  /**
193  * Sets the text to be rendred.
194  * @see Text::setText().
195  */
196  void setText(const std::string& text) override;
197 
198  /**
199  * Sets the size of the text.
200  * @see Text::setSize().
201  */
202  bool setSize(const Scalar fixedWidth, const Scalar fixedHeight, const Scalar fixedLineHeight) override;
203 
204  /**
205  * Sets the font to be used when rendering the text.
206  * @see Text::setFont().
207  */
208  void setFont(const std::string& fontFamily, const std::string& styleName = std::string()) override;
209 
210  /**
211  * Returns the material which is used to render the background of the text.
212  * @see Text::setBackgroundMaterial().
213  */
214  void setBackgroundMaterial(const MaterialRef& material) override;
215 
216  /**
217  * Sets the alignment mode of the text.
218  * @see Text::setAlignmentMode().
219  */
220  void setAlignmentMode(const AlignmentMode alignmentMode) override;
221 
222  /**
223  * Sets the horizontal anchor of the text.
224  * @see Text::setHorizontalAnchor().
225  */
226  void setHorizontalAnchor(const HorizontalAnchor horizontalAnchor) override;
227 
228  /**
229  * Sets the vertical anchor of the text.
230  * @see Text::setVerticalAnchor().
231  */
232  void setVerticalAnchor(const VerticalAnchor verticalAnchor) override;
233 
234  /**
235  * Sets an explicit lookup table which will be used to determine the text geometry (the vertices of the text).
236  * @see Text::setGeometryLookupTable().
237  */
238  void setGeometryLookupTable(const LookupCorner2<Vector3>& lookupTable) override;
239 
240  /**
241  * Returns whether a specific font is currently available.
242  * @see Text::isFontAvailable().
243  */
244  bool isFontAvailable(const std::string& familyName, const std::string& styleName = std::string()) const override;
245 
246  /**
247  * Returns the default front currently available.
248  * @see Text::availableDefaultFont().
249  */
250  std::string availableDefaultFont(std::string* styleName = nullptr) const override;
251 
252  /**
253  * Returns all available family names.
254  * @see Text::availableFamilyNames().
255  */
256  std::vector<std::string> availableFamilyNames() const override;
257 
258  /**
259  * Returns the style names of all available fonts with specified family name.
260  * @see Text::availableStyleNames().
261  */
262  std::vector<std::string> availableStyleNames(const std::string& familyName) const override;
263 
264  /**
265  * Renders the triangles defined by the associated vertex set and the defined triangle faces.
266  * @see Renderable::render().
267  */
268  void render(const GLESFramebuffer& framebuffer, const SquareMatrix4& projectionMatrix, const HomogenousMatrix4& camera_T_object, const HomogenousMatrix4& camera_T_world, const SquareMatrix3& normalMatrix, GLESAttributeSet& attributeSet, const Lights& lights) override;
269 
270  /**
271  * Returns the type of this object.
272  * @see Object::type().
273  */
274  ObjectType type() const override;
275 
276  /**
277  * Returns descriptive information about the object as string.
278  * @see Object::descriptiveInformation().
279  */
280  std::string descriptiveInformation() const override;
281 
282  /**
283  * Releases all text texture resources.
284  * The font manager holds textures even if the owning text objects are disposed.<br>
285  * Therefore, an explicit releasing of all text resources is necessary before the rendering engine is disposed.
286  */
287  static void release();
288 
289  protected:
290 
291  /**
292  * Creates a new GLESceneGraph box object.
293  */
295 
296  /**
297  * Destructs a GLESceneGraph box object.
298  */
299  ~GLESText() override;
300 
301  /**
302  * Rebuilds the primitive vertex, normals and face sets.
303  * @see GLESShape::rebuildPrimitives().
304  */
305  void rebuildPrimitives() override;
306 
307  /**
308  * Updates the bounding box of this primitive.
309  * @see GLESIndependentPrimitive::updateBoundingBox().
310  */
311  void updateBoundingBox() override;
312 
313  /**
314  * Calculates the size of the reulting text block.
315  * @param font The font to be used
316  * @param linePixelBoundingBoxes The resulting bounding boxes of the individual text lines, in the font pixel domain
317  * @param textWidthPixels The resulting width of the text block, in the font pixel domain
318  * @param textHeightPixels The resulting height of the text block, in the font pixel domain
319  * @param textWidth The resulting width of the text block, in space
320  * @param textHeight The resulting height of the text block, in space
321  * @return True, if succeeded
322  */
323  bool calculateTextSize(const CV::Fonts::Font& font, CV::PixelBoundingBoxesI& linePixelBoundingBoxes, unsigned int& textWidthPixels, unsigned int& textHeightPixels, Scalar& textWidth, Scalar& textHeight) const;
324 
325  protected:
326 
327  /// The text which is actually rendered.
328  std::string text_;
329 
330  /// The fixed size of the text; (0, 0) to use the.
331  Vector2 fixedWidthHeight_ = Vector2(0, 0);
332 
333  /// The fixed height of each text line; 0 to use 'fixedWidthHeight_'.
334  Scalar fixedLineHeight_ = Scalar(0.05);
335 
336  /// The actual resulting size of the rendered text; (0, 0) until known
337  Vector2 resultingSize_ = Vector2(0, 0);
338 
339  /// The font family to be used.
340  std::string fontFamily_;
341 
342  /// The font's style name to be used, empty to use the default style.
343  std::string styleName_;
344 
345  /// The text's alignment mode.
346  AlignmentMode alignmentMode_ = AM_LEFT;
347 
348  /// The text's horizontal anchor.
349  HorizontalAnchor horizontalAnchor_ = HA_LEFT;
350 
351  /// The text's vertical anchor.
352  VerticalAnchor verticalAnchor_ = VA_TOP;
353 
354  /// True, if the text premitives need to be rebuilt.
355  bool needsRebuild_ = false;
356 
357  /// The textures object holding the texture with the font's characters.
359 
360  /// The background material to be used.
362 
363  /// The optional lookup table for the text geometry.
365 };
366 
367 inline GLESText::FontManager::FontData::FontData(const CV::Fonts::SharedFont& font, const TexturesRef& textures, const CV::Fonts::Font::SharedCharacters& characters, const unsigned int textureWidth, const unsigned int textureHeight) :
368  font_(font),
369  textures_(textures),
370  characters_(characters),
371  textureWidth_(textureWidth),
372  textureHeight_(textureHeight)
373 {
374  // nothing to do here
375 }
376 
378 {
379  return bool(textures_);
380 }
381 
382 }
383 
384 }
385 
386 }
387 
388 #endif // META_OCEAN_RENDERING_GLES_TEXT_H
This class is the base class for all font implementations.
Definition: Font.h:43
std::shared_ptr< Characters > SharedCharacters
Definition of a shared pointers holding characters.
Definition: Font.h:170
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a 2D lookup object with values at the bins' corners defining the individual loo...
Definition: Lookup2.h:636
This class is the base class for all rendering engines like.
Definition: Engine.h:46
This class implements a GLESceneGraph attribute set object.
Definition: GLESAttributeSet.h:40
This class implements a class factory for all GLESceneGraph objects.
Definition: GLESFactory.h:30
This class implements a base for all GLESceneGraph framebuffers.
Definition: rendering/glescenegraph/GLESFramebuffer.h:34
This class is the base class for all GLESceneGraph shape objects.
Definition: GLESShape.h:34
This class stores the relevant information necessary to render the text using one specific font.
Definition: GLESText.h:59
CV::Fonts::SharedFont font_
The font of this object.
Definition: GLESText.h:86
CV::Fonts::Font::SharedCharacters characters_
The layout information for all characters.
Definition: GLESText.h:92
FontData()=default
Default constructor creating an invalid object.
bool isValid() const
Returns whether this object hold valid information.
Definition: GLESText.h:377
TexturesRef textures_
The textures object holding the texture with the font's characters.
Definition: GLESText.h:89
The manager providing access to the texture containing the font's characters and some associated info...
Definition: GLESText.h:50
std::pair< std::string, std::string > FontPair
Definition of a pair combining the font's family name and style name.
Definition: GLESText.h:106
FontData fontData(Rendering::Engine &engine, const std::string &fontFamily, const std::string &styleName=std::string())
Returns the relevant information necessary to render the text using a specific font.
CV::Fonts::SharedFont font(const std::string &fontFamily, const std::string &styleName=std::string())
Returns the font (with correct font size) which is used to create the relevant information for the ma...
std::map< FontPair, FontData > FontMap
Definition of a map mapping the font's name pair to FontData objects.
Definition: GLESText.h:111
FontMap fontMap_
The map mapping font names to FontData objects.
Definition: GLESText.h:142
void release()
Releases all text texture resources.
Lock lock_
The manager's lock.
Definition: GLESText.h:145
This class implements a GLESceneGraph text object.
Definition: GLESText.h:41
void rebuildPrimitives() override
Rebuilds the primitive vertex, normals and face sets.
TexturesRef textures_
The textures object holding the texture with the font's characters.
Definition: GLESText.h:358
std::string fontFamily_
The font family to be used.
Definition: GLESText.h:340
std::string styleName_
The font's style name to be used, empty to use the default style.
Definition: GLESText.h:343
std::string text_
The text which is actually rendered.
Definition: GLESText.h:328
ObjectType type() const override
Returns the type of this object.
HorizontalAnchor horizontalAnchor() const override
Returns the horizontal anchor of the text.
void setAlignmentMode(const AlignmentMode alignmentMode) override
Sets the alignment mode of the text.
Vector2 size() const override
Returns the actual width and height of the text.
std::string text() const override
Returns the text to be rendered.
VerticalAnchor verticalAnchor() const override
Returns the vertical anchor of the text.
void setVerticalAnchor(const VerticalAnchor verticalAnchor) override
Sets the vertical anchor of the text.
bool isFontAvailable(const std::string &familyName, const std::string &styleName=std::string()) const override
Returns whether a specific font is currently available.
void setGeometryLookupTable(const LookupCorner2< Vector3 > &lookupTable) override
Sets an explicit lookup table which will be used to determine the text geometry (the vertices of the ...
AlignmentMode alignmentMode() const override
Returns the alignment mode of the text.
std::string availableDefaultFont(std::string *styleName=nullptr) const override
Returns the default front currently available.
bool calculateTextSize(const CV::Fonts::Font &font, CV::PixelBoundingBoxesI &linePixelBoundingBoxes, unsigned int &textWidthPixels, unsigned int &textHeightPixels, Scalar &textWidth, Scalar &textHeight) const
Calculates the size of the reulting text block.
void render(const GLESFramebuffer &framebuffer, const SquareMatrix4 &projectionMatrix, const HomogenousMatrix4 &camera_T_object, const HomogenousMatrix4 &camera_T_world, const SquareMatrix3 &normalMatrix, GLESAttributeSet &attributeSet, const Lights &lights) override
Renders the triangles defined by the associated vertex set and the defined triangle faces.
MaterialRef backgroundMaterial() const override
Returns the material which is used to render the background of the text.
bool setSize(const Scalar fixedWidth, const Scalar fixedHeight, const Scalar fixedLineHeight) override
Sets the size of the text.
GLESText()
Creates a new GLESceneGraph box object.
std::string fontFamily(std::string *styleName=nullptr) const override
Returns the font which is used to render the text.
void setHorizontalAnchor(const HorizontalAnchor horizontalAnchor) override
Sets the horizontal anchor of the text.
std::vector< std::string > availableStyleNames(const std::string &familyName) const override
Returns the style names of all available fonts with specified family name.
~GLESText() override
Destructs a GLESceneGraph box object.
void updateBoundingBox() override
Updates the bounding box of this primitive.
std::string descriptiveInformation() const override
Returns descriptive information about the object as string.
MaterialRef backgroundMaterial_
The background material to be used.
Definition: GLESText.h:361
LookupCorner2< Vector3 > lookupTable_
The optional lookup table for the text geometry.
Definition: GLESText.h:364
static void release()
Releases all text texture resources.
std::vector< std::string > availableFamilyNames() const override
Returns all available family names.
void setBackgroundMaterial(const MaterialRef &material) override
Returns the material which is used to render the background of the text.
void setFont(const std::string &fontFamily, const std::string &styleName=std::string()) override
Sets the font to be used when rendering the text.
void setText(const std::string &text) override
Sets the text to be rendred.
This class implements a GLESceneGraph triangles object.
Definition: GLESTriangles.h:33
ObjectType
Definition of different object type.
Definition: Object.h:63
This class is the base class for all texts.
Definition: rendering/Text.h:39
HorizontalAnchor
Definition of individual horizontal anchor points for the entire text block.
Definition: rendering/Text.h:94
AlignmentMode
Definition of individual alignment modes within the text block.
Definition: rendering/Text.h:46
VerticalAnchor
Definition of individual vertical anchor points for the entire text block.
Definition: rendering/Text.h:139
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
std::vector< PixelBoundingBoxI > PixelBoundingBoxesI
Definition of a vector holding bounding box objects with positive and negative coordinate values.
Definition: PixelBoundingBox.h:49
std::shared_ptr< Font > SharedFont
Definition of a shared font pointer.
Definition: Font.h:27
float Scalar
Definition of a scalar type.
Definition: Math.h:128
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition: Vector2.h:21
std::vector< LightPair > Lights
Definition of a set holding light sources.
Definition: GLESLightSource.h:44
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15