• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef API_FONT_IFONT_MANAGER_H
17 #define API_FONT_IFONT_MANAGER_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/array_view.h>
22 #include <base/containers/string.h>
23 #include <base/containers/string_view.h>
24 #include <base/containers/vector.h>
25 #include <core/plugin/intf_interface.h>
26 #include <font/intf_font.h>
27 #include <font/namespace.h>
28 
29 FONT_BEGIN_NAMESPACE()
30 
31 enum class Weight : uint64_t {
32     Invisible = 0,
33     Thin = 100,
34     ExtraLight = 200,
35     Light = 300,
36     Normal = 400,
37     Medium = 500,
38     SemiBold = 600,
39     Bold = 700,
40     ExtraBold = 800,
41     Black = 900,
42     ExtraBlack = 1000,
43 };
44 
45 enum class Width : uint64_t {
46     UltraCondensed = 500,
47     ExtraCondensed = 625,
48     Condensed = 750,
49     SemiCondensed = 875,
50     Normal = 1000,
51     SemiExpanded = 1125,
52     Expanded = 1250,
53     ExtraExpanded = 1500,
54     UltraExpanded = 2000,
55 };
56 
57 enum class SlantType : uint64_t {
58     Upright,
59     Italic,
60     Oblique,
61 };
62 
63 union FontStyle {
64     uint64_t bits;
65     struct {
66         Weight Weight : 10;
67         uint64_t Slant : 7;
68         uint64_t BackSlant : 1;
69         SlantType SlantType : 2;
70         Width Width : 11;
71         uint64_t OpticalSize : 8;
72         uint64_t Pad : 25;
73     };
74 
75     FontStyle& operator=(uint64_t _bits)
76     {
77         bits = _bits;
78         return *this;
79     }
uint64_t()80     inline constexpr operator uint64_t() const
81     {
82         return bits;
83     }
84     inline constexpr bool operator==(const FontStyle& style) const
85     {
86         return bits == uint64_t(style);
87     }
88 };
89 
90 static constexpr FontStyle Regular = { 0x73E800190 };
91 static constexpr FontStyle Bold = { 0x73E8002BC };
92 static constexpr FontStyle Italic = { 0x73E800190 | (1 << 18) };
93 static constexpr FontStyle Oblique = { 0x73E800190 | (1 << 19) };
94 
95 /**
96  * Font face identification structure.
97  *
98  * FontManager will build a TypeFace list from font files gathered in lookup directories.
99  *
100  */
101 struct TypeFace {
102     BASE_NS::string path;      // font file path
103     BASE_NS::string name;      // family name as defined in font file
104     BASE_NS::string styleName; // style name as defined in font file
105     uint64_t uid;              // physical PATH hash as map key for font buffers map
106     long index;                // face index for quick access to face in font file
107     FontStyle style;           //
108 };
109 
110 class IFontManager : public CORE_NS::IInterface {
111 public:
112     static constexpr BASE_NS::Uid UID { "0ee45da4-ef82-423b-88ff-c203dcc51a56" };
113     using Ptr = BASE_NS::refcnt_ptr<IFontManager>;
114 
115     // check if provided typeface has a glyph with corresponding code;
116     // return glyph's index or 0 for undefined character code ("missing glyph")
117     // NOTE: this function will also update charmap index in given typeface
118     // This function is useful for checking glyph's existence without creating
119     // new font
120     virtual uint32_t GetGlyphIndex(const TypeFace&, uint32_t code) = 0;
121 
122     /**
123     return all typefaces from font files found in default lookup directories.
124     */
125     virtual BASE_NS::array_view<const TypeFace> GetTypeFaces() const = 0;
126     /**
127     return first matching typeface given a face name in default lookup directories.
128     */
129     virtual const TypeFace* GetTypeFace(BASE_NS::string_view name) = 0;
130     /**
131     return first matching typeface given a face and a style name in default lookup directories.
132     */
133     virtual const TypeFace* GetTypeFace(BASE_NS::string_view name, BASE_NS::string_view styleName) = 0;
134     /**
135     return all typefaces from provided font file path.
136     */
137     virtual BASE_NS::vector<TypeFace> GetTypeFaces(BASE_NS::string_view filePath) = 0;
138     /**
139     return all typefaces from font files found in provided lookup directories.
140     */
141     virtual BASE_NS::vector<TypeFace> GetTypeFaces(BASE_NS::array_view<const BASE_NS::string_view> lookupDirs) = 0;
142 
143     /**
144     Create a new font for typeface given in argument.
145     Note that font cache key is the typeface.uid.
146     */
147     virtual IFont::Ptr CreateFont(const TypeFace&) = 0;
148     /**
149     Create a new font object from font blob given in argument, TypeFace uid member must be pre-initialized with
150     a value that uniquely identify this font.
151     */
152     virtual IFont::Ptr CreateFontFromMemory(const TypeFace&, const BASE_NS::vector<uint8_t>& fontData) = 0;
153 
154     // flush caches
155     virtual void FlushCaches() = 0;
156 
157     /**
158     Triggers an update for atlas textures. All pending changes will be copied to GPU memory.
159     */
160     virtual void UploadPending() = 0;
161 
162 protected:
163     IFontManager() = default;
164     virtual ~IFontManager() = default;
165     IFontManager(IFontManager const&) = delete;
166     IFontManager& operator=(IFontManager const&) = delete;
167 };
168 FONT_END_NAMESPACE()
169 
170 #endif // API_FONT_IFONT_MANAGER_H
171