Search code examples
c#console-application

Can I get a console unicode characters screen width and if they can be overlapped?


I've dug through the properties of char and I'm not finding a way to see it. In forms, I'm could measure the font based on type and size, but clearly I can't do that with a console. I find I'm required to add a space after some Unicode characters because the following text will overlap it if I don't. Some Unicode characters are twice the width of others.

Example: Showing different sizes on screen

Currently, I'm having to eyeball it and create a map stating this information, but would like to make it more generic.

I found this, but that is more for padding over all strings and I'm building a library to generate colors, symbols, and formatting and returning a full string for a user to use Console.WriteLine(fullLineText);

I found out about Variation Selectors, but that is only if I already know which one's need them. Not much different visually, that just adding the space, like I was before. Example:

Console.OutputEncoding = Encoding.Unicode;
//Has '\ufe0f' (Color) Variation Selector
//The '\ude0e' (Monochrome) Variation Selector
Console.WriteLine("v1: [\u2620]\n" +
                  "v2: [\u2620\ufe0f]\n" +
                  "v3: [\u2620 ]");

enter image description here

Is it possible to programmatically figure it out?


Solution

  • From what I can gather, there isn't a real way outside of creating a map for everything. With Rune, I find most of the 1/2 byte looking characters have Plane set to 0 and BMP is set to True, but that isn't in all cases.

    You will notice that \u23f0 has an extra space and \ud83c\udd8f is missing the space. I guess I'll just map out the ones I'll support and leave it to the user to append a space when they need it.

    Examples I used, where it states Padded: True, means I added \ufe0f: enter image description here

    EDIT: List of all emoji characters that have the variation selectors. Color and Monochrome: https://unicode.org/Public/16.0.0/ucd/emoji/emoji-variation-sequences.txt