Modules/GraphicApplication/tests/FontCoverageTests.cpp
agent 92de3007e9 GraphicApplication: NanoVG canvas, window decorations and ImGui debug UI
canvas/ is a generic drawing interface on one NanoVG Vulkan context per
window surface. NanoVG reads the recording command buffer from the
GpuFrame handed to drawBegin(). It offers only generic primitives: shapes,
gradients, paths with holes, composite operations, typed text styles
(TextStyle, TextAlign, FontFace) and typed ImageHandles for files, pixels
and registered render targets. Origin and clipping are scoped: SavedState,
Translation and Placement restore them, and every primitive honours the
origin.

Text uses bundled faces (regular, bold, mono, math) with symbol,
special-character and emoji fallbacks, and a Cyrillic cut of the matching
weight last in each face's chain; tests check every bundled face loads and
covers the characters found in UI sources and the Russian alphabet.
rsc/FONTS.md records where each font comes from.

decorations/ owns client-side decorations as one concept: WindowDecorations
paints the base, rounded corners, outline and shadow of the same
WindowFrame the backend uses for edge hit-testing.

debug_ui/ owns the process ImGui and ImPlot contexts. DebugGUI renders
through the ImGui Vulkan backend with its own platform layer fed from
InputState, or runs headless so code that calls ImGui directly still works
when the interface is hidden.
2026-10-29 05:24:06 +03:00

196 lines
6.3 KiB
C++

