handle newline

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@786 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
Nishi 2025-11-23 05:25:40 +00:00
commit 77c91948a5
2 changed files with 26 additions and 10 deletions

View file

@ -101,7 +101,7 @@ static int ttf_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int
MwRect r; MwRect r;
MwLLPixmap p; MwLLPixmap p;
int ax, lsb; int ax, lsb;
int x = 0; int x = 0, y = 0;
if(ttf == NULL) return 1; if(ttf == NULL) return 1;
tw = MwTextWidth(handle, text); tw = MwTextWidth(handle, text);
@ -116,6 +116,11 @@ static int ttf_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int
unsigned char* out; unsigned char* out;
text += MwUTF8ToUTF32(text, &c); text += MwUTF8ToUTF32(text, &c);
if(c == '\n') {
x = 0;
y += (ttf->ascent - ttf->descent) * ttf->scale;
continue;
}
stbtt_GetCodepointHMetrics(&ttf->font, c, &ax, &lsb); stbtt_GetCodepointHMetrics(&ttf->font, c, &ax, &lsb);
@ -128,7 +133,7 @@ static int ttf_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int
for(cy = 0; cy < oh; cy++) { for(cy = 0; cy < oh; cy++) {
for(cx = 0; cx < ow; cx++) { for(cx = 0; cx < ow; cx++) {
int ox = x + (lsb * ttf->scale) + cx; int ox = x + (lsb * ttf->scale) + cx;
int oy = (ttf->ascent * ttf->scale) + y0 + cy; int oy = y + (ttf->ascent * ttf->scale) + y0 + cy;
unsigned char* opx = &px[(oy * tw + ox) * 4]; unsigned char* opx = &px[(oy * tw + ox) * 4];
opx[0] = color->common.red; opx[0] = color->common.red;
@ -193,7 +198,7 @@ static int ttf_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int
unsigned char* px; unsigned char* px;
MwLLPixmap p; MwLLPixmap p;
MwRect r; MwRect r;
int x = 0; int x = 0, y = 0;
if(ttf == NULL) return 1; if(ttf == NULL) return 1;
tw = MwTextWidth(handle, text); tw = MwTextWidth(handle, text);
@ -207,13 +212,19 @@ static int ttf_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int
int cy, cx; int cy, cx;
text += MwUTF8ToUTF32(text, &c); text += MwUTF8ToUTF32(text, &c);
if(c == '\n') {
x = 0;
y += ttf->face->height * 14 / ttf->face->units_per_EM;
continue;
}
FT_Load_Char(ttf->face, c, FT_LOAD_RENDER); FT_Load_Char(ttf->face, c, FT_LOAD_RENDER);
bmp = &ttf->face->glyph->bitmap; bmp = &ttf->face->glyph->bitmap;
for(cy = 0; cy < bmp->rows; cy++) { for(cy = 0; cy < bmp->rows; cy++) {
for(cx = 0; cx < bmp->width; cx++) { for(cx = 0; cx < bmp->width; cx++) {
int ox = x + cx + ttf->face->glyph->bitmap_left; int ox = x + cx + ttf->face->glyph->bitmap_left;
int oy = th - ttf->face->glyph->bitmap_top + cy + (ttf->face->descender * 14 / ttf->face->units_per_EM); int oy = y + (ttf->face->height * 14 / ttf->face->units_per_EM) - ttf->face->glyph->bitmap_top + cy + (ttf->face->descender * 14 / ttf->face->units_per_EM);
unsigned char* opx = &px[(oy * tw + ox) * 4]; unsigned char* opx = &px[(oy * tw + ox) * 4];
opx[0] = color->common.red; opx[0] = color->common.red;
@ -247,19 +258,24 @@ static int ttf_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int
static int ttf_MwTextWidth(MwWidget handle, const char* text) { static int ttf_MwTextWidth(MwWidget handle, const char* text) {
ttf_t* ttf = MwGetVoid(handle, MwNfont); ttf_t* ttf = MwGetVoid(handle, MwNfont);
int tw = 0; int tw = 0, mtw = 0;
if(ttf == NULL) return -1; if(ttf == NULL) return -1;
while(text[0] != 0) { while(text[0] != 0) {
int c; int c;
text += MwUTF8ToUTF32(text, &c); text += MwUTF8ToUTF32(text, &c);
if(c == '\n') {
tw = 0;
continue;
}
FT_Load_Char(ttf->face, c, FT_LOAD_RENDER); FT_Load_Char(ttf->face, c, FT_LOAD_RENDER);
tw += ttf->face->glyph->metrics.horiAdvance / 64; tw += ttf->face->glyph->metrics.horiAdvance / 64;
if(tw > mtw) mtw = tw;
} }
return tw; return mtw;
} }
static int ttf_MwTextHeight(MwWidget handle, int count) { static int ttf_MwTextHeight(MwWidget handle, int count) {