-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathFont.c
371 lines (293 loc) · 10.4 KB
/
Font.c
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2016-7-27
* Update : 2021-2-8
* Author : scott.cgi
*/
#include <stdio.h>
#include <string.h>
#include "Engine/Toolkit/Utils/ArrayIntSet.h"
#include "Engine/Extension/Font.h"
#include "Engine/Toolkit/Platform/Log.h"
#include "Engine/Graphics/OpenGL/SubMesh.h"
static ArrayList(Font*) fontCacheList[1] = AArrayList_Init(Font*, 5);
static ArrayList(FontText*) textCacheList[1] = AArrayList_Init(FontText*, 30);
static Font* Get(const char* filePath)
{
TextureAtlas* textureAtlas = ATextureAtlas->Get(filePath);
ALog_A
(
textureAtlas->textureList->size == 1,
"Font not support TextureAtlas has multiple texture"
);
Font* font = AArrayList_Pop(fontCacheList, Font*);
if (font == NULL)
{
font = malloc(sizeof(Font));
AMesh->InitWithCapacity
(
AArrayList_Get(textureAtlas->textureList, 0, Texture*),
50,
font->mesh
);
AArrayIntSet->Init(font->fontTextSet);
AArrayList ->Init(sizeof(SubMesh*), font->unusedSubMeshList);
}
else
{
AArrayIntSet->Clear(font->fontTextSet);
AArrayList ->Clear(font->unusedSubMeshList);
AMesh ->Clear(font->mesh);
}
font->textureAtlas = textureAtlas;
return font;
}
static FontText* GetText(Font* font)
{
FontText* text = AArrayList_Pop(textCacheList, FontText*);
if (text == NULL)
{
text = malloc(sizeof(FontText));
AArrayList->Init(sizeof(SubMesh*), text->usedSubMeshList);
text->usedSubMeshList->increase = 10;
}
else
{
AArrayList->Clear(text->usedSubMeshList);
}
// reset drawable
ADrawable->Init(text->drawable);
text->alignment = FontTextAlignment_HorizontalLeft;
text->charSpacing = 0.0f;
text->font = font;
AArrayIntSet->TryAdd(font->fontTextSet, (intptr_t) text);
return text;
}
static void Draw(Font* font)
{
for (int i = 0; i < font->fontTextSet->elementList->size; ++i)
{
ADrawable->Draw(AArrayList_Get(font->fontTextSet->elementList, i, FontText*)->drawable);
}
AMesh_Draw(font->mesh);
}
static inline TextureAtlasQuad* GetAtlasQuad(FontText* text, const char* str, int index)
{
TextureAtlasQuad* atlasQuad = ATextureAtlas_GetQuad(text->font->textureAtlas, (char[2]) {str[index], '\0'});
ALog_A
(
atlasQuad != NULL,
"AFont SetString not found char = %c in TextureAtlas quads = %s",
str[index],
text->font->textureAtlas->filePath
);
return atlasQuad;
}
static inline void SetNewChar(FontText* text, const char* str, int len)
{
// set text new char
for (int i = 0; i < len; ++i)
{
TextureAtlasQuad* atlasQuad = GetAtlasQuad(text, str, i);
SubMesh* subMesh = AArrayList_Get(text->usedSubMeshList, i, SubMesh*);
ASubMesh->SetUVWithQuad
(
subMesh,
atlasQuad->quad
);
}
}
static void SetString(FontText* text, const char* str)
{
ArrayList* children = text->font->mesh->childList;
int len = (int) strlen(str);
text->drawable->height = 0.0f;
text->drawable->width = 0.0f;
if (len == text->usedSubMeshList->size)
{
SetNewChar(text, str, len);
}
else if (len > text->usedSubMeshList->size)
{
SetNewChar(text, str, text->usedSubMeshList->size);
int originalSize = children->size;
// add chars more than text has
for (int i = text->usedSubMeshList->size; i < len; ++i)
{
TextureAtlasQuad* atlasQuad = GetAtlasQuad(text, str, i);
SubMesh* subMesh;
if (text->font->unusedSubMeshList->size > 0)
{
subMesh = AArrayList_Pop(text->font->unusedSubMeshList, SubMesh*);
ADrawable_SetVisible(subMesh->drawable);
ADrawable_SetPositionSame2(subMesh->drawable, 0.0f);
ADrawable_SetColor(subMesh->drawable, COLOR_WHITE_ARRAY);
ASubMesh->SetUVWithQuad
(
subMesh,
atlasQuad->quad
);
if (subMesh->drawable->parent != text->drawable)
{
ADrawable_SetParent(subMesh->drawable, text->drawable);
}
}
else
{
// not enough SubMesh for text char so add SubMesh
subMesh = AMesh->AddChildWithQuad(text->font->mesh, atlasQuad->quad);
ADrawable_SetParent(subMesh->drawable, text->drawable);
}
AArrayList_Add(text->usedSubMeshList, subMesh);
}
if (originalSize != children->size)
{
// if regenerate the SubMesh's drawable the parent must visible,
// or parent property will lost
AMesh->GenerateBuffer(text->font->mesh);
}
}
else
{
SetNewChar(text, str, len);
// text has more chars remove it
for (int i = text->usedSubMeshList->size - 1; i >= len; --i)
{
SubMesh* subMesh = AArrayList_Get(text->usedSubMeshList, i, SubMesh*);
if (ADrawable_CheckVisible(subMesh->drawable))
{
ADrawable_SetInvisible(subMesh->drawable);
}
AArrayList->Remove(text->usedSubMeshList, i);
AArrayList_Add(text->font->unusedSubMeshList, subMesh);
}
}
// set text size arrangement and alignment
// set each char position
switch (text->alignment)
{
case FontTextAlignment_HorizontalLeft:
{
for (int i = 0; i < text->usedSubMeshList->size; ++i)
{
SubMesh* subMesh = AArrayList_Get(text->usedSubMeshList, i, SubMesh*);
if (subMesh->drawable->height > text->drawable->height)
{
text->drawable->height = subMesh->drawable->height;
}
ADrawable_SetPositionX(subMesh->drawable, text->drawable->width + subMesh->drawable->width / 2);
text->drawable->width += subMesh->drawable->width + text->charSpacing;
}
// remove the end space
text->drawable->width -= text->charSpacing;
break;
}
case FontTextAlignment_HorizontalRight:
{
for (int i = text->usedSubMeshList->size - 1; i > -1; --i)
{
SubMesh* subMesh = AArrayList_Get(text->usedSubMeshList, i, SubMesh*);
if (subMesh->drawable->height > text->drawable->height)
{
text->drawable->height = subMesh->drawable->height;
}
ADrawable_SetPositionX(subMesh->drawable, -text->drawable->width - subMesh->drawable->width / 2);
text->drawable->width += subMesh->drawable->width + text->charSpacing;
}
// remove the end space
text->drawable->width -= text->charSpacing;
break;
}
case FontTextAlignment_VerticalTop:
{
for (int i = 0; i < text->usedSubMeshList->size; ++i)
{
SubMesh* subMesh = AArrayList_Get(text->usedSubMeshList, i, SubMesh*);
if (subMesh->drawable->width > text->drawable->width)
{
text->drawable->width = subMesh->drawable->width;
}
ADrawable_SetPositionY(subMesh->drawable, -text->drawable->height - subMesh->drawable->height / 2);
text->drawable->height += subMesh->drawable->height + text->charSpacing;
}
// remove the end space
text->drawable->height -= text->charSpacing;
break;
}
case FontTextAlignment_VerticalBottom:
{
for (int i = text->usedSubMeshList->size - 1; i > -1; --i)
{
SubMesh* subMesh = AArrayList_Get(text->usedSubMeshList, i, SubMesh*);
if (subMesh->drawable->width > text->drawable->width)
{
text->drawable->width = subMesh->drawable->width;
}
ADrawable_SetPositionY(subMesh->drawable, text->drawable->height + subMesh->drawable->height / 2);
text->drawable->height += subMesh->drawable->height + text->charSpacing;
}
// remove the end space
text->drawable->height -= text->charSpacing;
break;
}
}
}
static void SetInt(FontText* text, int num)
{
// max int digits count
char buffer[12];
sprintf (buffer, "%d", num);
SetString(text, buffer);
}
static void SetFloat(FontText* text, float num)
{
// max float digits count
char buffer[20];
sprintf (buffer, "%.1f", num);
SetString(text, buffer);
}
static void Release(Font* font)
{
ALog_A(font->textureAtlas != NULL, "AFont Release font %p already Released", font);
for (int i = 0; i < font->fontTextSet->elementList->size; ++i)
{
FontText* text = AArrayList_Get(font->fontTextSet->elementList, i, FontText*);
text->font = NULL;
AArrayList_Add(textCacheList, text);
}
font->textureAtlas = NULL;
AArrayList_Add(fontCacheList, font);
}
static void ReleaseText(FontText* text)
{
ALog_A(text->font != NULL, "AFont ReleaseText text %p already Released", text);
for (int i = 0; i < text->usedSubMeshList->size; ++i)
{
SubMesh* subMesh = AArrayList_Get(text->usedSubMeshList, i, SubMesh*);
ADrawable_SetInvisible(subMesh->drawable);
AArrayList_Add(text->font->unusedSubMeshList, subMesh);
}
AArrayIntSet->TryRemove(text->font->fontTextSet, (intptr_t) text);
ADrawable_SetColor(text->drawable, COLOR_WHITE_ARRAY);
text->font = NULL;
AArrayList_Add(textCacheList, text);
}
struct AFont AFont[1] =
{{
Get,
GetText,
Draw,
SetString,
SetInt,
SetFloat,
Release,
ReleaseText,
}};