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 <map>
12 #include <optional>
13 #include <unordered_map>
14 
15 //----------------------------------------------------------------------------
16 
17 #include "fontstore.h"
18 #include "glyph.h"
19 
20 namespace vrv {
21 
22 class FontInfo;
23 
24 //----------------------------------------------------------------------------
25 // Resources
26 //----------------------------------------------------------------------------
27 
33 class Resources {
34 public:
35  using GlyphNameTable = std::unordered_map<std::string, char32_t>;
36  using FaceStyle = std::pair<FontStore::Weight, FontStore::Style>;
37 
42  Resources();
43  virtual ~Resources() = default;
45 
50  static std::string GetDefaultPath() { return s_defaultPath; }
51  static void SetDefaultPath(const std::string &path) { s_defaultPath = path; }
52 
53  std::string GetPath() const { return m_path; }
54  void SetPath(const std::string &path) { m_path = path; }
55  const FontStore &GetFontStore() const { return m_fontStore; }
56  FontStore &GetFontStoreForModification() { return m_fontStore; }
58 
60  bool Ok() const;
61 
66 
67  bool InitFonts();
72  std::optional<FontStore::FontFile> GetMusicFontForEmbedding(
73  const std::string &fontName, std::string &bundledFile) const;
75  bool SetFallbackFont(const std::string &fontName);
77  std::string GetFallbackFont() const { return m_fallbackFontName; }
78 
80  bool SetCurrentFont(const std::string &fontName);
81  std::string GetCurrentFont() const { return m_currentFontName; }
82  bool IsFontLoaded(const std::string &fontName) const;
84 
89 
90  const Glyph *GetGlyph(char32_t smuflCode) const;
92  const Glyph *GetGlyph(char32_t smuflCode, const std::string &fontName) const;
94  const Glyph *GetGlyph(const std::string &smuflName) const;
96  char32_t GetGlyphCode(const std::string &smuflName) const;
98 
102  bool IsSmuflFallbackNeeded(const std::u32string &text) const;
103 
108 
109  bool SetTextFont(const std::string &fontName);
111  std::string GetTextFont() const { return m_textFontName; }
113  std::string GetTextFontMetricEquivalent() const;
115  std::optional<FontStore::ShapedRun> ShapeText(const FontInfo &font, const std::u32string &text) const;
117  int GetTextAdvance(const FontInfo &font, const FontStore::ShapedRun &run) const;
119  const Glyph *GetTextGlyph(char32_t code, const FontInfo &font) const;
121  const Glyph *GetRuntimeGlyph(FontStore::FaceIdentity face, int glyphId, const std::string &code = "") const;
123  bool FontHasGlyphAvailable(const std::string &fontName, char32_t smuflCode) const;
125 
130  static char32_t GetSmuflGlyphForUnicodeChar(const char32_t unicodeChar);
131 
135  static FaceStyle GetFaceStyle(const FontInfo &font);
136 
142  static std::string FindSmuflMetadata(const std::string &fontFilename);
143 
144 private:
145  std::string m_path;
146  std::string m_fallbackFontName;
147  std::string m_currentFontName;
148  std::string m_textFontName;
149 
151  mutable std::optional<std::pair<char32_t, const Glyph *>> m_cachedGlyph;
152 
154  mutable std::unordered_map<uint64_t, std::map<std::pair<int, std::string>, Glyph>> m_runtimeGlyphs;
155 
157  std::map<std::string, std::vector<unsigned char>> m_musicFontSubsets;
158 
160  FontStore m_fontStore;
161 
162  //----------------//
163  // Static members //
164  //----------------//
165 
167  static thread_local std::string s_defaultPath;
168 };
169 
170 } // namespace vrv
171 
172 #endif
This class is store font properties.
Definition: devicecontextbase.h:137
This class registers OpenType fonts at runtime and gives access to their glyph metrics,...
Definition: fontstore.h:27
This class is used for storing a music font glyph.
Definition: glyph.h:32
This class provides resource values.
Definition: resources.h:33
std::string GetFallbackFont() const
Get the fallback font name.
Definition: resources.h:77
std::string GetTextFont() const
Return the default text family.
Definition: resources.h:111
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.
int GetTextAdvance(const FontInfo &font, const FontStore::ShapedRun &run) const
Return the scaled advance of a shaped run, including letter spacing.
std::string GetTextFontMetricEquivalent() const
The widespread font family with the same metrics as the text font, if any (e.g., Times for Tinos)
static FaceStyle GetFaceStyle(const FontInfo &font)
Static method that returns the weight and style of the face for a drawing font.
bool SetFallbackFont(const std::string &fontName)
Set the music fallback family, which has to be registered.
bool IsSmuflFallbackNeeded(const std::u32string &text) const
Check if the text has any character that needs the smufl fallback font.
const Glyph * GetRuntimeGlyph(FontStore::FaceIdentity face, int glyphId, const std::string &code="") const
Returns a cached runtime glyph by immutable face identity and glyph ID.
const Glyph * GetGlyph(char32_t smuflCode) const
Retrieving glyphs.
const Glyph * GetTextGlyph(char32_t code, const FontInfo &font) const
Returns a glyph from the runtime face selected by a drawing font.
static std::string FindSmuflMetadata(const std::string &fontFilename)
Static method that looks for the SMuFL metadata file of a music font file.
std::optional< FontStore::ShapedRun > ShapeText(const FontInfo &font, const std::u32string &text) const
Shape text using the family and style requested by a drawing font.
bool SetTextFont(const std::string &fontName)
Text fonts.
bool Ok() const
Status checker.
const Glyph * GetGlyph(const std::string &smuflName) const
Returns the glyph (if exists) for a glyph name in the current SMuFL font.
char32_t GetGlyphCode(const std::string &smuflName) const
Returns the glyph (if exists) for a glyph name in the current SMuFL font.
bool SetCurrentFont(const std::string &fontName)
Select a particular font.
const Glyph * GetGlyph(char32_t smuflCode, const std::string &fontName) const
Returns a music glyph using an explicit registered family.
std::optional< FontStore::FontFile > GetMusicFontForEmbedding(const std::string &fontName, std::string &bundledFile) const
Return the font file of a music font for embedding in the SVG, which is a subset for the bundled font...
bool InitFonts()
Font initialization.
The identity of a face, which is the same for identical font data.
Definition: fontstore.h:34
A shaped text run.
Definition: fontstore.h:73