jcs
/textview
/amendments
/4
textview: More progress, wrapping and adjusting baselines properly
jcs made amendment 4 over 2 years ago
--- textview.c Wed Dec 6 16:31:05 2023
+++ textview.c Mon Dec 11 11:15:29 2023
@@ -7,9 +7,11 @@
#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);
+struct TVChunk * _TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk,
+ unsigned long length);
+void TVApplyStyle(struct TVStyle *style);
void _TVDrawText(struct TVRec *tv, struct TVChunk *chunk,
unsigned long off, bool append);
@@ -43,31 +45,6 @@ TVNew(Rect *destRect, Rect *viewRect)
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)
{
@@ -102,24 +79,80 @@ _TVCreateChunk(struct TVRec *tv, unsigned long length)
return chunk;
}
+struct TVChunk *
+_TVSplitChunk(struct TVRec *tv, struct TVChunk *chunk, unsigned long length)
+{
+ struct TVChunk *new_chunk, *prev_chunk, *next_chunk;
+ short width;
+
+ /*
+ * This leaks some space by not putting the new chunk right after
+ * chunk->text[pos], but it avoids having to create a temporary buffer
+ * to copy chunk->text[pos .. 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;
+
+ new_chunk->prev_chunk = chunk;
+ if (chunk->next_chunk) {
+ chunk->next_chunk->prev_chunk = new_chunk;
+ chunk->next_chunk = new_chunk;
+ }
+
+ _TVClearChunkRect(tv, chunk);
+
+ new_chunk->length = chunk->length - length;
+ memcpy(new_chunk->text, chunk->text + length,
+ chunk->length - length);
+ chunk->length = length;
+
+ TVApplyStyle(&chunk->style);
+ width = TextWidth(chunk->text, 0, chunk->length);
+ chunk->rect.right -= width;
+ _TVDrawText(tv, chunk, 0, false);
+
+ 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;
+
+ /* not drawn */
+
+ return new_chunk;
+}
+
void
+TVApplyStyle(struct TVStyle *style)
+{
+ TextFont(style->font);
+ TextFace(style->style);
+ TextSize(style->size);
+}
+
+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;
+ long j, last_break, carry_chars;
Point p;
- short width, height;
- char c, *last_space = NULL;
+ short width;
+ char c;
bool new_chunk, new_line;
+
+ TVApplyStyle(style);
+ GetFontInfo(&style->font_info);
- height = _TVFontHeight(style->font, style->size);
line_right = tv->cur_chunk->rect.right;
+ carry_chars = 0;
for (n = 0; n < length; n++) {
- c = text[n];
- new_chunk = false;
- new_line = false;
+ c = text[n];
+ new_chunk = false;
+ new_line = false;
if (c == '\r') {
new_chunk = true;
@@ -127,13 +160,32 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char
} else {
width = TextWidth(text, n, 1);
- if (width + line_right > tv->destRect.right) {
+ if (width + line_right > tv->destRect.right && c != ' ') {
new_chunk = true;
new_line = true;
- for (n =
+ /* walk back to last space and break after it */
+ last_break = 0;
+ for (j = tv->cur_chunk->length; j > 0; j--) {
+ if (tv->cur_chunk->text[j - 1] != ' ')
+ continue;
+
+ tv->cur_chunk = _TVSplitChunk(tv, tv->cur_chunk, j);
+ line_right -= (tv->cur_chunk->prev_chunk->rect.right -
+ tv->cur_chunk->prev_chunk->rect.left);
+ new_chunk = false;
+ break;
+ }
- /* TODO: drag preceding chunks on this line down, until a space */
+ if (new_chunk) {
+ /*
+ * TODO: if no space in that chunk, keep checking
+ * previous chunks in this line
+ */
+ panic("walk back to previous chunk to find space");
+ }
+
+ TVApplyStyle(&tv->cur_chunk->style);
} else if (memcmp(style, &tv->cur_chunk->style,
sizeof(struct TVStyle)) != 0) {
new_chunk = true;
@@ -141,9 +193,10 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char
}
if (new_chunk) {
- chunk = _TVCreateChunk(tv, length);
- chunk->height = height;
+ chunk = _TVCreateChunk(tv, length - n);
+ chunk->baseline = style->font_info.ascent;
chunk->style = *style;
+ GetFontInfo(&chunk->style.font_info);
if (tv->cur_chunk != chunk) {
if (n > 0)
@@ -154,7 +207,7 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char
tv->cur_chunk->prev_chunk = prev_chunk;
prev_chunk->next_chunk = tv->cur_chunk;
- if (tv->cur_chunk->height > prev_chunk->height) {
+ if (tv->cur_chunk->baseline > prev_chunk->baseline) {
/* all chunks on this line must grow */
for (tchunk = prev_chunk; tchunk != NULL;
tchunk = tchunk->prev_chunk) {
@@ -163,20 +216,24 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char
_TVClearChunkRect(tv, tchunk);
- tchunk->height = tv->cur_chunk->height;
+ tchunk->baseline = tv->cur_chunk->baseline;
_TVDrawText(tv, tchunk, 0, false);
}
- }
+ } else if (tv->cur_chunk->baseline < prev_chunk->baseline)
+ tv->cur_chunk->baseline =
+ tv->cur_chunk->prev_chunk->baseline;
+
+ TVApplyStyle(&tv->cur_chunk->style);
}
}
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;
+ tv->cur_chunk->prev_chunk->rect.bottom +
+ tv->cur_chunk->prev_chunk->style.font_info.leading;
+ tv->cur_chunk->rect.left = tv->destRect.left;
+ tv->cur_chunk->rect.right = line_right;
}
if (c == '\r') {
@@ -187,12 +244,13 @@ TVAppend(struct TVRec *tv, struct TVStyle *style, char
/* 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;
+ tv->cur_chunk->rect.bottom = tv->cur_chunk->baseline +
+ tv->cur_chunk->style.font_info.descent;
} else {
tv->cur_chunk->text[tv->cur_chunk->length++] = c;
tv->cur_chunk->free_length--;
line_right += width;
+//_TVDrawText(tv, tv->cur_chunk, 0, false);
}
}
@@ -214,17 +272,15 @@ _TVDrawText(struct TVRec *tv, struct TVChunk *chunk, u
else
panic("need to implement redrawing a partial string");
- b = chunk->rect.top + chunk->height - 6;
+ b = chunk->rect.top + chunk->baseline;
MoveTo(l, b);
- TextFont(chunk->style.font);
- TextFace(chunk->style.style);
- TextSize(chunk->style.size);
+ TVApplyStyle(&chunk->style);
DrawText(chunk->text, off, chunk->length - off);
GetPen(&p);
chunk->rect.right = p.h;
- chunk->rect.bottom = p.v;
+ chunk->rect.bottom = p.v + chunk->style.font_info.descent;
#if 1
FrameRect(&chunk->rect);
#endif
--- textview.h Wed Dec 6 15:35:33 2023
+++ textview.h Fri Dec 8 19:57:24 2023
@@ -1,4 +1,5 @@
struct TVStyle {
+ struct FontInfo font_info;
short font;
unsigned char style;
unsigned char size;
@@ -11,7 +12,7 @@ struct TVChunk {
unsigned long free_length;
unsigned long line;
struct TVStyle style;
- short height;
+ unsigned long baseline;
Rect rect;
char text[];
};