summaryrefslogtreecommitdiff
path: root/ui/font.h
blob: c9d834a66f9c9adeea626f80be16c4c8adc8717f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef FONT_H
#define FONT_H

#include <GL/glew.h>
#include <array>
#include <cstddef>
#include <cstdint>
#include <glm/glm.hpp>
#include <map>
#include <string>
#include <string_view>
#include <vector>

class Font {
public:
	Font(const char * const path, unsigned int height);

	using Quad = std::array<glm::vec4, 4>;
	using Quads = std::vector<Quad>;
	using TextureQuads = std::map<GLuint /*textureId*/, Quads>;
	TextureQuads render(const std::string_view text) const;

	struct CharData {
		size_t textureIdx;
		glm::uvec2 size;
		glm::uvec2 position;
		glm::ivec2 bearing;
		long advance;
	};
	struct FontTexture {
		GLuint texture;
		unsigned int used;
	};

	static glm::uvec3 getTextureSize(unsigned int height);

protected:
	void generateChars(const std::string_view text) const;
	const CharData getChar(char) const;
	std::size_t getTextureWithSpace(unsigned int adv) const;

	std::string path;
	glm::uvec3 size;
	mutable std::map<uint32_t, CharData> charsData;
	mutable std::vector<FontTexture> fontTextures;
};

#endif