18 #include "flatui/internal/flatui_util.h"
19 #include "flatui/internal/glyph_cache.h"
20 #include "flatui/internal/hb_complex_font.h"
65 const int32_t kIndexInvalid = -1;
68 const uint32_t kDefaultColor = 0xffffffff;
76 kTextLayoutDirectionLTR = 0,
77 kTextLayoutDirectionRTL = 1,
78 kTextLayoutDirectionTTB = 2,
111 kTextAlignmentLeft = 0,
112 kTextAlignmentRight = 1,
113 kTextAlignmentCenter = 2,
114 kTextAlignmentJustify = 4,
115 kTextAlignmentLeftJustify = kTextAlignmentJustify,
116 kTextAlignmentRightJustify = kTextAlignmentJustify | kTextAlignmentRight,
117 kTextAlignmentCenterJustify = kTextAlignmentJustify | kTextAlignmentCenter,
129 kFontBufferStatusReady = 0,
130 kFontBufferStatusNeedReconstruct = 1,
131 kFontBufferStatusNeedCacheUpdate = 2,
140 kEllipsisModeTruncateCharacter = 0,
141 kEllipsisModeTruncateWord = 1,
152 FontFamily(
const std::string &name, int32_t index,
const std::string &lang,
154 : index_(index), family_name_(family_name) {
156 font_name_ = NormalizeFontName(name);
159 font_name_ = CreateFontCollectionName(font_name_);
161 original_name_ = name;
163 FontFamily(
const char *name,
bool family_name)
164 : index_(kIndexInvalid), family_name_(family_name) {
165 font_name_ = NormalizeFontName(name);
166 original_name_ = name;
168 FontFamily(
const char *name) : index_(kIndexInvalid), family_name_(
false) {
169 font_name_ = NormalizeFontName(name);
170 original_name_ = name;
173 : index_(kIndexInvalid), family_name_(
false) {
174 font_name_ = NormalizeFontName(name);
175 original_name_ = name;
181 const std::string &
get_name()
const {
return font_name_; }
197 std::string CreateFontCollectionName(
const std::string &name) {
199 std::stringstream ss;
201 return name + ss.str();
204 static std::string NormalizeFontName(
const std::string &name) {
205 std::size_t found = name.find_last_of(
"/\\");
206 if (found == std::string::npos) {
209 return name.substr(found + 1);
212 std::string original_name_;
213 std::string font_name_;
227 : font_id_(kNullHash),
229 cache_id_(kNullHash),
231 size_(mathfu::kZeros2i),
267 GlyphFlags glyph_flags,
bool caret_info,
bool ref_count,
268 bool enable_hyphenation =
false,
bool rtl_layout =
false,
271 HashedId cache_id = kNullHash) {
274 cache_id_ = cache_id;
275 font_size_ = font_size;
276 kerning_scale_ = kerning_scale;
277 line_height_scale_ = line_height_scale;
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;
297 if (cache_id_ != kNullHash) {
298 return cache_id_ == other.cache_id_;
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_);
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);
331 if (lhs.cache_id_ != kNullHash && rhs.cache_id_ != kNullHash) {
332 return lhs.cache_id_ < rhs.cache_id_;
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);
352 const mathfu::vec2i &
get_size()
const {
return size_; }
388 if (alignment == kTextAlignmentLeft || alignment == kTextAlignmentCenter) {
405 return size_.y == 0 || size_.y > font_size_;
413 void set_size(mathfu::vec2i &size) { size_ = size; }
422 float kerning_scale_;
423 float line_height_scale_;
427 struct FontBufferFlags {
431 bool enable_hyphenation : 1;
432 GlyphFlags glyph_flags : 2;
436 uint32_t flags_value_;
437 FontBufferFlags flags_;
479 internal_leading_(0),
482 external_leading_(0) {}
487 : base_line_(base_line),
488 internal_leading_(internal_leading),
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);
517 assert(internal_leading >= 0);
528 assert(ascender >= 0);
539 assert(descender <= 0);
551 assert(external_leading <= 0);
557 return internal_leading_ + ascender_ - descender_ - external_leading_;
571 int32_t internal_leading_;
583 int32_t external_leading_;
598 position_.data[0] = x;
599 position_.data[1] = y;
600 position_.data[2] = z;
606 mathfu::vec3_packed position_;
607 mathfu::vec2_packed uv_;
617 : slice_index_(kIndexInvalid), underline_(
false), color_(kDefaultColor) {}
623 : slice_index_(kIndexInvalid), underline_(underline), color_(color) {}
634 return (slice_index_ == other.slice_index_ &&
635 underline_ == other.underline_ && color_ == other.color_);
646 size_t value = HashValue(key.slice_index_);
647 value = HashCombine<bool>(value, key.underline_);
648 value = HashCombine<uint32_t>(value, key.color_);
655 return std::tie(lhs.slice_index_, lhs.underline_, lhs.color_) <
656 std::tie(rhs.slice_index_, rhs.underline_, rhs.color_);
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_;
669 uint32_t get_color()
const {
return color_; }
672 bool get_underline()
const {
return underline_; }
675 const std::vector<UnderlineInfo> &get_underline_info()
const {
676 return underline_info_;
680 int32_t get_slice_index()
const {
return slice_index_; }
681 void set_slice_index(int32_t slice_index) { slice_index_ = slice_index; }
689 void UpdateUnderline(int32_t vertex_index,
const mathfu::vec2i &y_pos);
690 void WrapUnderline(int32_t vertex_index);
696 int32_t slice_index_;
707 std::vector<UnderlineInfo> underline_info_;
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) {}
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;
758 bool lastline_must_break()
const {
return lastline_must_break_; }
759 void set_lastline_must_break(
bool b) { lastline_must_break_ = b; }
761 bool appending_buffer()
const {
return appending_buffer_; }
762 void set_appending_buffer(
bool b) { appending_buffer_ = b; }
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; }
767 std::vector<attribute_map_it> &attribute_history() {
768 return attribute_history_;
770 std::vector<uint32_t> &word_boundary() {
771 return word_boundary_;
773 std::vector<uint32_t> &word_boundary_caret() {
774 return word_boundary_caret_;
777 HbFont *original_font()
const {
return original_font_; }
778 void set_original_font(HbFont *font) { original_font_ = font; }
780 float original_font_size()
const {
return original_font_size_; }
781 void set_original_font_size(
float size) { original_font_size_ = size; }
783 float current_font_size()
const {
return current_font_size_; }
784 void set_current_font_size(
float size) { current_font_size_ = size; }
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;
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_;
799 bool appending_buffer_;
801 HbFont *original_font_;
802 float original_font_size_;
803 float current_font_size_;
804 int32_t original_base_line_;
813 GlyphInfo(HashedId face_id, uint32_t code_point,
float size)
814 : face_id_(face_id), code_point_(code_point), size_(size) {}
822 uint32_t code_point_;
846 typedef std::map<FontBufferParameters, std::unique_ptr<FontBuffer>,
851 : size_(mathfu::kZeros2i),
854 has_ellipsis_(false),
856 line_start_indices_.push_back(0);
872 : size_(mathfu::kZeros2i),
875 has_ellipsis_(false),
877 glyph_info_.reserve(size);
879 caret_positions_.reserve(size + 1);
881 line_start_indices_.push_back(0);
892 const std::vector<FontBufferAttributes> &
get_slices()
const {
898 return indices_[index];
902 const std::vector<FontVertex> &
get_vertices()
const {
return vertices_; }
914 const mathfu::vec2i
get_size()
const {
return size_; }
939 size_t sum_indices = 0;
940 for (
size_t i = 0; i < indices_.size(); ++i) {
941 sum_indices += indices_[i].size();
955 if (!caret_positions_.capacity() || index > caret_positions_.size())
957 return caret_positions_[index];
962 return caret_positions_;
976 int32_t end_index)
const;
980 const std::vector<LinkInfo> &
get_links()
const {
return links_; }
984 return static_cast<int32_t
>(vertices_.size()) / 4;
994 friend GlyphCacheRow;
1000 std::vector<FontBufferAttributes> &
get_slices() {
return slices_; }
1003 std::vector<uint16_t> &
get_indices(int32_t index) {
return indices_[index]; }
1009 void set_metrics(
const FontMetrics &
metrics) { metrics_ =
metrics; }
1014 void set_size(
const mathfu::vec2i &size) { size_ = size; }
1025 void set_revision(uint32_t revision) { revision_ = revision; }
1031 void set_pass(int32_t pass) { pass_ = pass; }
1034 void set_iterator(fontbuffer_map_it it) { it_map_ = it; }
1037 fontbuffer_map_it get_iterator() {
return it_map_; }
1043 void AddGlyphInfo(HashedId face_id, uint32_t codepoint,
float size) {
1044 glyph_info_.push_back(GlyphInfo(face_id, codepoint, size));
1057 void AddVertices(
const mathfu::vec2 &pos, int32_t base_line,
float scale,
1058 const GlyphCacheEntry &entry);
1064 void AddIndices(int32_t buffer_idx, int32_t count);
1069 void UpdateUnderline(int32_t buffer_idx, int32_t vertex_index,
1070 const mathfu::vec2i &y_pos);
1076 void AddCaretPosition(int32_t x, int32_t y);
1081 void AddCaretPosition(
const mathfu::vec2 &pos);
1089 void AddWordBoundary(
const FontBufferParameters ¶meters,
1090 FontBufferContext *context);
1098 void UpdateUV(int32_t index,
const mathfu::vec4 &uv);
1106 void UpdateLine(
const FontBufferParameters ¶meters,
1108 FontBufferContext *context);
1116 float AdjustCurrentLine(
const FontBufferParameters ¶meters,
1117 FontBufferContext *context);
1120 void set_ref_count(uint32_t ref_count) { ref_count_ = ref_count; }
1124 int32_t GetBufferIndex(int32_t slice, FontBufferContext *context);
1127 void Invalidate() { valid_ =
false; }
1131 void AddCacheRowReference(GlyphCacheRow *p) { referencing_row_.insert(p); }
1135 void ReleaseCacheRowReference() {
1136 auto begin = referencing_row_.begin();
1137 auto end = referencing_row_.end();
1138 while (begin != end) {
1143 referencing_row_.clear();
1147 FontMetrics metrics_;
1154 std::vector<FontBufferAttributes> slices_;
1157 std::vector<std::vector<uint16_t>> indices_;
1160 std::vector<FontVertex> vertices_;
1164 std::vector<GlyphInfo> glyph_info_;
1169 std::vector<mathfu::vec2i> caret_positions_;
1174 std::vector<LinkInfo> links_;
1177 mathfu::vec2i size_;
1188 uint32_t ref_count_;
1201 std::vector<uint32_t> line_start_indices_;
1205 std::set<GlyphCacheRow *> referencing_row_;
1209 fontbuffer_map_it it_map_;
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
std::string link
Link address. If from HTML, this is the href text.
Definition: font_buffer.h:452
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
HTML link href, and the location of the link text in FontBuffer.
Definition: font_buffer.h:443
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
int32_t start_glyph_index
Definition: font_buffer.h:457
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
int32_t end_glyph_index
First glyph not in the link text in the FontBuffer.
Definition: font_buffer.h:460
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.