Skip to content
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

Fix multibyte-strings #213

Merged
merged 1 commit into from
Aug 17, 2019
Merged

Fix multibyte-strings #213

merged 1 commit into from
Aug 17, 2019

Conversation

mattn
Copy link
Contributor

@mattn mattn commented Aug 17, 2019

On Windows 10, WriteConsoleOutputW break compatibility. When writing three unicode characters, the character is displayed overlayed with 3 cells.

#include <windows.h>
#include <stdio.h>

int
main(int argc, char* argv[]) {
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	CHAR_INFO ci[3] = {};
	COORD buffer_size = { 3, 1 };
	COORD start_coord = { 0, 0 };
	SMALL_RECT sr = { 0, 0, 80, 25 };
	SHORT white = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;

	ci[0].Char.UnicodeChar = L'あ';
	ci[0].Attributes = white;
	ci[1].Char.UnicodeChar = L'い';
	ci[1].Attributes = white;
	ci[2].Char.UnicodeChar = L'う';
	ci[2].Attributes = white;
	WriteConsoleOutputW(h, (CHAR_INFO*)ci, buffer_size, start_coord, &sr);	
	return 0;
}

image

Currently, all of products using termbox-go does not work correctly with wide characters on Windows 10.

This is a problem. However, I thought that WriteConsoleOutputW have design problem for a long time. WriteConsoleOutputW can be specified buffer size and rectangle size. The buffer size is a length of char_info array. But the character might be 2 cells. So this mean that this API can be specified sizes over expected width. For example, display 80 characters with the buffer size 80, it possibly require 160 cells.

To fix this problem, we must add spaces after the each 2 cells characters.

#include <windows.h>
#include <stdio.h>

int
main(int argc, char* argv[]) {
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	CHAR_INFO ci[6] = {};
	COORD buffer_size = { 6, 1 };
	COORD start_coord = { 0, 0 };
	SMALL_RECT sr = { 0, 0, 80, 25 };
	SHORT white = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;

	ci[0].Char.UnicodeChar = L'あ';
	ci[0].Attributes = white;
	ci[1].Char.UnicodeChar = L' ';
	ci[1].Attributes = white;
	ci[2].Char.UnicodeChar = L'い';
	ci[2].Attributes = white;
	ci[3].Char.UnicodeChar = L' ';
	ci[3].Attributes = white;
	ci[4].Char.UnicodeChar = L'う';
	ci[4].Attributes = white;
	ci[5].Char.UnicodeChar = L' ';
	ci[5].Attributes = white;
	WriteConsoleOutputW(h, (CHAR_INFO*)ci, buffer_size, start_coord, &sr);	
	return 0;
}

image

On Windows 10, WriteConsoleOutputW break compatibility. When writing three
unicode characters, the character is displayed overlayed with 3 cells.
So this change insert spaces after the character for unicode characters.
@mattn
Copy link
Contributor Author

mattn commented Aug 17, 2019

@nsf FYI, the breaking compatibility of Windows 10 break also _demo/editbox and _demo/keyboard. I have a patch to fix this.

image

image

@nsf nsf merged commit 92e767c into nsf:master Aug 17, 2019
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.

None yet

2 participants