YSTest: Font.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

Font.h
浏览该文件的文档.
1 /*
2 © 2009-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YSL_INC_Adaptor_Font_h_
29 #define YSL_INC_Adaptor_Font_h_ 1
30
31 #include "../Core/YModules.h"
32 #include YFM_YSLib_Core_YFunc
33 #include YFM_YSLib_Core_YObject
34 #include <string>
35 #include YFM_YSLib_Core_YException
36 #include YFM_YSLib_Adaptor_YTextBase
37 #include <ystdex/hash.hpp> // for ystdex::hash_combine_seq;
38 #include <ystdex/cache.hpp>
39 //#include <ft2build.h>
40 //#include FT_FREETYPE_H
41
43
44 struct FT_SizeRec_;
45 using FT_Size = ::FT_SizeRec_*;
46 using FT_SizeRec = ::FT_SizeRec_;
47 struct FT_FaceRec_;
48 using FT_FaceRec = ::FT_FaceRec_;
49 struct FT_GlyphSlotRec_;
50 using FT_GlyphSlot = ::FT_GlyphSlotRec_*;
51 struct FT_LibraryRec_;
52 using FT_Library = ::FT_LibraryRec_*;
53 struct FT_Size_Metrics_;
54 using FT_Size_Metrics = FT_Size_Metrics_;
56
57 namespace YSLib
58 {
59
60 namespace Drawing
61 {
62
63 class Font;
64 class FontCache;
65 class FontFamily;
66 class Typeface;
67
68
73 using FontSize = std::uint8_t;
78 using FontPath = string;
83 using FamilyName = string;
88 using StyleName = string;
89
90
95 enum class FontStyle : std::uint8_t
96 {
97 Regular = 0,
98 Bold = 1,
99 Italic = 2,
100 Underline = 4,
101 Strikeout = 8
102 };
103
108 DefBitmaskEnum(FontStyle)
109
110
111
116 yconstfn PDefH(const char*, FetchName, FontStyle style) ynothrow
117 ImplRet(style == FontStyle::Bold ? "Bold" : (style == FontStyle::Italic
118 ? "Italic" : (style == FontStyle::Underline ? "Underline" : (style
119 == FontStyle::Strikeout ? "Strikeout" : "Regular"))))
120
121
126 class YF_API FontException : public LoggedEvent
127 {
128 public:
130 using FontError = int;
131
132 private:
133 FontError err;
134
135 public:
136 FontException(FontError e, const std::string& msg = {})
137 : LoggedEvent(msg),
138 err(e)
139 {}
141 DefDeCopyCtor(FontException)
146 ~FontException() override;
147
148 DefGetter(const ynothrow, FontError, ErrorCode, err)
149 };
150
151
156 class YF_API NativeFontSize final : private noncopyable
157 {
158 private:
159 ::FT_Size size;
160
161 public:
163 NativeFontSize(::FT_FaceRec&, FontSize);
164 NativeFontSize(NativeFontSize&&) ynothrow;
166 ~NativeFontSize();
167
168 ::FT_SizeRec&
169 GetSizeRec() const;
170
176 void
177 Activate() const;
178 };
179
180
185 class YF_API FontFamily final : private noncopyable
186 {
187 public:
189 using FaceMap = map<const StyleName, lref<Typeface>>;
190
191 private:
192 FamilyName family_name;
193
194 protected:
196 FaceMap mFaces;
197
198 public:
203 FontFamily(const FamilyName&);
204
209 void
210 operator+=(Typeface&);
215 bool
216 operator-=(Typeface&) ynothrow;
217
218 DefGetter(const ynothrow, const FamilyName&, FamilyName, family_name)
220
221
225 observer_ptr<Typeface>
226 GetTypefacePtr(FontStyle) const;
228 observer_ptr<Typeface>
229 GetTypefacePtr(const StyleName&) const;
231 Typeface&
233 GetTypefaceRef(FontStyle) const;
235 Typeface&
236 GetTypefaceRef(const StyleName&) const;
237 };
238
239
244 class YF_API Typeface final : private noncopyable, private nonmovable
245 {
247 friend class Font;
249 friend class CharBitmap;
250
251 private:
253
254 struct BitmapKey
255 {
257 unsigned Flags;
259 unsigned GlyphIndex;
260 FontSize Size;
262 FontStyle Style;
263
265 friend PDefHOp(bool, ==, const BitmapKey& x, const BitmapKey& y)
266 ynothrow
267 ImplRet(x.Flags == y.Flags && x.GlyphIndex == y.GlyphIndex
268 && x.Size == y.Size && x.Style == y.Style)
269 };
270
271 struct BitmapKeyHash
272 {
273 PDefHOp(size_t, (), const BitmapKey& key) const ynothrow
274 ImplRet(ystdex::hash_combine_seq(size_t(key.Style), key.Size,
275 key.GlyphIndex, key.Flags))
276 };
277
278 class SmallBitmapData
279 {
280 friend class CharBitmap;
281
282 private:
288 byte width = 255, height = 0;
289 signed char left = 0, top = 0;
290 byte format = 0, max_grays = 0;
291 short pitch = 0;
292 signed char xadvance = 0, yadvance = 0;
293 byte* buffer = {};
295
296 public:
298 SmallBitmapData(::FT_GlyphSlot, FontStyle);
299 SmallBitmapData(SmallBitmapData&&);
300 ~SmallBitmapData();
301 };
303
305 long face_index;
307 int cmap_index;
308 StyleName style_name;
310 pair<lref<FontFamily>, lref<::FT_FaceRec_>> ref;
312 mutable ystdex::used_list_cache<BitmapKey, SmallBitmapData, BitmapKeyHash>
313 bitmap_cache;
315 mutable unordered_map<char32_t, unsigned> glyph_index_cache;
317 mutable unordered_map<FontSize, NativeFontSize> size_cache;
318
319 public:
324 Typeface(FontCache&, const FontPath&, std::uint32_t = 0);
326 ~Typeface();
327
328 DefGetterMem(const ynothrow, FamilyName, FamilyName, GetFontFamily())
333 DefGetter(const ynothrow, const FontFamily&, FontFamily, ref.first)
334 DefGetter(const ynothrow, const StyleName&, StyleName, style_name)
339 DefGetter(const ynothrow, int, CMapIndex, cmap_index)
340
341 private:
343
344 SmallBitmapData&
345 LookupBitmap(const BitmapKey&) const;
346
348 unsigned
349 LookupGlyphIndex(char32_t) const;
350
352 NativeFontSize&
353 LookupSize(FontSize) const;
354
355 public:
356 PDefH(void, ClearBitmapCache, )
357 ImplExpr(bitmap_cache.clear())
358
359 PDefH(void, ClearGlyphIndexCache, )
360 ImplExpr(glyph_index_cache.clear())
362
364 PDefH(void, ClearSizeCache, )
365 ImplExpr(size_cache.clear())
366 };
367
368
374 YF_API const Typeface&
375 FetchDefaultTypeface();
376
377
383 class YF_API CharBitmap final
384 {
385 public:
387 using BufferType = byte*;
392 enum FormatType
393 {
394 None = 0,
395 Mono,
396 Gray,
397 Gray2,
398 Gray4,
399 LCD,
400 LCD_V
401 };
403 using NativeType = Typeface::SmallBitmapData*;
408 using PitchType = short;
410 using ScaleType = byte;
412 using SignedScaleType = signed char;
413
414 private:
415 NativeType bitmap = {};
416
417 public:
419
420 DefDeCtor(CharBitmap)
423 yconstfn
424 CharBitmap(NativeType b)
425 : bitmap(b)
426 {}
428
429 yconstfn DefCvt(const ynothrow, NativeType, bitmap)
430
431
436 yconstfn DefGetter(const ynothrowv, BufferType, Buffer,
437 Deref(bitmap).buffer)
438 yconstfn DefGetter(const ynothrowv, FormatType, Format,
439 FormatType(Deref(bitmap).format))
440 yconstfn DefGetter(const ynothrowv, ScaleType, GrayLevel,
441 Deref(bitmap).max_grays)
442 yconstfn DefGetter(const ynothrowv, ScaleType, Height, Deref(bitmap).height)
443 yconstfn DefGetter(const ynothrowv, SignedScaleType, Left,
444 Deref(bitmap).left)
445 yconstfn DefGetter(const ynothrowv, PitchType, Pitch, Deref(bitmap).pitch)
446 yconstfn DefGetter(const ynothrowv, SignedScaleType, Top, Deref(bitmap).top)
447 yconstfn DefGetter(const ynothrowv, ScaleType, Width, Deref(bitmap).width)
448 yconstfn DefGetter(const ynothrowv, SignedScaleType, XAdvance,
449 Deref(bitmap).xadvance)
450 yconstfn DefGetter(const ynothrowv, SignedScaleType, YAdvance,
451 Deref(bitmap).yadvance)
453 };
454
455
460 class YF_API FontCache final : private noncopyable,
461 private OwnershipTag<Typeface>, private OwnershipTag<FontFamily>
462 {
463 friend class Typeface;
468 friend class Font;
469
470 public:
476 using FaceMap = map<FontPath, unique_ptr<Typeface>>;
481 using FamilyMap = unordered_map<FamilyName, unique_ptr<FontFamily>>;
482
488 static yconstexpr const size_t DefaultGlyphCacheSize = yimpl(128U << 10);
489
490 private:
492 ::FT_Library library;
493
494 protected:
499 FaceMap mFaces;
501 FamilyMap mFamilies;
506 observer_ptr<Typeface> pDefaultFace;
507
508 public:
514 explicit
515 FontCache(size_t = DefaultGlyphCacheSize);
520 ~FontCache();
521
522 public:
527 DefGetter(const ynothrow, const FaceMap&, Faces, mFaces)
529 DefGetter(const ynothrow, const FamilyMap&, FamilyIndices, mFamilies)
531
532 observer_ptr<const FontFamily>
534 GetFontFamilyPtr(const FamilyName&) const;
539 observer_ptr<const Typeface>
540 GetDefaultTypefacePtr() const;
542 observer_ptr<const Typeface>
543 GetTypefacePtr(const FamilyName&, const StyleName&) const;
545
546 private:
551 void
552 Add(const FontPath&, unique_ptr<Typeface>);
553
554 public:
560 size_t
561 LoadTypefaces(const FontPath&);
562
568 FontFamily&
569 LookupFamily(const FamilyName&);
570
574 void
575 InitializeDefaultTypeface();
576 };
577
578
583 class YF_API Font final
584 {
585 public:
586 static yconstexpr const FontSize DefaultSize = 12,
587 MinimalSize = 4, MaximalSize = 96;
588
589 private:
591 lref<Typeface> typeface;
593 FontSize font_size;
598 FontStyle style;
599
600 public:
605 Font()
606 : Font(FetchDefaultTypeface().GetFontFamily())
607 {}
612 explicit
613 Font(const FontFamily&, FontSize = DefaultSize,
614 FontStyle = FontStyle::Regular);
615
616 DefPred(const ynothrow, Bold, bool(style & FontStyle::Bold))
617 DefPred(const ynothrow, Italic, bool(style & FontStyle::Italic))
618 DefPred(const ynothrow, Underline, bool(style & FontStyle::Underline))
619 DefPred(const ynothrow, Strikeout, bool(style & FontStyle::Strikeout))
620
625 std::int8_t
626 GetAdvance(char32_t, CharBitmap = {}) const;
631 std::int8_t
632 GetAscender() const;
637 std::int8_t
638 GetDescender() const;
639 DefGetterMem(const ynothrow, const FamilyName&, FamilyName,
640 GetFontFamily())
641 DefGetterMem(const ynothrow, const FontFamily&, FontFamily, GetTypeface())
642 DefGetter(const ynothrow, FontSize, Size, font_size)
644 DefGetter(const ynothrow, FontStyle, Style, style)
654 CharBitmap
655 GetGlyph(char32_t c, yimpl(unsigned flags = 4U)) const;
660 FontSize
661 GetHeight() const ynothrow;
663 DefGetter(const, StyleName, StyleName, FetchName(style))
664
665 private:
670 ::FT_Size_Metrics
671 GetInternalInfo() const;
672
673 public:
678 DefGetter(const ynothrow, Typeface&, Typeface, typeface)
679
683 void
684 SetSize(FontSize = DefaultSize);
690 bool
691 SetStyle(FontStyle);
692 };
693
694 } // namespace Drawing;
695
696 } // namespace YSLib;
697
698 #endif
699
YSLib::Drawing::GetHeight
tr GetHeight())) inline PDefH(void
按字体高度和行距调整文本区域的底边距。
YSLib::Drawing::FetchName
FetchName
Definition: Font.h:116
YSLib::Drawing::Typeface
字型。
Definition: Font.h:244
YSLib::Drawing::FontStyle::Strikeout
删除线。
YSLib::Drawing::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:583
platform::msg
const string & msg
Definition: Debug.h:464
DefPred
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:232
FT_FaceRec
::FT_FaceRec_ FT_FaceRec
Definition: Font.h:48
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
YSLib::Drawing::FontCache::library
::FT_Library library
库实例。
Definition: Font.h:492
YSLib::Drawing::buffer
The boundary is out of the buffer
Definition: YDraw.h:118
YSLib::Drawing::Typeface::BitmapKeyHash
Definition: Font.h:271
DefBitmaskEnum
#define DefBitmaskEnum(_tEnum)
Definition: YBaseMacro.h:481
YSLib::Drawing::Font::font_size
FontSize font_size
Definition: Font.h:593
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
hash.hpp
散列接口。
platform::Deref
yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:451
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
YSLib::Drawing::FontCache
字体缓存。
Definition: Font.h:460
YSLib::Drawing::NativeFontSize
字体异常。
Definition: Font.h:156
YSLib::Drawing::Font::typeface
lref< Typeface > typeface
Definition: Font.h:591
YSLib::Drawing::FamilyName
string FamilyName
字型家族名称。
Definition: Font.h:83
FT_GlyphSlot
::FT_GlyphSlotRec_ * FT_GlyphSlot
Definition: Font.h:50
YSLib::Drawing::CharBitmap::Gray
Definition: Font.h:396
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: YException.h:50
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:679
YSLib::Drawing::Typeface::glyph_index_cache
unordered_map< char32_t, unsigned > glyph_index_cache
Definition: Font.h:315
platform::containers::string
basic_string< char > string
Definition: Container.h:101
platform::KeyCategory::None
表示未分配或保留的键。
Definition: Keys.h:101
YSLib::Drawing::FontFamily::family_name
FamilyName family_name
Definition: Font.h:192
YSLib::Drawing::CharBitmap::LCD
Definition: Font.h:399
YSLib::Drawing::CharBitmap::FormatType
FormatType
Definition: Font.h:392
YSLib::Drawing::Typeface::BitmapKey::GlyphIndex
unsigned GlyphIndex
Definition: Font.h:259
YSLib::Drawing::Font::style
FontStyle style
字体样式。
Definition: Font.h:598
YSLib::Drawing::FontFamily::mFaces
FaceMap mFaces
字型组索引类型。
Definition: Font.h:196
ystdex::lref
左值引用包装。
Definition: ref.hpp:74
FT_SizeRec
::FT_SizeRec_ FT_SizeRec
Definition: Font.h:46
YSLib::Drawing::Typeface::BitmapKey::Size
FontSize Size
Definition: Font.h:260
YSLib::Drawing::FetchDefaultTypeface
YF_API const Typeface & FetchDefaultTypeface()
取默认字型引用。
Definition: Font.cpp:381
DefDeCtor
#define DefDeCtor(_t)
简单通用成员函数定义。
Definition: YBaseMacro.h:157
YSLib::Drawing::Typeface::cmap_index
int cmap_index
Definition: Font.h:307
YSLib::Drawing::x
The boundary is out of the const Rect SDst SPos x
Definition: YDraw.h:127
YSLib::Drawing::FontCache::FamilyMap
unordered_map< FamilyName, unique_ptr< FontFamily >> FamilyMap
字型家族组索引类型。
Definition: Font.h:481
YSLib::Drawing::FontStyle::Bold
粗体。
ystdex::used_list_cache
按最近使用策略刷新的缓存。
Definition: cache.hpp:170
YSLib::Drawing::StyleName
string StyleName
字型样式名称。
Definition: Font.h:88
YSLib::Activate
Activate
Definition: YApplication.h:210
YSLib::Drawing::NativeFontSize::size
::FT_Size size
Definition: Font.h:159
platform::KeyCodes::DS::Left
Definition: Keys.h:221
YSLib::Drawing::FontCache::mFaces
FaceMap mFaces
字型映射。
Definition: Font.h:499
DefGetterMem
#define DefGetterMem(_q, _t, _n, _m)
Definition: YBaseMacro.h:247
YSLib::Drawing::FontCache::pDefaultFace
observer_ptr< Typeface > pDefaultFace
默认字型指针。
Definition: Font.h:506
YSLib::Drawing::ImplExpr
tr TextRegion &tr ImplExpr(tr.Margin.Bottom=SPos(FetchResizedBottomMargin(tr))) YF_API SDst FetchCharWidth(const Font &
取指定的字符使用指定字体的显示宽度。
YSLib::Drawing::Typeface::BitmapKey
Definition: Font.h:254
YF_API
#define YF_API
Definition: Platform.h:56
Drawing
YSLib::Drawing::CharBitmap
字符位图。
Definition: Font.h:383
YSLib::Drawing::Typeface::bitmap_cache
ystdex::used_list_cache< BitmapKey, SmallBitmapData, BitmapKeyHash > bitmap_cache
Definition: Font.h:313
YSLib::yimpl
YF_API yimpl(GUIApplication &) FetchGlobalInstance()
取全局应用程序实例。
YSLib::Drawing::CharBitmap::Gray2
Definition: Font.h:397
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
YSLib::Drawing::FontStyle::Italic
斜体。
YSLib::Drawing::Typeface::face_index
long face_index
Definition: Font.h:305
YSLib::Drawing::FontCache::mFamilies
FamilyMap mFamilies
字型家族组索引。
Definition: Font.h:501
YSLib::Drawing::FontPath
string FontPath
字体文件路径。
Definition: Font.h:78
YSLib::Drawing::Typeface::ref
pair< lref< FontFamily >, lref<::FT_FaceRec_ > > ref
Definition: Font.h:310
YSLib::Drawing::FontStyle::Regular
常规字体。
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:786
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
YSLib::Drawing::Typeface::BitmapKey::Style
FontStyle Style
Definition: Font.h:262
ystdex::nonmovable
Definition: base.h:81
YSLib::Drawing::FontSize
std::uint8_t FontSize
字体大小。
Definition: Font.h:73
cache.hpp
高速缓冲容器模板。
std
Definition: memory.hpp:1183
YSLib::ImplRet
ImplRet(FetchGlobalInstance().GetGUIHostRef()) YF_API void Execute(GUIApplication &
执行程序主消息循环。
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
YSLib::Drawing::CharBitmap::BufferType
byte * BufferType
Definition: Font.h:387
YSLib::Drawing::Typeface::style_name
StyleName style_name
Definition: Font.h:308
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:834
ystdex::hash_combine_seq
size_t hash_combine_seq(size_t seed, const _type &val)
重复计算序列散列。
Definition: hash.hpp:67
DefCvt
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:206
YSLib::Drawing::CharBitmap::Mono
Definition: Font.h:395
YSLib::Drawing::CharBitmap::SignedScaleType
signed char SignedScaleType
Definition: Font.h:412
YSLib::Drawing::Font::GetAscender
bool(style &FontStyle::Strikeout)) std std::int8_t GetAscender() const
取跨距。
Definition: Font.h:632
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:162
ystdex::noncopyable
不可复制对象：禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: base.h:41
YSLib::Drawing::Size
屏幕区域大小。
Definition: YGDIBase.h:293
platform_ex::Windows::ErrorCode
unsigned long ErrorCode
Definition: MinGW32.h:57
YSLib::Drawing::FontStyle
FontStyle
字体样式。
Definition: Font.h:95
YSLib::Drawing::FontStyle::Underline
下划线。
YSLib::Drawing::Typeface::size_cache
unordered_map< FontSize, NativeFontSize > size_cache
Definition: Font.h:317
YSLib::Drawing::Font::Font
Font()
默认构造：使用默认字型家族、大小和样式的字体对象。
Definition: Font.h:605
YSLib::Drawing::CharBitmap::ScaleType
byte ScaleType
Definition: Font.h:410
FT_Size_Metrics
FT_Size_Metrics_ FT_Size_Metrics
Definition: Font.h:54
YSLib::Drawing::char32_t
tr TextRegion &tr char32_t
Definition: TextLayout.h:92
YSLib::Drawing::PDefH
yconstfn PDefH(Size, Transpose, const Size &s) ynothrow ImplRet(
转置。
Definition: YGDIBase.h:516
YSLib::OwnershipTag
指定对参数指定类型的成员具有所有权的标签。
Definition: YObject.h:51
YSLib::Drawing::CharBitmap::PitchType
short PitchType
Definition: Font.h:408
YSLib::Drawing::CharBitmap::Gray4
Definition: Font.h:398
YSLib::Drawing::Rect::Size
yconstfn const Point static_cast< const Point & > this X const Size Size
Definition: YGDIBase.h:751
YSLib::IO::int
YF_API int
Definition: File.h:51
YSLib::Drawing::Typeface::SmallBitmapData
Definition: Font.h:278
YSLib::Drawing::FontFamily::FaceMap
map< const StyleName, lref< Typeface >> FaceMap
字型组索引类型。
Definition: Font.h:189
YSLib::Drawing::Typeface::BitmapKey::Flags
unsigned Flags
Definition: Font.h:257
YSLib::Drawing::FontCache::FaceMap
map< FontPath, unique_ptr< Typeface >> FaceMap
字型映射类型。
Definition: Font.h:476
YSLib::Drawing::FontFamily
字型家族。
Definition: Font.h:185
FT_Size
::FT_SizeRec_ * FT_Size
Definition: Font.h:45
YSLib::Drawing::c
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect Color c
Definition: YDraw.h:174
FT_Library
::FT_LibraryRec_ * FT_Library
Definition: Font.h:52