Files
AnyPS5/core/libs/tests/FontFixture.hpp
T

51 lines
2.1 KiB
C++

#ifndef CORE_LIBS_TESTS_FONTFIXTURE_HPP
#define CORE_LIBS_TESTS_FONTFIXTURE_HPP
#include <cstddef>
#include <initializer_list>
#include <map>
#include <string>
#include <vector>
namespace FontFixture {
inline void Put16(std::vector<unsigned char>& out, int value) {
out.push_back(static_cast<unsigned char>((value >> 8) & 0xFF));
out.push_back(static_cast<unsigned char>(value & 0xFF));
}
inline std::vector<unsigned char> Words(std::initializer_list<int> values) {
std::vector<unsigned char> out;
for (const int value : values) Put16(out, value);
return out;
}
inline std::vector<unsigned char> BuildFont(int glyphCount, std::map<std::string, std::vector<unsigned char>> tables) {
tables["glyf"] = Words({0, 0});
tables["head"] = Words({1, 0, 1, 0, 0, 0, 0x5F0F, 0x3CF5, 0, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 2, 0, 0});
tables["hhea"] = Words({1, 0, 800, -200, 0, 500, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1});
tables["hmtx"] = Words({500});
tables["hmtx"].resize(2 + 2 * static_cast<std::size_t>(glyphCount));
tables["loca"].assign(2 * (static_cast<std::size_t>(glyphCount) + 1), 0);
tables["maxp"] = Words({1, 0, glyphCount, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0});
const int tableCount = static_cast<int>(tables.size());
int entrySelector = 0;
while ((2 << entrySelector) <= tableCount) ++entrySelector;
std::vector<unsigned char> font = Words({1, 0, tableCount, 16 << entrySelector, entrySelector, 16 * tableCount - (16 << entrySelector)});
int offset = 12 + 16 * tableCount;
for (const auto& table : tables) {
font.insert(font.end(), table.first.begin(), table.first.end());
for (const int value : {0, 0, offset >> 16, offset & 0xFFFF, 0, static_cast<int>(table.second.size())}) Put16(font, value);
offset += static_cast<int>((table.second.size() + 3) & ~std::size_t{3});
}
for (const auto& table : tables) {
font.insert(font.end(), table.second.begin(), table.second.end());
font.resize((font.size() + 3) & ~std::size_t{3});
}
return font;
}
}
#endif