jcs
/textview
/amendments
/19
Implement incremental line calculation
jcs made amendment 19 over 2 years ago
--- textview.c Mon Oct 14 21:32:16 2024
+++ textview.c Tue Oct 15 23:29:29 2024
@@ -19,6 +19,8 @@
#include <string.h>
#include "textview.h"
+#define DEBUG_DRAWING 1
+
#define TEXT_SLAB_SIZE (16 * 1024)
#define CHUNK_SLAB_SIZE (sizeof(struct TVChunk) * 50)
#define LINE_GROUP_NLINES 1000
@@ -33,20 +35,18 @@
#define CHUNK_HEIGHT(chunk) (chunk)->baseline + \
(chunk)->style.font_info.descent;
-#define DEBUG_DRAWING 1
-
struct TVRec * TVLock(TVHandle tvh);
void TVUnlock(struct TVRec *);
struct TVSlab * TVAllocateTextSlab(struct TVRec *tv);
struct TVSlab * TVAllocateChunkSlab(struct TVRec *tv);
Handle TVAllocateLineGroup(struct TVRec *tv, short groupn);
+struct TVSlab * TVGetTextSlab(struct TVRec *tv, unsigned long addr);
struct TVChunk * TVGetChunk(struct TVRec *tv, TVChunkPtr ptr);
struct TVChunk * TVCreateChunk(struct TVRec *tv);
struct TVLine * TVGetLine(struct TVRec *tv, unsigned long line);
-bool TVCalcLines(struct TVRec *tv, unsigned long start);
+bool TVCalcLines(struct TVRec *tv);
void TVApplyStyle(struct TVStyle *style);
struct TVStyle * TVFindStyle(struct TVRec *tv, struct TVStyle *style);
-void TVGrowBoundsToFit(struct TVRec *tv);
void TVScrollUnlocked(struct TVRec *tv, short horiz, short vert);
bool TVDrawVisibleChunks(struct TVRec *tv);
void TVUseCanvas(struct TVRec *tv);
@@ -57,6 +57,7 @@ TVNew(Rect *view, Rect *padding)
{
TVHandle tvh;
struct TVRec *tv;
+ struct TVLine *line;
long width, height;
GrafPtr old_port;
@@ -186,40 +187,19 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
TVUnlock(tv);
return false;
}
-
-#if 0
+
+ chunk->start = text_slab->base + text_slab->pos;
+ chunk->style = tv->cur_style;
if (prev_chunk) {
- prev_style = TVFindStyle(tv, &tv->styles[prev_chunk->style]);
- /* keep us on the same line as the previous chunk */
- chunk->top = prev_chunk->rect.bottom - prev_style->height;
- chunk->rect.bottom = chunk->rect.top + cur_style->height;
- chunk->rect.left = prev_chunk->rect_end_right;
- chunk->rect.right = prev_chunk->rect_end_right;
- chunk->rect_start_left = prev_chunk->rect_end_right;
- chunk->rect_end_right = prev_chunk->rect_end_right;
- prev_chunk->next_chunk = chunk->ptr;
chunk->prev_chunk = prev_chunk->ptr;
- chunk->start_line = chunk->end_line = prev_chunk->end_line;
- } else {
- chunk->rect.top = tv->padding.top;
- chunk->rect.left = tv->padding.left;
- chunk->rect.bottom = chunk->rect.top + cur_style->height;
- chunk->rect.right = chunk->rect.left;
- chunk->rect_start_left = chunk->rect.left;
- chunk->rect_end_right = chunk->rect_start_left;
- chunk->start_line = chunk->end_line = 1;
+ prev_chunk->next_chunk = chunk->ptr;
}
-#endif
-
- chunk->slab = tv->last_text_slab;
- chunk->start = text_slab->pos;
- chunk->style = tv->cur_style;
}
avail = text_slab->size - text_slab->pos;
if (avail == 0) {
/* new text slab, new chunk */
- chunk->end = text_slab->pos;
+ chunk->end = text_slab->base + text_slab->pos;
if ((text_slab = TVAllocateTextSlab(tv)) == NULL) {
ret = false;
@@ -239,11 +219,11 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
text_slab->pos += avail;
length -= avail;
tv->text_length += avail;
- chunk->end = text_slab->pos;
- chunk->dirty = 1;
+ chunk->end = text_slab->base + text_slab->pos;
+ chunk->dirty = true;
}
- if (TVCalcLines(tv, 0))
+ if (TVCalcLines(tv))
TVDrawVisibleChunks(tv);
else
ret = false;
@@ -625,6 +605,11 @@ TVAllocateTextSlab(struct TVRec *tv)
}
tv->text_slabs[slot].size = size;
tv->text_slabs[slot].pos = 0;
+ if (slot != 0)
+ tv->text_slabs[slot].base =
+ tv->text_slabs[tv->last_text_slab].base +
+ tv->text_slabs[tv->last_text_slab].size;
+
tv->last_text_slab = slot;
/* leave locked */
@@ -682,6 +667,25 @@ TVAllocateLineGroup(struct TVRec *tv, short groupn)
return tv->lines[groupn];
}
+struct TVSlab *
+TVGetTextSlab(struct TVRec *tv, unsigned long addr)
+{
+ short n;
+ struct TVSlab *slab;
+
+ for (n = 0; n < sizeof(tv->text_slabs) / sizeof(tv->text_slabs[0]);
+ n++) {
+ slab = &tv->text_slabs[n];
+ if (slab->data == NULL)
+ continue;
+ if (addr >= slab->base && addr <= slab->base + slab->size)
+ return slab;
+ }
+
+ Debugger();
+ return NULL;
+}
+
struct TVChunk *
TVGetChunk(struct TVRec *tv, TVChunkPtr ptr)
{
@@ -810,20 +814,157 @@ TVFindStyle(struct TVRec *tv, struct TVStyle *style)
return NULL;
}
-void
-TVGrowBoundsToFit(struct TVRec *tv)
+bool
+TVCalcLines(struct TVRec *tv)
{
- struct TVChunk *chunk;
+ struct TVSlab *text_slab;
+ struct TVChunk *chunk, *last_chunk, *prev_chunk, *next_chunk;
+ struct TVStyle *style, *prev_style;
+ struct TVLine *line, *next_line;
+ unsigned long avail, start, linen, last_space, max_right;
+ long n, back_width, back_n;
+ short width, space_width, height;
+ char c, tc, *text_slab_data;
+ bool new_line;
+
+ /* find first dirty chunk but search backwards */
+ last_chunk = TVGetChunk(tv, tv->last_chunk);
+ chunk = last_chunk;
+ while (chunk != NULL) {
+ prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
+ if (!prev_chunk || !prev_chunk->dirty)
+ break;
+ chunk = prev_chunk;
+ }
- chunk = TVGetChunk(tv, tv->last_chunk);
+ max_right = tv->bounds.right - tv->padding.right;
+
+ for (; chunk != NULL; chunk = TVGetChunk(tv, chunk->next_chunk)) {
+ style = &tv->styles[chunk->style];
+ if (style == NULL)
+ return false;
+ TVApplyStyle(style);
+
+ if (chunk->prev_chunk == 0) {
+ chunk->top = tv->padding.top;
+ chunk->start_left = tv->padding.left;
+ chunk->start_line = 1;
+ chunk->end_line = 1;
+ } else {
+ /*
+ * TODO: compare style heights, use bigger and change
+ * end of previous chunk(s) on this same line
+ */
+ prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
+ chunk->top = prev_chunk->top;
+ chunk->start_left = prev_chunk->end_right;
+ chunk->start_line = prev_chunk->end_line;
+ chunk->end_line = chunk->start_line;
+ }
+ chunk->end_right = chunk->start_left;
+
+ /* setup text slab */
+ text_slab = TVGetTextSlab(tv, chunk->start);
+ text_slab_data = *(text_slab->data);
+
+ new_line = false;
+ last_space = 0;
+
+ linen = chunk->end_line;
+ line = TVGetLine(tv, linen);
+
+ if (linen == chunk->start_line) {
+ line->rect.top = chunk->top;
+ line->rect.left = chunk->end_right;
+ start = chunk->start;
+ } else
+ start = line->start;
+
+ line->rect.bottom = line->rect.top + style->height;
+
+ for (n = start; n < chunk->end; n++) {
+ c = text_slab_data[n - text_slab->base];
+
+ if (c == '\r') {
+ new_line = true;
+ width = 0;
+ } else {
+ if (!(width = style->width_cache[c]))
+ width = style->width_cache[c] = CharWidth(c);
+
+ if (c == ' ') {
+ last_space = n;
+ space_width = 0;
+ } else if (chunk->end_right + width >= max_right) {
+ new_line = true;
+ if (last_space) {
+ /* TODO: what if this crosses chunks? */
+ n = last_space + 1;
+ chunk->end_right -= space_width;
+ c = text_slab_data[n];
+ }
+ }
+ }
+
+ if (new_line) {
+ new_line = false;
+ space_width = 0;
+
+ line->rect.right = chunk->end_right;
+
+ if (n == chunk->start) {
+ /* no text on previous line, start on next line */
+ chunk->top = chunk->bottom;
+ chunk->start_left = tv->padding.left;
+ chunk->end_right = chunk->start_left;
+ chunk->start_line++;
+ line->rect.top = chunk->top;
+ line->rect.bottom = line->rect.top;
+ } else {
+ chunk->bottom = line->rect.bottom;
+ chunk->end_right = tv->padding.left;
+ }
+
+ linen++;
+ chunk->end_line = linen;
+ next_line = TVGetLine(tv, linen);
+ next_line->start = n;
+ if (c == '\r')
+ next_line->start++;
+ next_line->rect.top = line->rect.bottom - 1;
+ next_line->rect.left = tv->padding.left;
+ next_line->rect.right = chunk->end_right;
+ next_line->rect.bottom = next_line->rect.top;
+ if (n + 1 < chunk->end) {
+ next_line->rect.bottom += style->height;
+ chunk->bottom = next_line->rect.bottom;
+ }
+
+ line = next_line;
+ }
+
+ chunk->end_right += width;
+ }
+
+ chunk->bottom = line->rect.bottom;
+ chunk->end_line = linen;
+ chunk->dirty = false;
+ }
+
+done_chunks:
+ tv->nlines = linen;
- if (chunk && chunk->bottom > tv->bounds.bottom - tv->padding.bottom) {
- tv->bounds.bottom = chunk->bottom + tv->padding.bottom;
+ if (last_chunk->bottom > tv->bounds.bottom - tv->padding.bottom) {
+ tv->bounds.bottom = last_chunk->bottom + tv->padding.bottom;
tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom;
}
- if (tv->autoscroll && (tv->scrolled.bottom > tv->view_height))
- TVScrollUnlocked(tv, 0, -SHRT_MAX);
+ if (tv->autoscroll && (tv->scrolled.bottom > tv->view_height)) {
+ tv->scrolled.bottom = tv->view_height;
+ tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
+ }
+
+ return true;
}
bool
@@ -834,8 +975,8 @@ TVDrawVisibleChunks(struct TVRec *tv)
struct TVStyle *style;
struct TVLine *line, *next_line;
Rect r;
- char c, *text_slab_data;
- unsigned long bottom, left, datan, chunkn, chunklen, linen;
+ char c, *text_slab_data, *start;
+ unsigned long bottom, datan, linen, chunklen;
unsigned short chars;
TVUseCanvas(tv);
@@ -845,6 +986,15 @@ TVDrawVisibleChunks(struct TVRec *tv)
for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL;
chunk = TVGetChunk(tv, chunk->next_chunk)) {
+ chunklen = chunk->end - chunk->start;
+
+ if ((long)chunk->bottom < tv->scrolled.top) {
+ datan += chunklen;
+ continue;
+ }
+ if ((long)chunk->top > tv->scrolled.bottom)
+ break;
+
/* setup style */
style = &tv->styles[chunk->style];
if (style == NULL)
@@ -852,24 +1002,13 @@ TVDrawVisibleChunks(struct TVRec *tv)
TVApplyStyle(style);
/* setup text slab */
- text_slab = &tv->text_slabs[chunk->slab];
+ text_slab = TVGetTextSlab(tv, chunk->start);
text_slab_data = *(text_slab->data);
- chunklen = CHUNKPTR_OFFSET(chunk->end) -
- CHUNKPTR_OFFSET(chunk->start);
-
- if ((long)chunk->bottom < tv->scrolled.top) {
- datan += chunklen;
- continue;
- }
- if ((long)chunk->top > tv->scrolled.bottom)
- break;
-
linen = chunk->start_line;
line = TVGetLine(tv, linen);
- chunkn = 0;
- while (datan < tv->text_length && chunkn < chunklen &&
+ while (datan < tv->text_length && datan < chunk->end &&
line != NULL) {
if (linen < chunk->end_line &&
(next_line = TVGetLine(tv, linen + 1)))
@@ -882,27 +1021,21 @@ TVDrawVisibleChunks(struct TVRec *tv)
if (chars > chunklen)
chars = chunklen;
- if (!(chars == 1 && *(text_slab_data + line->start) == '\r')
-#if 0
- &&
+ if (linen == chunk->start_line)
+ start = text_slab_data + (chunk->start - text_slab->base);
+ else
+ start = text_slab_data + (line->start - text_slab->base);
+
+ if (!(chars == 1 && start[0] == '\r') &&
tv->scrolled.top + line->rect.bottom >= 0 &&
- tv->scrolled.top + line->rect.top <= tv->view_height
-#endif
- ) {
- if (linen == chunk->start_line)
- left = chunk->start_left;
- else
- left = tv->padding.left;
-
- MoveTo(left, tv->scrolled.top + line->rect.top +
- style->baseline);
- DrawText(text_slab_data + line->start, 0, chars);
+ tv->scrolled.top + line->rect.top <= tv->view_height) {
+ MoveTo(chunk->start_left,
+ tv->scrolled.top + line->rect.top + style->baseline);
+ DrawText(start, 0, chars);
}
datan += chars;
- chunkn += chars;
-
- if (chunkn == chunklen)
+ if (datan == chunk->end)
break;
linen++;
@@ -971,144 +1104,6 @@ TVScrollUnlocked(struct TVRec *tv, short horiz, short
TVRevealCanvas(tv);
}
-bool
-TVCalcLines(struct TVRec *tv, unsigned long start)
-{
- struct TVSlab *text_slab;
- struct TVChunk *chunk, *prev_chunk;
- struct TVStyle *style, *prev_style;
- struct TVLine *line, *next_line;
- Rect r;
- unsigned long avail, start_pos, linen, last_space, max_right;
- long pos, j, n, back_width, back_n;
- short width, space_width, height;
- char c, tc, *text_slab_data;
- bool new_line;
-
- for (n = 0; n < sizeof(tv->lines) / sizeof(tv->lines[0]); n++) {
- if (tv->lines[n]) {
- HLock(tv->lines[n]);
- memset((char *)*(tv->lines[n]), 0, LINE_GROUP_SIZE);
- }
- }
-
- linen = 1;
- line = TVGetLine(tv, linen);
- line->start = 0;
- line->rect.top = tv->padding.top;
- line->rect.left = tv->padding.left;
- line->rect.right = line->rect.left;
- line->rect.bottom = line->rect.top;
-
- prev_chunk = NULL;
- max_right = tv->bounds.right - tv->padding.right;
-
- for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL;
- chunk = TVGetChunk(tv, chunk->next_chunk)) {
- style = &tv->styles[chunk->style];
- if (style == NULL)
- return false;
- TVApplyStyle(style);
-
- if (prev_chunk) {
- /* compare style heights, use bigger and possbly change end of
- * previous chunk */
- chunk->top = prev_chunk->bottom - prev_style->height;
- chunk->bottom = chunk->top + style->height;
- chunk->start_left = prev_chunk->end_right;
- chunk->start_line = prev_chunk->end_line;
- } else {
- chunk->top = tv->padding.top;
- chunk->bottom = chunk->top + style->height;
- chunk->start_left = tv->padding.left;
- chunk->start_line = 1;
- }
- chunk->end_line = chunk->start_line;
- chunk->end_right = chunk->start_left;
-
- /* setup text slab */
- text_slab = &tv->text_slabs[chunk->slab];
- text_slab_data = *(text_slab->data);
-
- new_line = false;
- last_space = 0;
-
- for (n = chunk->start; n < chunk->end; n++) {
- c = text_slab_data[n];
-
- if (c == '\r') {
- new_line = true;
- width = 0;
- } else {
- if (!(width = style->width_cache[c]))
- width = style->width_cache[c] = CharWidth(c);
-
- if (c == ' ') {
- last_space = n;
- space_width = 0;
- } else if (chunk->end_right + width >= max_right) {
- new_line = true;
- if (last_space) {
- n = last_space + 1;
- chunk->end_right -= space_width;
- c = text_slab_data[n];
- }
- }
- }
-
- if (new_line) {
- new_line = false;
- space_width = 0;
- linen++;
-
- line->rect.bottom = line->rect.top + style->height;
- line->rect.right = chunk->end_right;
-
- next_line = TVGetLine(tv, linen);
- next_line->start = n;
- next_line->rect.top = line->rect.bottom - 1;
- next_line->rect.left = tv->padding.left;
- next_line->rect.right = chunk->end_right;
- next_line->rect.bottom = next_line->rect.top;
-
- if (n == chunk->start) {
- /* we didn't put any text on previous line, start over */
- chunk->top = chunk->bottom;
- chunk->bottom = chunk->top + style->height;
- chunk->start_left = tv->padding.left;
- chunk->end_right = chunk->start_left;
- chunk->start_line++;
- } else {
- chunk->bottom += style->height;
- chunk->end_right = tv->padding.left;
- }
-
- chunk->end_line++;
- line = next_line;
- }
-
- chunk->end_right += width;
- }
-
- prev_chunk = chunk;
- prev_style = style;
- }
-
- tv->nlines = linen;
-
- if (prev_chunk->bottom > tv->bounds.bottom - tv->padding.bottom) {
- tv->bounds.bottom = prev_chunk->bottom + tv->padding.bottom;
- tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom;
- }
-
- if (tv->autoscroll && (tv->scrolled.bottom > tv->view_height)) {
- tv->scrolled.bottom = tv->view_height;
- tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
- }
-
- return true;
-}
-
void
TVUseCanvas(struct TVRec *tv)
{
@@ -1150,3 +1145,21 @@ TVRevealCanvas(struct TVRec *tv)
tv->canvas_old_port = NULL;
}
+
+#if 0
+void
+TVResize(struct TVRec *tv)
+{
+#if 0
+ for (n = 0; n < sizeof(tv->lines) / sizeof(tv->lines[0]); n++) {
+ if (tv->lines[n]) {
+ HLock(tv->lines[n]);
+ memset((char *)*(tv->lines[n]), 0, LINE_GROUP_SIZE);
+ }
+ }
+
+ tv->start_line = 1;
+ tv->end_line = 1;
+#endif
+}
+#endif
\ No newline at end of file
--- textview.h Mon Oct 14 21:33:57 2024
+++ textview.h Tue Oct 15 22:47:05 2024
@@ -59,18 +59,16 @@ struct TVChunk {
TVChunkPtr ptr;
TVChunkPtr prev_chunk;
TVChunkPtr next_chunk;
+ unsigned long start;
unsigned long start_line;
+ unsigned long end;
unsigned long end_line;
- unsigned short start;
- unsigned short end;
- unsigned char style;
- unsigned char slab;
- bool dirty;
- unsigned char _pad;
unsigned long top;
unsigned long bottom;
unsigned short start_left;
unsigned short end_right;
+ unsigned char style;
+ bool dirty;
};
struct TVLine {
@@ -80,6 +78,7 @@ struct TVLine {
struct TVSlab {
Handle data;
+ unsigned long base;
unsigned long size;
unsigned long pos;
};