Translate

Testing glyph data extraction using FreeType:

Similar methods can be used to achieve many interesting ideas!


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#include <ft2build.h>

#include FT_FREETYPE_H

#include <wchar.h>

#include <sys/time.h>

#include <time.h>

#include <unistd.h>

#include <sys/ioctl.h>

FT_Bitmap getBitMap(FT_Library library, FT_Face face, int charIndex) {

    FT_Bitmap bitmap;

    FT_GlyphSlot slot;

    FT_Error error;

    error = FT_Load_Glyph(face, charIndex, FT_LOAD_DEFAULT);

    if (error) {

        printf("Failed to load glyph\n");

        exit(1);

    }

    slot = face->glyph;

    error = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);

    if (error) {

        printf("Failed to render glyph\n");

        exit(1);

    }

    bitmap.buffer = slot->bitmap.buffer;

    bitmap.width = slot->bitmap.width;

    bitmap.rows = slot->bitmap.rows;

    return bitmap;

}

void print_bitmap(FT_Bitmap *bitmap) {

    int x, y;

    for (y = 0; y < bitmap->rows; y++) {

        for (x = 0; x < bitmap->width; x++) {

            printf(bitmap->buffer[x + bitmap->width * y] ? "##" : "  ");

        }

        printf("\n");

    }

}

int main() {

    struct winsize sSize;

	ioctl(STDOUT_FILENO, TIOCGWINSZ, &sSize);

    FT_Library library;

    FT_Face face;

    FT_Error error;

    FT_UInt charIndex;

    

  const  char font_file[] = "./SysSans-Hans-Regular.ttf";

    int font_size = (sSize.ws_row-3)/3;

    

    error = FT_Init_FreeType(&library);

    if (error) {

        printf("Failed to initialize FreeType library\n");

        return 1;

    }

    error = FT_New_Face(library, font_file, 0, &face);

    if (error == FT_Err_Unknown_File_Format) {

        printf("Unsupported font file format\n");

        return 1;

    } else if (error) {

        printf("Failed to open font file\n");

        return 1;

    }

    error = FT_Set_Pixel_Sizes(face, 0, font_size);

    if (error) {

        printf("Failed to set font size\n");

        return 1;

    }

    

    for (int i = 0; i < 100; i++) {

        system("clear");

        srand(clock());

        int k=rand()%(127-33);

        //0x4e00 -0x9fff Chinese

      

            charIndex = FT_Get_Char_Index(face, k+34);

            FT_Bitmap bitmap = getBitMap(library, face, charIndex);

            print_bitmap(&bitmap);

            printf("\n");

           

            

            

            

            srand(clock());

        k=rand()%(218-166);

       

            charIndex = FT_Get_Char_Index(face, k+166);

            bitmap = getBitMap(library, face, charIndex);

            print_bitmap(&bitmap);

            printf("\n");

            

            

            srand(clock());

        k=rand()%(0x9fff-0x4e00);

       

            charIndex = FT_Get_Char_Index(face, k+0x4e00);

            bitmap = getBitMap(library, face, charIndex);

            print_bitmap(&bitmap);

            printf("\n");

            

            sleep(1);

         

        

    }

    

    FT_Done_Face(face);

    FT_Done_FreeType(library);

    return 0;

}

沒有留言:

發佈留言