Skip to content

Commit

Permalink
refactor: update directwrite operation calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
fxliang committed Nov 20, 2024
1 parent ede0caa commit 3bfd452
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 90 deletions.
4 changes: 2 additions & 2 deletions WeaselUI/Layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDWriteTextFormat1> pTextFormat,
ComPtr<IDWriteTextFormat1>& pTextFormat,
PDWR pDWR,
LPSIZE lpSize) const = 0;

Expand Down
106 changes: 50 additions & 56 deletions WeaselUI/StandardLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ std::wstring StandardLayout::GetLabelText(const std::vector<Text>& labels,
}

void weasel::StandardLayout::GetTextSizeDW(
const std::wstring text,
const std::wstring& text,
size_t nCount,
ComPtr<IDWriteTextFormat1> pTextFormat,
ComPtr<IDWriteTextFormat1>& pTextFormat,
PDWR pDWR,
LPSIZE lpSize) const {
D2D1_SIZE_F sz;
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions WeaselUI/StandardLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDWriteTextFormat1> pTextFormat,
ComPtr<IDWriteTextFormat1>& pTextFormat,
PDWR pDWR,
LPSIZE lpSize) const;

Expand Down
55 changes: 25 additions & 30 deletions WeaselUI/WeaselPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 3bfd452

Please sign in to comment.