Line data Source code
1 : /**
2 : Copyright (c) 2024 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef CORE_FONT_SPFONTLIBRARY_H_
24 : #define CORE_FONT_SPFONTLIBRARY_H_
25 :
26 : #include "SPFontFace.h"
27 :
28 : namespace STAPPLER_VERSIONIZED stappler::font {
29 :
30 : class FontLibrary;
31 :
32 : class FontFaceObjectHandle : public RefBase<memory::StandartInterface>, public InterfaceObject<memory::StandartInterface> {
33 : public:
34 : virtual ~FontFaceObjectHandle();
35 :
36 : bool init(const Rc<FontLibrary> &, Rc<FontFaceObject> &&, Function<void(const FontFaceObjectHandle *)> &&onDestroy);
37 :
38 4471 : FT_Face getFace() const { return _face->getFace(); }
39 :
40 : bool acquireTexture(char16_t, const Callback<void(const CharTexture &)> &);
41 :
42 : protected:
43 : Rc<FontLibrary> _library;
44 : Rc<FontFaceObject> _face;
45 : Function<void(const FontFaceObjectHandle *)> _onDestroy;
46 : };
47 :
48 : class FontLibrary : public RefBase<memory::StandartInterface>, public InterfaceObject<memory::StandartInterface> {
49 : public:
50 : enum class DefaultFontName {
51 : None,
52 : RobotoFlex_VariableFont,
53 : RobotoMono_VariableFont,
54 : RobotoMono_Italic_VariableFont,
55 : DejaVuSans
56 : };
57 :
58 : struct FontData {
59 : bool persistent;
60 : BytesView view;
61 : Bytes bytes;
62 : Function<Bytes()> callback;
63 :
64 24 : FontData(BytesView v, bool p) : persistent(p) {
65 24 : if (persistent) {
66 24 : view = v;
67 : } else {
68 0 : bytes = v.bytes<Interface>();
69 0 : view = bytes;
70 : }
71 24 : }
72 0 : FontData(Bytes &&b) : persistent(false), bytes(move(b)) {
73 0 : view = bytes;
74 0 : }
75 72 : FontData(Function<Bytes()> &&cb) : persistent(true), callback(move(cb)) { }
76 : };
77 :
78 : static BytesView getFont(DefaultFontName);
79 : static StringView getFontName(DefaultFontName);
80 :
81 : FontLibrary();
82 : virtual ~FontLibrary();
83 :
84 : Rc<FontFaceData> openFontData(StringView, FontLayoutParameters params, bool isParamsPreconfigured, const Callback<FontData()> & = nullptr);
85 :
86 : Rc<FontFaceObject> openFontFace(StringView, const FontSpecializationVector &, const Callback<FontData()> &);
87 : Rc<FontFaceObject> openFontFace(const Rc<FontFaceData> &, const FontSpecializationVector &);
88 :
89 : void invalidate();
90 :
91 : void update();
92 :
93 : uint16_t getNextId();
94 : void releaseId(uint16_t);
95 :
96 : Rc<FontFaceObjectHandle> makeThreadHandle(const Rc<FontFaceObject> &);
97 :
98 : protected:
99 : FT_Face newFontFace(BytesView);
100 : void doneFontFace(FT_Face);
101 :
102 : mem_std::Mutex _mutex;
103 : std::shared_mutex _sharedMutex;
104 : Map<StringView, Rc<FontFaceObject>> _faces;
105 : Map<StringView, Rc<FontFaceData>> _data;
106 : Map<FontFaceObject *, Map<std::thread::id, Rc<FontFaceObjectHandle>>> _threads;
107 : FT_Library _library = nullptr;
108 :
109 : std::bitset<1024 * 16> _fontIds;
110 : };
111 :
112 : }
113 :
114 : #endif /* CORE_FONT_SPFONTLIBRARY_H_ */
|