Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI performance, some fix and features #1037

Merged
merged 7 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions RimeWithWeasel/RimeWithWeasel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,13 +839,13 @@ static void _RimeGetBool(RimeConfig* config, char* key, bool cond, T& value, con
}
// parse string option to T type value, with fallback
template <typename T>
void _RimeParseStringOpt(RimeConfig* config, const std::string key, T& value, const std::map<std::string, T> amap) {
void _RimeParseStringOptWithFallback(RimeConfig* config, const std::string key, T& value, const std::map<std::string, T> amap, const T& fallback) {
char str_buff[256] = { 0 };
if (RimeConfigGetString(config, key.c_str(), str_buff, sizeof(str_buff) - 1)) {
auto it = amap.find(std::string(str_buff));
if(it != amap.end())
value = it->second;
value = (it != amap.end()) ? it->second : fallback;
}
else value = fallback;
}
static inline void _abs(int* value) { *value = abs(*value); } // turn *value to be non-negative
// get int type value with fallback key fb_key, and func to execute after reading
Expand Down Expand Up @@ -921,32 +921,34 @@ static void _UpdateUIStyle(RimeConfig* config, UI* ui, bool initialize)
_RimeGetIntWithFallback(config, "style/label_font_point", &style.label_font_point, "style/font_point", _abs);
_RimeGetIntWithFallback(config, "style/comment_font_point", &style.comment_font_point, "style/font_point", _abs);
_RimeGetIntWithFallback(config, "style/mouse_hover_ms", &style.mouse_hover_ms, NULL, _abs);
_RimeGetIntWithFallback(config, "style/candidate_abbreviate_length", &style.candidate_abbreviate_length, NULL, _abs);
_RimeGetBool(config, "style/inline_preedit", initialize, style.inline_preedit, true, false);
_RimeGetBool(config, "style/vertical_auto_reverse", initialize, style.vertical_auto_reverse, true, false);
const std::map<std::string, UIStyle::PreeditType> _preeditMap = {
{std::string("composition"), UIStyle::COMPOSITION},
{std::string("preview"), UIStyle::PREVIEW},
{std::string("preview_all"), UIStyle::PREVIEW_ALL}
};
_RimeParseStringOpt(config, "style/preedit_type", style.preedit_type, _preeditMap);
_RimeParseStringOptWithFallback(config, "style/preedit_type", style.preedit_type, _preeditMap, style.preedit_type);
const std::map < std::string , UIStyle::AntiAliasMode > _aliasModeMap = {
{std::string("force_dword"), UIStyle::FORCE_DWORD},
{std::string("cleartype"), UIStyle::CLEARTYPE},
{std::string("grayscale"), UIStyle::GRAYSCALE},
{std::string("aliased"), UIStyle::ALIASED},
{std::string("default"), UIStyle::DEFAULT}
};
_RimeParseStringOpt(config, "style/antialias_mode", style.antialias_mode, _aliasModeMap);
_RimeParseStringOptWithFallback(config, "style/antialias_mode", style.antialias_mode, _aliasModeMap, style.antialias_mode);
const std::map<std::string, UIStyle::LayoutAlignType> _alignType = {
{std::string("top"), UIStyle::ALIGN_TOP},
{std::string("center"), UIStyle::ALIGN_CENTER},
{std::string("bottom"), UIStyle::ALIGN_BOTTOM}
};
_RimeParseStringOpt(config, "style/layout/align_type", style.align_type, _alignType);
_RimeParseStringOptWithFallback(config, "style/layout/align_type", style.align_type, _alignType, style.align_type);
_RimeGetBool(config, "style/display_tray_icon", initialize, style.display_tray_icon, true, false);
_RimeGetBool(config, "style/ascii_tip_follow_cursor", initialize, style.ascii_tip_follow_cursor, true, false);
_RimeGetBool(config, "style/horizontal", initialize, style.layout_type, UIStyle::LAYOUT_HORIZONTAL, UIStyle::LAYOUT_VERTICAL);
_RimeGetBool(config, "style/paging_on_scroll", initialize, style.paging_on_scroll, true, false);
_RimeGetBool(config, "style/click_to_capture", initialize, style.click_to_capture, true, false);
_RimeGetBool(config, "style/fullscreen", false, style.layout_type,
((style.layout_type == UIStyle::LAYOUT_HORIZONTAL) ? UIStyle::LAYOUT_HORIZONTAL_FULLSCREEN : UIStyle::LAYOUT_VERTICAL_FULLSCREEN), style.layout_type);
_RimeGetBool(config, "style/vertical_text", false, style.layout_type, UIStyle::LAYOUT_VERTICAL_TEXT, style.layout_type);
Expand All @@ -957,7 +959,7 @@ static void _UpdateUIStyle(RimeConfig* config, UI* ui, bool initialize)
{std::string("vertical"), true}
};
bool _text_orientation_bool = false;
_RimeParseStringOpt(config, "style/text_orientation", _text_orientation_bool, _text_orientation);
_RimeParseStringOptWithFallback(config, "style/text_orientation", _text_orientation_bool, _text_orientation, _text_orientation_bool);
if(_text_orientation_bool)
style.layout_type = UIStyle::LAYOUT_VERTICAL_TEXT;
_RimeGetStringWithFunc(config, "style/label_format", style.label_text_format);
Expand All @@ -974,7 +976,7 @@ static void _UpdateUIStyle(RimeConfig* config, UI* ui, bool initialize)
{std::string("vertical+fullscreen"), UIStyle::LAYOUT_VERTICAL_FULLSCREEN},
{std::string("horizontal+fullscreen"), UIStyle::LAYOUT_HORIZONTAL_FULLSCREEN}
};
_RimeParseStringOpt(config, "style/layout/type", style.layout_type, _layoutMap);
_RimeParseStringOptWithFallback(config, "style/layout/type", style.layout_type, _layoutMap, style.layout_type);
// disable max_width when full screen
if( style.layout_type == UIStyle::LAYOUT_HORIZONTAL_FULLSCREEN || style.layout_type == UIStyle::LAYOUT_VERTICAL_FULLSCREEN )
{
Expand Down Expand Up @@ -1056,7 +1058,7 @@ static bool _UpdateUIStyleColor(RimeConfig* config, UIStyle& style, std::string
{std::string("rgba"), COLOR_RGBA},
{std::string("abgr"), COLOR_ABGR}
};
_RimeParseStringOpt(config, (prefix+"/color_format"), fmt, _colorFmt);
_RimeParseStringOptWithFallback(config, (prefix+"/color_format"), fmt, _colorFmt, COLOR_ABGR);
_RimeConfigGetColor32bWithFallback(config, (prefix + "/back_color"), style.back_color, fmt, 0xffffffff);
_RimeConfigGetColor32bWithFallback(config, (prefix + "/shadow_color"), style.shadow_color, fmt, TRANSPARENT_COLOR);
_RimeConfigGetColor32bWithFallback(config, (prefix + "/prevpage_color"), style.prevpage_color, fmt, TRANSPARENT_COLOR);
Expand Down
27 changes: 13 additions & 14 deletions WeaselUI/DirectWriteResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ DirectWriteResources::~DirectWriteResources()
pD2d1Factory.Reset();
}

