Skip to content

Commit

Permalink
Use DirectWrite to render text in the status bar
Browse files Browse the repository at this point in the history
This makes the status bar use DirectWrite to render text rather than a mix of Uniscribe and GDI.

This brings improved text rendering, including sub-pixel positioning and improved handling of colour changes.

This includes colour font support on Windows 8.1 and newer.
  • Loading branch information
reupen committed Jun 19, 2024
1 parent e884384 commit 2373afb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

### Features

- Text in the status bar is now rendered using DirectWrite.
[[#897](https://github.com/reupen/columns_ui/pull/897)]

This includes colour font support on Windows 8.1 and newer (allowing the use
of, for example, colour emojis).

- Ctrl+Tab and Shift+Ctrl+Tab can now be used in Tab stack and Playlist tabs to
switch to the next and previous tab respectively.
[[#817](https://github.com/reupen/columns_ui/pull/817)]
Expand Down
8 changes: 4 additions & 4 deletions foo_ui_columns/foo_ui_columns.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<Culture>0x0809</Culture>
</ResourceCompile>
<Link>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;Dwrite.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>DebugFastLink</GenerateDebugInformation>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down Expand Up @@ -160,7 +160,7 @@
<Culture>0x0809</Culture>
</ResourceCompile>
<Link>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;Dwrite.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>DebugFastLink</GenerateDebugInformation>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down Expand Up @@ -198,7 +198,7 @@
<Culture>0x0809</Culture>
</ResourceCompile>
<Link>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;Dwrite.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>Debug</GenerateDebugInformation>
<DataExecutionPrevention>
</DataExecutionPrevention>
Expand Down Expand Up @@ -233,7 +233,7 @@
<Culture>0x0809</Culture>
</ResourceCompile>
<Link>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;Dwrite.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>Debug</GenerateDebugInformation>
<DataExecutionPrevention>
</DataExecutionPrevention>
Expand Down
53 changes: 38 additions & 15 deletions foo_ui_columns/status_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct StatusBarState {
std::string track_count_text;
std::string volume_text;
wil::unique_hfont font;
std::optional<uih::direct_write::Context> direct_write_ctx;
std::optional<uih::direct_write::TextFormat> direct_write_text_format;
wil::unique_hbitmap lock_bitmap;
wil::unique_hicon lock_icon;
std::unique_ptr<colours::dark_mode_notifier> dark_mode_notifier;
Expand All @@ -37,7 +39,20 @@ void on_status_font_change()

state->lock_icon.reset();
state->lock_bitmap.reset();
state->font.reset(fb2k::std_api_get<fonts::manager>()->get_font(font_client_status_guid));

LOGFONT log_font{};
fb2k::std_api_get<fonts::manager>()->get_font(font_client_status_guid, log_font);

state->font.reset(CreateFontIndirect(&log_font));
state->direct_write_text_format.reset();

try {
if (!state->direct_write_ctx)
state->direct_write_ctx = std::make_optional<uih::direct_write::Context>();

state->direct_write_text_format = state->direct_write_ctx->create_text_format(log_font);
}
CATCH_LOG();

SetWindowFont(g_status, state->font.get(), TRUE);

Expand Down Expand Up @@ -172,25 +187,34 @@ std::string get_playlist_lock_text()

int calculate_volume_size(const char* p_text)
{
return win32_helpers::status_bar_get_text_width(g_status, p_text);
if (!state->direct_write_text_format)
return 0;

return state->direct_write_text_format->measure_text_width(p_text);
}

int calculate_selected_length_size(const char* p_text)
{
return std::max(win32_helpers::status_bar_get_text_width(g_status, p_text),
win32_helpers::status_bar_get_text_width(g_status, "0d 00:00:00"));
if (!state->direct_write_text_format)
return 0;

return std::max(state->direct_write_text_format->measure_text_width(p_text),
state->direct_write_text_format->measure_text_width(L"0d 00:00:00"));
}

int calculate_selected_count_size(const char* p_text)
{
return std::max(win32_helpers::status_bar_get_text_width(g_status, p_text),
win32_helpers::status_bar_get_text_width(g_status, "0,000 tracks"));
if (!state->direct_write_text_format)
return 0;

return std::max(state->direct_write_text_format->measure_text_width(p_text),
state->direct_write_text_format->measure_text_width(L"0,000 tracks"));
}

int calculate_playback_lock_size(const char* p_text)
{
return uih::get_font_height(state->font.get()) - 2_spx + 2_spx
+ win32_helpers::status_bar_get_text_width(g_status, p_text);
+ state->direct_write_text_format->measure_text_width(p_text);
}

std::string get_selected_length_text(unsigned dp = 0)
Expand Down Expand Up @@ -402,15 +426,15 @@ void draw_item_content(const HDC dc, const StatusBarPartID part_id, const std::s
const auto text_colour = get_colour(dark::ColourID::StatusBarText, colours::is_dark_mode_active());

if (part_id == StatusBarPartID::PlaybackInformation) {
text_out_colours_tab(dc, text.data(), gsl::narrow<int>(text.size()), 0, 0, &rc, FALSE, text_colour, true, false,
uih::ALIGN_LEFT);
if (state->direct_write_text_format)
uih::direct_write::text_out_columns_and_colours(
*state->direct_write_text_format, dc, text, 0, 0, rc, false, text_colour, true, true, uih::ALIGN_LEFT);
return;
}

const auto font_height = uih::get_dc_font_height(dc);
int x = rc.left;
const int y = rc.top + (wil::rect_height(rc) - font_height) / 2;
const auto icon_size = font_height - 2_spx;
int x = rc.left;

if (part_id == StatusBarPartID::PlaylistLock) {
const auto icon_y = rc.top + (wil::rect_height(rc) - icon_size) / 2;
Expand All @@ -436,10 +460,9 @@ void draw_item_content(const HDC dc, const StatusBarPartID part_id, const std::s
x += icon_size + 2_spx;
}

const auto utf16_text = pfc::stringcvt::string_wide_from_utf8(text.data(), text.size());
SetTextColor(dc, text_colour);
SetBkMode(dc, TRANSPARENT);
ExtTextOutW(dc, x, y, ETO_CLIPPED, &rc, utf16_text, gsl::narrow<UINT>(utf16_text.length()), nullptr);
if (state->direct_write_text_format)
uih::direct_write::text_out_columns_and_colours(
*state->direct_write_text_format, dc, text, x - rc.left, 0, rc, false, text_colour, false, false);
}

} // namespace
Expand Down

0 comments on commit 2373afb

Please sign in to comment.