merge git PR #3 (round 2)

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@469 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
IoIxD
2025-10-23 00:29:37 +00:00
parent 64e8898945
commit 71685aebd6
2 changed files with 369 additions and 0 deletions

232
src/text/draw.c Normal file
View File

@@ -0,0 +1,232 @@
/* $Id$ */
#include <Mw/Milsko.h>
#ifdef HAS_FREETYPE
#include <ft2build.h>
#include FT_FREETYPE_H
#endif
#define bitmap_FontWidth 7
#define bitmap_FontHeight 14
#define ft_FontWidth 14
#define ft_FontHeight 14
static void bitmap_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color) {
int i = 0, x, y, sx, sy;
int tw;
int th;
unsigned char* px;
MwRect r;
MwLLPixmap p;
if(strlen(text) == 0) text = " ";
tw = MwTextWidth(handle, text);
th = MwTextHeight(handle, text);
px = malloc(tw * th * 4);
memset(px, 0, tw * th * 4);
sx = 0;
sy = 0;
while(text[i] != 0) {
int out;
i += MwUTF8ToUTF32(text + i, &out);
if(out >= 0x80) out = 0;
if(out == '\n') {
sx = 0;
sy += bitmap_FontHeight;
} else {
for(y = 0; y < bitmap_FontHeight; y++) {
for(x = 0; x < bitmap_FontWidth; x++) {
unsigned char* ppx = &px[((sy + y) * tw + sx + x) * 4];
if((bold ? MwBoldFontData : MwFontData)[out].data[y] & (1 << ((bitmap_FontWidth - 1) - x))) {
ppx[0] = color->red;
ppx[1] = color->green;
ppx[2] = color->blue;
ppx[3] = 255;
} else {
ppx[0] = 0;
ppx[1] = 0;
ppx[2] = 0;
ppx[3] = 0;
}
}
}
sx += bitmap_FontWidth;
}
}
p = MwLoadRaw(handle, px, tw, th);
r.x = point->x;
r.y = point->y - th / 2;
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);
}
#ifdef HAS_FREETYPE
static void ft_MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color) {
FT_Error err;
FT_Error (*_FT_Load_Char)(FT_Face face,
FT_ULong char_code,
FT_Int32 load_flags);
FT_Error (*_FT_Set_Pixel_Sizes)(FT_Face face,
FT_UInt pixel_width,
FT_UInt pixel_height);
MwLL l = handle->lowlevel;
int i = 0, x, y, sx, sy;
int tw;
int th;
unsigned char* px;
MwRect r;
MwLLPixmap p;
if(strlen(text) == 0) text = " ";
tw = MwTextWidth(handle, text);
th = MwTextHeight(handle, text);
// allocate 4x memory then what we need to account for spacing, italics, etc.
px = malloc(tw * th * 16);
memset(px, 0, tw * th * 16);
sx = 0;
sy = 0;
FT_WITH_FUNC(l, FT_Set_Pixel_Sizes, {
if((err = _FT_Set_Pixel_Sizes(l->ftFace, ft_FontWidth, ft_FontHeight)) != 0) {
if(r.width == 0 || r.height == 0) {
printf("failed to set pixel sizes");
print_ft_error(l->ftLib, err);
return;
}
}
});
FT_WITH_FUNC(l, FT_Load_Char, {
while(text[i] != 0) {
int out;
i += MwUTF8ToUTF32(text + i, &out);
if(out >= 0x80) out = 0;
if((err = _FT_Load_Char(l->ftFace, out, FT_LOAD_RENDER)) != 0) {
printf("[WARNING] Failed to render %c. ", out);
print_ft_error(l->ftLib, err);
continue;
}
FT_GlyphSlot glyph = l->ftFace->glyph;
FT_Bitmap bitmap = glyph->bitmap;
r.width = bitmap.width;
r.height = bitmap.rows;
if(sy == 0) {
sy = r.height;
}
if(r.width == 0 || r.height == 0) {
sx += ft_FontWidth;
continue;
}
if(out == '\n') {
sx = 0;
sy += r.height;
} else {
// printf("%ld\n", (glyph->advance.x / r.width));
int gsx = sx + glyph->bitmap_left;
int gsy = (sy - glyph->bitmap_top);
for(y = 0; y < r.height; y++) {
for(x = 0; x < r.width; x++) {
unsigned char* ppx = &px[((gsy + y) * tw + gsx + x) * 4];
unsigned char gray = bitmap.buffer[(y * r.width) + x];
ppx[0] = color->red - gray;
ppx[1] = color->green - gray;
ppx[2] = color->blue - gray;
ppx[3] = gray;
}
}
sx += r.width;
// sx += glyph->advance.x;
}
}
});
p = MwLoadRaw(handle, px, tw, th);
r.x = point->x;
r.y = point->y - th / 2;
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);
}
#endif
void MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color) {
bitmap_MwDrawText(handle, point, text, bold, align, color);
}
int MwTextWidth(MwWidget handle, const char* text) {
(void)handle;
/* TODO: check newline */
#ifdef HAS_FREETYPE
if(handle->lowlevel->ftLib != NULL) {
return strlen(text) * ft_FontWidth;
} else {
#endif
return strlen(text) * bitmap_FontWidth;
#ifdef HAS_FREETYPE
}
#endif
}
int MwTextHeight(MwWidget handle, const char* text) {
int c = 1;
int i = 0;
(void)handle;
(void)text;
while(text[i] != 0) {
int out;
i += MwUTF8ToUTF32(text + i, &out);
if(out == '\n') c++;
}
#ifdef HAS_FREETYPE
if(handle->lowlevel->ftLib != NULL) {
return ft_FontHeight * c;
} else {
#endif
return bitmap_FontHeight * c;
#ifdef HAS_FREETYPE
}
#endif
}

