update how newlines are handled
All checks were successful
pyrite-dev/milsko/pipeline/head This commit looks good

This commit is contained in:
Nishi 2026-04-27 04:04:04 +09:00
commit 21e2f2d599
Signed by: nishi
GPG key ID: 27EF69B208EB9343
4 changed files with 63 additions and 40 deletions

View file

@ -266,7 +266,7 @@ int main() {
add(f);
f = frame("Label", -PaddingContent, -PaddingContent, MwLabelClass,
MwNtext, "Epic text\nI am newline too!",
MwNtext, "Epic text\nNewline can be used too!",
NULL);
add(f);

View file

@ -55,12 +55,6 @@ static int ft2_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const c
if(l <= 0) break;
text += l;
if(c == '\n') {
x = 0;
y += ttf->face->height * ttf->px / ttf->face->units_per_EM;
continue;
}
ft_table.FT_Load_Char(ttf->face, c, FT_LOAD_RENDER);
bmp = &ttf->face->glyph->bitmap;
@ -86,11 +80,9 @@ static int ft2_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const c
old_text = text;
text += MwUTF8ToUTF32(text, &c2);
if(c2 != '\n') {
ft_table.FT_Get_Kerning(ttf->face, c, c2, FT_KERNING_DEFAULT, &vec);
ft_table.FT_Get_Kerning(ttf->face, c, c2, FT_KERNING_DEFAULT, &vec);
x += vec.x >> 6;
}
x += vec.x >> 6;
text = old_text;
}

View file

@ -33,11 +33,6 @@ static int stbtt_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const
const char* old_text;
text += MwUTF8ToUTF32(text, &c);
if(c == '\n') {
x = 0;
y += ceil((ttf->ascent - ttf->descent) * ttf->scale);
continue;
}
stbtt_GetCodepointHMetrics(&ttf->font, c, &ax, &lsb);
@ -69,11 +64,9 @@ static int stbtt_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const
old_text = text;
text += MwUTF8ToUTF32(text, &c2);
if(c2 != '\n') {
kern = stbtt_GetCodepointKernAdvance(&ttf->font, c, c2);
kern = stbtt_GetCodepointKernAdvance(&ttf->font, c, c2);
x += ceil(kern * ttf->scale);
}
x += ceil(kern * ttf->scale);
text = old_text;

View file

@ -15,6 +15,21 @@ void (*MwFLFontFree)(void* handle) = NULL;
static void bitmap_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, MwLLColor color);
static int count_nl(const char* text) {
int c = 1;
int i = 0;
while(text[i] != 0) {
int out;
int l = MwUTF8ToUTF32(text + i, &out);
if(l == 0) break;
i += l;
if(out == '\n') c++;
}
return c;
}
static int is_bold(MwWidget handle, MwFLFont ttf) {
size_t u = (size_t)ttf;
int s;
@ -50,24 +65,56 @@ static MwFLFont assume_ttf(MwWidget handle, MwFLFont ttf) {
#endif
void MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, int align, MwLLColor color) {
MwPoint p = *point;
MwPoint p = *point;
char* input = (char*)text;
char* last = input;
int cp;
if(strlen(text) == 0) return;
#ifdef TTF
if(ttf <= MwFLBuildFont(0xff)) ttf = assume_ttf(handle, ttf);
#endif
if(align == MwALIGNMENT_CENTER) {
p.x -= MwTextWidth(handle, ttf, text) / 2;
} else if(align == MwALIGNMENT_END) {
p.x -= MwTextWidth(handle, ttf, text);
}
p.y -= MwTextHeight(handle, ttf, text) / 2 - (MwTextHeight(handle, ttf, text) / count_nl(text)) / 2;
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;
}
}
p.x = point->x;
if(align == MwALIGNMENT_CENTER) {
p.x -= MwTextWidth(handle, ttf, line) / 2;
} else if(align == MwALIGNMENT_END) {
p.x -= MwTextWidth(handle, ttf, line);
}
#ifdef TTF
if(MwFLDrawText)
if(MwGetInteger(handle, MwNbitmapFont) || ttf == NULL || MwFLDrawText(handle, ttf, &p, text, color))
if(MwFLDrawText)
if(MwGetInteger(handle, MwNbitmapFont) || ttf == NULL || MwFLDrawText(handle, ttf, &p, line, color))
#endif
bitmap_MwDrawText(handle, &p, text, is_bold(handle, ttf), color);
bitmap_MwDrawText(handle, &p, line, is_bold(handle, ttf), color);
p.y += MwTextHeight(handle, ttf, line);
free(line);
last = input;
}
}
}
int MwTextWidth(MwWidget handle, MwFLFont ttf, const char* text) {
@ -113,7 +160,7 @@ int MwTextWidth(MwWidget handle, MwFLFont ttf, const char* text) {
free(line);
last = input + 1;
last = input;
}
}
@ -121,8 +168,7 @@ int MwTextWidth(MwWidget handle, MwFLFont ttf, const char* text) {
}
int MwTextHeight(MwWidget handle, MwFLFont ttf, const char* text) {
int c = 1;
int i = 0;
int c = count_nl(text);
#ifdef TTF
int st;
#endif
@ -135,14 +181,6 @@ int MwTextHeight(MwWidget handle, MwFLFont ttf, const char* text) {
(void)ttf;
#endif
while(text[i] != 0) {
int out;
int l = MwUTF8ToUTF32(text + i, &out);
if(l == 0) break;
i += l;
if(out == '\n') c++;
}
#ifdef TTF
if(MwFLTextHeight)
if(!MwGetInteger(handle, MwNbitmapFont) && ttf != NULL && (st = MwFLTextHeight(ttf, c)) != -1) return st;