From 81503a33c95581f88fc834ea2cd44dbd407cf313 Mon Sep 17 00:00:00 2001 From: Nomango Date: Fri, 23 Aug 2019 17:26:41 +0800 Subject: [PATCH] update TextLayout --- src/kiwano/2d/Text.cpp | 6 +- src/kiwano/2d/Text.h | 2 +- src/kiwano/renderer/Renderer.cpp | 3 +- src/kiwano/renderer/Renderer.h | 1 - src/kiwano/renderer/TextLayout.cpp | 66 ++++++++++++- .../renderer/win32/D2DDeviceResources.cpp | 95 ++----------------- .../renderer/win32/D2DDeviceResources.h | 1 - 7 files changed, 80 insertions(+), 94 deletions(-) diff --git a/src/kiwano/2d/Text.cpp b/src/kiwano/2d/Text.cpp index 6354d071..875bfdc2 100644 --- a/src/kiwano/2d/Text.cpp +++ b/src/kiwano/2d/Text.cpp @@ -126,11 +126,11 @@ namespace kiwano 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; } } diff --git a/src/kiwano/2d/Text.h b/src/kiwano/2d/Text.h index 7dd84ef8..286e96e8 100644 --- a/src/kiwano/2d/Text.h +++ b/src/kiwano/2d/Text.h @@ -102,7 +102,7 @@ namespace kiwano // 设置文字斜体(默认值为 false) void SetItalic( - bool val + bool italic ); // 设置文本自动换行的宽度(默认为 0) diff --git a/src/kiwano/renderer/Renderer.cpp b/src/kiwano/renderer/Renderer.cpp index 6599abd2..699ba874 100644 --- a/src/kiwano/renderer/Renderer.cpp +++ b/src/kiwano/renderer/Renderer.cpp @@ -706,7 +706,7 @@ namespace kiwano 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; if (!d2d_res_) @@ -720,7 +720,6 @@ namespace kiwano hr = d2d_res_->CreateTextLayout( output, text, - style, format.GetTextFormat() ); } diff --git a/src/kiwano/renderer/Renderer.h b/src/kiwano/renderer/Renderer.h index 8802f593..4e14f017 100644 --- a/src/kiwano/renderer/Renderer.h +++ b/src/kiwano/renderer/Renderer.h @@ -138,7 +138,6 @@ namespace kiwano void CreateTextLayout( TextLayout& layout, String const& text, - TextStyle const& style, TextFormat const& format ); diff --git a/src/kiwano/renderer/TextLayout.cpp b/src/kiwano/renderer/TextLayout.cpp index c4033479..8a4f2eaa 100644 --- a/src/kiwano/renderer/TextLayout.cpp +++ b/src/kiwano/renderer/TextLayout.cpp @@ -76,9 +76,73 @@ namespace kiwano Renderer::GetInstance()->CreateTextLayout( *this, text, - style, 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() diff --git a/src/kiwano/renderer/win32/D2DDeviceResources.cpp b/src/kiwano/renderer/win32/D2DDeviceResources.cpp index ca062b00..8a90e67e 100644 --- a/src/kiwano/renderer/win32/D2DDeviceResources.cpp +++ b/src/kiwano/renderer/win32/D2DDeviceResources.cpp @@ -71,7 +71,6 @@ namespace kiwano HRESULT CreateTextLayout( _Out_ ComPtr& text_layout, _In_ String const& text, - _In_ TextStyle const& text_style, _In_ ComPtr const& text_format ) const override; @@ -460,95 +459,21 @@ namespace kiwano return hr; } - HRESULT D2DDeviceResources::CreateTextLayout(_Out_ ComPtr & text_layout, _In_ String const & text, - _In_ TextStyle const & text_style, _In_ ComPtr const& text_format) const + HRESULT D2DDeviceResources::CreateTextLayout(_Out_ ComPtr& text_layout, _In_ String const& text, + _In_ ComPtr const& text_format) const { if (!dwrite_factory_) return E_UNEXPECTED; - HRESULT hr; ComPtr output; - UInt32 length = static_cast(text.length()); - - if (text_style.wrap_width > 0) - { - hr = dwrite_factory_->CreateTextLayout( - text.c_str(), - length, - text_format.get(), - text_style.wrap_width, - 0, - &output - ); - } - else - { - hr = dwrite_factory_->CreateTextLayout( - text.c_str(), - length, - text_format.get(), - 0, - 0, - &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); - } - } - } + HRESULT hr = dwrite_factory_->CreateTextLayout( + text.c_str(), + static_cast(text.length()), + text_format.get(), + 0, + 0, + &output + ); if (SUCCEEDED(hr)) { diff --git a/src/kiwano/renderer/win32/D2DDeviceResources.h b/src/kiwano/renderer/win32/D2DDeviceResources.h index 419386ca..5c117a29 100644 --- a/src/kiwano/renderer/win32/D2DDeviceResources.h +++ b/src/kiwano/renderer/win32/D2DDeviceResources.h @@ -223,7 +223,6 @@ namespace kiwano virtual HRESULT CreateTextLayout( _Out_ ComPtr& text_layout, _In_ String const& text, - _In_ TextStyle const& text_style, _In_ ComPtr const& text_format ) const = 0;