625 lines
14 KiB
C++
625 lines
14 KiB
C++
#include "dl_text.h"
|
||
#include "math/dl_math.h"
|
||
#include "dl_factory.h"
|
||
#include "../io/dl_input.h"
|
||
|
||
namespace dl
|
||
{
|
||
//! 文本默认order(因为有背景,所以不能为0)
|
||
constexpr float Z_TEXT = Math::OffsetOrder(Z_DEFAULT, 0, 1);
|
||
|
||
Text::Text() :
|
||
_coor(nullptr),
|
||
_bFormat(false),
|
||
_fontId(0),
|
||
_areaSize{},
|
||
_order(Z_TEXT)
|
||
{
|
||
}
|
||
|
||
|
||
|
||
void Text::SetString(std::string_view str)
|
||
{
|
||
if (_str != str)
|
||
_set_string(pointer(std::string{ str }));
|
||
}
|
||
|
||
|
||
void Text::SetFontId(size_t id)
|
||
{
|
||
_fontId = id;
|
||
_set_string(&_str);
|
||
}
|
||
|
||
|
||
void Text::SetFontSize(unsigned size)
|
||
{
|
||
if (_fontSize[TextParam::NORMAL] == size)
|
||
return;
|
||
_fontSize[TextParam::NORMAL] = size;
|
||
_set_string(&_str);
|
||
}
|
||
|
||
|
||
void Text::SetOutline(unsigned outline, const Color& color)
|
||
{
|
||
_fontSize[TextParam::OUTLINE] = outline;
|
||
_col[TextParam::OUTLINE] = color;
|
||
|
||
_set_string(&_str);
|
||
}
|
||
|
||
|
||
void Text::SetShadow(unsigned shadow, const Color& color /*= ColorDef::BLACK*/)
|
||
{
|
||
_fontSize[TextParam::SHADOW] = shadow;
|
||
_col[TextParam::SHADOW] = color;
|
||
|
||
_set_string(&_str);
|
||
}
|
||
|
||
void Text::SetBackground(bool open, const Color& col /*= ColorDef::TEXT_BG*/)
|
||
{
|
||
//此处颜色可能发生改变
|
||
if (open)
|
||
{
|
||
if (!_sprBg)
|
||
{
|
||
_sprBg = g_factory->CreateSpriteDefault(col);
|
||
_sprBg->SetCoor(_coor);
|
||
_sprBg->SetOrder(Math::OffsetOrder(_order, 0, 0, 0));
|
||
}
|
||
else
|
||
_sprBg->SetColor(col);
|
||
_update_spr_bg();
|
||
}
|
||
else
|
||
{
|
||
_sprBg.CancelRef();
|
||
}
|
||
}
|
||
|
||
std::string Text::GetString()
|
||
{
|
||
return _str;
|
||
}
|
||
|
||
bool Text::IsPickup()
|
||
{
|
||
if (_allNode.size() == 0)
|
||
return false;
|
||
|
||
Position pos = _coor->WorldToThis(toPosition(g_input->GetCursorPosition()));
|
||
|
||
return Math::IsXyInSize(toPoint(pos), _areaSize);
|
||
}
|
||
|
||
Text::~Text()
|
||
{
|
||
delete _coor;
|
||
}
|
||
|
||
|
||
|
||
void Text::_update_order()
|
||
{
|
||
if (_sprBg)
|
||
_sprBg->SetOrder(Math::OffsetOrder(_order, 0, 0, 0));
|
||
for (auto& iter : _allNode)
|
||
{
|
||
if (iter._spr)
|
||
iter._spr->SetOrder(Math::OffsetOrder(_order, 0, 0, -3));
|
||
if (iter._sprOutline)
|
||
iter._sprOutline->SetOrder(Math::OffsetOrder(_order, 0, 0, -2));
|
||
if (iter._sprShadow)
|
||
iter._sprShadow->SetOrder(Math::OffsetOrder(_order, 0, 0, -1));
|
||
}
|
||
}
|
||
|
||
|
||
void Text::_update_spr_bg()
|
||
{
|
||
_sprBg->SetQuad(createQuad(_areaSize, false));
|
||
}
|
||
|
||
void Text::_set_string(const std::string* str, const std::u32string* p_str_utf)
|
||
{
|
||
//由于标签保留使用中文的可能性,这里要保证单个字符比较的正确性
|
||
//而且标签转换数值,所以使用utf_string
|
||
|
||
//str_utf存在时避免多余转换
|
||
if (str == nullptr
|
||
&& p_str_utf == nullptr)
|
||
{
|
||
log_err0("参数不能同时为空!");
|
||
return;
|
||
}
|
||
|
||
_str = str ? *str : CodeCvt::Utf32ToUtf8(*p_str_utf);
|
||
|
||
_allNode.clear();
|
||
|
||
|
||
if (_str.empty())
|
||
{
|
||
//这里需要计算出一些数据
|
||
_update_layout();
|
||
return;
|
||
}
|
||
|
||
|
||
auto fn_write_char = [&](size_t id, unsigned size, unsigned outline, unsigned shadow, char32_t ch, const Color& col, const Color& col_outline, const Color& col_shadow)
|
||
{
|
||
//特殊字符处理
|
||
if (ch == U' ')
|
||
{
|
||
_allNode.push_back(TextNode(ch, (Float)(size / 2), (Float)size, 0, 0));
|
||
return;
|
||
}
|
||
if (ch == U'\t')
|
||
{//此处advance为几个字体宽度
|
||
_allNode.push_back(TextNode(ch, 2, (Float)size, 0, 0));
|
||
return;
|
||
}
|
||
if (ch == U'\n')
|
||
{
|
||
_allNode.push_back(TextNode(ch, 0, (Float)size, 0, 0));
|
||
return;
|
||
}
|
||
|
||
gcSprite spr, spr_outline, spr_shadow;
|
||
Float advance = g_factory->CreateSpriteChar({ id, ch, size, outline }, spr, spr_outline);
|
||
|
||
spr->SetCoor(_coor);
|
||
spr->SetColor(col);
|
||
//spr->SetOrder(_order);
|
||
|
||
|
||
if (outline > 0)
|
||
{
|
||
spr_outline->SetCoor(_coor);
|
||
spr_outline->SetColor(col_outline);
|
||
//spr_outline->SetOrder(_order);
|
||
}
|
||
|
||
if (shadow > 0)
|
||
{
|
||
spr_shadow = outline > 0 ? g_factory->CloneSprite(spr_outline) : g_factory->CloneSprite(spr);
|
||
spr_shadow->SetColor(col_shadow);
|
||
}
|
||
|
||
TextNode node = TextNode(ch, advance, (Float)size, -spr->GetQuad()[0][1], shadow);
|
||
node._spr = spr;
|
||
node._sprOutline = spr_outline;
|
||
node._sprShadow = spr_shadow;
|
||
node.RecordQuad();
|
||
|
||
_allNode.push_back(node);
|
||
};
|
||
|
||
|
||
std::u32string str_utf;
|
||
if (p_str_utf)
|
||
str_utf = *p_str_utf;
|
||
else
|
||
{
|
||
if (CodeCvt::Utf8::Check(_str.c_str()) == 0)
|
||
{
|
||
log_err0("字符串不是u8编码!");
|
||
|
||
std::string str_u8 = CodeCvt::MultiByteToUtf8(_str.c_str());
|
||
str_utf = CodeCvt::Utf8ToUtf32(str_u8);
|
||
}
|
||
else
|
||
str_utf = CodeCvt::Utf8ToUtf32(_str);
|
||
}
|
||
|
||
|
||
//
|
||
if (_bFormat)
|
||
{
|
||
enum class FlagState
|
||
{
|
||
NORMAL,
|
||
FLAG,
|
||
};
|
||
|
||
|
||
FlagState state = FlagState::NORMAL;//当前读取状态
|
||
Color cur_color = _col[TextParam::NORMAL];//当前颜色
|
||
Color cur_color_outline = _col[TextParam::OUTLINE];//当前颜色
|
||
Color cur_color_shadow = _col[TextParam::SHADOW];//当前颜色
|
||
|
||
size_t cur_font_id = _fontId;//当前字体
|
||
unsigned cur_font_size = _fontSize[TextParam::NORMAL];
|
||
unsigned cur_font_ouline = _fontSize[TextParam::OUTLINE];//当前描边大小
|
||
unsigned cur_font_shadow = _fontSize[TextParam::SHADOW];//当前阴影偏移
|
||
|
||
//标签使用wchar_t
|
||
std::wstring str_flag;
|
||
|
||
for (auto& iter : str_utf)
|
||
{
|
||
if (iter == 0)
|
||
{
|
||
if (iter != str_utf.back())
|
||
log_warn0("空字符出现在非尾部!");
|
||
break;
|
||
}
|
||
|
||
const char32_t& ch = iter;
|
||
switch (state)
|
||
{
|
||
case FlagState::NORMAL:
|
||
if (ch == U'[')
|
||
state = FlagState::FLAG;
|
||
else
|
||
{//以当前状态写入一个字符
|
||
fn_write_char(cur_font_id, cur_font_size, cur_font_ouline, cur_font_shadow, ch, cur_color, cur_color_outline, cur_color_shadow);
|
||
}
|
||
break;
|
||
case FlagState::FLAG:
|
||
if (ch ==U']')
|
||
{
|
||
//分长度处理
|
||
if (str_flag.empty())
|
||
{
|
||
log_err0("空标签");
|
||
}
|
||
else if (str_flag.size() == 1)
|
||
{
|
||
if (str_flag[0] == L'f')
|
||
{
|
||
cur_font_id = _fontId;
|
||
cur_font_size = _fontId;
|
||
cur_font_ouline = _fontId;
|
||
cur_font_shadow = _fontId;
|
||
}
|
||
else if (str_flag[0] == L'c')
|
||
{
|
||
cur_color = _col[TextParam::NORMAL];
|
||
cur_color_outline = _col[TextParam::OUTLINE];
|
||
cur_color_shadow = _col[TextParam::SHADOW];
|
||
}
|
||
else if (str_flag[0] == L'n')
|
||
{
|
||
//以当前状态写入\n
|
||
fn_write_char(cur_font_id, cur_font_size, cur_font_ouline, cur_font_shadow, U'\n', cur_color, cur_color_outline, cur_color_shadow);
|
||
}
|
||
else if (str_flag[0] == L't')
|
||
{
|
||
//以当前状态写入\t
|
||
fn_write_char(cur_font_id, cur_font_size, cur_font_ouline, cur_font_shadow, U'\t', cur_color, cur_color_outline, cur_color_shadow);
|
||
}
|
||
else if (str_flag[0] == L'l')
|
||
{
|
||
//以当前状态写入 [
|
||
fn_write_char(cur_font_id, cur_font_size, cur_font_ouline, cur_font_shadow, U'[', cur_color, cur_color_outline, cur_color_shadow);
|
||
}
|
||
else if (str_flag[0] == L'r')
|
||
{
|
||
//以当前状态写入 ]
|
||
fn_write_char(cur_font_id, cur_font_size, cur_font_ouline, cur_font_shadow, U']', cur_color, cur_color_outline, cur_color_shadow);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (str_flag[1] != L'_')
|
||
log_err0("未知标签:" + CodeCvt::WideCharToUtf8(str_flag));
|
||
else
|
||
{
|
||
if (str_flag[0] == L'f')
|
||
{
|
||
std::vector<std::wstring> vec;//f、id、大小、描边、阴影
|
||
split_no_empty(str_flag, L'_', vec);
|
||
|
||
if (vec.size() > 1)
|
||
cur_font_id = stoi(vec[1]);
|
||
if (vec.size() > 2)
|
||
cur_font_size = stoi(vec[2]);
|
||
if (vec.size() > 3)
|
||
cur_font_ouline = stoi(vec[3]);
|
||
if (vec.size() > 4)
|
||
cur_font_shadow = stoi(vec[4]);
|
||
}
|
||
else if (str_flag[0] == L'c')
|
||
{
|
||
std::vector<std::wstring> vec;//c、大小、描边、阴影
|
||
split_no_empty(str_flag, L'_', vec);
|
||
|
||
if (vec.size() > 1)
|
||
cur_color = g_factory->GetColor(CodeCvt::WideCharToUtf8(vec[1].c_str()));
|
||
if (vec.size() > 2)
|
||
cur_color_outline = g_factory->GetColor(CodeCvt::WideCharToUtf8(vec[2].c_str()));
|
||
if (vec.size() > 3)
|
||
cur_color_shadow = g_factory->GetColor(CodeCvt::WideCharToUtf8(vec[3].c_str()));
|
||
}
|
||
else if (str_flag[0] == L'n')
|
||
{
|
||
std::vector<std::wstring> vec;//n、次数
|
||
split_no_empty(str_flag, L'_', vec);
|
||
|
||
//以当前状态写入\n 多次
|
||
if (vec.size() > 1)
|
||
{
|
||
size_t i = stoi(vec[1]);
|
||
for (size_t j = 0; j < i; ++j)
|
||
fn_write_char(cur_font_id, cur_font_size,
|
||
cur_font_ouline, cur_font_shadow, U'\n',
|
||
cur_color, cur_color_outline, cur_color_shadow);
|
||
}
|
||
}
|
||
else if (str_flag[0] == L't')
|
||
{
|
||
std::vector<std::wstring> vec;//n、宽度
|
||
split_no_empty(str_flag, L'_', vec);
|
||
|
||
//指定宽度 写入\t
|
||
_allNode.push_back(TextNode(U'\t', stoi(vec[1]), (Float)cur_font_size, 0, 0));
|
||
}
|
||
}
|
||
}
|
||
|
||
state = FlagState::NORMAL;
|
||
str_flag.clear();
|
||
}
|
||
else
|
||
str_flag.push_back(ch);//TODO utf_char转wchar_t
|
||
break;
|
||
default:
|
||
log_err0("错误的标签!");
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Float sx = 0;
|
||
for (auto& iter : str_utf)
|
||
{
|
||
if (iter == 0)
|
||
{
|
||
if (iter != str_utf.back())
|
||
log_warn0("空字符出现在非尾部!");
|
||
break;;
|
||
}
|
||
|
||
const char32_t& ch = iter;
|
||
|
||
fn_write_char(_fontId,
|
||
_fontSize[TextParam::NORMAL], _fontSize[TextParam::OUTLINE], _fontSize[TextParam::SHADOW], ch,
|
||
_col[TextParam::NORMAL], _col[TextParam::OUTLINE], _col[TextParam::SHADOW]);
|
||
}
|
||
}
|
||
|
||
_update_order();
|
||
_update_layout();
|
||
}
|
||
|
||
|
||
void Text::_update_layout()
|
||
{
|
||
//第一遍处理,生成每行
|
||
struct Line
|
||
{
|
||
size_t _beg;
|
||
size_t _end;
|
||
Float _w;//宽度
|
||
Float _h;//字体高度
|
||
Float _base;//基线之上的最大高度
|
||
};
|
||
std::vector<Line> vec_line;
|
||
size_t j = 0;//单行下标
|
||
size_t beg = 0;
|
||
Float sx = 0;
|
||
Float max_h = (Float)_fontSize[TextParam::NORMAL];
|
||
Float max_base = 0;
|
||
for (size_t cur = 0; cur < _allNode.size(); ++cur)
|
||
{
|
||
auto& node = _allNode[cur];
|
||
//update_layout会多次调用,注意不能随便改变node的内容
|
||
if (node._ch == '\n')
|
||
{
|
||
vec_line.push_back({ beg, cur, sx, max_h, max_base });
|
||
beg = cur;
|
||
sx = 0;
|
||
max_h = (Float)_fontSize[TextParam::NORMAL];
|
||
max_base = 0;
|
||
}
|
||
else if (node._ch == '\t')
|
||
{
|
||
unsigned advance = (unsigned)(_fontSize[TextParam::NORMAL] * node._advance);
|
||
Float x = (Float)Math::Align((unsigned)sx, advance);
|
||
//对齐后任在原地,则步进advance
|
||
if (x == sx)
|
||
{
|
||
node._advance1 = advance;
|
||
sx += advance;
|
||
}
|
||
else
|
||
{
|
||
node._advance1 = x - sx;
|
||
sx = x;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (_layout._maxW)
|
||
{//自动换行
|
||
if (sx + node._advance > _layout._maxW)
|
||
{
|
||
//如果开启行首不能为特定符号
|
||
if (_layout._isCantFirstPunct
|
||
&& j != 0//不是首行
|
||
)
|
||
//TODO:由于编码问题,这里暂时未处理
|
||
//&& !_is_char_can_first(node._ch))//不能在行首
|
||
{
|
||
sx += node._advance;
|
||
if (node._h > max_h)
|
||
max_h = node._h;
|
||
}
|
||
else
|
||
{
|
||
//如果开启单词必须在单行
|
||
if (_layout._isWordInSingleLine
|
||
&& isalpha(node._ch))
|
||
{//往前回退到非字母
|
||
for (size_t i = cur; i > beg; --i)
|
||
{
|
||
if (!isalpha(_allNode[i]._ch))
|
||
{
|
||
vec_line.push_back({ beg, i, sx, max_h, max_base });
|
||
beg = i;
|
||
sx = 0;
|
||
max_h = (Float)_fontSize[TextParam::NORMAL];
|
||
max_base = 0;
|
||
break;
|
||
}
|
||
else//每回退一个减去对应宽度
|
||
sx -= _allNode[i]._advance;
|
||
}
|
||
}
|
||
else
|
||
{//正常自动换行
|
||
sx += node._advance;
|
||
vec_line.push_back({ beg, cur, sx, max_h, max_base });
|
||
beg = cur;
|
||
sx = 0;
|
||
max_h = (Float)_fontSize[TextParam::NORMAL];
|
||
max_base = 0;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
sx += node._advance;
|
||
if (node._base > max_base)
|
||
max_base = node._base;
|
||
|
||
if (node._h > max_h)
|
||
max_h = node._h;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
sx += node._advance;
|
||
if (node._base > max_base)
|
||
max_base = node._base;
|
||
|
||
if (node._h > max_h)
|
||
max_h = node._h;
|
||
}
|
||
}
|
||
}
|
||
|
||
//计算尾行下方需要额外增加像素
|
||
Float extra_h = 0;
|
||
|
||
if (beg != _allNode.size())
|
||
{//尾行
|
||
vec_line.push_back({ beg, _allNode.size(), sx, max_h + extra_h, max_base });
|
||
}
|
||
|
||
|
||
|
||
//计算max_w
|
||
Float max_w = 0;
|
||
for (auto& iter : vec_line)
|
||
{
|
||
if (iter._w > max_w)
|
||
max_w = iter._w;
|
||
//dnd_debug(DL::TEST, format("[{}, {}) - {:.0f},{:.0f}", iter._beg, iter._end, iter._w, iter._h));
|
||
}
|
||
|
||
//逐行设定位置
|
||
Float sy = 0;
|
||
for (auto& iter : vec_line)
|
||
{
|
||
Float x = 0;
|
||
if (_layout._alignHorizontalLine == Align::Direction::FRONT)
|
||
x = max_w - iter._w;
|
||
else if (_layout._alignHorizontalLine == Align::Direction::ZERO)
|
||
x = (max_w - iter._w) / 2;
|
||
|
||
|
||
Float sx = 0;
|
||
for (size_t i = iter._beg; i < iter._end; ++i)
|
||
{
|
||
auto& node = _allNode[i];
|
||
|
||
//垂直对齐,本行内
|
||
//底对齐,往下偏移本行最大基线大小
|
||
Float y = iter._base;
|
||
|
||
node.Set(sx + x, sy + y);
|
||
if (node._ch == '\t')
|
||
sx += node._advance1;
|
||
else
|
||
sx += node._advance;
|
||
}
|
||
sy += (iter._h * _layout._lineSpacing);
|
||
}
|
||
|
||
//sy即为整体高度
|
||
_areaSize = Math::CastU(Position{ max_w, sy });
|
||
|
||
if (_sprBg)
|
||
_update_spr_bg();
|
||
}
|
||
|
||
bool Text::_is_char_can_first(char ch)
|
||
{
|
||
// && ch != ','
|
||
// && ch != '。'
|
||
// && ch != '!'
|
||
// && ch != '?'
|
||
return !ispunct(ch)
|
||
|| (ch != ','
|
||
&& ch != '.'
|
||
&& ch != '!'
|
||
&& ch != '?');
|
||
}
|
||
|
||
void Text::Render()
|
||
{
|
||
if (_sprBg)
|
||
_sprBg->Render();
|
||
for (auto& iter : _allNode)
|
||
{
|
||
if (iter._sprShadow)
|
||
iter._sprShadow->Render();
|
||
if (iter._sprOutline)
|
||
iter._sprOutline->Render();
|
||
if (iter._spr)
|
||
iter._spr->Render();
|
||
}
|
||
}
|
||
|
||
|
||
void Text::SetCenter(const Position& pos)
|
||
{
|
||
SetPositionAlign(pos, { Align::Direction::ZERO, Align::Direction::ZERO });
|
||
}
|
||
|
||
void Text::SetOrder(float order)
|
||
{
|
||
if (_order != order)
|
||
{
|
||
_order = order;
|
||
_update_order();
|
||
}
|
||
}
|
||
|
||
|
||
void Text::SetColor(const Color& col, TextParam type)
|
||
{
|
||
if (_col[type] != col)
|
||
{
|
||
_col[type] = col;
|
||
_set_string(&_str, nullptr);
|
||
}
|
||
}
|
||
|
||
} |