Verovio
Source code documentation
fontstore.h
1 // Name: fontstore.h
3 // Author: Simon Waloschek
4 // Created: 2026
5 // Copyright (c) Authors and others. All rights reserved.
7 
8 #ifndef __VRV_FONTSTORE_H__
9 #define __VRV_FONTSTORE_H__
10 
11 #include <cstdint>
12 #include <memory>
13 #include <optional>
14 #include <string>
15 #include <vector>
16 
17 namespace vrv {
18 
19 //----------------------------------------------------------------------------
20 // FontStore
21 //----------------------------------------------------------------------------
22 
27 class FontStore {
28 public:
29  enum class Kind { Music, Text };
30  enum class Weight { Normal, Bold };
31  enum class Style { Normal, Italic };
32 
34  struct FaceIdentity {
35  uint64_t m_value = 0;
36  bool operator==(const FaceIdentity &) const = default;
37  explicit operator bool() const { return m_value != 0; }
38  };
39 
41  struct GlyphMetrics {
42  FaceIdentity m_face;
43  int m_glyphId = 0;
44  int m_unitsPerEm = 0;
45  int m_advanceX = 0;
46  int m_xBearing = 0;
47  int m_yBearing = 0;
48  int m_width = 0;
49  int m_height = 0;
50  };
51 
53  struct GlyphAnchor {
54  std::string m_name;
55  double m_x = 0.0;
56  double m_y = 0.0;
57  };
58 
60  struct GlyphPlacement {
61  FaceIdentity m_face;
62  int m_unitsPerEm = 0;
63  int m_glyphId = 0;
64  int m_cluster = 0;
65  int m_advanceX = 0;
66  int m_advanceY = 0;
67  int m_offsetX = 0;
68  int m_offsetY = 0;
69  bool operator==(const GlyphPlacement &) const = default;
70  };
71 
73  struct ShapedRun {
74  FaceIdentity m_face;
75  int m_unitsPerEm = 0;
76  std::vector<GlyphPlacement> m_glyphs;
77  bool operator==(const ShapedRun &) const = default;
78 
80  int GetClusterGapCount() const;
81  };
82 
84  struct FontFile {
85  Weight m_weight = Weight::Normal;
86  Style m_style = Style::Normal;
88  std::string m_format;
89  std::string m_mimeType;
90  std::vector<unsigned char> m_data;
91  };
92 
94  struct Counters {
95  int m_decodedFonts = 0;
96  int m_extractedMetrics = 0;
97  int m_extractedOutlines = 0;
98  int m_shapedRuns = 0;
99  };
100 
105  FontStore();
106  ~FontStore();
107  FontStore(FontStore &&) noexcept;
108  FontStore &operator=(FontStore &&) noexcept;
109  FontStore(const FontStore &) = delete;
110  FontStore &operator=(const FontStore &) = delete;
112 
119  std::string RegisterTextFont(const unsigned char *data, int length, const std::string &alias = "");
120  std::string RegisterMusicFont(
121  const unsigned char *data, int length, const std::string &smuflMetadataJson, const std::string &alias = "");
123 
128  bool HasFace(
129  Kind kind, const std::string &family, Weight weight = Weight::Normal, Style style = Style::Normal) const;
130  std::optional<GlyphMetrics> GetGlyphMetrics(Kind kind, const std::string &family, char32_t codepoint,
131  Weight weight = Weight::Normal, Style style = Style::Normal) const;
132  std::optional<GlyphMetrics> GetGlyphMetrics(FaceIdentity face, int glyphId) const;
133  std::optional<std::string> GetGlyphOutline(Kind kind, const std::string &family, int glyphId,
134  Weight weight = Weight::Normal, Style style = Style::Normal) const;
135  std::optional<std::string> GetGlyphOutline(FaceIdentity face, int glyphId) const;
136  std::vector<GlyphAnchor> GetMusicGlyphAnchors(const std::string &family, const std::string &glyphName) const;
138  std::vector<FontFile> GetFontFiles(Kind kind, const std::string &family) const;
140 
145  std::optional<ShapedRun> ShapeText(const std::string &family, const std::u32string &text,
146  Weight weight = Weight::Normal, Style style = Style::Normal, const std::string &musicFamily = "",
147  const std::string &musicFallbackFamily = "") const;
148 
150  uint64_t GetGeneration() const;
151 
154 
157 
159  static std::vector<unsigned char> ReadFile(const std::string &filename);
160 
161 private:
162  class Impl;
163  std::unique_ptr<Impl> m_impl;
164 };
165 
166 } // namespace vrv
167 
168 #endif // __VRV_FONTSTORE_H__
This class registers OpenType fonts at runtime and gives access to their glyph metrics,...
Definition: fontstore.h:27
void PinBundledData()
Pin the currently registered bundled faces in the process cache.
Counters GetCounters() const
Return the amount of work done by the store.
std::optional< ShapedRun > ShapeText(const std::string &family, const std::u32string &text, Weight weight=Weight::Normal, Style style=Style::Normal, const std::string &musicFamily="", const std::string &musicFallbackFamily="") const
Shape a text with a text family.
static std::vector< unsigned char > ReadFile(const std::string &filename)
Read a font or metadata file, which is empty if it cannot be read or is too large to be registered.
std::vector< FontFile > GetFontFiles(Kind kind, const std::string &family) const
Return the files of all faces registered for a family (or alias), without synthesized faces.
uint64_t GetGeneration() const
Return a value changed with each registration.
This class models CDATA (text)
Definition: text.h:23
The amount of work done by the store, for checking that the glyph data is extracted lazily.
Definition: fontstore.h:94
The identity of a face, which is the same for identical font data.
Definition: fontstore.h:34
A registered font file, kept in the format it was registered with (e.g., WOFF2 is not decompressed)
Definition: fontstore.h:84
std::string m_format
The CSS @font-face format: "woff2", "woff", "opentype", or "truetype".
Definition: fontstore.h:88
A SMuFL anchor of a glyph, in staff spaces.
Definition: fontstore.h:53
The metrics of a glyph, in font units.
Definition: fontstore.h:41
The placement of a glyph in a shaped run, in font units.
Definition: fontstore.h:60
A shaped text run.
Definition: fontstore.h:73
int GetClusterGapCount() const
Return the number of gaps between clusters, where letter spacing applies.