diff --git a/WeaselUI/Layout.h b/WeaselUI/Layout.h index ec38cf68c..d96ad8e80 100644 --- a/WeaselUI/Layout.h +++ b/WeaselUI/Layout.h @@ -96,9 +96,9 @@ class Layout { const wchar_t* format) const = 0; virtual bool IsInlinePreedit() const = 0; virtual bool ShouldDisplayStatusIcon() const = 0; - virtual void GetTextSizeDW(const std::wstring text, + virtual void GetTextSizeDW(const std::wstring& text, size_t nCount, - ComPtr pTextFormat, + ComPtr& pTextFormat, PDWR pDWR, LPSIZE lpSize) const = 0; diff --git a/WeaselUI/StandardLayout.cpp b/WeaselUI/StandardLayout.cpp index b4f0878dc..24c4abb3f 100644 --- a/WeaselUI/StandardLayout.cpp +++ b/WeaselUI/StandardLayout.cpp @@ -12,9 +12,9 @@ std::wstring StandardLayout::GetLabelText(const std::vector& labels, } void weasel::StandardLayout::GetTextSizeDW( - const std::wstring text, + const std::wstring& text, size_t nCount, - ComPtr pTextFormat, + ComPtr& pTextFormat, PDWR pDWR, LPSIZE lpSize) const { D2D1_SIZE_F sz; @@ -26,65 +26,59 @@ void weasel::StandardLayout::GetTextSizeDW( return; } // 创建文本布局 - if (pTextFormat != NULL) { - if (_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT) - hr = pDWR->CreateTextLayout(text.c_str(), (int)nCount, pTextFormat.Get(), - 0.0f, (float)_style.max_height); - else - hr = pDWR->CreateTextLayout(text.c_str(), (int)nCount, pTextFormat.Get(), - (float)_style.max_width, 0); - } + if (_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT) + HR(pDWR->CreateTextLayout(text.c_str(), (int)nCount, pTextFormat.Get(), + 0.0f, (float)_style.max_height)); + else + HR(pDWR->CreateTextLayout(text.c_str(), (int)nCount, pTextFormat.Get(), + (float)_style.max_width, 0)); - if (SUCCEEDED(hr)) { - if (_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT) { - DWRITE_FLOW_DIRECTION flow = _style.vertical_text_left_to_right - ? DWRITE_FLOW_DIRECTION_LEFT_TO_RIGHT - : DWRITE_FLOW_DIRECTION_RIGHT_TO_LEFT; - pDWR->SetLayoutReadingDirection(DWRITE_READING_DIRECTION_TOP_TO_BOTTOM); - pDWR->SetLayoutFlowDirection(flow); - } - // 获取文本尺寸 - DWRITE_TEXT_METRICS textMetrics; - hr = pDWR->GetLayoutMetrics(&textMetrics); - sz = D2D1::SizeF(ceil(textMetrics.widthIncludingTrailingWhitespace), - ceil(textMetrics.height)); + if (_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT) { + DWRITE_FLOW_DIRECTION flow = _style.vertical_text_left_to_right + ? DWRITE_FLOW_DIRECTION_LEFT_TO_RIGHT + : DWRITE_FLOW_DIRECTION_RIGHT_TO_LEFT; + HR(pDWR->SetLayoutReadingDirection(DWRITE_READING_DIRECTION_TOP_TO_BOTTOM)); + HR(pDWR->SetLayoutFlowDirection(flow)); + } + // 获取文本尺寸 + DWRITE_TEXT_METRICS textMetrics; + HR(pDWR->GetLayoutMetrics(&textMetrics)); + sz = D2D1::SizeF(ceil(textMetrics.widthIncludingTrailingWhitespace), + ceil(textMetrics.height)); - lpSize->cx = (int)sz.width; - lpSize->cy = (int)sz.height; - pDWR->ResetLayout(); + lpSize->cx = (int)sz.width; + lpSize->cy = (int)sz.height; - if (_style.layout_type != UIStyle::LAYOUT_VERTICAL_TEXT) { - auto max_width = _style.max_width == 0 - ? textMetrics.widthIncludingTrailingWhitespace - : _style.max_width; - hr = pDWR->CreateTextLayout(text.c_str(), (int)nCount, pTextFormat.Get(), - max_width, textMetrics.height); - } else { - auto max_height = - _style.max_height == 0 ? textMetrics.height : _style.max_height; - hr = pDWR->CreateTextLayout(text.c_str(), (int)nCount, pTextFormat.Get(), - textMetrics.widthIncludingTrailingWhitespace, - max_height); - } + if (_style.layout_type != UIStyle::LAYOUT_VERTICAL_TEXT) { + auto max_width = _style.max_width == 0 + ? textMetrics.widthIncludingTrailingWhitespace + : _style.max_width; + HR(pDWR->CreateTextLayout(text.c_str(), (int)nCount, pTextFormat.Get(), + max_width, textMetrics.height)); + } else { + auto max_height = + _style.max_height == 0 ? textMetrics.height : _style.max_height; + HR(pDWR->CreateTextLayout(text.c_str(), (int)nCount, pTextFormat.Get(), + textMetrics.widthIncludingTrailingWhitespace, + max_height)); + } - if (_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT) { - pDWR->SetLayoutReadingDirection(DWRITE_READING_DIRECTION_TOP_TO_BOTTOM); - pDWR->SetLayoutFlowDirection(DWRITE_FLOW_DIRECTION_RIGHT_TO_LEFT); - } - DWRITE_OVERHANG_METRICS overhangMetrics; - hr = pDWR->GetLayoutOverhangMetrics(&overhangMetrics); - { - if (overhangMetrics.left > 0) - lpSize->cx += (LONG)(overhangMetrics.left + 1); - if (overhangMetrics.right > 0) - lpSize->cx += (LONG)(overhangMetrics.right + 1); - if (overhangMetrics.top > 0) - lpSize->cy += (LONG)(overhangMetrics.top + 1); - if (overhangMetrics.bottom > 0) - lpSize->cy += (LONG)(overhangMetrics.bottom + 1); - } + if (_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT) { + HR(pDWR->SetLayoutReadingDirection(DWRITE_READING_DIRECTION_TOP_TO_BOTTOM)); + HR(pDWR->SetLayoutFlowDirection(DWRITE_FLOW_DIRECTION_RIGHT_TO_LEFT)); + } + DWRITE_OVERHANG_METRICS overhangMetrics; + HR(pDWR->GetLayoutOverhangMetrics(&overhangMetrics)); + { + if (overhangMetrics.left > 0) + lpSize->cx += (LONG)(overhangMetrics.left + 1); + if (overhangMetrics.right > 0) + lpSize->cx += (LONG)(overhangMetrics.right + 1); + if (overhangMetrics.top > 0) + lpSize->cy += (LONG)(overhangMetrics.top + 1); + if (overhangMetrics.bottom > 0) + lpSize->cy += (LONG)(overhangMetrics.bottom + 1); } - pDWR->ResetLayout(); } CSize StandardLayout::GetPreeditSize(CDCHandle dc, diff --git a/WeaselUI/StandardLayout.h b/WeaselUI/StandardLayout.h index b16cc05ae..23eef3590 100644 --- a/WeaselUI/StandardLayout.h +++ b/WeaselUI/StandardLayout.h @@ -51,9 +51,9 @@ class StandardLayout : public Layout { virtual CSize GetAfterSize() { return _aftersz; } virtual weasel::TextRange GetPreeditRange() { return _range; } - void GetTextSizeDW(const std::wstring text, + void GetTextSizeDW(const std::wstring& text, size_t nCount, - ComPtr pTextFormat, + ComPtr& pTextFormat, PDWR pDWR, LPSIZE lpSize) const; diff --git a/WeaselUI/WeaselPanel.cpp b/WeaselUI/WeaselPanel.cpp index 636824cf4..432688a44 100644 --- a/WeaselUI/WeaselPanel.cpp +++ b/WeaselUI/WeaselPanel.cpp @@ -1227,41 +1227,36 @@ void WeaselPanel::_TextOut(const CRect& rc, float alpha = (float)((inColor >> 24) & 255) / 255.0f; HRESULT hr = S_OK; if (pDWR->pBrush == NULL) { - hr = pDWR->CreateBrush(D2D1::ColorF(r, g, b, alpha)); - if (FAILED(hr)) - MessageBox(L"Failed CreateBrush", L"Info"); + HR(pDWR->CreateBrush(D2D1::ColorF(r, g, b, alpha))); } else pDWR->SetBrushColor(D2D1::ColorF(r, g, b, alpha)); - if (NULL != pDWR->pBrush && NULL != pTextFormat) { - pDWR->CreateTextLayout(psz.c_str(), (int)cch, pTextFormat, - (float)rc.Width(), (float)rc.Height()); - if (m_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT) { - DWRITE_FLOW_DIRECTION flow = m_style.vertical_text_left_to_right - ? DWRITE_FLOW_DIRECTION_LEFT_TO_RIGHT - : DWRITE_FLOW_DIRECTION_RIGHT_TO_LEFT; - pDWR->SetLayoutReadingDirection(DWRITE_READING_DIRECTION_TOP_TO_BOTTOM); - pDWR->SetLayoutFlowDirection(flow); - } + HR(pDWR->CreateTextLayout(psz.c_str(), (int)cch, pTextFormat, + (float)rc.Width(), (float)rc.Height())); + if (m_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT) { + DWRITE_FLOW_DIRECTION flow = m_style.vertical_text_left_to_right + ? DWRITE_FLOW_DIRECTION_LEFT_TO_RIGHT + : DWRITE_FLOW_DIRECTION_RIGHT_TO_LEFT; + HR(pDWR->SetLayoutReadingDirection(DWRITE_READING_DIRECTION_TOP_TO_BOTTOM)); + HR(pDWR->SetLayoutFlowDirection(flow)); + } - // offsetx for font glyph over left - float offsetx = (float)rc.left; - float offsety = (float)rc.top; - // prepare for space when first character overhanged - DWRITE_OVERHANG_METRICS omt; - pDWR->GetLayoutOverhangMetrics(&omt); - if (m_style.layout_type != UIStyle::LAYOUT_VERTICAL_TEXT && omt.left > 0) - offsetx += omt.left; - if (m_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT && omt.top > 0) - offsety += omt.top; - - if (pDWR->pTextLayout != NULL) { - pDWR->DrawTextLayoutAt({offsetx, offsety}); + // offsetx for font glyph over left + float offsetx = (float)rc.left; + float offsety = (float)rc.top; + // prepare for space when first character overhanged + DWRITE_OVERHANG_METRICS omt; + HR(pDWR->GetLayoutOverhangMetrics(&omt)); + if (m_style.layout_type != UIStyle::LAYOUT_VERTICAL_TEXT && omt.left > 0) + offsetx += omt.left; + if (m_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT && omt.top > 0) + offsety += omt.top; + + if (pDWR->pTextLayout != NULL) { + pDWR->DrawTextLayoutAt({offsetx, offsety}); #if 0 - D2D1_RECT_F rectf = D2D1::RectF(offsetx, offsety, offsetx + rc.Width(), offsety + rc.Height()); - pDWR->DrawRect(&rectf); + D2D1_RECT_F rectf = D2D1::RectF(offsetx, offsety, offsetx + rc.Width(), offsety + rc.Height()); + pDWR->DrawRect(&rectf); #endif - } - pDWR->ResetLayout(); } }