/* * Copyright (C) 2023 Christopher J. Howard * * This file is part of Antkeeper source code. * * Antkeeper source code 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 3 of the License, or * (at your option) any later version. * * Antkeeper source code 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 Antkeeper source code. If not, see . */ #include "game/fonts.hpp" #include #include #include #include #include #include "game/strings.hpp" #include static void build_bitmap_font(const type::typeface& typeface, float size, const std::unordered_set& charset, type::bitmap_font& font, render::material& material, std::shared_ptr shader_template) { // Get font metrics for given size if (type::font_metrics metrics; typeface.get_metrics(size, metrics)) { font.set_font_metrics(metrics); } // For each UTF-32 character code in the character set for (char32_t code: charset) { // Skip missing glyphs if (!typeface.get_charset().contains(code)) { continue; } // Add glyph to font type::bitmap_glyph& glyph = font.insert(code); typeface.get_metrics(size, code, glyph.metrics); typeface.get_bitmap(size, code, glyph.bitmap, glyph.bitmap_width, glyph.bitmap_height); } // Pack glyph bitmaps into the font bitmap font.pack(); // Create font material material.set_blend_mode(render::material_blend_mode::translucent); // Create font bitmap variable material.set_variable("font_bitmap", std::make_shared(1, font.get_texture())); material.set_shader_template(shader_template); } void load_fonts(::game& ctx) { // Load dyslexia-friendly typeface (if enabled) bool dyslexia_font_loaded = false; if (ctx.dyslexia_font) { const auto dyslexia_font_path = get_string(ctx, "font_dyslexia"); ctx.typefaces["dyslexia"] = ctx.resource_manager->load(dyslexia_font_path); dyslexia_font_loaded = true; } // Load typefaces if (dyslexia_font_loaded) { // Override standard typefaces with dyslexia-friendly typeface ctx.typefaces["serif"] = ctx.typefaces["dyslexia"]; ctx.typefaces["sans_serif"] = ctx.typefaces["dyslexia"]; ctx.typefaces["monospace"] = ctx.typefaces["dyslexia"]; } else { // Load standard typefaces const auto serif_font_path = get_string(ctx, "font_serif"); const auto sans_serif_font_path = get_string(ctx, "font_sans_serif"); const auto monospace_font_path = get_string(ctx, "font_monospace"); ctx.typefaces["serif"] = ctx.resource_manager->load(serif_font_path); ctx.typefaces["sans_serif"] = ctx.resource_manager->load(sans_serif_font_path); ctx.typefaces["monospace"] = ctx.resource_manager->load(monospace_font_path); } // Build character set /* std::unordered_set charset; { // Add all character codes from the basic Latin unicode block for (char32_t code = type::unicode::block::basic_latin.first; code <= type::unicode::block::basic_latin.last; ++code) charset.insert(code); // Add all character codes from game strings for (auto it = ctx.string_map->begin(); it != ctx.string_map->end(); ++it) { // Convert UTF-8 string to UTF-32 std::wstring_convert, char32_t> convert; std::u32string u32 = convert.from_bytes(it->second); /// Insert each character code from the UTF-32 string into the character set for (char32_t code: u32) charset.insert(code); } } */ // Load bitmap font shader std::shared_ptr font_shader_template = ctx.resource_manager->load("bitmap-font.glsl"); // Point size to pixel size conversion factor const float dpi = ctx.window_manager->get_display(0).get_dpi(); const float pt_to_px = (dpi / 72.0f) * ctx.font_scale; // Build debug font if (auto it = ctx.typefaces.find("monospace"); it != ctx.typefaces.end()) { build_bitmap_font(*it->second, ctx.debug_font_size_pt * pt_to_px, it->second->get_charset(), ctx.debug_font, *ctx.debug_font_material, font_shader_template); } // Build menu font if (auto it = ctx.typefaces.find("sans_serif"); it != ctx.typefaces.end()) { build_bitmap_font(*it->second, ctx.menu_font_size_pt * pt_to_px, it->second->get_charset(), ctx.menu_font, *ctx.menu_font_material, font_shader_template); } // Build title font if (auto it = ctx.typefaces.find("serif"); it != ctx.typefaces.end()) { build_bitmap_font(*it->second, ctx.title_font_size_pt * pt_to_px, it->second->get_charset(), ctx.title_font, *ctx.title_font_material, font_shader_template); } }