summaryrefslogtreecommitdiff
path: root/ui/font.h
blob: 4b44ee3015d05ae0b1aa87504fbb703fb67959f5 (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
49
50
51
52
#ifndef FONT_H
#define FONT_H

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

class Font {
public:
	static Cache<Font, std::filesystem::path, unsigned int> cachedFontRenderings;

	Font(std::filesystem::path 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 {
		glTexture 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::filesystem::path path;
	glm::uvec3 size;
	mutable std::map<uint32_t, CharData> charsData;
	mutable std::vector<FontTexture> fontTextures;
};

#endif