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

Shrink textwindow stacksize #1162

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/dsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ class RgbaColor {
(int)(255 - ((bgra >> 24) & 0xff)));
}
};
static_assert(sizeof(RgbaColor) == 4, "RgbaColor must be exactly 4 bytes");

struct RgbaColorCompare {
bool operator()(RgbaColor a, RgbaColor b) const {
Expand Down
8 changes: 4 additions & 4 deletions src/modify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ void GraphicsWindow::MakeTangentArc() {
// in our group and workplane) that generate entities that have an
// endpoint at our vertex to be rounded.
int i, c = 0;
Entity *ent[2];
Request *req[2];
std::array<Entity *, 2> ent;
std::array<Request *, 2> req;
hRequest hreq[2];
hEntity hent[2];
bool pointf[2];
Expand Down Expand Up @@ -314,8 +314,8 @@ void GraphicsWindow::MakeTangentArc() {
// And thereafter we mustn't touch the entity or req ptrs,
// because the new requests/entities we add might force a
// realloc.
memset(ent, 0, sizeof(ent));
memset(req, 0, sizeof(req));
ent.fill(nullptr);
req.fill(nullptr);

Vector pinter;
double r = 0.0, vv = 0.0;
Expand Down
109 changes: 48 additions & 61 deletions src/textwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,11 @@ void TextWindow::ClearSuper() {
Platform::WindowRef oldWindow = std::move(window);
std::shared_ptr<ViewportCanvas> oldCanvas = canvas;

// Cannot use *this = {} here because TextWindow instances
// are 2.4MB long; this causes stack overflows in prologue
// Don't put grid-sized arrays directly in this class;
// this causes stack overflows in prologue
// when built with MSVC, even with optimizations.
memset(this, 0, sizeof(*this));
*this = {};
static_assert(sizeof(*this) < 1000*1000, "don't be huge");

// Return old canvas
window = std::move(oldWindow);
Expand All @@ -297,7 +298,6 @@ void TextWindow::ClearSuper() {
MakeColorTable(fgColors, fgColorTable);
MakeColorTable(bgColors, bgColorTable);

ClearScreen();
Show();
}

Expand Down Expand Up @@ -338,10 +338,8 @@ void TextWindow::ClearScreen() {
int i, j;
for(i = 0; i < MAX_ROWS; i++) {
for(j = 0; j < MAX_COLS; j++) {
text[i][j] = ' ';
meta[i][j].fg = 'd';
meta[i][j].bg = 'd';
meta[i][j].link = NOT_A_LINK;
text(i, j) = ' ';
meta(i, j) = {};
}
top[i] = i*2;
}
Expand All @@ -363,28 +361,22 @@ void TextWindow::Printf(bool halfLine, const char *fmt, ...) {
Printf(halfLine, endString);
return;
}

va_list vl;
va_start(vl, fmt);

int r, c;
r = rows;
int r = rows;
top[r] = (r == 0) ? 0 : (top[r-1] + (halfLine ? 3 : 2));
rows++;

for(c = 0; c < MAX_COLS; c++) {
text[r][c] = ' ';
meta[r][c].link = NOT_A_LINK;
for(int c = 0; c < MAX_COLS; c++) {
text(r, c) = ' ';
meta(r, c).link = NOT_A_LINK;
}

char fg = 'd';
char bg = 'd';
RgbaColor bgRgb = RGBi(0, 0, 0);
int link = NOT_A_LINK;
uint32_t data = 0;
LinkFunction *f = NULL, *h = NULL;
MetaField metaVal;

c = 0;
int c = 0;
while(*fmt) {
char buf[1024];

Expand Down Expand Up @@ -439,12 +431,12 @@ void TextWindow::Printf(bool halfLine, const char *fmt, ...) {
break;
}
case 'E':
fg = 'd';
metaVal.fg = 'd';
// leave the background, though
link = NOT_A_LINK;
data = 0;
f = NULL;
h = NULL;
metaVal.link = NOT_A_LINK;
metaVal.data = 0;
metaVal.f = NULL;
metaVal.h = NULL;
break;

case 'F':
Expand All @@ -457,10 +449,11 @@ void TextWindow::Printf(bool halfLine, const char *fmt, ...) {
case 'z': rgbPtr = va_arg(vl, RgbaColor *); break;
}
if(*fmt == 'F') {
fg = cc;
metaVal.fg = cc;
} else {
bg = cc;
if(rgbPtr) bgRgb = *rgbPtr;
metaVal.bg = cc;
if(rgbPtr)
metaVal.bgRgb = *rgbPtr;
}
fmt++;
break;
Expand All @@ -469,23 +462,23 @@ void TextWindow::Printf(bool halfLine, const char *fmt, ...) {
if(fmt[1] == '\0') goto done;
fmt++;
if(*fmt == 'p') {
link = va_arg(vl, int);
metaVal.link = va_arg(vl, int);
} else {
link = *fmt;
metaVal.link = *fmt;
}
break;

case 'f':
f = va_arg(vl, LinkFunction *);
metaVal.f = va_arg(vl, LinkFunction *);
break;

case 'h':
h = va_arg(vl, LinkFunction *);
metaVal.h = va_arg(vl, LinkFunction *);
break;

case 'D': {
unsigned int v = va_arg(vl, unsigned int);
data = (uint32_t)v;
metaVal.data = (uint32_t)v;
break;
}
case '%':
Expand All @@ -501,14 +494,8 @@ void TextWindow::Printf(bool halfLine, const char *fmt, ...) {
for(utf8_iterator it(buf); *it; ++it) {
for(size_t i = 0; i < canvas->GetBitmapFont()->GetWidth(*it); i++) {
if(c >= MAX_COLS) goto done;
text[r][c] = (i == 0) ? *it : ' ';
meta[r][c].fg = fg;
meta[r][c].bg = bg;
meta[r][c].bgRgb = bgRgb;
meta[r][c].link = link;
meta[r][c].data = data;
meta[r][c].f = f;
meta[r][c].h = h;
text(r, c) = (i == 0) ? *it : ' ';
meta(r, c) = metaVal;
c++;
}
}
Expand All @@ -518,9 +505,9 @@ void TextWindow::Printf(bool halfLine, const char *fmt, ...) {
fmt = it.ptr();
}
while(c < MAX_COLS) {
meta[r][c].fg = fg;
meta[r][c].bg = bg;
meta[r][c].bgRgb = bgRgb;
meta(r, c).fg = metaVal.fg;
meta(r, c).bg = metaVal.bg;
meta(r, c).bgRgb = metaVal.bgRgb;
c++;
}

Expand Down Expand Up @@ -967,13 +954,13 @@ void TextWindow::Paint() {
int x = LEFT_MARGIN + c*CHAR_WIDTH_;
int y = (ltop-scrollPos)*(LINE_HEIGHT/2) + 4;

int fg = meta[r][c].fg;
int bg = meta[r][c].bg;
int fg = meta(r, c).fg;
int bg = meta(r, c).bg;

// On the first pass, all the background quads; on the next
// pass, all the foreground (i.e., font) quads.
if(a == 0) {
RgbaColor bgRgb = meta[r][c].bgRgb;
RgbaColor bgRgb = meta(r, c).bgRgb;
int bh = LINE_HEIGHT, adj = 0;
if(bg == 'z') {
bh = CHAR_HEIGHT;
Expand All @@ -994,42 +981,42 @@ void TextWindow::Paint() {
RgbaColor fgRgb = RgbaColor::FromFloat(fgColorTable[fg*3+0],
fgColorTable[fg*3+1],
fgColorTable[fg*3+2]);
if(text[r][c] != ' ') {
uiCanvas.DrawBitmapChar(text[r][c], x, y + CHAR_HEIGHT, fgRgb);
if(text(r, c) != ' ') {
uiCanvas.DrawBitmapChar(text(r, c), x, y + CHAR_HEIGHT, fgRgb);
}

// If this is a link and it's hovered, then draw the
// underline
if(meta[r][c].link && meta[r][c].link != 'n' &&
if(meta(r, c).link && meta(r, c).link != 'n' &&
(r == hoveredRow && c == hoveredCol))
{
int cs = c, cf = c;
while(cs >= 0 && meta[r][cs].link &&
meta[r][cs].f == meta[r][c].f &&
meta[r][cs].data == meta[r][c].data)
while(cs >= 0 && meta(r, cs).link &&
meta(r, cs).f == meta(r, c).f &&
meta(r, cs).data == meta(r, c).data)
{
cs--;
}
cs++;

while( meta[r][cf].link &&
meta[r][cf].f == meta[r][c].f &&
meta[r][cf].data == meta[r][c].data)
while( meta(r, cf).link &&
meta(r, cf).f == meta(r, c).f &&
meta(r, cf).data == meta(r, c).data)
{
cf++;
}

// But don't underline checkboxes or radio buttons
while(((text[r][cs] >= 0xe000 && text[r][cs] <= 0xefff) ||
text[r][cs] == ' ') &&
while(((text(r, cs) >= 0xe000 && text(r, cs) <= 0xefff) ||
text(r, cs) == ' ') &&
cs < cf)
{
cs++;
}

// Always use the color of the rightmost character
// in the link, so that underline is consistent color
fg = meta[r][cf-1].fg;
fg = meta(r, cf-1).fg;
fgRgb = RgbaColor::FromFloat(fgColorTable[fg*3+0],
fgColorTable[fg*3+1],
fgColorTable[fg*3+2]);
Expand Down Expand Up @@ -1114,7 +1101,7 @@ void TextWindow::MouseEvent(bool leftClick, bool leftDown, double x, double y) {
hoveredRow = r;
hoveredCol = c;

const auto &item = meta[r][c];
const auto &item = meta(r, c);
if(leftClick) {
if(item.link && item.f) {
(item.f)(item.link, item.data);
Expand Down
46 changes: 35 additions & 11 deletions src/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,42 @@ class TextWindow {
int scrollPos; // The scrollbar position, in half-row units
int halfRows; // The height of our window, in half-row units

uint32_t text[MAX_ROWS][MAX_COLS];
typedef void LinkFunction(int link, uint32_t v);
static inline constexpr uint32_t computeIndex(uint32_t row, uint32_t col) {
return row * MAX_COLS + col;
}
template<typename T>
class GridData {
public:
GridData() : data_(new T[MAX_COLS * MAX_ROWS]) {
}
void reset() {
data_.reset(new T[MAX_COLS * MAX_ROWS]);
}
const T &operator()(uint32_t row, uint32_t col) const {
return data_[computeIndex(row, col)];
}
T &operator()(uint32_t row, uint32_t col) {
return data_[computeIndex(row, col)];
}

private:
std::unique_ptr<T[]> data_;
};

GridData<uint32_t> text;
enum { NOT_A_LINK = 0 };
struct {
char fg;
char bg;
RgbaColor bgRgb;
int link;
uint32_t data;
LinkFunction *f;
LinkFunction *h;
} meta[MAX_ROWS][MAX_COLS];
typedef void LinkFunction(int link, uint32_t v);
struct MetaField {
char fg = 'd';
char bg = 'd';
RgbaColor bgRgb = RGBi(0, 0, 0);
int link = NOT_A_LINK;
uint32_t data = 0;
LinkFunction *f = nullptr;
LinkFunction *h = nullptr;
};

GridData<MetaField> meta;
int hoveredRow, hoveredCol;

int top[MAX_ROWS]; // in half-line units, or -1 for unused
Expand Down