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

151 lines
4.1 KiB

  1. /*
  2. * Copyright (C) 2021 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. #ifndef ANTKEEPER_TYPE_BITMAP_FONT_HPP
  20. #define ANTKEEPER_TYPE_BITMAP_FONT_HPP
  21. #include "type/font.hpp"
  22. #include "type/bitmap-glyph.hpp"
  23. #include "resources/image.hpp"
  24. #include <unordered_map>
  25. namespace type {
  26. /**
  27. * Raster font in which glyphs are stored as arrays of pixels.
  28. *
  29. * @see type::font
  30. * @see type::font_metrics
  31. * @see type::bitmap_glyph
  32. * @see image
  33. */
  34. class bitmap_font: public font
  35. {
  36. public:
  37. /**
  38. * Creates a bitmap font and sets its metrics.
  39. *
  40. * @param metrics Metrics describing the font.
  41. */
  42. bitmap_font(const font_metrics& metrics);
  43. /// Creates an empty bitmap font.
  44. bitmap_font();
  45. /// Destroys a bitmap font.
  46. virtual ~bitmap_font() = default;
  47. /// @copydoc font::contains(char32_t) const
  48. virtual bool contains(char32_t code) const;
  49. /**
  50. * Inserts a glyph into the font.
  51. *
  52. * @param code UTF-32 character code of the glyph to insert.
  53. * @param glyph Bitmap glyph data.
  54. */
  55. void insert(char32_t code, const bitmap_glyph& glyph);
  56. /**
  57. * Removes a glyph from the font.
  58. *
  59. * @param code UTF-32 character code of the glyph to remove.
  60. */
  61. void remove(char32_t code);
  62. /**
  63. * Removes all glyphs from the font.
  64. */
  65. void clear();
  66. /**
  67. * Packs all glyph bitmaps into the font bitmap.
  68. *
  69. * @param resize Automatically resize the font bitmap to contain all glyphs. Bitmap size will start at the closest power of two to the largest glyph, then its dimensions will increase to the next power of two until its large enough that all glyphs can be contained.
  70. * @return `true` if all glyphs were successfully packed, `false` otherwise.
  71. *
  72. * @except std::runtime_error Glyph bitmap format doesn't match font bitmap format.
  73. * @except std::runtime_error Not enough space in font bitmap to pack glyph.
  74. */
  75. bool pack(bool resize = true);
  76. /**
  77. * Unpacks all glyph bitmaps from the font bitmap.
  78. *
  79. * @param resize Automatically resizes the font bitmap to zero.
  80. */
  81. void unpack(bool resize = true);
  82. /// Returns a reference to the bitmap containing glyph pixel data.
  83. const image& get_bitmap() const;
  84. /// @copydoc bitmap_font::get_bitmap() const
  85. image& get_bitmap();
  86. /**
  87. * @copydoc font::get_glyph_metrics(char32_t) const
  88. *
  89. * @except std::invalid_argument Cannot fetch metrics of unknown bitmap glyph
  90. */
  91. virtual const glyph_metrics& get_glyph_metrics(char32_t code) const;
  92. /**
  93. * Returns a reference to the glyph corresponding to a UTF-32 character code.
  94. *
  95. * @param code UTF-32 character code of a glyph.
  96. * @return Reference to the corresponding glyph.
  97. *
  98. * @except std::invalid_argument Cannot get unknown bitmap glyph
  99. */
  100. const bitmap_glyph& get_glyph(char32_t code) const;
  101. /// @copydoc bitmap_font::get_glyph(char32_t) const
  102. bitmap_glyph& get_glyph(char32_t code);
  103. /**
  104. * Returns a reference to the glyph corresponding to a UTF-32 character code, performing an insertion if such glyph does not already exist.
  105. *
  106. * @param code UTF-32 character code of a glyph.
  107. * @return Reference to the corresponding glyph.
  108. */
  109. bitmap_glyph& operator[](char32_t code);
  110. private:
  111. std::unordered_map<char32_t, bitmap_glyph> glyphs;
  112. image bitmap;
  113. };
  114. inline const image& bitmap_font::get_bitmap() const
  115. {
  116. return bitmap;
  117. }
  118. inline image& bitmap_font::get_bitmap()
  119. {
  120. return bitmap;
  121. }
  122. inline bitmap_glyph& bitmap_font::operator[](char32_t code)
  123. {
  124. return glyphs[code];
  125. }
  126. } // namespace type
  127. #endif // ANTKEEPER_TYPE_BITMAP_FONT_HPP