From 8d289856fd1ec86cb49f3d2d189a2604b3a8a3d7 Mon Sep 17 00:00:00 2001 From: David Schmenk Date: Sat, 30 Nov 2024 16:31:57 -0800 Subject: [PATCH] Start TTF conversion for DHGR --- src/dhgr.tk/utils/ttfconv.c | 104 ++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/dhgr.tk/utils/ttfconv.c diff --git a/src/dhgr.tk/utils/ttfconv.c b/src/dhgr.tk/utils/ttfconv.c new file mode 100644 index 0000000..0493980 --- /dev/null +++ b/src/dhgr.tk/utils/ttfconv.c @@ -0,0 +1,104 @@ +/* + * + */ + +#include +#include +#include + +#include +#include +//#include FT_FREETYPE2_H +#define FONT_WIDTH 12 +#define FONT_HEIGHT 20 +/* + * Glyph render routines. + */ +unsigned char *glyph_buf; +int glyph_width, glyph_height, glyph_base, glyph_pitch; +void pixblt(unsigned char *srcbuf, int srcpitch, unsigned char *dstbuf, int dstpitch, int width, int height) +{ + int i, j; + + for (j = 0; j < height; j++) + { + for (i = 0; i < width; i++) + dstbuf[i] = srcbuf[i]; + srcbuf += srcpitch; + dstbuf += dstpitch; + } +} +void strblt(char *string, unsigned char *dstbuf, int dstpitch, int x, int y) +{ + int c, l = strlen(string); + dstbuf += x + y * dstpitch; + for (c = 0; c < l; c++) + { + pixblt(glyph_buf + (string[c] - 32) * glyph_width, + glyph_pitch, + dstbuf, + dstpitch, + glyph_width, + glyph_height); + dstbuf += glyph_width; + } +} +void create_font_glyphs(FT_Face face) +{ + int c, glyph_top; + + FT_Set_Pixel_Sizes(face, glyph_width * 1.5, glyph_height); + glyph_pitch = glyph_width * (128-32); + glyph_buf = malloc(glyph_pitch * glyph_height); + memset(glyph_buf, 0, glyph_pitch * glyph_height); + for (c = 32; c < 128; c++) + { + FT_Load_Char(face, c, FT_LOAD_RENDER); + glyph_top = glyph_base - face->glyph->bitmap_top; + if (glyph_top > 0 && glyph_top < glyph_height) + pixblt(face->glyph->bitmap.buffer, + face->glyph->bitmap.pitch, + glyph_buf + (c - 32) * glyph_width + face->glyph->bitmap_left + glyph_top * glyph_pitch, + glyph_pitch, + face->glyph->bitmap.width, + face->glyph->bitmap.rows); + } +} +void die(char *str) +{ + fprintf(stderr,"error: %s\n", str); + exit(-1); +} +int main(int argc, char **argv) +{ + char *fontfile; + FT_Library ft; + FT_Face face; + + if (argc > 1) + fontfile = argv[1]; + else + die( "Missing font file"); + /* + * Init FreeType library. + */ + if (FT_Init_FreeType(&ft)) + die("FreeType error"); + if (FT_New_Face(ft, fontfile, 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_base = glyph_height * 3 / 4; + create_font_glyphs(face); + + return 0; +} +