HRESULT DirectWriteResources::InitResources(std::wstring label_font_face, int label_font_point,
std::wstring font_face, int font_point,
std::wstring comment_font_face, int comment_font_point, bool vertical_text)
HRESULT DirectWriteResources::InitResources(const std::wstring& label_font_face, const int& label_font_point,
const std::wstring& font_face, const int& font_point,
const std::wstring& comment_font_face, const int& comment_font_point, const bool& vertical_text)
{
// prepare d2d1 resources
pPreeditTextFormat.Reset();
Expand Down Expand Up @@ -131,14 +131,13 @@ HRESULT DirectWriteResources::InitResources(std::wstring label_font_face, int la
pTextFormat->SetWordWrapping(wrapping);
_SetFontFallback(pTextFormat, fontFaceStrVector);
}
fontFaceStrVector.swap(std::vector<std::wstring>());
decltype(fontFaceStrVector)().swap(fontFaceStrVector);

fontFaceStrVector = ws_split(font_face, L",");
//_ParseFontFace(fontFaceStrVector[0], fontWeight, fontStyle);
fontFaceStrVector[0] = std::regex_replace(fontFaceStrVector[0], std::wregex(STYLEORWEIGHT, std::wregex::icase), L"");
hResult = pDWFactory->CreateTextFormat(_mainFontFace.c_str(), NULL,
fontWeight, fontStyle, DWRITE_FONT_STRETCH_NORMAL,
//font_point * dpiScaleX_, L"", reinterpret_cast<IDWriteTextFormat**>(&pPreeditTextFormat));
font_point * dpiScaleX_, L"", reinterpret_cast<IDWriteTextFormat**>(pPreeditTextFormat.GetAddressOf()));
if( pPreeditTextFormat != NULL)
{
Expand All @@ -154,7 +153,7 @@ HRESULT DirectWriteResources::InitResources(std::wstring label_font_face, int la
pPreeditTextFormat->SetWordWrapping(wrapping);
_SetFontFallback(pPreeditTextFormat, fontFaceStrVector);
}
fontFaceStrVector.swap(std::vector<std::wstring>());
decltype(fontFaceStrVector)().swap(fontFaceStrVector);

// label font text format set up
fontFaceStrVector = ws_split(label_font_face, L",");
Expand All @@ -178,7 +177,7 @@ HRESULT DirectWriteResources::InitResources(std::wstring label_font_face, int la
pLabelTextFormat->SetWordWrapping(wrapping);
_SetFontFallback(pLabelTextFormat, fontFaceStrVector);
}
fontFaceStrVector.swap(std::vector<std::wstring>());
decltype(fontFaceStrVector)().swap(fontFaceStrVector);

// comment font text format set up
fontFaceStrVector = ws_split(comment_font_face, L",");
Expand All @@ -202,11 +201,11 @@ HRESULT DirectWriteResources::InitResources(std::wstring label_font_face, int la
pCommentTextFormat->SetWordWrapping(wrapping);
_SetFontFallback(pCommentTextFormat, fontFaceStrVector);
}
fontFaceStrVector.swap(std::vector<std::wstring>());
decltype(fontFaceStrVector)().swap(fontFaceStrVector);
return hResult;
}

HRESULT DirectWriteResources::InitResources(UIStyle& style, UINT dpi = 96)
HRESULT DirectWriteResources::InitResources(const UIStyle& style, const UINT& dpi = 96)
{
_style = style;
if(dpi)
Expand All @@ -218,7 +217,7 @@ HRESULT DirectWriteResources::InitResources(UIStyle& style, UINT dpi = 96)
style.comment_font_face, style.comment_font_point, style.layout_type==UIStyle::LAYOUT_VERTICAL_TEXT);
}

