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
|
/*
* Copyright 2013 Jeremie Roy. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "text_metrics.h"
#include "utf8.h"
TextMetrics::TextMetrics(FontManager* _fontManager)
: m_fontManager(_fontManager)
{
clearText();
}
void TextMetrics::clearText()
{
m_width = m_height = m_x = m_lineHeight = m_lineGap = 0;
}
void TextMetrics::appendText(FontHandle _fontHandle, const char* _string)
{
const FontInfo& font = m_fontManager->getFontInfo(_fontHandle);
if (font.lineGap > m_lineGap)
{
m_lineGap = font.lineGap;
}
if ( (font.ascender - font.descender) > m_lineHeight)
{
m_height -= m_lineHeight;
m_lineHeight = font.ascender - font.descender;
m_height += m_lineHeight;
}
CodePoint codepoint = 0;
uint32_t state = 0;
for (; *_string; ++_string)
{
if (!utf8_decode(&state, (uint32_t*)&codepoint, *_string) )
{
const GlyphInfo* glyph = m_fontManager->getGlyphInfo(_fontHandle, codepoint);
if (NULL != glyph)
{
if (codepoint == L'\n')
{
m_height += m_lineGap + font.ascender - font.descender;
m_lineGap = font.lineGap;
m_lineHeight = font.ascender - font.descender;
m_x = 0;
}
m_x += glyph->advance_x;
if(m_x > m_width)
{
m_width = m_x;
}
}
else
{
BX_CHECK(false, "Glyph not found");
}
}
}
BX_CHECK(state == UTF8_ACCEPT, "The string is not well-formed");
}
TextLineMetrics::TextLineMetrics(const FontInfo& _fontInfo)
{
m_lineHeight = _fontInfo.ascender - _fontInfo.descender + _fontInfo.lineGap;
}
uint32_t TextLineMetrics::getLineCount(const bx::StringView& _str) const
{
CodePoint codepoint = 0;
uint32_t state = 0;
uint32_t lineCount = 1;
for (const char* ptr = _str.getPtr(); ptr != _str.getTerm(); ++ptr)
{
if (utf8_decode(&state, (uint32_t*)&codepoint, *ptr) == UTF8_ACCEPT)
{
if (codepoint == L'\n')
{
++lineCount;
}
}
}
BX_CHECK(state == UTF8_ACCEPT, "The string is not well-formed");
return lineCount;
}
void TextLineMetrics::getSubText(const bx::StringView& _str, uint32_t _firstLine, uint32_t _lastLine, const char*& _begin, const char*& _end)
{
CodePoint codepoint = 0;
uint32_t state = 0;
// y is bottom of a text line
uint32_t currentLine = 0;
const char* ptr = _str.getPtr();
while (ptr != _str.getTerm()
&& (currentLine < _firstLine) )
{
for (; ptr != _str.getTerm(); ++ptr)
{
if (utf8_decode(&state, (uint32_t*)&codepoint, *ptr) == UTF8_ACCEPT)
{
if (codepoint == L'\n')
{
++currentLine;
++ptr;
break;
}
}
}
}
BX_CHECK(state == UTF8_ACCEPT, "The string is not well-formed");
_begin = ptr;
while (ptr != _str.getTerm()
&& (currentLine < _lastLine) )
{
for (; ptr != _str.getTerm(); ++ptr)
{
if(utf8_decode(&state, (uint32_t*)&codepoint, *ptr) == UTF8_ACCEPT)
{
if(codepoint == L'\n')
{
++currentLine;
++ptr;
break;
}
}
}
}
BX_CHECK(state == UTF8_ACCEPT, "The string is not well-formed");
_end = ptr;
}
|