From 936f0a564069738f11b956a41730bcc15bd130a0 Mon Sep 17 00:00:00 2001 From: fxliang Date: Wed, 29 Nov 2023 12:49:26 +0800 Subject: [PATCH] perf: preedit calc in layout calculation --- WeaselUI/Layout.h | 4 ++++ WeaselUI/StandardLayout.cpp | 43 ++++++++++++++++++------------------- WeaselUI/StandardLayout.h | 8 ++++++- WeaselUI/WeaselPanel.cpp | 26 ++++++---------------- 4 files changed, 39 insertions(+), 42 deletions(-) diff --git a/WeaselUI/Layout.h b/WeaselUI/Layout.h index b6ba7422d..8fa700032 100644 --- a/WeaselUI/Layout.h +++ b/WeaselUI/Layout.h @@ -68,6 +68,10 @@ namespace weasel virtual CRect GetContentRect() = 0; virtual CRect GetPrepageRect() = 0; virtual CRect GetNextpageRect() = 0; + virtual weasel::TextRange GetPreeditRange() = 0; + virtual CSize GetBeforeSize() = 0; + virtual CSize GetHilitedSize() = 0; + virtual CSize GetAfterSize() = 0; virtual std::wstring GetLabelText(const std::vector &labels, int id, const wchar_t *format) const = 0; virtual bool IsInlinePreedit() const = 0; diff --git a/WeaselUI/StandardLayout.cpp b/WeaselUI/StandardLayout.cpp index 86851f568..f610bf241 100644 --- a/WeaselUI/StandardLayout.cpp +++ b/WeaselUI/StandardLayout.cpp @@ -78,7 +78,7 @@ void weasel::StandardLayout::GetTextSizeDW(const std::wstring text, size_t nCoun pDWR->ResetLayout(); } -CSize StandardLayout::GetPreeditSize(CDCHandle dc, const weasel::Text& text, ComPtr pTextFormat, PDWR pDWR) const +CSize StandardLayout::GetPreeditSize(CDCHandle dc, const weasel::Text& text, ComPtr pTextFormat, PDWR pDWR) { const std::wstring& preedit = text.str; const std::vector &attrs = text.attributes; @@ -88,34 +88,33 @@ CSize StandardLayout::GetPreeditSize(CDCHandle dc, const weasel::Text& text, Com weasel::TextRange range; for (size_t j = 0; j < attrs.size(); ++j) if (attrs[j].type == weasel::HIGHLIGHTED) - range = attrs[j].range; - if(range.start < range.end) + _range = attrs[j].range; + if(_range.start < _range.end) { - std::wstring before_str = preedit.substr(0, range.start); - std::wstring hilited_str = preedit.substr(range.start, range.end); - std::wstring after_str = preedit.substr(range.end); - CSize beforesz(0,0), hilitedsz(0,0), aftersz(0,0); - GetTextSizeDW(before_str, before_str.length(), pTextFormat, pDWR, &beforesz); - GetTextSizeDW(hilited_str, hilited_str.length(), pTextFormat, pDWR, &hilitedsz); - GetTextSizeDW(after_str, after_str.length(), pTextFormat, pDWR, &aftersz); + std::wstring before_str = preedit.substr(0, _range.start); + std::wstring hilited_str = preedit.substr(_range.start, _range.end); + std::wstring after_str = preedit.substr(_range.end); + GetTextSizeDW(before_str, before_str.length(), pTextFormat, pDWR, &_beforesz); + GetTextSizeDW(hilited_str, hilited_str.length(), pTextFormat, pDWR, &_hilitedsz); + GetTextSizeDW(after_str, after_str.length(), pTextFormat, pDWR, &_aftersz); auto width_max = 0, height_max = 0; if(_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT) { - width_max = max(width_max, beforesz.cx); - width_max = max(width_max, hilitedsz.cx); - width_max = max(width_max, aftersz.cx); - height_max += beforesz.cy + (beforesz.cy > 0) * _style.hilite_spacing; - height_max += hilitedsz.cy + (hilitedsz.cy > 0) * _style.hilite_spacing; - height_max += aftersz.cy; // + (aftersz.cy > 0) * _style.hilite_spacing; + width_max = max(width_max, _beforesz.cx); + width_max = max(width_max, _hilitedsz.cx); + width_max = max(width_max, _aftersz.cx); + height_max += _beforesz.cy + (_beforesz.cy > 0) * _style.hilite_spacing; + height_max += _hilitedsz.cy + (_hilitedsz.cy > 0) * _style.hilite_spacing; + height_max += _aftersz.cy; } else { - height_max = max(height_max, beforesz.cy); - height_max = max(height_max, hilitedsz.cy); - height_max = max(height_max, aftersz.cy); - width_max += beforesz.cx + (beforesz.cx > 0) * _style.hilite_spacing; - width_max += hilitedsz.cx + (hilitedsz.cx > 0) * _style.hilite_spacing; - width_max += aftersz.cx ;// + (aftersz.cx > 0) * _style.hilite_spacing; + height_max = max(height_max, _beforesz.cy); + height_max = max(height_max, _hilitedsz.cy); + height_max = max(height_max, _aftersz.cy); + width_max += _beforesz.cx + (_beforesz.cx > 0) * _style.hilite_spacing; + width_max += _hilitedsz.cx + (_hilitedsz.cx > 0) * _style.hilite_spacing; + width_max += _aftersz.cx ; } size.cx = width_max; size.cy = height_max; diff --git a/WeaselUI/StandardLayout.h b/WeaselUI/StandardLayout.h index a39bda8b7..1212c0191 100644 --- a/WeaselUI/StandardLayout.h +++ b/WeaselUI/StandardLayout.h @@ -36,17 +36,23 @@ namespace weasel virtual CRect GetContentRect() { return _contentRect; } virtual CRect GetPrepageRect() { return _prePageRect; } virtual CRect GetNextpageRect() { return _nextPageRect; } + virtual CSize GetBeforeSize() { return _beforesz; } + virtual CSize GetHilitedSize() { return _hilitedsz; } + virtual CSize GetAfterSize() { return _aftersz; } + virtual weasel::TextRange GetPreeditRange() { return _range; } void GetTextSizeDW(const std::wstring text, size_t nCount, ComPtr pTextFormat, PDWR pDWR, LPSIZE lpSize) const; protected: /* Utility functions */ - CSize GetPreeditSize(CDCHandle dc, const weasel::Text& text, ComPtr pTextFormat = NULL, PDWR pDWR = NULL) const; + CSize GetPreeditSize(CDCHandle dc, const weasel::Text& text, ComPtr pTextFormat = NULL, PDWR pDWR = NULL); bool _IsHighlightOverCandidateWindow(CRect& rc, CDCHandle& dc); void _PrepareRoundInfo(CDCHandle& dc); void UpdateStatusIconLayout(int* width, int* height); + CSize _beforesz, _hilitedsz, _aftersz; + weasel::TextRange _range; CSize _contentSize; CRect _preeditRect, _auxiliaryRect, _highlightRect; CRect _candidateRects[MAX_CANDIDATES_COUNT]; diff --git a/WeaselUI/WeaselPanel.cpp b/WeaselUI/WeaselPanel.cpp index d4995e76d..2ae3f80e0 100644 --- a/WeaselUI/WeaselPanel.cpp +++ b/WeaselUI/WeaselPanel.cpp @@ -436,20 +436,15 @@ bool WeaselPanel::_DrawPreedit(Text const& text, CDCHandle dc, CRect const& rc) IDWriteTextFormat1* txtFormat = pDWR->pPreeditTextFormat.Get(); if (!t.empty()) { - weasel::TextRange range; - std::vector const& attrs = text.attributes; - for (size_t j = 0; j < attrs.size(); ++j) - if (attrs[j].type == weasel::HIGHLIGHTED) - range = attrs[j].range; + weasel::TextRange range = m_layout->GetPreeditRange(); if (range.start < range.end) { - CSize beforeSz, hilitedSz, afterSz; std::wstring before_str = t.substr(0, range.start); std::wstring hilited_str = t.substr(range.start, range.end); std::wstring after_str = t.substr(range.end); - m_layout->GetTextSizeDW(before_str, before_str.length(), txtFormat, pDWR, &beforeSz); - m_layout->GetTextSizeDW(hilited_str, hilited_str.length(), txtFormat, pDWR, &hilitedSz); - m_layout->GetTextSizeDW(after_str, after_str.length(), txtFormat, pDWR, &afterSz); + CSize beforeSz = m_layout->GetBeforeSize(); + CSize hilitedSz = m_layout->GetHilitedSize(); + CSize afterSz = m_layout->GetAfterSize(); int x = rc.left; int y = rc.top; @@ -528,18 +523,11 @@ bool WeaselPanel::_DrawPreeditBack(Text const& text, CDCHandle dc, CRect const& IDWriteTextFormat1* txtFormat = pDWR->pPreeditTextFormat.Get(); if (!t.empty()) { - weasel::TextRange range; - std::vector const& attrs = text.attributes; - for (size_t j = 0; j < attrs.size(); ++j) - if (attrs[j].type == weasel::HIGHLIGHTED) - range = attrs[j].range; + weasel::TextRange range = m_layout->GetPreeditRange(); if (range.start < range.end) { - CSize beforeSz, hilitedSz; - std::wstring before_str = t.substr(0, range.start); - std::wstring hilited_str = t.substr(range.start, range.end); - m_layout->GetTextSizeDW(before_str, before_str.length(), txtFormat, pDWR, &beforeSz); - m_layout->GetTextSizeDW(hilited_str, hilited_str.length(), txtFormat, pDWR, &hilitedSz); + CSize beforeSz = m_layout->GetBeforeSize(); + CSize hilitedSz = m_layout->GetHilitedSize(); int x = rc.left; int y = rc.top;