Project Alice
Loading...
Searching...
No Matches
bmfont.hpp
Go to the documentation of this file.
1/*
2bm_font example implementation with Kerning, for C++ and OpenGL 2.0
3
4This is free and unencumbered software released into the public domain.
5
6Anyone is free to copy, modify, publish, use, compile, sell, or
7distribute this software, either in source code form or as a compiled
8binary, for any purpose, commercial or non-commercial, and by any
9means.
10
11In jurisdictions that recognize copyright laws, the author or authors
12of this software dedicate any and all copyright interest in the
13software to the public domain. We make this dedication for the benefit
14of the public at large and to the detriment of our heirs and
15successors. We intend this dedication to be an overt act of
16relinquishment in perpetuity of all present and future rights to this
17software under copyright law.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25OTHER DEALINGS IN THE SOFTWARE.
26
27For more information, please refer to <http://unlicense.org/>
28*/
29
30#pragma once
31
32#include <vector>
33#include "simple_fs.hpp"
34#include "texture.hpp"
35#include "unordered_dense.h"
36
37namespace sys {
38struct state;
39}
40
41namespace text {
42
44 int32_t x = 0;
45 int32_t y = 0;
46 int32_t width = 0;
47 int32_t height = 0;
48 int32_t x_offset = 0;
49 int32_t y_offset = 0;
50 int32_t x_advance = 0;
51 int32_t page = 0;
52};
53
54class bm_font {
55public:
56 bm_font() { }
57 bm_font(sys::state& state, simple_fs::file& font_metrics, simple_fs::file& font_image) {
58 auto font_result = ogl::make_font_texture(font_image);
59 ftexid = font_result.handle;
60 parse_font(state, font_metrics);
61 assert(ftexid != 0);
62 width = int16_t(font_result.size);
63 };
64 bm_font(bm_font&& src) noexcept {
65 ftexid = src.ftexid;
66 chars = src.chars;
67 kernings = std::move(src.kernings);
68 width = src.width;
69 height = src.height;
70 base = src.base;
71 line_height = src.line_height;
72 src.ftexid = 0;
73 }
74 bm_font& operator=(bm_font&& src) noexcept {
75 ftexid = src.ftexid;
76 chars = src.chars;
77 kernings = std::move(src.kernings);
78 width = src.width;
79 height = src.height;
80 base = src.base;
81 line_height = src.line_height;
82 src.ftexid = 0;
83 return *this;
84 }
85 ~bm_font();
86
87 std::array<char_descriptor, 256> chars;
88 ankerl::unordered_dense::map<uint16_t, int32_t> kernings;
89 int32_t line_height = 0;
90 int32_t base = 0;
91 int32_t width = 0;
92 int32_t height = 0;
93 int32_t pages = 0;
94 int32_t scale_w = 0;
95 int32_t scale_h = 0;
96 GLuint ftexid = 0;
97
98 float get_height() const { return float(line_height); }
99 float get_string_width(sys::state& state, char const*, uint32_t) const;
101 int get_kerning_pair(char, char) const;
102};
103
104bm_font const& get_bm_font(sys::state& state, uint16_t font_handle);
105
106} // namespace text
bm_font(sys::state &state, simple_fs::file &font_metrics, simple_fs::file &font_image)
Definition: bmfont.hpp:57
ankerl::unordered_dense::map< uint16_t, int32_t > kernings
Definition: bmfont.hpp:88
int32_t pages
Definition: bmfont.hpp:93
int32_t width
Definition: bmfont.hpp:91
GLuint ftexid
Definition: bmfont.hpp:96
int32_t line_height
Definition: bmfont.hpp:89
bool parse_font(sys::state &state, simple_fs::file &f)
Definition: bmfont.cpp:120
int32_t base
Definition: bmfont.hpp:90
bm_font(bm_font &&src) noexcept
Definition: bmfont.hpp:64
int32_t height
Definition: bmfont.hpp:92
float get_string_width(sys::state &state, char const *, uint32_t) const
Definition: bmfont.cpp:139
int32_t scale_h
Definition: bmfont.hpp:95
int get_kerning_pair(char, char) const
Definition: bmfont.cpp:131
int32_t scale_w
Definition: bmfont.hpp:94
std::array< char_descriptor, 256 > chars
Definition: bmfont.hpp:87
bm_font & operator=(bm_font &&src) noexcept
Definition: bmfont.hpp:74
float get_height() const
Definition: bmfont.hpp:98
#define assert(condition)
Definition: debug.h:74
font_texture_result make_font_texture(simple_fs::file &f)
Definition: texture.cpp:745
Definition: constants.hpp:4
Definition: bmfont.cpp:118
bm_font const & get_bm_font(sys::state &state, uint16_t font_handle)
Definition: bmfont.cpp:162
uint uint32_t