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  static void setDefaultUIFont(RE_Font *font);
110  static RE_Font *getDefaultUIFont();
111  static bool isDefaultUIFont(const RE_Font &font);
112 
113  bool isValid() const { return myFontValid; }
114 
115  /// Returns the full name of the font.
116  const char *getName() const;
117 
118  const FONT_Info &getFontInfo() const { return myFontInfo; }
119 
120  /// Get the size, in points, of this font.
121  float getSize() const { return myFontSize; }
122 
123  bool operator==(const RE_Font &other) const;
124  bool operator!=(const RE_Font &other) const
125  {
126  return !(operator==(other));
127  }
128 
129  /// @{
130  /// Font query
131 
132  /// Returns the height of the ascender, in pixels. The ascender is the
133  /// distance from the font's baseline to the top of the tallest glyph.
134  float getAscender();
135 
136  /// The descender is the distance, in pixels, from the baseline to the
137  /// bottom-most pixel of all glyphs in the font.
138  float getDescender();
139 
140  /// The height is the baseline-to-baseline distance for this font. This
141  /// should be used as the appropriate vertical spacing for the font.
142  float getHeight();
143 
144  /// Get the x-height. This is usually the distance from the baseline to
145  /// the top of the lower-case x.
146  float getXHeight();
147 
148  /// Returns the signed distance from the baseline to where the the
149  /// underline for the font should be drawn.
150  float getUnderlinePos();
151 
152  /// Returns the thickness of the underline to be drawn, corresponding with
153  /// the font's size and weight.
154  float getUnderlineThickness();
155 
156  /// Returns true if the font contains a glyph for the given code point.
157  bool hasGlyph(utf32 cp);
158 
159  /// Get the horizontal advance, in pixels, of the cursor, after drawing
160  /// the given code point's glyph.
161  /// NOTE: This ignores kerning! If you want to get the width of a whole
162  /// string, use getStringWidth instead.
163  float getHorizAdvance(utf32 cp);
164 
165  /// Get the horizontal advance, in pixels, of the cursor, after drawing
166  /// the given code point's glyph, and including it's kerning pair glyph.
167  float getHorizAdvance(utf32 cp, utf32 cp_next);
168 
169  /// Get the width of a string, including all kerning adjustments.
170  float getStringWidth(const utf8 *s, const utf8 *e = nullptr,
171  const UT_Unicode::transform *cp_xform = nullptr);
172  float getStringWidth(const UT_StringView &str,
173  const UT_Unicode::transform *cp_xform = nullptr)
174  {
175  return getStringWidth(str.begin(), str.end(), cp_xform);
176  }
177  float getStringWidth(const UT_String &str,
178  const UT_Unicode::transform *cp_xform = nullptr)
179  {
180  return getStringWidth(str.c_str(), nullptr, cp_xform);
181  }
182  float getStringWidth(const UT_StringRef &str,
183  const UT_Unicode::transform *cp_xform = nullptr)
184  {
185  return getStringWidth(str.begin(), str.end(), cp_xform);
186  }
187  float getStringWidth(const UT_WorkBuffer &str,
188  const UT_Unicode::transform *cp_xform = nullptr)
189  {
190  return getStringWidth(str.begin(), str.end(), cp_xform);
191  }
192 
193  /// Quick check to see if a string exceeds the given width. Usually faster
194  /// than calling getStringWidth if only the threshold check is required.
195  bool isStringWiderThan(const utf8 *s, const utf8 *e, float width,
196  const UT_Unicode::transform *cp_xform = nullptr);
197  bool isStringWiderThan(const UT_StringView &str, float width,
198  const UT_Unicode::transform *cp_xform = nullptr)
199  {
200  return isStringWiderThan(str.begin(), str.end(), width, cp_xform);
201  }
202 
203  /// Get the length of string that can be put in the given space, using
204  /// the provided ellipsis text to replace removed characters.
205  bool getTrimmedString(const utf8 *&s, const utf8 *&e, const utf8 *ellipsis,
206  float max_width, bool trim_right,
207  const UT_Unicode::transform *cp_xform = nullptr,
208  float *trimmed_width = nullptr);
209 
210  /// Split the string into two parts, at a breakable space or a breakable
211  /// hyphen, using the font's metric to ensure that the left part doesn't
212  /// exceed \c max_width. The particular splitting strategy, and whether
213  /// \c max_width is strictly honored, is governed by \c split_type.
214  /// If the splitting fails, this function returns \c false.
215  bool splitString(const UT_StringView &s,
216  UT_StringView &left, UT_StringView &right, float max_width,
218  const UT_Unicode::transform *cp_xform = nullptr);
219 
220  /// Takes an input string, and splits it up into a list of strings none
221  /// wider than \c max_width (depending on splitting strategy). The string
222  /// is initially split by newline characters, and then each sub-string
223  /// split using \c splitString to satisfy the maximum width requirement.
224  /// If \c max_height is given, then only as many lines as will fit in that
225  /// vertical space, assuming the lines are spaced out by the amount
226  /// returned by \c getHeight.
227  /// Returns \c false if it failed to fully wrap the input string.
228  bool wrapString(const UT_StringView &s, UT_StringViewArray &result,
229  float max_width, float max_height = FLT_MAX,
231  const UT_Unicode::transform *cp_xform = nullptr);
232 
233  /// Performs rendering of the render data generated by createFontDeferData.
234  static void renderFontDeferData(RE_Render *r,
235  UT_Array<RE_FontDeferData> &deferData);
236 
237  /// Builds directly renderable data structures from the provided set
238  /// of strings, fonts, positions, and colors.
239  static void createFontDeferData(RE_Render *r,
240  const UT_StringArray &strings,
241  const UT_Array<RE_Font *> &fonts,
242  const UT_Array<UT_Unicode::transform *> &xforms,
243  const UT_Array<UT_FRGBA> &clrs,
244  const UT_Array<UT_Vector3F> &positions,
245  fpreal32 sx, fpreal32 sy,
246  fpreal32 tx, fpreal32 ty,
247  UT_Array<RE_FontDeferData> &defer_data,
248  bool use_shared_font_buffers);
249 
250  /// Faster String width method for integers.
251  float getIntegerWidth( const char* s );
252 
253  /// Faster String width method for floats.
254  float getFloatWidth( const char* s );
255 
256 private:
257  struct PassKey {};
258 public:
259  RE_Font(const FONT_Info &font_info, float size, const PassKey &);
260  RE_Font(RE_Font &ref, float scale, const PassKey &);
261 
262 private:
263  RE_Font();
264 
265  RE_Font &fallbackFontForCodepoint(utf32 cp);
266 
267  bool ensureFont();
268  bool splitStringImpl(
269  const UT_StringView &s,
270  UT_StringView &left, UT_StringView &right, float max_width,
272  const UT_Unicode::transform *cp_xform = nullptr);
273 
274  const FONT_Info &myFontInfo;
275  re_FontInstance *myInstance;
276 
277  /// The size, in points, of this font.
278  float myFontSize;
279 
280  /// When we are a reference to another font, this value stores the
281  /// amount by which we want to scale the glyphs in that referenced font.
282  float myFontScale;
283 
284  bool myFontValid;
285 
286  friend class re_FontCache;
287  friend class RE_RenderUI;
288 };
289 #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:172
#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:121
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:524
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:113
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:124
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:182
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:177
GLboolean r
Definition: glcorearb.h:1222
const int myBucket
Definition: RE_Font.h:63
const FONT_Info & getFontInfo() const
Definition: RE_Font.h:118
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:187
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:197