HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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(NULL), myUV(NULL), myCd(NULL),
66  myNext(NULL), myArraySize(0),
67  myBucket(bucket) {}
68 
70 
71  void init(RE_Render *r, int size);
72 };
73 
75 {
76 public:
78  : myTexture(NULL),
79  myTextGeo(NULL),
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 = NULL,
164  const UT_Unicode::transform *cp_xform = NULL);
165  float getStringWidth(const UT_StringView &str,
166  const UT_Unicode::transform *cp_xform = NULL)
167  {
168  return getStringWidth(str.begin(), str.end(), cp_xform);
169  }
170 
171  /// Quick check to see if a string exceeds the given width. Usually faster
172  /// than calling getStringWidth if only the threshold check is required.
173  bool isStringWiderThan(const utf8 *s, const utf8 *e, float width,
174  const UT_Unicode::transform *cp_xform = NULL);
175  bool isStringWiderThan(const UT_StringView &str, float width,
176  const UT_Unicode::transform *cp_xform = NULL)
177  {
178  return isStringWiderThan(str.begin(), str.end(), width, cp_xform);
179  }
180 
181  /// Get the length of string that can be put in the given space, using
182  /// the provided ellipsis text to replace removed characters.
183  bool getTrimmedString(const utf8 *&s, const utf8 *&e, const utf8 *ellipsis,
184  float max_width, bool trim_right,
185  const UT_Unicode::transform *cp_xform = NULL,
186  float *trimmed_width = NULL);
187 
188  /// Split the string into two parts, at a breakable space or a breakable
189  /// hyphen, using the font's metric to ensure that the left part doesn't
190  /// exceed \c max_width. The particular splitting strategy, and whether
191  /// \c max_width is strictly honored, is governed by \c split_type.
192  /// If the splitting fails, this function returns \c false.
193  bool splitString(const UT_StringView &s,
194  UT_StringView &left, UT_StringView &right, float max_width,
196  const UT_Unicode::transform *cp_xform = NULL);
197 
198  /// Takes an input string, and splits it up into a list of strings none
199  /// wider than \c max_width (depending on splitting strategy). The string
200  /// is initially split by newline characters, and then each sub-string
201  /// split using \c splitString to satisfy the maximum width requirement.
202  /// If \c max_height is given, then only as many lines as will fit in that
203  /// vertical space, assuming the lines are spaced out by the amount
204  /// returned by \c getHeight.
205  /// Returns \c false if it failed to fully wrap the input string.
206  bool wrapString(const UT_StringView &s, UT_StringViewArray &result,
207  float max_width, float max_height = FLT_MAX,
209  const UT_Unicode::transform *cp_xform = NULL);
210 
211  /// Performs rendering of the render data generated by createFontDeferData.
212  static void renderFontDeferData(RE_Render *r,
213  UT_Array<RE_FontDeferData> &deferData);
214 
215  /// Builds directly renderable data structures from the provided set
216  /// of strings, fonts, positions, and colors.
217  static void createFontDeferData(RE_Render *r,
218  const UT_StringArray &strings,
219  const UT_Array<RE_Font *> &fonts,
220  const UT_Array<UT_Unicode::transform *> &xforms,
221  const UT_Array<UT_FRGBA> &clrs,
222  const UT_Array<UT_Vector3F> &positions,
223  fpreal32 sx, fpreal32 sy,
224  fpreal32 tx, fpreal32 ty,
225  UT_Array<RE_FontDeferData> &defer_data,
226  bool use_shared_font_buffers);
227 
228  /// Faster String width method for integers.
229  float getIntegerWidth( const char* s );
230 
231  /// Faster String width method for floats.
232  float getFloatWidth( const char* s );
233 
234 private:
235  RE_Font(const FONT_Info &font_info, float size);
236  RE_Font(RE_Font &ref, float scale);
237  RE_Font();
238 
239  RE_Font &fallbackFontForCodepoint(utf32 cp);
240 
241  bool ensureFont();
242 
243  const FONT_Info &myFontInfo;
244  re_FontInstance *myInstance;
245 
246  /// The size, in points, of this font.
247  float myFontSize;
248 
249  /// When we are a reference to another font, this value stores the
250  /// amount by which we want to scale the glyphs in that referenced font.
251  float myFontScale;
252 
253  bool myFontValid;
254 
255  friend class re_FontCache;
256  friend class RE_RenderUI;
257 };
258 #endif
RE_VertexArray * myUV
Definition: RE_Font.h:58
float getStringWidth(const UT_StringView &str, const UT_Unicode::transform *cp_xform=NULL)
The size, in points, of this font.
Definition: RE_Font.h:165
char utf8
Definition: SYS_Types.h:40
unsigned int utf32
Definition: SYS_Types.h:42
#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:2004
float getSize() const
Get the size, in points, of this font.
Definition: RE_Font.h:114
const_iterator begin() const
Returns a constant iterator pointing to the beginning of the string.
GLsizeiptr size
Definition: glcorearb.h:663
GLint GLsizei width
Definition: glcorearb.h:102
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:30
bool isValid() const
Definition: RE_Font.h:106
GLint ref
Definition: glcorearb.h:123
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:1932
RE_StringSplitType
Specifies which method to use to split string in RE_Font::splitString.
Definition: RE_Font.h:37
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
void init(RE_Render *r, int size)
bool isStringWiderThan(const UT_StringView &str, float width, const UT_Unicode::transform *cp_xform=NULL)
The size, in points, of this font.
Definition: RE_Font.h:175
RE_VertexArray * myP
Definition: RE_Font.h:57
GLboolean r
Definition: glcorearb.h:1221
const_iterator end() const
Returns a constant iterator pointing to the end of the string.
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
float fpreal32
Definition: SYS_Types.h:184
RE_VertexArray * myCd
Definition: RE_Font.h:59