Store shared original string to glyph layouts directly

This commit is contained in:
Benau 2021-09-03 13:09:42 +08:00
parent 474e2e025a
commit 33a4369a71
8 changed files with 29 additions and 28 deletions

View File

@ -9,7 +9,9 @@
#include "dimension2d.h" #include "dimension2d.h"
#include <algorithm> #include <algorithm>
#include <memory>
#include <numeric> #include <numeric>
#include <string>
#include <vector> #include <vector>
namespace irr namespace irr
@ -17,6 +19,11 @@ namespace irr
namespace gui namespace gui
{ {
enum ShapeFlag
{
SF_DISABLE_CACHE = 1, /* Disable caching glyph layouts. */
};
enum GlyphLayoutFlag enum GlyphLayoutFlag
{ {
GLF_RTL_LINE = 1, /* This line from this glyph is RTL. */ GLF_RTL_LINE = 1, /* This line from this glyph is RTL. */
@ -51,6 +58,8 @@ u32 original_index;
u16 flags; u16 flags;
//! this is the face_idx used in stk face ttf //! this is the face_idx used in stk face ttf
u16 face_idx; u16 face_idx;
//! original string, which is used to map with cluster above
std::shared_ptr<std::u32string> orig_string;
}; };
namespace Private namespace Private

View File

@ -120,11 +120,9 @@ public:
*/ */
virtual void setInvisibleCharacters( const wchar_t *s ) = 0; virtual void setInvisibleCharacters( const wchar_t *s ) = 0;
//! Convert text to glyph layouts for fast rendering with caching enabled //! Convert text to glyph layouts for fast rendering with optional caching enabled. */
/* If line_data is not null, each broken line u32string will be saved and
can be used for advanced glyph and text mapping, and cache will be disabled. */
virtual void initGlyphLayouts(const core::stringw& text, virtual void initGlyphLayouts(const core::stringw& text,
std::vector<GlyphLayout>& gls, std::vector<std::u32string>* line_data = NULL) = 0; std::vector<GlyphLayout>& gls, u32 shape_flag = 0) = 0;
virtual f32 getInverseShaping() const = 0; virtual f32 getInverseShaping() const = 0;
virtual f32 getScale() const = 0; virtual f32 getScale() const = 0;

View File

@ -65,7 +65,7 @@ public:
bool vcenter=false, const core::rect<s32>* clip=0) {} bool vcenter=false, const core::rect<s32>* clip=0) {}
virtual void initGlyphLayouts(const core::stringw& text, virtual void initGlyphLayouts(const core::stringw& text,
std::vector<GlyphLayout>& gls, std::vector<std::u32string>* line_data = NULL) {} std::vector<GlyphLayout>& gls, u32 shape_flag = 0) {}
//! returns the dimension of a text //! returns the dimension of a text
virtual core::dimension2d<u32> getDimension(const wchar_t* text) const; virtual core::dimension2d<u32> getDimension(const wchar_t* text) const;

View File

