Kiwano Engine v1.3.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{
27
28class Renderer;
29
40{
41 enum Value : uint32_t
42 {
43 Thin = 100U,
44 ExtraLight = 200U,
45 Light = 300U,
46 Normal = 400U,
47 Medium = 500U,
48 Bold = 700U,
49 ExtraBold = 800U,
50 Black = 900U,
51 ExtraBlack = 950U
52 };
53};
54
59enum class FontPosture
60{
61 Normal,
62 Oblique,
63 Italic,
64};
65
70enum class FontStretch
71{
72 Unknown,
73 UltraCondensed,
74 ExtraCondensed,
75 Condensed,
76 SemiCondensed,
77 Normal,
78 SemiExpanded,
79 Expanded,
80 ExtraExpanded,
81 UltraExpanded,
82};
83
88class Font : public NativeObject
89{
90public:
94 static RefPtr<Font> Preload(StringView file);
95
99 static RefPtr<Font> Preload(const Resource& resource);
100
103 Font();
104
111 Font(StringView family_name, float size, uint32_t weight = FontWeight::Normal,
113
116 String GetFamilyName() const;
117
120 float GetSize() const;
121
124 uint32_t GetWeight() const;
125
128 FontPosture GetPosture() const;
129
132 FontStretch GetStretch() const;
133
134protected:
137 void SetFamilyName(StringView name);
138
139protected:
140 float size_;
141 uint32_t weight_;
142 FontPosture posture_;
143 FontStretch stretch_;
144 String family_name_;
145};
146
151class KGE_API FontCache final : public Singleton<FontCache>
152{
154
155public:
158 void AddFont(size_t key, RefPtr<Font> font);
159
162 void AddFontByFamily(StringView font_family, RefPtr<Font> font);
163
166 RefPtr<Font> GetFont(size_t key) const;
167
170 RefPtr<Font> GetFontByFamily(StringView font_family) const;
171
174 void RemoveFont(size_t key);
175
178 void RemoveFontByFamily(StringView font_family);
179
182 void Clear();
183
184 ~FontCache();
185
186private:
187 FontCache();
188
189 String TransformFamily(String family) const;
190
191private:
192 using FontMap = UnorderedMap<size_t, RefPtr<Font>>;
193 FontMap font_cache_;
194
195 using FontFamilyMap = UnorderedMap<String, RefPtr<Font>>;
196 FontFamilyMap font_family_cache_;
197};
198
201inline String Font::GetFamilyName() const
202{
203 return family_name_;
204}
205
206inline float Font::GetSize() const
207{
208 return size_;
209}
210
211inline uint32_t Font::GetWeight() const
212{
213 return weight_;
214}
215
217{
218 return posture_;
219}
220
222{
223 return stretch_;
224}
225
227{
228 family_name_ = name;
229}
230
231} // namespace kiwano
纹理缓存
Definition: Font.h:152
字体
Definition: Font.h:89
static RefPtr< Font > Preload(StringView file)
预加载字体
Definition: Font.cpp:29
Font()
创建系统默认字体
Definition: Font.cpp:87
float GetSize() const
获取字号
Definition: Font.h:206
uint32_t GetWeight() const
获取字体粗细值
Definition: Font.h:211
FontPosture GetPosture() const
获取字体形态
Definition: Font.h:216
void SetFamilyName(StringView name)
获取字体族
Definition: Font.h:226
FontStretch GetStretch() const
获取字体拉伸
Definition: Font.h:221
String GetFamilyName() const
获取字体族
Definition: Font.h:201
含有本地指针的对象
Definition: NativeObject.hpp:32
引用计数智能指针
Definition: RefBasePtr.hpp:35
资源
Definition: Resource.h:41
Definition: Singleton.h:28
FontPosture
字体形态
Definition: Font.h:60
FontStretch
字体拉伸
Definition: Font.h:71
字体粗细值
Definition: Font.h:40
Value
Definition: Font.h:42
@ Bold
加粗
Definition: Font.h:48
@ Normal
正常
Definition: Font.h:46