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;
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 
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 
120 {
121 public:
122  using FontError = ::FT_Error;
123 
124 private:
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);
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 
176 
177 private:
179 
180 protected:
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;
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 
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 
259  {
260  ::FTC_SBitRec_ sbit;
261 
263  SmallBitmapData(::FT_GlyphSlot, FontStyle);
265  ~SmallBitmapData();
266  };
268 
269  ::FT_Long face_index;
270  ::FT_Int cmap_index;
273  pair<std::reference_wrapper<FontFamily>,
274  std::reference_wrapper<::FT_FaceRec_>> ref;
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 
320  LookupBitmap(const BitmapKey&) const;
321 
322  ::FT_UInt
323  LookupGlyphIndex(ucs4_t) const;
324 
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&
350 
351 
357 class YF_API CharBitmap final
358 {
359 public:
360  using BufferType = ::FT_Byte*;
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:
380 
381 public:
385  yconstfn
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:
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;
562 
563 public:
569  : Font(FetchDefaultTypeface().GetFontFamily())
570  {}
575  explicit
576  Font(const FontFamily&, FontSize = DefaultSize,
579 
581  Font(Font&& fnt)
582  : Font(fnt)
583  {}
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 
FontCache & Cache
Definition: Font.h:175
FamilyMap mFamilies
字型家族组索引。
Definition: Font.h:443
std::string FamilyName
字型家族名称。
Definition: Font.h:73
::FTC_SBit NativeType
Definition: Font.h:372
static auto first(const _tIterator &i) -> decltype((i->first))
Definition: iterator.hpp:759
#define ImplRet(...)
Definition: YBaseMacro.h:97
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:172
std::uint32_t u32
Definition: yadaptor.h:69
指定对于参数指定类型的成员具有所有权的标签。
Definition: yobject.h:47
bitmap_cache(2047U)
StyleName style_name
Definition: Font.h:271
bool operator<(const path< _tSeqCon, _tNorm > &x, const path< _tSeqCon, _tNorm > &y)
Definition: path.hpp:379
#define YF_API
Definition: Platform.h:64
DefDeCopyCtor(Font) Font(Font &&fnt)
Definition: Font.h:580
字体异常。
Definition: Font.h:119
::FT_Library library
库实例。
Definition: Font.h:439
#define DefBitmaskEnum(_tEnum)
Definition: YBaseMacro.h:392
Typeface * pDefaultFace
默认字型指针。
Definition: Font.h:445
GMRUCache< BitmapKey, SmallBitmapData, BitmapKeyHash > bitmap_cache
Definition: Font.h:277
pair< std::reference_wrapper< FontFamily >, std::reference_wrapper<::FT_FaceRec_ > > ref
Definition: Font.h:274
本机字体大小。
Definition: Font.h:141
yconstfn bool operator==(const GBinaryGroup< _type > &a, const GBinaryGroup< _type > &b) ynothrow
比较:屏幕二元组相等关系。
Definition: ygdibase.h:181
字符位图。
Definition: Font.h:357
FontException(FontError e, const std::string &msg="")
Definition: Font.h:128
不可复制对象:禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: utility.hpp:75
sizeof(AlphaType)*GetAreaOf(GetSize())) using CompactPixmap void SetSize(const Size &) override
重新设置缓冲区大小。
std::string StyleName
字型样式名称。
Definition: Font.h:78
#define ImplExpr(...)
Definition: YBaseMacro.h:93
字体缓存。
Definition: Font.h:415
字型标识。
Definition: Font.h:227
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
bool Activate(const shared_ptr< Shell > &hShl)
激活 Shell 对象:控制权转移给此对象以维持单线程运行。
Definition: yapp.h:164
FaceMap mFaces
字型组索引类型。
Definition: Font.h:181
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
std::int8_t s8
Definition: yadaptor.h:71
FontStyle
字体样式。
Definition: Font.h:85
FontStyle style
字体样式。
Definition: Font.h:561
FontSize font_size
Definition: Font.h:556
::FT_Byte * BufferType
Definition: Font.h:360
::FT_Long face_index
Definition: Font.h:269
YF_API const Typeface & FetchDefaultTypeface()
取默认字型引用。
Definition: Font.cpp:367
std::reference_wrapper< Typeface > typeface
Definition: Font.h:554
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
::FT_Char SignedScaleType
Definition: Font.h:376
按最近最多使用策略刷新的缓存。
Definition: Cache.hpp:48
表示未分配或保留的键。
Definition: Keys.h:99
map< const StyleName, Typeface * > FaceMap
字型组索引类型。
Definition: Font.h:173
std::uint8_t u8
通用数据类型。
Definition: yadaptor.h:67
::FT_Int cmap_index
Definition: Font.h:270
yconstfn CharBitmap(const NativeType &b)
使用本机类型对象构造字符位图对象。
Definition: Font.h:386
FamilyName family_name
Definition: Font.h:178
记录日志的异常事件类。
Definition: yexcept.h:58
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:462
std::string FontPath
字体文件路径。
Definition: Font.h:68
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:164
unordered_map< FontSize, NativeFontSize > size_cache
Definition: Font.h:281
yconstfn Size() ynothrow
无参数构造。
Definition: ygdibase.h:266
char32_t ucs4_t
UCS-4 字符类型。
Definition: chrdef.h:45
#define DefGetterMem(_q, _t, _n, _m)
Definition: YBaseMacro.h:185
if(YB_UNLIKELY(r >=sGraphics.Height)) throw std return pBuffer r *sGraphics Width
Definition: ygdibase.cpp:155
PDefH(void, DrawLineSeg, const Graphics &g, const Rect &bounds, const Point &p1, const Point &p2, Color c) ImplExpr(DrawLineSeg(g
unordered_map< FamilyName, unique_ptr< FontFamily >> FamilyMap
字型家族组索引类型。
Definition: Font.h:429
字体:字模,包含字型、样式和大小。
Definition: Font.h:546
DefGetter(const ynothrow, AlphaType *, BufferAlphaPtr, pBufferAlpha) DefGetter(const ynothrow
取 Alpha 缓冲区的指针。
set< Typeface *, ystdex::deref_comp< const Typeface >> FaceSet
Definition: Font.h:427
Font()
默认构造:使用默认字型家族、大小和样式的字体对象。
Definition: Font.h:568
屏幕区域大小。
Definition: ygdibase.h:249
const FontPath Path
Definition: Font.h:233
FaceSet sFaces
字型组。
Definition: Font.h:442
unordered_map< ucs4_t,::FT_UInt > glyph_index_cache
Definition: Font.h:279
字型家族 (Typeface Family) 标识。
Definition: Font.h:170
size_t hash_combine_seq(size_t seed, const _type &val)
重复计算序列散列。
Definition: functional.hpp:487
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154
u8 FontSize
字体大小。
Definition: Font.h:63
::FT_Short PitchType
Definition: Font.h:374