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
|
/* Copyright (C) 2022 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lib/self_test.h"
#include "graphics/FontManager.h"
#include "graphics/FontMetrics.h"
#include "gui/CGUI.h"
#include "gui/CGUIText.h"
#include "gui/SettingTypes/CGUIString.h"
#include "ps/CLogger.h"
#include "ps/ConfigDB.h"
#include "ps/Filesystem.h"
#include "ps/ProfileViewer.h"
#include "ps/VideoMode.h"
#include "renderer/Renderer.h"
#include "scriptinterface/ScriptInterface.h"
#include <algorithm>
#include <array>
class TestCGUIText : public CxxTest::TestSuite
{
CProfileViewer* m_Viewer = nullptr;
CRenderer* m_Renderer = nullptr;
public:
void setUp()
{
g_VFS = CreateVfs();
TS_ASSERT_OK(g_VFS->Mount(L"", DataDir() / "mods" / "_test.minimal" / "", VFS_MOUNT_MUST_EXIST));
TS_ASSERT_OK(g_VFS->Mount(L"cache", DataDir() / "_testcache" / "", 0, VFS_MAX_PRIORITY));
CXeromyces::Startup();
// The renderer spews messages.
TestLogger logger;
// We need to initialise the renderer to initialise the font manager.
// TODO: decouple this.
CConfigDB::Initialise();
CConfigDB::Instance()->SetValueString(CFG_SYSTEM, "rendererbackend", "dummy");
g_VideoMode.InitNonSDL();
g_VideoMode.CreateBackendDevice(false);
m_Viewer = new CProfileViewer;
m_Renderer = new CRenderer;
}
void tearDown()
{
delete m_Renderer;
delete m_Viewer;
g_VideoMode.Shutdown();
CConfigDB::Shutdown();
CXeromyces::Terminate();
g_VFS.reset();
DeleteDirectory(DataDir() / "_testcache");
}
void test_empty()
{
CGUI gui(g_ScriptContext);
CGUIText empty;
}
void test_wrapping()
{
CGUI gui(g_ScriptContext);
const CStrW font = L"console";
// Make sure this matches the value of the file.
// TODO: load dynamically.
const float lineHeight = 12.f;
const float lineSpacing = 15.f;
CGUIString string;
CGUIText text;
float width = 0.f;
float renderedWidth = 0.f;
float padding = 0.f;
EAlign align = EAlign::LEFT;
// Thing to note: the space before the newline should collapse in right-alignment.
string.SetValue(L"Some long text that will wrap-around. \n New line.");
text = CGUIText(gui, string, font, width, padding, align, nullptr);
// Width 0 means no wrapping, so we should be getting one render call & one line.
// TODO: is it wanted that \n doesn't wrap in that case?
// We have 11 calls: the 9 words (wrap-around is split in two), the space after the newline, and the newline itself.
TS_ASSERT_EQUALS(text.GetTextCalls().size(), 11);
TS_ASSERT_EQUALS(text.GetSize().Height, lineHeight);
width = 100.f;
padding = 2.0f;
align = EAlign::LEFT;
text = CGUIText(gui, string, font, width, padding, align, nullptr);
renderedWidth = text.GetSize().Width;
// We have 10 calls: the 9 words (wrap-around is split in two), the space after the newline.
TS_ASSERT_EQUALS(text.GetTextCalls().size(), 10);
TS_ASSERT_LESS_THAN(text.GetSize().Width, width);
TS_ASSERT_EQUALS(text.GetSize().Height, padding * 2 + lineHeight + lineSpacing * 4);
align = EAlign::RIGHT;
text = CGUIText(gui, string, font, width, padding, align, nullptr);
TS_ASSERT_EQUALS(text.GetTextCalls().size(), 10);
TS_ASSERT_EQUALS(text.GetSize().Width, renderedWidth); // Should be the same width as the left-case.
TS_ASSERT_EQUALS(text.GetSize().Height, padding * 2 + lineHeight + lineSpacing * 4);
width = 400.f;
padding = 3.0f;
align = EAlign::LEFT;
text = CGUIText(gui, string, font, width, padding, align, nullptr);
TS_ASSERT_EQUALS(text.GetTextCalls().size(), 10);
TS_ASSERT_LESS_THAN(text.GetSize().Width, width);
TS_ASSERT_EQUALS(text.GetSize().Height, padding * 2 + lineHeight + lineSpacing);
width = 400.f;
padding = 5.0f;
align = EAlign::CENTER;
text = CGUIText(gui, string, font, width, padding, align, nullptr);
renderedWidth = text.GetSize().Width;
TS_ASSERT_LESS_THAN(text.GetSize().Width, width);
TS_ASSERT_EQUALS(text.GetSize().Height, padding * 2 + lineHeight + lineSpacing);
align = EAlign::RIGHT;
text = CGUIText(gui, string, font, width, padding, align, nullptr);
TS_ASSERT_EQUALS(text.GetSize().Width, renderedWidth); // Should be the same width as the center-case.
TS_ASSERT_EQUALS(text.GetSize().Height, padding * 2 + lineHeight + lineSpacing);
align = EAlign::LEFT;
text = CGUIText(gui, string, font, width, padding, align, nullptr);
TS_ASSERT_EQUALS(text.GetSize().Width, renderedWidth); // Should be the same width as the center-case.
TS_ASSERT_EQUALS(text.GetSize().Height, padding * 2 + lineHeight + lineSpacing);
width = 400.f;
padding = 100.0f;
align = EAlign::LEFT;
text = CGUIText(gui, string, font, width, padding, align, nullptr);
renderedWidth = text.GetSize().Width;
TS_ASSERT_LESS_THAN(text.GetSize().Width, width);
TS_ASSERT_EQUALS(text.GetSize().Height, padding * 2 + lineHeight + lineSpacing * 2);
align = EAlign::RIGHT;
text = CGUIText(gui, string, font, width, padding, align, nullptr);
TS_ASSERT_EQUALS(text.GetSize().Width, renderedWidth); // Should be the same width as the left-case.
TS_ASSERT_EQUALS(text.GetSize().Height, padding * 2 + lineHeight + lineSpacing * 2);
}
void test_layout_wrapping_center()
{
// The short word should be layouted the same as when there is no
// second word, because it's the only one word in the first line until
// the width is enough to fit both. So we need to check that for
// increasing width X positions of both words are also increasing until
// they fit into a single line.
//
// Width is too small for both:
// +--------------------+
// | Shortword |
// | (Veryverylongword) |
// +--------------------+
//
// Right before the big enough width:
// +---------------------------+
// | Shortword |
// | (Veryverylongword) |
// +---------------------------+
//
// Width is enough to fit both:
// +------------------------------+
// | Shortword (Veryverylongword) |
// +------------------------------+
//
CGUI gui(g_ScriptContext);
const CStrW firstWord = L"Shortword";
const CStrW secondWord = L"(Veryverylongword)";
const CStrW text = firstWord + L" " + secondWord;
CGUIString string;
string.SetValue(text);
const CStrW font = L"console";
CFontMetrics fontMetrics{CStrIntern(font.ToUTF8())};
int firstWordWidth = 0, firstWordHeight = 0;
fontMetrics.CalculateStringSize(firstWord.c_str(), firstWordWidth, firstWordHeight);
TS_ASSERT(firstWordWidth > 0);
int secondWordWidth = 0, secondWordHeight = 0;
fontMetrics.CalculateStringSize(secondWord.c_str(), secondWordWidth, secondWordHeight);
TS_ASSERT(secondWordWidth > 0);
TS_ASSERT(firstWordWidth < secondWordWidth);
const float spaceWidth = fontMetrics.GetCharacterWidth(L' ');
const float lineHeight = fontMetrics.GetHeight();
const float lineSpacing = fontMetrics.GetLineSpacing();
auto layoutText = [&gui, &string, &font, lineHeight, firstWordWidth](const float width)
{
const float padding = 0.0f;
const CGUIText text(gui, string, font, width, padding, EAlign::CENTER, nullptr);
std::array<CVector2D, 2> positions;
TS_ASSERT_EQUALS(text.GetTextCalls().size(), positions.size());
// If the second word is on the next line then the first word should be
// centered alone.
if (text.GetTextCalls()[0].m_Pos.Y + lineHeight <= text.GetTextCalls()[1].m_Pos.Y)
{
const float expectedX = (width - firstWordWidth) / 2.0f;
TS_ASSERT_EQUALS(text.GetTextCalls()[0].m_Pos.X, expectedX);
}
std::transform(
text.GetTextCalls().begin(), text.GetTextCalls().end(), positions.begin(),
[](const CGUIText::STextCall& textCall) { return textCall.m_Pos; });
return positions;
};
const float testPadding = 2;
const float beginWidth = firstWordWidth - testPadding;
const float endWidth = firstWordWidth + spaceWidth + secondWordWidth + testPadding;
std::array<CVector2D, 2> previousPositions = layoutText(beginWidth);
TS_ASSERT_EQUALS(std::get<0>(previousPositions).Y, lineHeight);
TS_ASSERT_EQUALS(std::get<1>(previousPositions).Y, lineHeight + lineSpacing);
bool firstFit = false;
for (float width = beginWidth; width <= endWidth; width += 1.0f)
{
std::array<CVector2D, 2> positions = layoutText(width);
TS_ASSERT_EQUALS(std::get<0>(positions).Y, lineHeight);
if (std::get<0>(positions).X >= std::get<0>(previousPositions).X)
{
if (firstFit)
{
TS_ASSERT_EQUALS(std::get<0>(positions).Y, std::get<1>(positions).Y);
}
else
{
TS_ASSERT_EQUALS(std::get<1>(positions).Y, lineHeight + lineSpacing);
}
TS_ASSERT(std::get<1>(positions).X >= std::get<1>(previousPositions).X);
}
else
{
TS_ASSERT(!firstFit);
firstFit = true;
TS_ASSERT_EQUALS(std::get<0>(positions).Y, std::get<1>(positions).Y);
TS_ASSERT(std::get<0>(positions).X < std::get<1>(positions).X);
}
previousPositions = positions;
}
TS_ASSERT(firstFit);
}
void test_overflow()
{
CGUI gui(g_ScriptContext);
const CStrW font = L"console";
// Make sure this matches the value of the file.
// TODO: load dynamically.
const float lineHeight = 12.f;
const float lineSpacing = 15.f;
float renderedWidth = 0.f;
const float width = 200.f;
const float padding = 20.f;
CGUIString string;
CGUIText text;
string.SetValue(L"wordthatisverylonganddefinitelywontfitinaline and other words");
text = CGUIText(gui, string, font, width, padding, EAlign::LEFT, nullptr);
renderedWidth = text.GetSize().Width;
TS_ASSERT_EQUALS(text.GetSize().Height, lineHeight + lineSpacing * 1 + padding * 2);
text = CGUIText(gui, string, font, width, padding, EAlign::CENTER, nullptr);
TS_ASSERT_EQUALS(renderedWidth, text.GetSize().Width);
TS_ASSERT_EQUALS(text.GetSize().Height, lineHeight + lineSpacing * 1 + padding * 2);
text = CGUIText(gui, string, font, width, padding, EAlign::RIGHT, nullptr);
TS_ASSERT_EQUALS(renderedWidth, text.GetSize().Width);
TS_ASSERT_EQUALS(text.GetSize().Height, lineHeight + lineSpacing * 1 + padding * 2);
string.SetValue(L"other words and wordthatisverylonganddefinitelywontfitinaline");
text = CGUIText(gui, string, font, width, padding, EAlign::LEFT, nullptr);
renderedWidth = text.GetSize().Width;
TS_ASSERT_EQUALS(text.GetSize().Height, lineHeight + lineSpacing * 1 + padding * 2);
text = CGUIText(gui, string, font, width, padding, EAlign::CENTER, nullptr);
TS_ASSERT_EQUALS(renderedWidth, text.GetSize().Width);
TS_ASSERT_EQUALS(text.GetSize().Height, lineHeight + lineSpacing * 1 + padding * 2);
text = CGUIText(gui, string, font, width, padding, EAlign::RIGHT, nullptr);
TS_ASSERT_EQUALS(renderedWidth, text.GetSize().Width);
TS_ASSERT_EQUALS(text.GetSize().Height, lineHeight + lineSpacing * 1 + padding * 2);
string.SetValue(L"wordthatisverylonganddefinitelywontfitinaline");
text = CGUIText(gui, string, font, width, padding, EAlign::LEFT, nullptr);
renderedWidth = text.GetSize().Width;
TS_ASSERT_EQUALS(text.GetSize().Height, lineHeight + padding * 2);
text = CGUIText(gui, string, font, width, padding, EAlign::CENTER, nullptr);
TS_ASSERT_EQUALS(renderedWidth, text.GetSize().Width);
TS_ASSERT_EQUALS(text.GetSize().Height, lineHeight + padding * 2);
text = CGUIText(gui, string, font, width, padding, EAlign::RIGHT, nullptr);
TS_ASSERT_EQUALS(renderedWidth, text.GetSize().Width);
TS_ASSERT_EQUALS(text.GetSize().Height, lineHeight + padding * 2);
}
void test_multiple_blank_spaces()
{
CGUI gui(g_ScriptContext);
const CStrW font = L"console";
// Make sure this matches the value of the file.
// TODO: load dynamically.
const float lineHeight = 12.f;
const float lineSpacing = 15.f;
CGUIString string;
CGUIText text;
float width = 100.f;
float renderedWidth = 0.f;
float padding = 0.f;
EAlign align = EAlign::LEFT;
string.SetValue(L" word another \n spaces \n \n word ");
text = CGUIText(gui, string, font, width, padding, align, nullptr);
// Blank spaces are treated as a word.
TS_ASSERT_EQUALS(text.GetTextCalls().size(), 26);
TS_ASSERT_EQUALS(text.GetSize().Height, lineHeight + lineSpacing * 4);
TS_ASSERT_EQUALS(text.GetSize().Width, 89.f);
renderedWidth = text.GetSize().Width;
align = EAlign::RIGHT;
text = CGUIText(gui, string, font, width, padding, align, nullptr);
TS_ASSERT_EQUALS(text.GetSize().Width, renderedWidth);
}
};
|