psemek/libs/ui/source/default_fonts.cpp

43 lines
1.2 KiB
C++

#include <psemek/ui/font.hpp>
#include <psemek/ui/monospace_font.hpp>
#include <psemek/gfx/resource/font_9x12_png.hpp>
#include <psemek/io/memory_stream.hpp>
namespace psemek::ui
{
std::unique_ptr<font> make_default_9x12_font()
{
character_range range{32, 128};
std::string_view name = "default_9x12";
geom::vector<int, 2> size{9, 12};
gfx::texture_2d atlas = gfx::texture_2d::from_pixmap(gfx::read_png_monochrome(io::memory_istream{gfx::resource::font_9x12_png}));
atlas.nearest_filter();
atlas.clamp();
gl::TexParameteri(atlas.target, gl::TEXTURE_SWIZZLE_G, gl::RED);
gl::TexParameteri(atlas.target, gl::TEXTURE_SWIZZLE_B, gl::RED);
gl::TexParameteri(atlas.target, gl::TEXTURE_SWIZZLE_A, gl::RED);
std::vector<geom::box<float, 2>> texcoords(range.end - range.begin);
for (char32_t c = range.begin; c < range.end; ++c)
{
int const row = 16;
int x = (c - range.begin) % row;
int y = (c - range.begin) / row;
geom::box<float, 2> b;
b[0].min = x * 11 + 1;
b[0].max = b[0].min + 9;
b[1].min = y * 14 + 1;
b[1].max = b[1].min + 12;
texcoords[c - range.begin] = b;
}
return std::make_unique<monospace_font>(range, name, size, std::move(atlas), std::move(texcoords));
}
}