1
0
mirror of https://github.com/dschmenk/PLASMA.git synced 2025-01-17 22:30:41 +00:00

Better font file format

This commit is contained in:
David Schmenk 2024-12-03 15:06:18 -08:00
parent d9466112a8
commit 480ea58026
3 changed files with 134 additions and 108 deletions

View File

@ -4,6 +4,7 @@ AFLAGS = -o $@
DHGRLIB = dhgr.tk/libs/DHGRLIB.REL
DHGRUTILS = dhgr.tk/libs//DHGRUTILS.REL
DHGRTEST = dhgr.tk/test/DHGRTEST.REL
FONTTEST = dhgr.tk/test/FONTTEST.REL
DHGRSPRED = dhgr.tk/utils/DHGRSPRED.REL
DHGREASY = dhgr.tk/utils/DHGREASY.REL
DHGRRGB = dhgr.tk/utils/DHGRRGB.REL
@ -17,10 +18,10 @@ libs: $(DHGRLIB) $(DHGRUTILS)
cp $(DHGRLIB) prodos/sys
cp $(DHGRUTILS) prodos/sys
test: $(DHGRTEST)
test: $(DHGRTEST) $(FONTTEST)
-mkdir prodos/dhgr.tk/
-mkdir prodos/dhgr.tk/test
cp $(DHGRTEST) prodos/dhgr.tk/test
cp dhgr.tk/test/*.REL prodos/dhgr.tk/test
cp dhgr.tk/test/*.BIN prodos/dhgr.tk/test
utils: $(DHGRSPRED) $(DHGREASY) $(DHGRRGB) $(DHGRSHOW) $(DLGRCONV)
@ -62,6 +63,10 @@ $(DHGRTEST): dhgr.tk/test/dhgrtest.pla $(DHGRLIB) $(DHGRUTILS) dhgr.tk/inc/dhgrl
./plasm -AMOW dhgr.tk/test/dhgrtest.pla
acme --setpc 4094 -o $(DHGRTEST) dhgr.tk/test/dhgrtest.a
$(FONTTEST): dhgr.tk/test/fonttest.pla $(DHGRLIB) $(DHGRUTILS) dhgr.tk/inc/dhgrlib.plh dhgr.tk/inc/dhgrutils.plh
./plasm -AMOW dhgr.tk/test/fonttest.pla
acme --setpc 4094 -o $(FONTTEST) dhgr.tk/test/fonttest.a
$(DHGRSPRED): dhgr.tk/utils/dhgrspred.pla $(DHGRLIB) $(DHGRUTILS) dhgr.tk/inc/dhgrlib.plh dhgr.tk/inc/dhgrutils.plh
./plasm -AMOW dhgr.tk/utils/dhgrspred.pla
acme --setpc 4094 -o $(DHGRSPRED) dhgr.tk/utils/dhgrspred.a

View File

@ -151,13 +151,6 @@ end
// Test Harness //
// //
//////////////////////////////////////////
def fontTest#0
var xorg,yorg,width,height,pixptr,t_ptr,f_ptr
xorg,yorg,width,height,pixptr = spriteRead("trade.spr")
dcgrPixmap(70-xorg,96-yorg,width,height,pixptr)
getc
end
def dhgrTest#0
word i
@ -452,7 +445,6 @@ end
rlesprptr = heapmark
heapalloc(dcgrEncPixMap(SPR_W,SPR_H,@sprite,rlesprptr))
dhgrMode(DHGR_COLOR_MODE)
fontTest
dhgrTest
screenRead("splash.dhgr")
dhgrOp(OP_SRC)

View File

@ -9,25 +9,79 @@
#include <freetype2/ft2build.h>
#include <freetype/freetype.h>
//#include FT_FREETYPE2_H
#define FONT_WIDTH 32 // 12
#define FONT_HEIGHT 12 // 20
#define FONT_WIDTH 32
#define FONT_HEIGHT 12
#define GLYPH_FIRST 32
#define GLYPH_LAST 127
#define GLYPH_COUNT (GLYPH_LAST-GLYPH_FIRST+1)
/*
* Bit reversals
*/
unsigned char pixReverse[256];
unsigned char clrSwap[256];
unsigned char clrRot[] = {0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,
0x01,0x03,0x05,0x07,0x09,0x0B,0x0D,0x0F};
void write_glyph(FILE *fp, int left, int top, int width, int height, int advance, unsigned char *buf, int pitch)
{
unsigned char glyphdef[5], *swapbuf;
int i;
glyphdef[0] = (left + 3) / 4;
glyphdef[1] = -top;
glyphdef[2] = (width + 3) / 4;
glyphdef[3] = height;
glyphdef[4] = (advance + 3) / 4;
fwrite(&glyphdef, 1, 5, fp);
swapbuf = malloc(pitch * height);
for (i = 0; i < pitch * height; i++)
swapbuf[i] = clrSwap[buf[i]];
buf = swapbuf;
while (height--)
{
fwrite(buf, 1, (width + 7) / 8, fp);
buf = buf + pitch;
}
free(swapbuf);
}
void write_font_file(char *filename, FT_Face face, int glyph_width, int glyph_height)
{
FILE *fp;
unsigned char ch;
int c;
if ((fp = fopen(filename, "wb")))
{
ch = GLYPH_FIRST;
fwrite(&ch, 1, 1, fp);
ch = GLYPH_COUNT;
fwrite(&ch, 1, 1, fp);
ch = (glyph_width + 3) / 4;
fwrite(&ch, 1, 1, fp);
ch = glyph_height;
fwrite(&ch, 1, 1, fp);
for (c = GLYPH_FIRST; c <= GLYPH_LAST; c++)
{
FT_Load_Char(face, c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
write_glyph(fp,
face->glyph->bitmap_left,
face->glyph->bitmap_top,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
face->glyph->advance.x >> 6,
face->glyph->bitmap.buffer,
face->glyph->bitmap.pitch);
}
fclose(fp);
}
}
/*
* Glyph render routines.
*/
unsigned char *glyph_buf;
int glyph_width, glyph_byte_width, glyph_height, glyph_base, glyph_pitch;
void bitblt(unsigned char *srcbuf, int srcpitch, unsigned char *dstbuf, int dstpitch, int width, int height)
void bitblt(int xorg, int yorg, int width, int height, unsigned char *srcbuf, int srcpitch, unsigned char *dstbuf, int dstpitch)
{
int i, j;
width = (width + 7) / 8;
dstbuf = dstbuf + yorg * dstpitch;
width = (width + 7) / 8; // Width in bytes
for (j = 0; j < height; j++)
{
for (i = 0; i < width; i++)
@ -36,48 +90,53 @@ void bitblt(unsigned char *srcbuf, int srcpitch, unsigned char *dstbuf, int dstp
dstbuf += dstpitch;
}
}
void strblt(char *string, unsigned char *dstbuf, int dstpitch, int x, int y)
void write_bitmap_file(char *filename, FT_Face face, int glyph_width, int glyph_height)
{
int c, l = strlen(string);
dstbuf += x + y * dstpitch;
for (c = 0; c < l; c++)
{
bitblt(glyph_buf + (string[c] - 32) * glyph_byte_width,
glyph_pitch,
dstbuf,
dstpitch,
glyph_width,
glyph_height);
dstbuf += glyph_width;
}
}
void create_font_glyphs(FT_Face face)
{
int c, glyph_top;
int bitmap_top, bitmap_rows;
int c, glyph_base, glyph_byte_width, glyph_pitch;
unsigned char *glyph_buf, *bitmap_buf;
FILE *fp;
FT_Set_Pixel_Sizes(face, glyph_width * 1.5, glyph_height);
glyph_pitch = (128-32+1) * glyph_byte_width;
glyph_buf = malloc(glyph_pitch * glyph_height);
glyph_base = glyph_height * 3 / 4 ; //+ 1;
glyph_width = glyph_width * 1.25;
//glyph_height = glyph_height * 1.25 + 1;
glyph_byte_width = (glyph_width + 7) / 8;
glyph_pitch = GLYPH_COUNT * glyph_byte_width;
glyph_buf = malloc(glyph_pitch * glyph_height);
memset(glyph_buf, 0, glyph_pitch * glyph_height);
for (c = 32; c < 128; c++)
for (c = GLYPH_FIRST; c <= GLYPH_LAST; c++)
{
FT_Load_Char(face, c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
glyph_top = glyph_base - face->glyph->bitmap_top;
if (glyph_top > 0 && glyph_top < glyph_height)
/*
bitblt(face->glyph->bitmap.buffer,
face->glyph->bitmap.pitch,
glyph_buf + ((c - 32) * glyph_width + face->glyph->bitmap_left) / 8 + glyph_top * glyph_pitch,
glyph_pitch,
face->glyph->bitmap.width,
face->glyph->bitmap.rows);
*/
bitblt(face->glyph->bitmap.buffer, // + face->glyph->bitmap_left,
face->glyph->bitmap.pitch,
glyph_buf + (c - 32) * glyph_byte_width + glyph_top * glyph_pitch,
glyph_pitch,
face->glyph->bitmap.width,
face->glyph->bitmap.rows);
bitmap_buf = face->glyph->bitmap.buffer,
bitmap_top = glyph_base - face->glyph->bitmap_top;
bitmap_rows = face->glyph->bitmap.rows;
if (bitmap_top < 0)
{
bitmap_rows += bitmap_top;
bitmap_buf = bitmap_buf - face->glyph->bitmap.pitch * bitmap_top;
bitmap_top = 0;
}
if (bitmap_top < glyph_height)
{
if (bitmap_top + bitmap_rows > glyph_height)
bitmap_rows = glyph_height - bitmap_top;
bitblt(0,
bitmap_top,
face->glyph->bitmap.width,
bitmap_rows,
bitmap_buf,
face->glyph->bitmap.pitch,
glyph_buf + (c - GLYPH_FIRST) * glyph_byte_width,
glyph_pitch);
}
else
printf("Glyph (%c) height: %d, top: %d, rows: %d Skipped!\n", c, glyph_height, face->glyph->bitmap_top, face->glyph->bitmap.rows);
}
if ((fp = fopen(filename, "wb")))
{
fprintf(fp, "P4\n%d\n%d\n", glyph_width * GLYPH_COUNT, glyph_height);
fwrite(glyph_buf, 1, glyph_pitch * glyph_height, fp);
fclose(fp);
}
}
void die(char *str)
@ -85,44 +144,30 @@ void die(char *str)
fprintf(stderr,"error: %s\n", str);
exit(-1);
}
int writeGlyphFile(char *filename, int xorg, int yorg, int width, int height, unsigned char *buf, int pitch)
{
FILE *fp;
unsigned char sprdef[4], *swapbuf;
int i;
if ((fp = fopen(filename, "wb")))
{
//fprintf(fp, "P4\n%d\n%d\n", width, height);
sprdef[0] = xorg;
sprdef[1] = yorg;
sprdef[2] = width/4;
sprdef[3] = height;
fwrite(&sprdef, 1, 4, fp);
swapbuf = malloc(pitch * height);
for (i = 0; i < pitch * height; i++)
swapbuf[i] = clrSwap[buf[i]];
fwrite(swapbuf, 1, pitch * height, fp);
free(swapbuf);
fclose(fp);
return 1;
}
return 0;
}
int main(int argc, char **argv)
{
char *fontfile, *glyph_file;
char *ttf_file, *font_file;
FT_Library ft;
FT_Face face;
unsigned char pixReverse;
unsigned char *font_buf;
int glyph_width, glyph_height, font_pitch;
unsigned char bitReverse;
int i;
glyph_file = NULL;
font_file = NULL;
if (argc > 1)
{
fontfile = argv[1];
ttf_file = argv[1];
if (argc > 2)
glyph_file = argv[2];
font_file = argv[2];
if (argc > 3)
glyph_width = atoi(argv[3]);
else
glyph_width = FONT_WIDTH;
if (argc > 4)
glyph_height = atoi(argv[4]);
else
glyph_height = FONT_HEIGHT;
}
else
die( "Missing font file");
@ -131,7 +176,7 @@ int main(int argc, char **argv)
*/
for (i = 0; i < 256; i++)
{
pixReverse = ((i & 0x01) << 7)
bitReverse = ((i & 0x01) << 7)
| ((i & 0x02) << 5)
| ((i & 0x04) << 3)
| ((i & 0x08) << 1)
@ -139,42 +184,26 @@ int main(int argc, char **argv)
| ((i & 0x20) >> 3)
| ((i & 0x40) >> 5)
| ((i & 0x80) >> 7);
clrSwap[i] = clrRot[pixReverse & 0x0F]
| (clrRot[pixReverse >> 4] << 4);
clrSwap[i] = clrRot[bitReverse & 0x0F]
| (clrRot[bitReverse >> 4] << 4);
}
/*
* Init FreeType library.
*/
if (FT_Init_FreeType(&ft))
die("FreeType error");
if (FT_New_Face(ft, fontfile, 0, &face))
if (FT_New_Face(ft, ttf_file, 0, &face))
die("Missing font");
if (face->num_fixed_sizes)
{
glyph_width = face->available_sizes[0].width;
glyph_height = face->available_sizes[0].height;
}
else
{
glyph_width = FONT_WIDTH;
glyph_height = FONT_HEIGHT;
}
glyph_byte_width = (glyph_width + 7) / 8;
glyph_base = glyph_height * 3 / 4;
//create_font_glyphs(face);
FT_Set_Pixel_Sizes(face, glyph_width /* 1.5*/, glyph_height);
FT_Load_Char(face, 'A', FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
if (glyph_file)
{
if (!writeGlyphFile(glyph_file,
-face->glyph->bitmap_left,
face->glyph->bitmap_top,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
face->glyph->bitmap.buffer,
face->glyph->bitmap.pitch))
die("Error writing glyph file");
}
//if (glyph_width < 14) glyph_width = 14;
//if (glyph_height < 8) glyph_height = 8;
FT_Set_Pixel_Sizes(face, glyph_width, glyph_height);
//write_bitmap_file(font_file, face, glyph_width, glyph_height);
write_font_file(font_file, face, glyph_width, glyph_height);
return 0;
}