From eeb5831bb4d62c35eca6db333137a9b8bf682e6e Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sat, 6 Jun 2020 11:04:36 -0700 Subject: deprecated wayland --- sys/libfont/test.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 116 insertions(+), 3 deletions(-) (limited to 'sys/libfont/test.c') diff --git a/sys/libfont/test.c b/sys/libfont/test.c index 7ce9dc0..5097009 100644 --- a/sys/libfont/test.c +++ b/sys/libfont/test.c @@ -2,16 +2,128 @@ #include #include +#include +#include +#include FT_FREETYPE_H /* ugh */ + #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" #define DIV 2 #define W 1920/DIV -#define H 1080 -#define L H/80 +#define H 1080/2 +#define L H/35 + +static char *phrase = "abcdefghijklmnopqrstuvwxyx"; +static byte *fontpath = "/home/nolln/root/data/Inconsolata-Regular.ttf"; + +static FT_Library lib; +static FT_Face face; + +static +void +loadfonts(int size) +{ + FT_New_Face(lib, (const byte*)fontpath, 0, &face); + FT_Set_Char_Size(face, 0, size << 6, 72, 72); +} + +int +main() +{ + int i, x, y, x0, y0, c, r, err; + FT_GlyphSlot slot; + FT_Bitmap bitmap; + FT_UInt index; + uchar *pixel; + font·Info *info; + + pixel = calloc(H,W); + mmap·Reader fontfile; + + err = 0; + fontfile = mmap·open(fontpath); + if (!fontfile.len) { + panicf("fail"); + } + + // info = font·make(fontfile.ubuf, 0, mem·sys, nil); + if (!info) + panicf("failed to load info"); + + if (!FcInit()) + panicf("could not initialize fontconfig"); + if (FT_Init_FreeType(&lib)) + panicf("could not initialize freetype"); + + loadfonts(14); -static char *phrase = "strlen(info)=000"; + x0 = 10, y0 = 100; + slot = face->glyph; + for (i = 0; i < strlen(phrase); i++) { + printf("phrase[i]='%c'\n", phrase[i]); + err = FT_Load_Char(face, (rune)phrase[i], FT_LOAD_RENDER); + if (err) + panicf("failed to load font face"); + + x = x0 + slot->bitmap_left; + y = y0 - slot->bitmap_top; + printf("buffer size: [%d, %d]\n", slot->bitmap.rows, slot->bitmap.width); + for (c = 0; c < slot->bitmap.width; c++) { + for (r = 0; r < slot->bitmap.rows; r++) { + if (x+c < 0 || x+c > W || y+r < 0 || y+r > H) + continue; + pixel[(x+c)+W*(y+r)] += slot->bitmap.buffer[c + slot->bitmap.width*r]; + } + } + x0 += slot->advance.x >> 6; + y0 += slot->advance.y >> 6; + } + + // { + // float x = 0.; + // float dy = 0., dx; + // float scale; + // int ascent, descent, baseln; + // int adv, lsb, off, r0[2] = {0}, r1[2] = {0}, w, h, i, j, k ; + + // uchar *work; + + // font·vmetrics(info, &ascent, &descent, &baseln); + // scale = font·scaleheightto(info, L); + // for (i = 0; i < strlen(phrase); i++) { + // dx = x - (float)floor(x); + // font·code_hmetrics(info, phrase[i], &adv, &lsb); + // font·code_bbox_subpixel(info, phrase[i], scale, scale, dx, 0, r0, r0+1, r1, r1+1); + + // y = ascent + r0[1]; + // dy = y - (float)floor(y); + + // w = r1[0]-r0[0]; + // h = r1[1]-r0[1]; + + // work = calloc(w, h); + // font·code_fillbitmap_subpixel(info, work, w, h, w, scale, scale, dx, 0, phrase[i]); + + // off = (int)floor(x+10) + r0[0] + ((int)y-100) * W; + // for (j = 0; j < h; j++) { + // for (k = 0; k < w; k++) { + // pixel[off + k + W*j] += work[k + w*j]; + // } + // } + // free(work); + + // x += scale * adv; + // if (phrase[i+1]) + // x += scale * font·code_kernadvance(info, phrase[i], phrase[i+1]); + // } + // } + stbi_write_png("out.png", W, H, 1, pixel, W); + + exit(0); +} +#if 0 int main() { @@ -77,3 +189,4 @@ end: mmap·close(fontfile); return err; } +#endif -- cgit v1.2.1