15 #ifndef FONT_MANAGER_H
16 #define FONT_MANAGER_H
20 #include "fplbase/renderer.h"
21 #include "fplutil/mutex.h"
23 #include "flatui/font_util.h"
24 #include "flatui/version.h"
25 #include "flatui/internal/distance_computer.h"
26 #include "flatui/internal/glyph_cache.h"
27 #include "flatui/internal/flatui_util.h"
28 #include "flatui/internal/hb_complex_font.h"
29 #include "flatui/internal/hyphenator.h"
31 #if defined(__APPLE__) || defined(__ANDROID__)
32 #define FLATUI_SYSTEM_FONT (1)
33 #endif // defined(__APPLE__) || defined(__ANDROID__)
36 typedef struct FT_LibraryRec_ *FT_Library;
37 typedef struct FT_GlyphSlotRec_ *FT_GlyphSlot;
38 typedef unsigned long FT_ULong;
41 typedef const struct hb_language_impl_t *hb_language_t;
56 class FontBufferContext;
65 const int32_t kVerticesPerGlyph = 4;
89 #ifdef FLATUI_SYSTEM_FONT
96 static const HashedId kSystemFontId = HashId(
kSystemFont);
97 #endif // FLATUI_SYSTEM_FONT
123 FontManager(
const mathfu::vec2i &cache_size, int32_t max_slices);
137 bool Open(
const char *font_name);
156 bool Close(
const char *font_name);
190 bool SelectFont(
const char *font_names[], int32_t count);
286 for (
auto it = map_buffers_.begin(); it != map_buffers_.end(); it++) {
288 if (!it->first.get_ref_count_flag()) {
289 map_buffers_.erase(it);
310 if (!(slice & kGlyphFormatsColor)) {
311 return glyph_cache_->get_monochrome_buffer()->get_texture(slice);
313 return glyph_cache_->get_color_buffer()->get_texture(slice &
314 ~kGlyphFormatsColor);
327 size_selector_.swap(selector);
356 if (direction == kTextLayoutDirectionTTB) {
357 fplbase::LogError(
"TextLayoutDirectionTTB is not supported yet.");
361 layout_direction_ = direction;
373 if (hyb_path !=
nullptr) {
374 hyb_path_ = hyb_path;
376 if (!hyphenation_rule_.empty() && !hyb_path_.empty()) {
377 std::string pattern_file =
378 hyb_path_ +
"/hyph-" + hyphenation_rule_ +
".hyb";
379 hyphenator_.Open(pattern_file.c_str());
407 ellipsis_ = ellipsis;
408 ellipsis_mode_ = mode;
420 static const int32_t kRenderPass = -1;
428 static void Terminate();
432 int32_t LayoutText(
const char *text,
size_t length, int32_t max_width = 0,
433 int32_t current_width = 0,
bool last_line =
false,
434 bool enable_hyphenation =
false,
435 int32_t *rewind =
nullptr);
438 bool UpdateBuffer(
const WordEnumerator &word_enum,
449 bool AppendEllipsis(
const WordEnumerator &word_enum,
457 bool UpdateMetrics(int32_t top, int32_t height,
469 const GlyphCacheEntry *GetCachedEntry(uint32_t code_point, uint32_t y_size,
481 bool UpdatePass(
bool start_subpass);
488 int32_t ConvertSize(int32_t size);
492 int32_t GetCaretPosCount(
const WordEnumerator &enumerator,
493 const hb_glyph_info_t *info, int32_t glyph_count,
498 FontBuffer *CreateBuffer(
const char *text, uint32_t length,
500 mathfu::vec2 *text_pos =
nullptr);
506 FontBuffer *FillBuffer(
const char *text, uint32_t length,
509 mathfu::vec2 *text_pos =
nullptr);
512 void SetLanguageSettings();
515 int32_t Hyphenate(
const char *text,
size_t length, int32_t available_space,
519 void SetFontProperties(
const HtmlSection &font_section,
524 const ScriptInfo *FindLocale(
const char *locale);
527 bool IsLanguageSupported(
const char *language);
533 void SetLineHeightScale(
float line_height_scale) {
534 line_height_scale_ = line_height_scale;
542 void SetKerningScale(
float kerning_scale) { kerning_scale_ = kerning_scale; }
548 bool OpenSystemFont();
554 bool UpdateFontCoverage(FT_Face face, std::set<FT_ULong> *font_coverage);
558 bool OpenSystemFontApple();
559 bool CloseSystemFontApple();
562 bool OpenSystemFontAndroid();
563 bool CloseSystemFontAndroid();
564 void ReorderSystemFonts(std::vector<FontFamily> *font_list)
const;
565 #endif // __ANDROID__
569 bool CloseSystemFont();
572 bool face_initialized_;
575 std::unordered_map<std::string, std::unique_ptr<FaceData>> map_faces_;
578 HbFontCache font_cache_;
581 HbFont *current_font_;
586 std::map<FontBufferParameters, std::unique_ptr<FontBuffer>,
587 FontBufferParameters> map_buffers_;
590 static FT_Library *ft_;
593 static hb_buffer_t *harfbuzz_buf_;
596 std::unique_ptr<GlyphCache> glyph_cache_;
599 int32_t current_atlas_revision_;
601 int32_t atlas_last_flush_revision_;
605 int32_t current_pass_;
608 std::function<int32_t(const int32_t)> size_selector_;
613 std::string language_;
616 static const ScriptInfo script_table_[];
617 static const char *language_table_[];
618 hb_language_t hb_language_;
621 float line_height_scale_;
622 float kerning_scale_;
628 std::string ellipsis_;
632 std::string hyb_path_;
633 std::string hyphenation_rule_;
634 Hyphenator hyphenator_;
637 std::vector<char> wordbreak_info_;
640 std::vector<int32_t> fontface_index_;
645 DistanceComputer<uint8_t> sdf_computer_;
648 fplutil::Mutex *cache_mutex_;
651 std::vector<FontFamily> system_fallback_list_;
653 const FlatUIVersion *version_;
691 void set(fplbase::Shader *shader) {
694 pos_offset_ = shader->FindUniform(
"pos_offset");
695 color_ = shader->FindUniform(
"color");
696 clipping_ = shader->FindUniform(
"clipping");
697 threshold_ = shader->FindUniform(
"threshold");
699 void set_renderer(
const fplbase::Renderer &renderer) {
700 shader_->Set(renderer);
703 void set_position_offset(
const mathfu::vec3 &vec) {
704 assert(fplbase::ValidUniformHandle(pos_offset_));
705 shader_->SetUniform(pos_offset_, vec);
707 void set_color(
const mathfu::vec4 &vec) {
708 assert(fplbase::ValidUniformHandle(color_));
709 shader_->SetUniform(color_, vec);
711 void set_clipping(
const mathfu::vec4 &vec) {
712 assert(fplbase::ValidUniformHandle(clipping_));
713 shader_->SetUniform(clipping_, vec);
715 void set_threshold(
float f) {
716 assert(fplbase::ValidUniformHandle(threshold_));
717 shader_->SetUniform(threshold_, &f, 1);
720 fplbase::UniformHandle clipping_handle() {
return clipping_; }
721 fplbase::UniformHandle color_handle() {
return color_; }
722 fplbase::UniformHandle position_offset_handle() {
return pos_offset_; }
723 fplbase::UniformHandle threshold_handle() {
return threshold_; }
726 fplbase::Shader *shader_;
727 fplbase::UniformHandle pos_offset_;
728 fplbase::UniformHandle color_;
729 fplbase::UniformHandle clipping_;
730 fplbase::UniformHandle threshold_;
736 #endif // FONT_MANAGER_H
Helper class to handle shaders for a font rendering. The class keeps a reference to a shader...
Definition: font_manager.h:689
const char * GetLanguage()
Definition: font_manager.h:340
void RemapBuffers(bool flush_cache)
Optionally flush the glyph cache and update existing FontBuffer's UV information. The API doesn't cha...
const char * hyphenation
Hyphenation rule.
Definition: font_manager.h:674
const int32_t kGlyphCacheWidth
The default size of the glyph cache width.
Definition: font_manager.h:71
const int32_t kGlyphCacheHeight
The default size of the glyph cache height.
Definition: font_manager.h:76
FontManager manages font rendering with OpenGL utilizing freetype and harfbuzz as a glyph rendering a...
Definition: font_manager.h:109
TextLayoutDirection GetLayoutDirection()
Definition: font_manager.h:384
bool SelectFont(const char *font_name)
Select the current font face. The font face will be used by a glyph rendering.
const char *const kDefaultLanguage
The default language used for a line break.
Definition: font_manager.h:87
const int32_t kIndicesPerGlyph
The number of vertices/indices per a glyph entry.
Definition: font_manager.h:66
void StartLayoutPass()
Indicate a start of new layout pass.
FontBufferStatus GetFontBufferStatus(const FontBuffer &font_buffer) const
Check a status of the font buffer.
void EnableColorGlyph()
Enable an use of color glyphs in supporting OTF/TTF font. The API will initialize internal glyph cach...
bool Close(const char *font_name)
Discard a font face that has been opened via Open().
HbFont * GetCurrentFont()
Definition: font_manager.h:387
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
bool Open(const char *font_name)
Open a font face, TTF, OT font.
FontBufferStatus
A status of FontBuffer correspoinding current glyph cache contents. Enumerations: ...
Definition: font_buffer.h:128
bool FontLoaded()
Definition: font_manager.h:255
void ReleaseBuffer(FontBuffer *buffer)
Release the FonBuffer instance. If the FontBuffer is a reference counting buffer, the API decrements ...
const int32_t kGlyphCacheMaxSlices
The default size of the max glyph cache slices. The number of cache slices grows up to the value...
Definition: font_manager.h:82
TextLayoutDirection direction
Script layout direciton.
Definition: font_manager.h:678
~FontManager()
The destructor for FontManager.
void FlushLayout()
Flush the existing FontBuffer in the cache.
Definition: font_manager.h:285
fplbase::Texture * GetAtlasTexture(int32_t slice)
Definition: font_manager.h:307
void SetLocale(const char *locale)
void SetSizeSelector(std::function< int32_t(const int32_t)> selector)
The user can supply a size selector function to adjust glyph sizes when storing a glyph cache entry...
Definition: font_manager.h:326
void SetTextEllipsis(const char *ellipsis, EllipsisMode mode=kEllipsisModeTruncateCharacter)
Set an ellipsis string used in label/edit widgets.
Definition: font_manager.h:405
FontBuffer * GetBuffer(const char *text, size_t length, const FontBufferParameters ¶meters)
Retrieve a vertex buffer for a font rendering using glyph cache.
This struct holds the script information used for a text layout.
Definition: font_manager.h:660
bool StartRenderPass()
Indicates a start of new render pass.
Definition: font_manager.h:300
this is used with the texture atlas rendering.
Definition: font_buffer.h:832
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
const char *const kSystemFont
A constant to spefify loading a system font. Used with OpenFont() and SelectFont() API Currently the ...
Definition: font_manager.h:95
void SetLayoutDirection(const TextLayoutDirection direction)
Set a script layout direction.
Definition: font_manager.h:355
EllipsisMode
A flag controlling appending behavior of the ellipsis. Enumerations:
Definition: font_buffer.h:139
FontBuffer * GetHtmlBuffer(const char *html, const FontBufferParameters ¶meters)
Retrieve a vertex buffer for basic HTML rendering.
const char * locale
A C-string corresponding to the of the language defined in ISO 639 and the country code difined in IS...
Definition: font_manager.h:666
const char * script
ISO 15924 Script code.
Definition: font_manager.h:670
bool FlushAndUpdate()
Flush the existing glyph cache contents and start new layout pass.
Definition: font_manager.h:280
FontManager()
The default constructor for FontManager.
void SetScript(const char *script)
Set a script used for a script layout.
void SetupHyphenationPatternPath(const char *hyb_path)
Set up hyphenation pattern path. Since the hyphenation pattern is different per locale, current locale needs to be set properly for the hyphnation to work.
Definition: font_manager.h:372