Skip to content

Use wcwidth for u_char_width - #1198

Merged
flyingmutant merged 4 commits into
cmus:masterfrom
wooosh:wcwidth
Oct 20, 2022
Merged

flyingmutant merged 4 commits into
cmus:masterfrom
wooosh:wcwidth

Conversation

@wooosh

@wooosh wooosh commented May 21, 2022 •

Copy link
Copy Markdown
Contributor

Resolves #1196.

Changes:

  • Old u_char_width was removed because it produced results inconsistent with wcwidth for many codepoints.
  • The new implementation of u_char_width uses wcwidth
  • A vendored implementation of wcwidth has been added from the musl libc, and will be used in situations where sizeof(wchar_t) != sizeof(uchar) or wcwidth does not exist.

Things that need to be addressed before this is ready to merge:

  • Test that the configure script and musl wcwidth work on windows. (see below)
  • The original u_char_width implementation would always return width of 1 for non-utf8 locales, even if it was a control character. I'm not sure if that was done for a specific reason, or if it was an oversight that never lead to issues in practice. The new implementation handle control characters even for non-utf8 locales, but this change might need to be reverted.
  • I have not yet tested this with non-CJK languages or terminals besides xterm and urxvt.

@wooosh

wooosh commented May 21, 2022

Copy link
Copy Markdown
Contributor Author

Nevermind about the first concern w/windows, I somehow got confused into thinking that cmus supported windows, and AFAICT, it does not. This might mean that the vendored implementation can be removed entirely, as windows is the only modern platform I'm aware that doesn't define wchar_t to be 4 bytes.

@gavtroy gavtroy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original u_char_width implementation would always return width of 1 for non-utf8 locales, even if it was a control character. I'm not sure if that was done for a specific reason, or if it was an oversight that never lead to issues in practice.

An oversight in 7e4ec7a, and yes I don't imagine control characters are encountered often.

windows

I'm not sure to what level it's supported, but cmus can be compiled with cygwin. I don't have cygwin to test how it handles wchar_t but google suggests it's probably 2 bytes / UTF-16.

Comment thread uchar.c Outdated
if (w >= 0)
return w;
else
return 2;

@gavtroy gavtroy May 21, 2022 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming it's for the likes of U+FFFE "�" would 1 not make more sense? (Assuming we ever get here.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be my assumption, but my reading of the original source for this function lead me to believe it returned 2 for unknown characters, for reasons that aren't really clear to me:

	/* ? */
	if (u >= 0x30000U && u <= 0x3fffdU)
		goto wide;

Maybe the intent was that overallocating space would be preferred to underallocating? I don't have a strong opinion either way, as I believe this should only be reached for private use characters, which is probably better set to a default width of 1, since they are unlikely to show up in song metadata, and if the user wants to use them in their status bar, they can manually pad it if need be.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth noting that it's not incredibly uncommon for wcwidth to be overriden with LD_PRELOAD to handle icon widths as well, for example https://github.com/powerman/wcwidth-icons.

I will change the default width to 1 unless there was a specific reason for the old behavior.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have taken /* ? */ to mean "I don't know what to call this range but it seems to be filled with wide characters".

At least U+FFFE does hit this case for me and displays as only 1 column wide, so I'd go with 1.

It's worth noting that it's not incredibly uncommon for wcwidth to be overriden with LD_PRELOAD to handle icon widths as well

Not something I would have thought of, but I suppose it shouldn't matter to us? If anything this change might let it work properly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be resolved by 9a9d878.

@wooosh

wooosh commented May 21, 2022

Copy link
Copy Markdown
Contributor Author

I'm not sure to what level it's supported, but cmus can be compiled with cygwin. I don't have cygwin to test how it handles wchar_t but google suggests it's probably 2 bytes / UTF-16.

The current code in this PR should work on windows because it will not use wchar_t if it isn't 4 bytes, but I cannot actually verify that myself without access to a windows machine. I also don't know if the terminals available for windows actually handle anything beyond 16 bit codepoints to begin with, though.

I.e. fix u_copy_chars() / u_skip_chars() to continue copying zero-width
chars (combining marks) when no display width is left.

@gavtroy gavtroy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pgaskin perhaps you can test with Cygwin at some point, although seems to me this should cover all bases.

@wooosh

wooosh commented May 23, 2022 •

Copy link
Copy Markdown
Contributor Author

This appears to work reliably on the zalgo text test album in xterm and xfce4-terminal (which is VTE based), in terms of creating correct padding, though the first track appears blank:

image

My terminal is capable of displaying the song title, so I do not believe it is a font issue:
image

It's unclear to me exactly what is happening here, a test program I've written to test the u_char_width function appears to produce "reasonable" results:

