jcs
/textview
/amendments
/57
*: Add support for right and center alignment
Also avoid some errant setting of grew which were causing more
EraseRect doings than necessary.
jcs made amendment 57 about 1 year ago
--- textview.c Thu Nov 21 20:11:53 2024
+++ textview.c Fri Dec 13 17:06:12 2024
@@ -21,10 +21,12 @@
//#define USE_CANVAS 1
+/* NOTE: update TVFindStyle when additional fields are added */
#define ARE_STYLES_EQUAL(one, two) \
((one)->font == (two)->font && \
(one)->style == (two)->style && \
(one)->size == (two)->size && \
+ (one)->align == (two)->align && \
(one)->tag == (two)->tag && \
(one)->bitmap_data_size == (two)->bitmap_data_size)
@@ -1222,9 +1224,11 @@ create_style:
SET_SLABLOC_OFFSET(tstyle->loc, slab->pos);
/* only copy over the important fields, rest will stay 0 */
+ /* NOTE: keep this in sync with ARE_STYLES_EQUAL */
tstyle->font = style->font;
tstyle->style = style->style;
tstyle->size = style->size;
+ tstyle->align = style->align;
tstyle->tag = style->tag;
if (style->bitmap_data_size) {
tstyle->font_info = style->font_info;
@@ -1543,6 +1547,15 @@ reload_chunk:
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;
@@ -1575,17 +1588,22 @@ reload_chunk:
chunk->end_right += width;
if (style->font_info.descent > line->baseline) {
- if (line->baseline)
+ if (line->baseline && line->start != line->end)
line->grew = true;
line->baseline = style->font_info.descent;
height = style->font_info.ascent + style->font_info.descent;
}
- if (style->font_info.leading > line->leading) {
+ if (style->font_info.leading > line->leading &&
+ line->start != line->end) {
+ if (line->leading)
+ line->grew = true;
line->leading = style->font_info.leading;
- line->grew = true;
}
- if (line->rect.bottom - line->rect.top < height) {
- line->grew = true;
+ if (line->rect.bottom - line->rect.top < height &&
+ line->start != line->end) {
+ if (line->rect.bottom != line->rect.top &&
+ line->rect.bottom != 0)
+ line->grew = true;
line->rect.bottom = line->rect.top + height;
}
}
@@ -1594,6 +1612,14 @@ reload_chunk:
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;
}
done_chunks:
@@ -1626,11 +1652,11 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch
if (match->text_end == match->text_start)
return true;
- if (match->line->rect.bottom < -(tv->scrolled.top))
+ if (match->rect.bottom < -(tv->scrolled.top))
/* no sense drawing what we can't see yet */
return true;
- if (match->line->rect.top >= -(tv->scrolled.top) + tv->frame.bottom)
+ if (match->rect.top >= -(tv->scrolled.top) + tv->frame.bottom)
/* further down than the scrolled view, we've drawn enough lines */
return false;
@@ -1641,9 +1667,23 @@ TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch
r.bottom = tv->scrolled.top + match->line->rect.bottom;
EraseRect(&r);
match->line->grew = false;
+ } else if (match->style->align == TV_ALIGN_CENTER) {
+ /* erase from this chunk to the right of the view */
+ r.left = tv->scrolled.left + match->rect.left;
+ r.top = tv->scrolled.top + match->line->rect.top;
+ r.right = tv->scrolled.left + tv->view.right - tv->padding.right;
+ r.bottom = tv->scrolled.top + match->line->rect.bottom;
+ EraseRect(&r);
+ } else if (match->style->align == TV_ALIGN_RIGHT) {
+ /* erase from the right of the view to the right of the view */
+ r.left = tv->scrolled.left + match->rect.left;
+ r.top = tv->scrolled.top + match->line->rect.top;
+ r.right = tv->scrolled.left + tv->view.right - tv->padding.right;
+ r.bottom = tv->scrolled.top + match->line->rect.bottom;
+ EraseRect(&r);
}
- MoveTo(match->rect.left, tv->scrolled.top + match->line->rect.bottom -
+ MoveTo(match->rect.left, tv->scrolled.top + match->rect.bottom -
match->line->baseline);
if (match->style->bitmap_data_size) {
r = match->style->bitmap.bounds;
@@ -1777,18 +1817,18 @@ prep_chunk:
if (match.line->start > match.chunk->start) {
match.text_start = match.line->start;
- match.rect.left = match.line->rect.left;
+ 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.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.rect.right = match.line->rect.right + match.line->indent;
} else {
match.text_end = match.chunk->end;
- match.rect.right = match.chunk->end_right;
+ match.rect.right = match.chunk->end_right + match.line->indent;
}
ret = true;
--- textview.h Wed Nov 20 17:07:28 2024
+++ textview.h Fri Dec 13 13:15:54 2024
@@ -46,10 +46,17 @@ typedef unsigned long TVSlabLocator;
#define SET_SLABLOC_OFFSET(loc, x) \
{ (loc) = ((loc) & 0xffff0000) | ((x) & 0x0000ffff); }
+enum {
+ TV_ALIGN_LEFT,
+ TV_ALIGN_CENTER,
+ TV_ALIGN_RIGHT
+};
+
struct TVStyle {
unsigned char font;
unsigned char style;
unsigned char size;
+ unsigned char align;
unsigned long tag;
/* these are private and ignored when fed into TVAppend() */
@@ -79,6 +86,7 @@ struct TVLine {
unsigned long start;
unsigned long end;
BigRect rect;
+ unsigned long indent;
unsigned long nline;
unsigned short leading;
unsigned char baseline;