YSTest: Font.h 源文件

YSTest PreAlpha_b500_20140530 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
Font.h
浏览该文件的文档.
1 /*
2 © 2009-2014 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 YFM_YSLib_Core_Cache
38
39 //包含 FreeType2 。
40
41 #include <ft2build.h>
42
43 #include FT_FREETYPE_H
44 #include FT_CACHE_H
45
46 namespace YSLib
47 {
48
49 namespace Drawing
50 {
51
52 //前向声明。
53 class Font;
54 class FontCache;
55 class FontFamily;
56 class Typeface;
57
58
63 using FontSize = u8;
68 using FontPath = std::string;
73 using FamilyName = std::string;
78 using StyleName = std::string;
79
80
85 enum class FontStyle : u8
86 {
87 Regular = 0,
88 Bold = 1,
89 Italic = 2,
90 Underline = 4,
91 Strikeout = 8
92 };
93
98 DefBitmaskEnum(FontStyle)
99
100
101
106 yconstfn const char*
107 FetchName(FontStyle style) ynothrow
108 {
109 return style == FontStyle::Bold ? "Bold" : (style == FontStyle::Italic
110 ? "Italic" : (style == FontStyle::Underline ? "Underline" : (style
111 == FontStyle::Strikeout ? "Strikeout" : "Regular")));
112 }
113
114
119 class YF_API FontException : public LoggedEvent
120 {
121 public:
122 using FontError = ::FT_Error;
123
124 private:
125 FontError err;
126
127 public:
128 FontException(FontError e, const std::string& msg = "")
129 : LoggedEvent(msg),
130 err(e)
131 {}
132
133 DefGetter(const ynothrow, FontError, ErrorCode, err)
134 };
135
136
141 class YF_API NativeFontSize final : private noncopyable
142 {
143 private:
144 ::FT_Size size;
145
146 public:
148 NativeFontSize(::FT_FaceRec&, FontSize);
149 NativeFontSize(NativeFontSize&&) ynothrow;
151 ~NativeFontSize();
152
153 ::FT_SizeRec&
154 GetSizeRec() const;
155
161 void
162 Activate() const;
163 };
164
165
170 class YF_API FontFamily final : private noncopyable
171 {
172 public:
173 using FaceMap = map<const StyleName, Typeface*>;
174
175 FontCache& Cache;
176
177 private:
178 FamilyName family_name;
179
180 protected:
181 FaceMap mFaces;
182
183 public:
187 FontFamily(FontCache&, const FamilyName&);
188
193 void
194 operator+=(Typeface&);
199 bool
200 operator-=(Typeface&);
201
202 DefGetter(const ynothrow, const FamilyName&, FamilyName, family_name)
207 Typeface*
208 GetTypefacePtr(FontStyle) const;
212 Typeface*
213 GetTypefacePtr(const StyleName&) const;
215 Typeface&
216 GetTypefaceRef(FontStyle) const;
218 Typeface&
219 GetTypefaceRef(const StyleName&) const;
220 };
221
222
227 class YF_API Typeface final : private noncopyable, private nonmovable
228 {
230 friend class Font;
231
232 public:
233 const FontPath Path;
234
235 private:
237
238 struct BitmapKey
239 {
240 ::FT_UInt Flags;
241 ::FT_UInt GlyphIndex;
242 FontSize Size;
244 FontStyle Style;
245
246 PDefHOp(bool, ==, const BitmapKey& key) const ynothrow
247 ImplRet(Flags == key.Flags && GlyphIndex == key.GlyphIndex
248 && Size == key.Size && Style == key.Style)
249 };
250
251 struct BitmapKeyHash
252 {
253 PDefHOp(size_t, (), const BitmapKey& key) const ynothrow
254 ImplRet(ystdex::hash_combine_seq(size_t(key.Style), key.Size,
255 key.GlyphIndex, key.Flags))
256 };
257
258 struct SmallBitmapData
259 {
260 ::FTC_SBitRec_ sbit;
261
263 SmallBitmapData(::FT_GlyphSlot, FontStyle);
264 SmallBitmapData(SmallBitmapData&&);
265 ~SmallBitmapData();
266 };
268
269 ::FT_Long face_index;
270 ::FT_Int cmap_index;
271 StyleName style_name;
273 pair<std::reference_wrapper<FontFamily>,
274 std::reference_wrapper<::FT_FaceRec_>> ref;
276 mutable GMRUCache<BitmapKey, SmallBitmapData, BitmapKeyHash>
277 bitmap_cache;
279 mutable unordered_map<ucs4_t, ::FT_UInt> glyph_index_cache;
281 mutable unordered_map<FontSize, NativeFontSize> size_cache;
282
283 public:
288 Typeface(FontCache&, const FontPath&, u32 = 0);
290 ~Typeface();
291
295 bool
296 operator==(const Typeface&) const;
300 bool
301 operator<(const Typeface&) const;
302
303 DefGetterMem(const ynothrow, FamilyName, FamilyName, GetFontFamily())
308 DefGetter(const ynothrow, const FontFamily&, FontFamily, ref.first)
309 DefGetter(const ynothrow, const StyleName&, StyleName, style_name)
314 DefGetter(const ynothrow, ::FT_Int, CMapIndex, cmap_index)
315
316 private:
318
319 SmallBitmapData&
320 LookupBitmap(const BitmapKey&) const;
321
322 ::FT_UInt
323 LookupGlyphIndex(ucs4_t) const;
324
326 NativeFontSize&
327 LookupSize(FontSize) const;
328
329 public:
330 PDefH(void, ClearBitmapCache, )
331 ImplExpr(bitmap_cache.clear())
332
333 PDefH(void, ClearGlyphIndexCache, )
334 ImplExpr(glyph_index_cache.clear())
336
338 PDefH(void, ClearSizeCache, )
339 ImplExpr(size_cache.clear())
340 };
341
342
348 YF_API const Typeface&
349 FetchDefaultTypeface();
350
351
357 class YF_API CharBitmap final
358 {
359 public:
360 using BufferType = ::FT_Byte*;
362 enum FormatType
363 {
364 None = ::FT_PIXEL_MODE_NONE,
365 Mono = ::FT_PIXEL_MODE_MONO,
366 Gray = ::FT_PIXEL_MODE_GRAY,
367 Gray2 = ::FT_PIXEL_MODE_GRAY2,
368 Gray4 = ::FT_PIXEL_MODE_GRAY4,
369 LCD = ::FT_PIXEL_MODE_LCD,
370 LCD_V = ::FT_PIXEL_MODE_LCD_V
371 };
372 using NativeType = ::FTC_SBit;
374 using PitchType = ::FT_Short;
375 using ScaleType = ::FT_Byte;
376 using SignedScaleType = ::FT_Char;
377
378 private:
379 NativeType bitmap;
380
381 public:
385 yconstfn
386 CharBitmap(const NativeType& b)
387 : bitmap(b)
388 {}
389
390 yconstfn DefCvt(const ynothrow, NativeType, bitmap)
391
392 yconstfn DefGetter(const ynothrow, BufferType, Buffer, bitmap->buffer)
394 yconstfn DefGetter(const ynothrow, FormatType, Format,
395 FormatType(bitmap->format))
397 yconstfn DefGetter(const ynothrow, ScaleType, GrayLevel, bitmap->max_grays)
398 yconstfn DefGetter(const ynothrow, ScaleType, Height, bitmap->height)
399 yconstfn DefGetter(const ynothrow, SignedScaleType, Left, bitmap->left)
401 yconstfn DefGetter(const ynothrow, PitchType, Pitch, bitmap->pitch)
402 yconstfn DefGetter(const ynothrow, SignedScaleType, Top, bitmap->top)
403 yconstfn DefGetter(const ynothrow, ScaleType, Width, bitmap->width)
404 yconstfn DefGetter(const ynothrow, SignedScaleType, XAdvance,
405 bitmap->xadvance)
406 yconstfn DefGetter(const ynothrow, SignedScaleType, YAdvance,
407 bitmap->yadvance)
408 };
409
410
415 class YF_API FontCache final : private noncopyable,
416 private OwnershipTag<Typeface>, private OwnershipTag<FontFamily>
417 {
418 friend class Typeface;
423 friend class Font;
424
425 public:
426 using FaceSet = set<Typeface*, ystdex::deref_comp<const Typeface>>; \
429 using FamilyMap = unordered_map<FamilyName, unique_ptr<FontFamily>>;
430
436 static yconstexpr size_t DefaultGlyphCacheSize = 128U << 10;
437
438 private:
439 ::FT_Library library;
440
441 protected:
442 FaceSet sFaces;
443 FamilyMap mFamilies;
444
445 Typeface* pDefaultFace;
446
447 public:
453 explicit
454 FontCache(size_t = DefaultGlyphCacheSize);
459 ~FontCache();
460
461 public:
466 DefGetter(const ynothrow, const FaceSet&, Faces, sFaces)
467 DefGetter(const ynothrow, const FamilyMap&, FamilyIndices, mFamilies) \
469 // Font*
470 // GetFontPtr() const;
474 const FontFamily*
475 GetFontFamilyPtr(const FamilyName&) const;
481 const Typeface*
482 GetDefaultTypefacePtr() const;
483 // Typeface*
484 // GetTypefacePtr(u16) const; //!< 取字型组储存的指定索引的字型指针。
488 const Typeface*
489 GetTypefacePtr(const FamilyName&, const StyleName&) const;
490
491 private:
496 void
497 operator+=(unique_ptr<FontFamily>);
502 void
503 operator+=(Typeface&);
504
509 bool
510 operator-=(FontFamily&);
515 bool
516 operator-=(Typeface&);
517
521 void
522 ClearContainers();
523
524 public:
530 size_t
531 LoadTypefaces(const FontPath&);
532
533 public:
537 void
538 InitializeDefaultTypeface();
539 };
540
541
546 class YF_API Font final
547 {
548 public:
549 static yconstexpr FontSize DefaultSize = 12,
550 MinimalSize = 4, MaximalSize = 96;
551
552 private:
554 std::reference_wrapper<Typeface> typeface;
556 FontSize font_size;
561 FontStyle style;
562
563 public:
568 Font()
569 : Font(FetchDefaultTypeface().GetFontFamily())
570 {}
575 explicit
576 Font(const FontFamily&, FontSize = DefaultSize,
577 FontStyle = FontStyle::Regular);
579
580 DefDeCopyCtor(Font)
581 Font(Font&& fnt)
582 : Font(fnt)
583 {}
584 DefDeCopyAssignment(Font)
586
587 DefPred(const ynothrow, Bold, bool(style & FontStyle::Bold))
588 DefPred(const ynothrow, Italic, bool(style & FontStyle::Italic))
589 DefPred(const ynothrow, Underline, bool(style & FontStyle::Underline))
590 DefPred(const ynothrow, Strikeout, bool(style & FontStyle::Strikeout))
591
596 s8
597 GetAdvance(ucs4_t, FTC_SBit = {}) const;
602 s8
603 GetAscender() const;
604 DefGetter(const ynothrow, FontCache&, Cache, GetFontFamily().Cache)
609 s8
610 GetDescender() const;
611 DefGetterMem(const ynothrow, const FamilyName&, FamilyName,
612 GetFontFamily())
613 DefGetterMem(const ynothrow, const FontFamily&, FontFamily, GetTypeface())
614 DefGetter(const ynothrow, FontSize, Size, font_size)
616 DefGetter(const ynothrow, FontStyle, Style, style)
625 CharBitmap
626 GetGlyph(ucs4_t c, ::FT_UInt flags = FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL)
627 const;
632 FontSize
633 GetHeight() const ynothrow;
634 DefGetter(const ynothrow, StyleName, StyleName, FetchName(style))
635
636 private:
641 ::FT_Size_Metrics
642 GetInternalInfo() const;
643
644 public:
649 DefGetter(const ynothrow, Typeface&, Typeface, typeface)
650
654 void
655 SetSize(FontSize = DefaultSize);
661 bool
662 SetStyle(FontStyle);
663 };
664
665 } // namespace Drawing;
666
667 } // namespace YSLib;
668
669 #endif
670
YSLib::Drawing::FontFamily::Cache
FontCache & Cache
Definition: Font.h:175
YSLib::Drawing::FontCache::mFamilies
FamilyMap mFamilies
字型家族组索引。
Definition: Font.h:443
YSLib::Drawing::Typeface::BitmapKey::GlyphIndex
::FT_UInt GlyphIndex
Definition: Font.h:241
ystdex::nonmovable
Definition: utility.hpp:109
YSLib::Drawing::FamilyName
std::string FamilyName
字型家族名称。
Definition: Font.h:73
YSLib::Drawing::CharBitmap::NativeType
::FTC_SBit NativeType
Definition: Font.h:372
ystdex::iterator_transformation::first
static auto first(const _tIterator &i) -> decltype((i->first))
Definition: iterator.hpp:759
YSLib::Drawing::Typeface::BitmapKey::Flags
::FT_UInt Flags
Definition: Font.h:240
ImplRet
#define ImplRet(...)
Definition: YBaseMacro.h:97
DefPred
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:172
YSLib::u32
std::uint32_t u32
Definition: yadaptor.h:69
YSLib::OwnershipTag
指定对于参数指定类型的成员具有所有权的标签。
Definition: yobject.h:47
YSLib::Drawing::bitmap_cache
bitmap_cache(2047U)
YSLib::Drawing::FontStyle::Strikeout
删除线。
platform_ex::Windows::err
err(ec)
Definition: MinGW32.cpp:51
YSLib::Drawing::NativeFontSize::size
::FT_Size size
Definition: Font.h:144
YSLib::Drawing::FontStyle::Underline
下划线。
YSLib::Drawing::Typeface::style_name
StyleName style_name
Definition: Font.h:271
YSLib::Drawing::Typeface::BitmapKeyHash
Definition: Font.h:251
ystdex::operator<
bool operator<(const path< _tSeqCon, _tNorm > &x, const path< _tSeqCon, _tNorm > &y)
Definition: path.hpp:379
YF_API
#define YF_API
Definition: Platform.h:64
YSLib::Drawing::Font::DefDeCopyCtor
DefDeCopyCtor(Font) Font(Font &&fnt)
Definition: Font.h:580
YSLib::Drawing::FontException
字体异常。
Definition: Font.h:119
YSLib::Drawing::FontCache::library
::FT_Library library
库实例。
Definition: Font.h:439
YSLib::Drawing::FontException::FontError
::FT_Error FontError
Definition: Font.h:122
YSLib::Drawing::CharBitmap::bitmap
NativeType bitmap
Definition: Font.h:379
DefBitmaskEnum
#define DefBitmaskEnum(_tEnum)
Definition: YBaseMacro.h:392
YSLib::Drawing::Typeface::SmallBitmapData::sbit
::FTC_SBitRec_ sbit
Definition: Font.h:260
YSLib::Drawing::FontStyle::Regular
常规字体。
YSLib::Drawing::CharBitmap::ScaleType
::FT_Byte ScaleType
Definition: Font.h:375
YSLib::Drawing::FontCache::pDefaultFace
Typeface * pDefaultFace
默认字型指针。
Definition: Font.h:445
YSLib::Drawing::Typeface::bitmap_cache
GMRUCache< BitmapKey, SmallBitmapData, BitmapKeyHash > bitmap_cache
Definition: Font.h:277
YSLib::Drawing::Typeface::ref
pair< std::reference_wrapper< FontFamily >, std::reference_wrapper<::FT_FaceRec_ > > ref
Definition: Font.h:274
YSLib::Drawing::NativeFontSize
本机字体大小。
Definition: Font.h:141
YSLib::Drawing::operator==
yconstfn bool operator==(const GBinaryGroup< _type > &a, const GBinaryGroup< _type > &b) ynothrow
比较：屏幕二元组相等关系。
Definition: ygdibase.h:181
YSLib::Drawing::CharBitmap
字符位图。
Definition: Font.h:357
YSLib::Drawing::FontException::FontException
FontException(FontError e, const std::string &msg="")
Definition: Font.h:128
YSLib::Drawing::FontStyle::Italic
斜体。
ystdex::noncopyable
不可复制对象：禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: utility.hpp:75
YSLib::Drawing::SetSize
sizeof(AlphaType)*GetAreaOf(GetSize())) using CompactPixmap void SetSize(const Size &) override
重新设置缓冲区大小。
YSLib::Drawing::StyleName
std::string StyleName
字型样式名称。
Definition: Font.h:78
ImplExpr
#define ImplExpr(...)
Definition: YBaseMacro.h:93
YSLib::Drawing::FontCache
字体缓存。
Definition: Font.h:415
YSLib::Drawing::Typeface
字型标识。
Definition: Font.h:227
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
YSLib::Activate
bool Activate(const shared_ptr< Shell > &hShl)
激活 Shell 对象：控制权转移给此对象以维持单线程运行。
Definition: yapp.h:164
YSLib::Drawing::FontFamily::mFaces
FaceMap mFaces
字型组索引类型。
Definition: Font.h:181
YSLib::Drawing::Typeface::BitmapKey
Definition: Font.h:238
YSLib::string
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
YSLib::s8
std::int8_t s8
Definition: yadaptor.h:71
YSLib::Drawing::FontStyle
FontStyle
字体样式。
Definition: Font.h:85
YSLib::Drawing::Font::style
FontStyle style
字体样式。
Definition: Font.h:561
YSLib::Drawing::Font::font_size
FontSize font_size
Definition: Font.h:556
YSLib::Drawing::CharBitmap::BufferType
::FT_Byte * BufferType
Definition: Font.h:360
YSLib::Drawing::Typeface::face_index
::FT_Long face_index
Definition: Font.h:269
YSLib::Drawing::FetchDefaultTypeface
YF_API const Typeface & FetchDefaultTypeface()
取默认字型引用。
Definition: Font.cpp:367
YSLib::Drawing::Font::typeface
std::reference_wrapper< Typeface > typeface
Definition: Font.h:554
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
YSLib::Drawing::CharBitmap::SignedScaleType
::FT_Char SignedScaleType
Definition: Font.h:376
YSLib::GMRUCache
按最近最多使用策略刷新的缓存。
Definition: Cache.hpp:48
platform::KeyCategory::None
表示未分配或保留的键。
Definition: Keys.h:99
YSLib::Drawing::Typeface::SmallBitmapData
Definition: Font.h:258
YSLib::Drawing::FontFamily::FaceMap
map< const StyleName, Typeface * > FaceMap
字型组索引类型。
Definition: Font.h:173
YSLib::u8
std::uint8_t u8
通用数据类型。
Definition: yadaptor.h:67
YSLib::Drawing::Typeface::cmap_index
::FT_Int cmap_index
Definition: Font.h:270
YSLib::Drawing::CharBitmap::CharBitmap
yconstfn CharBitmap(const NativeType &b)
使用本机类型对象构造字符位图对象。
Definition: Font.h:386
YSLib::Drawing::FontFamily::family_name
FamilyName family_name
Definition: Font.h:178
YSLib::Drawing::c
c
Definition: ystyle.h:93
YSLib::Drawing::FontException::err
FontError err
Definition: Font.h:125
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: yexcept.h:58
YSLib::Drawing::Typeface::BitmapKey::Style
FontStyle Style
Definition: Font.h:244
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:462
YSLib::Drawing::FontPath
std::string FontPath
字体文件路径。
Definition: Font.h:68
platform::Consoles::Gray
Definition: Video.h:467
DefCvt
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:164
YSLib::Drawing::Typeface::size_cache
unordered_map< FontSize, NativeFontSize > size_cache
Definition: Font.h:281
YSLib::Drawing::Size::Size
yconstfn Size() ynothrow
无参数构造。
Definition: ygdibase.h:266
CHRLib::ucs4_t
char32_t ucs4_t
UCS-4 字符类型。
Definition: chrdef.h:45
DefGetterMem
#define DefGetterMem(_q, _t, _n, _m)
Definition: YBaseMacro.h:185
YSLib::Drawing::Width
if(YB_UNLIKELY(r >=sGraphics.Height)) throw std return pBuffer r *sGraphics Width
Definition: ygdibase.cpp:155
YSLib::Drawing::PDefH
PDefH(void, DrawLineSeg, const Graphics &g, const Rect &bounds, const Point &p1, const Point &p2, Color c) ImplExpr(DrawLineSeg(g
YSLib::Drawing::FontCache::FamilyMap
unordered_map< FamilyName, unique_ptr< FontFamily >> FamilyMap
字型家族组索引类型。
Definition: Font.h:429
YSLib::Drawing::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:546
YSLib::Drawing::DefGetter
DefGetter(const ynothrow, AlphaType *, BufferAlphaPtr, pBufferAlpha) DefGetter(const ynothrow
取 Alpha 缓冲区的指针。
YSLib::Drawing::FontCache::FaceSet
set< Typeface *, ystdex::deref_comp< const Typeface >> FaceSet
Definition: Font.h:427
YSLib::Drawing::format
format(fmt)
Definition: Image.cpp:242
YSLib::Drawing::Typeface::BitmapKey::Size
FontSize Size
Definition: Font.h:242
YSLib::Drawing::Font::Font
Font()
默认构造：使用默认字型家族、大小和样式的字体对象。
Definition: Font.h:568
YSLib::Drawing::Size
屏幕区域大小。
Definition: ygdibase.h:249
YSLib::Drawing::Typeface::Path
const FontPath Path
Definition: Font.h:233
YSLib::Drawing::CharBitmap::FormatType
FormatType
Definition: Font.h:362
YSLib::Drawing::FontCache::sFaces
FaceSet sFaces
字型组。
Definition: Font.h:442
YSLib::Drawing::FontStyle::Bold
粗体。
YSLib::Drawing::Typeface::glyph_index_cache
unordered_map< ucs4_t,::FT_UInt > glyph_index_cache
Definition: Font.h:279
YSLib::Drawing::FontFamily
字型家族 (Typeface Family) 标识。
Definition: Font.h:170
ystdex::hash_combine_seq
size_t hash_combine_seq(size_t seed, const _type &val)
重复计算序列散列。
Definition: functional.hpp:487
DefDeCopyAssignment
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154
YSLib::Drawing::FontSize
u8 FontSize
字体大小。
Definition: Font.h:63
YSLib::Drawing::CharBitmap::PitchType
::FT_Short PitchType
Definition: Font.h:374