char32: use utf8proc_charwidth() instead of wcwidth(), when available

It appears to be slightly more up-to-date with recent Unicode
versions.

In particular, it handles the new "Symbols for Legacy Computing
Supplement" block, introduced in Unicode 16.

Closes #1865
This commit is contained in:
Daniel Eklöf 2024-10-31 07:17:35 +01:00
parent f3e443ea47
commit d3cd4ad933
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 20 additions and 0 deletions

View file

@ -68,6 +68,11 @@
* OSC-9: sequences beginning with `<number>;` are now ignored. These * OSC-9: sequences beginning with `<number>;` are now ignored. These
sequences are ConEmu/Windows Terminal sequences, and not intended to sequences are ConEmu/Windows Terminal sequences, and not intended to
be notifications. be notifications.
* Use `utf8proc_charwidth()` instead of `wcwidth()`+`wcswidth()` when
calculating character width, when foot has been built with utf8proc
support ([#1865][1865]).
[1865]: https://codeberg.org/dnkl/foot/issues/1865
### Deprecated ### Deprecated

View file

@ -8,6 +8,10 @@
#include <wchar.h> #include <wchar.h>
#include <wctype.h> #include <wctype.h>
#if defined(FOOT_GRAPHEME_CLUSTERING)
#include <utf8proc.h>
#endif
static inline size_t c32len(const char32_t *s) { static inline size_t c32len(const char32_t *s) {
return wcslen((const wchar_t *)s); return wcslen((const wchar_t *)s);
} }
@ -69,11 +73,22 @@ static inline bool isc32graph(char32_t c32) {
} }
static inline int c32width(char32_t c) { static inline int c32width(char32_t c) {
#if defined(FOOT_GRAPHEME_CLUSTERING)
return utf8proc_charwidth((utf8proc_int32_t)c);
#else
return wcwidth((wchar_t)c); return wcwidth((wchar_t)c);
#endif
} }
static inline int c32swidth(const char32_t *s, size_t n) { static inline int c32swidth(const char32_t *s, size_t n) {
#if defined(FOOT_GRAPHEME_CLUSTERING)
int width = 0;
for (size_t i = 0; i < n; i++)
width += utf8proc_charwidth((utf8proc_int32_t)s[i]);
return width;
#else
return wcswidth((const wchar_t *)s, n); return wcswidth((const wchar_t *)s, n);
#endif
} }
size_t mbsntoc32(char32_t *dst, const char *src, size_t nms, size_t len); size_t mbsntoc32(char32_t *dst, const char *src, size_t nms, size_t len);