/* * Copyright (c) 2023-2024 joshua stein * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include "textview.h" Boolean TVResize(TVHandle tvh) { struct TVRec *tv; long n; struct TVLine *line; struct TVChunk *chunk; unsigned long top_start; tv = TVLock(tvh); SetRect(&tv->frame, 0, 0, tv->view.right - tv->view.left, tv->view.bottom - tv->view.top); TVUseCanvas(tv); EraseRect(&tv->frame); TVRevealCanvas(tv); /* find text position of first visible line to try to stay there */ top_start = 0; line = TVGetLine(tv, TVLineForY(tv, -(tv->scrolled.top))); if (line != NULL) top_start = line->start; #ifdef USE_CANVAS if (!TVSetupCanvas(tv)) { TVUnlock(tv); return false; } #endif tv->bounds.right = tv->frame.right; tv->bounds.bottom = tv->padding.top + tv->padding.bottom; tv->scrolled = tv->bounds; /* clear line groups except first one */ for (n = 1; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]); n++) { if (tv->line_groups[n] == NULL) continue; DisposHandle(tv->line_groups[n]); tv->line_groups[n] = NULL; } HLock(tv->line_groups[0]); memset((char *)*(tv->line_groups[0]), 0, TV_LINE_GROUP_SIZE); HUnlock(tv->line_groups[0]); tv->nlines = 0; tv->last_space = 0; tv->space_width = 0; tv->pending_redraw = false; for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL; chunk = TVGetChunk(tv, chunk->next_chunk)) { chunk->start_line = 0; chunk->end_line = 0; chunk->start_left = 0; chunk->end_right = 0; chunk->need_recalc = true; } if (!TVCalcLinesLocked(tv, true)) { /* XXX: if this fails, what are we supposed to do? */ TVUnlock(tv); return false; } if (tv->bounds.bottom > tv->frame.bottom) { if (tv->autoscroll) { tv->scrolled.bottom = tv->frame.bottom; tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom; } else { /* keep the line now holding that text at the top */ line = TVGetLine(tv, TVLineForOffset(tv, top_start)); if (line != NULL && line->start <= top_start && top_start < line->end) { tv->scrolled.top = -(line->rect.top); tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom; } if (tv->scrolled.bottom < tv->frame.bottom) { tv->scrolled.bottom = tv->frame.bottom; tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom; } } } TVRedrawRect(tv, &tv->frame, 0, true); TVUnlock(tv); return true; } long TVGetTextOffset(TVHandle tvh, Point p) { struct TVRec *tv; long off; tv = TVLock(tvh); off = TVOffsetForPoint(tv, (long)p.h - tv->scrolled.left - tv->view.left, (long)p.v - tv->scrolled.top - tv->view.top, false); TVUnlock(tv); return off; } Boolean TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dirty) { struct TVSlab *text_slab; struct TVChunk *chunk, *last_chunk, *prev_chunk; struct TVStyle *style; struct TVLine *line, *prev_line, *next_line; unsigned long start, nline; long n; short width, height, max_right; char *text_slab_data; register unsigned char *p, c; Boolean new_line; if (tv->first_chunk == 0) { tv->nlines = 0; return true; } if (ignore_dirty) chunk = TVGetChunk(tv, tv->first_chunk); else { /* find first dirty chunk but search from the end */ 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->need_recalc) break; chunk = prev_chunk; } } max_right = tv->view.right - tv->view.left - tv->padding.left - tv->padding.right; for (; chunk != NULL; chunk = TVGetChunk(tv, chunk->next_chunk)) { new_line = false; reload_chunk: style = TVGetStyle(tv, chunk->style); if (style == NULL) return false; TVApplyStyle(style); if (chunk->end_line == 0) { /* working with a new text chunk */ 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); if (prev_chunk == NULL) return false; 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; } } /* setup text slab */ text_slab = TVGetTextSlab(tv, chunk->start); if (text_slab == NULL) return false; text_slab_data = text_slab->data; /* find our current line */ nline = chunk->end_line; line = TVGetLine(tv, nline); if (line == NULL) return false; if (nline == 1) { line->rect.left = tv->padding.left; line->rect.top = tv->padding.top; line->start = 0; line->chunk = tv->first_chunk; start = line->end; } else { prev_line = TVGetLine(tv, nline - 1); if (prev_line == NULL) return false; line->rect.top = prev_line->rect.bottom; if (line->end) start = line->end; else start = prev_line->end; } line->nline = nline; height = style->font_info.ascent + style->font_info.descent + style->font_info.leading; /* * Walk each character and find its width, possibly wrapping and * starting a new line. */ p = (unsigned char *)text_slab_data + (start - text_slab->base); for (n = start; n < chunk->end; n++, p++) { c = *p; if (c == '\r') { new_line = true; width = 0; } else { if (c == '\t' || !(width = style->width_cache[c])) width = TVCharWidth(tv, style, line, chunk->end_right, c); if (c == ' ' && !style->bitmap_data_size) { tv->last_space = n; tv->space_width = 0; } else if (!new_line && tv->linewrap && chunk->end_right + width > max_right + tv->padding.left) { new_line = true; if (tv->last_space) { /* break after the last space */ n = tv->last_space + 1; chunk->end_right -= tv->space_width; line->end = n; line->rect.right = chunk->end_right; if (n >= chunk->start) { n = tv->last_space; p = (unsigned char *)text_slab_data + (n - text_slab->base); continue; } while (n < chunk->start) { if (!chunk->prev_chunk) { /* how did we get here? */ Debugger(); return false; } chunk->end_line = 0; chunk = TVGetChunk(tv, chunk->prev_chunk); if (chunk == NULL) return false; } chunk->end_right = line->rect.right; /* setup chunk with new_line set */ goto reload_chunk; } /* otherwise, hard-break this long word */ } else tv->space_width += width; } if (line->start == line->end) { /* start of new line disregards previous line's style */ line->baseline = style->font_info.descent; line->leading = style->font_info.leading; line->rect.bottom = line->rect.top + height; } if (new_line) { new_line = false; tv->space_width = 0; tv->last_space = 0; line->end = n; line->rect.right = chunk->end_right; if (style->align == TV_ALIGN_CENTER) line->indent = (max_right - (line->rect.right - line->rect.left)) / 2; else if (style->align == TV_ALIGN_RIGHT) line->indent = max_right - (line->rect.right - line->rect.left); else line->indent = 0; if (n == chunk->start) { /* no text on previous line, start on next line */ chunk->start_left = tv->padding.left; chunk->end_right = chunk->start_left; chunk->start_line++; } else if (height > line->rect.bottom - line->rect.top) line->rect.bottom = line->rect.top + height; nline++; chunk->end_line = nline; chunk->end_right = tv->padding.left; next_line = TVGetLine(tv, nline); if (next_line == NULL) return false; next_line->nline = nline; next_line->start = n; if (c == '\r') next_line->start++; next_line->chunk = chunk->loc; next_line->leading = style->font_info.leading; next_line->rect.top = line->rect.bottom; 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 += height; line = next_line; } chunk->end_right += width; if (line->start != line->end) { /* if style changes mid-line, previous chunks may grow */ if (style->font_info.descent > line->baseline) { if (line->baseline) line->grew = true; line->baseline = style->font_info.descent; } if (style->font_info.leading > line->leading) { if (line->leading) line->grew = true; line->leading = style->font_info.leading; } if (line->rect.bottom == 0 || (line->rect.bottom - line->rect.top < height)) { if (line->rect.bottom != line->rect.top && line->rect.bottom != 0) line->grew = true; line->rect.bottom = line->rect.top + height; } } } line->end = chunk->end; line->rect.right = chunk->end_right; chunk->end_line = nline; chunk->need_recalc = false; if (style->align == TV_ALIGN_CENTER) line->indent = (max_right - (line->rect.right - line->rect.left)) / 2; else if (style->align == TV_ALIGN_RIGHT) line->indent = max_right - (line->rect.right - line->rect.left); else line->indent = 0; } tv->nlines = nline; line = TVGetLine(tv, nline); if (line == NULL) return false; 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; } return true; } Boolean TVFindInRectLocked(struct TVRec *tv, BigRect *rect, long startline, TVFindInRectCallback callback, void *cookie) { struct TVFindInRectMatch match; unsigned long nline; Boolean ret; TVUseCanvas(tv); match.findrect = *rect; match.chunk = NULL; ret = false; if (startline == 0) startline = TVLineForY(tv, rect->top); if (startline == 0) startline = 1; for (nline = startline; nline <= tv->nlines; nline++) { match.line = TVGetLine(tv, nline); if (match.line == NULL) goto draw_done; if (match.line->rect.bottom < rect->top) /* is not in the view at all yet */ continue; if (match.line->rect.top >= rect->bottom) /* is further down than the view, we've done enough lines */ break; /* advance chunk if we need to catch up to this line */ if (match.chunk == NULL || match.chunk->end_line < nline) { if (match.chunk == NULL) { /* start at this line's chunk rather than the first one */ match.chunk = TVGetChunk(tv, match.line->chunk ? match.line->chunk : tv->first_chunk); if (match.chunk == NULL) goto draw_done; } while (match.chunk->end_line < nline || match.chunk->end < match.line->start) { match.chunk = TVGetChunk(tv, match.chunk->next_chunk); if (!match.chunk) goto draw_done; } prep_chunk: match.text_slab = TVGetTextSlab(tv, match.chunk->start); if (match.text_slab == NULL) goto draw_done; match.style = TVGetStyle(tv, match.chunk->style); if (match.style == NULL) goto draw_done; if (!match.style->bitmap_data_size) TVApplyStyle(match.style); } match.rect.top = match.line->rect.top; match.rect.bottom = match.line->rect.bottom; if (match.line->start > match.chunk->start) { match.text_start = match.line->start; match.rect.left = match.line->rect.left + match.line->indent; } else { match.text_start = match.chunk->start; match.rect.left = match.chunk->start_left + match.line->indent; } if (match.line->end < match.chunk->end) { match.text_end = match.line->end; match.rect.right = match.line->rect.right + match.line->indent; } else { match.text_end = match.chunk->end; match.rect.right = match.chunk->end_right + match.line->indent; } ret = true; if (callback(tv, &match, cookie) == false) /* callback requested we stop matching */ goto draw_done; if (match.chunk->end_line == nline && match.chunk->end < match.line->end) { /* more chunks on this line */ match.chunk = TVGetChunk(tv, match.chunk->next_chunk); if (match.chunk != NULL) goto prep_chunk; } } draw_done: TVRevealCanvas(tv); return ret; } short TVCharWidth(struct TVRec *tv, struct TVStyle *style, struct TVLine *line, long x, unsigned char c) { long next_tabstop; short width; if (c == '\r') return 0; if (style->bitmap_data_size) return style->bitmap.bounds.right - style->bitmap.bounds.left; if (c == '\t' && tv->tabstop) { next_tabstop = line->rect.left + (long)tv->tabstop * (((x - line->rect.left) / tv->tabstop) + 1); return next_tabstop - x; } if (!(width = style->width_cache[c])) width = style->width_cache[c] = CharWidth(c); return width; } unsigned long TVLineForY(struct TVRec *tv, long y) { struct TVLine *line; unsigned long lo, hi, mid; if (tv->nlines == 0) return 0; /* find the last line whose top is at or above y */ lo = 1; hi = tv->nlines; while (lo < hi) { mid = (lo + hi + 1) / 2; line = TVGetLine(tv, mid); if (line == NULL) return 0; if (line->rect.top <= y) lo = mid; else hi = mid - 1; } return lo; } unsigned long TVLineForOffset(struct TVRec *tv, unsigned long off) { struct TVLine *line; unsigned long lo, hi, mid; if (tv->nlines == 0) return 0; /* find the last line starting at or before off */ lo = 1; hi = tv->nlines; while (lo < hi) { mid = (lo + hi + 1) / 2; line = TVGetLine(tv, mid); if (line == NULL) return 0; if (line->start <= off) lo = mid; else hi = mid - 1; } return lo; } struct TVChunk * TVLineChunk(struct TVRec *tv, struct TVLine *line) { struct TVChunk *chunk, *tchunk; chunk = TVGetChunk(tv, line->chunk ? line->chunk : tv->first_chunk); /* the hint should be right, but don't trust it */ while (chunk != NULL && chunk->start > line->start && (tchunk = TVGetChunk(tv, chunk->prev_chunk)) != NULL) chunk = tchunk; while (chunk != NULL && chunk->end <= line->start && (tchunk = TVGetChunk(tv, chunk->next_chunk)) != NULL) chunk = tchunk; return chunk; } long TVXForOffset(struct TVRec *tv, struct TVLine *line, unsigned long off) { struct TVChunk *chunk; struct TVStyle *style; struct TVSlab *slab; unsigned long n, end; long x; if (off > line->end) off = line->end; x = line->rect.left; for (chunk = TVLineChunk(tv, line); chunk != NULL; chunk = TVGetChunk(tv, chunk->next_chunk)) { if (chunk->start > off) break; /* same as TVFindInRectLocked, so we measure where it drew */ if (chunk->start >= line->start) x = chunk->start_left; n = (chunk->start > line->start ? chunk->start : line->start); end = (chunk->end < off ? chunk->end : off); if (n < end) { style = TVGetStyle(tv, chunk->style); slab = TVGetTextSlab(tv, chunk->start); if (style == NULL || slab == NULL) break; if (!style->bitmap_data_size) TVApplyStyle(style); for (; n < end; n++) x += TVCharWidth(tv, style, line, x, slab->data[n - slab->base]); } if (chunk->end >= off) break; } return x + line->indent; } long TVOffsetForX(struct TVRec *tv, struct TVLine *line, long x, Boolean caret) { struct TVChunk *chunk; struct TVStyle *style; struct TVSlab *slab; unsigned long n, end; long lx; short width; x -= line->indent; lx = line->rect.left; for (chunk = TVLineChunk(tv, line); chunk != NULL; chunk = TVGetChunk(tv, chunk->next_chunk)) { if (chunk->start > line->end) break; if (chunk->start >= line->start) lx = chunk->start_left; n = (chunk->start > line->start ? chunk->start : line->start); end = (chunk->end < line->end ? chunk->end : line->end); if (n < end) { style = TVGetStyle(tv, chunk->style); slab = TVGetTextSlab(tv, chunk->start); if (style == NULL || slab == NULL) break; if (!style->bitmap_data_size) TVApplyStyle(style); for (; n < end; n++) { width = TVCharWidth(tv, style, line, lx, slab->data[n - slab->base]); if (caret) { if (x < lx + (width / 2)) return n; } else if (x < lx) return -1; else if (x < lx + width) return n; lx += width; } } if (chunk->end >= line->end) break; } return (caret ? line->end : -1); } long TVOffsetForPoint(struct TVRec *tv, long h, long v, Boolean caret) { struct TVLine *line; unsigned long nline; if (tv->nlines == 0) return (caret ? 0 : -1); line = TVGetLine(tv, 1); if (line == NULL || v < line->rect.top) return (caret ? 0 : -1); nline = TVLineForY(tv, v); line = TVGetLine(tv, nline); if (line == NULL) return (caret ? 0 : -1); if (nline == tv->nlines && v >= line->rect.bottom) return (caret ? tv->text_length : -1); return TVOffsetForX(tv, line, h, caret); }