HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_Font.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: RE_Font.h (RE Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __RE_Font__
12 #define __RE_Font__
13 
14 #include "RE_API.h"
15 #include "RE_Texture.h"
16 #include "RE_VertexArray.h"
17 
18 #include <FONT/FONT_Enums.h>
19 #include <UT/UT_Array.h>
20 #include <UT/UT_NonCopyable.h>
21 #include <UT/UT_Pixel.h>
22 #include <UT/UT_Rect.h>
23 #include <UT/UT_String.h>
24 #include <UT/UT_StringView.h>
25 #include <UT/UT_Unicode.h>
26 #include <UT/UT_UniquePtr.h>
27 #include <SYS/SYS_Math.h>
28 
29 class FONT_Info;
30 class re_FontCache;
31 class re_FontInstance;
32 class re_FontBuffers;
33 class RE_Render;
34 class RE_VertexArray;
35 class UT_WorkBuffer;
36 
37 /// Specifies which method to use to split string in RE_Font::splitString.
39 {
40  RE_SST_NARROWER_ONLY, ///< Only split at word boundaries if narrower
41  /// than the given maximum width. If no split
42  /// point was found before, the splitting
43  /// fails.
44  RE_SST_BOUNDARY_SOFT, ///< Try first to split on word boundaries at
45  /// less than the maximum width, otherwise
46  /// try word boundaries after.
47  RE_SST_BOUNDARY_HARD, ///< Try first to split on word boundaries at
48  /// less than maximum width, otherwise do a
49  /// hard split at the maximum width, ignoring
50  /// any boundary indicators.
51 };
52 
54 {
55 public:
56  enum { BUFFER_CHUNK_SIZE = 4096 };
57 
63  const int myBucket;
64 
65  RE_FontBuffers(int bucket)
66  : myP(nullptr), myUV(nullptr), myCd(nullptr),
67  myNext(nullptr), myArraySize(0),
68  myBucket(bucket) {}
69 
70  ~RE_FontBuffers();
71 
72  void init(RE_Render *r, int size);
73 };
74 
76 {
77 public:
79  : myTexture(nullptr),
80  myTextGeo(nullptr),
81  myNumVertices(0),
82  myOwnFontBuffers(false)
83  {
84  }
86  {
87  if(myOwnFontBuffers)
88  delete myTextGeo;
89  // else myTextGeo is returned to a buffer pool
90  }
91 
93 
94  RE_Texture *myTexture;
95  RE_FontBuffers *myTextGeo;
96  int myNumVertices;
97  bool myOwnFontBuffers;
98 };
99 
100 
102 {
103 public:
104  static RE_Font &get(const FONT_Info &font_info, float size);
105  static RE_Font &get(const char *font_name, float size);
106  static RE_Font &get(RE_Font &font, FONT_Variant variant);
107  static RE_Font &emptyFont();
108 
109  bool isValid() const { return myFontValid; }
110 
111  /// Returns the full name of the font.
112  const char *getName() const;
113 
114  const FONT_Info &getFontInfo() const { return myFontInfo; }
115 
116  /// Get the size, in points, of this font.
117  float getSize() const { return myFontSize; }
118 
119  bool operator==(const RE_Font &other) const;
120  bool operator!=(const RE_Font &other) const
121  {
122  return !(operator==(other));
123  }
124 
125  /// @{
126  /// Font query
127 
128  /// Returns the height of the ascender, in pixels. The ascender is the
129  /// distance from the font's baseline to the top of the tallest glyph.
130  float getAscender();
131 
132  /// The descender is the distance, in pixels, from the baseline to the
133  /// bottom-most pixel of all glyphs in the font.
134  float getDescender();
135 
136  /// The height is the baseline-to-baseline distance for this font. This
137  /// should be used as the appropriate vertical spacing for the font.
138  float getHeight();
139 
140  /// Get the x-height. This is usually the distance from the baseline to
141  /// the top of the lower-case x.
142  float getXHeight();
143 
144  /// Returns the signed distance from the baseline to where the the
145  /// underline for the font should be drawn.
146  float getUnderlinePos();
147 
148  /// Returns the thickness of the underline to be drawn, corresponding with
149  /// the font's size and weight.
150  float getUnderlineThickness();
151 
152  /// Returns true if the font contains a glyph for the given code point.
153  bool hasGlyph(utf32 cp);
154 
155  /// Get the horizontal advance, in pixels, of the cursor, after drawing
156  /// the given code point's glyph.
157  /// NOTE: This ignores kerning! If you want to get the width of a whole
158  /// string, use getStringWidth instead.
159  float getHorizAdvance(utf32 cp);
160 
161  /// Get the horizontal advance, in pixels, of the cursor, after drawing
162  /// the given code point's glyph, and including it's kerning pair glyph.
163  float getHorizAdvance(utf32 cp, utf32 cp_next);
164 
165  /// Get the width of a string, including all kerning adjustments.
166  float getStringWidth(const utf8 *s, const utf8 *e = nullptr,
167  const UT_Unicode::transform *cp_xform = nullptr);
168  float getStringWidth(const UT_StringView &str,
169  const UT_Unicode::transform *cp_xform = nullptr)
170  {
171  return getStringWidth(str.begin(), str.end(), cp_xform);
172  }
173  float getStringWidth(const UT_String &str,
174  const UT_Unicode::transform *cp_xform = nullptr)
175  {
176  return getStringWidth(str.c_str(), nullptr, cp_xform);
177  }
178  float getStringWidth(const UT_StringRef &str,
179  const UT_Unicode::transform *cp_xform = nullptr)
180  {
181  return getStringWidth(str.begin(), str.end(), cp_xform);
182  }
183  float getStringWidth(const UT_WorkBuffer &str,
184  const UT_Unicode::transform *cp_xform = nullptr)
185  {
186  return getStringWidth(str.begin(), str.end(), cp_xform);
187  }
188 
189  /// Quick check to see if a string exceeds the given width. Usually faster
190  /// than calling getStringWidth if only the threshold check is required.
191  bool isStringWiderThan(const utf8 *s, const utf8 *e, float width,
192  const UT_Unicode::transform *cp_xform = nullptr);
193  bool isStringWiderThan(const UT_StringView &str, float width,
194  const UT_Unicode::transform *cp_xform = nullptr)
195  {
196  return isStringWiderThan(str.begin(), str.end(), width, cp_xform);
197  }
198 
199  /// Get the length of string that can be put in the given space, using
200  /// the provided ellipsis text to replace removed characters.
201  bool getTrimmedString(const utf8 *&s, const utf8 *&e, const utf8 *ellipsis,
202  float max_width, bool trim_right,
203  const UT_Unicode::transform *cp_xform = nullptr,
204  float *trimmed_width = nullptr);
205 
206  /// Split the string into two parts, at a breakable space or a breakable
207  /// hyphen, using the font's metric to ensure that the left part doesn't
208  /// exceed \c max_width. The particular splitting strategy, and whether
209  /// \c max_width is strictly honored, is governed by \c split_type.
210  /// If the splitting fails, this function returns \c false.
211  bool splitString(const UT_StringView &s,
212  UT_StringView &left, UT_StringView &right, float max_width,
214  const UT_Unicode::transform *cp_xform = nullptr);
215 
216  /// Takes an input string, and splits it up into a list of strings none
217  /// wider than \c max_width (depending on splitting strategy). The string
218  /// is initially split by newline characters, and then each sub-string
219  /// split using \c splitString to satisfy the maximum width requirement.
220  /// If \c max_height is given, then only as many lines as will fit in that
221  /// vertical space, assuming the lines are spaced out by the amount
222  /// returned by \c getHeight.
223  /// Returns \c false if it failed to fully wrap the input string.
224  bool wrapString(const UT_StringView &s, UT_StringViewArray &result,
225  float max_width, float max_height = FLT_MAX,
227  const UT_Unicode::transform *cp_xform = nullptr);
228 
229  /// Performs rendering of the render data generated by createFontDeferData.
230  static void renderFontDeferData(RE_Render *r,
231  UT_Array<RE_FontDeferData> &deferData);
232 
233  /// Builds directly renderable data structures from the provided set
234  /// of strings, fonts, positions, and colors.
235  static void createFontDeferData(RE_Render *r,
236  const UT_StringArray &strings,
237  const UT_Array<RE_Font *> &fonts,
238  const UT_Array<UT_Unicode::transform *> &xforms,
239  const UT_Array<UT_FRGBA> &clrs,
240  const UT_Array<UT_Vector3F> &positions,
241  fpreal32 sx, fpreal32 sy,
242  fpreal32 tx, fpreal32 ty,
243  UT_Array<RE_FontDeferData> &defer_data,
244  bool use_shared_font_buffers);
245 
246  /// Faster String width method for integers.
247  float getIntegerWidth( const char* s );
248 
249  /// Faster String width method for floats.
250  float getFloatWidth( const char* s );
251 
252 private:
253  struct PassKey {};
254 public:
255  RE_Font(const FONT_Info &font_info, float size, const PassKey &);
256  RE_Font(RE_Font &ref, float scale, const PassKey &);
257 
258 private:
259  RE_Font();
260 
261  RE_Font &fallbackFontForCodepoint(utf32 cp);
262 
263  bool ensureFont();
264  bool splitStringImpl(
265  const UT_StringView &s,
266  UT_StringView &left, UT_StringView &right, float max_width,
268  const UT_Unicode::transform *cp_xform = nullptr);
269 
270  const FONT_Info &myFontInfo;
271  re_FontInstance *myInstance;
272 
273  /// The size, in points, of this font.
274  float myFontSize;
275 
276  /// When we are a reference to another font, this value stores the
277  /// amount by which we want to scale the glyphs in that referenced font.
278  float myFontScale;
279 
280  bool myFontValid;
281 
282  friend class re_FontCache;
283  friend class RE_RenderUI;
284 };
285 #endif
RE_VertexArray * myUV
Definition: RE_Font.h:59
SYS_FORCE_INLINE const_iterator begin() const
float getStringWidth(const UT_StringView &str, const UT_Unicode::transform *cp_xform=nullptr)
The size, in points, of this font.
Definition: RE_Font.h:168
#define RE_API
Definition: RE_API.h:10
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
RE_FontBuffers * myNext
Definition: RE_Font.h:62
GLint left
Definition: glcorearb.h:2005
GLdouble right
Definition: glad.h:2817
float getSize() const
Get the size, in points, of this font.
Definition: RE_Font.h:117
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE const_iterator end() const
Returns a constant iterator pointing to the end of the string.
GLdouble s
Definition: glad.h:3009
**But if you need a result
Definition: thread.h:622
float fpreal32
Definition: SYS_Types.h:200
const char * c_str() const
Definition: UT_String.h:515
SYS_FORCE_INLINE const_iterator end() const
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
GA_API const UT_StringHolder scale
A utility class to do read-only operations on a subset of an existing string.
Definition: UT_StringView.h:40
bool isValid() const
Definition: RE_Font.h:109
GLint ref
Definition: glcorearb.h:124
SYS_FORCE_INLINE const char * end() const
PXL_API const char * getName(const ColorSpace *space)
Return the name of the color space.
unsigned int utf32
Definition: SYS_Types.h:58
SYS_FORCE_INLINE const char * begin() const
Iterator compatibility.
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
RE_FontBuffers(int bucket)
Definition: RE_Font.h:65
FONT_Variant
Definition: FONT_Enums.h:19
GLsizei const GLchar *const * strings
Definition: glcorearb.h:1933
RE_StringSplitType
Specifies which method to use to split string in RE_Font::splitString.
Definition: RE_Font.h:38
GLsizeiptr size
Definition: glcorearb.h:664
bool operator!=(const RE_Font &other) const
Definition: RE_Font.h:120
int myArraySize
Definition: RE_Font.h:61
RE_VertexArray * myP
Definition: RE_Font.h:58
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE const_iterator begin() const
Returns a constant iterator pointing to the beginning of the string.
GLint GLsizei width
Definition: glcorearb.h:103
float getStringWidth(const UT_StringRef &str, const UT_Unicode::transform *cp_xform=nullptr)
The size, in points, of this font.
Definition: RE_Font.h:178
char utf8
Definition: SYS_Types.h:52
float getStringWidth(const UT_String &str, const UT_Unicode::transform *cp_xform=nullptr)
The size, in points, of this font.
Definition: RE_Font.h:173
GLboolean r
Definition: glcorearb.h:1222
const int myBucket
Definition: RE_Font.h:63
const FONT_Info & getFontInfo() const
Definition: RE_Font.h:114
RE_VertexArray * myCd
Definition: RE_Font.h:60
float getStringWidth(const UT_WorkBuffer &str, const UT_Unicode::transform *cp_xform=nullptr)
The size, in points, of this font.
Definition: RE_Font.h:183
MX_CORE_API StringVec splitString(const string &str, const string &sep)
bool isStringWiderThan(const UT_StringView &str, float width, const UT_Unicode::transform *cp_xform=nullptr)
The size, in points, of this font.
Definition: RE_Font.h:193