💿🐜 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.

142 lines
5.1 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 <engine/type/type.hpp>
  21. #include <engine/resources/resource-manager.hpp>
  22. #include <engine/render/material.hpp>
  23. #include <engine/render/material-flags.hpp>
  24. #include <engine/utility/hash/fnv1a.hpp>
  25. #include "game/strings.hpp"
  26. #include <codecvt>
  27. 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, std::shared_ptr<gl::shader_template> shader_template)
  28. {
  29. // Get font metrics for given size
  30. if (type::font_metrics metrics; typeface.get_metrics(size, metrics))
  31. {
  32. font.set_font_metrics(metrics);
  33. }
  34. // For each UTF-32 character code in the character set
  35. for (char32_t code: charset)
  36. {
  37. // Skip missing glyphs
  38. if (!typeface.get_charset().contains(code))
  39. {
  40. continue;
  41. }
  42. // Add glyph to font
  43. type::bitmap_glyph& glyph = font.insert(code);
  44. typeface.get_metrics(size, code, glyph.metrics);
  45. typeface.get_bitmap(size, code, glyph.bitmap, glyph.bitmap_width, glyph.bitmap_height);
  46. }
  47. // Pack glyph bitmaps into the font bitmap
  48. font.pack();
  49. // Create font material
  50. material.set_blend_mode(render::material_blend_mode::translucent);
  51. // Create font bitmap variable
  52. material.set_variable("font_bitmap", std::make_shared<render::matvar_texture_2d>(1, font.get_texture()));
  53. material.set_shader_template(shader_template);
  54. }
  55. void load_fonts(::game& ctx)
  56. {
  57. // Load dyslexia-friendly typeface (if enabled)
  58. bool dyslexia_font_loaded = false;
  59. if (ctx.dyslexia_font)
  60. {
  61. const auto dyslexia_font_path = get_string(ctx, "font_dyslexia");
  62. ctx.typefaces["dyslexia"] = ctx.resource_manager->load<type::typeface>(dyslexia_font_path);
  63. dyslexia_font_loaded = true;
  64. }
  65. // Load typefaces
  66. if (dyslexia_font_loaded)
  67. {
  68. // Override standard typefaces with dyslexia-friendly typeface
  69. ctx.typefaces["serif"] = ctx.typefaces["dyslexia"];
  70. ctx.typefaces["sans_serif"] = ctx.typefaces["dyslexia"];
  71. ctx.typefaces["monospace"] = ctx.typefaces["dyslexia"];
  72. }
  73. else
  74. {
  75. // Load standard typefaces
  76. const auto serif_font_path = get_string(ctx, "font_serif");
  77. const auto sans_serif_font_path = get_string(ctx, "font_sans_serif");
  78. const auto monospace_font_path = get_string(ctx, "font_monospace");
  79. ctx.typefaces["serif"] = ctx.resource_manager->load<type::typeface>(serif_font_path);
  80. ctx.typefaces["sans_serif"] = ctx.resource_manager->load<type::typeface>(sans_serif_font_path);
  81. ctx.typefaces["monospace"] = ctx.resource_manager->load<type::typeface>(monospace_font_path);
  82. }
  83. // Build character set
  84. /*
  85. std::unordered_set<char32_t> charset;
  86. {
  87. // Add all character codes from the basic Latin unicode block
  88. for (char32_t code = type::unicode::block::basic_latin.first; code <= type::unicode::block::basic_latin.last; ++code)
  89. charset.insert(code);
  90. // Add all character codes from game strings
  91. for (auto it = ctx.string_map->begin(); it != ctx.string_map->end(); ++it)
  92. {
  93. // Convert UTF-8 string to UTF-32
  94. std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert;
  95. std::u32string u32 = convert.from_bytes(it->second);
  96. /// Insert each character code from the UTF-32 string into the character set
  97. for (char32_t code: u32)
  98. charset.insert(code);
  99. }
  100. }
  101. */
  102. // Load bitmap font shader
  103. std::shared_ptr<gl::shader_template> font_shader_template = ctx.resource_manager->load<gl::shader_template>("bitmap-font.glsl");
  104. // Point size to pixel size conversion factor
  105. const float dpi = ctx.window_manager->get_display(0).get_dpi();
  106. const float pt_to_px = (dpi / 72.0f) * ctx.font_scale;
  107. // Build debug font
  108. if (auto it = ctx.typefaces.find("monospace"); it != ctx.typefaces.end())
  109. {
  110. 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);
  111. }
  112. // Build menu font
  113. if (auto it = ctx.typefaces.find("sans_serif"); it != ctx.typefaces.end())
  114. {
  115. 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);
  116. }
  117. // Build title font
  118. if (auto it = ctx.typefaces.find("serif"); it != ctx.typefaces.end())
  119. {
  120. 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);
  121. }
  122. }