AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

17

Rewrite #2: Store all text in text slabs

Chunks just retain style and text span information. This will be
easier to resize since storing text in chunks would have to shuffle
everything around.

jcs made amendment 17 over 2 years ago
--- textview.c Thu Oct 10 09:57:17 2024 +++ textview.c Fri Oct 11 20:29:16 2024 @@ -19,21 +19,33 @@ #include <string.h> #include "textview.h" -#define CHUNK_ALLOC_SIZE (16 * 1024) -#define IS_FIRST_EVER_CHUNK(chunk) ((chunk)->ptr == 0) +#define TEXT_SLAB_SIZE (16 * 1024) +#define CHUNK_SLAB_SIZE (sizeof(struct TVChunk) * 50) -//#define DEBUG_DRAWING 1 +#define ARE_STYLES_EQUAL(one, two) ((one)->font == (two)->font && \ + (one)->style == (two)->style && (one)->size == (two)->size) +#define IS_STYLE_EMPTY(_style) ((_style)->font == 0 && \ + (_style)->style == 0 && (_style)->size == 0) +#define NO_STYLE 0xff +#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); struct TVChunk * TVGetChunk(struct TVRec *tv, TVChunkPtr ptr); -struct TVChunk * TVCreateChunk(struct TVRec *tv, unsigned long length); +struct TVChunk * TVCreateChunk(struct TVRec *tv); struct TVChunk * TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, unsigned long length); void TVApplyStyle(struct TVStyle *style); +struct TVStyle * TVFindStyle(struct TVRec *tv, struct TVStyle *style); void TVGrowBoundsToFit(struct TVRec *tv, struct TVChunk *chunk); void TVScrollUnlocked(struct TVRec *tv, short horiz, short vert); -void TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off); +bool TVDrawChunk(struct TVRec *tv, struct TVChunk *chunk); void TVDrawOnCanvas(struct TVRec *tv); void TVShowCanvas(struct TVRec *tv); @@ -42,7 +54,6 @@ TVNew(Rect *view, Rect *padding) { TVHandle tvh; struct TVRec *tv; - struct TVChunk *chunk; long width, height; GrafPtr old_port; @@ -98,210 +109,138 @@ TVNew(Rect *view, Rect *padding) tv->bounds.bottom = tv->padding.top + tv->padding.bottom; tv->scrolled = tv->bounds; - /* create an initial blank chunk */ - tv->chunk_slabs[0] = NewHandleClear(CHUNK_ALLOC_SIZE); - if (tv->chunk_slabs[0] == NULL) { + if (TVAllocateTextSlab(tv) == NULL) { DisposHandle(tv->canvas_baseaddr); DisposHandle(tvh); return NULL; } - HLock(tv->chunk_slabs[0]); - chunk = (struct TVChunk *)(*(tv->chunk_slabs[0])); - chunk->next_chunk = EMPTY_CHUNKPTR; - chunk->prev_chunk = EMPTY_CHUNKPTR; - chunk->free_length = CHUNK_ALLOC_SIZE - sizeof(struct TVChunk); - chunk->line = 1; - chunk->rect.top = tv->padding.top; - chunk->rect.left = tv->padding.left; - chunk->rect.bottom = chunk->rect.top; - chunk->rect.right = chunk->rect.left; - - HUnlock(tv->chunk_slabs[0]); + if (TVAllocateChunkSlab(tv) == NULL) { + DisposHandle(tv->text_slabs[0].data); + DisposHandle(tv->canvas_baseaddr); + DisposHandle(tvh); + return NULL; + } + tv->cur_style = NO_STYLE; + #ifdef DEBUG_DRAWING + TVDrawOnCanvas(tv); EraseRect(&tv->canvas.portRect); FrameRect(&tv->canvas.portRect); + TVShowCanvas(tv); #endif HUnlock(tvh); return tvh; } -#define CHUNK_HEIGHT(chunk) (chunk)->baseline + \ - (chunk)->style.font_info.descent; - bool TVAppend(TVHandle tvh, struct TVStyle *style, char *text, unsigned long length) { struct TVRec *tv; - struct TVChunk *chunk = NULL, *cur_chunk, *prev_chunk, *tchunk; - Rect r; - long j, n, line_right; - short width; - char c; - bool new_chunk = false, new_line = false, ret = true, new_style = false; + struct TVSlab *text_slab; + struct TVChunk *chunk, *prev_chunk; + struct TVStyle *cur_style, *prev_style; + unsigned long avail; + short width, prev_style_idx; + char *text_slab_data; + bool new_chunk = false, ret = true; tv = TVLock(tvh); TVDrawOnCanvas(tv); - cur_chunk = TVGetChunk(tv, tv->cur_chunk); - line_right = cur_chunk->rect.right; - - 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; + /* setup style */ + prev_style_idx = tv->cur_style; + cur_style = TVFindStyle(tv, style); + if (cur_style == NULL) + return false; + if (tv->cur_style != prev_style_idx) new_chunk = true; - new_style = true; - } - - TVApplyStyle(style); + TVApplyStyle(cur_style); - for (n = 0; n < length; n++) { - c = text[n]; - - if (c == '\r') { - new_chunk = true; - new_line = true; - } else { - if (!(width = tv->width_cache[c])) - width = tv->width_cache[c] = CharWidth(c); - - if (c != ' ' && width + line_right >= - tv->bounds.right - tv->padding.right) { - new_chunk = true; - new_line = true; - } else if (cur_chunk->free_length == 0) { - new_chunk = true; + /* setup text slab */ + text_slab = &tv->text_slabs[tv->last_text_slab]; + + /* setup chunk */ + prev_chunk = NULL; + chunk = TVGetChunk(tv, tv->last_chunk); + + while (length) { + if (chunk == NULL || new_chunk) { + new_chunk = false; + prev_chunk = chunk; + chunk = TVCreateChunk(tv); + if (chunk == NULL) { + TVShowCanvas(tv); + TVUnlock(tv); + return false; } - - if (new_chunk && cur_chunk->length && !new_style) { - /* walk back to last space and break after it */ - for (j = cur_chunk->length; j > 0; j--) { - if (cur_chunk->text[j - 1] != ' ') - continue; - - tchunk = TVSplitChunk(tv, cur_chunk, j); - if (tchunk == NULL) { - ret = false; - goto done; - } - cur_chunk = tchunk; - tv->cur_chunk = cur_chunk->ptr; - new_chunk = false; - break; - } + + if (prev_chunk) { + prev_style = TVFindStyle(tv, &tv->styles[prev_chunk->style]); + /* keep us on the same line as the previous chunk */ + chunk->rect.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; + } 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->slab = tv->last_text_slab; + chunk->start = text_slab->pos; + chunk->style = tv->cur_style; } - if (new_chunk) { - new_chunk = false; - new_style = false; - - if (!new_line && IS_FIRST_EVER_CHUNK(cur_chunk)) - chunk = cur_chunk; - else { - chunk = TVCreateChunk(tv, length - n); - if (chunk == NULL) { - ret = false; - goto done; - } - chunk->prev_chunk = tv->cur_chunk; - cur_chunk->next_chunk = chunk->ptr; - prev_chunk = cur_chunk; + avail = text_slab->size - text_slab->pos; + if (avail == 0) { + /* new text slab, new chunk */ + chunk->end = text_slab->pos; + if (!TVDrawChunk(tv, chunk)) { + ret = false; + break; } - chunk->next_chunk = EMPTY_CHUNKPTR; - chunk->baseline = style->font_info.ascent; - chunk->style = *style; - chunk->rect.bottom = chunk->rect.top + CHUNK_HEIGHT(chunk); - - if (cur_chunk->length) - TVDrawText(tv, cur_chunk, 0); - cur_chunk = chunk; - tv->cur_chunk = cur_chunk->ptr; - tv->last_chunk = cur_chunk->ptr; - - if (!IS_FIRST_EVER_CHUNK(cur_chunk)) { - if (cur_chunk->baseline > prev_chunk->baseline) { - /* all chunks on this line must grow */ - tchunk = prev_chunk; - for (tchunk; tchunk != NULL; - tchunk = TVGetChunk(tv, tchunk->prev_chunk)) { - if (tchunk->line != cur_chunk->line) - break; - - 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 = cur_chunk->baseline; - tchunk->rect.bottom = tchunk->rect.top + - CHUNK_HEIGHT(tchunk); - if (tchunk->rect.bottom < chunk->rect.bottom) - tchunk->rect.bottom = chunk->rect.bottom; - - TVGrowBoundsToFit(tv, tchunk); - TVDrawText(tv, tchunk, 0); - } - } else if (cur_chunk->baseline < prev_chunk->baseline) { - cur_chunk->baseline = prev_chunk->baseline; - if (cur_chunk->rect.bottom < prev_chunk->rect.bottom) - cur_chunk->rect.bottom = prev_chunk->rect.bottom; - } - - TVApplyStyle(&cur_chunk->style); + if ((text_slab = TVAllocateTextSlab(tv)) == NULL) { + ret = false; + break; } + + new_chunk = true; + continue; } - if (new_line) { - new_line = false; - cur_chunk->line++; - prev_chunk = TVGetChunk(tv, cur_chunk->prev_chunk); - cur_chunk->rect.top = prev_chunk->rect.bottom - 1; - cur_chunk->rect.bottom = cur_chunk->rect.top + - CHUNK_HEIGHT(cur_chunk); - cur_chunk->rect.right -= cur_chunk->rect.left - - tv->padding.left; - cur_chunk->rect.left = tv->padding.left; - line_right = cur_chunk->rect.right; - - TVGrowBoundsToFit(tv, cur_chunk); - TVDrawText(tv, cur_chunk, 0); - } + text_slab_data = *(text_slab->data) + text_slab->pos; - if (c == '\r') { - cur_chunk->linebreak = true; - - /* next line will need to know how tall our empty space was */ - if (cur_chunk->length == 0) { - prev_chunk = TVGetChunk(tv, cur_chunk->prev_chunk); - cur_chunk->rect.top = prev_chunk->rect.bottom - 1; - cur_chunk->rect.bottom = cur_chunk->rect.top + - CHUNK_HEIGHT(cur_chunk); - } - line_right = tv->padding.left; - } else { - cur_chunk->text[cur_chunk->length++] = c; - cur_chunk->free_length--; - line_right += width; - cur_chunk->rect.right += width; -#ifdef DEBUG_DRAWING - TVDrawText(tv, cur_chunk, 0); -#endif - } + if (length < avail) + avail = length; + memcpy(text_slab_data, text, avail); + text += avail; + text_slab->pos += avail; + length -= avail; + chunk->text_length += avail; + tv->text_length += avail; } -done: - TVDrawText(tv, cur_chunk, 0); + if (chunk) { + chunk->end = text_slab->pos; + if (!TVDrawChunk(tv, chunk)) + ret = false; + } + TVShowCanvas(tv); TVUnlock(tv); return ret; @@ -396,7 +335,7 @@ TVTrackScrollControl(ControlHandle control, short part struct TVChunk *chunk, *top_chunk = NULL; long n, top; bool horiz; - +#if 0 if (tv_track_control == NULL) { /* TVTrackScrollControl without TVSetTrackScrollControl */ Debugger(); @@ -484,6 +423,7 @@ TVTrackScrollControl(ControlHandle control, short part update_value: SetCtlValue(control, -(tv->scrolled.top)); TVUnlock(tv); +#endif } void @@ -501,41 +441,55 @@ TVClear(TVHandle tvh) { struct TVRec *tv; struct TVChunk *tchunk, *cur_chunk; + struct TVSlab *slab; short n; tv = TVLock(tvh); - /* don't dispose of first one */ - for (n = 1; n < sizeof(tv->chunk_slabs) / sizeof(Handle); n++) { - if (tv->chunk_slabs[n] != NULL) { - DisposHandle(tv->chunk_slabs[n]); - tv->chunk_slabs[n] = 0; - } + /* free chunk slabs except first one */ + for (n = 1; n < sizeof(tv->chunk_slabs) / sizeof(tv->chunk_slabs[0]); + n++) { + if (tv->chunk_slabs[n].data == NULL) + continue; + + DisposHandle(tv->chunk_slabs[n].data); + tv->chunk_slabs[n].data = NULL; + tv->chunk_slabs[n].size = 0; + tv->chunk_slabs[n].pos = 0; } - HLock(tv->chunk_slabs[0]); - memset((char *)*(tv->chunk_slabs[0]), 0, CHUNK_ALLOC_SIZE); + HLock(tv->chunk_slabs[0].data); + memset((char *)*(tv->chunk_slabs[0].data), 0, tv->chunk_slabs[0].size); + HUnlock(tv->chunk_slabs[0].data); + tv->last_chunk_slab = 0; - tv->cur_chunk = 0; - cur_chunk = TVGetChunk(tv, tv->cur_chunk); - cur_chunk->free_length = CHUNK_ALLOC_SIZE - sizeof(struct TVChunk); - cur_chunk->line = 1; - cur_chunk->rect.top = tv->padding.top; - cur_chunk->rect.left = tv->padding.left; - cur_chunk->rect.bottom = cur_chunk->rect.top; - cur_chunk->rect.right = cur_chunk->rect.left; - cur_chunk->next_chunk = EMPTY_CHUNKPTR; - cur_chunk->prev_chunk = EMPTY_CHUNKPTR; - HUnlock(tv->chunk_slabs[0]); + /* free text slabs except first one */ + for (n = 1; n < sizeof(tv->text_slabs) / sizeof(tv->text_slabs[0]); + n++) { + if (tv->text_slabs[n].data == NULL) + continue; + + DisposHandle(tv->text_slabs[n].data); + tv->text_slabs[n].data = NULL; + tv->text_slabs[n].size = 0; + tv->text_slabs[n].pos = 0; + } - tv->last_chunk = tv->cur_chunk; - tv->first_chunk = tv->cur_chunk; - tv->first_visible_chunk = tv->cur_chunk; - tv->last_visible_chunk = tv->cur_chunk; + HLock(tv->text_slabs[0].data); + memset((char *)*(tv->text_slabs[0].data), 0, tv->text_slabs[0].size); + HUnlock(tv->text_slabs[0].data); + tv->last_text_slab = 0; + + tv->first_chunk = 0; + tv->last_chunk = 0; + + tv->text_length = 0; tv->bounds.bottom = tv->padding.top + tv->padding.bottom; tv->scrolled = tv->bounds; - memset(&tv->last_style, 0, sizeof(tv->last_style)); + + memset(&tv->styles, 0, sizeof(tv->styles)); + tv->cur_style = NO_STYLE; TVDrawOnCanvas(tv); EraseRect(&tv->canvas.portRect); @@ -548,7 +502,7 @@ TVUpdate(TVHandle tvh) { struct TVRec *tv; struct TVChunk *chunk; - +#if 0 tv = TVLock(tvh); TVDrawOnCanvas(tv); @@ -563,11 +517,13 @@ TVUpdate(TVHandle tvh) TVShowCanvas(tv); TVUnlock(tv); +#endif } void TVDispose(TVHandle tvh) { +#if 0 struct TVRec *tv; /* this will free all but the first slab */ @@ -580,6 +536,7 @@ TVDispose(TVHandle tvh) ClosePort(&tv->canvas); DisposeHandle(tv->canvas_baseaddr); DisposeHandle(tvh); +#endif } /* @@ -614,175 +571,138 @@ TVUnlock(struct TVRec *tv) return; } - for (n = 0; n < sizeof(tv->chunk_slabs) / sizeof(Handle); n++) - if (tv->chunk_slabs[n]) - HUnlock(tv->chunk_slabs[n]); + for (n = 0; n < sizeof(tv->text_slabs) / sizeof(tv->text_slabs[0]); n++) + if (tv->text_slabs[n].data) + HUnlock(tv->chunk_slabs[n].data); + for (n = 0; n < sizeof(tv->chunk_slabs) / sizeof(tv->chunk_slabs[0]); n++) + if (tv->chunk_slabs[n].data) + HUnlock(tv->chunk_slabs[n].data); HUnlock(tv->canvas_baseaddr); HUnlock(RecoverHandle(tv)); } -struct TVChunk * -TVGetChunk(struct TVRec *tv, TVChunkPtr ptr) +struct TVSlab * +TVAllocateTextSlab(struct TVRec *tv) { - struct TVChunk *chunk; - unsigned short off; - Handle slab; - char *addr; + unsigned long size; + short slot; - if (ptr == 0xffffffffL) /* XXX: why doesn't EMPTY_CHUNKPTR work? */ + if (tv->last_text_slab == 0 && tv->text_slabs[0].data == NULL) + slot = 0; + else + slot = tv->last_text_slab + 1; + + if (slot > sizeof(tv->text_slabs) / sizeof(tv->text_slabs[0])) { + /* no free slots */ + Debugger(); return NULL; + } - HLock(tv->chunk_slabs[CHUNKPTR_SLAB(ptr)]); - addr = *(tv->chunk_slabs[CHUNKPTR_SLAB(ptr)]); - - off = CHUNKPTR_OFFSET(ptr); - if (off % 2 != 0) + size = TEXT_SLAB_SIZE; + tv->text_slabs[slot].data = NewHandleClear(size); + if (tv->text_slabs[slot].data == NULL) { + /* TODO: try smaller allocations */ Debugger(); - addr += off; - chunk = (struct TVChunk *)addr; - - return chunk; + return NULL; + } + tv->text_slabs[slot].size = size; + tv->text_slabs[slot].pos = 0; + tv->last_text_slab = slot; + + /* leave locked */ + HLock(tv->text_slabs[slot].data); + + return &tv->text_slabs[slot]; } -struct TVChunk * -TVCreateChunk(struct TVRec *tv, unsigned long length) +struct TVSlab * +TVAllocateChunkSlab(struct TVRec *tv) { - struct TVChunk *chunk, *cur_chunk; - TVChunkPtr cptr; - unsigned long addr, size; - unsigned short off; - Handle slab; - short slab_slot, n; - - cur_chunk = TVGetChunk(tv, tv->cur_chunk); - - if (length + sizeof(struct TVChunk) + 1 > cur_chunk->free_length) { - /* allocate new slab */ - slab_slot = 0; - for (n = 1; n < sizeof(tv->chunk_slabs) / sizeof(Handle); n++) { - if (tv->chunk_slabs[n] == NULL) { - slab_slot = n; - break; - } - } - if (slab_slot == 0) - return NULL; - size = CHUNK_ALLOC_SIZE; - slab = NewHandleClear(size + 128); - if (slab == NULL) { - /* out of memory, try to allocate smaller chunks */ - size /= 2; - slab = NewHandleClear(size); - if (slab == NULL) { - size /= 2; - slab = NewHandleClear(size); - if (slab == NULL) - /* oh well, we tried all we could */ - return NULL; - } - } - HLock(slab); - tv->chunk_slabs[slab_slot] = slab; - SET_CHUNKPTR_SLAB(cptr, slab_slot); - SET_CHUNKPTR_OFFSET(cptr, 0); - chunk = TVGetChunk(tv, cptr); - chunk->ptr = cptr; - chunk->free_length = size - sizeof(struct TVChunk); - } else { - /* create new chunk in existing alloc */ - off = CHUNKPTR_OFFSET(cur_chunk->ptr) + sizeof(struct TVChunk) + - cur_chunk->length; - if (off % 2 != 0) - off++; - SET_CHUNKPTR_SLAB(cptr, CHUNKPTR_SLAB(cur_chunk->ptr)); - SET_CHUNKPTR_OFFSET(cptr, off); - chunk = TVGetChunk(tv, cptr); - chunk->ptr = cptr; - chunk->free_length = cur_chunk->free_length - - sizeof(struct TVChunk); - } + unsigned long size; + short slot; - cur_chunk->free_length = 0; + if (tv->last_chunk_slab == 0 && tv->chunk_slabs[0].data == NULL) + slot = 0; + else + slot = tv->last_chunk_slab + 1; - chunk->prev_chunk = cur_chunk->ptr; - chunk->next_chunk = EMPTY_CHUNKPTR; + if (slot > sizeof(tv->chunk_slabs) / sizeof(tv->chunk_slabs[0])) { + /* no free slots */ + Debugger(); + return NULL; + } - chunk->line = cur_chunk->line; - chunk->rect.top = cur_chunk->rect.top; - chunk->rect.left = chunk->rect.right = cur_chunk->rect.right; - chunk->rect.bottom = cur_chunk->rect.bottom; + size = CHUNK_SLAB_SIZE; + tv->chunk_slabs[slot].data = NewHandleClear(size); + if (tv->chunk_slabs[slot].data == NULL) { + /* TODO: try smaller allocations */ + Debugger(); + return NULL; + } + tv->chunk_slabs[slot].size = size; + tv->chunk_slabs[slot].pos = 0; + tv->last_chunk_slab = slot; - return chunk; + return &tv->chunk_slabs[slot]; } struct TVChunk * -TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, unsigned long length) +TVGetChunk(struct TVRec *tv, TVChunkPtr ptr) { - Rect r; - struct TVChunk *new_chunk, *prev_chunk, *next_chunk; - short width; - long oldright; + struct TVSlab *slab; + unsigned short slabn; + char *addr; + + if (ptr == 0) + return NULL; + + slabn = CHUNKPTR_SLAB(ptr); + if (slabn >= sizeof(tv->chunk_slabs) / sizeof(tv->chunk_slabs[0])) + return NULL; + + slab = &tv->chunk_slabs[slabn]; + HLock(slab->data); + addr = *(slab->data) + CHUNKPTR_OFFSET(ptr); + if ((unsigned long)addr % 2 != 0) + Debugger(); + + /* keep slab locked */ + + return (struct TVChunk *)addr; +} + +struct TVChunk * +TVCreateChunk(struct TVRec *tv) +{ + struct TVSlab *slab; + struct TVChunk *chunk; + TVChunkPtr cptr; - /* - * This leaks some space by not putting the new chunk right after - * chunk->text[length], but it avoids having to create a temporary - * buffer to copy chunk->text[length .. total length] to just to move - * it to new_chunk->text. - */ - new_chunk = TVCreateChunk(tv, chunk->length - length); - new_chunk->baseline = chunk->baseline; - new_chunk->style = chunk->style; + slab = &tv->chunk_slabs[tv->last_chunk_slab]; - if ((next_chunk = TVGetChunk(tv, chunk->next_chunk))) { - /* insert new_chunk between chunk and next_chunk */ - chunk->next_chunk = new_chunk->ptr; - next_chunk->prev_chunk = new_chunk->ptr; - new_chunk->prev_chunk = chunk->ptr; - new_chunk->next_chunk = next_chunk->ptr; - } else { - /* append */ - chunk->next_chunk = new_chunk->ptr; - new_chunk->prev_chunk = chunk->ptr; - new_chunk->next_chunk = EMPTY_CHUNKPTR; - tv->last_chunk = new_chunk->ptr; + if (slab->size - slab->pos < sizeof(struct TVChunk)) { + if ((slab = TVAllocateChunkSlab(tv)) == NULL) + return NULL; } - /* assigning to tv->cur_chunk must be done by caller */ + if (tv->last_chunk_slab == 0 && slab->pos == 0 && !tv->first_chunk) + /* for first chunk, offset pos so it's not confused with NULL */ + slab->pos = 2; - new_chunk->length = chunk->length - length; - new_chunk->free_length -= new_chunk->length; - memcpy(new_chunk->text, chunk->text + length, chunk->length - length); - chunk->length = length; + SET_CHUNKPTR_SLAB(cptr, tv->last_chunk_slab); + SET_CHUNKPTR_OFFSET(cptr, slab->pos); - TVApplyStyle(&chunk->style); - width = TextWidth(chunk->text, 0, chunk->length); - oldright = chunk->rect.right; - chunk->rect.right = chunk->rect.left + 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); - } + chunk = TVGetChunk(tv, cptr); + chunk->ptr = cptr; + slab->pos += sizeof(struct TVChunk); - /* line us up next to the current chunk */ - new_chunk->baseline = chunk->baseline; - new_chunk->rect.top = chunk->rect.top; - new_chunk->rect.bottom = chunk->rect.bottom; - new_chunk->rect.left = chunk->rect.right; - new_chunk->rect.right = new_chunk->rect.left + - TextWidth(new_chunk->text, 0, new_chunk->length); + if (!tv->first_chunk) + tv->first_chunk = cptr; - /* new chunk not drawn */ - return new_chunk; + tv->last_chunk = cptr; + + return chunk; } void @@ -793,6 +713,46 @@ TVApplyStyle(struct TVStyle *style) TextSize(style->size); } +struct TVStyle * +TVFindStyle(struct TVRec *tv, struct TVStyle *style) +{ + struct TVStyle *cur_style; + short n; + + if (tv->cur_style != NO_STYLE) { + cur_style = &tv->styles[tv->cur_style]; + + if (ARE_STYLES_EQUAL(cur_style, style)) + return cur_style; + } + + for (n = 0; n < sizeof(tv->styles) / sizeof(tv->styles[0]); n++) + if (ARE_STYLES_EQUAL(&tv->styles[n], style)) + return &tv->styles[n]; + + /* allocate a new one */ + for (n = 0; n < sizeof(tv->styles) / sizeof(tv->styles[0]); n++) { + if (IS_STYLE_EMPTY(&tv->styles[n])) { + /* only copy over the important fields, rest will stay 0 */ + tv->styles[n].font = style->font; + tv->styles[n].style = style->style; + tv->styles[n].size = style->size; + cur_style = &tv->styles[n]; + GetFontInfo(&cur_style->font_info); + cur_style->height = //cur_style->font_info.leading + + cur_style->font_info.ascent + cur_style->font_info.descent; + cur_style->baseline = cur_style->font_info.leading + + cur_style->font_info.ascent; + tv->cur_style = n; + return cur_style; + } + } + + /* no slots left */ + Debugger(); + return NULL; +} + void TVGrowBoundsToFit(struct TVRec *tv, struct TVChunk *chunk) { @@ -805,60 +765,119 @@ TVGrowBoundsToFit(struct TVRec *tv, struct TVChunk *ch TVScrollUnlocked(tv, 0, -SHRT_MAX); } -void -TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off) +bool +TVDrawChunk(struct TVRec *tv, struct TVChunk *chunk) { - struct TVChunk *last; - Point p; - long l, b; + struct TVSlab *text_slab; + struct TVChunk *prev_chunk, *tchunk; + struct TVStyle *style; Rect r; + Point pen; + unsigned long avail, start_pos; + long pos, j, n, back_width, back_n; + short width, prev_style; + char c, tc, *text_slab_data; + bool new_line; + + /* setup style */ + style = &tv->styles[chunk->style]; + if (style == NULL) + return false; + TVApplyStyle(style); + + TVDrawOnCanvas(tv); - if (off != 0) { - /* TODO: implement redrawing a partial string */ - Debugger(); - return; - } + /* setup text slab */ + text_slab = &tv->text_slabs[chunk->slab]; + text_slab_data = *(text_slab->data); + + /* continue where the previous chunk left off */ + MoveTo(tv->scrolled.left + chunk->rect_end_right, + tv->scrolled.top + chunk->rect.bottom - + (style->height - style->baseline)); + + new_line = false; + for (n = chunk->last_drawn; 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 (chunk->length - off > 0) { - l = tv->scrolled.left + chunk->rect.left; - b = tv->scrolled.top + chunk->rect.top + chunk->baseline; - - if (b + chunk->style.font_info.descent >= 0 && - tv->scrolled.top + chunk->rect.top <= tv->view_height) { - MoveTo(l, b); - TVApplyStyle(&chunk->style); - DrawText(chunk->text, off, chunk->length - off); - + if (c != ' ' && chunk->rect_end_right + width >= + tv->bounds.right - tv->padding.right) { + new_line = true; + + /* need to wrap this word, walk backwards until we break */ + GetPen(&pen); + back_width = 0; + for (back_n = 1; back_n < chunk->end - chunk->start; + back_n++) { + tc = text_slab_data[n - back_n]; + if (tc != ' ') { + back_width += style->width_cache[tc]; + continue; + } + r.bottom = pen.v + (style->height - style->baseline) + + style->font_info.descent; + r.top = r.bottom - style->height; + r.right = pen.h; + r.left = r.right - back_width; #ifdef DEBUG_DRAWING - r.top = chunk->rect.top + tv->scrolled.top; - r.bottom = chunk->rect.bottom + tv->scrolled.top; - r.left = tv->scrolled.left + chunk->rect.left; - r.right = tv->scrolled.left + chunk->rect.right; - if (r.right == r.left) - r.right++; - FrameRect(&r); + FrameRect(&r); +#else + EraseRect(&r); #endif + /* put the genie back in the bottle */ + n -= back_n - 1; + avail += back_n - 1; + chunk->rect_end_right -= back_width; + c = text_slab_data[n]; + width = style->width_cache[c]; + break; /* and continue below with new c */ + } + } + } - if (tv->cur_chunk != chunk->ptr) { - last = TVGetChunk(tv, tv->last_visible_chunk); - if (chunk->rect.top > last->rect.top) - tv->last_visible_chunk = chunk->ptr; + if (new_line) { + new_line = false; + chunk->rect.left = tv->padding.left; + chunk->end_line++; + + if (n == chunk->start) { + /* we didn't put any text on previous line, start over */ + chunk->rect.top += style->height; + chunk->rect.bottom = chunk->rect.top + style->height; + chunk->rect.right = chunk->rect.left; + chunk->rect_start_left = chunk->rect.left; + chunk->rect_end_right = chunk->rect.left; + chunk->start_line++; + } else { + chunk->rect.bottom += style->height; + if (chunk->rect_end_right > chunk->rect.right) + chunk->rect.right = chunk->rect_end_right; + chunk->rect_end_right = chunk->rect.left; } + + TVGrowBoundsToFit(tv, chunk); + MoveTo(tv->scrolled.left + chunk->rect_end_right, + tv->scrolled.top + chunk->rect.bottom - + (style->height - style->baseline)); } + + chunk->rect_end_right += width; + + DrawChar(c); } -#if 0 - if (tv->sel_start == chunk) { - r.left = l; - r.right = p.h; - r.top = b - chunk->baseline; - r.bottom = chunk->rect.bottom; - InvertRect(&r); - } -#endif - -TVShowCanvas(tv); -TVDrawOnCanvas(tv); + chunk->last_drawn = chunk->end; + TVGrowBoundsToFit(tv, chunk); + + TVShowCanvas(tv); + return true; } void @@ -869,7 +888,6 @@ TVScrollUnlocked(struct TVRec *tv, short horiz, short Rect was, sect; RgnHandle vacated; struct TVChunk *chunk, *start, *last = NULL; - bool first = false; if (!horiz && !vert) return; @@ -900,35 +918,18 @@ TVScrollUnlocked(struct TVRec *tv, short horiz, short top = tv->scrolled.top - was.top; ScrollRect(&tv->canvas.portRect, left, top, vacated); -#if 0 rvacated = tv->scrolled; - rvacated.bottom = top; + rvacated.top = tv->scrolled.bottom; + rvacated.bottom = rvacated.top - top; - if (top < 0) - start = TVGetChunk(tv, tv->last_visible_chunk); - else - start = TVGetChunk(tv, tv->first_visible_chunk); - - for (chunk = start; chunk != NULL; - chunk = TVGetChunk(tv, - (top < 0 ? chunk->prev_chunk : chunk->next_chunk))) { - if (chunk->rect.top > rvacated.bottom) - continue; + for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL; + chunk = TVGetChunk(tv, chunk->next_chunk)) { if (chunk->rect.bottom < rvacated.top) continue; - if (!first) { - tv->first_visible_chunk = chunk->ptr; - first = true; - } - TVDrawText(tv, chunk, 0); - last = chunk; + if (chunk->rect.top > rvacated.bottom) + break; + //TVDrawChunk(tv, chunk); } - - if (last) - tv->last_visible_chunk = last->ptr; - else - tv->last_visible_chunk = tv->first_visible_chunk; -#endif DisposeRgn(vacated); TVShowCanvas(tv); @@ -944,6 +945,7 @@ TVDrawOnCanvas(struct TVRec *tv) Debugger(); #ifdef DEBUG_DRAWING + SetOrigin(-tv->view.left, -tv->view.top); return; #endif @@ -961,6 +963,7 @@ TVShowCanvas(struct TVRec *tv) return; #ifdef DEBUG_DRAWING + SetOrigin(0, 0); return; #endif --- textview.h Wed Oct 9 17:26:55 2024 +++ textview.h Fri Oct 11 20:25:58 2024 @@ -19,17 +19,22 @@ #include "stdint.h" struct TVStyle { - struct FontInfo font_info; unsigned char font; unsigned char style; unsigned char size; + + /* these are private and ignored when fed into TVAppend() */ + struct FontInfo font_info; + unsigned char height; + unsigned char baseline; + unsigned char width_cache[256]; }; struct BigRect { long top; - long left; - long bottom; long right; + long bottom; + long left; }; typedef struct BigRect BigRect; #define BigRectToRect(br, r) SetRect(&(r), (br).left, (br).top, (br).right, (br).bottom) @@ -55,18 +60,25 @@ struct TVChunk { TVChunkPtr ptr; TVChunkPtr prev_chunk; TVChunkPtr next_chunk; - long free_length; - unsigned long line; - struct TVStyle style; + unsigned char style; + unsigned char slab; + unsigned short start; + unsigned short end; + unsigned long start_line; + unsigned long end_line; + unsigned long text_length; + unsigned short rect_start_left; + unsigned short rect_end_right; + unsigned short last_drawn; BigRect rect; - unsigned short length; - unsigned short sel_start; - unsigned short sel_finish; - unsigned char baseline; - bool linebreak; - char text[]; }; +struct TVSlab { + Handle data; + unsigned long size; + unsigned long pos; +}; + struct TVRec { /* bounds of where we live in window's port */ Rect view; @@ -85,20 +97,19 @@ struct TVRec { /* bounds as it is scrolled */ BigRect scrolled; - struct TVStyle last_style; + struct TVStyle styles[32]; - char width_cache[256]; + struct TVSlab text_slabs[32]; + struct TVSlab chunk_slabs[64]; - Handle chunk_slabs[64]; TVChunkPtr first_chunk; - TVChunkPtr cur_chunk; TVChunkPtr last_chunk; - TVChunkPtr first_visible_chunk; - TVChunkPtr last_visible_chunk; - - struct TVChunk *sel_start; - struct TVChunk *sel_finish; + unsigned long text_length; + + unsigned char cur_style; + unsigned char last_text_slab; + unsigned char last_chunk_slab; char unlock_refcnt; bool autoscroll; };