1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsCorePrerequisites.h"
6
7namespace bs
8{
9 /** @addtogroup Text
10 * @{
11 */
12
13 /** Kerning pair representing larger or smaller offset between a specific pair of characters. */
14 struct BS_SCRIPT_EXPORT(pl:true,m:GUI_Engine) KerningPair
15 {
16 UINT32 otherCharId;
17 INT32 amount;
18 };
19
20 /** Describes a single character in a font of a specific size. */
21 struct BS_SCRIPT_EXPORT(pl:true,m:GUI_Engine) CharDesc
22 {
23 UINT32 charId; /**< Character ID, corresponding to a Unicode key. */
24 UINT32 page; /**< Index of the texture the character is located on. */
25 float uvX, uvY; /**< Texture coordinates of the character in the page texture. */
26 float uvWidth, uvHeight; /**< Width/height of the character in texture coordinates. */
27 UINT32 width, height; /**< Width/height of the character in pixels. */
28 INT32 xOffset, yOffset; /**< Offset for the visible portion of the character in pixels. */
29 INT32 xAdvance, yAdvance; /**< Determines how much to advance the pen after writing this character, in pixels. */
30
31 /**
32 * Pairs that determine if certain character pairs should be closer or father together. for example "AV"
33 * combination.
34 */
35 Vector<KerningPair> kerningPairs;
36 };
37
38 /** @cond SPECIALIZATIONS */
39
40 // Make CHAR_DESC serializable
41 template<> struct RTTIPlainType<CharDesc>
42 {
43 enum { id = TID_CHAR_DESC }; enum { hasDynamicSize = 1 };
44
45 static void toMemory(const CharDesc& data, char* memory)
46 {
47 UINT32 size = getDynamicSize(data);
48
49 memcpy(memory, &size, sizeof(UINT32));
50 memory += sizeof(UINT32);
51
52 memory = rttiWriteElem(data.charId, memory);
53 memory = rttiWriteElem(data.page, memory);
54 memory = rttiWriteElem(data.uvX, memory);
55 memory = rttiWriteElem(data.uvY, memory);
56 memory = rttiWriteElem(data.uvWidth, memory);
57 memory = rttiWriteElem(data.uvHeight, memory);
58 memory = rttiWriteElem(data.width, memory);
59 memory = rttiWriteElem(data.height, memory);
60 memory = rttiWriteElem(data.xOffset, memory);
61 memory = rttiWriteElem(data.yOffset, memory);
62 memory = rttiWriteElem(data.xAdvance, memory);
63 memory = rttiWriteElem(data.yAdvance, memory);
64 rttiWriteElem(data.kerningPairs, memory);
65 }
66
67 static UINT32 fromMemory(CharDesc& data, char* memory)
68 {
69 UINT32 size;
70 memcpy(&size, memory, sizeof(UINT32));
71 memory += sizeof(UINT32);
72
73 memory = rttiReadElem(data.charId, memory);
74 memory = rttiReadElem(data.page, memory);
75 memory = rttiReadElem(data.uvX, memory);
76 memory = rttiReadElem(data.uvY, memory);
77 memory = rttiReadElem(data.uvWidth, memory);
78 memory = rttiReadElem(data.uvHeight, memory);
79 memory = rttiReadElem(data.width, memory);
80 memory = rttiReadElem(data.height, memory);
81 memory = rttiReadElem(data.xOffset, memory);
82 memory = rttiReadElem(data.yOffset, memory);
83 memory = rttiReadElem(data.xAdvance, memory);
84 memory = rttiReadElem(data.yAdvance, memory);
85 rttiReadElem(data.kerningPairs, memory);
86
87 return size;
88 }
89
90 static UINT32 getDynamicSize(const CharDesc& data)
91 {
92 UINT64 dataSize = sizeof(data.charId)
93 + sizeof(data.page)
94 + sizeof(data.uvX)
95 + sizeof(data.uvY)
96 + sizeof(data.uvWidth)
97 + sizeof(data.uvHeight)
98 + sizeof(data.width)
99 + sizeof(data.height)
100 + sizeof(data.xOffset)
101 + sizeof(data.yOffset)
102 + sizeof(data.xAdvance)
103 + sizeof(data.yAdvance)
104 + RTTIPlainType<Vector<KerningPair>>::getDynamicSize(data.kerningPairs);
105
106 dataSize += sizeof(UINT32);
107
108 return (UINT32)dataSize;
109 }
110 };
111
112 /** @endcond */
113 /** @} */
114}
115