forked from pyrite-dev/milsko
refactor font api
This commit is contained in:
parent
9fc659b652
commit
0de09207a4
31 changed files with 12209 additions and 6149 deletions
3022
src/text/font/boldmonottf.c
Normal file
3022
src/text/font/boldmonottf.c
Normal file
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
3022
src/text/font/monottf.c
Normal file
3022
src/text/font/monottf.c
Normal file
File diff suppressed because it is too large
Load diff
5891
src/text/font/ttf.c
5891
src/text/font/ttf.c
File diff suppressed because it is too large
Load diff
|
|
@ -4,7 +4,9 @@
|
|||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
static struct {
|
||||
#define HEIGHT 14
|
||||
|
||||
static struct ft_table {
|
||||
FT_Error (*FT_Init_FreeType)(FT_Library* alibrary);
|
||||
FT_Error (*FT_New_Memory_Face)(FT_Library library,
|
||||
const FT_Byte* file_base,
|
||||
|
|
@ -27,17 +29,15 @@ struct _MwFLFont {
|
|||
FT_Face face;
|
||||
void* data;
|
||||
};
|
||||
static int ft2_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color) {
|
||||
MwFLFont ttf = MwGetVoid(handle, bold ? MwNboldFont : MwNfont);
|
||||
static int ft2_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, MwLLColor color) {
|
||||
int tw, th;
|
||||
unsigned char* px;
|
||||
MwLLPixmap p;
|
||||
MwRect r;
|
||||
int x = 0, y = 0;
|
||||
if(ttf == NULL) return 1;
|
||||
|
||||
tw = MwTextWidth(handle, text);
|
||||
th = MwTextHeight(handle, text);
|
||||
tw = MwTextWidth(handle, ttf, text);
|
||||
th = MwTextHeight(handle, ttf, text);
|
||||
px = malloc(tw * th * 4);
|
||||
|
||||
memset(px, 0, tw * th * 4);
|
||||
|
|
@ -51,7 +51,7 @@ static int ft2_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int
|
|||
|
||||
if(c == '\n') {
|
||||
x = 0;
|
||||
y += ttf->face->height * 14 / ttf->face->units_per_EM;
|
||||
y += ttf->face->height * HEIGHT / ttf->face->units_per_EM;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ static int ft2_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int
|
|||
for(cy = 0; cy < bmp->rows; cy++) {
|
||||
for(cx = 0; cx < bmp->width; cx++) {
|
||||
int ox = x + cx + ttf->face->glyph->bitmap_left;
|
||||
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);
|
||||
int oy = y + (ttf->face->height * HEIGHT / ttf->face->units_per_EM) - ttf->face->glyph->bitmap_top + cy + (ttf->face->descender * HEIGHT / ttf->face->units_per_EM);
|
||||
unsigned char* opx = &px[(oy * tw + ox) * 4];
|
||||
|
||||
opx[0] = color->common.red;
|
||||
|
|
@ -80,12 +80,6 @@ static int ft2_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int
|
|||
r.width = tw;
|
||||
r.height = th;
|
||||
|
||||
if(align == MwALIGNMENT_CENTER) {
|
||||
r.x -= tw / 2;
|
||||
} else if(align == MwALIGNMENT_END) {
|
||||
r.x -= tw;
|
||||
}
|
||||
|
||||
MwLLDrawPixmap(handle->lowlevel, &r, p);
|
||||
MwLLDestroyPixmap(p);
|
||||
free(px);
|
||||
|
|
@ -93,10 +87,8 @@ static int ft2_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int ft2_MwTextWidth(MwWidget handle, const char* text) {
|
||||
MwFLFont ttf = MwGetVoid(handle, MwNfont);
|
||||
int tw = 0, mtw = 0;
|
||||
if(ttf == NULL) return -1;
|
||||
static int ft2_MwTextWidth(MwFLFont ttf, const char* text) {
|
||||
int tw = 0, mtw = 0;
|
||||
|
||||
while(text[0] != 0) {
|
||||
int c;
|
||||
|
|
@ -117,11 +109,8 @@ static int ft2_MwTextWidth(MwWidget handle, const char* text) {
|
|||
return mtw;
|
||||
}
|
||||
|
||||
static int ft2_MwTextHeight(MwWidget handle, int count) {
|
||||
MwFLFont ttf = MwGetVoid(handle, MwNfont);
|
||||
if(ttf == NULL) return -1;
|
||||
|
||||
return (ttf->face->height * 14 / ttf->face->units_per_EM) * count;
|
||||
static int ft2_MwTextHeight(MwFLFont ttf, int count) {
|
||||
return (ttf->face->height * HEIGHT / ttf->face->units_per_EM) * count;
|
||||
}
|
||||
|
||||
static void* ft2_MwFontLoad(unsigned char* data, unsigned int size) {
|
||||
|
|
@ -131,7 +120,7 @@ static void* ft2_MwFontLoad(unsigned char* data, unsigned int size) {
|
|||
ft_table.FT_Init_FreeType(&ttf->library);
|
||||
ft_table.FT_New_Memory_Face(ttf->library, ttf->data, size, 0, &ttf->face);
|
||||
|
||||
ft_table.FT_Set_Pixel_Sizes(ttf->face, 0, 14);
|
||||
ft_table.FT_Set_Pixel_Sizes(ttf->face, 0, HEIGHT);
|
||||
|
||||
return ttf;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,18 +11,16 @@ struct _MwFLFont {
|
|||
int descent;
|
||||
};
|
||||
|
||||
static int stbtt_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color) {
|
||||
MwFLFont ttf = MwGetVoid(handle, bold ? MwNboldFont : MwNfont);
|
||||
static int stbtt_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, MwLLColor color) {
|
||||
unsigned char* px;
|
||||
int tw, th;
|
||||
MwRect r;
|
||||
MwLLPixmap p;
|
||||
int ax, lsb;
|
||||
int x = 0, y = 0;
|
||||
if(ttf == NULL) return 1;
|
||||
|
||||
tw = MwTextWidth(handle, text);
|
||||
th = MwTextHeight(handle, text);
|
||||
tw = MwTextWidth(handle, ttf, text);
|
||||
th = MwTextHeight(handle, ttf, text);
|
||||
px = malloc(tw * th * 4);
|
||||
|
||||
memset(px, 0, tw * th * 4);
|
||||
|
|
@ -71,12 +69,6 @@ static int stbtt_MwDrawText(MwWidget handle, MwPoint* point, const char* text, i
|
|||
r.width = tw;
|
||||
r.height = th;
|
||||
|
||||
if(align == MwALIGNMENT_CENTER) {
|
||||
r.x -= tw / 2;
|
||||
} else if(align == MwALIGNMENT_END) {
|
||||
r.x -= tw;
|
||||
}
|
||||
|
||||
MwLLDrawPixmap(handle->lowlevel, &r, p);
|
||||
MwLLDestroyPixmap(p);
|
||||
free(px);
|
||||
|
|
@ -84,11 +76,9 @@ static int stbtt_MwDrawText(MwWidget handle, MwPoint* point, const char* text, i
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int stbtt_MwTextWidth(MwWidget handle, const char* text) {
|
||||
MwFLFont ttf = MwGetVoid(handle, MwNfont);
|
||||
int ax, lsb;
|
||||
int tw = 0;
|
||||
if(ttf == NULL) return -1;
|
||||
static int stbtt_MwTextWidth(MwFLFont ttf, const char* text) {
|
||||
int ax, lsb;
|
||||
int tw = 0;
|
||||
|
||||
while(text[0] != 0) {
|
||||
int c;
|
||||
|
|
@ -102,10 +92,7 @@ static int stbtt_MwTextWidth(MwWidget handle, const char* text) {
|
|||
return tw;
|
||||
}
|
||||
|
||||
static int stbtt_MwTextHeight(MwWidget handle, int count) {
|
||||
MwFLFont ttf = MwGetVoid(handle, MwNfont);
|
||||
if(ttf == NULL) return -1;
|
||||
|
||||
static int stbtt_MwTextHeight(MwFLFont ttf, int count) {
|
||||
return (ttf->ascent - ttf->descent) * ttf->scale * count;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
#include <Mw/Milsko.h>
|
||||
|
||||
int (*MwFLDrawText)(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color) = NULL;
|
||||
int (*MwFLTextWidth)(MwWidget handle, const char* text) = NULL;
|
||||
int (*MwFLTextHeight)(MwWidget handle, int count) = NULL;
|
||||
void* (*MwFLFontLoad)(unsigned char* data, unsigned int size) = NULL;
|
||||
void (*MwFLFontFree)(void* handle) = NULL;
|
||||
int (*MwFLDrawText)(MwWidget handle, MwFLFont font, MwPoint* point, const char* text, MwLLColor color) = NULL;
|
||||
int (*MwFLTextWidth)(MwFLFont font, const char* text) = NULL;
|
||||
int (*MwFLTextHeight)(MwFLFont font, int count) = NULL;
|
||||
void* (*MwFLFontLoad)(unsigned char* data, unsigned int size) = NULL;
|
||||
void (*MwFLFontFree)(void* handle) = NULL;
|
||||
|
||||
#if defined(USE_FREETYPE2) || defined(USE_STB_TRUETYPE)
|
||||
#define TTF
|
||||
|
|
@ -13,40 +13,91 @@ void (*MwFLFontFree)(void* handle) = NULL;
|
|||
#define FontWidth 7
|
||||
#define FontHeight 14
|
||||
|
||||
static void bitmap_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color);
|
||||
static void bitmap_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, MwLLColor color);
|
||||
|
||||
void MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color) {
|
||||
if(strlen(text) == 0) return;
|
||||
#ifdef TTF
|
||||
if(MwFLDrawText)
|
||||
if(MwGetInteger(handle, MwNbitmapFont) || MwFLDrawText(handle, point, text, bold, align, color))
|
||||
#endif
|
||||
bitmap_MwDrawText(handle, point, text, bold, align, color);
|
||||
static int is_bold(MwWidget handle, MwFLFont ttf) {
|
||||
size_t u = (size_t)ttf;
|
||||
int s;
|
||||
|
||||
if(u & MwFLFlagBold) return 1;
|
||||
|
||||
s = MwGetInteger(handle, MwNbold);
|
||||
if(s == MwDEFAULT) return 0;
|
||||
return s;
|
||||
}
|
||||
|
||||
int MwTextWidth(MwWidget handle, const char* text) {
|
||||
static int is_monospace(MwWidget handle, MwFLFont ttf) {
|
||||
size_t u = (size_t)ttf;
|
||||
int s;
|
||||
|
||||
if(u & MwFLFlagMonospace) return 1;
|
||||
|
||||
s = MwGetInteger(handle, MwNuseMonospace);
|
||||
if(s == MwDEFAULT) return 0;
|
||||
return s;
|
||||
}
|
||||
|
||||
#ifdef TTF
|
||||
|
||||
static MwFLFont assume_ttf(MwWidget handle, MwFLFont ttf) {
|
||||
if(MwGetInteger(handle, MwNbitmapFont)) return NULL;
|
||||
|
||||
if(is_monospace(handle, ttf)) return is_bold(handle, ttf) ? MwGetVoid(handle, MwNboldMonospaceFont) : MwGetVoid(handle, MwNmonospaceFont);
|
||||
return is_bold(handle, ttf) ? MwGetVoid(handle, MwNboldFont) : MwGetVoid(handle, MwNfont);
|
||||
}
|
||||
#endif
|
||||
|
||||
void MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, int align, MwLLColor color) {
|
||||
MwPoint p = *point;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#ifdef TTF
|
||||
if(MwFLDrawText)
|
||||
if(MwGetInteger(handle, MwNbitmapFont) || ttf == NULL || MwFLDrawText(handle, ttf, &p, text, color))
|
||||
#endif
|
||||
bitmap_MwDrawText(handle, &p, text, is_bold(handle, ttf), color);
|
||||
}
|
||||
|
||||
int MwTextWidth(MwWidget handle, MwFLFont ttf, const char* text) {
|
||||
/* TODO: check newline */
|
||||
#ifdef TTF
|
||||
int st;
|
||||
|
||||
if(ttf <= MwFLBuildFont(0xff)) ttf = assume_ttf(handle, ttf);
|
||||
|
||||
if(MwFLTextWidth)
|
||||
if(!MwGetInteger(handle, MwNbitmapFont) && (st = MwFLTextWidth(handle, text)) != -1) return st;
|
||||
if(!MwGetInteger(handle, MwNbitmapFont) && ttf != NULL && (st = MwFLTextWidth(ttf, text)) != -1) return st;
|
||||
#else
|
||||
(void)handle;
|
||||
|
||||
(void)ttf;
|
||||
#endif
|
||||
return MwUTF8Length(text) * FontWidth;
|
||||
}
|
||||
|
||||
int MwTextHeight(MwWidget handle, const char* text) {
|
||||
int MwTextHeight(MwWidget handle, MwFLFont ttf, const char* text) {
|
||||
int c = 1;
|
||||
int i = 0;
|
||||
#ifdef TTF
|
||||
int st;
|
||||
#endif
|
||||
|
||||
#ifdef TTF
|
||||
if(ttf <= MwFLBuildFont(0xff)) ttf = assume_ttf(handle, ttf);
|
||||
#else
|
||||
(void)handle;
|
||||
(void)text;
|
||||
(void)ttf;
|
||||
#endif
|
||||
|
||||
while(text[i] != 0) {
|
||||
int out;
|
||||
|
|
@ -58,7 +109,7 @@ int MwTextHeight(MwWidget handle, const char* text) {
|
|||
}
|
||||
#ifdef TTF
|
||||
if(MwFLTextHeight)
|
||||
if(!MwGetInteger(handle, MwNbitmapFont) && (st = MwFLTextHeight(handle, c)) != -1) return st;
|
||||
if(!MwGetInteger(handle, MwNbitmapFont) && ttf != NULL && (st = MwFLTextHeight(ttf, c)) != -1) return st;
|
||||
#endif
|
||||
return FontHeight * c;
|
||||
}
|
||||
|
|
@ -73,7 +124,7 @@ void MwFontFree(void* handle) {
|
|||
if(MwFLFontFree) MwFLFontFree(handle);
|
||||
}
|
||||
|
||||
static void bitmap_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color) {
|
||||
static void bitmap_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, MwLLColor color) {
|
||||
int i = 0, x, y, sx, sy;
|
||||
int tw, th;
|
||||
unsigned char* px;
|
||||
|
|
@ -81,8 +132,8 @@ static void bitmap_MwDrawText(MwWidget handle, MwPoint* point, const char* text,
|
|||
MwLLPixmap p;
|
||||
|
||||
if(strlen(text) == 0) text = " ";
|
||||
tw = MwTextWidth(handle, text);
|
||||
th = MwTextHeight(handle, text);
|
||||
tw = MwTextWidth(handle, NULL, text);
|
||||
th = MwTextHeight(handle, NULL, text);
|
||||
px = malloc(tw * th * 4);
|
||||
|
||||
memset(px, 0, tw * th * 4);
|
||||
|
|
@ -128,12 +179,6 @@ static void bitmap_MwDrawText(MwWidget handle, MwPoint* point, const char* text,
|
|||
r.width = tw;
|
||||
r.height = th;
|
||||
|
||||
if(align == MwALIGNMENT_CENTER) {
|
||||
r.x -= tw / 2;
|
||||
} else if(align == MwALIGNMENT_END) {
|
||||
r.x -= tw;
|
||||
}
|
||||
|
||||
MwLLDrawPixmap(handle->lowlevel, &r, p);
|
||||
MwLLDestroyPixmap(p);
|
||||
free(px);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue