Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #pragma once
23 :
24 : #include <memory>
25 : #include <unordered_map>
26 :
27 : #include "iengravingfont.h"
28 : #include "modularity/ioc.h"
29 : #include "draw/ifontprovider.h"
30 : #include "draw/types/geometry.h"
31 : #include "iengravingfontsprovider.h"
32 :
33 : #include "io/iodevice.h"
34 : #include "io/path.h"
35 :
36 : #include "infrastructure/smufl.h"
37 : #include "infrastructure/shape.h"
38 :
39 : #include "style/styledef.h"
40 : #include "types/symid.h"
41 :
42 : namespace muse {
43 : class JsonObject;
44 : }
45 :
46 : namespace muse::draw {
47 : class Painter;
48 : }
49 :
50 : namespace mu::engraving {
51 : class Shape;
52 :
53 : class EngravingFont : public IEngravingFont
54 : {
55 : muse::GlobalInject<muse::draw::IFontProvider> fontProvider;
56 : muse::GlobalInject<IEngravingFontsProvider> engravingFonts;
57 : public:
58 : EngravingFont(const std::string& name, const std::string& family, const muse::io::path_t& filePath,
59 : const muse::io::path_t& metadataPath);
60 : EngravingFont(const std::string& name, const std::string& family, std::shared_ptr<muse::io::IODevice> font,
61 : std::shared_ptr<muse::io::IODevice> metadata);
62 : EngravingFont(const EngravingFont& other);
63 :
64 : const std::string& name() const override;
65 : const std::string& family() const override;
66 :
67 : std::unordered_map<Sid, PropertyValue> engravingDefaults() const override;
68 : double textEnclosureThickness();
69 :
70 : char32_t symCode(SymId id) const override;
71 : SymId fromCode(char32_t code) const override;
72 : String toString(SymId id) const override;
73 :
74 : bool isValid(SymId id) const override;
75 :
76 : RectF bbox(SymId id, double mag) const override;
77 : RectF bbox(SymId id, const SizeF&) const override;
78 : RectF bbox(const SymIdList& s, double mag) const override;
79 : RectF bbox(const SymIdList& s, const SizeF& mag) const override;
80 : Shape shape(const SymIdList& s, double mag) const override;
81 : Shape shape(const SymIdList& s, const SizeF& mag) const override;
82 : Shape shapeWithCutouts(SymId id, double mag) override;
83 : Shape shapeWithCutouts(SymId id, const SizeF& mag) override;
84 :
85 : double width(SymId id, double mag) const override;
86 : double width(const SymIdList&, double mag) const override;
87 : double height(SymId id, double mag) const override;
88 : double advance(SymId id, double mag) const override;
89 :
90 : PointF smuflAnchor(SymId symId, SmuflAnchorId anchorId, double mag) const override;
91 :
92 : // Draw
93 : void draw(SymId id, muse::draw::Painter* p, double mag, const PointF& pos, const double angle = 0) const override;
94 : void draw(SymId id, muse::draw::Painter* p, const SizeF& mag, const PointF& pos, const double angle = 0) const override;
95 :
96 : void draw(const SymIdList& ids, muse::draw::Painter* p, double mag, const PointF& pos, const double angle = 0) const override;
97 : void draw(const SymIdList& ids, muse::draw::Painter* p, const SizeF& mag, const PointF& pos, const double angle = 0) const override;
98 :
99 : void ensureLoad();
100 :
101 : private:
102 :
103 : friend class SymbolFonts;
104 :
105 : struct Sym {
106 : char32_t code;
107 : RectF bbox;
108 : Shape shapeWithCutouts;
109 : double advance = 0.0;
110 :
111 : std::map<SmuflAnchorId, PointF> smuflAnchors;
112 : SymIdList subSymbolIds;
113 :
114 4502935 : bool isValid() const
115 : {
116 4502935 : return code != 0 && bbox.isValid();
117 : }
118 :
119 251 : bool isCompound() const
120 : {
121 251 : return !subSymbolIds.empty();
122 : }
123 : };
124 :
125 : void loadGlyphsWithAnchors(const muse::JsonObject& glyphsWithAnchors);
126 : void loadComposedGlyphs();
127 : void loadStylisticAlternates(const muse::JsonObject& glyphsWithAlternatesObject);
128 : void loadEngravingDefaults(const muse::JsonObject& engravingDefaultsObject);
129 : void computeMetrics(Sym& sym, const Smufl::Code& code);
130 :
131 : void constructShapeWithCutouts(Shape& shape, SymId id);
132 :
133 : Sym& sym(SymId id);
134 : const Sym& sym(SymId id) const;
135 :
136 : bool useFallbackFont(SymId id) const;
137 :
138 : bool m_loaded = false;
139 : std::vector<Sym> m_symbols;
140 : mutable muse::draw::Font m_font;
141 :
142 : std::string m_name;
143 : std::string m_family;
144 : std::shared_ptr<muse::io::IODevice> m_fontDevice;
145 : std::shared_ptr<muse::io::IODevice> m_metadataDevice;
146 :
147 : std::unordered_map<Sid, PropertyValue> m_engravingDefaults;
148 : double m_textEnclosureThickness = 0;
149 : };
150 : }
|