Magic_Game/core/objects/Text.cpp

470 lines
8.9 KiB
C++
Raw Normal View History

2018-10-03 22:02:46 +08:00
// Copyright (c) 2016-2018 Easy2D - Nomango
//
// 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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.
2018-09-06 23:26:32 +08:00
#include "..\e2dobject.h"
2018-09-05 13:17:07 +08:00
#include "..\e2dmodule.h"
//-------------------------------------------------------
// Style
//-------------------------------------------------------
2018-10-16 14:13:15 +08:00
easy2d::Text::Style::Style()
2018-05-24 20:37:34 +08:00
: color(Color::White)
, alignment(Align::Left)
2018-09-04 22:42:34 +08:00
, wrap(false)
, wrap_width(0.f)
, line_spacing(0.f)
, underline(false)
, strikethrough(false)
, outline(true)
, outline_color(Color(Color::Black, 0.5))
, outline_width(1.f)
, outline_stroke(Stroke::Round)
{}
2018-10-16 14:13:15 +08:00
easy2d::Text::Style::Style(
Color color,
Align alignment,
2018-09-04 22:42:34 +08:00
bool wrap,
float wrap_width,
float line_spacing,
bool underline,
bool strikethrough,
bool outline,
Color outline_color,
float outline_width,
Stroke outline_stroke
)
: color(color)
, alignment(alignment)
2018-09-04 22:42:34 +08:00
, wrap(wrap)
, wrap_width(wrap_width)
, line_spacing(line_spacing)
, underline(underline)
, strikethrough(strikethrough)
, outline(outline)
, outline_color(outline_color)
, outline_width(outline_width)
, outline_stroke(outline_stroke)
{}
//-------------------------------------------------------
// Text
//-------------------------------------------------------
2018-10-16 14:13:15 +08:00
easy2d::Text::Text()
2018-09-04 22:42:34 +08:00
: font_()
, style_()
, text_layout_(nullptr)
, text_format_(nullptr)
{
}
2018-10-16 14:13:15 +08:00
easy2d::Text::Text(const String & text, const Font & font, const Style & style)
2018-09-04 22:42:34 +08:00
: font_(font)
, style_(style)
, text_layout_(nullptr)
, text_format_(nullptr)
, text_(text)
{
2018-09-04 22:42:34 +08:00
Reset();
}
2018-10-16 14:13:15 +08:00
easy2d::Text::~Text()
{
2018-09-04 22:42:34 +08:00
SafeRelease(text_format_);
SafeRelease(text_layout_);
}
2018-10-16 14:13:15 +08:00
const easy2d::String& easy2d::Text::GetText() const
{
2018-09-04 22:42:34 +08:00
return text_;
}
2018-10-16 14:13:15 +08:00
const easy2d::Font& easy2d::Text::GetFont() const
{
2018-09-04 22:42:34 +08:00
return font_;
}
2018-10-16 14:13:15 +08:00
const easy2d::Text::Style& easy2d::Text::GetStyle() const
{
2018-09-04 22:42:34 +08:00
return style_;
}
2018-10-16 14:13:15 +08:00
const easy2d::String& easy2d::Text::GetFontFamily() const
{
2018-09-04 22:42:34 +08:00
return font_.family;
}
2018-10-16 14:13:15 +08:00
float easy2d::Text::GetFontSize() const
{
2018-09-04 22:42:34 +08:00
return font_.size;
}
2018-10-16 14:13:15 +08:00
UINT easy2d::Text::GetFontWeight() const
{
2018-09-04 22:42:34 +08:00
return font_.weight;
}
2018-10-16 14:13:15 +08:00
const easy2d::Color& easy2d::Text::GetColor() const
{
2018-09-04 22:42:34 +08:00
return style_.color;
}
2018-10-16 14:13:15 +08:00
const easy2d::Color& easy2d::Text::GetOutlineColor() const
{
2018-09-04 22:42:34 +08:00
return style_.outline_color;
}
2018-10-16 14:13:15 +08:00
float easy2d::Text::GetOutlineWidth() const
{
2018-09-04 22:42:34 +08:00
return style_.outline_width;
}
2018-10-16 14:13:15 +08:00
easy2d::Stroke easy2d::Text::GetOutlineStroke() const
{
2018-09-04 22:42:34 +08:00
return style_.outline_stroke;
}
2018-10-16 14:13:15 +08:00
int easy2d::Text::GetLineCount() const
{
2018-09-04 22:42:34 +08:00
if (text_layout_)
2018-04-09 18:41:56 +08:00
{
DWRITE_TEXT_METRICS metrics;
2018-09-04 22:42:34 +08:00
text_layout_->GetMetrics(&metrics);
2018-04-09 18:41:56 +08:00
return static_cast<int>(metrics.lineCount);
}
else
{
return 0;
}
}
2018-10-16 14:13:15 +08:00
bool easy2d::Text::IsItalic() const
{
2018-09-04 22:42:34 +08:00
return font_.italic;
}
2018-10-16 14:13:15 +08:00
bool easy2d::Text::strikethrough() const
{
2018-09-04 22:42:34 +08:00
return style_.strikethrough;
2018-04-22 14:08:29 +08:00
}
2018-10-16 14:13:15 +08:00
bool easy2d::Text::underline() const
2018-04-22 14:08:29 +08:00
{
2018-09-04 22:42:34 +08:00
return style_.underline;
2018-04-22 14:08:29 +08:00
}
2018-10-16 14:13:15 +08:00
bool easy2d::Text::outline() const
2018-04-22 14:08:29 +08:00
{
2018-09-04 22:42:34 +08:00
return style_.outline;
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetText(const String& text)
{
2018-09-04 22:42:34 +08:00
text_ = text;
Reset();
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetStyle(const Style& style)
{
2018-09-04 22:42:34 +08:00
style_ = style;
Reset();
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetFont(const Font & font)
{
2018-09-04 22:42:34 +08:00
font_ = font;
Reset();
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetFontFamily(const String& family)
{
2018-09-04 22:42:34 +08:00
font_.family = family;
Reset();
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetFontSize(float size)
{
2018-09-04 22:42:34 +08:00
font_.size = size;
Reset();
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetFontWeight(UINT weight)
{
2018-09-04 22:42:34 +08:00
font_.weight = weight;
Reset();
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetColor(Color color)
{
2018-09-04 22:42:34 +08:00
style_.color = color;
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetItalic(bool value)
{
2018-09-04 22:42:34 +08:00
font_.italic = value;
Reset();
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetWrapEnabled(bool wrap)
{
2018-09-04 22:42:34 +08:00
if (style_.wrap != wrap)
{
2018-09-04 22:42:34 +08:00
style_.wrap = wrap;
Reset();
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetWrapWidth(float wrap_width)
{
2018-09-04 22:42:34 +08:00
if (style_.wrap_width != wrap_width)
{
2018-09-04 22:42:34 +08:00
style_.wrap_width = std::max(wrap_width, 0.f);
2018-09-04 22:42:34 +08:00
if (style_.wrap)
{
2018-09-04 22:42:34 +08:00
Reset();
}
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetLineSpacing(float line_spacing)
{
2018-09-04 22:42:34 +08:00
if (style_.line_spacing != line_spacing)
{
2018-09-04 22:42:34 +08:00
style_.line_spacing = line_spacing;
Reset();
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetAlignment(Align align)
{
2018-09-04 22:42:34 +08:00
if (style_.alignment != align)
{
2018-09-04 22:42:34 +08:00
style_.alignment = align;
Reset();
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetUnderline(bool underline)
{
2018-09-04 22:42:34 +08:00
if (style_.underline != underline)
{
2018-09-04 22:42:34 +08:00
style_.underline = underline;
if (!text_format_)
CreateFormat();
CreateLayout();
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetStrikethrough(bool strikethrough)
{
2018-09-04 22:42:34 +08:00
if (style_.strikethrough != strikethrough)
{
2018-09-04 22:42:34 +08:00
style_.strikethrough = strikethrough;
if (!text_format_)
CreateFormat();
CreateLayout();
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetOutline(bool outline)
{
2018-09-04 22:42:34 +08:00
style_.outline = outline;
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetOutlineColor(Color outline_color)
{
2018-09-04 22:42:34 +08:00
style_.outline_color = outline_color;
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetOutlineWidth(float outline_width)
{
2018-09-04 22:42:34 +08:00
style_.outline_width = outline_width;
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::SetOutlineStroke(Stroke outline_stroke)
{
2018-09-04 22:42:34 +08:00
style_.outline_stroke = outline_stroke;
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::Draw() const
{
2018-09-04 22:42:34 +08:00
if (text_layout_)
{
2018-10-03 18:04:04 +08:00
auto graphics = Device::GetGraphics();
// <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
D2D1_RECT_F textLayoutRect = D2D1::RectF(0, 0, transform_.size.width, transform_.size.height);
// <20><><EFBFBD>û<EFBFBD>ˢ<EFBFBD><CBA2>ɫ<EFBFBD><C9AB>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
2018-09-30 14:54:43 +08:00
graphics->GetSolidBrush()->SetOpacity(display_opacity_);
// <20><>ȡ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD>Ⱦ<EFBFBD><C8BE>
2018-10-03 22:02:46 +08:00
auto text_renderer = graphics->GetTextRender();
text_renderer->SetTextStyle(
2018-09-04 22:42:34 +08:00
(D2D1_COLOR_F)style_.color,
style_.outline,
(D2D1_COLOR_F)style_.outline_color,
style_.outline_width,
D2D1_LINE_JOIN(style_.outline_stroke)
);
2018-10-03 22:02:46 +08:00
text_layout_->Draw(nullptr, text_renderer, 0, 0);
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::Reset()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>ʽ<EFBFBD><CABD>
2018-09-04 22:42:34 +08:00
CreateFormat();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD>
2018-09-04 22:42:34 +08:00
CreateLayout();
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::CreateFormat()
{
2018-09-04 22:42:34 +08:00
SafeRelease(text_format_);
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
2018-10-03 18:04:04 +08:00
Device::GetGraphics()->GetWriteFactory()->CreateTextFormat(
(const wchar_t *)font_.family,
2018-08-28 00:06:10 +08:00
nullptr,
2018-09-04 22:42:34 +08:00
DWRITE_FONT_WEIGHT(font_.weight),
font_.italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL,
2018-08-28 00:06:10 +08:00
DWRITE_FONT_STRETCH_NORMAL,
2018-09-04 22:42:34 +08:00
font_.size,
2018-08-28 00:06:10 +08:00
L"",
2018-09-04 22:42:34 +08:00
&text_format_
2018-08-28 00:06:10 +08:00
)
);
2018-08-28 00:06:10 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6>ʽ
2018-09-04 22:42:34 +08:00
text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT(style_.alignment));
2018-08-28 00:06:10 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>
2018-09-04 22:42:34 +08:00
if (style_.line_spacing == 0.f)
2018-05-24 12:24:39 +08:00
{
2018-09-04 22:42:34 +08:00
text_format_->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_DEFAULT, 0, 0);
2018-05-24 12:24:39 +08:00
}
else
{
2018-09-04 22:42:34 +08:00
text_format_->SetLineSpacing(
2018-08-28 00:06:10 +08:00
DWRITE_LINE_SPACING_METHOD_UNIFORM,
2018-09-04 22:42:34 +08:00
style_.line_spacing,
style_.line_spacing * 0.8f
2018-08-28 00:06:10 +08:00
);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
if (style_.wrap)
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
text_format_->SetWordWrapping(DWRITE_WORD_WRAPPING_WRAP);
2018-08-28 00:06:10 +08:00
}
else
{
2018-09-04 22:42:34 +08:00
text_format_->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Text::CreateLayout()
{
2018-09-04 22:42:34 +08:00
SafeRelease(text_layout_);
// <20>ı<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
if (text_.IsEmpty())
{
2018-09-04 22:42:34 +08:00
this->SetSize(0, 0);
return;
}
2018-09-04 22:42:34 +08:00
if (text_format_ == nullptr)
{
2018-09-04 22:42:34 +08:00
WARN("Text::CreateLayout failed! text_format_ NULL pointer exception.");
return;
}
2018-10-16 14:57:28 +08:00
UINT32 length = (UINT32)text_.Length();
2018-10-03 18:04:04 +08:00
auto writeFactory = Device::GetGraphics()->GetWriteFactory();
// <20><><EFBFBD>ı<EFBFBD><C4B1>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD>д<EFBFBD><D0B4><EFBFBD>
2018-09-04 22:42:34 +08:00
if (style_.wrap)
{
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
writeFactory->CreateTextLayout(
(const wchar_t *)text_,
2018-08-28 00:06:10 +08:00
length,
2018-09-04 22:42:34 +08:00
text_format_,
style_.wrap_width,
2018-08-28 00:06:10 +08:00
0,
2018-09-04 22:42:34 +08:00
&text_layout_
2018-08-28 00:06:10 +08:00
)
);
2018-08-28 00:06:10 +08:00
// <20><>ȡ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ֵĿ<D6B5><C4BF>Ⱥ͸߶<CDB8>
DWRITE_TEXT_METRICS metrics;
2018-09-04 22:42:34 +08:00
text_layout_->GetMetrics(&metrics);
2018-08-28 00:06:10 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
this->SetSize(metrics.layoutWidth, metrics.height);
}
else
{
2018-08-28 00:06:10 +08:00
// Ϊ<><CEAA>ֹ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD> layout <20>Ի<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
ThrowIfFailed(
writeFactory->CreateTextLayout(
(const wchar_t *)text_,
2018-08-28 00:06:10 +08:00
length,
2018-09-04 22:42:34 +08:00
text_format_,
2018-08-28 00:06:10 +08:00
0,
0,
2018-09-04 22:42:34 +08:00
&text_layout_
2018-08-28 00:06:10 +08:00
)
);
2018-08-28 00:06:10 +08:00
// <20><>ȡ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ֵĿ<D6B5><C4BF>Ⱥ͸߶<CDB8>
DWRITE_TEXT_METRICS metrics;
2018-09-04 22:42:34 +08:00
text_layout_->GetMetrics(&metrics);
2018-08-28 00:06:10 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
this->SetSize(metrics.width, metrics.height);
2018-08-28 00:06:10 +08:00
// <20><><EFBFBD>´<EFBFBD><C2B4><EFBFBD> layout
2018-09-04 22:42:34 +08:00
SafeRelease(text_layout_);
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
writeFactory->CreateTextLayout(
(const wchar_t *)text_,
2018-08-28 00:06:10 +08:00
length,
2018-09-04 22:42:34 +08:00
text_format_,
transform_.size.width,
2018-08-28 00:06:10 +08:00
0,
2018-09-04 22:42:34 +08:00
&text_layout_
2018-08-28 00:06:10 +08:00
)
);
2018-05-24 12:24:39 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>»<EFBFBD><C2BB>ߺ<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
DWRITE_TEXT_RANGE Range = { 0, length };
if (style_.underline)
{
2018-09-04 22:42:34 +08:00
text_layout_->SetUnderline(true, Range);
}
2018-09-04 22:42:34 +08:00
if (style_.strikethrough)
{
2018-09-04 22:42:34 +08:00
text_layout_->SetStrikethrough(true, Range);
}
}