bgfx/examples/common/font/text_metrics.cpp

144 lines
2.9 KiB
C++
Raw Normal View History

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