diff --git a/examples/basic/periodic.c b/examples/basic/periodic.c index 91dd601..cc55882 100644 --- a/examples/basic/periodic.c +++ b/examples/basic/periodic.c @@ -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); diff --git a/src/text/ft2.c b/src/text/ft2.c index 26cf8cd..472a93f 100644 --- a/src/text/ft2.c +++ b/src/text/ft2.c @@ -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; } diff --git a/src/text/stbtt.c b/src/text/stbtt.c index 0bacc80..369ad20 100644 --- a/src/text/stbtt.c +++ b/src/text/stbtt.c @@ -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; diff --git a/src/text/text.c b/src/text/text.c index eac182e..c3f7cc7 100644 --- a/src/text/text.c +++ b/src/text/text.c @@ -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;