Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2024 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 <vector>
25 : #include <functional>
26 :
27 : #include "ifontsengine.h"
28 :
29 : #include "global/modularity/ioc.h"
30 : #include "global/async/asyncable.h"
31 : #include "ifontsdatabase.h"
32 :
33 : #include "fontrendercache.h"
34 :
35 : namespace muse::draw {
36 : class IFontFace;
37 : class FontsEngine : public IFontsEngine, public Contextable, public async::Asyncable
38 : {
39 : GlobalInject<IFontsDatabase> fontsDatabase;
40 :
41 : public:
42 23 : FontsEngine(const modularity::ContextPtr& iocCtx)
43 23 : : Contextable(iocCtx) {}
44 : ~FontsEngine();
45 :
46 : void init();
47 : void setRenderCacheDirPath(const io::path_t& path, const std::string& revision = std::string()) override;
48 : void clearLoadedFaces() override;
49 :
50 : double lineSpacing(const Font& f) const override;
51 : double xHeight(const Font& f) const override;
52 : double height(const Font& f) const override;
53 : double capHeight(const Font& f) const override;
54 : double ascent(const Font& f) const override;
55 : double descent(const Font& f) const override;
56 :
57 : double underlinePos(const Font& f) const override;
58 : double lineWidth(const Font& f) const override;
59 : double strikeOutPos(const Font& f) const override;
60 :
61 : bool inFont(const Font& f, char32_t ucs4) const override;
62 :
63 : double horizontalAdvance(const Font& f, const char32_t& ch) const override;
64 : double horizontalAdvance(const Font& f, const std::u32string& text) const override;
65 :
66 : RectF boundingRect(const Font& f, const char32_t& ch) const override;
67 : RectF boundingRect(const Font& f, const std::u32string& text) const override;
68 : RectF tightBoundingRect(const Font& f, const std::u32string& text) const override;
69 :
70 : // For draw
71 : std::vector<GlyphImage> render(const Font& f, const std::u32string& text) const override;
72 :
73 : // For dev
74 : using FontFaceFactory = std::function<IFontFace* (const FontDataKey& dataKey, Font::Type type)>;
75 : void setFontFaceFactory(const FontFaceFactory& f);
76 :
77 : private:
78 :
79 : struct TextBlock {
80 : const char32_t* text = nullptr;
81 : int lenght = 0;
82 : };
83 :
84 : struct FontFaceTextBlock {
85 : TextBlock text;
86 : const IFontFace* face = nullptr;
87 : };
88 :
89 : struct RequireFace {
90 : IFontFace* face = nullptr; // real loaded face
91 : std::vector<IFontFace*> subtitutionFaces;
92 : FaceKey requireKey; // require face
93 : FontDataKey actualDataKey; // resolved face
94 :
95 : bool isSymbolMode() const;
96 : double pixelScale() const;
97 : double pixelScaleFor(const IFontFace* loadedFace) const;
98 : };
99 :
100 : IFontFace* createFontFace(const FontData& fontData, Font::Type type) const;
101 : RequireFace* fontFace(const Font& f, bool isSymbolMode = false) const;
102 : IFontFace* fontFaceByActualDataKey(const FontDataKey& actualDataKey, Font::Type type, int loadedPixelSize, bool isSymbolMode) const;
103 : IFontFace* sdfFontFaceFor(const IFontFace* layoutFace) const;
104 :
105 : std::vector<FontFaceTextBlock> splitTextByFontFaces(const RequireFace* rf, const TextBlock& text) const;
106 :
107 : FontFaceFactory m_fontFaceFactory;
108 :
109 : mutable std::vector<IFontFace*> m_loadedFaces;
110 : mutable std::vector<RequireFace*> m_requiredFaces;
111 :
112 : mutable FontRenderCache m_renderCache;
113 : };
114 : }
|