FlatUI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Enumerations Groups Pages
font_buffer.h
Go to the documentation of this file.
1 // Copyright 2017 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FONT_BUFFER_H
16 #define FONT_BUFFER_H
17 
18 #include "flatui/internal/flatui_util.h"
19 #include "flatui/internal/glyph_cache.h"
20 #include "flatui/internal/hb_complex_font.h"
21 
22 /// @brief Namespace for FlatUI library.
23 namespace flatui {
24 
25 /// @file
26 /// @addtogroup flatui_font_manager
27 /// @{
28 
29 /// @cond FLATUI_INTERNAL
30 // Forward decl.
31 class FontManager;
32 /// @endcond
33 
34 /// @var kFreeTypeUnit
35 ///
36 /// @brief Constant to convert FreeType unit to pixel unit.
37 ///
38 /// In FreeType & Harfbuzz, the position value unit is 1/64 px whereas
39 /// configurable in FlatUI. The constant is used to convert FreeType unit
40 /// to px.
41 const int32_t kFreeTypeUnit = 64;
42 
43 /// @var kLineHeightDefault
44 ///
45 /// @brief Default value for a line height factor.
46 ///
47 /// The line height is derived as the factor * a font height.
48 /// To change the line height, use `SetLineHeightScale()` API.
49 const float kLineHeightDefault = 1.2f;
50 
51 /// @var kKerningScaleDefault
52 ///
53 /// @brief Default value for a kerning scale factor.
54 ///
55 /// The kerning value is derived as the factor * kerning value retrieved from
56 /// harfbuzz. To change the kerning scale, use `SetKerningScale()` API.
57 const float kKerningScaleDefault = 1.0f;
58 
59 /// @var kCaretPositionInvalid
60 ///
61 /// @brief A sentinel value representing an invalid caret position.
62 const mathfu::vec2i kCaretPositionInvalid = mathfu::vec2i(-1, -1);
63 
64 // Constant that indicates invalid index.
65 const int32_t kIndexInvalid = -1;
66 
67 // Constant that indicates default color of the attributed buffer.
68 const uint32_t kDefaultColor = 0xffffffff;
69 
70 /// @enum TextLayoutDirection
71 ///
72 /// @brief Specify how to layout texts.
73 /// Default value is TextLayoutDirectionLTR.
74 ///
76  kTextLayoutDirectionLTR = 0,
77  kTextLayoutDirectionRTL = 1,
78  kTextLayoutDirectionTTB = 2,
79 };
80 
81 /// @enum TextAlignment
82 ///
83 /// @brief Alignment of the text.
84 ///
85 /// @note: Used for a typographic alignment in a label.
86 /// The enumeration is different from flatui::Alignment as it supports
87 /// justification setting.
88 /// Note that in RTL layout direction mode, the setting is flipped.
89 /// (e.g. kTextAlignmentLeft becomes kTextAlignmentRight)
90 /// kTextAlignLeft/Right/CenterJustify variants specify how the last line is
91 /// flushed left, right or centered.
92 ///
93 /// **Enumerations**:
94 /// * `kTextAlignmentLeft` - Text is aligned to the left of the given area.
95 /// Default setting.
96 /// * `kTextAlignmentRight` - Text is aligned to the right of the given area.
97 /// * `kTextAlignmentCenter` - Text is aligned to the center of the given
98 /// area.
99 /// * `kTextAlignmentJustify` - Text is 'justified'. Spaces between words are
100 /// stretched to align both the left and right ends of each line of text.
101 /// * `kTextAlignmentLeftJustify` - An alias of kTextAlignmentJustify. The last
102 /// line of a paragraph is aligned to the left.
103 /// * `kTextAlignmentRightJustify` - Text is 'justified'. The last line of a
104 /// paragraph is aligned to the right.
105 /// * `kTextAlignmentCenterJustify` - Text is 'justified'. The last line of a
106 /// paragraph is aligned to the center.
107 ///
108 /// For more detail of each settings, refer:
109 /// https://en.wikipedia.org/wiki/Typographic_alignment
111  kTextAlignmentLeft = 0,
112  kTextAlignmentRight = 1,
113  kTextAlignmentCenter = 2,
114  kTextAlignmentJustify = 4,
115  kTextAlignmentLeftJustify = kTextAlignmentJustify,
116  kTextAlignmentRightJustify = kTextAlignmentJustify | kTextAlignmentRight,
117  kTextAlignmentCenterJustify = kTextAlignmentJustify | kTextAlignmentCenter,
118 };
119 
120 /// @enum FontBufferStatus
121 /// @brief A status of FontBuffer correspoinding current glyph cache contents.
122 /// **Enumerations**:
123 /// * `kFontBufferStatusReady` - The font buffer is ready to use.
124 /// * `kFontBufferStatusNeedReconstruct` - The glyph cache has been flushed.
125 /// * The font buffer needs to be reconstructed.
126 /// * `kFontBufferStatusNeedCacheUpdate` - The glyph cache texture needs to be
127 /// * uploaded.
129  kFontBufferStatusReady = 0,
130  kFontBufferStatusNeedReconstruct = 1,
131  kFontBufferStatusNeedCacheUpdate = 2,
132 };
133 
134 /// @enum EllipsisMode
135 /// @brief A flag controlling appending behavior of the ellipsis.
136 /// **Enumerations**:
137 /// * `kEllipsisModeTruncateCharacter` - Truncate characters to make a space.
138 /// * `kEllipsisModeTruncateWord` - Truncate whole word to make a space.
140  kEllipsisModeTruncateCharacter = 0,
141  kEllipsisModeTruncateWord = 1,
142 };
143 
144 /// @struct FontFamily
145 ///
146 /// @brief A class holding font family information. The class provides various
147 /// ways to support fonts such as a font collection (multiple fonts in a file),
148 /// referencing a font by a famly name etc.
149 class FontFamily {
150  public:
151  // Constructors
152  FontFamily(const std::string &name, int32_t index, const std::string &lang,
153  bool family_name)
154  : index_(index), family_name_(family_name) {
155  // Normalize the font name.
156  font_name_ = NormalizeFontName(name);
157  lang_ = lang;
158  if (is_font_collection()) {
159  font_name_ = CreateFontCollectionName(font_name_);
160  }
161  original_name_ = name;
162  };
163  FontFamily(const char *name, bool family_name)
164  : index_(kIndexInvalid), family_name_(family_name) {
165  font_name_ = NormalizeFontName(name);
166  original_name_ = name;
167  }
168  FontFamily(const char *name) : index_(kIndexInvalid), family_name_(false) {
169  font_name_ = NormalizeFontName(name);
170  original_name_ = name;
171  };
172  FontFamily(const std::string &name)
173  : index_(kIndexInvalid), family_name_(false) {
174  font_name_ = NormalizeFontName(name);
175  original_name_ = name;
176  };
177 
178  // Accessors to members.
179  /// Font name. When the font is in a font collection file, the name is mangled
180  /// with a font collection index.
181  const std::string &get_name() const { return font_name_; }
182  /// Original font name. When family_name_ is set, it's treated as a family
183  /// name.
184  const std::string &get_original_name() const { return original_name_; }
185  /// Language. The entry is ignored when opening a font.
186  const std::string &get_language() const { return lang_; }
187  /// Index in a font collection. kIndexInvalid indicates the font is not a
188  /// font collection.
189  int32_t get_index() const { return index_; }
190  /// Check if the font name is a family name.
191  bool is_family_name() const { return family_name_; }
192  /// Check if the font is in a font collection that holds multiple fonts in a
193  /// single file.
194  bool is_font_collection() const { return index_ != kIndexInvalid; }
195 
196  protected:
197  std::string CreateFontCollectionName(const std::string &name) {
198  // Tweak the file name with a font collection index and return it.
199  std::stringstream ss;
200  ss << "#" << index_;
201  return name + ss.str();
202  }
203 
204  static std::string NormalizeFontName(const std::string &name) {
205  std::size_t found = name.find_last_of("/\\");
206  if (found == std::string::npos) {
207  return name;
208  }
209  return name.substr(found + 1);
210  }
211 
212  std::string original_name_;
213  std::string font_name_;
214  std::string lang_;
215  int32_t index_;
216  bool family_name_;
217 };
218 
219 /// @class FontBufferParameters
220 ///
221 /// @brief This class that includes font buffer parameters. It is used as a key
222 /// in the unordered_map to look up FontBuffer.
224  public:
225  /// @brief The default constructor for an empty FontBufferParameters.
227  : font_id_(kNullHash),
228  text_id_(kNullHash),
229  cache_id_(kNullHash),
230  font_size_(0),
231  size_(mathfu::kZeros2i),
232  kerning_scale_(kKerningScaleDefault),
233  line_height_scale_(kLineHeightDefault),
234  flags_value_(0) {}
235 
236  /// @brief Constructor for a FontBufferParameters.
237  ///
238  /// @param[in] font_id The HashedId for the font.
239  /// @param[in] text_id The HashedID for the text.
240  /// @param[in] font_size A float representing the size of the font.
241  /// @param[in] size The requested size of the FontBuffer in pixels.
242  /// The created FontBuffer size can be smaller than requested size as the
243  /// FontBuffer size is calcuated by the layout and rendering result.
244  /// @param[in] text_alignment A horizontal alignment of multi line
245  /// buffer.
246  /// @param[in] kerning_scale Value indicating kerning scale.
247  /// @param[in] line_height_scale Value indicating line height scale.
248  /// @param[in] glyph_flags A flag determing SDF generation for the font.
249  /// @param[in] caret_info A bool determining if the FontBuffer contains caret
250  /// info.
251  /// @param[in] ref_count A bool determining if the FontBuffer manages
252  /// reference counts of the buffer and referencing glyph cache rows.
253  /// @param[in] enable_hyphenation A bool determining if the hyphenation is
254  /// enabled for the FontBuffer.
255  /// @param[in] rtl_layout A bool determining if the FontBuffer is for RTL
256  /// (right to left) layout.
257  /// @param[in] kerning_scale A float value specifying a scale applied to the
258  /// kerning value between glyphs. Default value is kKerningScaleDefault(1.0).
259  /// @param[in] line_height_scale A float value specifying a scale applied to
260  /// the line height for each line break. Default value is
261  /// kLineHeightDefault(1.0).
262  /// @param[in] cache_id An extra ID to distinguish cache entry. For instance,
263  /// tweak the ID to have multiple FontBuffer instances with the same set of
264  /// parameters.
265  FontBufferParameters(HashedId font_id, HashedId text_id, float font_size,
266  const mathfu::vec2i &size, TextAlignment text_alignment,
267  GlyphFlags glyph_flags, bool caret_info, bool ref_count,
268  bool enable_hyphenation = false, bool rtl_layout = false,
269  float kerning_scale = kKerningScaleDefault,
270  float line_height_scale = kLineHeightDefault,
271  HashedId cache_id = kNullHash) {
272  font_id_ = font_id;
273  text_id_ = text_id;
274  cache_id_ = cache_id;
275  font_size_ = font_size;
276  kerning_scale_ = kerning_scale;
277  line_height_scale_ = line_height_scale;
278  size_ = size;
279  flags_value_ = 0;
280  flags_.text_alignement = text_alignment;
281  flags_.glyph_flags = glyph_flags;
282  flags_.caret_info = caret_info;
283  flags_.ref_count = ref_count;
284  flags_.rtl_layout = rtl_layout;
285  flags_.enable_hyphenation = enable_hyphenation;
286  }
287 
288  /// @brief The equal-to operator for comparing FontBufferParameters for
289  /// equality.
290  ///
291  /// @param[in] other The other FontBufferParameters to check against for
292  /// equality.
293  ///
294  /// @return Returns `true` if the two FontBufferParameters are equal.
295  /// Otherwise it returns `false`.
296  bool operator==(const FontBufferParameters &other) const {
297  if (cache_id_ != kNullHash) {
298  return cache_id_ == other.cache_id_;
299  }
300  return (font_id_ == other.font_id_ && text_id_ == other.text_id_ &&
301  font_size_ == other.font_size_ && size_.x == other.size_.x &&
302  size_.y == other.size_.y &&
303  kerning_scale_ == other.kerning_scale_ &&
304  line_height_scale_ == other.line_height_scale_ &&
305  flags_value_ == other.flags_value_ && cache_id_ == other.cache_id_);
306  }
307 
308  /// @brief The hash function for FontBufferParameters.
309  ///
310  /// @param[in] key A FontBufferParameters to use as the key for
311  /// hashing.
312  ///
313  /// @return Returns a `size_t` of the hash of the FontBufferParameters.
314  size_t operator()(const FontBufferParameters &key) const {
315  // Note that font_id_, text_id_ and cache_id_ are already hashed values.
316  size_t value = key.cache_id_;
317  if (key.cache_id_ == kNullHash) {
318  value = HashCombine<float>(value, key.font_size_);
319  value = HashCombine<float>(value, key.kerning_scale_);
320  value = HashCombine<float>(value, key.line_height_scale_);
321  value = HashCombine<int32_t>(value, key.flags_value_);
322  value = HashCombine<int32_t>(value, key.size_.x);
323  value = HashCombine<int32_t>(value, key.size_.y);
324  }
325  return value;
326  }
327 
328  /// @brief The compare operator for FontBufferParameters.
330  const FontBufferParameters &rhs) const {
331  if (lhs.cache_id_ != kNullHash && rhs.cache_id_ != kNullHash) {
332  return lhs.cache_id_ < rhs.cache_id_;
333  }
334  return std::tie(lhs.font_id_, lhs.text_id_, lhs.font_size_,
335  lhs.kerning_scale_, lhs.line_height_scale_,
336  lhs.flags_value_, lhs.size_.x, lhs.size_.y) <
337  std::tie(rhs.font_id_, rhs.text_id_, rhs.font_size_,
338  rhs.kerning_scale_, rhs.line_height_scale_,
339  rhs.flags_value_, rhs.size_.x, rhs.size_.y);
340  }
341 
342  /// @return Returns a font hash id.
343  HashedId get_font_id() const { return font_id_; }
344 
345  /// @return Returns a hash value of the text.
346  HashedId get_text_id() const { return text_id_; }
347 
348  /// @return Returns a cache id value.
349  HashedId get_cache_id() const { return cache_id_; }
350 
351  /// @return Returns the size value.
352  const mathfu::vec2i &get_size() const { return size_; }
353 
354  /// @return Returns the font size.
355  float get_font_size() const { return font_size_; }
356 
357  /// @return Returns a text alignment info.
358  TextAlignment get_text_alignment() const { return flags_.text_alignement; }
359 
360  /// @return Returns the kerning scale.
361  float get_kerning_scale() const { return kerning_scale_; }
362 
363  /// @return Returns the line height scale.
364  float get_line_height_scale() const { return line_height_scale_; }
365 
366  /// @return Returns a glyph setting info.
367  GlyphFlags get_glyph_flags() const { return flags_.glyph_flags; }
368 
369  /// @return Returns a flag indicating if the buffer has caret info.
370  bool get_caret_info_flag() const { return flags_.caret_info; }
371 
372  /// @return Returns a flag indicating if the buffer manages reference counts.
373  bool get_ref_count_flag() const { return flags_.ref_count; }
374 
375  /// @return Returns a flag indicating if the buffer is for RTL layout.
376  bool get_rtl_layout_flag() const { return flags_.rtl_layout; }
377 
378  /// @return Returns a flag indicating if the hyphenation is enabled.
379  bool get_enable_hyphenation_flag() const { return flags_.enable_hyphenation; }
380 
381  /// Retrieve a line length of the text based on given parameters.
382  /// a fixed line length (get_size.x) will be used if the text is justified
383  /// or right aligned otherwise the line length will be determined by the text
384  /// layout phase.
385  /// @return Returns the expected line width.
386  int32_t get_line_length() const {
387  auto alignment = get_text_alignment();
388  if (alignment == kTextAlignmentLeft || alignment == kTextAlignmentCenter) {
389  return 0;
390  } else {
391  // Other settings will use max width of the given area.
392  return size_.x * kFreeTypeUnit;
393  }
394  }
395 
396  /// @return Returns the multi line setting.
397  bool get_multi_line_setting() const {
398  // size.x == 0 indicates a single line mode.
399  if (!size_.x) {
400  return false;
401  }
402  if (get_text_alignment() != kTextAlignmentLeft) {
403  return true;
404  } else {
405  return size_.y == 0 || size_.y > font_size_;
406  }
407  }
408 
409  /// @Brief Set font size.
410  void set_font_size(float size) { font_size_ = size; }
411 
412  /// @Brief Set the size value.
413  void set_size(mathfu::vec2i &size) { size_ = size; }
414 
415  private:
416  HashedId font_id_;
417  HashedId text_id_;
418  // An extra ID to allow multiple FontBuffer entries in the cache.
419  HashedId cache_id_;
420  float font_size_;
421  mathfu::vec2i size_;
422  float kerning_scale_;
423  float line_height_scale_;
424 
425  // A structure that defines bit fields to hold multiple flag values related to
426  // the font buffer.
427  struct FontBufferFlags {
428  bool ref_count : 1;
429  bool caret_info : 1;
430  bool rtl_layout : 1;
431  bool enable_hyphenation : 1;
432  GlyphFlags glyph_flags : 2;
433  TextAlignment text_alignement : 3;
434  };
435  union {
436  uint32_t flags_value_;
437  FontBufferFlags flags_;
438  };
439 };
440 
441 /// @struct LinkInfo
442 /// @brief HTML link href, and the location of the link text in FontBuffer.
443 struct LinkInfo {
445  LinkInfo(const std::string &link, int32_t start_glyph_index,
446  int32_t end_glyph_index)
447  : link(link),
448  start_glyph_index(start_glyph_index),
449  end_glyph_index(end_glyph_index) {}
450 
451  /// Link address. If from HTML, this is the `href` text.
452  std::string link;
453 
454  /// First glyph for the link text in the FontBuffer holding the rendered html.
455  /// Call FontBuffer::CalculateBounds(start_glyph_index, end_glyph_index) to
456  /// get the bounding boxes for the link text.
458 
459  /// First glyph not in the link text in the FontBuffer.
461 };
462 
463 /// @class FontMetrics
464 ///
465 /// @brief This class has additional properties for font metrics.
466 //
467 /// For details of font metrics, refer http://support.microsoft.com/kb/32667
468 /// In this class, ascender and descender values are retrieved from FreeType
469 /// font property.
470 ///
471 /// And internal/external leading values are updated based on rendering glyph
472 /// information. When rendering a string, the leading values tracks max (min for
473 /// internal leading) value in the string.
474 class FontMetrics {
475  public:
476  /// The default constructor for FontMetrics.
478  : base_line_(0),
479  internal_leading_(0),
480  ascender_(0),
481  descender_(0),
482  external_leading_(0) {}
483 
484  /// The constructor with initialization parameters for FontMetrics.
485  FontMetrics(int32_t base_line, int32_t internal_leading, int32_t ascender,
486  int32_t descender, int32_t external_leading)
487  : base_line_(base_line),
488  internal_leading_(internal_leading),
489  ascender_(ascender),
490  descender_(descender),
491  external_leading_(external_leading) {
492  assert(internal_leading >= 0);
493  assert(ascender >= 0);
494  assert(descender <= 0);
495  assert(external_leading <= 0);
496  }
497 
498  /// The destructor for FontMetrics.
500 
501  /// @return Returns the baseline value as an int32_t.
502  int32_t base_line() const { return base_line_; }
503 
504  /// @brief set the baseline value.
505  ///
506  /// @param[in] base_line An int32_t to set as the baseline value.
507  void set_base_line(int32_t base_line) { base_line_ = base_line; }
508 
509  /// @return Returns the internal leading parameter as an int32_t.
510  int32_t internal_leading() const { return internal_leading_; }
511 
512  /// @brief Set the internal leading parameter value.
513  ///
514  /// @param[in] internal_leading An int32_t to set as the internal
515  /// leading value.
517  assert(internal_leading >= 0);
518  internal_leading_ = internal_leading;
519  }
520 
521  /// @return Returns the ascender value as an int32_t.
522  int32_t ascender() const { return ascender_; }
523 
524  /// @brief Set the ascender value.
525  ///
526  /// @param[in] ascender An int32_t to set as the ascender value.
527  void set_ascender(int32_t ascender) {
528  assert(ascender >= 0);
529  ascender_ = ascender;
530  }
531 
532  /// @return Returns the descender value as an int32_t.
533  int32_t descender() const { return descender_; }
534 
535  /// @brief Set the descender value.
536  ///
537  /// @param[in] descender An int32_t to set as the descender value.
538  void set_descender(int32_t descender) {
539  assert(descender <= 0);
540  descender_ = descender;
541  }
542 
543  /// @return Returns the external leading parameter value as an int32_t.
544  int32_t external_leading() const { return external_leading_; }
545 
546  /// @brief Set the external leading parameter value.
547  ///
548  /// @param[in] external_leading An int32_t to set as the external
549  /// leading value.
551  assert(external_leading <= 0);
552  external_leading_ = external_leading;
553  }
554 
555  /// @return Returns the total height of the glyph.
556  int32_t total() const {
557  return internal_leading_ + ascender_ - descender_ - external_leading_;
558  }
559 
560  private:
561  // Baseline: Baseline of the glpyhs.
562  // When rendering multiple glyphs in the same horizontal group,
563  // baselines need be aligned.
564  // Most of glyphs fit within the area of ascender + descender.
565  // However some glyphs may render in internal/external leading area.
566  // (e.g. Ã…, underlines)
567  int32_t base_line_;
568 
569  // Internal leading: Positive value that describes the space above the
570  // ascender.
571  int32_t internal_leading_;
572 
573  // Ascender: Positive value that describes the size of the ascender above
574  // the baseline.
575  int32_t ascender_;
576 
577  // Descender: Negative value that describes the size of the descender below
578  // the baseline.
579  int32_t descender_;
580 
581  // External leading : Negative value that describes the space below
582  // the descender.
583  int32_t external_leading_;
584 };
585 
586 /// @struct FontVertex
587 ///
588 /// @brief This struct holds all the font vertex data.
589 struct FontVertex {
590  /// @brief The constructor for a FontVertex.
591  ///
592  /// @param[in] x A float representing the `x` position of the vertex.
593  /// @param[in] y A float representing the `y` position of the vertex.
594  /// @param[in] z A float representing the `z` position of the vertex.
595  /// @param[in] u A float representing the `u` value in the UV mapping.
596  /// @param[in] v A float representing the `v` value in the UV mapping.
597  FontVertex(float x, float y, float z, float u, float v) {
598  position_.data[0] = x;
599  position_.data[1] = y;
600  position_.data[2] = z;
601  uv_.data[0] = u;
602  uv_.data[1] = v;
603  }
604 
605  /// @cond FONT_MANAGER_INTERNAL
606  mathfu::vec3_packed position_;
607  mathfu::vec2_packed uv_;
608  /// @endcond
609 };
610 
611 /// @class FontBufferAttributes
612 ///
613 /// @brief A structure holding attribute information of texts in a FontBuffer.
615  public:
617  : slice_index_(kIndexInvalid), underline_(false), color_(kDefaultColor) {}
618 
619  /// @brief The constructor to set default values.
620  /// @param[in] underline A flag indicating if the attribute has underline.
621  /// @param[in] color A color value of the attribute in RGBA8888.
622  FontBufferAttributes(bool underline, uint32_t color)
623  : slice_index_(kIndexInvalid), underline_(underline), color_(color) {}
624 
625  /// @brief The equal-to operator for comparing FontBufferAttributes for
626  /// equality.
627  ///
628  /// @param[in] other The other FontBufferAttributes to check against for
629  /// equality.
630  ///
631  /// @return Returns `true` if the two FontBufferAttributes are equal.
632  /// Otherwise it returns `false`.
633  bool operator==(const FontBufferAttributes &other) const {
634  return (slice_index_ == other.slice_index_ &&
635  underline_ == other.underline_ && color_ == other.color_);
636  }
637 
638  /// @brief The hash function for FontBufferAttributes.
639  ///
640  /// @param[in] key A FontBufferAttributes to use as the key for
641  /// hashing.
642  ///
643  /// @return Returns a `size_t` of the hash of the FontBufferAttributes.
644  size_t operator()(const FontBufferAttributes &key) const {
645  // Note that font_id_, text_id_ and cache_id_ are already hashed values.
646  size_t value = HashValue(key.slice_index_);
647  value = HashCombine<bool>(value, key.underline_);
648  value = HashCombine<uint32_t>(value, key.color_);
649  return value;
650  }
651 
652  /// @brief The compare operator for FontBufferAttributes.
654  const FontBufferAttributes &rhs) const {
655  return std::tie(lhs.slice_index_, lhs.underline_, lhs.color_) <
656  std::tie(rhs.slice_index_, rhs.underline_, rhs.color_);
657  }
658 
659  // Struct for the underline information.
660  struct UnderlineInfo {
661  UnderlineInfo(int32_t index, const mathfu::vec2i &y_pos)
662  : start_vertex_index_(index), end_vertex_index_(index), y_pos_(y_pos) {}
663  int32_t start_vertex_index_;
664  int32_t end_vertex_index_;
665  mathfu::vec2i y_pos_;
666  };
667 
668  // Getter of the color attribute. Color values are in RGBA8888.
669  uint32_t get_color() const { return color_; }
670 
671  // Getter of the underline attribute.
672  bool get_underline() const { return underline_; }
673 
674  // Getter of the underline information.
675  const std::vector<UnderlineInfo> &get_underline_info() const {
676  return underline_info_;
677  }
678 
679  // Getter/Setter of the slice index.
680  int32_t get_slice_index() const { return slice_index_; }
681  void set_slice_index(int32_t slice_index) { slice_index_ = slice_index; }
682 
683  private:
684  // Friend classes.
685  friend FontManager;
686  friend FontBuffer;
687 
688  /// Update underline information.
689  void UpdateUnderline(int32_t vertex_index, const mathfu::vec2i &y_pos);
690  void WrapUnderline(int32_t vertex_index);
691 
692  /// @var texture_index_
693  ///
694  /// @brief A index value indicating a texture slice in the texture cache used
695  /// to render the indices.
696  int32_t slice_index_;
697 
698  /// @var underline_
699  ///
700  /// @brief A flag indicating if the buffer is underline buffer. An underline
701  /// buffer need to be rendered with a shader that fills all the regions.
702  bool underline_;
703 
704  /// @var underline_info
705  ///
706  /// @brief A vector holding underline information.
707  std::vector<UnderlineInfo> underline_info_;
708 
709  /// @var color_
710  ///
711  /// @brief A value holds a text color specified for the glyghs.
712  uint32_t color_;
713 };
714 
715 /// @class FontBufferContext
716 /// @brief Temporary buffers used while generating FontBuffer.
717 /// Word boundary information. This information is used only with a typography
718 /// layout with a justification.
720  public:
722  : line_start_caret_index_(0),
723  lastline_must_break_(false),
724  appending_buffer_(false),
725  original_font_(nullptr),
726  original_font_size_(0.0f),
727  current_font_size_(0.0f),
728  original_base_line_(0) {}
729 
730  /// @var Type defining an interator to the attribute map that is tracking
731  /// FontBufferAttribute.
732  typedef std::map<FontBufferAttributes, int32_t,
733  FontBufferAttributes>::iterator attribute_map_it;
734 
735  /// @brief Clear the temporary buffers.
736  void Clear() {
737  word_boundary_.clear();
738  word_boundary_caret_.clear();
739  line_start_caret_index_ = 0;
740  lastline_must_break_ = false;
741  attribute_map_.clear();
742  attribute_history_.clear();
743  original_font_ = nullptr;
744  original_font_size_ = 0.0f;
745  current_font_size_ = 0.0f;
746  original_base_line_ = 0;
747  }
748 
749  /// @brief Set attribute to the FontBuffer. The attribute is used while
750  /// constructing a FontBuffer.
751  void SetAttribute(const FontBufferAttributes &attribute);
752 
753  /// @brief Look up an attribute from the attribute map while constructing
754  /// attributed FontBuffer.
755  attribute_map_it LookUpAttribute(const FontBufferAttributes &attribute);
756 
757  // Getter/Setter
758  bool lastline_must_break() const { return lastline_must_break_; }
759  void set_lastline_must_break(bool b) { lastline_must_break_ = b; }
760 
761  bool appending_buffer() const { return appending_buffer_; }
762  void set_appending_buffer(bool b) { appending_buffer_ = b; }
763 
764  uint32_t line_start_caret_index() const { return line_start_caret_index_; }
765  void set_line_start_caret_index(uint32_t i) { line_start_caret_index_ = i; }
766 
767  std::vector<attribute_map_it> &attribute_history() {
768  return attribute_history_;
769  };
770  std::vector<uint32_t> &word_boundary() {
771  return word_boundary_;
772  };
773  std::vector<uint32_t> &word_boundary_caret() {
774  return word_boundary_caret_;
775  };
776 
777  HbFont *original_font() const { return original_font_; }
778  void set_original_font(HbFont *font) { original_font_ = font; }
779 
780  float original_font_size() const { return original_font_size_; }
781  void set_original_font_size(float size) { original_font_size_ = size; }
782 
783  float current_font_size() const { return current_font_size_; }
784  void set_current_font_size(float size) { current_font_size_ = size; }
785 
786  int32_t original_base_line() const { return original_base_line_; }
787  void set_original_base_line(int32_t base_line) {
788  original_base_line_ = base_line;
789  }
790 
791  private:
792  std::vector<uint32_t> word_boundary_;
793  std::vector<uint32_t> word_boundary_caret_;
794  std::map<FontBufferAttributes, int32_t, FontBufferAttributes> attribute_map_;
795  std::vector<attribute_map_it> attribute_history_;
796  uint32_t line_start_caret_index_;
797  bool lastline_must_break_;
798  // flag indicating it's appending a font buffer.
799  bool appending_buffer_;
800 
801  HbFont *original_font_;
802  float original_font_size_;
803  float current_font_size_;
804  int32_t original_base_line_;
805 };
806 
807 /// @struct GlyphInfo
808 ///
809 /// @brief This struct holds the glyph information that is used when re-creating
810 /// a FontBuffer.
811 ///
812 struct GlyphInfo {
813  GlyphInfo(HashedId face_id, uint32_t code_point, float size)
814  : face_id_(face_id), code_point_(code_point), size_(size) {}
815 
816  /// @var face_id
817  /// @brief An ID indicating a font face.
818  HashedId face_id_;
819 
820  /// @var code_point
821  /// @brief A code point in the font face.
822  uint32_t code_point_;
823 
824  /// @var size
825  /// @brief A glyph size.
826  float size_;
827 };
828 
829 /// @class FontBuffer
830 ///
831 /// @brief this is used with the texture atlas rendering.
832 class FontBuffer {
833  public:
834  /// @var kIndiciesPerCodePoint
835  ///
836  /// @brief The number of indices per code point.
837  static const int32_t kIndiciesPerCodePoint = 6;
838 
839  /// @var kVerticesPerCodePoint
840  ///
841  /// @brief The number of vertices per code point.
842  static const int32_t kVerticesPerCodePoint = 4;
843 
844  /// @var Type defining an interator to the internal map that is tracking all
845  /// FontBuffer instances.
846  typedef std::map<FontBufferParameters, std::unique_ptr<FontBuffer>,
847  FontBufferParameters>::iterator fontbuffer_map_it;
848 
849  /// @brief The default constructor for a FontBuffer.
851  : size_(mathfu::kZeros2i),
852  revision_(0),
853  ref_count_(0),
854  has_ellipsis_(false),
855  valid_(true) {
856  line_start_indices_.push_back(0);
857  }
858 
859  /// @brief The constructor for FontBuffer with a given buffer size.
860  ///
861  /// @param[in] size A size of the FontBuffer in a number of glyphs.
862  /// @param[in] caret_info Indicates if the FontBuffer also maintains a caret
863  /// position buffer.
864  ///
865  /// @note Caret position does not match to glpyh position 1 to 1, because a
866  /// glyph can have multiple caret positions (e.g. Single 'ff' glyph can have 2
867  /// caret positions).
868  ///
869  /// Since it has a strong relationship to rendering positions, we store the
870  /// caret position information in the FontBuffer.
871  FontBuffer(uint32_t size, bool caret_info)
872  : size_(mathfu::kZeros2i),
873  revision_(0),
874  ref_count_(0),
875  has_ellipsis_(false),
876  valid_(true) {
877  glyph_info_.reserve(size);
878  if (caret_info) {
879  caret_positions_.reserve(size + 1);
880  }
881  line_start_indices_.push_back(0);
882  }
883 
884  /// The destructor for FontBuffer.
886 
887  /// @return Returns the FontMetrics metrics parameters for the font
888  /// texture.
889  const FontMetrics &metrics() const { return metrics_; }
890 
891  /// @return Returns the slices array as a const std::vector<int32_t>.
892  const std::vector<FontBufferAttributes> &get_slices() const {
893  return slices_;
894  }
895 
896  /// @return Returns the indices array as a const std::vector<uint16_t>.
897  const std::vector<uint16_t> &get_indices(int32_t index) const {
898  return indices_[index];
899  }
900 
901  /// @return Returns the vertices array as a const std::vector<FontVertex>.
902  const std::vector<FontVertex> &get_vertices() const { return vertices_; }
903 
904  /// @return Returns the non const version of vertices array as a
905  /// std::vector<FontVertex>.
906  /// Note that the updating the number of elements in the buffer would result
907  /// undefined behavior.
908  std::vector<FontVertex> &get_vertices() { return vertices_; }
909 
910  /// @return Returns the array of GlyphInfo as a const std::vector<GlyphInfo>.
911  const std::vector<GlyphInfo> &get_glyph_info() const { return glyph_info_; }
912 
913  /// @return Returns the size of the string as a const vec2i reference.
914  const mathfu::vec2i get_size() const { return size_; }
915 
916  /// @return Returns the glyph cache revision counter as a uint32_t.
917  ///
918  /// @note Each time a contents of the glyph cache is updated, the revision of
919  /// the cache is updated.
920  ///
921  /// If the cache revision and the buffer revision is different, the
922  /// font_manager try to re-construct the buffer.
923  int32_t get_revision() const { return revision_; }
924 
925  /// @return Returns the pass counter as an int32_t.
926  ///
927  /// @note In the render pass, this value is used if the user of the class
928  /// needs to call `StartRenderPass()` to upload the atlas texture.
929  int32_t get_pass() const { return pass_; }
930 
931  /// @brief Verifies that the sizes of the arrays used in the buffer are
932  /// correct.
933  ///
934  /// @warning Asserts if the array sizes are incorrect.
935  ///
936  /// @return Returns `true`.
937  bool Verify() const {
938  assert(vertices_.size() == glyph_info_.size() * kVerticesPerCodePoint);
939  size_t sum_indices = 0;
940  for (size_t i = 0; i < indices_.size(); ++i) {
941  sum_indices += indices_[i].size();
942  }
943  assert(sum_indices == glyph_info_.size() * kIndiciesPerCodePoint);
944  return valid_;
945  }
946 
947  /// @brief Retrieve a caret positions with a given index.
948  ///
949  /// @param[in] index The index of the caret position.
950  ///
951  /// @return Returns a vec2i containing the caret position. Otherwise it
952  /// returns `kCaretPositionInvalid` if the buffer does not contain
953  /// caret information at the given index, or if the index is out of range.
954  mathfu::vec2i GetCaretPosition(size_t index) const {
955  if (!caret_positions_.capacity() || index > caret_positions_.size())
956  return kCaretPositionInvalid;
957  return caret_positions_[index];
958  }
959 
960  /// @return Returns a const reference to the caret positions buffer.
961  const std::vector<mathfu::vec2i> &GetCaretPositions() const {
962  return caret_positions_;
963  }
964 
965  /// @return Returns `true` if the FontBuffer contains any caret positions.
966  /// If the caret positions array has 0 elements, it will return `false`.
967  bool HasCaretPositions() const { return caret_positions_.capacity() != 0; }
968 
969  /// @return Returns `true` if the FontBuffer has an ellipsis appended.
970  bool HasEllipsis() const { return has_ellipsis_; }
971 
972  /// @brief Helper to retrieve AABB info for glyph index range.
973  /// Note that we return multiple AABB bounds if the glyphs span multiple
974  /// lines. Each vec4 is in the format (min_x, min_y, max_x, max_y).
975  std::vector<mathfu::vec4> CalculateBounds(int32_t start_index,
976  int32_t end_index) const;
977 
978  /// @brief Return glyph indices and linked-to address of any HREF links that
979  /// have been rendered to this FontBuffer.
980  const std::vector<LinkInfo> &get_links() const { return links_; }
981 
982  /// @brief Return current glyph count stored in the buffer.
983  int32_t get_glyph_count() const {
984  return static_cast<int32_t>(vertices_.size()) / 4;
985  }
986 
987  /// @brief Gets the reference count of the buffer.
988  uint32_t get_ref_count() const { return ref_count_; }
989 
990  private:
991  // Make the FontManager and related classes as a friend class so that
992  // FontManager can manage FontBuffer class using it's private methods.
993  friend FontManager;
994  friend GlyphCacheRow;
995 
996  /// @return Returns the array of the slices that is used in the FontBuffer as
997  /// a std::vector<FontBufferAttributes>. When rendering the buffer, retrieve
998  /// corresponding
999  /// atlas texture from the glyph cache and bind the texture.
1000  std::vector<FontBufferAttributes> &get_slices() { return slices_; }
1001 
1002  /// @return Returns the indices array as a std::vector<uint16_t>.
1003  std::vector<uint16_t> &get_indices(int32_t index) { return indices_[index]; }
1004 
1005  /// @brief Sets the FontMetrics metrics parameters for the font
1006  /// texture.
1007  ///
1008  /// @param[in] metrics The FontMetrics to set for the font texture.
1009  void set_metrics(const FontMetrics &metrics) { metrics_ = metrics; }
1010 
1011  /// @brief Set the size of the string.
1012  ///
1013  /// @param[in] size A const vec2i reference to the size of the string.
1014  void set_size(const mathfu::vec2i &size) { size_ = size; }
1015 
1016  /// @brief Sets the glyph cache revision counter.
1017  ///
1018  /// @param[in] revision The uint32_t containing the new counter to set.
1019  ///
1020  /// @note Each time a contents of the glyph cache is updated, the revision of
1021  /// the cache is updated.
1022  ///
1023  /// If the cache revision and the buffer revision is different, the
1024  /// font_manager try to re-construct the buffer.
1025  void set_revision(uint32_t revision) { revision_ = revision; }
1026 
1027  /// @return Returns the pass counter as an int32_t.
1028  ///
1029  /// @note In the render pass, this value is used if the user of the class
1030  /// needs to call `StartRenderPass()` to upload the atlas texture.
1031  void set_pass(int32_t pass) { pass_ = pass; }
1032 
1033  /// @return Sets an iterator pointing the FontManager's internal map.
1034  void set_iterator(fontbuffer_map_it it) { it_map_ = it; }
1035 
1036  /// @return Returns an iterator pointing the map in the FontManager.
1037  fontbuffer_map_it get_iterator() { return it_map_; }
1038 
1039  /// @brief Adds a codepoint and related info of a glyph to the glyph info
1040  /// array.
1041  ///
1042  /// @param[in] codepoint A codepoint of the glyph.
1043  void AddGlyphInfo(HashedId face_id, uint32_t codepoint, float size) {
1044  glyph_info_.push_back(GlyphInfo(face_id, codepoint, size));
1045  }
1046 
1047  /// @brief Adds 4 vertices to be used for a glyph rendering to the
1048  /// vertex array.
1049  ///
1050  /// @param[in] pos A vec2 containing the `x` and `y` position of the first,
1051  /// unscaled vertex.
1052  /// @param[in] base_line A const int32_t representing the baseline for the
1053  /// vertices.
1054  /// @param[in] scale A float used to scale the size and offset.
1055  /// @param[in] entry A const GlyphCacheEntry reference whose offset and size
1056  /// are used in the scaling.
1057  void AddVertices(const mathfu::vec2 &pos, int32_t base_line, float scale,
1058  const GlyphCacheEntry &entry);
1059 
1060  /// @brief Adds 4 indices to be used for a glyph rendering to the
1061  /// index array.
1062  ///
1063  /// @param[in] buffer_idx An index of the index array to add the indices.
1064  void AddIndices(int32_t buffer_idx, int32_t count);
1065 
1066  /// @brief Update underline information of attributed FontBuffer.
1067  ///
1068  /// @param[in] buffer_idx An index of the attribute to add the underline.
1069  void UpdateUnderline(int32_t buffer_idx, int32_t vertex_index,
1070  const mathfu::vec2i &y_pos);
1071 
1072  /// @brief Add the given caret position to the buffer.
1073  ///
1074  /// @param[in] x The `x` position of the caret.
1075  /// @param[in] y The `y` position of the caret.
1076  void AddCaretPosition(int32_t x, int32_t y);
1077 
1078  /// @brief Add the caret position to the buffer.
1079  ///
1080  /// @param[in] pos The position of the caret.
1081  void AddCaretPosition(const mathfu::vec2 &pos);
1082 
1083  /// @brief Tell the buffer a word boundary.
1084  /// It may use the information to justify text layout later.
1085  ///
1086  /// @param[in] parameters Text layout parameters used to update the layout.
1087  /// @param[in] context FontBuffer context that is used while constructing a
1088  /// FontBuffer.
1089  void AddWordBoundary(const FontBufferParameters &parameters,
1090  FontBufferContext *context);
1091 
1092  /// @brief Update UV information of a glyph entry.
1093  ///
1094  /// @param[in] index The index of the glyph entry that should be updated.
1095  /// @param[in] uv The `uv` vec4 should include the top-left corner of UV value
1096  /// as `x` and `y`, and the bottom-right of UV value as the `w` and `z`
1097  /// components of the vector.
1098  void UpdateUV(int32_t index, const mathfu::vec4 &uv);
1099 
1100  /// @brief Indicates a start of new line. It may update a previous line's
1101  /// buffer contents based on typography layout settings.
1102  ///
1103  /// @param[in] parameters Text layout parameters used to update the layout.
1104  /// @param[in] context FontBuffer context that is used while constructing a
1105  /// FontBuffer.
1106  void UpdateLine(const FontBufferParameters &parameters,
1107  TextLayoutDirection layout_direction,
1108  FontBufferContext *context);
1109  /// @brief Adjust glyph positions in current line when a glyph size is changed
1110  /// to larger size while appending buffers.
1111  ///
1112  /// @param[in] parameters Text layout parameters used to update the layout.
1113  /// @param[in] context FontBuffer context that is used while constructing a
1114  /// FontBuffer.
1115  /// @return return an offset value for glyph's y position.
1116  float AdjustCurrentLine(const FontBufferParameters &parameters,
1117  FontBufferContext *context);
1118 
1119  /// @brief Sets the reference count of the buffer.
1120  void set_ref_count(uint32_t ref_count) { ref_count_ = ref_count; }
1121 
1122  /// @brief Retrieve an internal buffer index of accounting atlas slice in the
1123  /// FontBuffer.
1124  int32_t GetBufferIndex(int32_t slice, FontBufferContext *context);
1125 
1126  // @brief Invalidate the FontBuffer.
1127  void Invalidate() { valid_ = false; }
1128 
1129  /// @brief Add a reference to the glyph cache row that is referenced in the
1130  /// FontBuffer.
1131  void AddCacheRowReference(GlyphCacheRow *p) { referencing_row_.insert(p); }
1132 
1133  /// @brief Release references to the glyph cache row that is referenced in the
1134  /// FontBuffer.
1135  void ReleaseCacheRowReference() {
1136  auto begin = referencing_row_.begin();
1137  auto end = referencing_row_.end();
1138  while (begin != end) {
1139  auto row = *begin;
1140  row->Release(this);
1141  begin++;
1142  }
1143  referencing_row_.clear();
1144  }
1145 
1146  // Font metrics information.
1147  FontMetrics metrics_;
1148 
1149  // Arrays for font indices, vertices and code points.
1150  // They are hold as a separate vector because OpenGL draw call needs them to
1151  // be a separate array.
1152 
1153  // Slices that is used in the FontBuffer.
1154  std::vector<FontBufferAttributes> slices_;
1155 
1156  // Indices of the font buffer.
1157  std::vector<std::vector<uint16_t>> indices_;
1158 
1159  // Vertices data of the font buffer.
1160  std::vector<FontVertex> vertices_;
1161 
1162  // Code points and related mapping information used in the buffer. This array
1163  // is used to fetch and update UV entries when the glyph cache is flushed.
1164  std::vector<GlyphInfo> glyph_info_;
1165 
1166  // Caret positions in the buffer. We need to track them differently than a
1167  // vertices information because we support ligatures so that single glyph
1168  // can include multiple caret positions.
1169  std::vector<mathfu::vec2i> caret_positions_;
1170 
1171  // Glyph indices and linked-to address of any links that have been rendered
1172  // in this FontBuffer. Call FontBuffer::CalculateBounds() to get the
1173  // bounding boxes for the link text.
1174  std::vector<LinkInfo> links_;
1175 
1176  // Size of the string in pixels.
1177  mathfu::vec2i size_;
1178 
1179  // Revision of the FontBuffer corresponding glyph cache revision.
1180  // Caller needs to check the revision value if glyph texture has referencing
1181  // entries by checking the revision.
1182  int32_t revision_;
1183 
1184  // Pass id. Each pass should have it's own texture atlas contents.
1185  int32_t pass_;
1186 
1187  // Reference count related variables.
1188  uint32_t ref_count_;
1189 
1190  // A flag indicating if the font buffer has an ellipsis appended.
1191  bool has_ellipsis_;
1192 
1193  // A flag indicating the FontBuffer's validity. When a FontBuffer is a
1194  // reference counting buffer and a glyph cache row which the buffer references
1195  // are evicted, the buffer is marked as 'invalid'. In that case, the user need
1196  // to
1197  // re-create the buffer (and release one reference count).
1198  bool valid_;
1199 
1200  // Start glyph index of every line.
1201  std::vector<uint32_t> line_start_indices_;
1202 
1203  // Set holding iterators in glyph cache rows. When the buffer is released,
1204  // the API releases all glyph rows in the set.
1205  std::set<GlyphCacheRow *> referencing_row_;
1206 
1207  // Back reference to the map. When releasing a buffer, the API uses the
1208  // iterator to remove the entry from the map.
1209  fontbuffer_map_it it_map_;
1210 };
1211 
1212 /// @}
1213 
1214 } // namespace flatui
1215 
1216 #endif // FONT_BUFFER_H
FontMetrics(int32_t base_line, int32_t internal_leading, int32_t ascender, int32_t descender, int32_t external_leading)
The constructor with initialization parameters for FontMetrics.
Definition: font_buffer.h:485
const std::string & get_original_name() const
Definition: font_buffer.h:184
bool HasEllipsis() const
Definition: font_buffer.h:970
const mathfu::vec2i kCaretPositionInvalid
A sentinel value representing an invalid caret position.
Definition: font_buffer.h:62
const mathfu::vec2i get_size() const
Definition: font_buffer.h:914
bool operator()(const FontBufferParameters &lhs, const FontBufferParameters &rhs) const
The compare operator for FontBufferParameters.
Definition: font_buffer.h:329
HashedId get_font_id() const
Definition: font_buffer.h:343
attribute_map_it LookUpAttribute(const FontBufferAttributes &attribute)
Look up an attribute from the attribute map while constructing attributed FontBuffer.
void set_descender(int32_t descender)
Set the descender value.
Definition: font_buffer.h:538
int32_t get_glyph_count() const
Return current glyph count stored in the buffer.
Definition: font_buffer.h:983
bool HasCaretPositions() const
Definition: font_buffer.h:967
bool get_multi_line_setting() const
Definition: font_buffer.h:397
void set_size(mathfu::vec2i &size)
Set the size value.
Definition: font_buffer.h:413
FontManager manages font rendering with OpenGL utilizing freetype and harfbuzz as a glyph rendering a...
Definition: font_manager.h:109
const std::vector< GlyphInfo > & get_glyph_info() const
Definition: font_buffer.h:911
const std::vector< FontVertex > & get_vertices() const
Definition: font_buffer.h:902
const std::string & get_language() const
Language. The entry is ignored when opening a font.
Definition: font_buffer.h:186
const std::vector< mathfu::vec2i > & GetCaretPositions() const
Definition: font_buffer.h:961
bool Verify() const
Verifies that the sizes of the arrays used in the buffer are correct.
Definition: font_buffer.h:937
void Clear()
Clear the temporary buffers.
Definition: font_buffer.h:736
void set_external_leading(int32_t external_leading)
Set the external leading parameter value.
Definition: font_buffer.h:550
const std::vector< LinkInfo > & get_links() const
Return glyph indices and linked-to address of any HREF links that have been rendered to this FontBuff...
Definition: font_buffer.h:980
bool is_family_name() const
Check if the font name is a family name.
Definition: font_buffer.h:191
const std::vector< FontBufferAttributes > & get_slices() const
Definition: font_buffer.h:892
Definition: font_buffer.h:660
int32_t internal_leading() const
Definition: font_buffer.h:510
FontMetrics()
The default constructor for FontMetrics.
Definition: font_buffer.h:477
std::vector< mathfu::vec4 > CalculateBounds(int32_t start_index, int32_t end_index) const
Helper to retrieve AABB info for glyph index range. Note that we return multiple AABB bounds if the g...
A structure holding attribute information of texts in a FontBuffer.
Definition: font_buffer.h:614
TextAlignment get_text_alignment() const
Definition: font_buffer.h:358
HashedId get_cache_id() const
Definition: font_buffer.h:349
FontBuffer(uint32_t size, bool caret_info)
The constructor for FontBuffer with a given buffer size.
Definition: font_buffer.h:871
This class that includes font buffer parameters. It is used as a key in the unordered_map to look up ...
Definition: font_buffer.h:223
mathfu::vec2i GetCaretPosition(size_t index) const
Retrieve a caret positions with a given index.
Definition: font_buffer.h:954
GlyphFlags get_glyph_flags() const
Definition: font_buffer.h:367
int32_t get_revision() const
Definition: font_buffer.h:923
int32_t base_line() const
Definition: font_buffer.h:502
FontBufferStatus
A status of FontBuffer correspoinding current glyph cache contents. Enumerations: ...
Definition: font_buffer.h:128
FontBufferAttributes(bool underline, uint32_t color)
The constructor to set default values.
Definition: font_buffer.h:622
bool operator==(const FontBufferAttributes &other) const
The equal-to operator for comparing FontBufferAttributes for equality.
Definition: font_buffer.h:633
This struct holds the glyph information that is used when re-creating a FontBuffer.
Definition: font_buffer.h:812
bool get_caret_info_flag() const
Definition: font_buffer.h:370
void set_base_line(int32_t base_line)
set the baseline value.
Definition: font_buffer.h:507
This struct holds all the font vertex data.
Definition: font_buffer.h:589
std::vector< FontVertex > & get_vertices()
Definition: font_buffer.h:908
~FontBuffer()
The destructor for FontBuffer.
Definition: font_buffer.h:885
static const int32_t kVerticesPerCodePoint
The number of vertices per code point.
Definition: font_buffer.h:842
TextAlignment
Alignment of the text.
Definition: font_buffer.h:110
bool operator==(const FontBufferParameters &other) const
The equal-to operator for comparing FontBufferParameters for equality.
Definition: font_buffer.h:296
const mathfu::vec2i & get_size() const
Definition: font_buffer.h:352
int32_t descender() const
Definition: font_buffer.h:533
float get_line_height_scale() const
Definition: font_buffer.h:364
float get_font_size() const
Definition: font_buffer.h:355
bool is_font_collection() const
Definition: font_buffer.h:194
FontBufferParameters(HashedId font_id, HashedId text_id, float font_size, const mathfu::vec2i &size, TextAlignment text_alignment, GlyphFlags glyph_flags, bool caret_info, bool ref_count, bool enable_hyphenation=false, bool rtl_layout=false, float kerning_scale=kKerningScaleDefault, float line_height_scale=kLineHeightDefault, HashedId cache_id=kNullHash)
Constructor for a FontBufferParameters.
Definition: font_buffer.h:265
~FontMetrics()
The destructor for FontMetrics.
Definition: font_buffer.h:499
size_t operator()(const FontBufferParameters &key) const
The hash function for FontBufferParameters.
Definition: font_buffer.h:314
void set_font_size(float size)
Set font size.
Definition: font_buffer.h:410
bool operator()(const FontBufferAttributes &lhs, const FontBufferAttributes &rhs) const
The compare operator for FontBufferAttributes.
Definition: font_buffer.h:653
const float kLineHeightDefault
Default value for a line height factor.
Definition: font_buffer.h:49
float get_kerning_scale() const
Definition: font_buffer.h:361
size_t operator()(const FontBufferAttributes &key) const
The hash function for FontBufferAttributes.
Definition: font_buffer.h:644
void set_internal_leading(int32_t internal_leading)
Set the internal leading parameter value.
Definition: font_buffer.h:516
const std::string & get_name() const
Definition: font_buffer.h:181
FontBuffer()
The default constructor for a FontBuffer.
Definition: font_buffer.h:850
HashedId get_text_id() const
Definition: font_buffer.h:346
const FontMetrics & metrics() const
Definition: font_buffer.h:889
bool get_enable_hyphenation_flag() const
Definition: font_buffer.h:379
int32_t ascender() const
Definition: font_buffer.h:522
this is used with the texture atlas rendering.
Definition: font_buffer.h:832
const int32_t kFreeTypeUnit
Constant to convert FreeType unit to pixel unit.
Definition: font_buffer.h:41
A class holding font family information. The class provides various ways to support fonts such as a f...
Definition: font_buffer.h:149
Temporary buffers used while generating FontBuffer. Word boundary information. This information is us...
Definition: font_buffer.h:719
TextLayoutDirection
Specify how to layout texts. Default value is TextLayoutDirectionLTR.
Definition: font_buffer.h:75
This class has additional properties for font metrics.
Definition: font_buffer.h:474
int32_t get_index() const
Definition: font_buffer.h:189
EllipsisMode
A flag controlling appending behavior of the ellipsis. Enumerations:
Definition: font_buffer.h:139
int32_t get_pass() const
Definition: font_buffer.h:929
FontVertex(float x, float y, float z, float u, float v)
The constructor for a FontVertex.
Definition: font_buffer.h:597
bool get_rtl_layout_flag() const
Definition: font_buffer.h:376
int32_t get_line_length() const
Definition: font_buffer.h:386
FontBufferParameters()
The default constructor for an empty FontBufferParameters.
Definition: font_buffer.h:226
int32_t external_leading() const
Definition: font_buffer.h:544
int32_t total() const
Definition: font_buffer.h:556
void set_ascender(int32_t ascender)
Set the ascender value.
Definition: font_buffer.h:527
bool get_ref_count_flag() const
Definition: font_buffer.h:373
const std::vector< uint16_t > & get_indices(int32_t index) const
Definition: font_buffer.h:897
static const int32_t kIndiciesPerCodePoint
The number of indices per code point.
Definition: font_buffer.h:837
uint32_t get_ref_count() const
Gets the reference count of the buffer.
Definition: font_buffer.h:988
const float kKerningScaleDefault
Default value for a kerning scale factor.
Definition: font_buffer.h:57
void SetAttribute(const FontBufferAttributes &attribute)
Set attribute to the FontBuffer. The attribute is used while constructing a FontBuffer.