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_Pixel.h>
21 #include <UT/UT_Rect.h>
22 #include <UT/UT_UniquePtr.h>
23 #include <UT/UT_String.h>
24 #include <UT/UT_StringView.h>
25 #include <UT/UT_Unicode.h>
26 #include <SYS/SYS_Math.h>
27 
28 class FONT_Info;
29 class re_FontCache;
30 class re_FontInstance;
31 class re_FontBuffers;
32 class RE_Render;
33 class RE_VertexArray;
34 class UT_WorkBuffer;
35 
36 /// Specifies which method to use to split string in RE_Font::splitString.
38 {
39  RE_SST_NARROWER_ONLY, ///< Only split at word boundaries if narrower
40  /// than the given maximum width. If no split
41  /// point was found before, the splitting
42  /// fails.
43  RE_SST_BOUNDARY_SOFT, ///< Try first to split on word boundaries at
44  /// less than the maximum width, otherwise
45  /// try word boundaries after.
46  RE_SST_BOUNDARY_HARD, ///< Try first to split on word boundaries at
47  /// less than maximum width, otherwise do a
48  /// hard split at the maximum width, ignoring
49  /// any boundary indicators.
50 };
51 
53 {
54 public:
55  enum { BUFFER_CHUNK_SIZE = 4096 };
56 
62  const int myBucket;
63 
64  RE_FontBuffers(int bucket)
65  : myP(nullptr), myUV(nullptr), myCd(nullptr),
66  myNext(nullptr), myArraySize(0),
67  myBucket(bucket) {}
68 
69  ~RE_FontBuffers();
70 
71  void init(RE_Render *r, int size);
72 };
73 
75 {
76 public:
78  : myTexture(nullptr),
79  myTextGeo(nullptr),
80  myNumVertices(0),
81  myOwnFontBuffers(false)
82  {
83  }
85  {
86  if(myOwnFontBuffers)
87  delete myTextGeo;
88  // else myTextGeo is returned to a buffer pool
89  }
90 
95 };
96 
97 
99 {
100 public:
101  static RE_Font &get(const FONT_Info &font_info, float size);
102  static RE_Font &get(const char *font_name, float size);
103  static RE_Font &get(RE_Font &font, FONT_Variant variant);
104  static RE_Font &emptyFont();
105 
106  bool isValid() const { return myFontValid; }
107 
108  /// Returns the full name of the font.
109  const char *getName() const;
110 
111  const FONT_Info &getFontInfo() const { return myFontInfo; }
112 
113  /// Get the size, in points, of this font.
114  float getSize() const { return myFontSize; }
115 
116  bool operator==(const RE_Font &other) const;
117  bool operator!=(const RE_Font &other) const
118  {
119  return !(operator==(other));
120  }
121 
122  /// @{
123  /// Font query
124 
125  /// Returns the height of the ascender, in pixels. The ascender is the
126  /// distance from the font's baseline to the top of the tallest glyph.
127  float getAscender();
128 
129  /// The descender is the distance, in pixels, from the baseline to the
130  /// bottom-most pixel of all glyphs in the font.
131  float getDescender();
132 
133  /// The height is the baseline-to-baseline distance for this font. This
134  /// should be used as the appropriate vertical spacing for the font.
135  float getHeight();
136 
137  /// Get the x-height. This is usually the distance from the baseline to
138  /// the top of the lower-case x.
139  float getXHeight();
140 
141  /// Returns the signed distance from the baseline to where the the
142  /// underline for the font should be drawn.
143  float getUnderlinePos();
144 
145  /// Returns the thickness of the underline to be drawn, corresponding with
146  /// the font's size and weight.
147  float getUnderlineThickness();
148 
149  /// Returns true if the font contains a glyph for the given code point.
150  bool hasGlyph(utf32 cp);
151 
152  /// Get the horizontal advance, in pixels, of the cursor, after drawing
153  /// the given code point's glyph.
154  /// NOTE: This ignores kerning! If you want to get the width of a whole
155  /// string, use getStringWidth instead.
156  float getHorizAdvance(utf32 cp);
157 
158  /// Get the horizontal advance, in pixels, of the cursor, after drawing
159  /// the given code point's glyph, and including it's kerning pair glyph.
160  float getHorizAdvance(utf32 cp, utf32 cp_next);
161 
162  /// Get the width of a string, including all kerning adjustments.
163  float getStringWidth(const utf8 *s, const utf8 *e = nullptr,
164  const UT_Unicode::transform *cp_xform = nullptr);
165  float getStringWidth(const UT_StringView &str,
166  const UT_Unicode::transform *cp_xform = nullptr)
167  {
168  return getStringWidth(str.begin(), str.end(), cp_xform);
169  }
170  float getStringWidth(const UT_String &str,
171  const UT_Unicode::transform *cp_xform = nullptr)
172  {
173  return getStringWidth(str.c_str(), nullptr, cp_xform);
174  }
175  float getStringWidth(const UT_StringRef &str,
176  const UT_Unicode::transform *cp_xform = nullptr)
177  {
178  return getStringWidth(str.begin(), str.end(), cp_xform);
179  }
180  float getStringWidth(const UT_WorkBuffer &str,
181  const UT_Unicode::transform *cp_xform = nullptr)
182  {
183  return getStringWidth(str.begin(), str.end(), cp_xform);
184  }
185 
186  /// Quick check to see if a string exceeds the given width. Usually faster
187  /// than calling getStringWidth if only the threshold check is required.
188  bool isStringWiderThan(const utf8 *s, const utf8 *e, float width,
189  const UT_Unicode::transform *cp_xform = nullptr);
190  bool isStringWiderThan(const UT_StringView &str, float width,
191  const UT_Unicode::transform *cp_xform = nullptr)
192  {
193  return isStringWiderThan(str.begin(), str.end(), width, cp_xform);
194  }
195 
196  /// Get the length of string that can be put in the given space, using
197  /// the provided ellipsis text to replace removed characters.
198  bool getTrimmedString(const utf8 *&s, const utf8 *&e, const utf8 *ellipsis,
199  float max_width, bool trim_right,
200  const UT_Unicode::transform *cp_xform = nullptr,
201  float *trimmed_width = nullptr);
202 
203  /// Split the string into two parts, at a breakable space or a breakable
204  /// hyphen, using the font's metric to ensure that the left part doesn't
205  /// exceed \c max_width. The particular splitting strategy, and whether
206  /// \c max_width is strictly honored, is governed by \c split_type.
207  /// If the splitting fails, this function returns \c false.
208  bool splitString(const UT_StringView &s,
209  UT_StringView &left, UT_StringView &right, float max_width,
211  const UT_Unicode::transform *cp_xform = nullptr);
212 
213  /// Takes an input string, and splits it up into a list of strings none
214  /// wider than \c max_width (depending on splitting strategy). The string
215  /// is initially split by newline characters, and then each sub-string
216  /// split using \c splitString to satisfy the maximum width requirement.
217  /// If \c max_height is given, then only as many lines as will fit in that
218  /// vertical space, assuming the lines are spaced out by the amount
219  /// returned by \c getHeight.
220  /// Returns \c false if it failed to fully wrap the input string.
221  bool wrapString(const UT_StringView &s, UT_StringViewArray &result,
222  float max_width, float max_height = FLT_MAX,
224  const UT_Unicode::transform *cp_xform = nullptr);
225 
226  /// Performs rendering of the render data generated by createFontDeferData.
227  static void renderFontDeferData(RE_Render *r,
228  UT_Array<RE_FontDeferData> &deferData);
229 
230  /// Builds directly renderable data structures from the provided set
231  /// of strings, fonts, positions, and colors.
232  static void createFontDeferData(RE_Render *r,
233  const UT_StringArray &strings,
234  const UT_Array<RE_Font *> &fonts,
235  const UT_Array<UT_Unicode::transform *> &xforms,
236  const UT_Array<UT_FRGBA> &clrs,
237  const UT_Array<UT_Vector3F> &positions,
238  fpreal32 sx, fpreal32 sy,
239  fpreal32 tx, fpreal32 ty,
240  UT_Array<RE_FontDeferData> &defer_data,
241  bool use_shared_font_buffers);
242 
243  /// Faster String width method for integers.
244  float getIntegerWidth( const char* s );
245 
246  /// Faster String width method for floats.
247  float getFloatWidth( const char* s );
248 
249 private:
250  RE_Font(const FONT_Info &font_info, float size);
251  RE_Font(RE_Font &ref, float scale);
252  RE_Font();
253 
254  RE_Font &fallbackFontForCodepoint(utf32 cp);
255 
256  bool ensureFont();
257  bool splitStringImpl(
258  const UT_StringView &s,
259  UT_StringView &left, UT_StringView &right, float max_width,
261  const UT_Unicode::transform *cp_xform = nullptr);
262 
263  const FONT_Info &myFontInfo;
264  re_FontInstance *myInstance;
265 
266  /// The size, in points, of this font.
267  float myFontSize;
268 
269  /// When we are a reference to another font, this value stores the
270  /// amount by which we want to scale the glyphs in that referenced font.
271  float myFontScale;
272 
273  bool myFontValid;
274 
275  friend class re_FontCache;
276  friend class RE_RenderUI;
277 };
278 #endif
RE_VertexArray * myUV
Definition: RE_Font.h:58
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:165
#define RE_API
Definition: RE_API.h:10
RE_FontBuffers * myNext
Definition: RE_Font.h:61
bool myOwnFontBuffers
Definition: RE_Font.h:94
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:114
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:613
float fpreal32
Definition: SYS_Types.h:200
const char * c_str() const
Definition: UT_String.h:508
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:39
bool isValid() const
Definition: RE_Font.h:106
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.
int myNumVertices
Definition: RE_Font.h:93
RE_FontBuffers(int bucket)
Definition: RE_Font.h:64
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:37
GLsizeiptr size
Definition: glcorearb.h:664
RE_Texture * myTexture
Definition: RE_Font.h:91
bool operator!=(const RE_Font &other) const
Definition: RE_Font.h:117
int myArraySize
Definition: RE_Font.h:60
RE_VertexArray * myP
Definition: RE_Font.h:57
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:175
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:170
GLboolean r
Definition: glcorearb.h:1222
const int myBucket
Definition: RE_Font.h:62
RE_FontBuffers * myTextGeo
Definition: RE_Font.h:92
const FONT_Info & getFontInfo() const
Definition: RE_Font.h:111
RE_VertexArray * myCd
Definition: RE_Font.h:59
float getStringWidth(const UT_WorkBuffer &str, const UT_Unicode::transform *cp_xform=nullptr)
The size, in points, of this font.
Definition: RE_Font.h:180
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:190