Files
PowerToys/src/modules/cmdpal/Microsoft.Terminal.UI/FontIconGlyphClassifier.cpp

177 lines
5.6 KiB
C++
Raw Normal View History

CmdPal: Enhance font icon classification and visuals (#41573) ## Summary of the Pull Request - Introduces `FontIconGlyphClassifier` for classifying emojis and symbols. - Correctly recognizes multi-codepoint glyphs (e.g., 🧙🏼‍♀️ *woman mage with medium-light skin tone*). - Explicitly disallows multi-glyph icons (they would overflow anyway). - Distinguishes between emojis and regular text characters (letters, numbers, symbols), since emojis are slightly larger and require different padding. - Recognizes Unicode [Variation Selectors](https://en.wikipedia.org/wiki/Variation_Selectors_(Unicode_block)) to enforce specific styles: VS15 (U+FE0E) for text style (monochrome) and VS16 (U+FE0F) for emoji style (color). This lets developers choose which variant to display. By default, characters with both representations render as text/monochrome (e.g., ▶ `\u25B6`): <img width="428" height="39" alt="image" src="https://github.com/user-attachments/assets/c5e6865f-61de-4f45-9f3a-4e15e5e5ceb8" /> - Invalid icons are displayed as a dashed circle so extension developers can spot issues, without being overly distracting if they slip into production. - Updates `IconPathConverter` to use the new classifier for improved icon handling. - Adds `SampleIconPage` to demonstrate various icon usages and classifications. - Adjusts icon alignment in `IconBox` so icons are centered. - Scales negative padding for emojis in `IconBox` with control size, fixing misalignment and clipping (noticeable in tags and the details pane hero image). - Applies negative padding to all font icons. This removes the need for classification in these cases and ensures symbols rendered below the baseline remain visible. Based on [microsoft/terminal#19143](https://github.com/microsoft/terminal/pull/19143): Co-authored-by: Dustin L. Howett <duhowett@microsoft.com> Pictures? Pictures! <img width="1912" height="2394" alt="image" src="https://github.com/user-attachments/assets/05a16309-b658-4f21-8f9d-9a3f20db6ad8" /> Keyboard and flag/country emojis may look a bit off, but that’s how they’re actually rendered: <img width="482" height="95" alt="image" src="https://github.com/user-attachments/assets/dc7d4d0d-3dc8-4df5-9b9f-9e977e7e989f" /> <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: - #41489 - #41496 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed
2025-09-03 20:17:52 +02:00
#include "pch.h"
#include "FontIconGlyphClassifier.h"
#include "FontIconGlyphClassifier.g.cpp"
#include <icu.h>
#include <utility>
namespace winrt::Microsoft::Terminal::UI::implementation
{
namespace
{
// Check if the code point is in the Private Use Area range used by Fluent UI icons.
[[nodiscard]] constexpr bool _isFluentIconPua(const UChar32 cp) noexcept
{
Using centralized package management for vcxproj (#43920) ## Summary of the Pull Request This pull request updates the build system for several native and managed projects, modernizing NuGet package management and improving code analysis configuration. The main changes involve switching from legacy `packages.config` and manual `.props`/`.targets` imports to PackageReference style for native projects, updating package versions, and streamlining code analysis settings. **Build system modernization and package management:** * Migrated native projects (`PowerToys.MeasureToolCore.vcxproj`, `FindMyMouse.vcxproj`) from legacy `packages.config` and manual `.props`/`.targets` imports to NuGet PackageReference style, simplifying dependency management and build configuration. This includes removing the `packages.config` file and related import/error logic, and introducing `PackageReference` items for required packages. [[1]](diffhunk://#diff-76320b3a74a9241df46edb536ba0f817d7150ddf76bb0fe677e2b276f8bae95aL3-R18) [[2]](diffhunk://#diff-76320b3a74a9241df46edb536ba0f817d7150ddf76bb0fe677e2b276f8bae95aR41-L41) [[3]](diffhunk://#diff-76320b3a74a9241df46edb536ba0f817d7150ddf76bb0fe677e2b276f8bae95aL145-R153) [[4]](diffhunk://#diff-d3a7d80ebbca915b42727633451e769ed2306b418ef3d82b3b04fd5f79560f17L1-L17) [[5]](diffhunk://#diff-0f27869c4e90c8fd2c81f5688c58da99afcc9e5767e69ef7938265dbb6928e0fL3-R13) * Updated the centralized package versions in `Directory.Packages.props`, adding new entries for `boost`, `boost_regex-vc143`, `Microsoft.Windows.ImplementationLibrary`, and `Microsoft.WindowsAppSDK.Foundation` to support the new build system and dependencies. [[1]](diffhunk://#diff-5baf5f9e448ad54ab25a091adee0da05d4d228481c9200518fcb1b53a65d4156R10-R11) [[2]](diffhunk://#diff-5baf5f9e448ad54ab25a091adee0da05d4d228481c9200518fcb1b53a65d4156R74-R77) **Code analysis improvements:** * Added configuration to both native and managed projects (`PowerToys.MeasureToolCore.vcxproj`, `MeasureToolUI.csproj`) to suppress specific warnings (81010002) and exclude NuGet cache files from code analysis, reducing noise and improving build performance. [[1]](diffhunk://#diff-76320b3a74a9241df46edb536ba0f817d7150ddf76bb0fe677e2b276f8bae95aL3-R18) [[2]](diffhunk://#diff-4f2b49a1a5cc7da36ee6d5044792ef681fd0ea5bea12db9ebd4c3090680d4b07R6-R11) **Project reference and output handling:** * Updated the managed project (`MeasureToolUI.csproj`) to handle native project outputs more robustly, ensuring the WinMD and DLL files are available at runtime and configuring the project reference to avoid assembly reference issues. **Compiler configuration:** * Enhanced C++ compiler settings in `Cpp.Build.props` to treat angle-bracket includes as external, disable warnings and analysis for external headers, and optimize build performance.
2025-12-08 09:52:55 +08:00
constexpr UChar32 fluentIconsPrivateUseAreaStart = 0xE700;
constexpr UChar32 fluentIconsPrivateUseAreaEnd = 0xF8FF;
return cp >= fluentIconsPrivateUseAreaStart && cp <= fluentIconsPrivateUseAreaEnd;
CmdPal: Enhance font icon classification and visuals (#41573) ## Summary of the Pull Request - Introduces `FontIconGlyphClassifier` for classifying emojis and symbols. - Correctly recognizes multi-codepoint glyphs (e.g., 🧙🏼‍♀️ *woman mage with medium-light skin tone*). - Explicitly disallows multi-glyph icons (they would overflow anyway). - Distinguishes between emojis and regular text characters (letters, numbers, symbols), since emojis are slightly larger and require different padding. - Recognizes Unicode [Variation Selectors](https://en.wikipedia.org/wiki/Variation_Selectors_(Unicode_block)) to enforce specific styles: VS15 (U+FE0E) for text style (monochrome) and VS16 (U+FE0F) for emoji style (color). This lets developers choose which variant to display. By default, characters with both representations render as text/monochrome (e.g., ▶ `\u25B6`): <img width="428" height="39" alt="image" src="https://github.com/user-attachments/assets/c5e6865f-61de-4f45-9f3a-4e15e5e5ceb8" /> - Invalid icons are displayed as a dashed circle so extension developers can spot issues, without being overly distracting if they slip into production. - Updates `IconPathConverter` to use the new classifier for improved icon handling. - Adds `SampleIconPage` to demonstrate various icon usages and classifications. - Adjusts icon alignment in `IconBox` so icons are centered. - Scales negative padding for emojis in `IconBox` with control size, fixing misalignment and clipping (noticeable in tags and the details pane hero image). - Applies negative padding to all font icons. This removes the need for classification in these cases and ensures symbols rendered below the baseline remain visible. Based on [microsoft/terminal#19143](https://github.com/microsoft/terminal/pull/19143): Co-authored-by: Dustin L. Howett <duhowett@microsoft.com> Pictures? Pictures! <img width="1912" height="2394" alt="image" src="https://github.com/user-attachments/assets/05a16309-b658-4f21-8f9d-9a3f20db6ad8" /> Keyboard and flag/country emojis may look a bit off, but that’s how they’re actually rendered: <img width="482" height="95" alt="image" src="https://github.com/user-attachments/assets/dc7d4d0d-3dc8-4df5-9b9f-9e977e7e989f" /> <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: - #41489 - #41496 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed
2025-09-03 20:17:52 +02:00
}
// Determine if the given text (as a sequence of UChar code units) is emoji
[[nodiscard]] bool _isEmoji(const UChar* p, const int32_t length) noexcept
{
if (!p || length < 1)
{
return false;
}
// https://www.unicode.org/reports/tr51/#Emoji_Variation_Selector_Notes
constexpr UChar32 vs15CodePoint = 0xFE0E; // Variation Selectors 15: text variation selector
constexpr UChar32 vs16CodePoint = 0xFE0F; // Variation Selectors: 16 emoji variation selector
// Decode the first code point correctly (surrogate-safe)
int32_t i0{ 0 };
UChar32 first{ 0 };
U16_NEXT(p, i0, length, first);
for (int32_t i = 0; i < length;)
{
UChar32 cp{ 0 };
U16_NEXT(p, i, length, cp);
if (cp == vs16CodePoint) { return true; }
if (cp == vs15CodePoint) { return false; }
}
return !U_IS_SURROGATE(first) && u_hasBinaryProperty(first, UCHAR_EMOJI_PRESENTATION);
}
}
bool FontIconGlyphClassifier::IsLikelyToBeEmojiOrSymbolIcon(const hstring& text)
{
if (text.empty())
{
return false;
}
if (text.size() == 1 && !IS_HIGH_SURROGATE(text[0]))
{
// If it's a single code unit, it's definitely either zero or one grapheme clusters.
// If it turns out to be illegal Unicode, we don't really care.
return true;
}
if (text.size() >= 2 && text[0] <= 0x7F && text[1] <= 0x7F)
{
// Two adjacent ASCII characters (as seen in most file paths) aren't a single
// grapheme cluster.
return false;
}
// Use ICU to determine whether text is composed of a single grapheme cluster.
int32_t off{ 0 };
UErrorCode status{ U_ZERO_ERROR };
UBreakIterator* const bi{ ubrk_open(UBRK_CHARACTER,
nullptr,
reinterpret_cast<const UChar*>(text.data()),
static_cast<int>(text.size()),
&status) };
if (bi)
{
if (U_SUCCESS(status))
{
off = ubrk_next(bi);
}
ubrk_close(bi);
}
return std::cmp_equal(off, text.size());
}
FontIconGlyphKind FontIconGlyphClassifier::Classify(hstring const& text) noexcept
{
if (text.empty())
{
return FontIconGlyphKind::None;
}
const size_t textSize{ text.size() };
const auto* buffer{ reinterpret_cast<const UChar*>(text.c_str()) };
// Fast path 1: Single UTF-16 code unit (most common case)
if (textSize == 1)
{
const UChar ch{ buffer[0] };
if (IS_HIGH_SURROGATE(ch))
{
return FontIconGlyphKind::Invalid;
}
if (_isFluentIconPua(ch))
{
return FontIconGlyphKind::FluentSymbol;
}
if (_isEmoji(&ch, 1))
{
return FontIconGlyphKind::Emoji;
}
return FontIconGlyphKind::Other;
}
// Fast path 2: Common file path pattern - two ASCII printable characters
if (textSize >= 2 && buffer[0] <= 0x7F && buffer[1] <= 0x7F)
{
// Definitely multiple graphemes
return FontIconGlyphKind::Invalid;
}
// Expensive path: Use ICU to determine grapheme boundaries
UErrorCode status{ U_ZERO_ERROR };
UBreakIterator* bi{ ubrk_open(UBRK_CHARACTER,
nullptr,
buffer,
static_cast<int32_t>(textSize),
&status) };
if (U_FAILURE(status) || !bi)
{
return FontIconGlyphKind::Invalid;
}
const int32_t start{ ubrk_first(bi) };
const int32_t end{ ubrk_next(bi) }; // end of first grapheme
ubrk_close(bi);
// No graphemes found
if (end == UBRK_DONE || end <= start)
{
return FontIconGlyphKind::None;
}
// If there's more than one grapheme, it's not a valid icon glyph
if (std::cmp_not_equal(end, textSize))
{
return FontIconGlyphKind::Invalid;
}
// Exactly one grapheme: classify
const UChar* grapheme = buffer + start;
const int32_t graphemeLength = end - start;
if (graphemeLength == 1 && _isFluentIconPua(grapheme[0]))
{
return FontIconGlyphKind::FluentSymbol;
}
if (_isEmoji(grapheme, graphemeLength))
{
return FontIconGlyphKind::Emoji;
}
return FontIconGlyphKind::Other;
}
}