-
-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathFontTest.cpp
241 lines (203 loc) · 8.66 KB
/
FontTest.cpp
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
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://axmol.dev/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "FontTest.h"
#include "../testResource.h"
#include "2d/FontAtlasCache.h"
#include "2d/FontFreeType.h"
using namespace ax;
enum
{
kTagLabel1,
kTagLabel2,
kTagLabel3,
kTagLabel4,
kTagColor1,
kTagColor2,
kTagColor3,
};
// you don't need any ifdef anymore
static std::string fontList[] = {
"fonts/A Damn Mess.ttf", "fonts/Abberancy.ttf", "fonts/Abduction.ttf",
"fonts/Paint Boy.ttf", "fonts/Schwarzwald.ttf", "fonts/Scissor Cuts.ttf",
};
static int vAlignIdx = 0;
static TextVAlignment verticalAlignment[] = {
TextVAlignment::TOP,
TextVAlignment::CENTER,
TextVAlignment::BOTTOM,
};
FontTests::FontTests()
{
for (auto&& fontFile : fontList)
{
addTestCase("FontTests", [&]() {
vAlignIdx = 0;
return FontTest::create(fontFile);
});
}
for (auto&& fontFile : fontList)
{
addTestCase("FontTests", [&]() {
vAlignIdx = 1;
return FontTest::create(fontFile);
});
}
for (auto&& fontFile : fontList)
{
addTestCase("FontTests", [&]() {
vAlignIdx = 2;
return FontTest::create(fontFile);
});
}
ADD_TEST_CASE(FontNoReplacementTest);
}
void FontTest::showFont(std::string_view fontFile)
{
auto s = Director::getInstance()->getWinSize();
auto blockSize = Size(s.width / 3, 200);
float fontSize = 26;
removeChildByTag(kTagLabel1, true);
removeChildByTag(kTagLabel2, true);
removeChildByTag(kTagLabel3, true);
removeChildByTag(kTagLabel4, true);
removeChildByTag(kTagColor1, true);
removeChildByTag(kTagColor2, true);
removeChildByTag(kTagColor3, true);
auto top = Label::createWithSystemFont(fontFile, fontFile, 24);
auto left = Label::createWithSystemFont("alignment left", fontFile, fontSize, blockSize, TextHAlignment::LEFT,
verticalAlignment[vAlignIdx]);
auto center = Label::createWithSystemFont("alignment center", fontFile, fontSize, blockSize, TextHAlignment::CENTER,
verticalAlignment[vAlignIdx]);
auto right = Label::createWithSystemFont("alignment right", fontFile, fontSize, blockSize, TextHAlignment::RIGHT,
verticalAlignment[vAlignIdx]);
auto leftColor = LayerColor::create(Color4B(100, 100, 100, 255), blockSize.width, blockSize.height);
auto centerColor = LayerColor::create(Color4B(200, 100, 100, 255), blockSize.width, blockSize.height);
auto rightColor = LayerColor::create(Color4B(100, 100, 200, 255), blockSize.width, blockSize.height);
leftColor->setIgnoreAnchorPointForPosition(false);
centerColor->setIgnoreAnchorPointForPosition(false);
rightColor->setIgnoreAnchorPointForPosition(false);
top->setAnchorPoint(Vec2(0.5, 1));
left->setAnchorPoint(Vec2(0, 0.5));
leftColor->setAnchorPoint(Vec2(0, 0.5));
center->setAnchorPoint(Vec2(0, 0.5));
centerColor->setAnchorPoint(Vec2(0, 0.5));
right->setAnchorPoint(Vec2(0, 0.5));
rightColor->setAnchorPoint(Vec2(0, 0.5));
top->setPosition(s.width / 2, s.height - 20);
left->setPosition(0, s.height / 2);
leftColor->setPosition(left->getPosition());
center->setPosition(blockSize.width, s.height / 2);
centerColor->setPosition(center->getPosition());
right->setPosition(blockSize.width * 2, s.height / 2);
rightColor->setPosition(right->getPosition());
this->addChild(leftColor, -1, kTagColor1);
this->addChild(left, 0, kTagLabel1);
this->addChild(rightColor, -1, kTagColor2);
this->addChild(right, 0, kTagLabel2);
this->addChild(centerColor, -1, kTagColor3);
this->addChild(center, 0, kTagLabel3);
this->addChild(top, 0, kTagLabel4);
}
std::string FontTest::title() const
{
return "Font test";
}
FontNoReplacementTest* FontNoReplacementTest::create()
{
auto ret = new FontNoReplacementTest;
if (ret->init())
{
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
}
return ret;
}
FontNoReplacementTest::FontNoReplacementTest()
{
}
FontNoReplacementTest::~FontNoReplacementTest()
{
// need to clear the caches since we change the lookup dictionary after the application init.
FontAtlasCache::unloadFontAtlasTTF("fonts/A Damn Mess.ttf");
FontFreeType::releaseFont("fonts/A Damn Mess.ttf");
FontAtlasCache::unloadFontAtlasTTF("fonts/Abberancy.ttf");
FontFreeType::releaseFont("fonts/Abberancy.ttf");
FontAtlasCache::unloadFontAtlasTTF("fonts/Abduction.ttf");
FontFreeType::releaseFont("fonts/Abduction.ttf");
FontAtlasCache::unloadFontAtlasTTF("fonts/Schwarzwald.ttf");
FontFreeType::releaseFont("fonts/Schwarzwald.ttf");
}
void FontNoReplacementTest::onEnter()
{
TestCase::onEnter();
std::string suffix;
auto s = Director::getInstance()->getWinSize();
auto blockSize = Size(s.width / 3, 200);
float fontSize = 26;
removeChildByTag(kTagLabel1, true);
removeChildByTag(kTagLabel2, true);
removeChildByTag(kTagLabel3, true);
removeChildByTag(kTagLabel4, true);
removeChildByTag(kTagColor1, true);
removeChildByTag(kTagColor2, true);
removeChildByTag(kTagColor3, true);
auto top = Label::createWithTTF("fonts/A Damn Mess.ttf" + suffix, "fonts/A Damn Mess.ttf", 24);
auto left = Label::createWithTTF("fonts/Abberancy.ttf" + suffix, "fonts/Abberancy.ttf", fontSize, blockSize,
TextHAlignment::LEFT, verticalAlignment[0]);
auto center = Label::createWithTTF("fonts/Abduction.ttf" + suffix, "fonts/Abduction.ttf", fontSize, blockSize,
TextHAlignment::CENTER, verticalAlignment[0]);
auto right = Label::createWithTTF("fonts/Schwarzwald.ttf" + suffix, "fonts/Schwarzwald.ttf", fontSize, blockSize,
TextHAlignment::RIGHT, verticalAlignment[0]);
auto leftColor = LayerColor::create(Color4B(100, 100, 100, 255), blockSize.width, blockSize.height);
auto centerColor = LayerColor::create(Color4B(200, 100, 100, 255), blockSize.width, blockSize.height);
auto rightColor = LayerColor::create(Color4B(100, 100, 200, 255), blockSize.width, blockSize.height);
leftColor->setIgnoreAnchorPointForPosition(false);
centerColor->setIgnoreAnchorPointForPosition(false);
rightColor->setIgnoreAnchorPointForPosition(false);
top->setAnchorPoint(Vec2(0.5, 1));
left->setAnchorPoint(Vec2(0, 0.5));
leftColor->setAnchorPoint(Vec2(0, 0.5));
center->setAnchorPoint(Vec2(0, 0.5));
centerColor->setAnchorPoint(Vec2(0, 0.5));
right->setAnchorPoint(Vec2(0, 0.5));
rightColor->setAnchorPoint(Vec2(0, 0.5));
top->setPosition(s.width / 2, s.height - 20);
left->setPosition(0, s.height / 2);
leftColor->setPosition(left->getPosition());
center->setPosition(blockSize.width, s.height / 2);
centerColor->setPosition(center->getPosition());
right->setPosition(blockSize.width * 2, s.height / 2);
rightColor->setPosition(right->getPosition());
this->addChild(leftColor, -1, kTagColor1);
this->addChild(left, 0, kTagLabel1);
this->addChild(rightColor, -1, kTagColor2);
this->addChild(right, 0, kTagLabel2);
this->addChild(centerColor, -1, kTagColor3);
this->addChild(center, 0, kTagLabel3);
this->addChild(top, 0, kTagLabel4);
}
std::string FontNoReplacementTest::title() const
{
return "Font no replacement test";
}