From 387f5cc2e74fe9585f8ac95fd0897e093c66fd38 Mon Sep 17 00:00:00 2001 From: David Bauer Date: Fri, 8 Nov 2024 12:29:24 +0100 Subject: [PATCH 01/11] Reduce number of map lookups in GetGlyph Use find instead of contains & at --- src/resources.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/resources.cpp b/src/resources.cpp index 34983535dc4..0f409a5a7da 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -169,16 +169,17 @@ bool Resources::SetCurrentFont(const std::string &fontName, bool allowLoading) const Glyph *Resources::GetGlyph(char32_t smuflCode) const { - if (GetCurrentGlyphTable().contains(smuflCode)) { - return &GetCurrentGlyphTable().at(smuflCode); + const GlyphTable ¤tTable = this->GetCurrentGlyphTable(); + if (auto glyphIter = currentTable.find(smuflCode); glyphIter != currentTable.end()) { + return &glyphIter->second; } else if (!this->IsCurrentFontFallback()) { const GlyphTable &fallbackTable = this->GetFallbackGlyphTable(); - return (fallbackTable.contains(smuflCode)) ? &fallbackTable.at(smuflCode) : NULL; - } - else { - return NULL; + if (auto glyphIter = fallbackTable.find(smuflCode); glyphIter != fallbackTable.end()) { + return &glyphIter->second; + } } + return NULL; } const Glyph *Resources::GetGlyph(const std::string &smuflName) const From fd6c235972c2c91d1a47509aa9c9df10b282b365 Mon Sep 17 00:00:00 2001 From: David Bauer Date: Fri, 8 Nov 2024 12:36:30 +0100 Subject: [PATCH 02/11] Reduce map lookup in GetGlyph by smuflName Only call GetGlyphCode once --- src/resources.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/resources.cpp b/src/resources.cpp index 0f409a5a7da..96ba9696a2e 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -184,7 +184,10 @@ const Glyph *Resources::GetGlyph(char32_t smuflCode) const const Glyph *Resources::GetGlyph(const std::string &smuflName) const { - return (this->GetGlyphCode(smuflName)) ? &GetCurrentGlyphTable().at(this->GetGlyphCode(smuflName)) : NULL; + if (const char32_t code = this->GetGlyphCode(smuflName); code) { + return this->GetGlyph(code); + } + return NULL; } char32_t Resources::GetGlyphCode(const std::string &smuflName) const From d2fc41f80d045c24f690213e5c2cdfc09b90aeec Mon Sep 17 00:00:00 2001 From: David Bauer Date: Fri, 8 Nov 2024 12:42:43 +0100 Subject: [PATCH 03/11] Reduce map lookups in GetGlyphCode Use find instead of contains & at --- src/resources.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/resources.cpp b/src/resources.cpp index 96ba9696a2e..517ca7749e6 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -192,7 +192,10 @@ const Glyph *Resources::GetGlyph(const std::string &smuflName) const char32_t Resources::GetGlyphCode(const std::string &smuflName) const { - return m_glyphNameTable.contains(smuflName) ? m_glyphNameTable.at(smuflName) : 0; + if (auto glyphNameIter = m_glyphNameTable.find(smuflName); glyphNameIter != m_glyphNameTable.end()) { + return glyphNameIter->second; + } + return 0; } bool Resources::IsSmuflFallbackNeeded(const std::u32string &text) const From 9d049f5e90763e518423ce0ae697484b71861f6c Mon Sep 17 00:00:00 2001 From: David Bauer Date: Fri, 8 Nov 2024 13:16:34 +0100 Subject: [PATCH 04/11] Cache the last glyph that was looked up --- include/vrv/resources.h | 3 +++ src/resources.cpp | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/include/vrv/resources.h b/include/vrv/resources.h index 636e0c8a41f..c9c433e8d93 100644 --- a/include/vrv/resources.h +++ b/include/vrv/resources.h @@ -177,6 +177,9 @@ class Resources { */ GlyphNameTable m_glyphNameTable; + /** Cache of the last glyph that was looked up in loaded fonts */ + mutable std::optional> m_cachedGlyph; + //----------------// // Static members // //----------------// diff --git a/src/resources.cpp b/src/resources.cpp index 517ca7749e6..2bdaae6efab 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -59,6 +59,7 @@ Resources::Resources() bool Resources::InitFonts() { + m_cachedGlyph.reset(); m_loadedFonts.clear(); // Font Bravura first. As it is expected to have always all symbols we build the code -> name table from it @@ -95,6 +96,8 @@ bool Resources::InitFonts() bool Resources::SetFont(const std::string &fontName) { + m_cachedGlyph.reset(); + // and the default font provided in options, if it is not one of the previous if (!fontName.empty() && !IsFontLoaded(fontName)) { if (!LoadFont(fontName)) { @@ -148,12 +151,15 @@ bool Resources::LoadAll() bool Resources::SetFallback(const std::string &fontName) { + m_cachedGlyph.reset(); m_fallbackFontName = fontName; return true; } bool Resources::SetCurrentFont(const std::string &fontName, bool allowLoading) { + m_cachedGlyph.reset(); + if (IsFontLoaded(fontName)) { m_currentFontName = fontName; return true; @@ -169,14 +175,22 @@ bool Resources::SetCurrentFont(const std::string &fontName, bool allowLoading) const Glyph *Resources::GetGlyph(char32_t smuflCode) const { + if (m_cachedGlyph && m_cachedGlyph->first == smuflCode) { + return m_cachedGlyph->second; + } + const GlyphTable ¤tTable = this->GetCurrentGlyphTable(); if (auto glyphIter = currentTable.find(smuflCode); glyphIter != currentTable.end()) { - return &glyphIter->second; + const Glyph *glyph = &glyphIter->second; + m_cachedGlyph = std::make_pair(glyphIter->first, glyph); + return glyph; } else if (!this->IsCurrentFontFallback()) { const GlyphTable &fallbackTable = this->GetFallbackGlyphTable(); if (auto glyphIter = fallbackTable.find(smuflCode); glyphIter != fallbackTable.end()) { - return &glyphIter->second; + const Glyph *glyph = &glyphIter->second; + m_cachedGlyph = std::make_pair(glyphIter->first, glyph); + return glyph; } } return NULL; From e922baa11358db848bb12a7d84cba98574c27f0e Mon Sep 17 00:00:00 2001 From: David Bauer Date: Sat, 9 Nov 2024 17:05:51 +0100 Subject: [PATCH 05/11] Fix comment typo --- src/resources.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resources.cpp b/src/resources.cpp index 2bdaae6efab..4fbd86d92c7 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -98,7 +98,7 @@ bool Resources::SetFont(const std::string &fontName) { m_cachedGlyph.reset(); - // and the default font provided in options, if it is not one of the previous + // add the default font provided in options, if it is not one of the previous if (!fontName.empty() && !IsFontLoaded(fontName)) { if (!LoadFont(fontName)) { LogError("%s font could not be loaded.", fontName.c_str()); From 3deeec9e29cd70bca5d464bdb21f957e893c9162 Mon Sep 17 00:00:00 2001 From: David Bauer Date: Sat, 9 Nov 2024 17:18:56 +0100 Subject: [PATCH 06/11] Rename setter of fallback font and remove return It always returned true --- include/vrv/resources.h | 2 +- src/resources.cpp | 3 +-- src/toolkit.cpp | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/include/vrv/resources.h b/include/vrv/resources.h index c9c433e8d93..6a57755e87d 100644 --- a/include/vrv/resources.h +++ b/include/vrv/resources.h @@ -65,7 +65,7 @@ class Resources { /** Load all music fonts available in the resource directory */ bool LoadAll(); /** Set the fallback font (Leipzig or Bravura) when some glyphs are missing in the current font */ - bool SetFallback(const std::string &fontName); + void SetFallbackFont(const std::string &fontName); /** Get the fallback font name */ std::string GetFallbackFont() const { return m_defaultFontName; } /** Init the text font (bounding boxes and ASCII only) */ diff --git a/src/resources.cpp b/src/resources.cpp index 4fbd86d92c7..fcaaa029630 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -149,11 +149,10 @@ bool Resources::LoadAll() return success; } -bool Resources::SetFallback(const std::string &fontName) +void Resources::SetFallbackFont(const std::string &fontName) { m_cachedGlyph.reset(); m_fallbackFontName = fontName; - return true; } bool Resources::SetCurrentFont(const std::string &fontName, bool allowLoading) diff --git a/src/toolkit.cpp b/src/toolkit.cpp index 0d031038af1..e9cfcc77356 100644 --- a/src/toolkit.cpp +++ b/src/toolkit.cpp @@ -127,7 +127,7 @@ bool Toolkit::SetResourcePath(const std::string &path) success = success && this->SetFont(m_options->m_font.GetValue()); } if (m_options->m_fontFallback.IsSet()) { - success = success && resources.SetFallback(m_options->m_fontFallback.GetStrValue()); + resources.SetFallbackFont(m_options->m_fontFallback.GetStrValue()); } if (m_options->m_fontLoadAll.IsSet()) { success = success && resources.LoadAll(); @@ -1224,7 +1224,7 @@ bool Toolkit::SetOptions(const std::string &jsonOptions) } if (json.has("fontFallback")) { Resources &resources = m_doc.GetResourcesForModification(); - resources.SetFallback(m_options->m_fontFallback.GetStrValue()); + resources.SetFallbackFont(m_options->m_fontFallback.GetStrValue()); } if (json.has("fontLoadAll")) { Resources &resources = m_doc.GetResourcesForModification(); From 5e81502f6b86fe3a4a58c4acf5ee8fd0c822c7dd Mon Sep 17 00:00:00 2001 From: David Bauer Date: Sat, 9 Nov 2024 17:23:22 +0100 Subject: [PATCH 07/11] Make internal method private --- include/vrv/resources.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/vrv/resources.h b/include/vrv/resources.h index 6a57755e87d..bffc7a565f3 100644 --- a/include/vrv/resources.h +++ b/include/vrv/resources.h @@ -68,8 +68,6 @@ class Resources { void SetFallbackFont(const std::string &fontName); /** Get the fallback font name */ std::string GetFallbackFont() const { return m_defaultFontName; } - /** Init the text font (bounding boxes and ASCII only) */ - bool InitTextFont(const std::string &fontName, const StyleAttributes &style); /** Select a particular font */ bool SetCurrentFont(const std::string &fontName, bool allowLoading = false); @@ -160,6 +158,9 @@ class Resources { bool LoadFont(const std::string &fontName, ZipFileReader *zipFile = NULL); + /** Init the text font (bounding boxes and ASCII only) */ + bool InitTextFont(const std::string &fontName, const StyleAttributes &style); + const GlyphTable &GetCurrentGlyphTable() const { return m_loadedFonts.at(m_currentFontName).GetGlyphTable(); }; const GlyphTable &GetFallbackGlyphTable() const { return m_loadedFonts.at(m_fallbackFontName).GetGlyphTable(); }; From 7eec4f34ea52404720598d8bbc34c112f4b47d39 Mon Sep 17 00:00:00 2001 From: David Bauer Date: Sat, 9 Nov 2024 17:32:00 +0100 Subject: [PATCH 08/11] Fix getter of fallback font --- include/vrv/resources.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/vrv/resources.h b/include/vrv/resources.h index bffc7a565f3..be4737c2d1e 100644 --- a/include/vrv/resources.h +++ b/include/vrv/resources.h @@ -67,7 +67,7 @@ class Resources { /** Set the fallback font (Leipzig or Bravura) when some glyphs are missing in the current font */ void SetFallbackFont(const std::string &fontName); /** Get the fallback font name */ - std::string GetFallbackFont() const { return m_defaultFontName; } + std::string GetFallbackFont() const { return m_fallbackFontName; } /** Select a particular font */ bool SetCurrentFont(const std::string &fontName, bool allowLoading = false); From 4188cc430e209db27126a67fce89115843882dc2 Mon Sep 17 00:00:00 2001 From: David Bauer Date: Sat, 9 Nov 2024 17:38:36 +0100 Subject: [PATCH 09/11] Simplify return statement --- src/resources.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/resources.cpp b/src/resources.cpp index fcaaa029630..9288aa7f37f 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -167,9 +167,8 @@ bool Resources::SetCurrentFont(const std::string &fontName, bool allowLoading) m_currentFontName = fontName; return true; } - else { - return false; - } + + return false; } const Glyph *Resources::GetGlyph(char32_t smuflCode) const @@ -234,12 +233,7 @@ bool Resources::FontHasGlyphAvailable(const std::string &fontName, char32_t smuf } const GlyphTable &table = m_loadedFonts.at(fontName).GetGlyphTable(); - if (table.find(smuflCode) != table.end()) { - return true; - } - else { - return false; - } + return (table.find(smuflCode) != table.end()); } std::string Resources::GetCSSFontFor(const std::string &fontName) const From 620f513661a2b3256c2cc204def64614b51b0b7e Mon Sep 17 00:00:00 2001 From: David Bauer Date: Sat, 9 Nov 2024 18:10:49 +0100 Subject: [PATCH 10/11] Use std::all_of in LoadAll --- src/resources.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/resources.cpp b/src/resources.cpp index 9288aa7f37f..a9611ed8dc5 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -135,18 +135,18 @@ bool Resources::AddCustom(const std::vector &extraFonts) bool Resources::LoadAll() { - bool success = true; std::string path = Resources::GetPath() + "/"; - for (const std::filesystem::directory_entry &entry : std::filesystem::directory_iterator(path)) { - const std::filesystem::path path = entry.path(); - if (path.has_extension() && path.has_stem() && path.extension() == ".xml") { - const std::string fontName = path.stem().string(); - if (!IsFontLoaded(fontName)) { - success = success && LoadFont(fontName); + return std::ranges::all_of( + std::filesystem::directory_iterator(path), [this](const std::filesystem::directory_entry &entry) { + const std::filesystem::path &path = entry.path(); + if (path.has_extension() && path.has_stem() && path.extension() == ".xml") { + const std::string fontName = path.stem().string(); + if (!this->IsFontLoaded(fontName) && !this->LoadFont(fontName)) { + return false; + } } - } - } - return success; + return true; + }); } void Resources::SetFallbackFont(const std::string &fontName) @@ -167,7 +167,7 @@ bool Resources::SetCurrentFont(const std::string &fontName, bool allowLoading) m_currentFontName = fontName; return true; } - + return false; } From ffc5e132d96da6e32e5bc143fe1ccdf8891f4342 Mon Sep 17 00:00:00 2001 From: David Bauer Date: Sat, 9 Nov 2024 18:49:06 +0100 Subject: [PATCH 11/11] Add missing include --- include/vrv/resources.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/vrv/resources.h b/include/vrv/resources.h index be4737c2d1e..79b976e1c3b 100644 --- a/include/vrv/resources.h +++ b/include/vrv/resources.h @@ -8,6 +8,7 @@ #ifndef __VRV_RESOURCES_H__ #define __VRV_RESOURCES_H__ +#include #include //----------------------------------------------------------------------------