Verovio
Source code documentation
resources.h
1 // Name: resources.h
3 // Author: David Bauer
4 // Created: 2022
5 // Copyright (c) Authors and others. All rights reserved.
7 
8 #ifndef __VRV_RESOURCES_H__
9 #define __VRV_RESOURCES_H__
10 
11 #include <optional>
12 #include <unordered_map>
13 
14 //----------------------------------------------------------------------------
15 
16 #include "filereader.h"
17 #include "glyph.h"
18 
19 namespace vrv {
20 
21 //----------------------------------------------------------------------------
22 // Resources
23 //----------------------------------------------------------------------------
24 
30 class Resources {
31 public:
32  using StyleAttributes = std::pair<data_FONTWEIGHT, data_FONTSTYLE>;
33  using GlyphTable = std::unordered_map<char32_t, Glyph>;
34  using GlyphNameTable = std::unordered_map<std::string, char32_t>;
35  using GlyphTextMap = std::map<StyleAttributes, GlyphTable>;
36 
41  Resources();
42  virtual ~Resources() = default;
44 
49  static std::string GetDefaultPath() { return s_defaultPath; }
50  static void SetDefaultPath(const std::string &path) { s_defaultPath = path; }
51 
52  std::string GetPath() const { return m_path; }
53  void SetPath(const std::string &path) { m_path = path; }
55 
57  bool Ok() const { return (m_loadedFonts.size() > 1); }
58 
62  void UseLiberationTextFont(bool useLiberation) { m_useLiberation = useLiberation; }
63  bool UseLiberationTextFont() const { return m_useLiberation; }
64  std::string GetTextFont() const { return ((m_useLiberation) ? "Liberation" : "Times"); }
65 
70 
71  bool InitFonts();
73  bool SetFont(const std::string &fontName);
75  bool AddCustom(const std::vector<std::string> &extraFonts);
77  bool LoadAll();
79  void SetFallbackFont(const std::string &fontName);
81  std::string GetFallbackFont() const { return m_fallbackFontName; }
82 
84  bool SetCurrentFont(const std::string &fontName, bool allowLoading = false);
85  std::string GetCurrentFont() const { return m_currentFontName; }
86  bool IsFontLoaded(const std::string &fontName) const { return m_loadedFonts.find(fontName) != m_loadedFonts.end(); }
88 
93 
94  const Glyph *GetGlyph(char32_t smuflCode) const;
96  const Glyph *GetGlyph(const std::string &smuflName) const;
98  char32_t GetGlyphCode(const std::string &smuflName) const;
100 
104  bool IsSmuflFallbackNeeded(const std::u32string &text) const;
105 
109  bool IsCurrentFontFallback() const;
110 
115 
116  void SelectTextFont(data_FONTWEIGHT fontWeight, data_FONTSTYLE fontStyle) const;
118  const Glyph *GetTextGlyph(char32_t code) const;
120  bool FontHasGlyphAvailable(const std::string &fontName, char32_t smuflCode) const;
122 
127  std::string GetCSSFontFor(const std::string &fontName) const;
128 
132  std::string GetCustomFontname(const std::string &filename, const ZipFileReader &zipFile);
133 
138  static char32_t GetSmuflGlyphForUnicodeChar(const char32_t unicodeChar);
139 
140 private:
141  //----------------------------------------------------------------------------
142  // LoadedFont
143  //----------------------------------------------------------------------------
144 
145  class LoadedFont {
146 
147  public:
148  LoadedFont(const std::string &name, bool isFallback) : m_name(name), m_isFallback(isFallback) {}
149  ~LoadedFont() {}
150  const std::string GetName() const { return m_name; };
151  const GlyphTable &GetGlyphTable() const { return m_glyphTable; };
152  GlyphTable &GetGlyphTableForModification() { return m_glyphTable; };
153  bool isFallback() const { return m_isFallback; };
154 
155  void SetCSSFont(const std::string &css) { m_css = css; }
156  std::string GetCSSFont(const std::string &path) const;
157 
158  private:
159  std::string m_name;
161  GlyphTable m_glyphTable;
163  const bool m_isFallback;
165  std::string m_css;
166  };
167 
168  //----------------------------------------------------------------------------
169 
170  bool LoadFont(const std::string &fontName, ZipFileReader *zipFile = NULL);
171 
173  bool InitTextFont(const std::string &fontName, const StyleAttributes &style);
174 
175  const GlyphTable &GetCurrentGlyphTable() const { return m_loadedFonts.at(m_currentFontName).GetGlyphTable(); };
176  const GlyphTable &GetFallbackGlyphTable() const { return m_loadedFonts.at(m_fallbackFontName).GetGlyphTable(); };
177 
178  bool m_useLiberation;
179  std::string m_path;
180  std::string m_defaultFontName;
181  std::string m_fallbackFontName;
182  std::map<std::string, LoadedFont> m_loadedFonts;
183  std::string m_currentFontName;
184 
186  GlyphTextMap m_textFont;
187  mutable StyleAttributes m_currentStyle;
191  GlyphNameTable m_glyphNameTable;
192 
194  mutable std::optional<std::pair<char32_t, const Glyph *>> m_cachedGlyph;
195 
196  //----------------//
197  // Static members //
198  //----------------//
199 
201  static thread_local std::string s_defaultPath;
202 
204  static const StyleAttributes k_defaultStyle;
205 };
206 
207 } // namespace vrv
208 
209 #endif
This class is used for storing a music font glyph.
Definition: glyph.h:31
This class provides resource values.
Definition: resources.h:30
std::string GetFallbackFont() const
Get the fallback font name.
Definition: resources.h:81
static char32_t GetSmuflGlyphForUnicodeChar(const char32_t unicodeChar)
Static method that converts unicode music code points to SMuFL equivalent.
bool FontHasGlyphAvailable(const std::string &fontName, char32_t smuflCode) const
Returns true if the specified font is loaded and it contains the requested glyph.
bool SetFont(const std::string &fontName)
Set the font to be used and loads it if necessary.
const Glyph * GetTextGlyph(char32_t code) const
Returns the glyph (if exists) for the text font (bounding box and ASCII only)
std::string GetCustomFontname(const std::string &filename, const ZipFileReader &zipFile)
Retrieve the font name either from the filename path or from the zipFile content.
void SetFallbackFont(const std::string &fontName)
Set the fallback font (Leipzig or Bravura) when some glyphs are missing in the current font.
bool IsSmuflFallbackNeeded(const std::u32string &text) const
Check if the text has any character that needs the smufl fallback font.
std::string GetCSSFontFor(const std::string &fontName) const
Get the CSS font string for the corresponding font.
const Glyph * GetGlyph(char32_t smuflCode) const
Retrieving glyphs.
bool IsCurrentFontFallback() const
Check if the current font is the fallback font.
bool AddCustom(const std::vector< std::string > &extraFonts)
Add custom (external) fonts.
void UseLiberationTextFont(bool useLiberation)
Return the name of the text font (Times or Liberation)
Definition: resources.h:62
bool Ok() const
Status checker.
Definition: resources.h:57
const Glyph * GetGlyph(const std::string &smuflName) const
Returns the glyph (if exists) for a glyph name in the current SMuFL font.
bool LoadAll()
Load all music fonts available in the resource directory.
char32_t GetGlyphCode(const std::string &smuflName) const
Returns the glyph (if exists) for a glyph name in the current SMuFL font.
void SelectTextFont(data_FONTWEIGHT fontWeight, data_FONTSTYLE fontStyle) const
Text fonts.
bool InitFonts()
Font initialization.
bool SetCurrentFont(const std::string &fontName, bool allowLoading=false)
Select a particular font.
This class is a reader for zip archives.
Definition: filereader.h:33