jcs
/textview
/amendments
/21
Move top/bottom tracking from chunk to line, fix autoscroll
jcs made amendment 21 over 2 years ago
--- textview.c Wed Oct 16 11:31:16 2024
+++ textview.c Wed Oct 16 22:25:38 2024
@@ -19,7 +19,7 @@
#include <string.h>
#include "textview.h"
-#define DEBUG_DRAWING 1
+//#define DEBUG_DRAWING 1
#define TEXT_SLAB_SIZE (16 * 1024)
#define CHUNK_SLAB_SIZE (sizeof(struct TVChunk) * 50)
@@ -32,9 +32,6 @@
(_style)->style == 0 && (_style)->size == 0)
#define NO_STYLE 0xff
-#define CHUNK_HEIGHT(chunk) (chunk)->baseline + \
- (chunk)->style.font_info.descent;
-
struct TVRec * TVLock(TVHandle tvh);
void TVUnlock(struct TVRec *);
struct TVSlab * TVAllocateTextSlab(struct TVRec *tv);
@@ -136,13 +133,6 @@ TVNew(Rect *view, Rect *padding)
tv->cur_style = NO_STYLE;
-#ifdef DEBUG_DRAWING
- TVUseCanvas(tv);
- EraseRect(&tv->canvas.portRect);
- FrameRect(&tv->canvas.portRect);
- TVRevealCanvas(tv);
-#endif
-
HUnlock(tvh);
return tvh;
}
@@ -154,7 +144,7 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
struct TVRec *tv;
struct TVSlab *text_slab;
struct TVChunk *chunk, *prev_chunk;
- struct TVStyle *cur_style, *prev_style;
+ struct TVStyle *cur_style;
unsigned long avail;
short width, prev_style_idx;
char *text_slab_data;
@@ -189,6 +179,7 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
}
chunk->start = text_slab->base + text_slab->pos;
+ chunk->end = chunk->start;
chunk->style = tv->cur_style;
if (prev_chunk) {
chunk->prev_chunk = prev_chunk->ptr;
@@ -199,6 +190,7 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
avail = text_slab->size - text_slab->pos;
if (avail == 0) {
/* new text slab, new chunk */
+ Debugger();
chunk->end = text_slab->base + text_slab->pos;
if ((text_slab = TVAllocateTextSlab(tv)) == NULL) {
@@ -219,7 +211,7 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
text_slab->pos += avail;
length -= avail;
tv->text_length += avail;
- chunk->end = text_slab->base + text_slab->pos;
+ chunk->end += avail;
chunk->dirty = true;
}
@@ -678,7 +670,7 @@ TVGetTextSlab(struct TVRec *tv, unsigned long addr)
slab = &tv->text_slabs[n];
if (slab->data == NULL)
continue;
- if (addr >= slab->base && addr <= slab->base + slab->size)
+ if (addr >= slab->base && addr < slab->base + slab->size)
return slab;
}
@@ -800,10 +792,8 @@ TVFindStyle(struct TVRec *tv, struct TVStyle *style)
tv->styles[n].style = style->style;
tv->styles[n].size = style->size;
cur_style = &tv->styles[n];
+ TVApplyStyle(cur_style);
GetFontInfo(&cur_style->font_info);
- cur_style->height = cur_style->font_info.ascent +
- cur_style->font_info.descent;
- cur_style->baseline = cur_style->font_info.ascent;
tv->cur_style = n;
return cur_style;
}
@@ -819,15 +809,15 @@ TVCalcLines(struct TVRec *tv)
{
struct TVSlab *text_slab;
struct TVChunk *chunk, *tchunk, *last_chunk, *prev_chunk, *next_chunk;
- struct TVStyle *style, *prev_style;
- struct TVLine *line, *next_line;
+ struct TVStyle *style;
+ struct TVLine *line, *prev_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, grow_chunks;
+ bool new_line;
- /* find first dirty chunk but search backwards */
+ /* find first dirty chunk */
last_chunk = TVGetChunk(tv, tv->last_chunk);
chunk = last_chunk;
while (chunk != NULL) {
@@ -845,44 +835,45 @@ TVCalcLines(struct TVRec *tv)
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;
- prev_chunk = NULL;
- prev_style = NULL;
- } 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);
- prev_style = &tv->styles[prev_chunk->style];
- 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;
+ if (chunk->end_line == 0) {
+ if (chunk->prev_chunk == 0) {
+ prev_chunk = NULL;
+ chunk->start_left = tv->padding.left;
+ chunk->start_line = 1;
+ chunk->end_right = chunk->start_left;
+ chunk->end_line = 1;
+ } else {
+ prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
+ chunk->start_left = prev_chunk->end_right;
+ chunk->start_line = prev_chunk->end_line;
+ chunk->end_right = chunk->start_left;
+ 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);
linen = chunk->end_line;
line = TVGetLine(tv, linen);
+ if (linen == 1) {
+ line->rect.left = tv->padding.left;
+ line->rect.top = tv->padding.top;
+ line->start = 0;
+ start = line->end;
+ } else {
+ prev_line = TVGetLine(tv, linen - 1);
+ line->rect.top = prev_line->rect.bottom;
+ if (line->end)
+ start = line->end;
+ else
+ start = prev_line->end;
+ }
- if (linen == chunk->start_line) {
- line->rect.top = chunk->top;
- line->rect.left = chunk->end_right;
- start = chunk->start;
- } else
- start = line->start;
-
new_line = false;
- grow_chunks = false;
last_space = 0;
+ height = style->font_info.ascent + style->font_info.descent;
for (n = start; n < chunk->end; n++) {
c = text_slab_data[n - text_slab->base];
@@ -912,76 +903,55 @@ TVCalcLines(struct TVRec *tv)
new_line = false;
space_width = 0;
+ line->end = n;
line->rect.right = chunk->end_right;
if (n == chunk->start) {
/* no text on previous line, start on next line */
- chunk->top = chunk->bottom;
+ Debugger();
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;
- }
+ } else if (height > line->rect.bottom - line->rect.top)
+ line->rect.bottom = line->rect.bottom + height;
linen++;
chunk->end_line = linen;
+ chunk->end_right = tv->padding.left;
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.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;
- }
+ if (n + 1 < chunk->end)
+ next_line->rect.bottom += height;
line = next_line;
}
chunk->end_right += width;
- if (style->font_info.descent > line->baseline) {
- if (line->baseline != 0)
- grow_chunks = true;
+ if (style->font_info.descent > line->baseline)
line->baseline = style->font_info.descent;
- }
- if (line->rect.bottom - line->rect.top <
- style->font_info.ascent + style->font_info.descent) {
- if (line->rect.bottom != line->rect.top)
- grow_chunks = true;
- line->rect.bottom = line->rect.top +
- style->font_info.ascent + style->font_info.descent;
- }
-
- if (grow_chunks) {
- for (tchunk = TVGetChunk(tv, chunk->prev_chunk);
- tchunk != NULL;
- tchunk = TVGetChunk(tv, tchunk->prev_chunk)) {
- if (tchunk->end < line->start)
- break;
- tchunk->bottom = line->rect.bottom;
- }
- grow_chunks = false;
- }
+ if (line->rect.bottom - line->rect.top < height)
+ line->rect.bottom = line->rect.top + height;
}
- chunk->bottom = line->rect.bottom;
+ line->end = chunk->end;
+ line->rect.right = chunk->end_right;
chunk->end_line = linen;
chunk->dirty = false;
}
done_chunks:
tv->nlines = linen;
+ line = TVGetLine(tv, linen);
- if (last_chunk->bottom > tv->bounds.bottom - tv->padding.bottom) {
- tv->bounds.bottom = last_chunk->bottom + tv->padding.bottom;
+ if (line->rect.bottom > tv->bounds.bottom - tv->padding.bottom) {
+ tv->bounds.bottom = line->rect.bottom + tv->padding.bottom;
tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom;
}
@@ -1001,57 +971,72 @@ TVDrawVisibleChunks(struct TVRec *tv)
struct TVStyle *style;
struct TVLine *line, *next_line;
Rect r;
- char c, *text_slab_data, *start;
- unsigned long bottom, datan, linen, chunklen;
- unsigned short chars;
+ char c, *text_slab_data;
+ unsigned long bottom, start, end, textlen;
+ unsigned short chars, linen;
+ long top, left;
TVUseCanvas(tv);
EraseRect(&tv->canvas.portRect);
- datan = 0;
+ chunk = TVGetChunk(tv, tv->first_chunk);
+ text_slab = TVGetTextSlab(tv, chunk->start);
+ text_slab_data = *(text_slab->data);
- 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;
+ for (linen = 1; linen <= tv->nlines; linen++) {
+ line = TVGetLine(tv, linen);
+
+ if (tv->scrolled.top + line->rect.bottom < 0)
continue;
- }
- if ((long)chunk->top > tv->scrolled.bottom)
+#if 0
+ if (tv->scrolled.bottom < line->rect.top)
break;
+#endif
+ while (chunk && chunk->end_line < linen) {
+ chunk = TVGetChunk(tv, chunk->next_chunk);
+ if (!chunk)
+ break;
+ text_slab = TVGetTextSlab(tv, chunk->start);
+ text_slab_data = *(text_slab->data);
+ }
- /* setup style */
- style = &tv->styles[chunk->style];
- if (style == NULL)
- return false;
- TVApplyStyle(style);
-
- /* setup text slab */
- text_slab = TVGetTextSlab(tv, chunk->start);
- text_slab_data = *(text_slab->data);
+ while (chunk != NULL) {
+ style = &tv->styles[chunk->style];
+ if (style == NULL)
+ return false;
+ TVApplyStyle(style);
- linen = chunk->start_line;
- line = TVGetLine(tv, linen);
-
- while (datan < tv->text_length && datan < chunk->end &&
- line != NULL) {
- if (linen < chunk->end_line &&
- (next_line = TVGetLine(tv, linen + 1)))
- chars = next_line->start - line->start;
- else {
- next_line = NULL;
- chars = tv->text_length - line->start;
+ if (line->start > chunk->start) {
+ start = line->start;
+ left = line->rect.left;
+ } else {
+ start = chunk->start;
+ left = chunk->start_left;
}
- if (chars > chunklen)
- chars = chunklen;
-
- if (linen == chunk->start_line)
- start = text_slab_data + (chunk->start - text_slab->base);
+ if (line->end < chunk->end)
+ end = line->end;
else
- start = text_slab_data + (line->start - text_slab->base);
+ end = chunk->end;
+
+ top = tv->scrolled.top + line->rect.bottom - line->baseline;
+ MoveTo(left, top);
+ DrawText(text_slab_data + (start - text_slab->base), 0,
+ end - start);
+
+ if (end < chunk->end)
+ /* span to next line */
+ break;
+ /* otherwise load next chunk for this line */
+ chunk = TVGetChunk(tv, chunk->next_chunk);
+ if (!chunk)
+ break;
+ text_slab = TVGetTextSlab(tv, chunk->start);
+ text_slab_data = *(text_slab->data);
+ }
+ }
+#if 0
if (!(chars == 1 && start[0] == '\r') &&
tv->scrolled.top + line->rect.bottom >= 0 &&
tv->scrolled.top + line->rect.top <= tv->view_height) {
@@ -1059,15 +1044,7 @@ TVDrawVisibleChunks(struct TVRec *tv)
tv->scrolled.top + line->rect.bottom - line->baseline);
DrawText(start, 0, chars);
}
-
- datan += chars;
- if (datan == chunk->end)
- break;
-
- linen++;
- line = next_line;
- }
- }
+#endif
TVRevealCanvas(tv);
return true;
--- textview.h Wed Oct 16 10:53:17 2024
+++ textview.h Wed Oct 16 11:35:11 2024
@@ -39,8 +39,6 @@ struct TVStyle {
/* these are private and ignored when fed into TVAppend() */
struct FontInfo font_info;
- unsigned char height;
- unsigned char baseline;
unsigned char width_cache[256];
};
@@ -63,8 +61,6 @@ struct TVChunk {
unsigned long start_line;
unsigned long end;
unsigned long end_line;
- unsigned long top;
- unsigned long bottom;
unsigned short start_left;
unsigned short end_right;
unsigned char style;
@@ -73,6 +69,7 @@ struct TVChunk {
struct TVLine {
unsigned long start;
+ unsigned long end;
BigRect rect;
unsigned char baseline;
};