File: glFont.h

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (205 lines) | stat: -rw-r--r-- 7,425 bytes parent folder | download | duplicates (3)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef _GLFONT_H
#define _GLFONT_H

#include <string>
#include <deque>

#include "TextWrap.h"
#include "ustring.h"

#include "Rendering/GL/RenderDataBuffer.hpp"
#include "System/float4.h"
#include "System/Threading/SpringThreading.h"

#undef GetCharWidth // winapi.h

static constexpr int FONT_LEFT      = 1 << 0;
static constexpr int FONT_RIGHT     = 1 << 1;
static constexpr int FONT_CENTER    = 1 << 2;
static constexpr int FONT_BASELINE  = 1 << 3; // align to face baseline
static constexpr int FONT_VCENTER   = 1 << 4;
static constexpr int FONT_TOP       = 1 << 5; // align to text ascender
static constexpr int FONT_BOTTOM    = 1 << 6; // align to text descender
static constexpr int FONT_ASCENDER  = 1 << 7; // align to face ascender
static constexpr int FONT_DESCENDER = 1 << 8; // align to face descender

static constexpr int FONT_OUTLINE   = 1 << 9;
static constexpr int FONT_SHADOW    = 1 << 10;
static constexpr int FONT_NORM      = 1 << 11; // render in 0..1 space instead of 0..vsx|vsy
static constexpr int FONT_SCALE     = 1 << 12; // given size argument will be treated as scaling and not absolute fontsize

static constexpr int FONT_NEAREST   = 1 << 13; // round x,y render pos to nearest integer, so there is no interpolation blur for small fontsizes

static constexpr int FONT_BUFFERED  = 1 << 14; // make glFormat append to buffer outside a {Begin,End} pair


namespace Shader {
	struct IProgramObject;
};

class CglFont : public CTextWrap
{
public:
	static bool LoadConfigFonts();
	static bool LoadCustomFonts(const std::string& smallFontFile, const std::string& largeFontFile);
	static CglFont* LoadFont(const std::string& fontFile, bool small);
	static CglFont* LoadFont(const std::string& fontFile, int size, int outlinewidth = 2, float outlineweight = 5.0f);
	static void ReallocAtlases(bool pre);
	static void SwapRenderBuffers();

	CglFont(const std::string& fontFile, int size, int outlinewidth, float outlineweight);
	~CglFont();


	void Begin() { Begin(defShader); }
	void End() { End(defShader); }
	void Begin(Shader::IProgramObject* shader);
	void End(Shader::IProgramObject* shader);

	void DrawBufferedGL4() { DrawBuffered(); }
	void DrawBufferedGL4(Shader::IProgramObject* shader) { DrawBuffered(shader); }

	void DrawBuffered() { DrawBuffered(defShader); }
	void DrawBuffered(Shader::IProgramObject* shader);

	void SwapBuffers();

	void ResetBuffer(bool outline) {
		const unsigned int bi = GetBufferIdx(outline);

		prvBufferPos[bi] = mapBufferPtr[bi];
		curBufferPos[bi] = mapBufferPtr[bi];
	}
	void ResetBuffers() {
		ResetBuffer(false);
		ResetBuffer( true);
	}


	void glWorldBegin() { glWorldBegin(defShader); }
	void glWorldEnd() { glWorldEnd(defShader); }
	void glWorldBegin(Shader::IProgramObject* shader);
	void glWorldEnd(Shader::IProgramObject* shader);
	void glWorldPrint(const float3& p, const float size, const std::string& str);

	void SetViewMatrix(const CMatrix44f& mat) { viewMatrix = mat; }
	void SetProjMatrix(const CMatrix44f& mat) { projMatrix = mat; }

	static CMatrix44f DefViewMatrix();
	static CMatrix44f DefProjMatrix();

	/**
	 * @param s  absolute font size, or relative scale, if option FONT_SCALE is set
	 * @param options  FONT_SCALE | FONT_NORM |
	 *                 (FONT_LEFT | FONT_CENTER | FONT_RIGHT) |
	 *                 (FONT_BASELINE | FONT_DESCENDER | FONT_VCENTER |
	 *                  FONT_TOP | FONT_ASCENDER | FONT_BOTTOM) |
	 *                 FONT_NEAREST | FONT_OUTLINE | FONT_SHADOW
	 */
	void glPrint(float x, float y, float s, const int options, const std::string& str);
	void glPrintTable(float x, float y, float s, const int options, const std::string& str);
	void glFormat(float x, float y, float s, const int options, const char* fmt, ...);

