jcs
/textview
/amendments
/12
More performance improvements and edge-case fixes
Also add a debug mode that draws directly to the view and draws on
every character, to help fix wrapping and line-height change bugs.
jcs made amendment 12 over 2 years ago
--- textview.c Sun Oct 6 23:03:07 2024
+++ textview.c Mon Oct 7 12:47:05 2024
@@ -23,10 +23,13 @@
#define CHUNK_ALLOC_SIZE (16 * 1024)
#define IS_FIRST_EMPTY_CHUNK(tv) ((tv)->cur_chunk->style.size == 0)
+//#define DEBUG_DRAWING 1
+
struct TVChunk * TVCreateChunk(struct TVRec *tv, unsigned long length);
struct TVChunk * TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk,
unsigned long length);
void TVApplyStyle(struct TVStyle *style);
+void TVGrowBoundsToFit(struct TVRec *tv, struct TVChunk *chunk);
void TVDrawText(struct TVRec *tv, struct TVChunk *chunk,
unsigned long off);
void TVDrawOnCanvas(struct TVRec *tv);
@@ -86,6 +89,11 @@ TVNew(Rect *view, Rect *padding)
tv->cur_chunk->rect.right = tv->cur_chunk->rect.left;
tv->last_chunk = tv->cur_chunk;
+#ifdef DEBUG_DRAWING
+ EraseRect(&tv->canvas.bounds);
+ FrameRect(&tv->canvas.bounds);
+#endif
+
return tv;
}
@@ -140,6 +148,7 @@ TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk,
Rect r;
struct TVChunk *new_chunk, *prev_chunk, *next_chunk;
short width;
+ long oldright;
/*
* This leaks some space by not putting the new chunk right after
@@ -160,20 +169,27 @@ TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk,
tv->last_chunk = new_chunk;
}
- BigRectToRect(chunk->rect, r);
- r.top += tv->scrolled.top;
- r.left += tv->scrolled.left;
- EraseRect(&r);
-
new_chunk->length = chunk->length - length;
memcpy(new_chunk->text, chunk->text + length, chunk->length - length);
chunk->length = length;
TVApplyStyle(&chunk->style);
width = TextWidth(chunk->text, 0, chunk->length);
+ oldright = chunk->rect.right;
chunk->rect.right = chunk->rect.left + width;
- if (width)
+ if (width) {
+ BigRectToRect(chunk->rect, r);
+ r.top += tv->scrolled.top;
+ r.bottom += tv->scrolled.top;
+ r.left = tv->scrolled.left + chunk->rect.right;
+ r.right = tv->scrolled.left + oldright;
+#ifdef DEBUG_DRAWING
+ FillRect(&r, ltGray);
+#else
+ EraseRect(&r);
+#endif
TVDrawText(tv, chunk, 0);
+ }
/* line us up next to the current chunk */
new_chunk->baseline = chunk->baseline;
@@ -196,6 +212,18 @@ TVApplyStyle(struct TVStyle *style)
}
void
+TVGrowBoundsToFit(struct TVRec *tv, struct TVChunk *chunk)
+{
+ if (chunk->rect.bottom > tv->bounds.bottom - tv->padding.bottom) {
+ tv->bounds.bottom = chunk->rect.bottom + tv->padding.bottom;
+ tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom;
+ }
+
+ if (tv->autoscroll && (tv->scrolled.bottom > tv->view_height))
+ TVScroll(tv, 0, -SHRT_MAX);
+}
+
+void
TVAppend(struct TVRec *tv, struct TVStyle *style, char *text,
unsigned long length)
{
@@ -204,24 +232,23 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char
long j, n, line_right;
short width;
char c;
- bool new_chunk, new_line;
+ bool new_chunk = false, new_line = false;
TVDrawOnCanvas(tv);
TVApplyStyle(style);
- GetFontInfo(&style->font_info);
-
+
if (memcmp(&tv->last_style, style, sizeof(struct TVStyle)) != 0) {
+ GetFontInfo(&style->font_info);
memset(tv->width_cache, 0, sizeof(tv->width_cache));
tv->last_style = *style;
+ new_chunk = true;
}
-
+
line_right = tv->cur_chunk->rect.right;
for (n = 0; n < length; n++) {
c = text[n];
- new_chunk = false;
- new_line = false;
if (c == '\r') {
new_chunk = true;
@@ -230,7 +257,7 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char
if (!(width = tv->width_cache[c]))
width = tv->width_cache[c] = CharWidth(c);
- if (c != ' ' && width + line_right >
+ if (c != ' ' && width + line_right >=
tv->bounds.right - tv->padding.right) {
new_chunk = true;
new_line = true;
@@ -255,17 +282,14 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char
}
TVApplyStyle(&tv->cur_chunk->style);
- } else if (memcmp(style, &tv->cur_chunk->style,
- sizeof(struct TVStyle)) != 0) {
- new_chunk = true;
}
}
if (new_chunk) {
+ new_chunk = false;
chunk = TVCreateChunk(tv, length - n);
chunk->baseline = style->font_info.ascent;
chunk->style = *style;
- GetFontInfo(&chunk->style.font_info);
chunk->rect.bottom = chunk->rect.top + chunk->baseline +
chunk->style.font_info.descent;
@@ -287,23 +311,40 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char
if (tchunk->line != tv->cur_chunk->line)
break;
- BigRectToRect(tchunk->rect, r);
- r.top += tv->scrolled.top;
- r.left += tv->scrolled.left;
- EraseRect(&r);
+ if (tchunk->length) {
+ BigRectToRect(tchunk->rect, r);
+ r.top += tv->scrolled.top;
+ r.bottom += tv->scrolled.top;
+ r.left += tv->scrolled.left;
+ r.right += tv->scrolled.left;
+ EraseRect(&r);
+ }
tchunk->baseline = tv->cur_chunk->baseline;
+ tchunk->rect.bottom = tchunk->rect.top +
+ tchunk->baseline +
+ tchunk->style.font_info.descent;
+ if (tchunk->rect.bottom < chunk->rect.bottom)
+ tchunk->rect.bottom = chunk->rect.bottom;
+
+ TVGrowBoundsToFit(tv, tchunk);
TVDrawText(tv, tchunk, 0);
}
- } else if (tv->cur_chunk->baseline < prev_chunk->baseline)
+ } else if (tv->cur_chunk->baseline < prev_chunk->baseline) {
tv->cur_chunk->baseline =
tv->cur_chunk->prev_chunk->baseline;
+ if (tv->cur_chunk->rect.bottom <
+ tv->cur_chunk->prev_chunk->rect.bottom)
+ tv->cur_chunk->rect.bottom =
+ tv->cur_chunk->prev_chunk->rect.bottom;
+ }
TVApplyStyle(&tv->cur_chunk->style);
}
}
if (new_line) {
+ new_line = false;
tv->cur_chunk->line++;
tv->cur_chunk->rect.top =
tv->cur_chunk->prev_chunk->rect.bottom +
@@ -316,17 +357,9 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char
tv->padding.left;
tv->cur_chunk->rect.left = tv->padding.left;
line_right = tv->cur_chunk->rect.right;
-
- if (tv->cur_chunk->rect.bottom >
- tv->bounds.bottom - tv->padding.bottom) {
- tv->bounds.bottom = tv->cur_chunk->rect.bottom +
- tv->padding.bottom;
- tv->scrolled.bottom = tv->scrolled.top +
- tv->bounds.bottom;
- }
-
- if (tv->autoscroll && (tv->scrolled.bottom > tv->view_height))
- TVScroll(tv, 0, -SHRT_MAX);
+
+ TVGrowBoundsToFit(tv, tv->cur_chunk);
+ TVDrawText(tv, tv->cur_chunk, 0);
}
if (c == '\r') {
@@ -334,19 +367,21 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char
* TODO: remember that this was a hard line break and not
* just a wrapped line
*/
-
/* next line will need to know how tall our empty space was */
if (tv->cur_chunk->length == 0) {
tv->cur_chunk->rect.bottom = tv->cur_chunk->rect.top +
tv->cur_chunk->baseline +
tv->cur_chunk->style.font_info.descent;
}
- line_right = 0;
+ line_right = tv->padding.left;
} else {
tv->cur_chunk->text[tv->cur_chunk->length++] = c;
tv->cur_chunk->free_length--;
line_right += width;
tv->cur_chunk->rect.right += width;
+#ifdef DEBUG_DRAWING
+ TVDrawText(tv, tv->cur_chunk, 0);
+#endif
}
}
@@ -578,7 +613,13 @@ TVScroll(struct TVRec *tv, short horiz, short vert)
vacated = NewRgn();
left = tv->scrolled.left - was.left;
top = tv->scrolled.top - was.top;
+#ifdef DEBUG_DRAWING
+ InsetRect(&tv->canvas.bounds, 1, 1);
+#endif
ScrollRect(&tv->canvas.bounds, left, top, vacated);
+#ifdef DEBUG_DRAWING
+ InsetRect(&tv->canvas.bounds, -1, -1);
+#endif
rvacated = tv->scrolled;
rvacated.bottom = top;
@@ -619,15 +660,16 @@ TVUpdate(struct TVRec *tv)
void
TVDrawOnCanvas(struct TVRec *tv)
{
- Rect r;
-
if (++tv->canvas_refcnt != 1)
return;
+
+#ifdef DEBUG_DRAWING
+ return;
+#endif
GetClip(tv->port_region = NewRgn());
tv->canvas_cur_bits = thePort->portBits;
SetPortBits(&tv->canvas);
-
ClipRect(&tv->canvas.bounds);
}
@@ -639,13 +681,23 @@ TVShowCanvas(struct TVRec *tv)
if (--tv->canvas_refcnt != 0)
return;
+
+#ifdef DEBUG_DRAWING
+ return;
+#endif
SetPortBits(&tv->canvas_cur_bits);
SetClip(tv->port_region);
- DisposeRgn(tv->port_region);
+ if (tv->canvas_refcnt == 0)
+ DisposeRgn(tv->port_region);
CopyBits(&tv->canvas, &tv->canvas_cur_bits, &tv->canvas.bounds,
&tv->view, srcCopy, NULL);
+
+ if (tv->canvas_refcnt != 0) {
+ SetPortBits(&tv->canvas);
+ ClipRect(&tv->canvas.bounds);
+ }
ValidRect(&tv->view);
}