summaryrefslogtreecommitdiff
path: root/src/font.hpp
blob: 9be881c1d55649c1f706cf44b468fc82f4284605 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef PKE_FONT_TYPE_HPP
#define PKE_FONT_TYPE_HPP

#include "asset-manager.hpp"
#include "components.hpp"
#include "pk.h"

#include <glm/vec2.hpp>
#include <glm/vec4.hpp>

TypeSafeInt_H(FontTypeIndex, uint16_t, 0xFFFF);
TypeSafeInt_H(FontRenderIndex, uint16_t, 0xFFFF);

enum FONT_RENDER_SURFACE_AREA_TYPE_FLAGS : uint8_t {
	FONT_RENDER_SURFACE_AREA_TYPE_FLAGS_NONE = 0,
	FONT_RENDER_SURFACE_AREA_TYPE_FLAGS_GROW_VERTICAL   = (1 << 0),
	FONT_RENDER_SURFACE_AREA_TYPE_FLAGS_GROW_HORIZONTAL = (1 << 1),

	FONT_RENDER_SURFACE_AREA_TYPE_FLAGS_STATIC = 0,
	FONT_RENDER_SURFACE_AREA_TYPE_FLAGS_FLUID = (1 << 0) | (1 << 1),
};

struct FontGlyphChar {
	double advance;
	glm::vec2 sprite_region_min;
	glm::vec2 sprite_region_max;
	glm::dvec4 baseline_bounding_box;
	uint32_t unicode;
	bool is_whitespace;
};
struct FontRenderSettings {
	float char_scale;
	float line_height_scale;
	float char_spacing_scale;
	glm::ivec2 surface_area_size;
	FONT_RENDER_SURFACE_AREA_TYPE_FLAGS surface_area_type_flags;
};
struct FontRender {
	uint32_t len;
	FontTypeIndex index_ft;
	FontRenderIndex index_fr;
	FontRenderSettings settings;
};
struct FontTypeMSDFSettings {
	float minimum_scale;
	float px_range;
};
struct FontTypeSpacing {
	double geometry_scale;
	double em_size;
	double ascender_y;
	double descender_y;
	double line_height;
	double underline_y;
	double underline_thickness;
};
struct FontType : public Entity_Base {
	pk_cstr title;
	AssetHandle fontTextureAssetHandle;
	AssetHandle glyphDetailsAssetHandle;
	FontGlyphChar *glyphs;
	FontRender *renders = nullptr;
	glm::vec2 atlas_size;
	uint32_t n_glyphs;
	FontRenderIndex n_render = FontRenderIndex{0};
	FontRenderIndex h_render = FontRenderIndex{0};
	FontTypeIndex index_ft = FontTypeIndex{0};
	struct FontTypeMSDFSettings msdf_settings;
	struct FontTypeSpacing spacing;
	struct FontTypeGraphics {
		VkDeviceMemory deviceMemoryVert = VK_NULL_HANDLE;
		VkDeviceMemory deviceMemoryTexture = VK_NULL_HANDLE;
		VkDeviceMemory deviceMemoryInst = VK_NULL_HANDLE;
		VkImage textureImage = VK_NULL_HANDLE;
		VkImageView textureImageView = VK_NULL_HANDLE;
		VkDescriptorPool vkDescriptorPool = VK_NULL_HANDLE;
		VkDescriptorSet *vkDescriptorSets = nullptr;
	} gr;
	struct FontTypeBindings {
		BufferBindingDetails vertexBD;
		BufferBindingDetails uvBD;
		BufferBindingDetails atlasSizeBD;
		BufferBindingDetails indexBD;
		BufferBindingDetails instanceBD;
		uint32_t indexCount = 0;
		uint32_t instanceCounter = 0;
	} bindings;
};

void FontType_Init();
void FontType_Teardown();
void FontType_Tick();
void FontType_Serialize(std::ofstream &stream, FontType *ft);
void FontType_Deserialize(std::ifstream &stream);
FontType* FontType_GetFonts(FontTypeIndex &count);
FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle, AssetHandle glyphsHandle, FontTypeSpacing *spacing);
void FontType_Unload(FontTypeIndex idx);
FontRender FontType_AddStringRender(FontTypeIndex idx_ft, pk_cstr cstr);
void FontType_RemoveStringRender(FontRender fr);

#endif /* PKE_FONT_TYPE_HPP */