Magic_Game/src/kiwano/render/TextLayout.h

324 lines
8.3 KiB
C
Raw Normal View History

2019-08-15 11:22:51 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
2019-08-15 11:22:51 +08:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
2019-08-15 11:22:51 +08:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
2019-08-15 11:22:51 +08:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2020-02-06 16:54:47 +08:00
#include <kiwano/math/Math.h>
2020-02-16 12:53:18 +08:00
#include <kiwano/core/ObjectBase.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/TextStyle.hpp>
2019-08-15 11:22:51 +08:00
namespace kiwano
{
2020-02-16 12:53:18 +08:00
KGE_DECLARE_SMART_PTR(TextLayout);
2020-01-21 10:09:55 +08:00
/**
* \addtogroup Render
* @{
*/
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 文本布局
2020-02-16 12:53:18 +08:00
class KGE_API TextLayout : public virtual ObjectBase
2020-01-21 10:09:55 +08:00
{
public:
2020-02-16 12:53:18 +08:00
/// \~chinese
/// @brief 创建文本布局
static TextLayoutPtr Create();
/// \~chinese
/// @brief 创建文本布局
/// @param content 文字内容
/// @param style 文本样式
static TextLayoutPtr Create(const String& content, const TextStyle& style);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 构造空的文本布局
2020-01-21 10:09:55 +08:00
TextLayout();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 文本布局是否有效
2020-01-21 10:09:55 +08:00
bool IsValid() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 文本布局是否陈旧
2020-01-21 10:09:55 +08:00
bool IsDirty() const;
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 清空文本布局
void Clear();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 重设文本布局
/// @param content 文字内容
/// @param style 文本样式
void Reset(const String& content, const TextStyle& style);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取文本行数
2020-01-21 10:09:55 +08:00
uint32_t GetLineCount() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取文本布局大小
2020-01-21 10:09:55 +08:00
Size GetLayoutSize() const;
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 获取默认填充画刷
BrushPtr GetDefaultFillBrush() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 获取默认描边画刷
BrushPtr GetDefaultOutlineBrush() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 获取默认描边宽度
float GetDefaultOutlineWidth() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 获取默认描边线条样式
StrokeStylePtr GetDefaultOutlineStrokeStyle() const;
/// \~chinese
/// @brief 文字范围
struct TextRange
{
uint32_t start; ///< 起始位置
uint32_t length; ///< 长度
};
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字体
2020-02-16 12:53:18 +08:00
/// @param font 字体
/// @param range 文字范围
void SetFont(FontPtr font, TextRange range);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字体族
2020-02-16 12:53:18 +08:00
/// @param family 字体族
/// @param range 文字范围
void SetFontFamily(String const& family, TextRange range);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字号(默认值为 18
2020-02-16 12:53:18 +08:00
/// @param size 字号
/// @param range 文字范围
void SetFontSize(float size, TextRange range);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字体粗细值(默认值为 FontWeight::Normal
2020-02-16 12:53:18 +08:00
/// @param weight 粗细值
/// @param range 文字范围
void SetFontWeight(uint32_t weight, TextRange range);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 设置文字斜体(默认值为 false
/// @param italic 是否是斜体
/// @param range 文字范围
void SetItalic(bool italic, TextRange range);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 设置下划线
/// @param enable 是否显示下划线
/// @param range 文字范围
void SetUnderline(bool enable, TextRange range);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 设置删除线
/// @param enable 是否显示删除线
/// @param range 文字范围
void SetStrikethrough(bool enable, TextRange range);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 设置文字填充画刷,描边画刷和描边线宽
/// @param brush 画刷
/// @param range 文字范围
void SetFillBrush(BrushPtr brush, TextRange range);
/// \~chinese
/// @brief 设置文字描边画刷
/// @param brush 画刷
/// @param range 文字范围
void SetOutlineBrush(BrushPtr brush, TextRange range);
/// \~chinese
/// @brief 设置文字描边线宽
/// @param width 描边线宽
/// @param range 文字范围
void SetOutlineWidth(float width, TextRange range);
/// \~chinese
/// @brief 设置描边线条样式
/// @param stroke 线条样式
/// @param range 文字范围
void SetOutlineStrokeStyle(StrokeStylePtr stroke, TextRange range);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置对齐方式
2020-02-16 12:53:18 +08:00
/// @param align 对齐方式
2020-01-21 10:09:55 +08:00
void SetAlignment(TextAlign align);
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 设置文本自动换行的宽度
void SetWrapWidth(float wrap_width);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 设置行间距(默认为 0
void SetLineSpacing(float line_spacing);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 设置默认文字填充画刷
/// @param brush 画刷
void SetDefaultFillBrush(BrushPtr brush);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 设置默认文字描边画刷
/// @param brush 画刷
void SetDefaultOutlineBrush(BrushPtr brush);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 12:53:18 +08:00
/// @brief 设置默认文字描边线宽
/// @param width 描边线宽
void SetDefaultOutlineWidth(float width);
/// \~chinese
/// @brief 设置默认描边线条样式
/// @param stroke 线条样式
void SetDefaultOutlineStrokeStyle(StrokeStylePtr stroke);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 脏数据标志
2020-02-16 12:53:18 +08:00
enum class DirtyFlag : uint8_t
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
Clean = 0, ///< 干净布局
Dirty = 1 << 0, ///< 脏布局
Updated = 1 << 1, ///< 已更新
2020-01-21 10:09:55 +08:00
};
2020-02-16 12:53:18 +08:00
DirtyFlag GetDirtyFlag() const;
2020-01-21 10:09:55 +08:00
2020-02-16 12:53:18 +08:00
void SetDirtyFlag(DirtyFlag flag);
2020-01-21 10:09:55 +08:00
private:
2020-02-16 12:53:18 +08:00
DirtyFlag dirty_flag_;
BrushPtr default_fill_brush_;
BrushPtr default_outline_brush_;
float default_outline_width_;
StrokeStylePtr default_outline_stroke_;
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
public:
2020-01-21 10:09:55 +08:00
ComPtr<IDWriteTextLayout> GetTextLayout() const;
void SetTextLayout(ComPtr<IDWriteTextLayout> layout);
private:
2020-01-21 10:09:55 +08:00
ComPtr<IDWriteTextLayout> text_layout_;
2020-02-07 20:50:27 +08:00
#endif
2020-01-21 10:09:55 +08:00
};
/** @} */
inline bool TextLayout::IsValid() const
{
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-01-21 10:09:55 +08:00
return text_layout_ != nullptr;
2020-02-14 22:59:29 +08:00
#else
return false; // not supported
#endif
2020-01-21 10:09:55 +08:00
}
inline bool TextLayout::IsDirty() const
{
return dirty_flag_ != DirtyFlag::Clean;
}
2020-02-16 12:53:18 +08:00
inline void TextLayout::Clear()
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
text_layout_ = nullptr;
dirty_flag_ = DirtyFlag::Updated;
#else
return; // not supported
#endif
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
inline TextLayout::DirtyFlag TextLayout::GetDirtyFlag() const
2020-01-21 10:09:55 +08:00
{
return dirty_flag_;
}
2020-02-16 12:53:18 +08:00
inline void TextLayout::SetDirtyFlag(TextLayout::DirtyFlag flag)
2020-01-21 10:09:55 +08:00
{
dirty_flag_ = flag;
}
2020-02-16 12:53:18 +08:00
inline BrushPtr TextLayout::GetDefaultFillBrush() const
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
return default_fill_brush_;
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
inline BrushPtr TextLayout::GetDefaultOutlineBrush() const
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
return default_outline_brush_;
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
inline float TextLayout::GetDefaultOutlineWidth() const
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
return default_outline_width_;
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
inline StrokeStylePtr TextLayout::GetDefaultOutlineStrokeStyle() const
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
return default_outline_stroke_;
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
inline void TextLayout::SetDefaultFillBrush(BrushPtr brush)
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
default_fill_brush_ = brush;
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
inline void TextLayout::SetDefaultOutlineBrush(BrushPtr brush)
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
default_outline_brush_ = brush;
2019-08-15 11:22:51 +08:00
}
2020-02-07 20:50:27 +08:00
2020-02-16 12:53:18 +08:00
inline void TextLayout::SetDefaultOutlineWidth(float width)
2020-02-07 20:50:27 +08:00
{
2020-02-16 12:53:18 +08:00
default_outline_width_ = width;
2020-02-07 20:50:27 +08:00
}
2020-02-16 12:53:18 +08:00
inline void TextLayout::SetDefaultOutlineStrokeStyle(StrokeStylePtr stroke)
2020-02-07 20:50:27 +08:00
{
2020-02-16 12:53:18 +08:00
default_outline_stroke_ = stroke;
2020-02-07 20:50:27 +08:00
}
2020-02-16 12:53:18 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
inline ComPtr<IDWriteTextLayout> TextLayout::GetTextLayout() const
2020-02-07 20:50:27 +08:00
{
2020-02-16 12:53:18 +08:00
return text_layout_;
2020-02-07 20:50:27 +08:00
}
inline void TextLayout::SetTextLayout(ComPtr<IDWriteTextLayout> layout)
{
text_layout_ = layout;
}
#endif
2020-01-21 10:09:55 +08:00
} // namespace kiwano