File: NVGFont.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (251 lines) | stat: -rw-r--r-- 5,105 bytes parent folder | download | duplicates (2)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

#include "graphics/software/NVGFont.h"
#include "graphics/paths/PathRenderer.h"

#include "mod_table/mod_table.h"

#include "localization/localize.h"

#include <limits>

namespace
{
	const char* const TOKEN_SEPARATORS = "\n\t\r";
}

namespace font
{
	size_t NVGFont::getTokenLength(const char *string, size_t maxLength)
	{
		Assert(string != NULL);

		size_t length = 0;

		if (maxLength <= 0)
			return 0;

		if (!Unicode_text_mode) {
			// If we are not in unicode mode then we need to handle special characters
			// Otherwise we just assume that the text is UTF-8 encoded
			if (*string >= Lcl_special_chars || *string < 0)
				return 1;
		}

		const char *nullPtr = strchr(const_cast<char*>(string), '\0');
		const char *nextToken = strpbrk(const_cast<char*>(string), TOKEN_SEPARATORS);

		// WOHOO! Pointer arithmetic!!!
		if (nullPtr != NULL && (nextToken == NULL || nullPtr < nextToken))
		{
			length = nullPtr - string;
		}
		else if (nextToken != NULL)
		{
			if (nextToken == string)
			{
				length = 1;
			}
			else
			{
				length = nextToken - string;
			}
		}
		else
		{
			length = strlen(string);
		}

		if (length > maxLength)
		{
			length = maxLength;
		}

		if (!Unicode_text_mode) {
			// Same as above
			for (size_t i = 0; i < length; i++)
			{
				if (string[i] >= Lcl_special_chars || string[i] < 0)
				{
					// Special character needs to be handled separately
					return i;
				}
			}
		}

		return length;
	}

	NVGFont::NVGFont() : m_handle(-1), m_letterSpacing(0.0f), m_size(12.0f), m_tabWidth(20.0f)
	{

	}

	NVGFont::~NVGFont()
	{

	}
	
	void NVGFont::setHandle(int handle)
	{
		Assertion(handle >= 0, "Invalid font handle passed!");
		m_handle = handle;
	}

	void NVGFont::setSize(float size)
	{
		Assertion(size > 0.f, "Invalid size %f passed!", size);
		m_size = size;
	}

	void NVGFont::setLetterSpacing(float spacing)
	{
		Assertion(spacing >= 0.0f, "Invalid letter spacing passed!");
		m_letterSpacing = spacing;
	}

	void NVGFont::setTabWidth(float tabWidth)
	{
		Assertion(tabWidth >= 0.0f, "Invalid tab width passed!");
		m_tabWidth = tabWidth;
	}

	void NVGFont::setSpecialCharacterFont(font* fontData)
	{
		Assertion(fontData != nullptr, "Invalid font data pointer passed!");
		m_specialCharacters = fontData;
	}
	
	float NVGFont::getTextHeight() const
	{
		return m_lineHeight;
	}

	extern int get_char_width_old(font* fnt, ubyte c1, ubyte c2, int *width, int* spacing);
	void NVGFont::getStringSize(const char *text, size_t textLen, int resize_mode, float *width, float *height) const
	{
		using namespace graphics::paths;

		auto path = PathRenderer::instance();

		path->saveState();
		path->resetState();

		path->fontFaceId(m_handle);
		path->fontSize(m_size);
		path->textLetterSpacing(m_letterSpacing);
		path->textAlign(static_cast<TextAlign>(ALIGN_TOP | ALIGN_LEFT));

		if (resize_mode != -1)
		{
			float scaleX = 1.0f;
			float scaleY = 1.0f;

			gr_resize_screen_posf(nullptr, nullptr, &scaleX, &scaleY, resize_mode);

			path->scale(scaleX, scaleY);
		}

		float w = 0.0f;
		float h = this->getHeight();

		size_t tokenLength;

		const char *s = text;
		bool specialChar = false;
		float lineWidth = 0.0f;

		while ((tokenLength = getTokenLength(s, textLen)) > 0)
		{
			if (tokenLength > textLen)
			{
				tokenLength = textLen;
			}

			if (tokenLength == 1)
			{
				// We may have encountered a special character
				switch (*s)
				{
				case '\n':
					specialChar = true;

					h += this->getHeight();
					lineWidth = 0.0f;
					break;
				case '\t':
					specialChar = true;

					lineWidth += this->getTabWidth();
					break;
				default:
					if (!Unicode_text_mode) {
						// Same as before, this code is only needed in non-unicode mode	
						if (*s >= Lcl_special_chars || *s < 0)
						{
							specialChar = true;

							int charWidth;
							int spacing;

							if (m_specialCharacters == nullptr) {
								Error(LOCATION,
									  "Font %s has no special characters font! This is usually caused by ignoring a font table parsing warning.",
									  getName().c_str());
							}

							get_char_width_old(m_specialCharacters, static_cast<ubyte>(*s), '\0', &charWidth, &spacing);

							lineWidth += i2fl(spacing);
						}
					}
					break;
				}
			}

			if (!specialChar)
			{
				lineWidth += path->textBounds(0.f, 0.f, s, s + tokenLength, nullptr);
			}

			w = MAX(w, lineWidth);

			specialChar = false;

			s = s + tokenLength;

			textLen -= tokenLength;

			// see above, tokenLength is at most equal to textLen so this == 0 comparision should be safe
			if (textLen == 0)
			{
				break;
			}
		}

		if (height)
			*height = h;

		if (width)
			*width = w;

		path->restoreState();
	}
	void NVGFont::computeFontMetrics() {
		auto path = graphics::paths::PathRenderer::instance();

		path->saveState();
		path->resetState();

		path->fontFaceId(m_handle);
		path->fontSize(m_size);
		path->textLetterSpacing(m_letterSpacing);

		path->textMetrics(&_ascender, &_descender, &m_lineHeight);

		path->restoreState();

		_height = m_lineHeight + this->offsetTop + this->offsetBottom;

		checkFontMetrics();
	}
}