Antkeeper  0.0.1
bitmap-font.hpp
Go to the documentation of this file.
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 
20 #ifndef ANTKEEPER_TYPE_BITMAP_FONT_HPP
21 #define ANTKEEPER_TYPE_BITMAP_FONT_HPP
22 
23 #include <engine/type/font.hpp>
25 #include <engine/gl/texture.hpp>
26 #include <unordered_map>
27 
28 namespace type {
29 
38 class bitmap_font: public font
39 {
40 public:
46  explicit bitmap_font(const font_metrics& metrics);
47 
49  bitmap_font();
50 
52  virtual ~bitmap_font() = default;
53 
55  virtual bool contains(char32_t code) const;
56 
64  bitmap_glyph& insert(char32_t code);
65 
71  void remove(char32_t code);
72 
76  void clear();
77 
87  bool pack(bool resize = true);
88 
94  void unpack(bool resize = true);
95 
101  virtual const glyph_metrics& get_glyph_metrics(char32_t code) const;
102 
111  [[nodiscard]] const bitmap_glyph* get_glyph(char32_t code) const;
112  [[nodiscard]] bitmap_glyph* get_glyph(char32_t code);
114 
116 
118  [[nodiscard]] inline const std::shared_ptr<gl::texture_2d>& get_texture() const noexcept
119  {
120  return m_texture;
121  }
122 
123 private:
124  std::unordered_map<char32_t, bitmap_glyph> m_glyphs;
125  std::shared_ptr<gl::texture_2d> m_texture;
126  std::shared_ptr<gl::sampler> m_sampler;
127 };
128 
129 } // namespace type
130 
131 #endif // ANTKEEPER_TYPE_BITMAP_FONT_HPP
Raster font in which glyphs are stored as arrays of pixels.
Definition: bitmap-font.hpp:39
bitmap_font()
Creates an empty bitmap font.
Definition: bitmap-font.cpp:30
virtual const glyph_metrics & get_glyph_metrics(char32_t code) const
Returns metrics describing a glyph.
void unpack(bool resize=true)
Unpacks all glyph bitmaps from the font bitmap.
virtual bool contains(char32_t code) const
Returns true if the font contains a glyph with the given character code.
Definition: bitmap-font.cpp:34
bool pack(bool resize=true)
Packs all glyph bitmaps into the font bitmap.
Definition: bitmap-font.cpp:57
void remove(char32_t code)
Removes a glyph from the font.
Definition: bitmap-font.cpp:44
virtual ~bitmap_font()=default
Destroys a bitmap font.
const std::shared_ptr< gl::texture_2d > & get_texture() const noexcept
Returns a pointer to the glyph corresponding to a UTF-32 character code, or nullptr if no such glyph ...
bitmap_glyph & insert(char32_t code)
Inserts a glyph into the font.
Definition: bitmap-font.cpp:39
void clear()
Removes all glyphs from the font.
Definition: bitmap-font.cpp:52
const bitmap_glyph * get_glyph(char32_t code) const
Returns a pointer to the glyph corresponding to a UTF-32 character code, or nullptr if no such glyph ...
Abstract base class for fonts.
Definition: font.hpp:37
font_metrics metrics
Definition: font.hpp:100
Text and typography.
Definition: bitmap-font.cpp:24
Single glyph in a bitmap font.
Metrics describing properties of a font.
Metrics describing properties of a glyph.