add ncplane_putwc_stainable, ncplane_putwstr_stainable #985

This commit is contained in:
nick black 2020-09-18 01:39:02 -04:00 committed by Nick Black
parent d8cdc3996c
commit c5c608b22e
3 changed files with 62 additions and 1 deletions

View File

@ -1458,6 +1458,8 @@ ncplane_putwstr_aligned(struct ncplane* n, int y, ncalign_e align,
return ncplane_putwstr_yx(n, y, xpos, gclustarr); return ncplane_putwstr_yx(n, y, xpos, gclustarr);
} }
API int ncplane_putwstr_stainable(struct ncplane* n, const wchar_t* gclustarr);
static inline int static inline int
ncplane_putwstr(struct ncplane* n, const wchar_t* gclustarr){ ncplane_putwstr(struct ncplane* n, const wchar_t* gclustarr){
return ncplane_putwstr_yx(n, -1, -1, gclustarr); return ncplane_putwstr_yx(n, -1, -1, gclustarr);
@ -1472,12 +1474,20 @@ ncplane_putwc_yx(struct ncplane* n, int y, int x, wchar_t w){
return ncplane_putwstr_yx(n, y, x, warr); return ncplane_putwstr_yx(n, y, x, warr);
} }
// Call ncplane_putwc() at the current cursor position. // Write 'w' at the current cursor position, using the plane's current styling.
static inline int static inline int
ncplane_putwc(struct ncplane* n, wchar_t w){ ncplane_putwc(struct ncplane* n, wchar_t w){
return ncplane_putwc_yx(n, -1, -1, w); return ncplane_putwc_yx(n, -1, -1, w);
} }
// Write 'w' at the current cursor position, using any preexisting styling
// at that cell.
static inline int
ncplane_putwc_stainable(struct ncplane* n, wchar_t w){
wchar_t warr[2] = { w, L'\0' };
return ncplane_putwstr_stainable(n, warr);
}
// The ncplane equivalents of printf(3) and vprintf(3). // The ncplane equivalents of printf(3) and vprintf(3).
API int ncplane_vprintf_aligned(struct ncplane* n, int y, ncalign_e align, API int ncplane_vprintf_aligned(struct ncplane* n, int y, ncalign_e align,
const char* format, va_list ap); const char* format, va_list ap);

View File

@ -2318,6 +2318,23 @@ int ncplane_putstr_stainable(struct ncplane* n, const char* gclusters){
return ret; return ret;
} }
int ncplane_putwstr_stainable(ncplane* n, const wchar_t* gclustarr){
// maximum of six UTF8-encoded bytes per wchar_t
const size_t mbytes = (wcslen(gclustarr) * WCHAR_MAX_UTF8BYTES) + 1;
char* mbstr = 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 r = ncplane_putstr_stainable(n, mbstr);
free(mbstr);
return r;
}
int ncplane_putnstr_aligned(struct ncplane* n, int y, ncalign_e align, size_t s, const char* str){ int ncplane_putnstr_aligned(struct ncplane* n, int y, ncalign_e align, size_t s, const char* str){
char* chopped = strndup(str, s); char* chopped = strndup(str, s);
int ret = ncplane_putstr_aligned(n, y, align, chopped); int ret = ncplane_putstr_aligned(n, y, align, chopped);

View File

@ -953,6 +953,40 @@ TEST_CASE("Wide") {
CHECK(0 == notcurses_render(nc_)); CHECK(0 == notcurses_render(nc_));
} }
SUBCASE("Putwc") {
wchar_t w = L'\u2658';
CHECK(0 == ncplane_set_fg(n_, 0xff00ff));
CHECK(0 < ncplane_putwc(n_, w));
CHECK(0 == ncplane_set_fg(n_, 0x00ff00));
CHECK(0 == notcurses_render(nc_));
uint16_t stylemask;
uint64_t channels;
char* egc = notcurses_at_yx(nc_, 0, 0, &stylemask, &channels);
REQUIRE(egc);
CHECK(0 == stylemask);
CHECK(0xff00ff == channels_fg(channels));
CHECK(0 == strcmp("\u2658", egc));
free(egc);
}
SUBCASE("PutwcStainable") {
wchar_t w = L'\u2658';
CHECK(0 == ncplane_set_fg(n_, 0xff00ff));
CHECK(0 < ncplane_putwc(n_, w));
CHECK(0 == ncplane_set_fg(n_, 0x00ff00));
ncplane_home(n_);
CHECK(0 < ncplane_putwc_stainable(n_, w));
CHECK(0 == notcurses_render(nc_));
uint16_t stylemask;
uint64_t channels;
char* egc = notcurses_at_yx(nc_, 0, 0, &stylemask, &channels);
REQUIRE(egc);
CHECK(0 == stylemask);
CHECK(0xff00ff == channels_fg(channels));
CHECK(0 == strcmp("\u2658", egc));
free(egc);
}
CHECK(0 == notcurses_stop(nc_)); CHECK(0 == notcurses_stop(nc_));
} }