add ncplane_putwegc() and unit test #120

pull/124/head
nick black 5 years ago
parent 15c13898ea
commit 756eebc060
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -354,6 +354,28 @@ API int ncplane_putsimple(struct ncplane* n, char c, uint32_t attr, uint64_t cha
API int ncplane_putegc(struct ncplane* n, const char* gclust, uint32_t attr,
uint64_t channels, int* sbytes);
#define WCHAR_MAX_UTF8BYTES 6
// ncplane_putegc(), but following a conversion from wchar_t to UTF-8 multibyte.
static inline int
ncplane_putwegc(struct ncplane* n, const wchar_t* gclustarr, uint32_t attr,
uint64_t channels, int* sbytes){
// maximum of six UTF8-encoded bytes per wchar_t
const size_t mbytes = (wcslen(gclustarr) * WCHAR_MAX_UTF8BYTES) + 1;
char* mbstr = (char*)malloc(mbytes); // need cast for c++ callers
if(mbstr == NULL){
return -1;
}
size_t s = wcstombs(mbstr, gclustarr, mbytes);
if(s == (size_t)-1){
free(mbstr);
return -1;
}
int ret = ncplane_putegc(n, mbstr, attr, channels, sbytes);
free(mbstr);
return ret;
}
// Write a series of cells to the current location, using the current style.
// They will be interpreted as a series of columns (according to the definition
// of ncplane_putc()). Advances the cursor by some positive number of cells
@ -362,11 +384,11 @@ API int ncplane_putegc(struct ncplane* n, const char* gclust, uint32_t attr,
// which were written before the error.
API int ncplane_putstr(struct ncplane* n, const char* gclustarr);
// ncplane_putstr(), but following a conversion from wchar_t to UTF8.
// ncplane_putstr(), but following a conversion from wchar_t to UTF-8 multibyte.
static inline int
ncplane_putwstr(struct ncplane* n, const wchar_t* gclustarr){
// maximum of six UTF8-encoded bytes per wchar_t
const size_t mbytes = (wcslen(gclustarr) * 6) + 1;
const size_t mbytes = (wcslen(gclustarr) * WCHAR_MAX_UTF8BYTES) + 1;
char* mbstr = (char*)malloc(mbytes); // need cast for c++ callers
if(mbstr == NULL){
return -1;

@ -105,8 +105,8 @@ TEST_F(NcplaneTest, RejectBadRGB) {
EXPECT_NE(0, ncplane_set_fg_rgb(n_, 256, 256, 256));
}
// Verify we can emit a wide character, and it advances the cursor
TEST_F(NcplaneTest, EmitWchar) {
// Verify we can emit a multibyte character, and it advances the cursor
TEST_F(NcplaneTest, EmitCell) {
const char cchar[] = "";
cell c{};
EXPECT_EQ(strlen(cchar), cell_load(n_, &c, cchar));
@ -118,6 +118,18 @@ TEST_F(NcplaneTest, EmitWchar) {
EXPECT_EQ(0, notcurses_render(nc_));
}
// Verify we can emit a wide character, and it advances the cursor
TEST_F(NcplaneTest, EmitWchar) {
const wchar_t w = L'';
int sbytes;
EXPECT_LT(0, ncplane_putwegc(n_, &w, 0, 0, &sbytes));
int x, y;
ncplane_cursor_yx(n_, &y, &x);
EXPECT_EQ(0, y);
EXPECT_EQ(1, x);
EXPECT_EQ(0, notcurses_render(nc_));
}
// Verify we can emit a multibyte string, and it advances the cursor
TEST_F(NcplaneTest, EmitStr) {
const char s[] = "Σιβυλλα τι θελεις; respondebat illa: αποθανειν θελω.";

Loading…
Cancel
Save