jcs
/textview
/amendments
/3
textview: Initial work, painting/styling work, text wrap kinda works
jcs made amendment 3 over 2 years ago
--- textview.c Wed Dec 6 16:31:05 2023
+++ textview.c Wed Dec 6 16:31:05 2023
@@ -0,0 +1,250 @@
+#include <stdio.h>
+#include <stddef.h>
+#include <string.h>
+#include "textview.h"
+#include "util.h"
+
+#define CHUNK_ALLOC_SIZE (16 * 1024)
+#define IS_FIRST_EMPTY_CHUNK(tv) ((tv)->cur_chunk->style.size == 0)
+
+unsigned short _TVFontHeight(short font, short size);
+struct TVChunk * _TVCreateChunk(struct TVRec *tv, unsigned long length);
+void _TVClearChunkRect(struct TVRec *tv, struct TVChunk *chunk);
+void _TVDrawText(struct TVRec *tv, struct TVChunk *chunk,
+ unsigned long off, bool append);
+
+struct TVRec *
+TVNew(Rect *destRect, Rect *viewRect)
+{
+ struct TVRec *tv;
+ unsigned long size;
+
+ size = sizeof(struct TVRec) + sizeof(struct TVStyle);
+ tv = malloc(CHUNK_ALLOC_SIZE);
+ if (tv == NULL)
+ panic("malloc(%ul) failed", (unsigned long)CHUNK_ALLOC_SIZE);
+
+ memset(tv, 0, sizeof(struct TVRec));
+ tv->destRect = *destRect;
+ tv->viewRect = *viewRect;
+
+ /* create an initial blank chunk */
+ tv->chunks = (Ptr)tv + sizeof(struct TVRec);
+ tv->cur_chunk = (struct TVChunk *)tv->chunks;
+ memset(tv->cur_chunk, 0, sizeof(struct TVChunk));
+
+ tv->cur_chunk->free_length = CHUNK_ALLOC_SIZE - size;
+ tv->cur_chunk->line = 1;
+ tv->cur_chunk->rect.top = tv->cur_chunk->rect.bottom = tv->destRect.top;
+ tv->cur_chunk->rect.left = tv->cur_chunk->rect.right = tv->destRect.left;
+ tv->pen.v = tv->destRect.top;
+ tv->pen.h = tv->destRect.left;
+
+ return tv;
+}
+
+unsigned short
+_TVFontHeight(short font, short size)
+{
+ FMInput f_in;
+ FMOutput *f_out;
+
+ if (font == -1)
+ font = thePort->txFont;
+ if (size == -1)
+ size = thePort->txSize;
+
+ f_in.family = font;
+ f_in.size = size;
+ f_in.face = 0;
+ f_in.needBits = false;
+ f_in.device = 0;
+ f_in.numer.h = f_in.numer.v = 1;
+ f_in.denom.h = f_in.denom.v = 1;
+
+ f_out = FMSwapFont(&f_in);
+
+ return (((f_out->leading + f_out->ascent + f_out->descent) *
+ f_out->numer.v) / f_out->denom.v);
+}
+
+struct TVChunk *
+_TVCreateChunk(struct TVRec *tv, unsigned long length)
+{
+ struct TVChunk *chunk;
+ unsigned long addr;
+
+ if (IS_FIRST_EMPTY_CHUNK(tv))
+ /* new TV with empty chunk */
+ return tv->cur_chunk;
+
+ if (length + sizeof(struct TVChunk) > tv->cur_chunk->free_length) {
+ /* allocate new buffer */
+ chunk = malloc(CHUNK_ALLOC_SIZE);
+ if (chunk == NULL)
+ /* TODO: try to allocate a smaller chunk */
+ panic("malloc(%ld) failed", (unsigned long)CHUNK_ALLOC_SIZE);
+ } else {
+ /* create new chunk in existing alloc */
+ addr = (unsigned long)tv->cur_chunk + sizeof(struct TVChunk) +
+ tv->cur_chunk->length;
+ if (addr % 2 != 0)
+ addr++;
+ chunk = (struct TVChunk *)addr;
+ }
+
+ memset(chunk, 0, sizeof(struct TVChunk));
+ chunk->free_length = CHUNK_ALLOC_SIZE - sizeof(struct TVChunk);
+ chunk->line = tv->cur_chunk->line;
+ chunk->rect.top = tv->cur_chunk->rect.top;
+ chunk->rect.left = chunk->rect.right = tv->cur_chunk->rect.right;
+ chunk->rect.bottom = tv->cur_chunk->rect.bottom;
+ return chunk;
+}
+
+void
+TVAppend(struct TVRec *tv, struct TVStyle *style, char *text,
+ unsigned long length)
+{
+ struct TVChunk *chunk = NULL, *prev_chunk, *tchunk;
+ unsigned long n, free_length, line_right;
+ Point p;
+ short width, height;
+ char c, *last_space = NULL;
+ bool new_chunk, new_line;
+
+ height = _TVFontHeight(style->font, style->size);
+ line_right = tv->cur_chunk->rect.right;
+
+ for (n = 0; n < length; n++) {
+ c = text[n];
+ new_chunk = false;
+ new_line = false;
+
+ if (c == '\r') {
+ new_chunk = true;
+ new_line = true;
+ } else {
+ width = TextWidth(text, n, 1);
+
+ if (width + line_right > tv->destRect.right) {
+ new_chunk = true;
+ new_line = true;
+
+ for (n =
+
+ /* TODO: drag preceding chunks on this line down, until a space */
+ } else if (memcmp(style, &tv->cur_chunk->style,
+ sizeof(struct TVStyle)) != 0) {
+ new_chunk = true;
+ }
+ }
+
+ if (new_chunk) {
+ chunk = _TVCreateChunk(tv, length);
+ chunk->height = height;
+ chunk->style = *style;
+
+ if (tv->cur_chunk != chunk) {
+ if (n > 0)
+ _TVDrawText(tv, tv->cur_chunk, 0, false);
+
+ prev_chunk = tv->cur_chunk;
+ tv->cur_chunk = chunk;
+ tv->cur_chunk->prev_chunk = prev_chunk;
+ prev_chunk->next_chunk = tv->cur_chunk;
+
+ if (tv->cur_chunk->height > prev_chunk->height) {
+ /* all chunks on this line must grow */
+ for (tchunk = prev_chunk; tchunk != NULL;
+ tchunk = tchunk->prev_chunk) {
+ if (tchunk->line != tv->cur_chunk->line)
+ break;
+
+ _TVClearChunkRect(tv, tchunk);
+
+ tchunk->height = tv->cur_chunk->height;
+ _TVDrawText(tv, tchunk, 0, false);
+ }
+ }
+ }
+ }
+
+ if (new_line) {
+ tv->cur_chunk->line++;
+ tv->cur_chunk->rect.top = tv->cur_chunk->rect.bottom =
+ tv->cur_chunk->prev_chunk->rect.bottom;
+ tv->cur_chunk->rect.left = tv->cur_chunk->rect.right =
+ tv->destRect.left;
+ line_right = tv->cur_chunk->rect.right;
+ }
+
+ if (c == '\r') {
+ /*
+ * TODO: remember that this was a hard line break and not
+ * just a wrapped line
+ */
+
+ /* next line will need to know how tall our empty space was */
+ if (tv->cur_chunk->length == 0)
+ tv->cur_chunk->rect.bottom = tv->cur_chunk->rect.top +
+ height;
+ } else {
+ tv->cur_chunk->text[tv->cur_chunk->length++] = c;
+ tv->cur_chunk->free_length--;
+ line_right += width;
+ }
+ }
+
+ if (tv->cur_chunk->length)
+ _TVDrawText(tv, tv->cur_chunk, 0, false);
+}
+
+void
+_TVDrawText(struct TVRec *tv, struct TVChunk *chunk, unsigned long off,
+ bool append)
+{
+ Point p;
+ short l, b;
+
+ if (off == 0)
+ l = chunk->rect.left;
+ else if (append)
+ l = chunk->rect.right;
+ else
+ panic("need to implement redrawing a partial string");
+
+ b = chunk->rect.top + chunk->height - 6;
+
+ MoveTo(l, b);
+ TextFont(chunk->style.font);
+ TextFace(chunk->style.style);
+ TextSize(chunk->style.size);
+ DrawText(chunk->text, off, chunk->length - off);
+
+ GetPen(&p);
+ chunk->rect.right = p.h;
+ chunk->rect.bottom = p.v;
+#if 1
+ FrameRect(&chunk->rect);
+#endif
+}
+
+void
+_TVClearChunkRect(struct TVRec *tv, struct TVChunk *chunk)
+{
+ EraseRect(&chunk->rect);
+}
+
+void
+TVUpdate(Rect *rect, struct TVRec *tv)
+{
+ struct TVChunk *chunk;
+
+ /* TODO: constrain to rect */
+
+ for (chunk = (struct TVChunk *)tv->chunks; chunk != NULL;
+ chunk = chunk->next_chunk) {
+ _TVDrawText(tv, chunk, 0, false);
+ }
+}
--- textview.h Wed Dec 6 15:35:33 2023
+++ textview.h Wed Dec 6 15:35:33 2023
@@ -0,0 +1,30 @@
+struct TVStyle {
+ short font;
+ unsigned char style;
+ unsigned char size;
+};
+
+struct TVChunk {
+ unsigned long length;
+ struct TVChunk *prev_chunk;
+ struct TVChunk *next_chunk;
+ unsigned long free_length;
+ unsigned long line;
+ struct TVStyle style;
+ short height;
+ Rect rect;
+ char text[];
+};
+
+struct TVRec {
+ Rect destRect;
+ Rect viewRect;
+ Point pen;
+ struct TVChunk *cur_chunk;
+ Ptr chunks;
+};
+
+struct TVRec * TVNew(Rect *destRect, Rect *viewRect);
+void TVAppend(struct TVRec *tv, struct TVStyle *style, char *text,
+ unsigned long length);
+void TVUpdate(Rect *rect, struct TVRec *tv);