Magic_Game/src/kiwano/render/TextLayout.cpp

457 lines
12 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
2020-02-16 20:14:01 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
#include <kiwano/render/DirectX/NativePtr.h>
#endif
2019-08-15 11:22:51 +08:00
namespace kiwano
{
2020-02-17 17:01:12 +08:00
2020-02-16 12:53:18 +08:00
TextLayoutPtr TextLayout::Create()
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
TextLayoutPtr ptr = new (std::nothrow) TextLayout;
return ptr;
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
TextLayoutPtr TextLayout::Create(const String& content, const TextStyle& style)
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
TextLayoutPtr ptr = new (std::nothrow) TextLayout;
if (ptr)
{
ptr->Reset(content, style);
}
return ptr;
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
TextLayout::TextLayout()
: dirty_flag_(DirtyFlag::Clean)
2020-02-17 12:06:29 +08:00
, line_count_(0)
2020-01-21 10:09:55 +08:00
{
}
2019-08-15 11:22:51 +08:00
2020-02-17 12:06:29 +08:00
void TextLayout::Reset(const String& content, const TextStyle& style)
2020-01-21 10:09:55 +08:00
{
2020-02-17 17:01:12 +08:00
content_ = content;
if (!content_.empty())
2020-02-16 12:53:18 +08:00
{
2020-02-17 17:01:12 +08:00
Renderer::GetInstance().CreateTextLayout(*this, content_, style);
2020-02-16 12:53:18 +08:00
SetAlignment(style.alignment);
SetWrapWidth(style.wrap_width);
SetLineSpacing(style.line_spacing);
2020-02-16 20:14:01 +08:00
2020-02-16 12:53:18 +08:00
SetDefaultFillBrush(style.fill_brush);
SetDefaultOutlineBrush(style.outline_brush);
SetDefaultOutlineStrokeStyle(style.outline_stroke);
2020-02-16 20:14:01 +08:00
if (style.show_underline)
2020-02-17 17:01:12 +08:00
SetUnderline(style.show_underline, { 0, content_.length() });
2020-02-16 20:14:01 +08:00
if (style.show_strikethrough)
2020-02-17 17:01:12 +08:00
SetStrikethrough(style.show_strikethrough, { 0, content_.length() });
2020-02-16 12:53:18 +08:00
}
else
{
Clear();
}
2020-02-17 12:06:29 +08:00
SetDirtyFlag(DirtyFlag::Dirty);
2020-01-21 10:09:55 +08:00
}
2020-02-17 12:06:29 +08:00
Size TextLayout::GetSize() const
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
const_cast<TextLayout*>(this)->UpdateWhenDirty();
return size_;
2019-08-15 11:22:51 +08:00
}
2020-01-21 10:09:55 +08:00
2020-02-17 12:06:29 +08:00
uint32_t TextLayout::GetLineCount() const
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
const_cast<TextLayout*>(this)->UpdateWhenDirty();
return line_count_;
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
void TextLayout::SetFont(FontPtr font, TextRange range)
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
KGE_ASSERT(content_.size() >= (range.start + range.length));
2020-02-17 17:01:12 +08:00
if (range.length == 0)
return;
2020-02-17 12:06:29 +08:00
2020-02-16 12:53:18 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native);
if (native)
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
auto collection = NativePtr::Get<IDWriteFontCollection>(font);
2020-02-16 12:53:18 +08:00
2020-02-16 20:14:01 +08:00
HRESULT hr = native->SetFontCollection(collection.Get(), { range.start, range.length });
2020-02-16 12:53:18 +08:00
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontCollection failed");
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
#else
// not supported
#endif
2020-02-17 12:06:29 +08:00
SetDirtyFlag(DirtyFlag::Dirty);
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
void TextLayout::SetFontFamily(String const& family, TextRange range)
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
KGE_ASSERT(content_.size() >= (range.start + range.length));
2020-02-17 17:01:12 +08:00
if (range.length == 0)
return;
2020-02-17 12:06:29 +08:00
2020-02-16 12:53:18 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native);
if (native)
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
WideString font_family = family.empty() ? L"" : string::ToWide(family);
2020-02-16 20:14:01 +08:00
HRESULT hr = native->SetFontFamilyName(font_family.c_str(), { range.start, range.length });
2020-02-16 12:53:18 +08:00
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontFamilyName failed");
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
#else
// not supported
#endif
2020-02-17 12:06:29 +08:00
SetDirtyFlag(DirtyFlag::Dirty);
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
void TextLayout::SetFontSize(float size, TextRange range)
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
KGE_ASSERT(content_.size() >= (range.start + range.length));
2020-02-17 17:01:12 +08:00
if (range.length == 0)
return;
2020-02-17 12:06:29 +08:00
2020-02-16 12:53:18 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native);
if (native)
2020-01-21 10:09:55 +08:00
{
2020-02-16 20:14:01 +08:00
HRESULT hr = native->SetFontSize(size, { range.start, range.length });
2020-02-16 12:53:18 +08:00
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontSize failed");
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
#else
// not supported
#endif
2020-02-17 12:06:29 +08:00
SetDirtyFlag(DirtyFlag::Dirty);
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
void TextLayout::SetFontWeight(uint32_t weight, TextRange range)
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
KGE_ASSERT(content_.size() >= (range.start + range.length));
2020-02-17 17:01:12 +08:00
if (range.length == 0)
return;
2020-02-17 12:06:29 +08:00
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native);
if (native)
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
DWRITE_FONT_WEIGHT font_weight = DWRITE_FONT_WEIGHT(weight);
2020-02-16 20:14:01 +08:00
HRESULT hr = native->SetFontWeight(font_weight, { range.start, range.length });
2020-02-16 12:53:18 +08:00
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontWeight failed");
2020-01-21 10:09:55 +08:00
}
2020-02-14 22:59:29 +08:00
#else
// not supported
#endif
2020-02-17 12:06:29 +08:00
SetDirtyFlag(DirtyFlag::Dirty);
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
void TextLayout::SetItalic(bool italic, TextRange range)
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
KGE_ASSERT(content_.size() >= (range.start + range.length));
2020-02-17 17:01:12 +08:00
if (range.length == 0)
return;
2020-02-17 12:06:29 +08:00
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native);
if (native)
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
DWRITE_FONT_STYLE font_style = italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL;
2020-02-16 20:14:01 +08:00
HRESULT hr = native->SetFontStyle(font_style, { range.start, range.length });
2020-02-16 12:53:18 +08:00
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontStyle failed");
2020-01-21 10:09:55 +08:00
}
2020-02-14 22:59:29 +08:00
#else
// not supported
#endif
2020-02-17 12:06:29 +08:00
SetDirtyFlag(DirtyFlag::Dirty);
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
void TextLayout::SetUnderline(bool enable, TextRange range)
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
KGE_ASSERT(content_.size() >= (range.start + range.length));
2020-02-17 17:01:12 +08:00
if (range.length == 0)
return;
2020-02-17 12:06:29 +08:00
2020-02-16 12:53:18 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native);
if (native)
2020-01-21 10:09:55 +08:00
{
2020-02-16 20:14:01 +08:00
HRESULT hr = native->SetUnderline(enable, { range.start, range.length });
2020-02-16 12:53:18 +08:00
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetUnderline failed");
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
#else
2020-02-17 12:06:29 +08:00
// not supported
2020-02-16 12:53:18 +08:00
#endif
2020-02-17 12:06:29 +08:00
SetDirtyFlag(DirtyFlag::Dirty);
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
void TextLayout::SetStrikethrough(bool enable, TextRange range)
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
KGE_ASSERT(content_.size() >= (range.start + range.length));
2020-02-17 17:01:12 +08:00
if (range.length == 0)
return;
2020-02-17 12:06:29 +08:00
2020-02-16 12:53:18 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native);
if (native)
2020-01-21 10:09:55 +08:00
{
2020-02-16 20:14:01 +08:00
HRESULT hr = native->SetStrikethrough(enable, { range.start, range.length });
2020-02-16 12:53:18 +08:00
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetStrikethrough failed");
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
#else
2020-02-17 12:06:29 +08:00
// not supported
2020-02-16 12:53:18 +08:00
#endif
2020-02-17 12:06:29 +08:00
SetDirtyFlag(DirtyFlag::Dirty);
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
void TextLayout::SetFillBrush(BrushPtr brush, TextRange range)
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
KGE_ASSERT(content_.size() >= (range.start + range.length));
2020-02-17 17:01:12 +08:00
if (range.length == 0)
return;
2020-02-17 12:06:29 +08:00
2020-02-16 12:53:18 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native);
if (native)
2020-01-21 10:09:55 +08:00
{
2020-02-16 20:14:01 +08:00
HRESULT hr =
native->SetDrawingEffect(NativePtr::Get<ID2D1Brush>(brush).Get(), { range.start, range.length });
2020-02-16 12:53:18 +08:00
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetDrawingEffect failed");
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
#else
2020-02-17 12:06:29 +08:00
// not supported
2020-02-16 12:53:18 +08:00
#endif
2020-02-17 12:06:29 +08:00
SetDirtyFlag(DirtyFlag::Dirty);
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
void TextLayout::SetOutlineBrush(BrushPtr brush, TextRange range)
2020-01-21 10:09:55 +08:00
{
2020-02-17 12:06:29 +08:00
KGE_ASSERT(content_.size() >= (range.start + range.length));
2020-02-17 17:01:12 +08:00
if (range.length == 0)
return;
2020-02-17 12:06:29 +08:00
2020-02-16 12:53:18 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
// TODO
KGE_NOT_USED(range);
SetDefaultOutlineBrush(brush);
#else
return; // not supported
#endif
}
2020-01-21 10:09:55 +08:00
2020-02-16 12:53:18 +08:00
void TextLayout::SetOutlineStrokeStyle(StrokeStylePtr stroke, TextRange range)
{
2020-02-17 12:06:29 +08:00
KGE_ASSERT(content_.size() >= (range.start + range.length));
2020-02-17 17:01:12 +08:00
if (range.length == 0)
return;
2020-02-17 12:06:29 +08:00
2020-02-16 12:53:18 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
// TODO
KGE_NOT_USED(range);
SetDefaultOutlineStrokeStyle(stroke);
2020-02-14 22:59:29 +08:00
#else
return; // not supported
#endif
2020-01-21 10:09:55 +08:00
}
2020-02-16 12:53:18 +08:00
void TextLayout::SetAlignment(TextAlign align)
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native);
if (native)
2020-02-16 12:53:18 +08:00
{
DWRITE_TEXT_ALIGNMENT alignment = DWRITE_TEXT_ALIGNMENT();
switch (align)
{
case TextAlign::Left:
alignment = DWRITE_TEXT_ALIGNMENT_LEADING;
break;
case TextAlign::Right:
alignment = DWRITE_TEXT_ALIGNMENT_TRAILING;
break;
case TextAlign::Center:
alignment = DWRITE_TEXT_ALIGNMENT_CENTER;
break;
case TextAlign::Justified:
alignment = DWRITE_TEXT_ALIGNMENT_JUSTIFIED;
break;
}
2020-02-16 20:14:01 +08:00
HRESULT hr = native->SetTextAlignment(alignment);
2020-02-16 12:53:18 +08:00
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetTextAlignment failed");
}
#else
// not supported
#endif
2020-02-17 12:06:29 +08:00
SetDirtyFlag(DirtyFlag::Dirty);
2020-02-16 12:53:18 +08:00
}
void TextLayout::SetWrapWidth(float wrap_width)
{
2020-02-14 22:59:29 +08:00
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native);
if (native)
2020-02-16 12:53:18 +08:00
{
2020-02-17 12:06:29 +08:00
HRESULT hr = S_OK;
if (wrap_width > 0)
2020-02-16 12:53:18 +08:00
{
2020-02-17 12:06:29 +08:00
hr = native->SetWordWrapping(DWRITE_WORD_WRAPPING_WRAP);
if (SUCCEEDED(hr))
2020-02-16 12:53:18 +08:00
{
2020-02-16 20:14:01 +08:00
hr = native->SetMaxWidth(wrap_width);
2020-02-16 12:53:18 +08:00
}
2020-02-17 12:06:29 +08:00
}
else
{
hr = native->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
2020-02-16 12:53:18 +08:00
}
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetWordWrapping failed");
}
#else
2020-02-17 12:06:29 +08:00
// not supported
2020-02-16 12:53:18 +08:00
#endif
2020-02-17 12:06:29 +08:00
SetDirtyFlag(DirtyFlag::Dirty);
2020-02-16 12:53:18 +08:00
}
void TextLayout::SetLineSpacing(float line_spacing)
{
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
2020-02-16 20:14:01 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
KGE_ASSERT(native);
if (native)
2020-01-21 10:09:55 +08:00
{
2020-02-16 12:53:18 +08:00
HRESULT hr = S_OK;
float spacing = line_spacing;
if (spacing == 0.f)
{
2020-02-16 20:14:01 +08:00
hr = native->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_DEFAULT, 0, 0);
2020-02-16 12:53:18 +08:00
}
else
{
2020-02-16 20:14:01 +08:00
hr = native->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_UNIFORM, spacing, spacing * 0.8f);
2020-02-16 12:53:18 +08:00
}
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetLineSpacing failed");
2020-02-17 12:06:29 +08:00
}
#else
// not supported
#endif
SetDirtyFlag(DirtyFlag::Dirty);
}
bool TextLayout::UpdateWhenDirty()
{
#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
if (dirty_flag_ == DirtyFlag::Dirty)
{
SetDirtyFlag(DirtyFlag::Clean);
2020-02-17 17:01:12 +08:00
line_count_ = 0;
size_ = Size();
2020-02-17 12:06:29 +08:00
auto native = NativePtr::Get<IDWriteTextLayout>(this);
2020-02-17 17:01:12 +08:00
if (content_.empty() || !native)
return true;
2020-02-16 12:53:18 +08:00
2020-02-17 12:06:29 +08:00
HRESULT hr = S_OK;
DWRITE_TEXT_METRICS metrics;
hr = native->GetMetrics(&metrics);
if (SUCCEEDED(hr))
{
if (native->GetWordWrapping() == DWRITE_WORD_WRAPPING_NO_WRAP)
{
// Fix the layout width when the text does not wrap
hr = native->SetMaxWidth(metrics.widthIncludingTrailingWhitespace);
if (SUCCEEDED(hr))
{
hr = native->GetMetrics(&metrics);
}
}
}
if (SUCCEEDED(hr))
{
line_count_ = metrics.lineCount;
if (metrics.layoutWidth > 0)
{
size_ = Size(metrics.layoutWidth, metrics.height);
}
else
{
size_ = Size(metrics.widthIncludingTrailingWhitespace, metrics.height);
}
}
KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::GetMetrics failed");
return true;
2020-01-21 10:09:55 +08:00
}
2020-02-14 22:59:29 +08:00
#else
2020-02-17 12:06:29 +08:00
// not supported
2020-02-14 22:59:29 +08:00
#endif
2020-02-17 12:06:29 +08:00
return false;
2020-01-21 10:09:55 +08:00
}
} // namespace kiwano