update TextLayout

This commit is contained in:
Nomango 2019-08-23 17:26:41 +08:00
parent 84ede8af45
commit 81503a33c9
7 changed files with 80 additions and 94 deletions

View File

@ -126,11 +126,11 @@ namespace kiwano
style_.color = color; style_.color = color;
} }
void Text::SetItalic(bool val) void Text::SetItalic(bool italic)
{ {
if (font_.italic != val) if (font_.italic != italic)
{ {
font_.italic = val; font_.italic = italic;
format_dirty_ = true; format_dirty_ = true;
} }
} }

View File

@ -102,7 +102,7 @@ namespace kiwano
// 设置文字斜体(默认值为 false // 设置文字斜体(默认值为 false
void SetItalic( void SetItalic(
bool val bool italic
); );
// 设置文本自动换行的宽度(默认为 0 // 设置文本自动换行的宽度(默认为 0

View File

@ -706,7 +706,7 @@ namespace kiwano
ThrowIfFailed(hr); ThrowIfFailed(hr);
} }
void Renderer::CreateTextLayout(TextLayout& layout, String const& text, TextStyle const& style, TextFormat const& format) void Renderer::CreateTextLayout(TextLayout& layout, String const& text, TextFormat const& format)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
if (!d2d_res_) if (!d2d_res_)
@ -720,7 +720,6 @@ namespace kiwano
hr = d2d_res_->CreateTextLayout( hr = d2d_res_->CreateTextLayout(
output, output,
text, text,
style,
format.GetTextFormat() format.GetTextFormat()
); );
} }

View File

@ -138,7 +138,6 @@ namespace kiwano
void CreateTextLayout( void CreateTextLayout(
TextLayout& layout, TextLayout& layout,
String const& text, String const& text,
TextStyle const& style,
TextFormat const& format TextFormat const& format
); );

View File

@ -76,9 +76,73 @@ namespace kiwano
Renderer::GetInstance()->CreateTextLayout( Renderer::GetInstance()->CreateTextLayout(
*this, *this,
text, text,
style,
text_format_ text_format_
); );
HRESULT hr = text_layout_ ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
if (style.line_spacing == 0.f)
{
hr = text_layout_->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_DEFAULT, 0, 0);
}
else
{
hr = text_layout_->SetLineSpacing(
DWRITE_LINE_SPACING_METHOD_UNIFORM,
style.line_spacing,
style.line_spacing * 0.8f
);
}
}
if (SUCCEEDED(hr))
{
hr = text_layout_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT(style.alignment));
}
if (SUCCEEDED(hr))
{
hr = text_layout_->SetWordWrapping((style.wrap_width > 0) ? DWRITE_WORD_WRAPPING_WRAP : DWRITE_WORD_WRAPPING_NO_WRAP);
}
if (SUCCEEDED(hr))
{
if (style.underline)
{
hr = text_layout_->SetUnderline(true, { 0, text.length() });
}
}
if (SUCCEEDED(hr))
{
if (style.strikethrough)
{
text_layout_->SetStrikethrough(true, { 0, text.length() });
}
}
if (SUCCEEDED(hr))
{
if (style.wrap_width > 0)
{
hr = text_layout_->SetMaxWidth(style.wrap_width);
}
else
{
// Fix the layout width when the text does not wrap
DWRITE_TEXT_METRICS metrics;
hr = text_layout_->GetMetrics(&metrics);
if (SUCCEEDED(hr))
{
hr = text_layout_->SetMaxWidth(metrics.width);
}
}
}
ThrowIfFailed(hr);
} }
UInt32 TextLayout::GetLineCount() UInt32 TextLayout::GetLineCount()

View File

@ -71,7 +71,6 @@ namespace kiwano
HRESULT CreateTextLayout( HRESULT CreateTextLayout(
_Out_ ComPtr<IDWriteTextLayout>& text_layout, _Out_ ComPtr<IDWriteTextLayout>& text_layout,
_In_ String const& text, _In_ String const& text,
_In_ TextStyle const& text_style,
_In_ ComPtr<IDWriteTextFormat> const& text_format _In_ ComPtr<IDWriteTextFormat> const& text_format
) const override; ) const override;
@ -461,94 +460,20 @@ namespace kiwano
} }
HRESULT D2DDeviceResources::CreateTextLayout(_Out_ ComPtr<IDWriteTextLayout>& text_layout, _In_ String const& text, HRESULT D2DDeviceResources::CreateTextLayout(_Out_ ComPtr<IDWriteTextLayout>& text_layout, _In_ String const& text,
_In_ TextStyle const & text_style, _In_ ComPtr<IDWriteTextFormat> const& text_format) const _In_ ComPtr<IDWriteTextFormat> const& text_format) const
{ {
if (!dwrite_factory_) if (!dwrite_factory_)
return E_UNEXPECTED; return E_UNEXPECTED;
HRESULT hr;
ComPtr<IDWriteTextLayout> output; ComPtr<IDWriteTextLayout> output;
UInt32 length = static_cast<UInt32>(text.length()); HRESULT hr = dwrite_factory_->CreateTextLayout(
if (text_style.wrap_width > 0)
{
hr = dwrite_factory_->CreateTextLayout(
text.c_str(), text.c_str(),
length, static_cast<UINT32>(text.length()),
text_format.get(),
text_style.wrap_width,
0,
&output
);
}
else
{
hr = dwrite_factory_->CreateTextLayout(
text.c_str(),
length,
text_format.get(), text_format.get(),
0, 0,
0, 0,
&output &output
); );
}
if (SUCCEEDED(hr))
{
if (text_style.line_spacing == 0.f)
{
hr = output->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_DEFAULT, 0, 0);
}
else
{
hr = output->SetLineSpacing(
DWRITE_LINE_SPACING_METHOD_UNIFORM,
text_style.line_spacing,
text_style.line_spacing * 0.8f
);
}
}
if (SUCCEEDED(hr))
{
hr = output->SetTextAlignment(DWRITE_TEXT_ALIGNMENT(text_style.alignment));
}
if (SUCCEEDED(hr))
{
hr = output->SetWordWrapping((text_style.wrap_width > 0) ? DWRITE_WORD_WRAPPING_WRAP : DWRITE_WORD_WRAPPING_NO_WRAP);
}
if (SUCCEEDED(hr))
{
if (text_style.underline)
{
hr = output->SetUnderline(true, { 0, length });
}
}
if (SUCCEEDED(hr))
{
if (text_style.strikethrough)
{
output->SetStrikethrough(true, { 0, length });
}
}
if (SUCCEEDED(hr))
{
// Fix the layout width when the text does not wrap
if (!(text_style.wrap_width > 0))
{
DWRITE_TEXT_METRICS metrics;
hr = output->GetMetrics(&metrics);
if (SUCCEEDED(hr))
{
hr = output->SetMaxWidth(metrics.width);
}
}
}
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {

View File

@ -223,7 +223,6 @@ namespace kiwano
virtual HRESULT CreateTextLayout( virtual HRESULT CreateTextLayout(
_Out_ ComPtr<IDWriteTextLayout>& text_layout, _Out_ ComPtr<IDWriteTextLayout>& text_layout,
_In_ String const& text, _In_ String const& text,
_In_ TextStyle const& text_style,
_In_ ComPtr<IDWriteTextFormat> const& text_format _In_ ComPtr<IDWriteTextFormat> const& text_format
) const = 0; ) const = 0;