PuzzleSDK
FontTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "FontTest.h"
26#include "../testResource.h"
27#include "2d/CCFontAtlasCache.h"
28#include "2d/CCFontFreeType.h"
29
31
32enum {
37
41};
42
43//you don't need any ifdef anymore
44static std::string fontList[] =
45{
46 "fonts/A Damn Mess.ttf",
47 "fonts/Abberancy.ttf",
48 "fonts/Abduction.ttf",
49 "fonts/Paint Boy.ttf",
50 "fonts/Schwarzwald.ttf",
51 "fonts/Scissor Cuts.ttf",
52};
53
54static int vAlignIdx = 0;
55static TextVAlignment verticalAlignment[] =
56{
57 TextVAlignment::TOP,
58 TextVAlignment::CENTER,
59 TextVAlignment::BOTTOM,
60};
61
62
63FontTests::FontTests()
64{
65 for (auto& fontFile : fontList)
66 {
67 addTestCase("FontTests", [&](){vAlignIdx = 0; return FontTest::create(fontFile); });
68 }
69
70 for (auto& fontFile : fontList)
71 {
72 addTestCase("FontTests", [&](){ vAlignIdx = 1; return FontTest::create(fontFile); });
73 }
74
75 for (auto& fontFile : fontList)
76 {
77 addTestCase("FontTests", [&](){vAlignIdx = 2; return FontTest::create(fontFile); });
78 }
81}
82
83void FontTest::showFont(const std::string& fontFile)
84{
85 auto s = Director::getInstance()->getWinSize();
86
87 auto blockSize = Size(s.width/3, 200);
88 float fontSize = 26;
89
90 removeChildByTag(kTagLabel1, true);
91 removeChildByTag(kTagLabel2, true);
92 removeChildByTag(kTagLabel3, true);
93 removeChildByTag(kTagLabel4, true);
94 removeChildByTag(kTagColor1, true);
95 removeChildByTag(kTagColor2, true);
96 removeChildByTag(kTagColor3, true);
97
98 auto top = Label::createWithSystemFont(fontFile, fontFile, 24);
99 auto left = Label::createWithSystemFont("alignment left", fontFile, fontSize,
100 blockSize, TextHAlignment::LEFT, verticalAlignment[vAlignIdx]);
101 auto center = Label::createWithSystemFont("alignment center", fontFile, fontSize,
102 blockSize, TextHAlignment::CENTER, verticalAlignment[vAlignIdx]);
103 auto right = Label::createWithSystemFont("alignment right", fontFile, fontSize,
104 blockSize, TextHAlignment::RIGHT, verticalAlignment[vAlignIdx]);
105
106 auto leftColor = LayerColor::create(Color4B(100, 100, 100, 255), blockSize.width, blockSize.height);
107 auto centerColor = LayerColor::create(Color4B(200, 100, 100, 255), blockSize.width, blockSize.height);
108 auto rightColor = LayerColor::create(Color4B(100, 100, 200, 255), blockSize.width, blockSize.height);
109
110 leftColor->setIgnoreAnchorPointForPosition(false);
111 centerColor->setIgnoreAnchorPointForPosition(false);
112 rightColor->setIgnoreAnchorPointForPosition(false);
113
114
115 top->setAnchorPoint(Vec2(0.5, 1));
116 left->setAnchorPoint(Vec2(0,0.5));
117 leftColor->setAnchorPoint(Vec2(0,0.5));
118 center->setAnchorPoint(Vec2(0,0.5));
119 centerColor->setAnchorPoint(Vec2(0,0.5));
120 right->setAnchorPoint(Vec2(0,0.5));
121 rightColor->setAnchorPoint(Vec2(0,0.5));
122
123 top->setPosition(s.width/2,s.height-20);
124 left->setPosition(0,s.height/2);
125 leftColor->setPosition(left->getPosition());
126 center->setPosition(blockSize.width, s.height/2);
127 centerColor->setPosition(center->getPosition());
128 right->setPosition(blockSize.width*2, s.height/2);
129 rightColor->setPosition(right->getPosition());
130
131 this->addChild(leftColor, -1, kTagColor1);
132 this->addChild(left, 0, kTagLabel1);
133 this->addChild(rightColor, -1, kTagColor2);
134 this->addChild(right, 0, kTagLabel2);
135 this->addChild(centerColor, -1, kTagColor3);
136 this->addChild(center, 0, kTagLabel3);
137 this->addChild(top, 0, kTagLabel4);
138}
139
140std::string FontTest::title() const
141{
142 return "Font test";
143}
144
146{
147 auto ret = new (std::nothrow) FontNoReplacementTest;
148 if (ret && ret->init())
149 {
150 ret->autorelease();
151 }
152 else
153 {
154 delete ret;
155 ret = nullptr;
156 }
157
158 return ret;
159}
160
162{
163 _replace = false;
164}
165
167{
168 // need to clear the caches since we change the lookup dictionary after the application init.
169 FontAtlasCache::unloadFontAtlasTTF("fonts/A Damn Mess.ttf");
170 FontFreeType::releaseFont("fonts/A Damn Mess.ttf");
171 FontAtlasCache::unloadFontAtlasTTF("fonts/Abberancy.ttf");
172 FontFreeType::releaseFont("fonts/Abberancy.ttf");
173 FontAtlasCache::unloadFontAtlasTTF("fonts/Abduction.ttf");
174 FontFreeType::releaseFont("fonts/Abduction.ttf");
175 FontAtlasCache::unloadFontAtlasTTF("fonts/Schwarzwald.ttf");
176 FontFreeType::releaseFont("fonts/Schwarzwald.ttf");
177 FileUtils::getInstance()->setFilenameLookupDictionary(ValueMap());
178}
179
181{
183
184 std::string suffix;
185 if (_replace)
186 {
187 ValueMap dict{
188 { "fonts/A Damn Mess.ttf", Value("fonts/arial.ttf") },
189 { "fonts/Abberancy.ttf", Value("fonts/arial.ttf") },
190 { "fonts/Abduction.ttf", Value("fonts/arial.ttf") },
191 { "fonts/Schwarzwald.ttf", Value("fonts/arial.ttf") }
192 };
193
194 FileUtils::getInstance()->setFilenameLookupDictionary(dict);
195 suffix = " replaced by arial.ttf";
196 }
197
198 auto s = Director::getInstance()->getWinSize();
199
200 auto blockSize = Size(s.width / 3, 200);
201 float fontSize = 26;
202
203 removeChildByTag(kTagLabel1, true);
204 removeChildByTag(kTagLabel2, true);
205 removeChildByTag(kTagLabel3, true);
206 removeChildByTag(kTagLabel4, true);
207 removeChildByTag(kTagColor1, true);
208 removeChildByTag(kTagColor2, true);
209 removeChildByTag(kTagColor3, true);
210
211 auto top = Label::createWithTTF("fonts/A Damn Mess.ttf" + suffix, "fonts/A Damn Mess.ttf", 24);
212 auto left = Label::createWithTTF("fonts/Abberancy.ttf" + suffix, "fonts/Abberancy.ttf", fontSize,
213 blockSize, TextHAlignment::LEFT, verticalAlignment[0]);
214 auto center = Label::createWithTTF("fonts/Abduction.ttf" + suffix, "fonts/Abduction.ttf", fontSize,
215 blockSize, TextHAlignment::CENTER, verticalAlignment[0]);
216 auto right = Label::createWithTTF("fonts/Schwarzwald.ttf" + suffix, "fonts/Schwarzwald.ttf", fontSize,
217 blockSize, TextHAlignment::RIGHT, verticalAlignment[0]);
218
219 auto leftColor = LayerColor::create(Color4B(100, 100, 100, 255), blockSize.width, blockSize.height);
220 auto centerColor = LayerColor::create(Color4B(200, 100, 100, 255), blockSize.width, blockSize.height);
221 auto rightColor = LayerColor::create(Color4B(100, 100, 200, 255), blockSize.width, blockSize.height);
222
223 leftColor->setIgnoreAnchorPointForPosition(false);
224 centerColor->setIgnoreAnchorPointForPosition(false);
225 rightColor->setIgnoreAnchorPointForPosition(false);
226
227 top->setAnchorPoint(Vec2(0.5, 1));
228 left->setAnchorPoint(Vec2(0, 0.5));
229 leftColor->setAnchorPoint(Vec2(0, 0.5));
230 center->setAnchorPoint(Vec2(0, 0.5));
231 centerColor->setAnchorPoint(Vec2(0, 0.5));
232 right->setAnchorPoint(Vec2(0, 0.5));
233 rightColor->setAnchorPoint(Vec2(0, 0.5));
234
235 top->setPosition(s.width / 2, s.height - 20);
236 left->setPosition(0, s.height / 2);
237 leftColor->setPosition(left->getPosition());
238 center->setPosition(blockSize.width, s.height / 2);
239 centerColor->setPosition(center->getPosition());
240 right->setPosition(blockSize.width * 2, s.height / 2);
241 rightColor->setPosition(right->getPosition());
242
243 this->addChild(leftColor, -1, kTagColor1);
244 this->addChild(left, 0, kTagLabel1);
245 this->addChild(rightColor, -1, kTagColor2);
246 this->addChild(right, 0, kTagLabel2);
247 this->addChild(centerColor, -1, kTagColor3);
248 this->addChild(center, 0, kTagLabel3);
249 this->addChild(top, 0, kTagLabel4);
250}
251
253{
254 return "Font no replacement test";
255}
256
258{
259 auto ret = new (std::nothrow) FontReplacementTest;
260 if (ret && ret->init())
261 {
262 ret->autorelease();
263 }
264 else
265 {
266 delete ret;
267 ret = nullptr;
268 }
269
270 return ret;
271}
272
274{
275 _replace = true;
276}
277
278std::string FontReplacementTest::title() const
279{
280 return "Font replacement test";
281}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
static TextVAlignment verticalAlignment[]
Definition: FontTest.cpp:55
static int vAlignIdx
Definition: FontTest.cpp:54
static std::string fontList[]
Definition: FontTest.cpp:44
@ kTagColor1
Definition: FontTest.cpp:38
@ kTagLabel4
Definition: FontTest.cpp:36
@ kTagColor2
Definition: FontTest.cpp:39
@ kTagLabel1
Definition: FontTest.cpp:33
@ kTagLabel3
Definition: FontTest.cpp:35
@ kTagLabel2
Definition: FontTest.cpp:34
@ kTagColor3
Definition: FontTest.cpp:40
USING_NS_CC
Definition: FontTest.cpp:30
virtual std::string title() const override
Definition: FontTest.cpp:252
virtual void onEnter() override
Definition: FontTest.cpp:180
static FontNoReplacementTest * create()
Definition: FontTest.cpp:145
static FontReplacementTest * create()
Definition: FontTest.cpp:257
virtual std::string title() const override
Definition: FontTest.cpp:278
static FontTest * create(const std::string &fontFile)
Definition: FontTest.h:36
void showFont(const std::string &fontFile)
Definition: FontTest.cpp:83
virtual std::string title() const override
Definition: FontTest.cpp:140
virtual void onEnter() override
Definition: BaseTest.cpp:430