forked from HeliumProject/Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFont.h
244 lines (198 loc) · 8.72 KB
/
Font.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#pragma once
#include "Graphics/Graphics.h"
#include "Engine/Resource.h"
#include "Platform/Trace.h"
#include "Foundation/StringConverter.h"
#include "Rendering/RRenderResource.h"
#include "Reflect/MetaEnum.h"
#include "Reflect/MetaStruct.h"
namespace Helium
{
HELIUM_DECLARE_RPTR( RTexture2d );
/// Font resource.
class HELIUM_GRAPHICS_API Font : public Resource
{
HELIUM_DECLARE_ASSET( Font, Resource );
public:
/// Font texture sheet compression options.
struct ECompression : Reflect::Enum
{
enum Enum
{
GRAYSCALE_UNCOMPRESSED,
COLOR_COMPRESSED,
};
HELIUM_DECLARE_ENUM( ECompression );
static void PopulateMetaType( Helium::Reflect::MetaEnum& info )
{
info.AddElement( GRAYSCALE_UNCOMPRESSED, TXT( "GRAYSCALE_UNCOMPRESSED" ) );
info.AddElement( COLOR_COMPRESSED, TXT( "COLOR_COMPRESSED" ) );
}
};
/// Default texture point size.
static const uint32_t DEFAULT_POINT_SIZE = 12;
/// Default display resolution, in DPI.
static const uint32_t DEFAULT_DPI = 72;
/// Default texture sheet width, in pixels.
static const uint16_t DEFAULT_TEXTURE_SHEET_WIDTH = 256;
/// Default texture sheet height, in pixels.
static const uint16_t DEFAULT_TEXTURE_SHEET_HEIGHT = 256;
/// Default texture compression scheme.
static const ECompression::Enum DEFAULT_TEXTURE_COMPRESSION;
/// Character information.
struct HELIUM_GRAPHICS_API Character : Reflect::Struct
{
HELIUM_DECLARE_BASE_STRUCT(Font::Character);
static void PopulateMetaType( Reflect::MetaStruct& comp );
bool operator== (const Character& rhs) const
{
return (codePoint == rhs.codePoint &&
imageX == rhs.imageX &&
imageY == rhs.imageY &&
imageWidth == rhs.imageWidth &&
imageHeight == rhs.imageHeight &&
width == rhs.width &&
height == rhs.height &&
bearingX == rhs.bearingX &&
bearingY == rhs.bearingY &&
advance == rhs.advance &&
texture == rhs.texture);
}
bool operator!= (const Character& rhs) const
{
return !(*this == rhs);
}
/// Unicode code point value.
uint32_t codePoint;
/// Horizontal pixel coordinate of the top-left corner of the character in the texture sheet.
uint16_t imageX;
/// Vertical pixel coordinate of the top-left corner of the character in the texture sheet.
uint16_t imageY;
/// Pixel width of the character in the texture sheet.
uint16_t imageWidth;
/// Pixel height of the character in the texture sheet.
uint16_t imageHeight;
/// Character bounding box width (26.6 fixed-point value, in pixels).
int32_t width;
/// Character bounding box height (26.6 fixed-point value, in pixels).
int32_t height;
/// Horizontal distance from the cursor position to the left-most border of the character bounding box (26.6
/// fixed-point value, in pixels).
int32_t bearingX;
/// Vertical distance from the cursor position (on the baseline) to the top-most border of the character
/// bounding box (26.6 fixed-point value, in pixels).
int32_t bearingY;
/// Horizontal distance used to increment the cursor position when drawing the character (26.6 fixed-point
/// value, in pixels).
int32_t advance;
/// Texture sheet index.
uint8_t texture;
};
struct HELIUM_GRAPHICS_API PersistentResourceData : public Object
{
HELIUM_DECLARE_CLASS(Font::PersistentResourceData, Reflect::Object);
PersistentResourceData();
static void PopulateMetaType( Reflect::MetaStruct& comp );
/// Cached ascender height, in pixels (26.6 fixed-point value).
int32_t m_ascender;
/// Cached descender depth, in pixels (26.6 fixed-point value).
int32_t m_descender;
/// Cached font height, in pixels (26.6 fixed-point value).
int32_t m_height;
/// Maximum advance width when rendering text, in pixels (26.6 fixed-point value).
int32_t m_maxAdvance;
/// Array of characters (ordered by code point value to allow for binary searching).
//DynamicArray<CharacterPtr> m_characters;
DynamicArray<Character> m_characters;
/// Array of texture sheets.
RTexture2dPtr* m_pspTextures;
/// Texture sheet load IDs.
size_t* m_pTextureLoadIds;
/// Number of texture sheets.
uint8_t m_textureCount;
};
/// Persistent font resource data.
PersistentResourceData m_persistentResourceData;
/// @name Construction/Destruction
//@{
Font();
virtual ~Font();
//@}
static void PopulateMetaType( Reflect::MetaStruct& comp );
virtual bool NeedsPrecacheResourceData() const override;
virtual bool BeginPrecacheResourceData() override;
virtual bool TryFinishPrecacheResourceData() override;
//@}
/// @name Resource Serialization
//@{
virtual bool LoadPersistentResourceObject(Reflect::ObjectPtr &_object) override;
//@}
/// @name Resource Caching Support
//@{
virtual Name GetCacheName() const override;
//@}
/// @name Data Access
//@{
inline float32_t GetPointSize() const;
inline uint32_t GetDpi() const;
inline uint16_t GetTextureSheetWidth() const;
inline uint16_t GetTextureSheetHeight() const;
inline ECompression GetTextureCompression() const;
inline bool GetAntialiased() const;
inline int32_t GetAscenderFixed() const;
inline int32_t GetDescenderFixed() const;
inline int32_t GetHeightFixed() const;
inline int32_t GetMaxAdvanceFixed() const;
inline float32_t GetAscenderFloat() const;
inline float32_t GetDescenderFloat() const;
inline float32_t GetHeightFloat() const;
inline float32_t GetMaxAdvanceFloat() const;
//@}
/// @name Character Information
/// All character codes must be provided as Unicode code points (this may require conversion of string data if
/// the application is not built in Unicode mode, or for other special cases such as surrogate pairs in UTF-16
/// strings).
//@{
inline uint32_t GetCharacterCount() const;
inline const Character& GetCharacter( uint32_t index ) const;
inline uint32_t GetCharacterIndex( const Character* pCharacter ) const;
inline const Character* FindCharacter( uint32_t codePoint ) const;
//@}
/// @name Texture Sheet Access
//@{
inline uint8_t GetTextureSheetCount() const;
inline RTexture2d* GetTextureSheet( uint8_t index ) const;
//@}
/// @name Text Processing Support
//@{
template< typename GlyphHandler, typename CharType >
void ProcessText( const CharType* pString, GlyphHandler& rGlyphHandler ) const;
template< typename GlyphHandler, typename CharType, typename Allocator >
void ProcessText( const StringBase< CharType, Allocator >& rString, GlyphHandler& rGlyphHandler ) const;
//@}
/// @name Static Utility Functions
//@{
inline static float32_t Fixed26x6ToFloat32( int32_t value );
inline static int32_t Float32ToFixed26x6( float32_t value );
//@}
private:
/// Font size in points.
float32_t m_pointSize;
/// Font resolution, in DPI.
uint32_t m_dpi;
/// Width of each texture sheet.
uint16_t m_textureSheetWidth;
/// Height of each texture sheet.
uint16_t m_textureSheetHeight;
/// Texture compression scheme.
ECompression m_textureCompression;
/// True if this font should use anti-aliasing to smooth edges, false if not.
bool m_bAntialiased;
/// @name Text Processing Support, Private
//@{
template< typename GlyphHandler, typename CharType >
void ProcessText( const CharType* pString, size_t characterCount, GlyphHandler& rGlyphHandler ) const;
//@}
};
}
#include "Graphics/Font.inl"