void weasel::DirectWriteResources::SetDpi(UINT dpi)
void weasel::DirectWriteResources::SetDpi(const UINT& dpi)
{
dpiScaleX_ = dpi / 72.0f;
dpiScaleY_ = dpi / 72.0f;
Expand Down Expand Up @@ -250,7 +249,7 @@ static std::wstring _MatchWordsOutLowerCaseTrim1st(const std::wstring& wstr, con
return res;
}

void DirectWriteResources::_ParseFontFace(const std::wstring fontFaceStr, DWRITE_FONT_WEIGHT& fontWeight, DWRITE_FONT_STYLE& fontStyle)
void DirectWriteResources::_ParseFontFace(const std::wstring& fontFaceStr, DWRITE_FONT_WEIGHT& fontWeight, DWRITE_FONT_STYLE& fontStyle)
{
const std::wstring patWeight(L"(:thin|:extra_light|:ultra_light|:light|:semi_light|:medium|:demi_bold|:semi_bold|:bold|:extra_bold|:ultra_bold|:black|:heavy|:extra_black|:ultra_black)");
const std::map<std::wstring, DWRITE_FONT_WEIGHT> _mapWeight = {
Expand Down Expand Up @@ -286,7 +285,7 @@ void DirectWriteResources::_ParseFontFace(const std::wstring fontFaceStr, DWRITE
fontStyle = (it2 != _mapStyle.end()) ? it2->second : DWRITE_FONT_STYLE_NORMAL;
}

void DirectWriteResources::_SetFontFallback(ComPtr<IDWriteTextFormat1> textFormat, std::vector<std::wstring> fontVector)
void DirectWriteResources::_SetFontFallback(ComPtr<IDWriteTextFormat1> textFormat, const std::vector<std::wstring>& fontVector)
{
ComPtr<IDWriteFontFallback> pSysFallback;
pDWFactory->GetSystemFontFallback(pSysFallback.GetAddressOf());
Expand Down Expand Up @@ -338,13 +337,13 @@ void DirectWriteResources::_SetFontFallback(ComPtr<IDWriteTextFormat1> textForma
DWRITE_UNICODE_RANGE range = { first, last };
const WCHAR* familys = { _fontFaceWstr.c_str() };
pFontFallbackBuilder->AddMapping(&range, 1, &familys, 1);
fallbackFontsVector.swap(std::vector<std::wstring>());
decltype(fallbackFontsVector)().swap(fallbackFontsVector);
}
// add system defalt font fallback
pFontFallbackBuilder->AddMappings(pSysFallback.Get());
pFontFallbackBuilder->CreateFontFallback(pFontFallback.GetAddressOf());
textFormat->SetFontFallback(pFontFallback.Get());
fallbackFontsVector.swap(std::vector<std::wstring>());
decltype(fallbackFontsVector)().swap(fallbackFontsVector);
pFontFallback.Reset();
pSysFallback.Reset();
pFontFallbackBuilder.Reset();
Expand Down
5 changes: 4 additions & 1 deletion WeaselUI/FullScreenLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ void weasel::FullScreenLayout::DoLayout(CDCHandle dc, PDWR pDWR)
if (!_style.mark_text.empty() && (_style.hilited_mark_color & 0xff000000))
{
CSize sg;
GetTextSizeDW(_style.mark_text, _style.mark_text.length(), pDWR->pTextFormat, pDWR, &sg);
if(_style.mark_text.empty())
GetTextSizeDW(L"|", 1, pDWR->pTextFormat, pDWR, &sg);
else
GetTextSizeDW(_style.mark_text, _style.mark_text.length(), pDWR->pTextFormat, pDWR, &sg);
MARK_WIDTH = sg.cx;
MARK_HEIGHT = sg.cy;
MARK_GAP = MARK_WIDTH + 4;
Expand Down
2 changes: 1 addition & 1 deletion WeaselUI/HorizontalLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void HorizontalLayout::DoLayout(CDCHandle dc, PDWR pDWR )
MARK_HEIGHT = sg.cy;
MARK_GAP = MARK_WIDTH + 4;
}
int base_offset = ((_style.hilited_mark_color & 0xff000000) && !_style.mark_text.empty()) ? MARK_GAP : 0;
int base_offset = ((_style.hilited_mark_color & 0xff000000)) ? MARK_GAP : 0;

// calc page indicator
CSize pgszl, pgszr;
Expand Down
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
4 changes: 2 additions & 2 deletions WeaselUI/VHorizontalLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void VHorizontalLayout::DoLayout(CDCHandle dc, PDWR pDWR )
MARK_HEIGHT = sg.cy;
MARK_GAP = MARK_HEIGHT + 4;
}
int base_offset = ((_style.hilited_mark_color & 0xff000000) && !_style.mark_text.empty()) ? MARK_GAP : 0;
int base_offset = ((_style.hilited_mark_color & 0xff000000)) ? MARK_GAP : 0;

// calc page indicator
CSize pgszl, pgszr;
Expand Down Expand Up @@ -237,7 +237,7 @@ void VHorizontalLayout::DoLayoutWithWrap(CDCHandle dc, PDWR pDWR)
MARK_HEIGHT = sg.cy;
MARK_GAP = MARK_HEIGHT + 4;
}
int base_offset = ((_style.hilited_mark_color & 0xff000000) && !_style.mark_text.empty()) ? MARK_GAP : 0;
int base_offset = ((_style.hilited_mark_color & 0xff000000)) ? MARK_GAP : 0;

// calc page indicator
CSize pgszl, pgszr;
Expand Down
2 changes: 1 addition & 1 deletion WeaselUI/VerticalLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void weasel::VerticalLayout::DoLayout(CDCHandle dc, PDWR pDWR)
MARK_HEIGHT = sg.cy;
MARK_GAP = MARK_WIDTH + 4;
}
int base_offset = ((_style.hilited_mark_color & 0xff000000) && !_style.mark_text.empty()) ? MARK_GAP : 0;
int base_offset = ((_style.hilited_mark_color & 0xff000000)) ? MARK_GAP : 0;

// calc page indicator
CSize pgszl, pgszr;
Expand Down
Loading