Ocean
rendering/Text.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_TEXT_H
9 #define META_OCEAN_RENDERING_TEXT_H
10 
14 #include "ocean/rendering/Shape.h"
15 
16 #include "ocean/math/Lookup2.h"
17 
18 namespace Ocean
19 {
20 
21 namespace Rendering
22 {
23 
24 // Forward declaration
25 class Text;
26 
27 /**
28  * Definition of a smart object reference holding a text.
29  * @see SmartObjectRef, Text.
30  * @ingroup rendering
31  */
33 
34 /**
35  * This class is the base class for all texts.
36  * @ingroup rendering
37  */
38 class OCEAN_RENDERING_EXPORT Text : virtual public Shape
39 {
40  public:
41 
42  /**
43  * Definition of individual alignment modes within the text block.
44  */
46  {
47  /**
48  * The text is aligned to the left of the text block.
49  * The alignment mode is visualized below:
50  * <pre>
51  * ------------------------
52  * | This is the first line |
53  * | The second line |
54  * | ... |
55  * | The last line |
56  * ------------------------
57  * </pre>
58  */
60 
61  /**
62  * The text is aligned at the center of the text block.
63  * The alignment mode is visualized below:
64  * <pre>
65  * ------------------------
66  * | This is the first line |
67  * | The second line |
68  * | ... |
69  * | The last line |
70  * ------------------------
71  * </pre>
72  */
74 
75  /**
76  * The text is aligned to the left of the text block.
77  * The alignment mode is visualized below:
78  * <pre>
79  * ------------------------
80  * | This is the first line |
81  * | The second line |
82  * | ... |
83  * | The last line |
84  * ------------------------
85  * </pre>
86  */
87  AM_RIGHT
88  };
89 
90  /**
91  * Definition of individual horizontal anchor points for the entire text block.
92  */
94  {
95  /**
96  * The text block is anchored at the left edge.
97  * The anchor mode is visualized below:
98  * <pre>
99  * (A)------------------------- (A): HA_LEFT, VA_TOP
100  * | This is the first line |
101  * (B) ... | (B): HA_LEFT, VA_MIDDLE
102  * | The last line |
103  * (C)------------------------- (C): HA_LEFT, VA_BOTTOM
104  * </pre>
105  */
107 
108  /**
109  * The text block is anchored at the center of the text block.
110  * The anchor mode is visualized below:
111  * <pre>
112  * ------------(A)------------ (A): HA_CENTER, VA_TOP
113  * | This is the first line |
114  * | ... (B) | (B): HA_CENTER, VA_MIDDLE
115  * | The last line |
116  * -----------(C)------------ (C): HA_CENTER, VA_BOTTOM
117  * </pre>
118  */
120 
121  /**
122  * The text block is anchored at the right edge.
123  * The anchor mode is visualized below:
124  * <pre>
125  * -------------------------(A) (A): HA_RIGHT, VA_TOP
126  * | This is the first line |
127  * | ... (B) (B): HA_RIGHT, VA_MIDDLE
128  * | The last line |
129  * -------------------------(C) (C): HA_RIGHT, VA_BOTTOM
130  * </pre>
131  */
132  HA_RIGHT
133  };
134 
135  /**
136  * Definition of individual vertical anchor points for the entire text block.
137  */
139  {
140  /// The text block is anchored at the top edge.
142  /// The text block is anchored at middle oft he text block.
144  /// The text block is anchored at the bottom edge.
145  VA_BOTTOM
146  };
147 
148  public:
149 
150  /**
151  * Returns the text to be rendered.
152  * @return The text which will be rendered
153  */
154  virtual std::string text() const;
155 
156  /**
157  * Returns the actual width and height of the text.
158  * @return The text's width and height
159  */
160  virtual Vector2 size() const;
161 
162  /**
163  * Returns the font which is used to render the text.
164  * @param styleName Optional resulting style name of the font
165  * @return The text's font
166  */
167  virtual std::string fontFamily(std::string* styleName = nullptr) const;
168 
169  /**
170  * Returns the material which is used to render the background of the text.
171  * By default, the text will be rendered with a black opaque background.
172  * @return The text's background material
173  */
175 
176  /**
177  * Returns the alignment mode of the text.
178  * @return The text's alignment mode
179  */
180  virtual AlignmentMode alignmentMode() const;
181 
182  /**
183  * Returns the horizontal anchor of the text.
184  * @return The text's horizontal anchor
185  */
187 
188  /**
189  * Returns the vertical anchor of the text.
190  * @return The text's vertical anchor
191  */
193 
194  /**
195  * Sets the text to be rendred.
196  * Multiple lines can be rendered by adding a line feed character ('\n') at the end of a line.
197  * @param text The text to be rendered
198  */
199  virtual void setText(const std::string& text);
200 
201  /**
202  * Sets the size of the text.
203  * Four different size modes are supported:<br>
204  * 1) The text block has a fixed width and the height is automatically determined by preserving the aspect ratio.<br>
205  * 2) The text block has a fixed height and the width is automatically determined by preserving the aspect ratio.<br>
206  * 3) The text block has a fixed width and fixed height the text's aspect ratio is not preserved.<br>
207  * 4) The width and height of the text block is automatically determined by ensuring that each text line has a specified height while the aspect ratio is preserved.<br>
208  * @param fixedWidth The fixed width of the text block, with range (0, infinity), 0 to define the width indirect via 'fixedHeight' or 'fixedLineHeight'
209  * @param fixedHeight The fixed height of the text block, with range (0, infinity), 0 to define the height indirect via 'fixedWidth' or 'fixedLineHeight'
210  * @param fixedLineHeight The height of each line in the text block, with range (0, infinity), 0 to define the size via 'fixedWidth' and/or 'fixedHeight'
211  * @return True, if the provided combination of size parameter is valid
212  */
213  virtual bool setSize(const Scalar fixedWidth, const Scalar fixedHeight, const Scalar fixedLineHeight);
214 
215  /**
216  * Sets the font to be used when rendering the text.
217  * @param fontFamily The family identifying the font, must be valid
218  * @param styleName The optional style name of the font, empty to use the default style
219  * @see availableFamilyNames(), availableStyleNames().
220  */
221  virtual void setFont(const std::string& fontFamily, const std::string& styleName = std::string());
222 
223  /**
224  * Returns the material which is used to render the background of the text.
225  * By default, the text will be rendered with a black opaque background.<br>
226  * The background can be fully customized by e.g., setting a different diffuse color or the transparency of the background.
227  *
228  * To render the text without background, the material must be entirely transparent and a BlendAttribute must be attached to the corresponding AttributeSet:
229  * @code
230  * BlendAttributeRef blendAttribute = engine().factory().createBlendAttribute();
231  *
232  * blendAttribute->setSourceFunction(BlendAttribute::BlendingFunction::FUNCTION_ONE);
233  * blendAttribute->setDestinationFunction(BlendAttribute::BlendingFunction::FUNCTION_ONE_MINUS_SOURCE_ALPHA); // fully transparent with pre-multiplied alpha
234  *
235  * textAttributeSet->addAttribute(blendAttribute);
236  *
237  * textBackgroundMaterial->setTransparency(1.0f);
238  * @endcode
239  *
240  * To render the text with partially transparent background, the material must be translucent and a BlendAttribute must be attached to the corresponding AttributeSet:
241  * @code
242  * BlendAttributeRef blendAttribute = engine().factory().createBlendAttribute();
243  *
244  * blendAttribute->setSourceFunction(BlendAttribute::BlendingFunction::FUNCTION_SOURCE_ALPHA);
245  * blendAttribute->setDestinationFunction(BlendAttribute::BlendingFunction::FUNCTION_ONE_MINUS_SOURCE_ALPHA); // translucent with standard blending
246  *
247  * textAttributeSet->addAttribute(blendAttribute);
248  *
249  * textBackgroundMaterial->setTransparency(0.5f);
250  * @endcode
251  *
252  * To render the text with opaque background, the material must be opaque:
253  * @code
254  * textBackgroundMaterial->setTransparency(0.0f);
255  * @endcode
256  *
257  * @param material The background material to be set
258  */
259  virtual void setBackgroundMaterial(const MaterialRef& material);
260 
261  /**
262  * Sets the alignment mode of the text.
263  * @param alignmentMode The alignment node to be set
264  */
265  virtual void setAlignmentMode(const AlignmentMode alignmentMode);
266 
267  /**
268  * Sets the horizontal anchor of the text.
269  * @param horizontalAnchor The horizontal anchor to be set
270  */
271  virtual void setHorizontalAnchor(const HorizontalAnchor horizontalAnchor);
272 
273  /**
274  * Sets the vertical anchor of the text.
275  * @param verticalAnchor The vertical anchor to be set
276  */
277  virtual void setVerticalAnchor(const VerticalAnchor verticalAnchor);
278 
279  /**
280  * Sets an explicit lookup table which will be used to determine the text geometry (the vertices of the text).
281  * In case a lookup table is set, the horizontal and vertical anchor do not have any meaning anymore.<br>
282  * The width and height of the lookup table must be identical to the number of horizontal and vertical bins.
283  * @param lookupTable The lookup table to be set, an invalid lookup table to remove a previously set table
284  */
285  virtual void setGeometryLookupTable(const LookupCorner2<Vector3>& lookupTable);
286 
287  /**
288  * Returns whether a specific font is currently available.
289  * @param familyName The name of the font family to check
290  * @param styleName Optional explicit font style to check
291  * @return True, if so
292  */
293  virtual bool isFontAvailable(const std::string& familyName, const std::string& styleName = std::string()) const;
294 
295  /**
296  * Returns the default front currently available.
297  * @param styleName Optional resulting style name of the resulting font, nullptr if not of interest
298  * @return The family name of the currently available font, empty if no default is available
299  */
300  virtual std::string availableDefaultFont(std::string* styleName = nullptr) const;
301 
302  /**
303  * Returns all available family names.
304  * @return The family names which are available
305  * @see setFont(), availableStyleNames().
306  */
307  virtual std::vector<std::string> availableFamilyNames() const;
308 
309  /**
310  * Returns the style names of all available fonts with specified family name.
311  * @param familyName The family name of the fonts for which all available style names will be returned
312  * @return The style names of all available fonts
313  */
314  virtual std::vector<std::string> availableStyleNames(const std::string& familyName) const;
315 
316  /**
317  * Returns the type of this object.
318  * @see Object::type().
319  */
320  ObjectType type() const override;
321 
322  protected:
323 
324  /**
325  * Creates a new text object.
326  */
327  Text();
328 
329  /**
330  * Destructs the text object.
331  */
332  ~Text() override;
333 };
334 
335 }
336 
337 }
338 
339 #endif // META_OCEAN_RENDERING_TEXT_H
This class implements a 2D lookup object with values at the bins' corners defining the individual loo...
Definition: Lookup2.h:636
ObjectType
Definition of different object type.
Definition: Object.h:63
This class is the base class for all geometries.
Definition: rendering/Shape.h:35
This class is the base class for all texts.
Definition: rendering/Text.h:39
virtual void setGeometryLookupTable(const LookupCorner2< Vector3 > &lookupTable)
Sets an explicit lookup table which will be used to determine the text geometry (the vertices of the ...
HorizontalAnchor
Definition of individual horizontal anchor points for the entire text block.
Definition: rendering/Text.h:94
@ HA_CENTER
The text block is anchored at the center of the text block.
Definition: rendering/Text.h:119
@ HA_LEFT
The text block is anchored at the left edge.
Definition: rendering/Text.h:106
virtual std::string availableDefaultFont(std::string *styleName=nullptr) const
Returns the default front currently available.
virtual bool setSize(const Scalar fixedWidth, const Scalar fixedHeight, const Scalar fixedLineHeight)
Sets the size of the text.
AlignmentMode
Definition of individual alignment modes within the text block.
Definition: rendering/Text.h:46
@ AM_CENTER
The text is aligned at the center of the text block.
Definition: rendering/Text.h:73
@ AM_LEFT
The text is aligned to the left of the text block.
Definition: rendering/Text.h:59
~Text() override
Destructs the text object.
Text()
Creates a new text object.
ObjectType type() const override
Returns the type of this object.
virtual void setBackgroundMaterial(const MaterialRef &material)
Returns the material which is used to render the background of the text.
virtual AlignmentMode alignmentMode() const
Returns the alignment mode of the text.
virtual void setVerticalAnchor(const VerticalAnchor verticalAnchor)
Sets the vertical anchor of the text.
virtual std::string text() const
Returns the text to be rendered.
virtual VerticalAnchor verticalAnchor() const
Returns the vertical anchor of the text.
virtual Vector2 size() const
Returns the actual width and height of the text.
VerticalAnchor
Definition of individual vertical anchor points for the entire text block.
Definition: rendering/Text.h:139
@ VA_TOP
The text block is anchored at the top edge.
Definition: rendering/Text.h:141
@ VA_MIDDLE
The text block is anchored at middle oft he text block.
Definition: rendering/Text.h:143
virtual void setFont(const std::string &fontFamily, const std::string &styleName=std::string())
Sets the font to be used when rendering the text.
virtual HorizontalAnchor horizontalAnchor() const
Returns the horizontal anchor of the text.
virtual std::vector< std::string > availableFamilyNames() const
Returns all available family names.
virtual MaterialRef backgroundMaterial() const
Returns the material which is used to render the background of the text.
virtual std::vector< std::string > availableStyleNames(const std::string &familyName) const
Returns the style names of all available fonts with specified family name.
virtual std::string fontFamily(std::string *styleName=nullptr) const
Returns the font which is used to render the text.
virtual bool isFontAvailable(const std::string &familyName, const std::string &styleName=std::string()) const
Returns whether a specific font is currently available.
virtual void setAlignmentMode(const AlignmentMode alignmentMode)
Sets the alignment mode of the text.
virtual void setText(const std::string &text)
Sets the text to be rendred.
virtual void setHorizontalAnchor(const HorizontalAnchor horizontalAnchor)
Sets the horizontal anchor of the text.
float Scalar
Definition of a scalar type.
Definition: Math.h:128
SmartObjectRef< Text > TextRef
Definition of a smart object reference holding a text.
Definition: rendering/Text.h:25
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15