diff options
-rw-r--r-- | main.c | 51 |
1 files changed, 37 insertions, 14 deletions
@@ -50,8 +50,13 @@ #define GFX_PRONOUNS_MARGIN 1 #define GFX_PRONOUNS_FMT "<%S>" -#define PFP_LINES 3 -#define PFP_COLS 7 +#define GFX_PFP_MIN_LINES 3 +#define GFX_PFP_MIN_COLS 7 +#define GFX_PFP_MAX_LINES 8 +#define GFX_PFP_MAX_COLS 16 + +#define MIN(x,y) ((x)<(y)?(x):(y)) +#define MAX(x,y) ((x)>(y)?(x):(y)) /* colors */ @@ -105,7 +110,18 @@ int fast_utf8_width(Str s) { int n = 0; for (int i = 0; i < s.n; i++) { if (s.s[i] & 0x80) { - i += stdc_leading_ones((unsigned)i) - 1; + i += stdc_leading_ones((unsigned char)s.s[i]) - 1; + } + n++; + } + return n; +} + +int utf8_cp_idx(Str s, int dest) { + int n = 0; + for (int i = 0; i < s.n && n < dest; i++) { + if (s.s[i] & 0x80) { + i += stdc_leading_ones((unsigned char)s.s[i]) - 1; } n++; } @@ -222,8 +238,8 @@ void pfp_load(Pfp *pfp, Str src, Arena *a) { if (w > cols) cols = w; lines++; } - pfp->lines = lines > PFP_LINES ? lines : PFP_LINES; - pfp->cols = cols > PFP_COLS ? cols : PFP_COLS; + pfp->lines = lines > GFX_PFP_MIN_LINES ? lines : GFX_PFP_MIN_LINES; + pfp->cols = cols > GFX_PFP_MIN_COLS ? cols : GFX_PFP_MIN_COLS; pfp->line = new_arr(a, Str, pfp->lines); lines = 0; for (Str s = src, l = {0}; next_line(&s, &l);) { @@ -481,16 +497,17 @@ void gfx_predraw_post(GfxPost *post) { post->has_mention = !regexec(&re_mention, post->text.s, 0, NULL, 0); } -int gfx_post_height(GfxPost *post) { - int n = 0; - if (opt.see.pfp && post->src->user->pfp.lines > post->lines + 2) n = post->src->user->pfp.lines - post->lines - 2; - return post->lines + 2 + GFX_TEXT_MARGIN_Y * 2 + n; -} - int gfx_post_text_height(GfxPost *post) { return post->lines + 2 + GFX_TEXT_MARGIN_Y * 2; } +int gfx_post_height(GfxPost *post) { + int base = gfx_post_text_height(post); + int l = MIN(post->src->user->pfp.lines, GFX_PFP_MAX_LINES); + if (opt.see.pfp && l > base) return l; + return base; +} + void gfx_draw_post(GfxPost *post, int y, int x, int width, Arena *scratch) { int height = gfx_post_text_height(post); int total_height = gfx_post_height(post); @@ -529,11 +546,17 @@ void gfx_draw_post(GfxPost *post, int y, int x, int width, Arena *scratch) { if (opt.see.pfp) { color_set(self ? CPAIR_PFP_SELF : CPAIR_PFP, 0); Pfp *pfp = &u->pfp; + int h = MIN(pfp->lines, MAX(post->lines, GFX_PFP_MAX_LINES)); int pfptop = y + GFX_TEXT_MARGIN_Y; - if (height > pfp->lines) pfptop += (height - pfp->lines) >> 1; + if (height > h) pfptop += (height - h) >> 1; int pfpleft = left - pfp->cols - GFX_PFP_MARGIN; - for (int i = 0; i < pfp->lines; i++) { - mvaddnstr(pfptop + i, pfpleft, pfp->line[i].s, pfp->line[i].n); + for (int i = 0; i < h; i++) { + if (pfp->line[i].n > GFX_PFP_MAX_COLS) { + int w = utf8_cp_idx(pfp->line[i], GFX_PFP_MAX_COLS); + mvaddnstr(pfptop + i, pfpleft, pfp->line[i].s, w); + } else { + mvaddnstr(pfptop + i, pfpleft, pfp->line[i].s, pfp->line[i].n); + } } } |