measure line properly

This commit is contained in:
Nishi 2026-04-27 03:14:12 +09:00
commit ca703f24d5
Signed by: nishi
GPG key ID: 27EF69B208EB9343
3 changed files with 53 additions and 17 deletions

View file

@ -1068,10 +1068,10 @@ void MwGetClipboard(MwWidget handle, int clipboard_type) {
MwLLGetClipboard(handle->lowlevel, clipboard_type); MwLLGetClipboard(handle->lowlevel, clipboard_type);
} }
void MwSetUser(MwWidget handle, void* user){ void MwSetUser(MwWidget handle, void* user) {
handle->user = user; handle->user = user;
} }
void* MwGetUser(MwWidget handle){ void* MwGetUser(MwWidget handle) {
return handle->user; return handle->user;
} }

View file

@ -71,19 +71,53 @@ void MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text,
} }
int MwTextWidth(MwWidget handle, MwFLFont ttf, const char* text) { int MwTextWidth(MwWidget handle, MwFLFont ttf, const char* text) {
/* TODO: check newline */ int len = 0;
#ifdef TTF
int st; int st;
int cp;
if(ttf <= MwFLBuildFont(0xff)) ttf = assume_ttf(handle, ttf); char* input = (char*)text;
char* last = input;
if(MwFLTextWidth) #ifndef TTF
if(!MwGetInteger(handle, MwNbitmapFont) && ttf != NULL && (st = MwFLTextWidth(ttf, text)) != -1) return st;
#else
(void)handle; (void)handle;
(void)ttf; (void)ttf;
#endif #endif
return MwUTF8Length(text) * FontWidth;
#ifdef TTF
if(ttf <= MwFLBuildFont(0xff)) ttf = assume_ttf(handle, ttf);
#endif
while(*input != 0) {
input += MwUTF8ToUTF32(input, &cp);
if(*input == 0 || cp == '\n') {
char* line = malloc(input - last + 1);
int i;
memcpy(line, last, input - last);
line[input - last] = 0;
for(i = 0; line[i] != 0; i++) {
if(line[i] == '\r' || line[i] == '\n') {
line[i] = 0;
break;
}
}
#ifdef TTF
if(MwFLTextWidth && !MwGetInteger(handle, MwNbitmapFont) && ttf != NULL && (st = MwFLTextWidth(ttf, line)) != -1) {
} else
#endif
st = MwUTF8Length(line) * FontWidth;
if(st > len) len = st;
free(line);
last = input + 1;
}
}
return len;
} }
int MwTextHeight(MwWidget handle, MwFLFont ttf, const char* text) { int MwTextHeight(MwWidget handle, MwFLFont ttf, const char* text) {

View file

@ -193,14 +193,13 @@ static void frame_draw(MwWidget handle) {
char* str; char* str;
int l = (j == 0 ? MwGetInteger(handle->parent, MwNleftPadding) : 0); int l = (j == 0 ? MwGetInteger(handle->parent, MwNleftPadding) : 0);
MwPoint p2 = p; MwPoint p2 = p;
p2.y += (MwTextHeight(handle, Font, "M") + Padding) / 2; p2.y += (MwTextHeight(handle, Font, "M") + Padding) / 2;
if(t == NULL) t = ""; if(t == NULL) t = "";
str = MwStringDuplicate(t); str = MwStringDuplicate(t);
if(MwUTF8Length(str) > (get_col_width(lb, j) - l) / MwTextWidth(handle, Font, "M")) { if(MwUTF8Length(str) > (get_col_width(lb, j) - l) / MwTextWidth(handle, Font, "M")) {
int ind = (get_col_width(lb, j) - l) / MwTextWidth(handle, Font, "M"); int ind = (get_col_width(lb, j) - l) / MwTextWidth(handle, Font, "M") - 3;
int seek = 0; int seek = 0;
int l = 0; int l = 0;
@ -219,18 +218,21 @@ static void frame_draw(MwWidget handle) {
if(j == (arrlen(lb->list[i].name) - 1)) p.x -= MwDefaultBorderWidth(handle); if(j == (arrlen(lb->list[i].name) - 1)) p.x -= MwDefaultBorderWidth(handle);
if(arrlen(lb->alignment) <= j || lb->alignment[j] == MwALIGNMENT_BEGINNING) { if(arrlen(lb->alignment) <= j || lb->alignment[j] == MwALIGNMENT_BEGINNING) {
p.x += 4; p.x += 4;
p2.x = p.x;
MwDrawText(handle, Font, &p2, str, MwALIGNMENT_BEGINNING, selected ? base2 : text2); MwDrawText(handle, Font, &p2, str, MwALIGNMENT_BEGINNING, selected ? base2 : text2);
p.x -= 4; p.x -= 4;
p.x += get_col_width(lb, j); p.x += get_col_width(lb, j);
} else if(lb->alignment[j] == MwALIGNMENT_CENTER) { } else if(lb->alignment[j] == MwALIGNMENT_CENTER) {
p.x += (get_col_width(lb, j) - l) / 2; p.x += (get_col_width(lb, j) - l) / 2;
p2.x = p.x;
MwDrawText(handle, Font, &p2, str, MwALIGNMENT_CENTER, selected ? base2 : text2); MwDrawText(handle, Font, &p2, str, MwALIGNMENT_CENTER, selected ? base2 : text2);
p.x += (get_col_width(lb, j) - l) / 2; p.x += (get_col_width(lb, j) - l) / 2;
p.x += l; p.x += l;
} else if(lb->alignment[j] == MwALIGNMENT_END) { } else if(lb->alignment[j] == MwALIGNMENT_END) {
p.x += get_col_width(lb, j); p.x += get_col_width(lb, j);
p.x -= 4; p.x -= 4;
p2.x = p.x;
MwDrawText(handle, Font, &p2, str, MwALIGNMENT_END, selected ? base2 : text2); MwDrawText(handle, Font, &p2, str, MwALIGNMENT_END, selected ? base2 : text2);
p.x += 4; p.x += 4;
} }