Skip to content

Commit

Permalink
fix: UI layout sizes are not DPI aware.
Browse files Browse the repository at this point in the history
  • Loading branch information
fxliang committed May 28, 2024
1 parent 22fce58 commit 5d1bf58
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 127 deletions.
30 changes: 15 additions & 15 deletions WeaselUI/DirectWriteResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ std::vector<std::wstring> wc_split(const wchar_t* in, const wchar_t* delim) {
DirectWriteResources::DirectWriteResources(weasel::UIStyle& style,
UINT dpi = 96)
: _style(style),
dpiScaleX_(0),
dpiScaleY_(0),
dpiScaleFontPoint(0),
dpiScaleLayout(0),
pD2d1Factory(NULL),
pDWFactory(NULL),
pRenderTarget(NULL),
Expand Down Expand Up @@ -70,9 +70,9 @@ DirectWriteResources::DirectWriteResources(weasel::UIStyle& style,
pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(1.0f, 1.0f, 1.0f, 1.0f),
pBrush.GetAddressOf());
// get the dpi information
dpiScaleX_ = dpiScaleY_ = (float)dpi;
dpiScaleX_ /= 72.0f;
dpiScaleY_ /= 72.0f;
dpiScaleFontPoint = dpiScaleLayout = (float)dpi;
dpiScaleFontPoint /= 72.0f;
dpiScaleLayout /= 96.0f;

InitResources(style, dpi);
}
Expand Down Expand Up @@ -130,8 +130,8 @@ HRESULT DirectWriteResources::InitResources(
DWRITE_FONT_WEIGHT fontWeight = DWRITE_FONT_WEIGHT_NORMAL;
DWRITE_FONT_STYLE fontStyle = DWRITE_FONT_STYLE_NORMAL;
// convert percentage to float
float linespacing = dpiScaleX_ * ((float)_style.linespacing / 100.0f);
float baseline = dpiScaleX_ * ((float)_style.baseline / 100.0f);
float linespacing = dpiScaleFontPoint * ((float)_style.linespacing / 100.0f);
float baseline = dpiScaleFontPoint * ((float)_style.baseline / 100.0f);
if (_style.layout_type == UIStyle::LAYOUT_VERTICAL_TEXT)
baseline = linespacing / 2;
// setup font weight and font style by the first unit of font_face setting
Expand All @@ -142,7 +142,7 @@ HRESULT DirectWriteResources::InitResources(
std::wregex(STYLEORWEIGHT, std::wregex::icase), L"");
hResult = pDWFactory->CreateTextFormat(
_mainFontFace.c_str(), NULL, fontWeight, fontStyle,
DWRITE_FONT_STRETCH_NORMAL, font_point * dpiScaleX_, L"",
DWRITE_FONT_STRETCH_NORMAL, font_point * dpiScaleFontPoint, L"",
reinterpret_cast<IDWriteTextFormat**>(pTextFormat.GetAddressOf()));
if (pTextFormat != NULL) {
if (vertical_text) {
Expand All @@ -169,7 +169,7 @@ HRESULT DirectWriteResources::InitResources(
std::wregex(STYLEORWEIGHT, std::wregex::icase), L"");
hResult = pDWFactory->CreateTextFormat(
_mainFontFace.c_str(), NULL, fontWeight, fontStyle,
DWRITE_FONT_STRETCH_NORMAL, font_point * dpiScaleX_, L"",
DWRITE_FONT_STRETCH_NORMAL, font_point * dpiScaleFontPoint, L"",
reinterpret_cast<IDWriteTextFormat**>(pPreeditTextFormat.GetAddressOf()));
if (pPreeditTextFormat != NULL) {
if (vertical_text) {
Expand Down Expand Up @@ -199,7 +199,7 @@ HRESULT DirectWriteResources::InitResources(
std::wregex(STYLEORWEIGHT, std::wregex::icase), L"");
hResult = pDWFactory->CreateTextFormat(
_mainFontFace.c_str(), NULL, fontWeight, fontStyle,
DWRITE_FONT_STRETCH_NORMAL, label_font_point * dpiScaleX_, L"",
DWRITE_FONT_STRETCH_NORMAL, label_font_point * dpiScaleFontPoint, L"",
reinterpret_cast<IDWriteTextFormat**>(pLabelTextFormat.GetAddressOf()));
if (pLabelTextFormat != NULL) {
if (vertical_text) {
Expand Down Expand Up @@ -228,7 +228,7 @@ HRESULT DirectWriteResources::InitResources(
std::wregex(STYLEORWEIGHT, std::wregex::icase), L"");
hResult = pDWFactory->CreateTextFormat(
_mainFontFace.c_str(), NULL, fontWeight, fontStyle,
DWRITE_FONT_STRETCH_NORMAL, comment_font_point * dpiScaleX_, L"",
DWRITE_FONT_STRETCH_NORMAL, comment_font_point * dpiScaleFontPoint, L"",
reinterpret_cast<IDWriteTextFormat**>(pCommentTextFormat.GetAddressOf()));
if (pCommentTextFormat != NULL) {
if (vertical_text) {
Expand All @@ -255,8 +255,8 @@ HRESULT DirectWriteResources::InitResources(const UIStyle& style,
const UINT& dpi = 96) {
_style = style;
if (dpi) {
dpiScaleX_ = dpi / 72.0f;
dpiScaleY_ = dpi / 72.0f;
dpiScaleFontPoint = dpi / 72.0f;
dpiScaleLayout = dpi / 96.0f;
}
return InitResources(style.label_font_face, style.label_font_point,
style.font_face, style.font_point,
Expand All @@ -265,8 +265,8 @@ HRESULT DirectWriteResources::InitResources(const UIStyle& style,
}

void weasel::DirectWriteResources::SetDpi(const UINT& dpi) {
dpiScaleX_ = dpi / 72.0f;
dpiScaleY_ = dpi / 72.0f;
dpiScaleFontPoint = dpi / 72.0f;
dpiScaleLayout = dpi / 96.0f;

pPreeditTextFormat.Reset();
pTextFormat.Reset();
Expand Down
11 changes: 6 additions & 5 deletions WeaselUI/FullScreenLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,18 @@ bool FullScreenLayout::AdjustFontPoint(CDCHandle dc,
int fontPointComment;

if (pDWR->pLabelTextFormat != NULL)
fontPointLabel =
(int)(pDWR->pLabelTextFormat->GetFontSize() / pDWR->dpiScaleX_);
fontPointLabel = (int)(pDWR->pLabelTextFormat->GetFontSize() /
pDWR->dpiScaleFontPoint);
else
fontPointLabel = 0;
if (pDWR->pTextFormat != NULL)
fontPoint = (int)(pDWR->pTextFormat->GetFontSize() / pDWR->dpiScaleX_);
fontPoint =
(int)(pDWR->pTextFormat->GetFontSize() / pDWR->dpiScaleFontPoint);
else
fontPoint = 0;
if (pDWR->pCommentTextFormat != NULL)
fontPointComment =
(int)(pDWR->pCommentTextFormat->GetFontSize() / pDWR->dpiScaleX_);
fontPointComment = (int)(pDWR->pCommentTextFormat->GetFontSize() /
pDWR->dpiScaleFontPoint);
else
fontPointComment = 0;
CSize sz = m_layout->GetContentSize();
Expand Down
5 changes: 3 additions & 2 deletions WeaselUI/FullScreenLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class FullScreenLayout : public StandardLayout {
const Context& context,
const Status& status,
const CRect& inputPos,
Layout* layout)
: StandardLayout(style, context, status),
Layout* layout,
PDWR pDWR)
: StandardLayout(style, context, status, pDWR),
mr_inputPos(inputPos),
m_layout(layout) {}
virtual ~FullScreenLayout() { delete m_layout; }
Expand Down
5 changes: 3 additions & 2 deletions WeaselUI/HorizontalLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class HorizontalLayout : public StandardLayout {
public:
HorizontalLayout(const UIStyle& style,
const Context& context,
const Status& status)
: StandardLayout(style, context, status) {}
const Status& status,
PDWR pDWR)
: StandardLayout(style, context, status, pDWR) {}
virtual void DoLayout(CDCHandle dc, PDWR pDWR = NULL);
};
}; // namespace weasel
35 changes: 28 additions & 7 deletions WeaselUI/Layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ using namespace weasel;

Layout::Layout(const UIStyle& style,
const Context& context,
const Status& status)
const Status& status,
PDWR pDWR)
: _style(style),
_context(context),
_status(status),
Expand All @@ -13,15 +14,35 @@ Layout::Layout(const UIStyle& style,
labels(_context.cinfo.labels),
id(_context.cinfo.highlighted),
candidates_count((int)candidates.size()),
real_margin_x((abs(_style.margin_x) > _style.hilite_padding_x)
? abs(_style.margin_x)
: _style.hilite_padding_x),
real_margin_y((abs(_style.margin_y) > _style.hilite_padding_y)
? abs(_style.margin_y)
: _style.hilite_padding_y),
labelFontValid(!!(_style.label_font_point > 0)),
textFontValid(!!(_style.font_point > 0)),
cmtFontValid(!!(_style.comment_font_point > 0)) {
if (pDWR) {
float scale = pDWR->dpiScaleLayout;
_style.min_width = (int)(_style.min_width * scale);
_style.min_height = (int)(_style.min_height * scale);
_style.max_width = (int)(_style.max_width * scale);
_style.max_height = (int)(_style.max_height * scale);
_style.border = (int)(_style.border * scale);
_style.margin_x = (int)(_style.margin_x * scale);
_style.margin_y = (int)(_style.margin_y * scale);
_style.spacing = (int)(_style.spacing * scale);
_style.candidate_spacing = (int)(_style.candidate_spacing * scale);
_style.hilite_spacing = (int)(_style.hilite_spacing * scale);
_style.hilite_padding_x = (int)(_style.hilite_padding_x * scale);
_style.hilite_padding_y = (int)(_style.hilite_padding_y * scale);
_style.round_corner = (int)(_style.round_corner * scale);
_style.round_corner_ex = (int)(_style.round_corner_ex * scale);
_style.shadow_radius = (int)(_style.shadow_radius * scale);
_style.shadow_offset_x = (int)(_style.shadow_offset_x * scale);
_style.shadow_offset_y = (int)(_style.shadow_offset_y * scale);
}
real_margin_x = ((abs(_style.margin_x) > _style.hilite_padding_x)
? abs(_style.margin_x)
: _style.hilite_padding_x);
real_margin_y = ((abs(_style.margin_y) > _style.hilite_padding_y)
? abs(_style.margin_y)
: _style.hilite_padding_y);
offsetX = offsetY = 0;
if (_style.shadow_radius != 0) {
offsetX = abs(_style.shadow_offset_x) + _style.shadow_radius * 2;
Expand Down
11 changes: 7 additions & 4 deletions WeaselUI/Layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ struct IsToRoundStruct {

class Layout {
public:
Layout(const UIStyle& style, const Context& context, const Status& status);
Layout(const UIStyle& style,
const Context& context,
const Status& status,
PDWR pDWR);

virtual void DoLayout(CDCHandle dc, PDWR pDWR = NULL) = 0;
/* All points in this class is based on the content area */
Expand Down Expand Up @@ -104,18 +107,18 @@ class Layout {
int mark_width = 4;
int mark_gap = 8;
int mark_height = 0;
int real_margin_x;
int real_margin_y;
UIStyle _style;

protected:
const UIStyle& _style;
const Context& _context;
const Status& _status;
const std::vector<Text>& candidates;
const std::vector<Text>& comments;
const std::vector<Text>& labels;
const int& id;
const int candidates_count;
const int real_margin_x;
const int real_margin_y;
const int labelFontValid;
const int textFontValid;
const int cmtFontValid;
Expand Down
5 changes: 3 additions & 2 deletions WeaselUI/StandardLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class StandardLayout : public Layout {
public:
StandardLayout(const UIStyle& style,
const Context& context,
const Status& status)
: Layout(style, context, status) {}
const Status& status,
PDWR pDWR)
: Layout(style, context, status, pDWR) {}

/* Layout */

Expand Down
5 changes: 3 additions & 2 deletions WeaselUI/VHorizontalLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class VHorizontalLayout : public StandardLayout {
public:
VHorizontalLayout(const UIStyle& style,
const Context& context,
const Status& status)
: StandardLayout(style, context, status) {}
const Status& status,
PDWR pDWR)
: StandardLayout(style, context, status, pDWR) {}
virtual void DoLayout(CDCHandle dc, PDWR pDWR = NULL);

private:
Expand Down
5 changes: 3 additions & 2 deletions WeaselUI/VerticalLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class VerticalLayout : public StandardLayout {
public:
VerticalLayout(const UIStyle& style,
const Context& context,
const Status& status)
: StandardLayout(style, context, status) {}
const Status& status,
PDWR pDWR)
: StandardLayout(style, context, status, pDWR) {}
virtual void DoLayout(CDCHandle dc, PDWR pDWR = NULL);
};
}; // namespace weasel
Loading

0 comments on commit 5d1bf58

Please sign in to comment.