Skip to content

Commit

Permalink
perf: preedit calc in layout calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
fxliang committed Nov 30, 2023
1 parent 8133578 commit 936f0a5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 42 deletions.
4 changes: 4 additions & 0 deletions WeaselUI/Layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Text> &labels, int id, const wchar_t *format) const = 0;
virtual bool IsInlinePreedit() const = 0;
Expand Down
43 changes: 21 additions & 22 deletions WeaselUI/StandardLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDWriteTextFormat1> pTextFormat, PDWR pDWR) const
CSize StandardLayout::GetPreeditSize(CDCHandle dc, const weasel::Text& text, ComPtr<IDWriteTextFormat1> pTextFormat, PDWR pDWR)
{
const std::wstring& preedit = text.str;
const std::vector<weasel::TextAttribute> &attrs = text.attributes;
Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion WeaselUI/StandardLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDWriteTextFormat1> pTextFormat, PDWR pDWR, LPSIZE lpSize) const;

protected:
/* Utility functions */
CSize GetPreeditSize(CDCHandle dc, const weasel::Text& text, ComPtr<IDWriteTextFormat1> pTextFormat = NULL, PDWR pDWR = NULL) const;
CSize GetPreeditSize(CDCHandle dc, const weasel::Text& text, ComPtr<IDWriteTextFormat1> 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];
Expand Down
26 changes: 7 additions & 19 deletions WeaselUI/WeaselPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<weasel::TextAttribute> 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;
Expand Down Expand Up @@ -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<weasel::TextAttribute> 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;
Expand Down

0 comments on commit 936f0a5

Please sign in to comment.