	void SetAutoOutlineColor(bool enable); // auto-select outline color for in-text-colorcodes
	void SetTextColor(const float4* color = nullptr);
	void SetOutlineColor(const float4* color = nullptr);
	void SetColors(const float4* textColor = nullptr, const float4* outlineColor = nullptr);
	void SetTextColor(float r, float g, float b, float a) { const float4 f{r, g, b, a}; SetTextColor(&f); };
	void SetOutlineColor(float r, float g, float b, float a) { const float4 f{r, g, b, a}; SetOutlineColor(&f); };
	void SetTextDepth(float z) { textDepth.x = z; }
	void SetOutlineDepth(float z) { textDepth.y = z; }

	float GetCharacterWidth(const char32_t c);

	inline float GetTextWidth(const std::string& text) override;
	inline float GetTextHeight(const std::string& text, float* descender = nullptr, int* numLines = nullptr);

	static std::deque<std::string> SplitIntoLines(const std::u8string&);

	const std::string& GetFilePath() const { return fontPath; }

	static constexpr char8_t ColorCodeIndicator  = 0xFF;
	static constexpr char8_t ColorResetIndicator = 0x08; // =: '\\b'
	static bool threadSafety;

	// typedef void (*ColorCodeCallBack)(float4);
	typedef std::function<void(float4)> ColorCodeCallBack;

private:
	static const float4* ChooseOutlineColor(const float4& textColor);
	static const float4 GetTexScaleMatrix(float w, float h) { return {1.0f / w, 0.0f, 0.0f, 1.0f / h}; } // 2x2

	void RenderString(float x, float y, float scaleX, float scaleY, const std::string& str, const ColorCodeCallBack& cccb);
	void RenderStringShadow(float x, float y, float scaleX, float scaleY, const std::string& str, const ColorCodeCallBack& cccb);
	void RenderStringOutlined(float x, float y, float scaleX, float scaleY, const std::string& str, const ColorCodeCallBack& cccb);

private:
	float GetTextWidth_(const std::u8string& text);
	float GetTextHeight_(const std::u8string& text, float* descender = nullptr, int* numLines = nullptr);

private:
	unsigned int GetBufferIdx(bool outline) const { return (currBufferIndx * 2 + outline); }

	enum {
		PRIMARY_BUFFER = 0,
		OUTLINE_BUFFER = 1,
	};

private:
	std::string fontPath;
	spring::recursive_mutex bufferMutex;


	// used by {Begin,End}; each double-buffered
	GL::RenderDataBuffer primaryBuffer[2];
	GL::RenderDataBuffer outlineBuffer[2];

	GL::RenderDataBufferTC primaryBufferTC[2];
	GL::RenderDataBufferTC outlineBufferTC[2];

	Shader::IProgramObject* defShader = nullptr;
	Shader::IProgramObject* curShader = nullptr;

	VA_TYPE_TC* mapBufferPtr[2 * 2] = {nullptr, nullptr, nullptr, nullptr}; // {primary,outline} start-pos
	VA_TYPE_TC* prvBufferPos[2 * 2] = {nullptr, nullptr, nullptr, nullptr}; // previous {primary,outline} write-pos
	VA_TYPE_TC* curBufferPos[2 * 2] = {nullptr, nullptr, nullptr, nullptr}; // current {primary,outline} write-pos


	unsigned int currBufferIndx = 0;
	unsigned int lastPrintFrame = 0;

	bool inBeginEndBlock = false; // implies bufferMutex is locked
	bool autoOutlineColor = false; // auto-select outline color for in-text-colorcodes

	float4 textColor;
	float4 outlineColor;

	// colors set when glPrint was called; ColorResetIndicator will reset to these
	float4 baseTextColor;
	float4 baseOutlineColor;

	float2 textDepth;

	CMatrix44f viewMatrix;
	CMatrix44f projMatrix;
};


extern CglFont* font;
extern CglFont* smallFont;


// wrappers
float CglFont::GetTextWidth(const std::string& text)
{
	return GetTextWidth_(toustring(text));
}
float CglFont::GetTextHeight(const std::string& text, float* descender, int* numLines)
{
	return GetTextHeight_(toustring(text), descender, numLines);
}

#endif /* _GLFONT_H */