Magic_Game/src/kiwano/render/TextLayout.cpp

201 lines
4.9 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.
2020-01-17 16:55:47 +08:00
#include <kiwano/render/Renderer.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/render/TextLayout.h>
2019-08-15 11:22:51 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
TextLayout::TextLayout()
: dirty_flag_(DirtyFlag::Clean)
{
}
void TextLayout::Update()
{
if (!IsDirty())
return;
2020-02-14 22:59:29 +08:00
Renderer::GetInstance().CreateTextLayout(*this);
2020-01-21 10:09:55 +08:00
}
void TextLayout::SetText(const String& text)
{
text_ = text;
dirty_flag_ |= DirtyFlag::DirtyLayout;
}
2019-08-15 11:22:51 +08:00
2020-01-21 10:09:55 +08:00
void TextLayout::SetStyle(const TextStyle& style)
{
style_ = style;
dirty_flag_ |= DirtyFlag::DirtyLayout;
}
void TextLayout::SetFont(FontPtr font)
{
if (style_.font != font)
{
style_.font = font;
dirty_flag_ |= DirtyFlag::DirtyFormat;
}
2019-08-15 11:22:51 +08:00
}
2020-01-21 10:09:55 +08:00
void TextLayout::SetFontFamily(String const& family)
{
if (style_.font_family != family)
{
style_.font_family = family;
dirty_flag_ |= DirtyFlag::DirtyFormat;
}
}
void TextLayout::SetFontSize(float size)
{
if (style_.font_size != size)
{
style_.font_size = size;
dirty_flag_ |= DirtyFlag::DirtyFormat;
}
}
void TextLayout::SetFontWeight(uint32_t weight)
{
if (style_.font_weight != weight)
{
style_.font_weight = weight;
dirty_flag_ |= DirtyFlag::DirtyFormat;
}
}
void TextLayout::SetItalic(bool italic)
{
if (style_.italic != italic)
{
style_.italic = italic;
dirty_flag_ |= DirtyFlag::DirtyFormat;
}
}
uint32_t TextLayout::GetLineCount() const
{
// Force to update layout
const_cast<TextLayout*>(this)->Update();
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-01-21 10:09:55 +08:00
if (text_layout_)
{
DWRITE_TEXT_METRICS metrics;
if (SUCCEEDED(GetTextLayout()->GetMetrics(&metrics)))
{
return metrics.lineCount;
}
}
2020-02-14 22:59:29 +08:00
#else
// not supported
#endif
2020-01-21 10:09:55 +08:00
return 0;
}
Size TextLayout::GetLayoutSize() const
{
// Force to update layout
const_cast<TextLayout*>(this)->Update();
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-01-21 10:09:55 +08:00
if (text_layout_)
{
DWRITE_TEXT_METRICS metrics;
if (SUCCEEDED(GetTextLayout()->GetMetrics(&metrics)))
{
return (metrics.layoutWidth > 0) ? Size(metrics.layoutWidth, metrics.height)
: Size(metrics.width, metrics.height);
}
}
2020-02-14 22:59:29 +08:00
#else
// not supported
#endif
2020-01-21 10:09:55 +08:00
return Size();
}
void TextLayout::SetWrapWidth(float wrap_width)
{
2020-02-15 17:32:32 +08:00
if (style_.wrap_width != wrap_width)
2020-01-21 10:09:55 +08:00
{
2020-02-15 17:32:32 +08:00
style_.wrap_width = wrap_width;
dirty_flag_ |= DirtyFlag::DirtyLayout;
2020-01-21 10:09:55 +08:00
}
}
void TextLayout::SetLineSpacing(float line_spacing)
{
2020-02-15 17:32:32 +08:00
if (style_.line_spacing != line_spacing)
2020-01-21 10:09:55 +08:00
{
2020-02-15 17:32:32 +08:00
style_.line_spacing = line_spacing;
dirty_flag_ |= DirtyFlag::DirtyLayout;
2020-01-21 10:09:55 +08:00
}
}
void TextLayout::SetAlignment(TextAlign align)
{
2020-02-15 17:32:32 +08:00
if (style_.alignment != align)
2020-01-21 10:09:55 +08:00
{
2020-02-15 17:32:32 +08:00
style_.alignment = align;
dirty_flag_ |= DirtyFlag::DirtyLayout;
2020-01-21 10:09:55 +08:00
}
}
void TextLayout::SetUnderline(bool enable, uint32_t start, uint32_t length)
{
// Force to update layout
Update();
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-01-21 10:09:55 +08:00
HRESULT hr = text_layout_ ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
hr = text_layout_->SetUnderline(enable, { start, length });
}
2020-02-15 17:32:32 +08:00
KGE_THROW_IF_FAILED(hr, "Apply underline style to text layout failed");
2020-02-14 22:59:29 +08:00
#else
return; // not supported
#endif
2020-01-21 10:09:55 +08:00
}
void TextLayout::SetStrikethrough(bool enable, uint32_t start, uint32_t length)
{
// Force to update layout
Update();
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-01-21 10:09:55 +08:00
HRESULT hr = text_layout_ ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
hr = text_layout_->SetStrikethrough(enable, { start, length });
}
2020-02-15 17:32:32 +08:00
KGE_THROW_IF_FAILED(hr, "Apply strikethrough style to text layout failed");
2020-02-14 22:59:29 +08:00
#else
return; // not supported
#endif
2020-01-21 10:09:55 +08:00
}
} // namespace kiwano