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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#include "UnityPrefix.h"
#include "TextUtil.h"
#include "Runtime/Filters/Misc/Font.h"
#include "Runtime/Misc/ResourceManager.h"
#include "Runtime/Graphics/Transform.h"
#include "Runtime/Shaders/Material.h"
#include "Runtime/Shaders/Shader.h"
#include "Runtime/GfxDevice/GfxDevice.h"
#include "Runtime/Shaders/VBO.h"
#include "Runtime/Scripting/ScriptingUtility.h"
#include "Runtime/Shaders/Shader.h"
#include "Runtime/Scripting/ScriptingUtility.h"
using namespace std;
class Font;
namespace TextUtil_Static
{
static Font *s_DefaultFont = NULL;
} // namespace TextUtil_Static
static PPtr <Font> s_CurrentFont = NULL;
static PPtr<Material> s_CurrentMaterial = NULL;
void SetTextFont (Font *font) { s_CurrentFont = font; }
void SetDrawTextMaterial (Material *m) { s_CurrentMaterial = m; }
Material *GetDrawTextMaterial () { return s_CurrentMaterial; }
extern "C" UInt16* AllocateUTF16Memory(int bytes)
{
return (UInt16*)UNITY_MALLOC(kMemUTF16String,bytes);
}
#if ENABLE_SCRIPTING
#if ENABLE_MONO
UTF16String::UTF16String (MonoString *sourceString) {
if (sourceString != SCRIPTING_NULL && mono_string_length (sourceString) != 0)
{
text = mono_string_chars (sourceString);
length = mono_string_length (sourceString);
owns = false;
}
else
{
owns = false;
text = NULL;
length = 0;
}
}
#elif UNITY_WINRT
UTF16String::UTF16String (ScriptingStringPtr sourceString)
{
if (sourceString != SCRIPTING_NULL)
{
Platform::String^ utf16String = safe_cast<Platform::String^>(sourceString);
length = utf16String->Length();
int size = length * sizeof(UInt16);
text = (UInt16*)UNITY_MALLOC(kMemUTF16String, size);
memcpy(text, utf16String->Data(), size);
owns = true;
}
else
{
owns = false;
text = NULL;
length = 0;
}
}
#else
UTF16String::UTF16String (ScriptingStringPtr sourceString)
{
if (sourceString != SCRIPTING_NULL)
{
std::string str = scripting_cpp_string_for(sourceString);
text = AllocateUTF16Memory(str.size()*sizeof(UInt16));
ConvertUTF8toUTF16 (str.c_str(), str.size(), text, length);
owns = true;
}
else
{
owns = false;
text = NULL;
length = 0;
}
}
#endif
#endif
extern "C" void UTF16String_TakeOverPreAllocatedUTF16Bytes(UTF16String* utfString, UInt16* bytes, int length)
{
utfString->TakeOverPreAllocatedUTF16Bytes(bytes,length);
}
void UTF16String::TakeOverPreAllocatedUTF16Bytes(UInt16* bytes, int size)
{
if (owns)
UNITY_FREE(kMemUTF16String,text);
text = bytes;
length = size;
owns = true;
}
void UTF16String::InitFromCharPointer (const char* str)
{
int slen = strlen (str);
if (slen != 0) {
text = AllocateUTF16Memory(slen*sizeof(UInt16));
ConvertUTF8toUTF16 (str, slen, text, length);
owns = true;
}
else {
text = NULL;
length = 0;
owns = false;
}
}
UTF16String::UTF16String (const char* str) {
InitFromCharPointer(str);
}
UTF16String::~UTF16String () {
if (owns)
UNITY_FREE(kMemUTF16String,text);
}
UTF16String::UTF16String (const UTF16String &other) {
if (other.length != 0)
{
length = other.length;
text =(UInt16*)UNITY_MALLOC(kMemUTF16String,length*sizeof(UInt16));
memcpy(text,other.text,sizeof (UInt16) * length);
owns = true;
}
else
{
length = 0;
text = NULL;
owns = false;
}
}
void UTF16String::CopyString (const UTF16String &other)
{
if (owns)
UNITY_FREE(kMemUTF16String,text);
if (other.length != 0)
{
length = other.length;
text =(UInt16*)UNITY_MALLOC(kMemUTF16String,length*sizeof(UInt16));
memcpy(text,other.text,sizeof (UInt16) * length);
owns = true;
}
else
{
length = 0;
text = NULL;
owns = false;
}
}
#if ENABLE_SCRIPTING
#if ENABLE_MONO
void UTF16String::BorrowString( ScriptingString *sourceString )
{
if (owns)
UNITY_FREE(kMemUTF16String,text);
if (sourceString != NULL && mono_string_length (sourceString) != 0)
{
text = mono_string_chars (sourceString);
length = mono_string_length (sourceString);
owns = false;
}
else
{
owns = false;
text = NULL;
length = 0;
}
}
#elif UNITY_WINRT
void UTF16String::BorrowString(ScriptingStringPtr sourceString)
{
if (owns)
UNITY_FREE(kMemUTF16String,text);
if (sourceString != SCRIPTING_NULL)
{
Platform::String^ utf16String = safe_cast<Platform::String^>(sourceString);
length = utf16String->Length();
int size = length * sizeof(UInt16);
text = (UInt16*)UNITY_MALLOC(kMemUTF16String, size);
memcpy(text, utf16String->Data(), size);
owns = true;
}
else
{
owns = false;
text = NULL;
length = 0;
}
}
#endif
void UTF8ToUTF16String (const char* src, UTF16String& dst)
{
UTF16String tmp(src);
dst.TakeOverPreAllocatedUTF16Bytes(tmp.text, tmp.length);
tmp.owns = false;
}
ScriptingStringPtr UTF16String::GetScriptingString ()
{
if (!length || !text)
return SCRIPTING_NULL;
#if ENABLE_MONO
UInt16* buffer = new UInt16[length+1];
memcpy(buffer, text, length * 2);
buffer[length] = 0;
MonoString* retval = MonoStringNewUTF16 ((const wchar_t*) buffer);
delete[] buffer;
return retval;
#elif UNITY_FLASH || UNITY_WINRT
std::vector<char> tempStr;
tempStr.resize(length * 4);
int outLength;
ConvertUTF16toUTF8 (text, length, &tempStr[0], outLength);
tempStr[outLength]=0;
return scripting_string_new((char const*)&tempStr[0]);
#endif
}
#endif
Font*
GetTextFont ()
{
using namespace TextUtil_Static;
Font* font = s_CurrentFont;
if (font == NULL) {
// Make sure we have default resource
if (s_DefaultFont == NULL) {
s_DefaultFont = GetBuiltinResource<Font> (kDefaultFontName);
if (!s_DefaultFont || !s_DefaultFont->GetMaterial()) {
LogString ("Couldn't load default font or font material!");
}
}
font = s_DefaultFont;
}
return font;
}
#include "TextMeshGenerator2.h"
static TextMeshGenerator2 &GetMeshGen (const UTF16String &text, Font *font, TextAnchor align, float width) {
const float tabSize = 16;
return TextMeshGenerator2::Get (text, font, align, kAuto, width, tabSize, 1.0f, false, true, ColorRGBA32(0xffffffff));
}
float CalcTextHeight (const UTF16String &text, float width) {
TextMeshGenerator2 &gen = GetMeshGen (text, (Font*)GetTextFont(), kDontCare, width);
return gen.GetSize().y;
}
Vector2f CalcTextSize (const UTF16String &text) {
TextMeshGenerator2 &gen = GetMeshGen (text, (Font*)GetTextFont(), kDontCare, 0);
return gen.GetSize ();
}
Vector2f GetCursorPosition (const Rectf &screenRect, const UTF16String &text, TextAnchor align, bool wordWrap, int textPosition) {
float width = wordWrap ? screenRect.Width() : 0;
TextMeshGenerator2 &gen = GetMeshGen (text, (Font*)GetTextFont(), align, width);
return gen.GetCursorPosition(screenRect, textPosition);
}
|