/** * @file dl_text.h * @brief 文本绘制 * * * @version 1.1 * @author lveyou * @date 22-09-12 * * @note 本质上也是精灵的组合 * 换行规则:某些标点符号不得出现在行首、英文单词整个在同一行、不得超出设置的最大宽度(简单起见,允许超出一点点) * 简单起见,不再考虑对齐问题。均以左上对齐排版,外部通过GetAreaSize返回占地大小,进行整体偏移 * 空格不是标点符号 * * @note 第一步生成TextNode,第二步再对TextNode排版 * * @note 字体大小自适应ui缩放逻辑: * 通过扩大字体;计算父节点缩放,设置自身缩放,最终变换为1 */ #pragma once #include "base/dl_color.h" #include "base/dl_array_enum.h" #include "dl_sprite.h" namespace dl { //! 文本参数 enum class TextParam { NORMAL, //!< 正常 OUTLINE,//!< 描边 SHADOW, //!< 阴影 MAX_NUM }; //! 单个字符 struct TextNode { char32_t _ch; gcSprite _spr; gcSprite _sprOutline; gcSprite _sprShadow; Float _advance; Float _advance1;//现在\t使用它作为真正长度,而advance为设置的宽度比如2.0 Float _h;//字体高度 Float _base;//基线之上的高度 unsigned _shadow; Quad _quad[3]; TextNode(char32_t ch, Float advance, Float h, Float base, unsigned shadow) : _ch(ch), _advance(advance), _advance1{ 0 }, _h(h), _base(base), _shadow(shadow) { } void RecordQuad() { if (_spr) _quad[0] = _spr->GetQuad(); if (_sprOutline) _quad[1] = _sprOutline->GetQuad(); if (_sprShadow) _quad[2] = _sprShadow->GetQuad(); } void Set(Float x, Float y) { if (_spr) _spr->SetQuad(_quad[0] + Position{ x, y }); if (_sprOutline) _sprOutline->SetQuad(_quad[1] + Position{ x, y }); if (_sprShadow) _sprShadow->SetQuad(_quad[2] + Position{ x + _shadow, y + _shadow }); } }; class Text { friend class Factory; friend class PoolList; public: //! 布局属性 struct Layout { Align::Direction _alignHorizontalLine;//!< 单行水平对齐 unsigned _maxW; //!< 最大宽度,0为无限 Float _lineSpacing; //!< 行间距 bool _isCantFirstPunct; //!< 行首不能为一些符号 bool _isWordInSingleLine; //!< 单词必须在单行 Layout() : _alignHorizontalLine(Align::Direction::BACK), _maxW(0), _lineSpacing(1.0), _isCantFirstPunct(false), _isWordInSingleLine(false) {} }; /** * @brief 绘制 */ void Render(); /** * @brief 设置是否要格式化字符串(默认为否) */ void SetFormat(bool open) { if (open != _bFormat) { _bFormat = open; _refresh_string(); } } /** * @brief 设置布局属性(单行而已) */ void SetLayoutAlign(Align::Direction dir_h) { _layout._alignHorizontalLine = dir_h; _update_layout(); } /** * @brief 设置Coor位置,来整体对齐 * @note 字体大小变化后,需要重新设置 */ void SetPositionAlign(const Position& pos = {}, const Align::Direction2& dir = Align::LT) { Position offset = -Align::Size2Anchor(toPosition(GetSize()), dir); _coor->SetPosition(pos + offset); } /** * @brief 设置Coor位置,并根据整体大小居中 * @note 字体大小变化后,需要重新设置 */ void SetCenter(const Position& pos = {}); /** * @brief 设置自动换行宽度,0为不自动换行 */ void SetMaxW(unsigned w) { _layout._maxW = w; _update_layout(); } /** * @brief 设置行间距,默认为1.0 */ void SetLineSpacing(Float k) { _layout._lineSpacing = k; _update_layout(); } const Layout& GetLayout() { return _layout; } /** * @brief 设置绘制顺序 */ void SetOrder(float order); /** * @brief 设置颜色 */ void SetColor(const Color& col, TextParam type = TextParam::NORMAL); /** * @brief 设置内容 * @param[in] str 要设置的内容 * @retval 未修改返回false * * @remarks 格式化标签如下 * @remarks [c_red] 颜色 红色,可以有3个,额外指定描边和阴影的颜色 * @remarks [f_0] 字体 id为0的字体 * @remarks [s_24_2_1] 字体大小 24大小,2描边大小,1阴影偏移 * @remarks [spr_test_0_1] 精灵 test, [0, 1] * @remarks [v_money] 变量 money * @remarks [t_8] 对齐 tab对齐到8个字体大小,默认为2个(即4个英文字母宽度) * @remarks [l] 左中括号 * @remarks [r] 右中括号 * @remarks [n_2] 换行 换行2次,默认为1次 * @remarks 特殊的 [c]、[f] 还原到字体本身颜色和字体 */ bool SetString(std::string_view str); /** * @brief 设置字体id */ void SetFontId(std::size_t id); /** * @brief 设置字体大小 */ void SetFontSize(unsigned size); /** * @brief 设置描边属性 */ void SetOutline(unsigned outline, const Color& color = ColorDef::BLACK); /** * @brief 设置阴影属性 */ void SetShadow(unsigned shadow, const Color& color = ColorDef::BLACK); /** * @brief 设置背景精灵 * @note 自动设定为文本大小,但可以设定颜色 */ void SetBackground(bool open, const Color& col = ColorDef::TEXT_BG); //! 注意传入gcText会调用上面的重载 void SetBackground(Text* b) { if (b->_sprBg) SetBackground(true, b->_sprBg->GetColor()); else SetBackground(false); } /** * @brief 返回坐标系 * @note 注意,直接调整位置是以左上角对齐,请优先使用SetPositionAlign * @note 缩放会自动控制,请勿设置 */ Coor* GetCoor() { return _coor; } float GetOrder() { return _order; } std::string GetString(); Color GetColor(TextParam type = TextParam::NORMAL) { return _col[type]; } /** * @brief 返回区域大小 * @note 字体本身一定是左上角为0 * @note 高度与字体大小相关,例如"a"与"q"的高度一样 */ Size GetSize(); /** * @brief 返回字体大小 */ unsigned GetFontSize(TextParam type = TextParam::NORMAL) { return _fontSize[type]; } /** * @brief 是否被光标选中 */ bool IsPickup(); /** * @brief 返回所在位置的字符 * @param[in] pos 自己坐标系 */ size_t Pos2Index(const Position& pos); /** * @brief 下标转到 字符坐标 */ Position Index2Pos(size_t i); ~Text(); private: Text(); size_t _fontId;// 字体id ArrayEnum _fontSize;// 设置字体大小 ArrayEnum _fontSizeReal;// 实际字体大小 ArrayEnum _col;// 字体颜色 float _order;// 绘制顺序 Layout _layout;// 布局 Coor* _coor;// 坐标系 std::vector _allNode;// 所有节点 bool _bFormat;// 格式化 Size _areaSize;// 占地大小(自己坐标系) gcSprite _sprBg;// 背景精灵 // 缓存参数 std::string _str; void _update_order(); void _update_spr_bg(); void _refresh_string(); //! 更新布局,并计算占地大小 void _update_layout(); //! 能在行首的字符 bool _is_char_can_first(char ch); // 计算scale bool _update_scale(); }; using gcText = GC; }