YSTest: Font.h 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
Font.h
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2009 - 2013.
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/yfunc.hpp"
32 #include "../Core/yobject.h"
33 #include <string>
34 #include "../Core/yexcept.h"
35 #include "YTextBase.h"
36
37 //包含 FreeType2 。
38
39 #include <ft2build.h>
40
41 #include FT_FREETYPE_H
42 #include FT_CACHE_H
43
44 YSL_BEGIN
45
46 YSL_BEGIN_NAMESPACE(Drawing)
47
48 //前向声明。
49 class Font;
50 class FontCache;
51 class FontFamily;
52 class Typeface;
53
54
59 typedef u8 FontSize;
64 typedef std::string FontPath;
69 typedef std::string FamilyName;
74 typedef std::string StyleName;
75
76
81 enum class FontStyle : u8
82 {
83 Regular = 0,
84 Bold = 1,
85 Italic = 2,
86 Underline = 4,
87 Strikeout = 8
88 };
89
90 DefBitmaskOperations(FontStyle, u8)
91
92
93
98 yconstfn const char*
99 FetchName(FontStyle style) ynothrow
100 {
101 return style == FontStyle::Bold ? "Bold" : (style == FontStyle::Italic
102 ? "Italic" : (style == FontStyle::Underline ? "Underline" : (style
103 == FontStyle::Strikeout ? "Strikeout" : "Regular")));
104 }
105
106
112 class YF_API FontFamily : private noncopyable
113 {
114 public:
115 typedef map<const StyleName, Typeface*> FaceMap;
116
117 FontCache& Cache;
118
119 private:
120 FamilyName family_name;
121
122 protected:
123 FaceMap mFaces;
124
125 public:
129 FontFamily(FontCache&, const FamilyName&);
130
135 void
136 operator+=(Typeface&);
141 bool
142 operator-=(Typeface&);
143
144 DefGetter(const ynothrow, const FamilyName&, FamilyName, family_name)
149 Typeface*
150 GetTypefacePtr(FontStyle) const;
154 Typeface*
155 GetTypefacePtr(const StyleName&) const;
156 };
157
158
164 class YF_API Typeface : private noncopyable
165 {
166 friend ::FT_Error
167 simpleFaceRequester(::FTC_FaceID, ::FT_Library, ::FT_Pointer, ::FT_Face*);
168
169 // static const ::FT_Matrix MNormal, MOblique;
170 public:
171 const FontPath Path;
172
173 private:
174 FontFamily* pFontFamily;
175 // ::FT_Face face;
176 // bool bBold, bOblique, bUnderline;
177 StyleName style_name;
178
179 ::FT_Long face_index;
180 ::FT_Int cmap_index;
181 /* FT_Long nGlyphs;
182 FT_UShort uUnitPerEM;
183 FT_Int nCharmaps;
184 FT_Long lUnderlinePos;
185 FT_Matrix matrix;
186 vector<u8> fixSizes;*/
187
188 public:
193 Typeface(FontCache&, const FontPath&, u32 = 0
194 /*, const bool bb = false,
195 const bool bi = false, const bool bu = false*/);
196
200 bool
201 operator==(const Typeface&) const;
205 bool
206 operator<(const Typeface&) const;
207
208 DefGetterMem(const ynothrow, FamilyName, FamilyName, GetFontFamily())
213 DefGetter(const ynothrow, const FontFamily&, FontFamily, *pFontFamily)
214 DefGetter(const ynothrow, const StyleName&, StyleName, style_name)
219 DefGetter(const ynothrow, ::FT_Int, CMapIndex, cmap_index)
220 };
221
222
229 YF_API const Typeface&
230 FetchDefaultTypeface() ythrow(LoggedEvent);
231
232
239 class YF_API CharBitmap
240 {
241 public:
242 typedef ::FTC_SBit NativeType;
243 typedef ::FT_Byte* BufferType;
244 typedef ::FT_Byte ScaleType;
245 typedef ::FT_Char SignedScaleType;
246
247 private:
248 NativeType bitmap;
249
250 public:
254 yconstfn
255 CharBitmap(const NativeType& b)
256 : bitmap(b)
257 {}
258
259 yconstfn DefCvt(const ynothrow, NativeType, bitmap)
260
261 yconstfn DefGetter(const ynothrow, BufferType, Buffer, bitmap->buffer)
262 yconstfn DefGetter(const ynothrow, ScaleType, Width, bitmap->width)
263 yconstfn DefGetter(const ynothrow, ScaleType, Height, bitmap->height)
264 yconstfn DefGetter(const ynothrow, SignedScaleType, Left, bitmap->left)
265 yconstfn DefGetter(const ynothrow, SignedScaleType, Top, bitmap->top)
266 yconstfn DefGetter(const ynothrow, SignedScaleType, XAdvance,
267 bitmap->xadvance)
268 yconstfn DefGetter(const ynothrow, SignedScaleType, YAdvance,
269 bitmap->yadvance)
270 };
271
272
278 class YF_API FontCache : private noncopyable,
279 private OwnershipTag<Typeface>, private OwnershipTag<FontFamily>
280 {
281 friend class Typeface;
286 friend class Font;
287
288 public:
289 typedef set<Typeface*, ystdex::deref_comp<const Typeface>>
290 FaceSet;
291 typedef map<FamilyName, FontFamily*> FamilyMap;
292
298 static yconstexpr size_t DefaultGlyphCacheSize = 128U << 10;
299
300 private:
301 ::FT_Library library;
302 ::FTC_Manager manager;
303 ::FTC_CMapCache cmapCache;
304 ::FTC_SBitCache sbitCache;
305
306 protected:
307 FaceSet sFaces;
308 FamilyMap mFamilies;
309
310 Typeface* pDefaultFace;
311
312 public:
317 explicit
318 FontCache(size_t = DefaultGlyphCacheSize);
322 ~FontCache();
323
324 public:
329 DefGetter(const ynothrow, const FaceSet&, Faces, sFaces)
330 DefGetter(const ynothrow, const FamilyMap&, FamilyIndices, mFamilies) \
332 // Font*
333 // GetFontPtr() const;
337 const FontFamily*
338 GetFontFamilyPtr(const FamilyName&) const;
344 const Typeface*
345 GetDefaultTypefacePtr() const ythrow(LoggedEvent);
346 // Typeface*
347 // GetTypefacePtr(u16) const; //!< 取字型组储存的指定索引的字型指针。
351 const Typeface*
352 GetTypefacePtr(const FamilyName&, const StyleName&) const;
353
354 private:
359 ::FT_Face
360 GetNativeFace(Typeface*) const;
361
366 void
367 operator+=(FontFamily&);
372 void
373 operator+=(Typeface&);
374
379 bool
380 operator-=(FontFamily&);
385 bool
386 operator-=(Typeface&);
387
388 public:
392 void
393 ClearCache();
394
395 private:
399 void
400 ClearContainers();
401
406 void
407 LoadTypeface(const FontPath&, size_t) ynothrow;
408
409 public:
415 size_t
416 LoadTypefaces(const FontPath&);
417
418 public:
422 void
423 InitializeDefaultTypeface();
424
425 /*
426 !\brief 清除字形缓存。
427 */
428 PDefH(void, ResetGlyphCache, )
429 ImplRet(FTC_Manager_Reset(manager))
430 };
431
432
438 class YF_API Font
439 {
440 public:
441 static yconstexpr FontSize DefaultSize = 12,
442 MinimalSize = 4, MaximalSize = 96;
443
444 private:
445 mutable ::FTC_ScalerRec scaler;
450 FontStyle style;
451
452 public:
457 Font()
458 : Font(FetchDefaultTypeface().GetFontFamily())
459 {}
464 explicit
465 Font(const FontFamily&, FontSize = DefaultSize,
466 FontStyle = FontStyle::Regular);
467
468 DefPred(const ynothrow, Bold, bool(style & FontStyle::Bold))
469 DefPred(const ynothrow, Italic, bool(style & FontStyle::Italic))
470 DefPred(const ynothrow, Underline, bool(style & FontStyle::Underline))
471 DefPred(const ynothrow, Strikeout, bool(style & FontStyle::Strikeout))
472
477 s8
478 GetAdvance(ucs4_t, FTC_SBit = {}) const;
483 s8
484 GetAscender() const;
489 s8
490 GetDescender() const;
491 DefGetterMem(const ynothrow, const FamilyName&, FamilyName,
492 GetFontFamily())
493 DefGetter(const ynothrow, FontCache&, Cache, GetFontFamily().Cache)
494 DefGetterMem(const ynothrow, const FontFamily&, FontFamily, GetTypeface())
495 DefGetter(const ynothrow, FontSize, Size, FontSize(scaler.height))
496 DefGetter(const ynothrow, FontStyle, style, style)
504 CharBitmap
505 GetGlyph(ucs4_t c, ::FT_UInt flags = FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL)
506 const;
511 FontSize
512 GetHeight() const ynothrow;
513 DefGetter(const ynothrow, StyleName, StyleName, FetchName(style))
514
515 private:
520 ::FT_SizeRec&
521 GetInternalInfo() const;
522
523 public:
528 DefGetter(const ynothrow, Typeface&, Typeface,
529 *static_cast<Typeface*>(scaler.face_id))
530
534 void
535 SetSize(FontSize = DefaultSize);
540 bool
541 SetStyle(FontStyle);
542 };
543
544 YSL_END_NAMESPACE(Drawing)
545
546 YSL_END
547
548 #endif
549