u_char_width test
$ ./wcwidth "ẹ̶͌l̴͓ ̵̜r̶̹e̴͕ẗ̵̹o̷̯r̴̲n̶̗o̵̝ ̸̣d̴̢è̵̙l̴͗ ̸̻g̵̲a̸̰m̶̫ḙ̵̬r̴̬"
codept - width
0x1eb9 - 1
0x0336 - 0
0x034c - 0
0x006c - 1
0x0334 - 0
0x0353 - 0
0x0020 - 1
0x0335 - 0
0x031c - 0
0x0072 - 1
0x0336 - 0
0x0339 - 0
0x0065 - 1
0x0334 - 0
0x0355 - 0
0x1e97 - 1
0x0335 - 0
0x0339 - 0
0x006f - 1
0x0337 - 0
0x032f - 0
0x0072 - 1
0x0334 - 0
0x0332 - 0
0x006e - 1
0x0336 - 0
0x0317 - 0
0x006f - 1
0x0335 - 0
0x031d - 0
0x0020 - 1
0x0338 - 0
0x0323 - 0
0x0064 - 1
0x0334 - 0
0x0322 - 0
0x00e8 - 1
0x0335 - 0
0x0319 - 0
0x006c - 1
0x0334 - 0
0x0357 - 0
0x0020 - 1
0x0338 - 0
0x033b - 0
0x0067 - 1
0x0335 - 0
0x0332 - 0
0x0061 - 1
0x0338 - 0
0x0330 - 0
0x006d - 1
0x0336 - 0
0x032b - 0
0x1e19 - 1
0x0335 - 0
0x032c - 0
0x0072 - 1
0x0334 - 0
0x032c - 0

My guess is that it is somehow related to the large number of combining characters, and zero width somehow meaning that the combining characters overwrite the letters they modify, but I'm not entirely sure. I'll investigate further to try to see what the issue is, especially since it worked with the old code.

edit: Interestingly, appending one or more characters to the title causes it to appear:
image

@wooosh

wooosh commented May 23, 2022 •

Copy link
Copy Markdown
Contributor Author

Furthermore, attaching that song title to another mp3 with audio and attempting to play it causes cmus to segfault.

#0  0x00005555555804b2 in ip_close (ip=0x55555597bbe0) at input.c:644
#1  0x000055555558b2cc in _producer_stop () at player.c:680
#2  _producer_stop () at player.c:677
#3  _producer_unload () at player.c:688
#4  0x000055555558cc6c in _consumer_handle_eof () at player.c:828
#5  consumer_loop (arg=<optimized out>) at player.c:908
#6  0x00007ffff7f82eae in start_thread (arg=0x7ffff604c640) at pthread_create.c:463
#7  0x00007ffff7cf12ff in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

@wooosh

wooosh commented May 23, 2022

Copy link
Copy Markdown
Contributor Author

Nevermind, I believe this is entirely unrelated to the changes I've made based on the backtrace and likely has to do something with creating two songs of the exact same name and of different file formats. The segfault is actually caused when I try to open the metadata-only ogg version while there is a audio version of the same title under a different artist.

@gavtroy

gavtroy commented May 23, 2022

Copy link
Copy Markdown
Collaborator

the first track appears blank

Although this issue doesn't exist on the current release, it's already broken on master since a strcpy somewhere was replaced by a more intelligent function. This is actually fixed by #1151 although it's not clear why from the description (and I don't remember either, but this is the exact bug that prompted the fix 🙂).

segfault

It could be a side effect of a rare allocation issue fixed by #1152... or more likely it could be a separate issue as you're thinking. But it's probably worth cherry-picking that refactoring/fix before you dig too deep.

@wooosh
wooosh marked this pull request as ready for review May 31, 2022 00:09
@wooosh

wooosh commented Jun 5, 2022

Copy link
Copy Markdown
Contributor Author

I've been using this branch for close to two weeks (with CJK, cyrllic, emojis, and zalgotext) and have been unable to reproduce the crash after cherry picking that fix, I believe this should be ready for review for merging.

@flyingmutant

Copy link
Copy Markdown
Member

@gavtroy am I correct that this should go in together with #1151 and #1152?

@gavtroy

gavtroy commented Oct 18, 2022

Copy link
Copy Markdown
Collaborator

#1151 could be merged first, since this PR was rebased on it.

#1152 isn't strictly related, other than fixing a rare crash that can be encountered when manually testing tracks with very "interesting" titles. (Although feel free to merge it at any time 🙂 )

@flyingmutant
flyingmutant merged commit b7d9d3f into cmus:master Oct 20, 2022
@flyingmutant

Copy link
Copy Markdown
Member

Thanks a lot, finally merged!

bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this pull request May 27, 2024
https://build.opensuse.org/request/show/1176840
by user mia + anag+factory
- Update to 2.11.0
  * Add support for .nomedia file
    gh#cmus/cmus#1190
  * Fix a typo in an error message
    gh#cmus/cmus#1206
  * Take opus output gain into account when checking
    replaygain_limit peak
    gh#cmus/cmus#1201
  * #1219: Update Unofficial IRC channel
    gh#cmus/cmus#1220
  * Recognize artist/album sort keys from ffmpeg plugin
    gh#cmus/cmus#1165
  * Fix missing combining marks on last character in a field
    gh#cmus/cmus#1151
  * Use wcwidth for u_char_width
    gh#cmus/cmus#1198
  * Fix under-allocation when many zero-width characters are
    present
    gh#cmus/cmus#1152
  * Draw title and command lines on first update
    gh#cmus/cmus#1257
  * Switch to -latest os in build workflow
    gh#cmus/cmus#1265
  * ip/mp4: Use MP4FindTrackId() rather than assuming it's
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmus does not properly detect width of Japanese characters

3 participants