#include "CanvasFonts.hpp"
#include "stb_truetype.h"
#include "UnitTest++/UnitTest++.h"
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <optional>
#include <regex>
#include <set>
#include <string>
#include <vector>
namespace {
const std::filesystem::path kSourceRoot = MODULES_SOURCE_DIR;
const std::filesystem::path kResourceRoot = kSourceRoot / "GraphicApplication" / "rsc";
const std::vector<std::string> kUiSourceDirectories = {
"Widgets/private", "Widgets/public", "Taskboard/private", "Taskboard/public"
};
class LoadedFont {
public:
explicit LoadedFont(const tp::FontFile& file) {
std::ifstream stream(kResourceRoot / file.path, std::ios::binary);
mBytes.assign(std::istreambuf_iterator<char>(stream), {});
const auto* data = reinterpret_cast<const unsigned char*>(mBytes.data());
mValid = !mBytes.empty() && stbtt_InitFont(&mInfo, data, stbtt_GetFontOffsetForIndex(data, 0));
}
[[nodiscard]] bool isValid() const { return mValid; }
[[nodiscard]] bool hasGlyph(char32_t codepoint) const {
return mValid && stbtt_FindGlyphIndex(&mInfo, int(codepoint)) != 0;
}
private:
std::vector<char> mBytes;
stbtt_fontinfo mInfo{};
bool mValid = false;
};
std::optional<char32_t> decodeUtf8(const std::string& text, size_t& position) {
const auto byte = [&](size_t offset) { return static_cast<unsigned char>(text[position + offset]); };
const unsigned char lead = byte(0);
const size_t length = lead < 0x80 ? 1 : lead >= 0xF0 ? 4 : lead >= 0xE0 ? 3 : lead >= 0xC0 ? 2 : 0;
if (length == 0 || position + length > text.size()) {
position++;
return std::nullopt;
}
char32_t codepoint = length == 1 ? lead : lead & (0xFF >> (length + 1));
for (size_t index = 1; index < length; index++) {
codepoint = (codepoint << 6) | (byte(index) & 0x3F);
}
position += length;
return codepoint;
}
void collectEscapes(const std::string& source, std::set<char32_t>& codepoints) {
static const std::regex escape(R"(\\u([0-9A-Fa-f]{4})|\\U([0-9A-Fa-f]{8}))");
for (std::sregex_iterator match(source.begin(), source.end(), escape), end; match != end; ++match) {
const std::string digits = (*match)[1].matched ? (*match)[1].str() : (*match)[2].str();
codepoints.insert(char32_t(std::stoul(digits, nullptr, 16)));
}
}
void collectLiterals(const std::string& source, std::set<char32_t>& codepoints) {
for (size_t position = 0; position < source.size();) {
const std::optional<char32_t> codepoint = decodeUtf8(source, position);
if (codepoint && *codepoint >= 0x80) {
codepoints.insert(*codepoint);
}
}
}
std::set<char32_t> codepointsUsedByUi() {
std::set<char32_t> codepoints;
for (const std::string& directory : kUiSourceDirectories) {
if (!std::filesystem::is_directory(kSourceRoot / directory)) {
continue;
}
for (const auto& entry : std::filesystem::recursive_directory_iterator(kSourceRoot / directory)) {
if (!entry.is_regular_file()) {
continue;
}
std::ifstream stream(entry.path(), std::ios::binary);
const std::string source{ std::istreambuf_iterator<char>(stream), {} };
collectEscapes(source, codepoints);
collectLiterals(source, codepoints);
}
}
return codepoints;
}
std::string hex(char32_t codepoint) {
char buffer[16];
std::snprintf(buffer, sizeof(buffer), "U+%04X", unsigned(codepoint));
return buffer;
}
struct FaceChain {
const char* label;
tp::FontFile face;
std::vector<tp::FontFile> fallbacks;
};
std::vector<FaceChain> faceChains() {
return {
{ "regular", tp::kRegularFont, { tp::kRegularFallbacks.begin(), tp::kRegularFallbacks.end() } },
{ "bold", tp::kBoldFont, { tp::kBoldFallbacks.begin(), tp::kBoldFallbacks.end() } },
{ "monospace", tp::kMonospaceFont, { tp::kMonospaceFallbacks.begin(), tp::kMonospaceFallbacks.end() } },
};
}
const char* const kRussianAlphabetAndPunctuation =
"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя№«»—–…";
std::set<char32_t> codepointsOf(const char* text) {
std::set<char32_t> codepoints;
const std::string source{ text };
for (size_t position = 0; position < source.size();) {
const std::optional<char32_t> codepoint = decodeUtf8(source, position);
if (codepoint) {
codepoints.insert(*codepoint);
}
}
return codepoints;
}
void checkChainCovers(const FaceChain& chain, const std::set<char32_t>& codepoints) {
const LoadedFont primary(chain.face);
std::vector<LoadedFont> fallbacks;
for (const tp::FontFile& file : chain.fallbacks) {
fallbacks.emplace_back(file);
}
for (const char32_t codepoint : codepoints) {
bool covered = primary.hasGlyph(codepoint);
for (const LoadedFont& fallback : fallbacks) {
covered = covered || fallback.hasGlyph(codepoint);
}
CHECK_EQUAL(
std::string(chain.label) + " " + hex(codepoint) + " drawn",
std::string(chain.label) + " " + hex(codepoint) + (covered ? " drawn" : " has no glyph")
);
}
}
template <typename Chain>
bool chainContains(const Chain& chain, const tp::FontFile& font) {
for (const tp::FontFile& file : chain) {
if (std::strcmp(file.path, font.path) == 0) {
return true;
}
}
return false;
}
}
SUITE(FontCoverage) {
TEST(EveryBundledFontLoads) {
for (const tp::FontFile& file : tp::kTextFonts) {
CHECK(LoadedFont(file).isValid());
}
for (const tp::FontFile& file : tp::kFallbackFonts) {
CHECK(LoadedFont(file).isValid());
}
}
TEST(EveryCharacterTheUiDrawsHasAGlyphInEveryTextFace) {
const std::set<char32_t> used = codepointsUsedByUi();
for (const FaceChain& chain : faceChains()) {
checkChainCovers(chain, used);
}
}
TEST(RussianTextHasAGlyphInEveryTextFace) {
const std::set<char32_t> russian = codepointsOf(kRussianAlphabetAndPunctuation);
CHECK_EQUAL(size_t(72), russian.size());
for (const FaceChain& chain : faceChains()) {
checkChainCovers(chain, russian);
}
}
TEST(EachFaceFallsBackToTheCyrillicCutOfItsOwnWeight) {
CHECK(chainContains(tp::kRegularFallbacks, tp::kCyrillicRegularFont));
CHECK(chainContains(tp::kMonospaceFallbacks, tp::kCyrillicRegularFont));
CHECK(chainContains(tp::kBoldFallbacks, tp::kCyrillicBoldFont));
CHECK(!chainContains(tp::kBoldFallbacks, tp::kCyrillicRegularFont));
CHECK(!chainContains(tp::kRegularFallbacks, tp::kCyrillicBoldFont));
}
}