@ -263,8 +263,7 @@ namespace LineBreakingRules
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/* Turn text into glyph layout for rendering by libraqm. */ /* Turn text into glyph layout for rendering by libraqm. */
void FontManager::shape(const std::u32string& text, void FontManager::shape(const std::u32string& text,
std::vector<irr::gui::GlyphLayout>& gls, std::vector<irr::gui::GlyphLayout>& gls)
std::vector<std::u32string>* line_data)
{ {
// Helper struct // Helper struct
struct ShapeGlyph struct ShapeGlyph
@ -302,6 +301,8 @@ void FontManager::shape(const std::u32string& text,
lines.push_back(U""); lines.push_back(U"");
int start = 0; int start = 0;
std::shared_ptr<std::u32string> orig_string =
std::make_shared<std::u32string>(text);
for (unsigned l = 0; l < lines.size(); l++) for (unsigned l = 0; l < lines.size(); l++)
{ {
std::vector<ShapeGlyph> glyphs; std::vector<ShapeGlyph> glyphs;
@ -516,6 +517,7 @@ void FontManager::shape(const std::u32string& text,
gl.flags |= gui::GLF_RTL_CHAR; gl.flags |= gui::GLF_RTL_CHAR;
if (FT_HAS_COLOR(glyphs[idx].ftface)) if (FT_HAS_COLOR(glyphs[idx].ftface))
gl.flags |= gui::GLF_COLORED; gl.flags |= gui::GLF_COLORED;
gl.orig_string = orig_string;
cur_line.push_back(gl); cur_line.push_back(gl);
} }
// Sort glyphs in logical order // Sort glyphs in logical order
@ -590,22 +592,19 @@ std::vector<irr::gui::GlyphLayout>&
} // getCachedLayouts } // getCachedLayouts
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** Convert text to glyph layouts for fast rendering with caching enabled /** Convert text to glyph layouts for fast rendering with (optional) caching
* If line_data is not null, each broken line u32string will be saved and * enabled.
* can be used for advanced glyph and text mapping, and cache will be
* disabled, no newline characters are allowed in text if line_data is not
* NULL.
*/ */
void FontManager::initGlyphLayouts(const core::stringw& text, void FontManager::initGlyphLayouts(const core::stringw& text,
std::vector<irr::gui::GlyphLayout>& gls, std::vector<irr::gui::GlyphLayout>& gls,
std::vector<std::u32string>* line_data) u32 shape_flag)
{ {
if (GUIEngine::isNoGraphics() || text.empty()) if (GUIEngine::isNoGraphics() || text.empty())
return; return;
if (line_data != NULL) if ((shape_flag & gui::SF_DISABLE_CACHE) != 0)
{ {
shape(StringUtils::wideToUtf32(text), gls, line_data); shape(StringUtils::wideToUtf32(text), gls);
return; return;
} }

View File

@ -132,8 +132,7 @@ public:
unsigned getShapingDPI() const { return m_shaping_dpi; } unsigned getShapingDPI() const { return m_shaping_dpi; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void shape(const std::u32string& text, void shape(const std::u32string& text,
std::vector<irr::gui::GlyphLayout>& gls, std::vector<irr::gui::GlyphLayout>& gls);
std::vector<std::u32string>* line_data = NULL);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
std::vector<irr::gui::GlyphLayout>& getCachedLayouts std::vector<irr::gui::GlyphLayout>& getCachedLayouts
(const irr::core::stringw& str); (const irr::core::stringw& str);
@ -142,7 +141,7 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void initGlyphLayouts(const irr::core::stringw& text, void initGlyphLayouts(const irr::core::stringw& text,
std::vector<irr::gui::GlyphLayout>& gls, std::vector<irr::gui::GlyphLayout>& gls,
std::vector<std::u32string>* line_data = NULL); irr::u32 shape_flag = 0);
#endif #endif
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void loadFonts(); void loadFonts();

View File

@ -154,17 +154,15 @@ s32 ScalableFont::getHeightPerLine() const
} // getHeightPerLine } // getHeightPerLine
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** Convert text to glyph layouts for fast rendering with caching enabled /** Convert text to glyph layouts for fast rendering with optional caching
* If line_data is not null, each broken line u32string will be saved and * enabled.
* can be used for advanced glyph and text mapping, and cache will be
* disabled.
*/ */
void ScalableFont::initGlyphLayouts(const core::stringw& text, void ScalableFont::initGlyphLayouts(const core::stringw& text,
std::vector<GlyphLayout>& gls, std::vector<GlyphLayout>& gls,
std::vector<std::u32string>* line_data) u32 shape_flag)
{ {
#ifndef SERVER_ONLY #ifndef SERVER_ONLY
font_manager->initGlyphLayouts(text, gls, line_data); font_manager->initGlyphLayouts(text, gls, shape_flag);
#endif #endif
} // initGlyphLayouts } // initGlyphLayouts

View File

@ -89,7 +89,7 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
virtual void initGlyphLayouts(const core::stringw& text, virtual void initGlyphLayouts(const core::stringw& text,
std::vector<GlyphLayout>& gls, std::vector<GlyphLayout>& gls,
std::vector<std::u32string>* line_data = NULL); u32 shape_flag = 0);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** returns the dimension of a text */ /** returns the dimension of a text */
virtual core::dimension2d<u32> getDimension(const wchar_t* text) const; virtual core::dimension2d<u32> getDimension(const wchar_t* text) const;

View File

@ -265,10 +265,8 @@ void NetworkingLobby::addMoreServerInfo(core::stringw info)
#ifndef SERVER_ONLY #ifndef SERVER_ONLY
const unsigned box_width = m_text_bubble->getDimension().Width; const unsigned box_width = m_text_bubble->getDimension().Width;
const float box_height = m_text_bubble->getDimension().Height; const float box_height = m_text_bubble->getDimension().Height;
// For future copy text from lobby chat
std::vector<std::u32string> text_line;
std::vector<GlyphLayout> cur_info; std::vector<GlyphLayout> cur_info;
font_manager->initGlyphLayouts(info, cur_info, &text_line); font_manager->initGlyphLayouts(info, cur_info, gui::SF_DISABLE_CACHE);
gui::IGUIFont* font = GUIEngine::getFont(); gui::IGUIFont* font = GUIEngine::getFont();
gui::breakGlyphLayouts(cur_info, box_width, gui::breakGlyphLayouts(cur_info, box_width,
font->getInverseShaping(), font->getScale()); font->getInverseShaping(), font->getScale());