Kiwano Engine v1.2.x
Font.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/core/Resource.h>
23#include <kiwano/platform/NativeObject.hpp>
24
25namespace kiwano
26{
27KGE_DECLARE_SMART_PTR(Font);
28
29class Renderer;
30
41{
42 enum Value : uint32_t
43 {
44 Thin = 100U,
45 ExtraLight = 200U,
46 Light = 300U,
47 Normal = 400U,
48 Medium = 500U,
49 Bold = 700U,
50 ExtraBold = 800U,
51 Black = 900U,
52 ExtraBlack = 950U
53 };
54};
55
60enum class FontPosture
61{
62 Normal,
63 Oblique,
64 Italic,
65};
66
71enum class FontStretch
72{
73 Unknown,
74 UltraCondensed,
75 ExtraCondensed,
76 Condensed,
77 SemiCondensed,
78 Normal,
79 SemiExpanded,
80 Expanded,
81 ExtraExpanded,
82 UltraExpanded,
83};
84
89class Font : public NativeObject
90{
91public:
95 static FontPtr Preload(const String& file);
96
100 static FontPtr Preload(const Resource& resource);
101
104 Font();
105
112 Font(const String& family_name, float size, uint32_t weight = FontWeight::Normal,
114
117 String GetFamilyName() const;
118
121 float GetSize() const;
122
125 uint32_t GetWeight() const;
126
129 FontPosture GetPosture() const;
130
133 FontStretch GetStretch() const;
134
135protected:
138 void SetFamilyName(const String& name);
139
140protected:
141 float size_;
142 uint32_t weight_;
143 FontPosture posture_;
144 FontStretch stretch_;
145 String family_name_;
146};
147
152class KGE_API FontCache final : public Singleton<FontCache>
153{
155
156public:
159 void AddFont(size_t key, FontPtr font);
160
163 void AddFontByFamily(const String& font_family, FontPtr font);
164
167 FontPtr GetFont(size_t key) const;
168
171 FontPtr GetFontByFamily(const String& font_family) const;
172
175 void RemoveFont(size_t key);
176
179 void RemoveFontByFamily(const String& font_family);
180
183 void Clear();
184
185 ~FontCache();
186
187private:
188 FontCache();
189
190 String TransformFamily(String family) const;
191
192private:
193 using FontMap = UnorderedMap<size_t, FontPtr>;
194 FontMap font_cache_;
195
196 using FontFamilyMap = UnorderedMap<String, FontPtr>;
197 FontFamilyMap font_family_cache_;
198};
199
202inline String Font::GetFamilyName() const
203{
204 return family_name_;
205}
206
207inline float Font::GetSize() const
208{
209 return size_;
210}
211
212inline uint32_t Font::GetWeight() const
213{
214 return weight_;
215}
216
218{
219 return posture_;
220}
221
223{
224 return stretch_;
225}
226
227inline void Font::SetFamilyName(const String& name)
228{
229 family_name_ = name;
230}
231
232} // namespace kiwano
纹理缓存
Definition: Font.h:153
字体
Definition: Font.h:90
Font()
创建系统默认字体
Definition: Font.cpp:87
float GetSize() const
获取字号
Definition: Font.h:207
void SetFamilyName(const String &name)
获取字体族
Definition: Font.h:227
uint32_t GetWeight() const
获取字体粗细值
Definition: Font.h:212
FontPosture GetPosture() const
获取字体形态
Definition: Font.h:217
static FontPtr Preload(const String &file)
预加载字体
Definition: Font.cpp:29
FontStretch GetStretch() const
获取字体拉伸
Definition: Font.h:222
String GetFamilyName() const
获取字体族
Definition: Font.h:202
含有本地指针的对象
Definition: NativeObject.hpp:34
资源
Definition: Resource.h:41
Definition: Singleton.h:28
FontPosture
字体形态
Definition: Font.h:61
FontStretch
字体拉伸
Definition: Font.h:72
字体粗细值
Definition: Font.h:41
Value
Definition: Font.h:43
@ Bold
加粗
Definition: Font.h:49
@ Normal
正常
Definition: Font.h:47