-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Give API to measure the space that a string occupies #218
Comments
IIRC, determining the width of a string is a pretty hard problem actually. There are all sorts of crazy Unicode edge cases to handle, there are some assumptions we make in the code manually (eg box-drawing chars are single-width always). Adding @adiviness as he has been working in that area quite a lot. I'd really doubt that we'd be adding another API to conhost. Is there an equivalent API on *nix that we could use for inspiration? |
It is false for CJK languages since 1980s using some fonts, but true for other fonts (like my Sarasa Gothic). |
I have seen multiple libraries trying to "guess" the actual width of a string, like https://github.com/martinheidegger/varsize-string If we have an accurate API then it would greatly help people writing console applications. |
on the unix side, there isn't a great API for terminal emulators. the closest are the wcwidth and wcswidth functions. they effectively operate on code points. most programs (both terminal emulators and editors/tools) tend to just use common examples of complicated rules:
the original question was about the rendering box needed for a particular grapheme in a particular font. this shouldn't matter, but in practice, a lot of fonts (including monospace ones) aren't consistent in their widths/heights. they can be narrower or wider than a single cell requiring manual intervention to center/scale them in the respective cells. freetype/fontconfig are the standard font related libraries in the unix world for rendering. along those lines, wide-characters (i.e. CJK) should be taking up two cells even if the font gets it wrong. otherwise you easily run out of sync with the console's idea of cursor location and the remote application's idea of cursor location. i grok that this might be a fundamental limitation in the existing Windows console code and is not trivial to resolve. hth. |
+1 for having such a function. |
The most important thing is not how you measure the width, it is important that the measurement of terminal app and console app agree with each other. When the width doesn't match, it will mess up all ncurses apps or tmux/screen. So instead of providing another platform dependent function, I strongly suggest using a widely used library like utf8proc (with this patch) to determine charactor width. It follows the Unicode standard mostly. And there are characters with situational width, depending on locales. Make sure you app can handle this or just use the library. |
@kghost @miniksa |
From @alabuzhev in #10592
|
fyi: there is new Unicode Terminal Complex Script Support, or TCSS proposal |
@DHowett-MSFT - I'm super interested in helping with this. At the minimum, you can count on Terminal.Gui as being a test case. Please feel free to reach out (tig (at) kindel (dot) com). |
Note that there's also Contour's Unicode Core proposal, which has already been adopted by a number of other terminals, and at least one application that I'm aware of. |
This is an extension to #57.
Under a certain console/PTY, assume the font family/size is specified, give a string, and return the space (a bit mask of the character matrix?) it would occupy.
The text was updated successfully, but these errors were encountered: