Magic_Game/src/kiwano/render/TextLayout.h

273 lines
6.6 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-01-17 16:55:47 +08:00
#include <kiwano/render/TextStyle.hpp>
2019-08-15 11:22:51 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
/**
* \addtogroup Render
* @{
*/
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 文本布局
2020-02-11 12:09:59 +08:00
class KGE_API TextLayout : public Noncopyable
2020-01-21 10:09:55 +08:00
{
public:
/// \~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-10 17:32:04 +08:00
/// @brief 更新文本布局
/// @note 文本布局是懒更新的,在修改文本布局的属性后需要手动更新
2020-01-21 10:09:55 +08:00
void Update();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取文本
2020-01-21 10:09:55 +08:00
const String& GetText() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取文本样式
2020-01-21 10:09:55 +08:00
const TextStyle& GetStyle() const;
/// \~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-10 17:32:04 +08:00
/// @brief 获取填充画刷
2020-01-21 10:09:55 +08:00
BrushPtr GetFillBrush() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取描边画刷
2020-01-21 10:09:55 +08:00
BrushPtr GetOutlineBrush() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文本
2020-01-21 10:09:55 +08:00
void SetText(const String& text);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文本样式
2020-01-21 10:09:55 +08:00
void SetStyle(const TextStyle& style);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字体
2020-01-21 10:09:55 +08:00
void SetFont(FontPtr font);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字体族
2020-01-21 10:09:55 +08:00
void SetFontFamily(String const& family);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字号(默认值为 18
2020-01-21 10:09:55 +08:00
void SetFontSize(float size);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字体粗细值(默认值为 FontWeight::Normal
2020-01-21 10:09:55 +08:00
void SetFontWeight(uint32_t weight);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字填充画刷
2020-01-21 10:09:55 +08:00
void SetFillBrush(BrushPtr brush);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字斜体(默认值为 false
2020-01-21 10:09:55 +08:00
void SetItalic(bool italic);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文本自动换行的宽度
2020-01-21 10:09:55 +08:00
void SetWrapWidth(float wrap_width);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置行间距(默认为 0
2020-01-21 10:09:55 +08:00
void SetLineSpacing(float line_spacing);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置对齐方式
2020-01-21 10:09:55 +08:00
void SetAlignment(TextAlign align);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字描边画刷
2020-01-21 10:09:55 +08:00
void SetOutlineBrush(BrushPtr brush);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字描边线宽
2020-01-21 10:09:55 +08:00
void SetOutlineWidth(float outline_width);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字描边线相交样式
2020-02-06 16:54:47 +08:00
void SetOutlineStroke(StrokeStylePtr outline_stroke);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置下划线
/// @param enable 是否显示下划线
/// @param start 起始位置
/// @param length 长度
2020-01-21 10:09:55 +08:00
void SetUnderline(bool enable, uint32_t start, uint32_t length);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置删除线
/// @param enable 是否显示删除线
/// @param start 起始位置
/// @param length 长度
2020-01-21 10:09:55 +08:00
void SetStrikethrough(bool enable, uint32_t start, uint32_t length);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 脏数据标志
2020-01-21 10:09:55 +08:00
enum DirtyFlag : uint8_t
{
2020-02-10 17:32:04 +08:00
Clean = 0, ///< 干净数据
DirtyFormat = 1, ///< 文字格式待更新
DirtyLayout = 1 << 1, ///< 文字布局待更新
Updated = 1 << 2, ///< 数据已更新
2020-01-21 10:09:55 +08:00
};
uint8_t GetDirtyFlag() const;
void SetDirtyFlag(uint8_t flag);
private:
uint8_t dirty_flag_;
String text_;
TextStyle style_;
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<IDWriteTextFormat> GetTextFormat() const;
void SetTextFormat(ComPtr<IDWriteTextFormat> format);
ComPtr<IDWriteTextLayout> GetTextLayout() const;
void SetTextLayout(ComPtr<IDWriteTextLayout> layout);
private:
2020-01-21 10:09:55 +08:00
ComPtr<IDWriteTextFormat> text_format_;
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;
}
inline const String& TextLayout::GetText() const
{
return text_;
}
inline const TextStyle& TextLayout::GetStyle() const
{
return style_;
}
inline uint8_t TextLayout::GetDirtyFlag() const
{
return dirty_flag_;
}
inline void TextLayout::SetDirtyFlag(uint8_t flag)
{
dirty_flag_ = flag;
}
inline BrushPtr TextLayout::GetFillBrush() const
{
return style_.fill_brush;
}
inline BrushPtr TextLayout::GetOutlineBrush() const
{
return style_.outline_brush;
}
inline void TextLayout::SetFillBrush(BrushPtr brush)
{
style_.fill_brush = brush;
}
inline void TextLayout::SetOutlineBrush(BrushPtr brush)
{
style_.outline_brush = brush;
}
inline void TextLayout::SetOutlineWidth(float outline_width)
{
style_.outline_width = outline_width;
}
2020-02-06 16:54:47 +08:00
inline void TextLayout::SetOutlineStroke(StrokeStylePtr outline_stroke)
2020-01-21 10:09:55 +08:00
{
style_.outline_stroke = outline_stroke;
2019-08-15 11:22:51 +08:00
}
2020-02-07 20:50:27 +08:00
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-07 20:50:27 +08:00
inline ComPtr<IDWriteTextFormat> TextLayout::GetTextFormat() const
{
return text_format_;
}
inline ComPtr<IDWriteTextLayout> TextLayout::GetTextLayout() const
{
return text_layout_;
}
inline void TextLayout::SetTextFormat(ComPtr<IDWriteTextFormat> format)
{
text_format_ = format;
}
inline void TextLayout::SetTextLayout(ComPtr<IDWriteTextLayout> layout)
{
text_layout_ = layout;
}
#endif
2020-01-21 10:09:55 +08:00
} // namespace kiwano