jcs
/textview
/amendments
/64
TVDrawMatch: Fix drawing text with tabs, other minor fixes
jcs made amendment 64 2 months ago
--- textview.c Fri Dec 20 19:58:15 2024
+++ textview.c Wed Sep 23 13:10:54 2026
@@ -166,8 +166,7 @@ TVAppendBitMap(TVHandle tvh, BitMap *bitmap, struct TV
width = bitmap->bounds.right - bitmap->bounds.left;
height = bitmap->bounds.bottom - bitmap->bounds.top;
- bitmap_size = ((((width - 1) / (bitmap->rowBytes * 8)) + 1) * 2) *
- height;
+ bitmap_size = (long)bitmap->rowBytes * height;
style = (struct TVStyle *)NewPtrClear(sizeof(struct TVStyle) +
bitmap_size);
@@ -179,6 +178,8 @@ TVAppendBitMap(TVHandle tvh, BitMap *bitmap, struct TV
memcpy(style->bitmap_data, bitmap->baseAddr, bitmap_size);
style->bitmap.baseAddr = (Ptr)style->bitmap_data;
+ tv = TVLock(tvh);
+
if (!bstyle)
bstyle = TVGetStyle(tv, tv->cur_style);
if (bstyle) {
@@ -189,7 +190,6 @@ TVAppendBitMap(TVHandle tvh, BitMap *bitmap, struct TV
style->tag = bstyle->tag;
}
- tv = TVLock(tvh);
ret = TVAppendLocked(tv, style, " ", 1, 0);
TVUnlock(tv);
@@ -282,6 +282,7 @@ TVResize(TVHandle tvh)
tv->nlines = 0;
tv->last_space = 0;
+ tv->space_width = 0;
for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL;
chunk = TVGetChunk(tv, chunk->next_chunk)) {
@@ -308,7 +309,7 @@ TVResize(TVHandle tvh)
if (line == NULL)
break;
- if (line->start >= top_start && line->end <= top_start) {
+ if (line->start <= top_start && top_start < line->end) {
/* keep this line at the top */
tv->scrolled.top = -(line->rect.top);
tv->scrolled.bottom = tv->scrolled.top +
@@ -488,8 +489,10 @@ TVGetText(TVHandle tvh, char *dst, unsigned long start
tv = TVLock(tvh);
+ if (end > tv->text_length)
+ end = tv->text_length;
read = 0;
- while (read < end - start) {
+ while (start + read < end) {
slab = TVGetTextSlab(tv, start + read);
if (slab == NULL) {
read = 0;
@@ -597,14 +600,18 @@ TVTrackScrollControl(ControlHandle control, short part
TVFindInRectLocked(tv, &visible, 0, TVSetTopBottomMatch, NULL);
if (tv->top_line == NULL) {
- tv->top_line = TVGetLine(tv, nline);
- if (tv->top_line == NULL)
+ tv->top_line = TVGetLine(tv, 1);
+ if (tv->top_line == NULL) {
+ TVUnlock(tv);
return;
+ }
}
if (tv->bottom_line == NULL) {
tv->bottom_line = TVGetLine(tv, tv->nlines);
- if (tv->bottom_line == NULL)
+ if (tv->bottom_line == NULL) {
+ TVUnlock(tv);
return;
+ }
}
switch (part) {
@@ -641,6 +648,8 @@ TVTrackScrollControl(ControlHandle control, short part
/* scroll to the line after the bottom-most line */
if (tv->bottom_line->nline < tv->nlines) {
line = TVGetLine(tv, tv->bottom_line->nline + 1);
+ if (line == NULL)
+ break;
TVScrollUnlocked(tv, 0,
-(tv->scrolled.top - tv->frame.bottom + line->rect.bottom));
break;
@@ -715,7 +724,7 @@ TVClear(TVHandle tvh)
memset(slab->data, 0, slab->size);
slab->pos = 0;
slab->next_slab = NULL;
- HUnlock(tv->first_text_slab);
+ HUnlock(tv->first_style_slab);
tv->cur_style = 0;
@@ -757,6 +766,7 @@ TVClear(TVHandle tvh)
tv->text_length = 0;
tv->nlines = 0;
tv->last_space = 0;
+ tv->space_width = 0;
/* clear line groups except first one */
for (n = 1; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]);
@@ -821,6 +831,7 @@ TVDispose(TVHandle tvh)
#ifdef USE_CANVAS
ClosePort(tv->canvas);
+ DisposPtr(tv->canvas);
DisposeHandle(tv->canvas_baseaddr);
#endif
@@ -851,7 +862,7 @@ TVUnlock(struct TVRec *tv)
struct TVSlab *slab;
short n;
- if (tv->unlock_refcnt-- != 0) {
+ if (--tv->unlock_refcnt != 0) {
if (tv->unlock_refcnt < 0)
Debugger();
return;
@@ -935,7 +946,6 @@ TVSetupCanvas(struct TVRec *tv)
RectRgn(tv->canvas->clipRgn, &tv->canvas->portRect);
RectRgn(tv->canvas->visRgn, &tv->canvas->portRect);
SetPort(old_port);
- HUnlock(tv->canvas_baseaddr);
tv->canvas->portRect.right = width;
tv->canvas->portRect.bottom = height;
@@ -1011,7 +1021,7 @@ TVAllocateLineGroup(struct TVRec *tv, short groupn)
{
short n;
- if (groupn > sizeof(tv->line_groups) / sizeof(tv->line_groups[0]))
+ if (groupn >= sizeof(tv->line_groups) / sizeof(tv->line_groups[0]))
return NULL;
if (tv->line_groups[groupn] == NULL)
@@ -1058,6 +1068,8 @@ TVGetChunk(struct TVRec *tv, TVSlabLocator loc)
break;
slabh = slab->next_slab;
}
+ if (slabh == NULL)
+ return NULL;
addr = slab->data + SLABLOC_OFFSET(loc);
if ((unsigned long)addr % 2 != 0) {
@@ -1224,7 +1236,7 @@ create_style:
HLock(slabh);
slab = (struct TVSlab *)*slabh;
- if (slab->pos + sizeof(struct TVStyle) + tstyle->bitmap_data_size >
+ if (slab->pos + sizeof(struct TVStyle) + style->bitmap_data_size >
slab->size) {
/* need a new slab */
slabh = TVAllocateSlab(tv, TV_STYLE_SLAB_SIZE,
@@ -1306,7 +1318,7 @@ TVAppendLocked(struct TVRec *tv, struct TVStyle *style
new_chunk = true;
/* setup text slab */
- HUnlock(tv->last_text_slab);
+ HLock(tv->last_text_slab);
text_slab = (struct TVSlab *)*(tv->last_text_slab);
/* setup chunk */
@@ -1348,16 +1360,17 @@ TVAppendLocked(struct TVRec *tv, struct TVStyle *style
if (length < avail)
avail = length;
- memcpy(text_slab_data, text, avail);
+ if (terminator && avail == length) {
+ memcpy(text_slab_data, text, avail - 1);
+ text_slab_data[avail - 1] = terminator;
+ } else
+ memcpy(text_slab_data, text, avail);
text += avail;
text_slab->pos += avail;
length -= avail;
tv->text_length += avail;
chunk->end += avail;
chunk->need_recalc = true;
-
- if (length == 0 && terminator)
- text_slab->data[text_slab->pos - 1] = terminator;
}
if (!tv->autocalc)
@@ -1426,7 +1439,7 @@ TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dir
struct TVLine *line, *prev_line, *next_line;
unsigned long avail, start, nline, max_right;
long n, back_width, back_n, next_tabstop;
- short width, space_width, height;
+ short width, height;
char *text_slab_data;
register unsigned char c;
Boolean new_line;
@@ -1519,28 +1532,27 @@ reload_chunk:
width = 0;
} else {
if (style->bitmap_data_size)
- width = style->font_info.widMax;
+ width = style->bitmap.bounds.right -
+ style->bitmap.bounds.left;
else if (c == '\t' && tv->tabstop) {
- next_tabstop = (long)tv->tabstop *
+ next_tabstop = line->rect.left +
+ (long)tv->tabstop *
(((chunk->end_right - line->rect.left) /
tv->tabstop) + 1);
- if (next_tabstop == chunk->end_right)
- next_tabstop += tv->tabstop;
- width = next_tabstop - chunk->end_right -
- line->rect.left;
+ width = next_tabstop - chunk->end_right;
} else if (!(width = style->width_cache[c]))
width = style->width_cache[c] = CharWidth(c);
if (c == ' ' && !style->bitmap_data_size) {
tv->last_space = n;
- space_width = 0;
+ tv->space_width = 0;
} else if (!new_line && tv->linewrap &&
chunk->end_right + width >= max_right) {
new_line = true;
if (tv->last_space) {
/* break after the last space */
n = tv->last_space + 1;
- chunk->end_right -= space_width;
+ chunk->end_right -= tv->space_width;
line->end = tv->last_space + 1;
line->rect.right = chunk->end_right;
@@ -1558,7 +1570,7 @@ reload_chunk:
}
/* otherwise, hard-break this long word */
} else
- space_width += width;
+ tv->space_width += width;
}
if (line->start == line->end) {
@@ -1570,7 +1582,7 @@ reload_chunk:
if (new_line) {
new_line = false;
- space_width = 0;
+ tv->space_width = 0;
tv->last_space = 0;
line->end = n;
@@ -1672,7 +1684,7 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch
void *cookie)
{
unsigned long n, j;
- long next_tabstop;
+ long next_tabstop, origin;
unsigned char c;
char *text, *spanstart;
short ts;
@@ -1728,6 +1740,7 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch
CopyBits(&match->style->bitmap, &thePort->portBits,
&match->style->bitmap.bounds, &r, srcOr, NULL);
} else if (tv->tabstop) {
+ origin = match->line->rect.left + match->line->indent;
text = match->text_slab_data + (match->text_start -
match->text_slab->base);
spanstart = NULL;
@@ -1737,10 +1750,8 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch
if (spanstart != NULL)
DrawText(spanstart, 0, text - spanstart);
GetPen(&pen);
- next_tabstop = (long)tv->tabstop *
- (((pen.h - match->rect.left) / tv->tabstop) + 1);
- if (next_tabstop == pen.h)
- next_tabstop += tv->tabstop;
+ next_tabstop = origin + ((long)tv->tabstop *
+ (((pen.h - origin) / tv->tabstop) + 1));
#if 0
/* draw dotted lines for tabs */
/* XXX: disabled for now but might be useful as an option */
@@ -1790,9 +1801,12 @@ TVFindInRectLocked(struct TVRec *tv, BigRect *rect, lo
if (ppl == 0)
ppl = 1;
startline = rect->top / ppl;
+ if (startline > tv->nlines)
+ startline = tv->nlines;
if (startline > 1) {
match.line = TVGetLine(tv, startline);
- while (startline > 0 && match.line->rect.top > rect->top) {
+ while (startline > 1 && match.line != NULL &&
+ match.line->rect.top > rect->top) {
startline--;
match.line = TVGetLine(tv, startline);
}
--- textview.h Sun Dec 15 16:22:12 2024
+++ textview.h Wed Sep 23 13:10:54 2026
@@ -32,12 +32,12 @@ typedef struct BigRect BigRect;
(br).left = (r).left; \
(br).top = (r).top; \
(br).right = (r).right; \
- (br).bottom = (r).bottom; )
+ (br).bottom = (r).bottom; }
/* when storing data in slabs, TVSlabLocator stores slab id and offset */
typedef unsigned long TVSlabLocator;
-/* high 4 are slab id, low 4 byte offset */
+/* high 2 bytes are slab id, low 2 bytes are offset */
#define SLABLOC_SLABID(x) (((x) >> 16) & 0xffff)
#define SLABLOC_OFFSET(x) ((x) & 0x0000ffff)
@@ -139,6 +139,7 @@ struct TVRec {
#define TV_TEXT_SLAB_SIZE (8 * 1024)
unsigned long text_length;
unsigned long last_space;
+ short space_width;
Handle first_chunk_slab;
Handle last_chunk_slab;