Windows version wcswidth_l

I have some text to write to the Windows console where I need to know the real width of the columns. wcswidth_l seems to be the best option on platforms that have it (although mbswidth_l()

it would be better since I have no desire to use wchar_t, but for some reason it doesn't exist). But in addition to other platforms, I need something that works on Windows. While it's unlikely there is a portable solution, I don't know anything about Windows at all. I think the console has an API to get the cursor position etc., so I can write some text and check the position change. That would be accurate, I think, but writing an additional conclusion is generally not acceptable.

How can I get the column width of a string or character on Windows?


wcswidth_l returns the number of console columns used to display the row. Some characters take up one column and others, for example. Japanese characters, take two.

As an example, the width of the column "a あ" is four. "a" is one, "one", and "あ" is two. (Let's assume the console is configured to actually display non-ascii characters). It would also be nice if the API supported strings using the 65001 (UTF-8) code page.


source to share

2 answers

First of all, the Windows Console API is here .

Second, is this the function you are looking for GetConsoleFontSize ?

I'll try to quickly print an example in a second.

EDIT: Here you go. Forgive me if there is a small mistake. In fact, I found it even easier. GetCurrentConsoleFont

fills in the structure COORD

on the way for the index to be passed to GetConsoleFontSize

, so the step was saved :)

#define _WIN32_WINNT 0x0501 //XP, 0x0601=windows 7
#include <windows.h>

int main()
    HANDLE hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);

    GetCurrentConsoleFont (hStdOutput, FALSE, &cfi);

    //cfi.dwFontSize.X == x size
    //cfi.dwFontSize.Y == y size



If you don't mind invisible output, you can use CreateConsoleScreenBuffer

to pretty much have an invisible console window in your command while leaving yours unchanged. GetConsoleScreenBufferInfoEx

will tell you the position of the cursor, after which you can use WriteConsole

to write to the buffer (invisible) and again check the position of the cursor against the number of characters written. Note that checking the cursor location beforehand does not require clearing the screen to use this method.

If you cannot afford to draw additional inference, visible or invisible, I am not sure if there actually is a possibility.



Portable approach

Since the width of the characters depends more on the characters themselves, rather than on the system on which they are displayed (well, there may be exceptions, but they should be quite rare), a separate function can be used for this (on Windows too), This requires Unicode characters since it makes parsing line widths easier, but it is certainly possible to write a wrapper to convert between encodings.

Available implementation

Here is a suitable and portable implementation that can be plugged into his application and returned for use on Windows.



All Articles