update how newlines are handled
All checks were successful
pyrite-dev/milsko/pipeline/head This commit looks good
All checks were successful
pyrite-dev/milsko/pipeline/head This commit looks good
This commit is contained in:
parent
2ae01d3902
commit
21e2f2d599
4 changed files with 63 additions and 40 deletions
|
|
@ -266,7 +266,7 @@ int main() {
|
||||||
add(f);
|
add(f);
|
||||||
|
|
||||||
f = frame("Label", -PaddingContent, -PaddingContent, MwLabelClass,
|
f = frame("Label", -PaddingContent, -PaddingContent, MwLabelClass,
|
||||||
MwNtext, "Epic text\nI am newline too!",
|
MwNtext, "Epic text\nNewline can be used too!",
|
||||||
NULL);
|
NULL);
|
||||||
add(f);
|
add(f);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,12 +55,6 @@ static int ft2_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const c
|
||||||
if(l <= 0) break;
|
if(l <= 0) break;
|
||||||
text += l;
|
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);
|
ft_table.FT_Load_Char(ttf->face, c, FT_LOAD_RENDER);
|
||||||
bmp = &ttf->face->glyph->bitmap;
|
bmp = &ttf->face->glyph->bitmap;
|
||||||
|
|
||||||
|
|
@ -86,11 +80,9 @@ static int ft2_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const c
|
||||||
old_text = text;
|
old_text = text;
|
||||||
text += MwUTF8ToUTF32(text, &c2);
|
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;
|
text = old_text;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,11 +33,6 @@ static int stbtt_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const
|
||||||
const char* old_text;
|
const char* old_text;
|
||||||
|
|
||||||
text += MwUTF8ToUTF32(text, &c);
|
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);
|
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;
|
old_text = text;
|
||||||
text += MwUTF8ToUTF32(text, &c2);
|
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;
|
text = old_text;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 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) {
|
static int is_bold(MwWidget handle, MwFLFont ttf) {
|
||||||
size_t u = (size_t)ttf;
|
size_t u = (size_t)ttf;
|
||||||
int s;
|
int s;
|
||||||
|
|
@ -51,23 +66,55 @@ static MwFLFont assume_ttf(MwWidget handle, MwFLFont ttf) {
|
||||||
|
|
||||||
void MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, int align, MwLLColor color) {
|
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;
|
if(strlen(text) == 0) return;
|
||||||
#ifdef TTF
|
#ifdef TTF
|
||||||
if(ttf <= MwFLBuildFont(0xff)) ttf = assume_ttf(handle, ttf);
|
if(ttf <= MwFLBuildFont(0xff)) ttf = assume_ttf(handle, ttf);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
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) {
|
if(align == MwALIGNMENT_CENTER) {
|
||||||
p.x -= MwTextWidth(handle, ttf, text) / 2;
|
p.x -= MwTextWidth(handle, ttf, line) / 2;
|
||||||
} else if(align == MwALIGNMENT_END) {
|
} else if(align == MwALIGNMENT_END) {
|
||||||
p.x -= MwTextWidth(handle, ttf, text);
|
p.x -= MwTextWidth(handle, ttf, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef TTF
|
#ifdef TTF
|
||||||
if(MwFLDrawText)
|
if(MwFLDrawText)
|
||||||
if(MwGetInteger(handle, MwNbitmapFont) || ttf == NULL || MwFLDrawText(handle, ttf, &p, text, color))
|
if(MwGetInteger(handle, MwNbitmapFont) || ttf == NULL || MwFLDrawText(handle, ttf, &p, line, color))
|
||||||
#endif
|
#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) {
|
int MwTextWidth(MwWidget handle, MwFLFont ttf, const char* text) {
|
||||||
|
|
@ -113,7 +160,7 @@ int MwTextWidth(MwWidget handle, MwFLFont ttf, const char* text) {
|
||||||
|
|
||||||
free(line);
|
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 MwTextHeight(MwWidget handle, MwFLFont ttf, const char* text) {
|
||||||
int c = 1;
|
int c = count_nl(text);
|
||||||
int i = 0;
|
|
||||||
#ifdef TTF
|
#ifdef TTF
|
||||||
int st;
|
int st;
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -135,14 +181,6 @@ int MwTextHeight(MwWidget handle, MwFLFont ttf, const char* text) {
|
||||||
(void)ttf;
|
(void)ttf;
|
||||||
#endif
|
#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
|
#ifdef TTF
|
||||||
if(MwFLTextHeight)
|
if(MwFLTextHeight)
|
||||||
if(!MwGetInteger(handle, MwNbitmapFont) && ttf != NULL && (st = MwFLTextHeight(ttf, c)) != -1) return st;
|
if(!MwGetInteger(handle, MwNbitmapFont) && ttf != NULL && (st = MwFLTextHeight(ttf, c)) != -1) return st;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue