mirror of
https://github.com/fmtlib/fmt.git
synced 2026-09-13 14:22:26 +08:00
Fix inconsistent display width for emoji outside East Asian Wide ranges (#4896)
* Widen display_width_of() to cover Emoji_Presentation code points
fmt::detail::display_width_of() only treated East Asian Wide/Fullwidth
code points and two hand-picked emoji ranges as two columns wide, so
emoji outside those ranges (e.g. the Dingbats block: cross mark U+274C,
white heavy check mark U+2705) were measured as one column even though
most terminals render them double-width. This produced visibly
inconsistent padding under {:^N} compared to CJK text (fixes #4851).
Replaced the ad hoc boolean expression with a sorted table of ranges
(East Asian Wide/Fullwidth plus the full Emoji_Presentation set from
Unicode's emoji-data.txt) looked up via binary search, and added
regression tests covering the original report plus edge cases:
multiple emoji, mixed emoji/CJK/ASCII content, precision truncation,
alignment/fill variants, newly covered emoji ranges, and regional
indicator (flag) pairs.
* Make wide_cp_ranges C++11-compatible; apply clang-format
* Move wide_cp_range struct into display_width_of()
* Derive wide_cp_ranges from East_Asian_Width data, restore constexpr
* Move wide_cp_ranges to display_width_of function body
* Update format.h
* Update wide_cp_ranges array to include comments
* Add inline to display_width_of and add 1F300-1F5FF and 1F900-1F9FF ranges to match [format.string.std]
* Update display_width_of lo hi varible declaration
---------
Co-authored-by: Eduardo Gomez Saldias <50159560+edugomez102@users.noreply.github.com>
This commit is contained in:
parent
bb1bcede25
commit
d0cec7ae2e
@ -650,25 +650,179 @@ FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) {
|
||||
}
|
||||
|
||||
FMT_CONSTEXPR inline auto display_width_of(uint32_t cp) noexcept -> size_t {
|
||||
return to_unsigned(
|
||||
1 + (cp >= 0x1100 &&
|
||||
(cp <= 0x115f || // Hangul Jamo init. consonants
|
||||
cp == 0x2329 || // LEFT-POINTING ANGLE BRACKET
|
||||
cp == 0x232a || // RIGHT-POINTING ANGLE BRACKET
|
||||
// CJK ... Yi except IDEOGRAPHIC HALF FILL SPACE:
|
||||
(cp >= 0x2e80 && cp <= 0xa4cf && cp != 0x303f) ||
|
||||
(cp >= 0xac00 && cp <= 0xd7a3) || // Hangul Syllables
|
||||
(cp >= 0xf900 && cp <= 0xfaff) || // CJK Compatibility Ideographs
|
||||
(cp >= 0xfe10 && cp <= 0xfe19) || // Vertical Forms
|
||||
(cp >= 0xfe30 && cp <= 0xfe6f) || // CJK Compatibility Forms
|
||||
(cp >= 0xff00 && cp <= 0xff60) || // Fullwidth Forms
|
||||
(cp >= 0xffe0 && cp <= 0xffe6) || // Fullwidth Forms
|
||||
(cp >= 0x20000 && cp <= 0x2fffd) || // CJK
|
||||
(cp >= 0x30000 && cp <= 0x3fffd) ||
|
||||
// Miscellaneous Symbols and Pictographs + Emoticons:
|
||||
(cp >= 0x1f300 && cp <= 0x1f64f) ||
|
||||
// Supplemental Symbols and Pictographs:
|
||||
(cp >= 0x1f900 && cp <= 0x1f9ff))));
|
||||
if (cp < 0x1100) return 1;
|
||||
struct wide_cp_range {
|
||||
uint32_t first;
|
||||
uint32_t last;
|
||||
};
|
||||
// Code points with display width 2, i.e. those with the Unicode
|
||||
// East_Asian_Width property set to W(ide) or F(ullwidth)
|
||||
// (https://www.unicode.org/reports/tr11/), sorted and merged.
|
||||
constexpr wide_cp_range wide_cp_ranges[] = {
|
||||
// Hangul Jamo
|
||||
{0x1100, 0x115f},
|
||||
// Miscellaneous Technical
|
||||
{0x231a, 0x231b},
|
||||
{0x2329, 0x232a},
|
||||
{0x23e9, 0x23ec},
|
||||
{0x23f0, 0x23f0},
|
||||
{0x23f3, 0x23f3},
|
||||
// Geometric Shapes
|
||||
{0x25fd, 0x25fe},
|
||||
// Miscellaneous Symbols
|
||||
{0x2614, 0x2615},
|
||||
{0x2630, 0x2637},
|
||||
{0x2648, 0x2653},
|
||||
{0x267f, 0x267f},
|
||||
{0x268a, 0x268f},
|
||||
{0x2693, 0x2693},
|
||||
{0x26a1, 0x26a1},
|
||||
{0x26aa, 0x26ab},
|
||||
{0x26bd, 0x26be},
|
||||
{0x26c4, 0x26c5},
|
||||
{0x26ce, 0x26ce},
|
||||
{0x26d4, 0x26d4},
|
||||
{0x26ea, 0x26ea},
|
||||
{0x26f2, 0x26f3},
|
||||
{0x26f5, 0x26f5},
|
||||
{0x26fa, 0x26fa},
|
||||
{0x26fd, 0x26fd},
|
||||
// Dingbats
|
||||
{0x2705, 0x2705},
|
||||
{0x270a, 0x270b},
|
||||
{0x2728, 0x2728},
|
||||
{0x274c, 0x274c},
|
||||
{0x274e, 0x274e},
|
||||
{0x2753, 0x2755},
|
||||
{0x2757, 0x2757},
|
||||
{0x2795, 0x2797},
|
||||
{0x27b0, 0x27b0},
|
||||
{0x27bf, 0x27bf},
|
||||
// Miscellaneous Symbols and Arrows
|
||||
{0x2b1b, 0x2b1c},
|
||||
{0x2b50, 0x2b50},
|
||||
{0x2b55, 0x2b55},
|
||||
// CJK Radicals Supplement
|
||||
{0x2e80, 0x2e99},
|
||||
{0x2e9b, 0x2ef3},
|
||||
// Kangxi Radicals
|
||||
{0x2f00, 0x2fd5},
|
||||
// Ideographic Description Characters .. CJK Symbols and Punctuation
|
||||
{0x2ff0, 0x303e},
|
||||
// Hiragana
|
||||
{0x3041, 0x3096},
|
||||
// Hiragana .. Katakana
|
||||
{0x3099, 0x30ff},
|
||||
// Bopomofo
|
||||
{0x3105, 0x312f},
|
||||
// Hangul Compatibility Jamo
|
||||
{0x3131, 0x318e},
|
||||
// Kanbun .. CJK Strokes
|
||||
{0x3190, 0x31e5},
|
||||
// CJK Strokes .. Enclosed CJK Letters and Months
|
||||
{0x31ef, 0x321e},
|
||||
// Enclosed CJK Letters and Months
|
||||
{0x3220, 0x3247},
|
||||
// Enclosed CJK Letters and Months .. Yi Syllables
|
||||
{0x3250, 0xa48c},
|
||||
// Yi Radicals
|
||||
{0xa490, 0xa4c6},
|
||||
// Hangul Jamo Extended-A
|
||||
{0xa960, 0xa97c},
|
||||
// Hangul Syllables
|
||||
{0xac00, 0xd7a3},
|
||||
// CJK Compatibility Ideographs
|
||||
{0xf900, 0xfaff},
|
||||
// Vertical Forms
|
||||
{0xfe10, 0xfe19},
|
||||
// CJK Compatibility Forms .. Small Form Variants
|
||||
{0xfe30, 0xfe52},
|
||||
// Small Form Variants
|
||||
{0xfe54, 0xfe66},
|
||||
{0xfe68, 0xfe6b},
|
||||
// Halfwidth and Fullwidth Forms
|
||||
{0xff01, 0xff60},
|
||||
{0xffe0, 0xffe6},
|
||||
// Ideographic Symbols and Punctuation
|
||||
{0x16fe0, 0x16fe4},
|
||||
{0x16ff0, 0x16ff1},
|
||||
// Tangut
|
||||
{0x17000, 0x187f7},
|
||||
// Tangut Components .. Khitan Small Script
|
||||
{0x18800, 0x18cd5},
|
||||
// Khitan Small Script .. Tangut Supplement
|
||||
{0x18cff, 0x18d08},
|
||||
// Kana Extended-B
|
||||
{0x1aff0, 0x1aff3},
|
||||
{0x1aff5, 0x1affb},
|
||||
{0x1affd, 0x1affe},
|
||||
// Kana Supplement .. Kana Extended-A
|
||||
{0x1b000, 0x1b122},
|
||||
// Small Kana Extension
|
||||
{0x1b132, 0x1b132},
|
||||
{0x1b150, 0x1b152},
|
||||
{0x1b155, 0x1b155},
|
||||
{0x1b164, 0x1b167},
|
||||
// Nushu
|
||||
{0x1b170, 0x1b2fb},
|
||||
// Tai Xuan Jing Symbols
|
||||
{0x1d300, 0x1d356},
|
||||
// Counting Rod Numerals
|
||||
{0x1d360, 0x1d376},
|
||||
// Mahjong Tiles
|
||||
{0x1f004, 0x1f004},
|
||||
// Playing Cards
|
||||
{0x1f0cf, 0x1f0cf},
|
||||
// Enclosed Alphanumeric Supplement
|
||||
{0x1f18e, 0x1f18e},
|
||||
{0x1f191, 0x1f19a},
|
||||
// Enclosed Ideographic Supplement
|
||||
{0x1f200, 0x1f202},
|
||||
{0x1f210, 0x1f23b},
|
||||
{0x1f240, 0x1f248},
|
||||
{0x1f250, 0x1f251},
|
||||
{0x1f260, 0x1f265},
|
||||
// Miscellaneous Symbols and Pictographs .. Emoticons, treated as
|
||||
// fully wide per [format.string.std] regardless of East_Asian_Width.
|
||||
{0x1f300, 0x1f64f},
|
||||
// Transport and Map Symbols
|
||||
{0x1f680, 0x1f6c5},
|
||||
{0x1f6cc, 0x1f6cc},
|
||||
{0x1f6d0, 0x1f6d2},
|
||||
{0x1f6d5, 0x1f6d7},
|
||||
{0x1f6dc, 0x1f6df},
|
||||
{0x1f6eb, 0x1f6ec},
|
||||
{0x1f6f4, 0x1f6fc},
|
||||
// Geometric Shapes Extended
|
||||
{0x1f7e0, 0x1f7eb},
|
||||
{0x1f7f0, 0x1f7f0},
|
||||
// Supplemental Symbols and Pictographs, treated as fully wide per
|
||||
// [format.string.std] regardless of East_Asian_Width.
|
||||
{0x1f900, 0x1f9ff},
|
||||
// Symbols and Pictographs Extended-A
|
||||
{0x1fa70, 0x1fa7c},
|
||||
{0x1fa80, 0x1fa89},
|
||||
{0x1fa8f, 0x1fac6},
|
||||
{0x1face, 0x1fadc},
|
||||
{0x1fadf, 0x1fae9},
|
||||
{0x1faf0, 0x1faf8},
|
||||
// CJK Unified Ideographs Extension B (plane 2)
|
||||
{0x20000, 0x2fffd},
|
||||
// CJK Unified Ideographs Extension G (plane 3)
|
||||
{0x30000, 0x3fffd},
|
||||
};
|
||||
size_t lo = 0;
|
||||
size_t hi = sizeof(wide_cp_ranges) / sizeof(wide_cp_range);
|
||||
while (lo < hi) {
|
||||
size_t mid = lo + (hi - lo) / 2;
|
||||
if (cp < wide_cp_ranges[mid].first)
|
||||
hi = mid;
|
||||
else if (cp > wide_cp_ranges[mid].last)
|
||||
lo = mid + 1;
|
||||
else
|
||||
return 2;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
template <typename T> struct is_integral : std::is_integral<T> {};
|
||||
|
||||
@ -606,6 +606,71 @@ TEST(format_test, display_width_precision) {
|
||||
EXPECT_EQ(fmt::format("{:.5}", "🐱🐱🐱"), "🐱🐱");
|
||||
}
|
||||
|
||||
TEST(format_test, display_width_emoji) {
|
||||
// U+2705 and U+274C are Emoji_Presentation code points outside the East
|
||||
// Asian Wide ranges; they should still occupy two columns like other
|
||||
// emoji (https://github.com/fmtlib/fmt/issues/4851).
|
||||
EXPECT_EQ(fmt::format("{:^6}", "✅"), " ✅ ");
|
||||
EXPECT_EQ(fmt::format("{:^6}", "❌"), " ❌ ");
|
||||
}
|
||||
|
||||
// Reproduces the exact example from
|
||||
// https://github.com/fmtlib/fmt/issues/4851: emoji should be centered like
|
||||
// other double-width (e.g. CJK) text instead of like single-width text.
|
||||
TEST(format_test, display_width_issue_4851) {
|
||||
EXPECT_EQ(fmt::format("{:^20}", 12345), " 12345 ");
|
||||
EXPECT_EQ(fmt::format("{:^20}", "normal string"), " normal string ");
|
||||
EXPECT_EQ(fmt::format("{:^20}", "❌"), " ❌ ");
|
||||
EXPECT_EQ(fmt::format("{:^20}", "✅"), " ✅ ");
|
||||
EXPECT_EQ(fmt::format("{:^20}", "Müller"), " Müller ");
|
||||
EXPECT_EQ(fmt::format("{:^20}", "我"), " 我 ");
|
||||
}
|
||||
|
||||
TEST(format_test, display_width_multiple_emoji) {
|
||||
// Several Emoji_Presentation code points back to back, each contributing
|
||||
// two columns.
|
||||
EXPECT_EQ(fmt::format("{:^10}", "❌✅"), " ❌✅ ");
|
||||
EXPECT_EQ(fmt::format("{:^12}", "❌✅❌"), " ❌✅❌ ");
|
||||
// Mixing an already-supported emoji range (🐱, U+1F431) with a newly
|
||||
// covered one (✅, U+2705).
|
||||
EXPECT_EQ(fmt::format("{:^10}", "🐱✅"), " 🐱✅ ");
|
||||
}
|
||||
|
||||
TEST(format_test, display_width_mixed_content) {
|
||||
// ASCII + new-range emoji + CJK in the same string.
|
||||
EXPECT_EQ(fmt::format("{:^11}", "A✅我"), " A✅我 ");
|
||||
// Accented Latin (each combined character is one column) + emoji + ASCII.
|
||||
EXPECT_EQ(fmt::format("{:^16}", "Müller❌!"), " Müller❌! ");
|
||||
}
|
||||
|
||||
TEST(format_test, display_width_precision_multiple_emoji) {
|
||||
// Precision truncates by display width, not code point count: a third
|
||||
// two-column emoji would push the total past the limit in both cases.
|
||||
EXPECT_EQ(fmt::format("{:.5}", "❌✅❌"), "❌✅");
|
||||
EXPECT_EQ(fmt::format("{:.4}", "❌✅❌"), "❌✅");
|
||||
}
|
||||
|
||||
TEST(format_test, display_width_emoji_alignment) {
|
||||
EXPECT_EQ(fmt::format("{:<10}", "✅"), "✅ ");
|
||||
EXPECT_EQ(fmt::format("{:>10}", "✅"), " ✅");
|
||||
EXPECT_EQ(fmt::format("{:*^10}", "✅"), "****✅****");
|
||||
}
|
||||
|
||||
TEST(format_test, display_width_new_emoji_ranges) {
|
||||
// Spot-check ranges added outside the two blocks fmt already supported
|
||||
// (Transport and Map Symbols, Symbols and Pictographs Extended-A).
|
||||
EXPECT_EQ(fmt::format("{:^10}", "🚗"), " 🚗 "); // U+1F697
|
||||
EXPECT_EQ(fmt::format("{:^10}", "🫠"), " 🫠 "); // U+1FAE0
|
||||
}
|
||||
|
||||
TEST(format_test, display_width_regional_indicator_pair) {
|
||||
// A flag is two regional indicator code points; each is Neutral under
|
||||
// East_Asian_Width (not Wide), so each is measured as one column, giving
|
||||
// the pair a total of two columns, matching how terminals render the flag
|
||||
// as a single two-column glyph.
|
||||
EXPECT_EQ(fmt::format("{:^10}", "🇺🇸"), " 🇺🇸 ");
|
||||
}
|
||||
|
||||
template <int N> struct test_format {
|
||||
template <typename... T>
|
||||
static auto format(fmt::string_view fmt, const T&... args) -> std::string {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user