#include #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/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); 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() { int i, j, k, err; float x, dx, dy, scale, sx, sy; uchar *bitmap, *work; font·Info *info; mmap·Reader fontfile; int w, h, off, y, ascent, descent, baseln; int adv, lsb, r0[2], r1[2]; err = 0; fontfile = mmap·open("/home/nolln/root/data/Inconsolata-Regular.ttf"); if (!fontfile.len) { err = 1; goto end; } info = font·make(fontfile.ubuf, 0, mem·sys, nil); if (!info) panicf("failed to load info"); bitmap = calloc(W*H, sizeof(*bitmap)); scale = font·scaleheightto(info, L); font·vmetrics(info, &ascent, &descent, &baseln); ascent *= scale; x = 0.; dy = 0.; 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, sizeof(*bitmap)); font·code_fillbitmap_subpixel_prefilter(info, work, w, h, w, scale, scale, dx, 0, 1, 1, &sx, &sy, phrase[i]); off = (int)floor(x+sx) + r0[0] + ((int)floor(sy)+y) * W; for (j = 0; j < h; j++) { for (k = 0; k < w; k++) { bitmap[off + W*j + k] += 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, bitmap, W); font·free(info); free(bitmap); end: mmap·close(fontfile); return err; } #endif