| 1 | /* $XFree86: xc/programs/xterm/wcwidth.character,v 1.3 2001/07/29 22:08:16 tsi Exp $ */ |
| 2 | /* |
| 3 | * This is an implementation of wcwidth() and wcswidth() as defined in |
| 4 | * "The Single UNIX Specification, Version 2, The Open Group, 1997" |
| 5 | * <http://www.UNIX-systems.org/online.html> |
| 6 | * |
| 7 | * Markus Kuhn -- 2001-01-12 -- public domain |
| 8 | */ |
| 9 | |
| 10 | #include <QString> |
| 11 | |
| 12 | #ifdef HAVE_UTF8PROC |
| 13 | #include <utf8proc.h> |
| 14 | #else |
| 15 | #include <cwchar> |
| 16 | #endif |
| 17 | |
| 18 | #include "konsole_wcwidth.h" |
| 19 | |
| 20 | int konsole_wcwidth(wchar_t ucs) |
| 21 | { |
| 22 | #ifdef HAVE_UTF8PROC |
| 23 | utf8proc_category_t cat = utf8proc_category( ucs ); |
| 24 | if (cat == UTF8PROC_CATEGORY_CO) { |
| 25 | // Co: Private use area. libutf8proc makes them zero width, while tmux |
| 26 | // assumes them to be width 1, and glibc's default width is also 1 |
| 27 | return 1; |
| 28 | } |
| 29 | return utf8proc_charwidth( ucs ); |
| 30 | #else |
| 31 | return wcwidth( ucs ); |
| 32 | #endif |
| 33 | } |
| 34 | |
| 35 | // single byte char: +1, multi byte char: +2 |
| 36 | int string_width( const std::wstring & wstr ) |
| 37 | { |
| 38 | int w = 0; |
| 39 | for ( size_t i = 0; i < wstr.length(); ++i ) { |
| 40 | w += konsole_wcwidth( wstr[ i ] ); |
| 41 | } |
| 42 | return w; |
| 43 | } |
| 44 | |