From c1bb41a0378736e0fdd535541daa8cd7d09a4cc6 Mon Sep 17 00:00:00 2001 From: Andrew Gregory Date: Mon, 1 Feb 2016 12:14:47 -0500 Subject: [PATCH] dump_pkg_info: fix wide character title alignment The padding added to the end of the title was based on the return value of mbstowcs which is the number of characters. This caused alignment issues for languages with characters that span multiple columns. Instead, base the padding on the number of columns needed by the translated string as returned by wcswidth. Fixes #47980 Signed-off-by: Andrew Gregory Signed-off-by: Allan McRae --- src/pacman/package.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/pacman/package.c b/src/pacman/package.c index ac474939..3ab9abc0 100644 --- a/src/pacman/package.c +++ b/src/pacman/package.c @@ -89,10 +89,12 @@ static char titles[_T_MAX][TITLE_MAXLEN * sizeof(wchar_t)]; static void make_aligned_titles(void) { unsigned int i; - size_t max = 0; + size_t maxlen = 0; + int maxcol = 0; static const wchar_t title_suffix[] = L" :"; wchar_t wbuf[ARRAYSIZE(titles)][TITLE_MAXLEN + ARRAYSIZE(title_suffix)]; size_t wlen[ARRAYSIZE(wbuf)]; + int wcol[ARRAYSIZE(wbuf)]; char *buf[ARRAYSIZE(wbuf)]; buf[T_ARCHITECTURE] = _("Architecture"); buf[T_BACKUP_FILES] = _("Backup Files"); @@ -125,14 +127,19 @@ static void make_aligned_titles(void) for(i = 0; i < ARRAYSIZE(wbuf); i++) { wlen[i] = mbstowcs(wbuf[i], buf[i], strlen(buf[i]) + 1); - if(wlen[i] > max) { - max = wlen[i]; + wcol[i] = wcswidth(wbuf[i], wlen[i]); + if(wcol[i] > maxcol) { + maxcol = wcol[i]; + } + if(wlen[i] > maxlen) { + maxlen = wlen[i]; } } for(i = 0; i < ARRAYSIZE(wbuf); i++) { - wmemset(wbuf[i] + wlen[i], L' ', max - wlen[i]); - wmemcpy(wbuf[i] + max, title_suffix, ARRAYSIZE(title_suffix)); + size_t padlen = maxcol - wcol[i]; + wmemset(wbuf[i] + wlen[i], L' ', padlen); + wmemcpy(wbuf[i] + wlen[i] + padlen, title_suffix, ARRAYSIZE(title_suffix)); wcstombs(titles[i], wbuf[i], sizeof(wbuf[i])); } }