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/render/NativeObject.h>
23 #include <kiwano/core/Resource.h>
24 
25 namespace kiwano
26 {
27 KGE_DECLARE_SMART_PTR(Font);
28 
29 class Renderer;
30 
40 struct FontWeight
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 
60 enum class FontPosture
61 {
62  Normal,
63  Oblique,
64  Italic,
65 };
66 
71 enum 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 
89 class Font : public NativeObject
90 {
91 public:
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 
135 protected:
138  void SetFamilyName(const String& name);
139 
140 protected:
141  float size_;
142  uint32_t weight_;
143  FontPosture posture_;
144  FontStretch stretch_;
145  String family_name_;
146 };
147 
152 class KGE_API FontCache final : public Singleton<FontCache>
153 {
154  friend Singleton<FontCache>;
155 
156 public:
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 
187 private:
188  FontCache();
189 
190  String TransformFamily(String family) const;
191 
192 private:
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 
202 inline String Font::GetFamilyName() const
203 {
204  return family_name_;
205 }
206 
207 inline float Font::GetSize() const
208 {
209  return size_;
210 }
211 
212 inline uint32_t Font::GetWeight() const
213 {
214  return weight_;
215 }
216 
218 {
219  return posture_;
220 }
221 
223 {
224  return stretch_;
225 }
226 
227 inline void Font::SetFamilyName(const String& name)
228 {
229  family_name_ = name;
230 }
231 
232 } // namespace kiwano
资源
Definition: Resource.h:40
含有本地指针的对象
Definition: NativeObject.h:42
void SetFamilyName(const String &name)
获取字体族
Definition: Font.h:227
加粗
Definition: Font.h:49
FontPosture
字体形态
Definition: Font.h:60
FontStretch
字体拉伸
Definition: Font.h:71
Definition: Singleton.h:27
正常
Definition: Font.h:47
float GetSize() const
获取字号
Definition: Font.h:207
Definition: Actor.cpp:26
字体粗细值
Definition: Font.h:40
uint32_t GetWeight() const
获取字体粗细值
Definition: Font.h:212
FontPosture GetPosture() const
获取字体形态
Definition: Font.h:217
Value
Definition: Font.h:42
字体
Definition: Font.h:89
FontStretch GetStretch() const
获取字体拉伸
Definition: Font.h:222
纹理缓存
Definition: Font.h:152
String GetFamilyName() const
获取字体族
Definition: Font.h:202