💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.5 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "game/fonts.hpp"
  20. #include "application.hpp"
  21. #include "type/type.hpp"
  22. #include "resources/resource-manager.hpp"
  23. #include "gl/texture-wrapping.hpp"
  24. #include "gl/texture-filter.hpp"
  25. #include "render/material.hpp"
  26. #include "render/material-flags.hpp"
  27. #include "utility/hash/fnv1a.hpp"
  28. #include "game/strings.hpp"
  29. #include <codecvt>
  30. using namespace hash::literals;
  31. namespace game {
  32. static void build_bitmap_font(const type::typeface& typeface, float size, const std::unordered_set<char32_t>& charset, type::bitmap_font& font, render::material& material, gl::shader_program* shader_program)
  33. {
  34. // Get font metrics for given size
  35. if (type::font_metrics metrics; typeface.get_metrics(size, metrics))
  36. font.set_font_metrics(metrics);
  37. // Format font bitmap
  38. image& font_bitmap = font.get_bitmap();
  39. font_bitmap.format(sizeof(std::byte), 1);
  40. // For each UTF-32 character code in the character set
  41. for (char32_t code: charset)
  42. {
  43. // Skip missing glyphs
  44. if (!typeface.has_glyph(code))
  45. continue;
  46. // Add glyph to font
  47. type::bitmap_glyph& glyph = font[code];
  48. typeface.get_metrics(size, code, glyph.metrics);
  49. typeface.get_bitmap(size, code, glyph.bitmap);
  50. }
  51. // Pack glyph bitmaps into the font bitmap
  52. font.pack();
  53. // Create font texture from bitmap
  54. gl::texture_2d* font_texture = new gl::texture_2d(font_bitmap.get_width(), font_bitmap.get_height(), gl::pixel_type::uint_8, gl::pixel_format::r, gl::color_space::linear, font_bitmap.data());
  55. font_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  56. font_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  57. // Create font material
  58. material.set_blend_mode(render::blend_mode::translucent);
  59. material.add_property<const gl::texture_2d*>("font_bitmap")->set_value(font_texture);
  60. material.set_shader_program(shader_program);
  61. }
  62. void load_fonts(game::context& ctx)
  63. {
  64. // Load dyslexia-friendly typeface (if enabled)
  65. bool dyslexia_font_loaded = false;
  66. if (ctx.dyslexia_font)
  67. {
  68. const auto dyslexia_font_path = get_string(ctx, "font_dyslexia"_fnv1a32);
  69. ctx.typefaces["dyslexia"] = ctx.resource_manager->load<type::typeface>(dyslexia_font_path);
  70. dyslexia_font_loaded = true;
  71. }
  72. // Load typefaces
  73. if (dyslexia_font_loaded)
  74. {
  75. // Override standard typefaces with dyslexia-friendly typeface
  76. ctx.typefaces["serif"] = ctx.typefaces["dyslexia"];
  77. ctx.typefaces["sans_serif"] = ctx.typefaces["dyslexia"];
  78. ctx.typefaces["monospace"] = ctx.typefaces["dyslexia"];
  79. }
  80. else
  81. {
  82. // Load standard typefaces
  83. const auto serif_font_path = get_string(ctx, "font_serif"_fnv1a32);
  84. const auto sans_serif_font_path = get_string(ctx, "font_sans_serif"_fnv1a32);
  85. const auto monospace_font_path = get_string(ctx, "font_monospace"_fnv1a32);
  86. ctx.typefaces["serif"] = ctx.resource_manager->load<type::typeface>(serif_font_path);
  87. ctx.typefaces["sans_serif"] = ctx.resource_manager->load<type::typeface>(sans_serif_font_path);
  88. ctx.typefaces["monospace"] = ctx.resource_manager->load<type::typeface>(monospace_font_path);
  89. }
  90. // Build character set
  91. std::unordered_set<char32_t> charset;
  92. {
  93. // Add all character codes from the basic Latin unicode block
  94. for (char32_t code = type::unicode::block::basic_latin.first; code <= type::unicode::block::basic_latin.last; ++code)
  95. charset.insert(code);
  96. // Add all character codes from game strings
  97. const auto& string_map = ctx.string_maps[ctx.language_index];
  98. for (auto it = string_map.begin(); it != string_map.end(); ++it)
  99. {
  100. // Convert UTF-8 string to UTF-32
  101. std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert;
  102. std::u32string u32 = convert.from_bytes(it->second);
  103. /// Insert each character code from the UTF-32 string into the character set
  104. for (char32_t code: u32)
  105. charset.insert(code);
  106. }
  107. }
  108. // Load bitmap font shader
  109. gl::shader_program* bitmap_font_shader = ctx.resource_manager->load<gl::shader_program>("bitmap-font.glsl");
  110. // Point size to pixel size conversion factor
  111. const float pt_to_px = (ctx.app->get_display_dpi() / 72.0f) * ctx.font_scale;
  112. // Build debug font
  113. if (auto it = ctx.typefaces.find("monospace"); it != ctx.typefaces.end())
  114. {
  115. build_bitmap_font(*it->second, ctx.debug_font_size_pt * pt_to_px, charset, ctx.debug_font, ctx.debug_font_material, bitmap_font_shader);
  116. }
  117. // Build menu font
  118. if (auto it = ctx.typefaces.find("sans_serif"); it != ctx.typefaces.end())
  119. {
  120. build_bitmap_font(*it->second, ctx.menu_font_size_pt * pt_to_px, charset, ctx.menu_font, ctx.menu_font_material, bitmap_font_shader);
  121. }
  122. // Build title font
  123. if (auto it = ctx.typefaces.find("serif"); it != ctx.typefaces.end())
  124. {
  125. build_bitmap_font(*it->second, ctx.title_font_size_pt * pt_to_px, charset, ctx.title_font, ctx.title_font_material, bitmap_font_shader);
  126. }
  127. }
  128. } // namespace game