mirror of
https://github.com/dankamongmen/notcurses
synced 2025-03-09 17:19:03 -04:00
plots: get width/height for pixel plots #1382
This commit is contained in:
parent
f3fafb42ee
commit
e36497866f
@ -227,7 +227,7 @@ int input_demo(ncpp::NotCurses* nc) {
|
|||||||
popts.minchannels = popts.maxchannels = 0;
|
popts.minchannels = popts.maxchannels = 0;
|
||||||
ncchannels_set_fg_rgb8(&popts.minchannels, 0x40, 0x50, 0xb0);
|
ncchannels_set_fg_rgb8(&popts.minchannels, 0x40, 0x50, 0xb0);
|
||||||
ncchannels_set_fg_rgb8(&popts.maxchannels, 0x40, 0xff, 0xd0);
|
ncchannels_set_fg_rgb8(&popts.maxchannels, 0x40, 0xff, 0xd0);
|
||||||
popts.gridtype = static_cast<ncblitter_e>(NCBLIT_8x1);
|
popts.gridtype = static_cast<ncblitter_e>(NCBLIT_PIXEL);
|
||||||
plot = ncuplot_create(pplane, &popts, 0, 0);
|
plot = ncuplot_create(pplane, &popts, 0, 0);
|
||||||
if(!plot){
|
if(!plot){
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
122
src/lib/plot.c
122
src/lib/plot.c
@ -45,12 +45,14 @@ typedef struct nc##X##plot { \
|
|||||||
\
|
\
|
||||||
int redraw_plot_##T(nc##X##plot* ncp){ \
|
int redraw_plot_##T(nc##X##plot* ncp){ \
|
||||||
ncplane_erase(ncp->plot.ncp); \
|
ncplane_erase(ncp->plot.ncp); \
|
||||||
const int scale = ncp->plot.bset->width; \
|
const int scale = ncp->plot.bset->geom == NCBLIT_PIXEL ? \
|
||||||
|
ncplane_notcurses_const(ncp->plot.ncp)->tcache.cellpixx : ncp->plot.bset->width; \
|
||||||
int dimy, dimx; \
|
int dimy, dimx; \
|
||||||
ncplane_dim_yx(ncp->plot.ncp, &dimy, &dimx); \
|
ncplane_dim_yx(ncp->plot.ncp, &dimy, &dimx); \
|
||||||
const int scaleddim = dimx * scale; \
|
const int scaleddim = dimx * scale; \
|
||||||
/* each transition is worth this much change in value */ \
|
/* each transition is worth this much change in value */ \
|
||||||
const size_t states = ncp->plot.bset->height + 1; \
|
const size_t states = (ncp->plot.bset->geom == NCBLIT_PIXEL ? \
|
||||||
|
ncplane_notcurses_const(ncp->plot.ncp)->tcache.cellpixy : ncp->plot.bset->height) + 1; \
|
||||||
/* FIXME can we not rid ourselves of this meddlesome double? either way, the \
|
/* FIXME can we not rid ourselves of this meddlesome double? either way, the \
|
||||||
interval is one row's range (for linear plots), or the base (base^slots== \
|
interval is one row's range (for linear plots), or the base (base^slots== \
|
||||||
maxy-miny) of the range (for exponential plots). */ \
|
maxy-miny) of the range (for exponential plots). */ \
|
||||||
@ -138,68 +140,70 @@ int redraw_plot_##T(nc##X##plot* ncp){ \
|
|||||||
calc_gradient_channels(&channels, ncp->plot.minchannels, ncp->plot.minchannels, \
|
calc_gradient_channels(&channels, ncp->plot.minchannels, ncp->plot.minchannels, \
|
||||||
ncp->plot.maxchannels, ncp->plot.maxchannels, y, x, dimy, dimx); \
|
ncp->plot.maxchannels, ncp->plot.maxchannels, y, x, dimy, dimx); \
|
||||||
ncplane_set_channels(ncp->plot.ncp, channels); \
|
ncplane_set_channels(ncp->plot.ncp, channels); \
|
||||||
size_t egcidx = 0, sumidx = 0; \
|
if(egc){ \
|
||||||
/* if we've got at least one interval's worth on the number of positions \
|
size_t egcidx = 0, sumidx = 0; \
|
||||||
times the number of intervals per position plus the starting offset, \
|
/* if we've got at least one interval's worth on the number of positions \
|
||||||
we're going to print *something* */ \
|
times the number of intervals per position plus the starting offset, \
|
||||||
for(int i = 0 ; i < scale ; ++i){ \
|
we're going to print *something* */ \
|
||||||
sumidx *= states; \
|
for(int i = 0 ; i < scale ; ++i){ \
|
||||||
if(intervalbase < gvals[i]){ \
|
sumidx *= states; \
|
||||||
if(ncp->plot.exponentiali){ \
|
if(intervalbase < gvals[i]){ \
|
||||||
/* we want the log-base-interval of gvals[i] */ \
|
if(ncp->plot.exponentiali){ \
|
||||||
double scaled = log(gvals[i] - ncp->miny) / log(interval); \
|
/* we want the log-base-interval of gvals[i] */ \
|
||||||
double sival = intervalbase ? log(intervalbase) / log(interval) : 0; \
|
double scaled = log(gvals[i] - ncp->miny) / log(interval); \
|
||||||
egcidx = scaled - sival; \
|
double sival = intervalbase ? log(intervalbase) / log(interval) : 0; \
|
||||||
|
egcidx = scaled - sival; \
|
||||||
|
}else{ \
|
||||||
|
egcidx = (gvals[i] - intervalbase) / interval; \
|
||||||
|
} \
|
||||||
|
if(egcidx >= states){ \
|
||||||
|
egcidx = states - 1; \
|
||||||
|
done = false; \
|
||||||
|
} \
|
||||||
|
sumidx += egcidx; \
|
||||||
}else{ \
|
}else{ \
|
||||||
egcidx = (gvals[i] - intervalbase) / interval; \
|
egcidx = 0; \
|
||||||
} \
|
} \
|
||||||
if(egcidx >= states){ \
|
/* printf(stderr, "y: %d i(scale): %d gvals[%d]: %ju egcidx: %zu sumidx: %zu interval: %f intervalbase: %ju\n", y, i, i, gvals[i], egcidx, sumidx, interval, intervalbase); */ \
|
||||||
egcidx = states - 1; \
|
|
||||||
done = false; \
|
|
||||||
} \
|
|
||||||
sumidx += egcidx; \
|
|
||||||
}else{ \
|
|
||||||
egcidx = 0; \
|
|
||||||
} \
|
} \
|
||||||
/* printf(stderr, "y: %d i(scale): %d gvals[%d]: %ju egcidx: %zu sumidx: %zu interval: %f intervalbase: %ju\n", y, i, i, gvals[i], egcidx, sumidx, interval, intervalbase); */ \
|
/* if we're not UTF8, we can only arrive here via NCBLIT_1x1 (otherwise \
|
||||||
} \
|
we would have errored out during construction). even then, however, \
|
||||||
/* if we're not UTF8, we can only arrive here via NCBLIT_1x1 (otherwise \
|
we need handle ASCII differently, since it can't print full block. \
|
||||||
we would have errored out during construction). even then, however, \
|
in ASCII mode, sumidx != 0 means swap colors and use space. in all \
|
||||||
we need handle ASCII differently, since it can't print full block. \
|
modes, sumidx == 0 means don't do shit, since we erased earlier. */ \
|
||||||
in ASCII mode, sumidx != 0 means swap colors and use space. in all \
|
/* if(sumidx)fprintf(stderr, "dimy: %d y: %d x: %d sumidx: %zu egc[%zu]: %lc\n", dimy, y, x, sumidx, sumidx, egc[sumidx]); */ \
|
||||||
modes, sumidx == 0 means don't do shit, since we erased earlier. */ \
|
if(sumidx){ \
|
||||||
/* if(sumidx)fprintf(stderr, "dimy: %d y: %d x: %d sumidx: %zu egc[%zu]: %lc\n", dimy, y, x, sumidx, sumidx, egc[sumidx]); */ \
|
if(notcurses_canutf8(ncplane_notcurses(ncp->plot.ncp))){ \
|
||||||
if(sumidx){ \
|
char utf8[MB_CUR_MAX + 1]; \
|
||||||
if(notcurses_canutf8(ncplane_notcurses(ncp->plot.ncp))){ \
|
int bytes = wctomb(utf8, egc[sumidx]); \
|
||||||
char utf8[MB_CUR_MAX + 1]; \
|
if(bytes < 0){ \
|
||||||
int bytes = wctomb(utf8, egc[sumidx]); \
|
return -1; \
|
||||||
if(bytes < 0){ \
|
} \
|
||||||
return -1; \
|
utf8[bytes] = '\0'; \
|
||||||
|
nccell* c = ncplane_cell_ref_yx(ncp->plot.ncp, dimy - y - 1, x); \
|
||||||
|
cell_set_bchannel(c, ncchannels_bchannel(channels)); \
|
||||||
|
cell_set_fchannel(c, ncchannels_fchannel(channels)); \
|
||||||
|
nccell_set_styles(c, NCSTYLE_NONE); \
|
||||||
|
if(pool_blit_direct(&ncp->plot.ncp->pool, c, utf8, bytes, 1) <= 0){ \
|
||||||
|
return -1; \
|
||||||
|
} \
|
||||||
|
}else{ \
|
||||||
|
const uint64_t swapbg = ncchannels_bchannel(channels); \
|
||||||
|
const uint64_t swapfg = ncchannels_fchannel(channels); \
|
||||||
|
ncchannels_set_bchannel(&channels, swapfg); \
|
||||||
|
ncchannels_set_fchannel(&channels, swapbg); \
|
||||||
|
ncplane_set_channels(ncp->plot.ncp, channels); \
|
||||||
|
if(ncplane_putchar_yx(ncp->plot.ncp, dimy - y - 1, x, ' ') <= 0){ \
|
||||||
|
return -1; \
|
||||||
|
} \
|
||||||
|
ncchannels_set_bchannel(&channels, swapbg); \
|
||||||
|
ncchannels_set_fchannel(&channels, swapfg); \
|
||||||
|
ncplane_set_channels(ncp->plot.ncp, channels); \
|
||||||
} \
|
} \
|
||||||
utf8[bytes] = '\0'; \
|
|
||||||
nccell* c = ncplane_cell_ref_yx(ncp->plot.ncp, dimy - y - 1, x); \
|
|
||||||
cell_set_bchannel(c, ncchannels_bchannel(channels)); \
|
|
||||||
cell_set_fchannel(c, ncchannels_fchannel(channels)); \
|
|
||||||
nccell_set_styles(c, NCSTYLE_NONE); \
|
|
||||||
if(pool_blit_direct(&ncp->plot.ncp->pool, c, utf8, bytes, 1) <= 0){ \
|
|
||||||
return -1; \
|
|
||||||
} \
|
|
||||||
}else{ \
|
|
||||||
const uint64_t swapbg = ncchannels_bchannel(channels); \
|
|
||||||
const uint64_t swapfg = ncchannels_fchannel(channels); \
|
|
||||||
ncchannels_set_bchannel(&channels, swapfg); \
|
|
||||||
ncchannels_set_fchannel(&channels, swapbg); \
|
|
||||||
ncplane_set_channels(ncp->plot.ncp, channels); \
|
|
||||||
if(ncplane_putchar_yx(ncp->plot.ncp, dimy - y - 1, x, ' ') <= 0){ \
|
|
||||||
return -1; \
|
|
||||||
} \
|
|
||||||
ncchannels_set_bchannel(&channels, swapbg); \
|
|
||||||
ncchannels_set_fchannel(&channels, swapfg); \
|
|
||||||
ncplane_set_channels(ncp->plot.ncp, channels); \
|
|
||||||
} \
|
} \
|
||||||
} \
|
if(done){ \
|
||||||
if(done){ \
|
break; \
|
||||||
break; \
|
} \
|
||||||
} \
|
} \
|
||||||
if(ncp->plot.exponentiali){ \
|
if(ncp->plot.exponentiali){ \
|
||||||
intervalbase = ncp->miny + pow(interval, (y + 1) * states - 1); \
|
intervalbase = ncp->miny + pow(interval, (y + 1) * states - 1); \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user