137
src/text/font.c Normal file
View File

@@ -0,0 +1,137 @@
/* $Id$ */
#include <Mw/Milsko.h>
/**
* Copyright notice:
* "Public domain font. Share and enjoy."
*/
MwFont MwFontData[] = {
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 0 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 1 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 2 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 3 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 4 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 5 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 6 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 7 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 8 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 9 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 10 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 11 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 12 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 13 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 14 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 15 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 16 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 17 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 18 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 19 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 20 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 21 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 22 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 23 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 24 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 25 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 26 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 27 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 28 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 29 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 30 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}}, /* 31 */
{0, 12, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, /* 32 */
{0, 12, {0, 0, 8, 8, 8, 8, 8, 8, 8, 0, 8, 8, 0, 0}}, /* 33 */
{0, 12, {0, 20, 20, 20, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, /* 34 */
{0, 12, {0, 0, 20, 20, 20, 62, 20, 20, 62, 20, 20, 20, 0, 0}}, /* 35 */
{0, 12, {0, 0, 8, 60, 74, 74, 40, 28, 10, 74, 74, 60, 8, 0}}, /* 36 */
{0, 12, {0, 0, 50, 74, 76, 56, 8, 16, 28, 50, 82, 76, 0, 0}}, /* 37 */
{0, 12, {0, 0, 24, 36, 36, 36, 24, 50, 74, 68, 76, 50, 0, 0}}, /* 38 */
{0, 12, {0, 8, 8, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, /* 39 */
{0, 12, {0, 2, 4, 8, 8, 16, 16, 16, 16, 16, 8, 8, 4, 2}}, /* 40 */
{0, 12, {0, 32, 16, 8, 8, 4, 4, 4, 4, 4, 8, 8, 16, 32}}, /* 41 */
{0, 12, {0, 0, 0, 0, 8, 42, 28, 8, 28, 42, 8, 0, 0, 0}}, /* 42 */
{0, 12, {0, 0, 0, 0, 8, 8, 8, 62, 8, 8, 8, 0, 0, 0}}, /* 43 */
{0, 12, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 8, 8, 16}}, /* 44 */
{0, 12, {0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 0, 0, 0}}, /* 45 */
{0, 12, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 28, 8, 0}}, /* 46 */
{0, 12, {0, 2, 2, 4, 4, 8, 8, 8, 16, 16, 32, 32, 64, 64}}, /* 47 */
{0, 12, {0, 0, 24, 36, 66, 66, 66, 66, 66, 66, 36, 24, 0, 0}}, /* 48 */
{0, 12, {0, 0, 8, 24, 40, 8, 8, 8, 8, 8, 8, 62, 0, 0}}, /* 49 */
{0, 12, {0, 0, 60, 66, 66, 2, 4, 4, 8, 16, 32, 126, 0, 0}}, /* 50 */
{0, 12, {0, 0, 126, 2, 4, 8, 28, 2, 2, 66, 66, 60, 0, 0}}, /* 51 */
{0, 12, {0, 0, 4, 12, 20, 20, 36, 36, 68, 126, 4, 4, 0, 0}}, /* 52 */
{0, 12, {0, 0, 126, 64, 64, 124, 66, 2, 2, 66, 66, 60, 0, 0}}, /* 53 */
{0, 12, {0, 0, 28, 32, 64, 64, 92, 98, 66, 66, 66, 60, 0, 0}}, /* 54 */
{0, 12, {0, 0, 126, 2, 4, 4, 8, 8, 16, 16, 32, 32, 0, 0}}, /* 55 */
{0, 12, {0, 0, 60, 66, 66, 36, 24, 36, 66, 66, 66, 60, 0, 0}}, /* 56 */
{0, 12, {0, 0, 60, 66, 66, 66, 70, 58, 2, 66, 68, 56, 0, 0}}, /* 57 */
{0, 12, {0, 0, 0, 0, 8, 28, 8, 0, 0, 8, 28, 8, 0, 0}}, /* 58 */
{0, 12, {0, 0, 0, 0, 0, 24, 24, 0, 0, 24, 8, 8, 16, 0}}, /* 59 */
{0, 12, {0, 0, 0, 2, 4, 8, 16, 32, 16, 8, 4, 2, 0, 0}}, /* 60 */
{0, 12, {0, 0, 0, 0, 0, 126, 0, 0, 126, 0, 0, 0, 0, 0}}, /* 61 */
{0, 12, {0, 0, 0, 32, 16, 8, 4, 2, 4, 8, 16, 32, 0, 0}}, /* 62 */
{0, 12, {0, 0, 60, 66, 66, 4, 8, 8, 8, 0, 8, 8, 0, 0}}, /* 63 */
{0, 12, {0, 0, 28, 34, 78, 82, 82, 82, 82, 78, 32, 30, 0, 0}}, /* 64 */
{0, 12, {0, 0, 24, 36, 66, 66, 66, 126, 66, 66, 66, 66, 0, 0}}, /* 65 */
{0, 12, {0, 0, 120, 68, 66, 68, 120, 68, 66, 66, 68, 120, 0, 0}}, /* 66 */
{0, 12, {0, 0, 60, 66, 66, 64, 64, 64, 64, 66, 66, 60, 0, 0}}, /* 67 */
{0, 12, {0, 0, 120, 68, 66, 66, 66, 66, 66, 66, 68, 120, 0, 0}}, /* 68 */
{0, 12, {0, 0, 126, 64, 64, 64, 120, 64, 64, 64, 64, 126, 0, 0}}, /* 69 */
{0, 12, {0, 0, 126, 64, 64, 64, 120, 64, 64, 64, 64, 64, 0, 0}}, /* 70 */
{0, 12, {0, 0, 60, 66, 66, 64, 64, 78, 66, 66, 70, 58, 0, 0}}, /* 71 */
{0, 12, {0, 0, 66, 66, 66, 66, 126, 66, 66, 66, 66, 66, 0, 0}}, /* 72 */
{0, 12, {0, 0, 62, 8, 8, 8, 8, 8, 8, 8, 8, 62, 0, 0}}, /* 73 */
{0, 12, {0, 0, 14, 4, 4, 4, 4, 4, 4, 68, 68, 56, 0, 0}}, /* 74 */
{0, 12, {0, 0, 66, 68, 72, 80, 96, 80, 72, 68, 66, 66, 0, 0}}, /* 75 */
{0, 12, {0, 0, 64, 64, 64, 64, 64, 64, 64, 64, 64, 126, 0, 0}}, /* 76 */
{0, 12, {0, 0, 66, 102, 102, 90, 90, 66, 66, 66, 66, 66, 0, 0}}, /* 77 */
{0, 12, {0, 0, 66, 66, 98, 98, 82, 74, 70, 70, 66, 66, 0, 0}}, /* 78 */
{0, 12, {0, 0, 60, 66, 66, 66, 66, 66, 66, 66, 66, 60, 0, 0}}, /* 79 */
{0, 12, {0, 0, 124, 66, 66, 66, 66, 124, 64, 64, 64, 64, 0, 0}}, /* 80 */
{0, 12, {0, 0, 60, 66, 66, 66, 66, 66, 114, 74, 70, 60, 4, 2}}, /* 81 */
{0, 12, {0, 0, 124, 66, 66, 66, 66, 124, 72, 68, 66, 66, 0, 0}}, /* 82 */
{0, 12, {0, 0, 60, 66, 66, 64, 48, 12, 2, 66, 66, 60, 0, 0}}, /* 83 */
{0, 12, {0, 0, 127, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0, 0}}, /* 84 */
{0, 12, {0, 0, 66, 66, 66, 66, 66, 66, 66, 66, 66, 60, 0, 0}}, /* 85 */
{0, 12, {0, 0, 66, 66, 66, 66, 36, 36, 36, 24, 24, 24, 0, 0}}, /* 86 */
{0, 12, {0, 0, 34, 34, 34, 34, 34, 34, 42, 42, 42, 20, 0, 0}}, /* 87 */
{0, 12, {0, 0, 66, 66, 36, 36, 24, 24, 36, 36, 66, 66, 0, 0}}, /* 88 */
{0, 12, {0, 0, 34, 34, 34, 20, 20, 8, 8, 8, 8, 8, 0, 0}}, /* 89 */
{0, 12, {0, 0, 126, 2, 4, 8, 8, 16, 32, 32, 64, 126, 0, 0}}, /* 90 */
{0, 12, {0, 30, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 30}}, /* 91 */
{0, 12, {0, 64, 64, 32, 32, 16, 16, 16, 8, 8, 4, 4, 2, 2}}, /* 92 */
{0, 12, {0, 60, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 60}}, /* 93 */
{0, 12, {0, 24, 36, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, /* 94 */
{0, 12, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 126}}, /* 95 */
{0, 12, {0, 16, 8, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, /* 96 */
{0, 12, {0, 0, 0, 0, 0, 60, 66, 2, 62, 66, 66, 62, 0, 0}}, /* 97 */
{0, 12, {0, 0, 64, 64, 64, 92, 98, 66, 66, 66, 98, 92, 0, 0}}, /* 98 */
{0, 12, {0, 0, 0, 0, 0, 60, 66, 64, 64, 64, 66, 60, 0, 0}}, /* 99 */
{0, 12, {0, 0, 2, 2, 2, 58, 70, 66, 66, 66, 70, 58, 0, 0}}, /* 100 */
{0, 12, {0, 0, 0, 0, 0, 60, 66, 66, 126, 64, 66, 60, 0, 0}}, /* 101 */
{0, 12, {0, 0, 12, 18, 16, 16, 124, 16, 16, 16, 16, 16, 0, 0}}, /* 102 */
{0, 12, {0, 0, 0, 0, 0, 58, 68, 68, 68, 56, 32, 92, 66, 60}}, /* 103 */
{0, 12, {0, 0, 64, 64, 64, 92, 98, 66, 66, 66, 66, 66, 0, 0}}, /* 104 */
{0, 12, {0, 0, 8, 8, 0, 24, 8, 8, 8, 8, 8, 62, 0, 0}}, /* 105 */
{0, 12, {0, 0, 2, 2, 0, 6, 2, 2, 2, 2, 2, 34, 34, 28}}, /* 106 */
{0, 12, {0, 0, 64, 64, 64, 68, 72, 80, 112, 72, 68, 66, 0, 0}}, /* 107 */
{0, 12, {0, 0, 24, 8, 8, 8, 8, 8, 8, 8, 8, 62, 0, 0}}, /* 108 */
{0, 12, {0, 0, 0, 0, 0, 52, 42, 42, 42, 42, 42, 34, 0, 0}}, /* 109 */
{0, 12, {0, 0, 0, 0, 0, 92, 98, 66, 66, 66, 66, 66, 0, 0}}, /* 110 */
{0, 12, {0, 0, 0, 0, 0, 60, 66, 66, 66, 66, 66, 60, 0, 0}}, /* 111 */
{0, 12, {0, 0, 0, 0, 0, 92, 98, 66, 66, 66, 98, 92, 64, 64}}, /* 112 */
{0, 12, {0, 0, 0, 0, 0, 58, 70, 66, 66, 66, 70, 58, 2, 2}}, /* 113 */
{0, 12, {0, 0, 0, 0, 0, 92, 98, 66, 64, 64, 64, 64, 0, 0}}, /* 114 */
{0, 12, {0, 0, 0, 0, 0, 60, 66, 32, 24, 4, 66, 60, 0, 0}}, /* 115 */
{0, 12, {0, 0, 16, 16, 16, 124, 16, 16, 16, 16, 18, 12, 0, 0}}, /* 116 */
{0, 12, {0, 0, 0, 0, 0, 66, 66, 66, 66, 66, 70, 58, 0, 0}}, /* 117 */
{0, 12, {0, 0, 0, 0, 0, 34, 34, 34, 20, 20, 8, 8, 0, 0}}, /* 118 */
{0, 12, {0, 0, 0, 0, 0, 34, 34, 42, 42, 42, 42, 20, 0, 0}}, /* 119 */
{0, 12, {0, 0, 0, 0, 0, 66, 66, 36, 24, 36, 66, 66, 0, 0}}, /* 120 */
{0, 12, {0, 0, 0, 0, 0, 66, 66, 66, 66, 70, 58, 2, 66, 60}}, /* 121 */
{0, 12, {0, 0, 0, 0, 0, 126, 4, 8, 16, 16, 32, 126, 0, 0}}, /* 122 */
{0, 12, {0, 6, 8, 8, 8, 8, 8, 16, 8, 8, 8, 8, 8, 6}}, /* 123 */
{0, 12, {0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}}, /* 124 */
{0, 12, {0, 48, 8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 8, 48}}, /* 125 */
{0, 12, {0, 32, 82, 74, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, /* 126 */
{0, 12, {0, 90, 66, 0, 66, 66, 0, 66, 66, 0, 66, 90, 0, 0}} /* 127 */
};