Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* example1.c */
- /* */
- /* This small program shows how to print a string with the */
- /* FreeType 2 library. */
- /* */
- /* sumber https://www.freetype.org/freetype2/docs/tutorial/example1.c */
- #include <stdio.h>
- #include <string.h> /* strlen() */
- #include <ft2build.h>
- #include FT_FREETYPE_H
- #define WIDTH 32
- #define HEIGHT 32
- // ukuran gambar yang akan tampil pada konsol
- unsigned char image[HEIGHT][WIDTH];
- // mengambil data buffer pixel bitmap dan menyalin ke variabel image
- void draw_bitmap(FT_Bitmap* bitmap, FT_Int x, FT_Int y){
- FT_Int i, j, p, q;
- FT_Int x_max = x + bitmap->width;
- FT_Int y_max = y + bitmap->rows;
- for(i = x, p = 0; i < x_max; i++, p++){
- for(j = y, q = 0; j < y_max; j++, q++){
- if(i < 0 || j < 0 || i >= WIDTH || j >= HEIGHT)
- continue;
- image[j][i] = bitmap->buffer[q * bitmap->width + p];
- }
- }
- }
- // mencetak variabel image ke konsol
- void show_image(){
- int i, j;
- printf("{\n");
- for(i = 0; i < HEIGHT; i++){
- for(j = 0; j < WIDTH; j++){
- if(j == 0)
- printf(" {");
- if(image[i][j] < 10)
- printf(" %d", image[i][j]);
- else if(image[i][j] < 100)
- printf(" %d", image[i][j]);
- else
- printf(" %d", image[i][j]);
- if(j != WIDTH - 1)
- printf(", ");
- }
- if(i == HEIGHT - 1)
- printf("}\n}\n");
- else
- printf("},\n");
- }
- }
- // main program
- int main(int argc, char** argv){
- FT_Library library;
- FT_Face face;
- FT_GlyphSlot slot;
- FT_Vector pen; /* untransformed origin */
- FT_Error error;
- char* filename;
- char* text;
- int target_height;
- int n, num_chars;
- int font_size;
- if(argc != 3){
- fprintf(stderr, "usage: %s font sample-text\n", argv[0]);
- exit(1);
- }
- filename = argv[1]; /* first argument */
- text = argv[2]; /* second argument */
- num_chars = strlen(text);
- font_size = 32; /* ukuran huruf */
- target_height = font_size;
- error = FT_Init_FreeType(&library); /* initialize library */
- // TODO: tambahkan error handling
- error = FT_New_Face(library, filename, 0, &face); /* create face object */
- // TODO: tambahkan error handling
- // mengeset ukuran huruf dan density nya
- // ukuran: 10 pt(titik)
- // density: 100 dpi
- error = FT_Set_Char_Size(face, font_size * 64, 0, 92, 0); /* set character size */
- // TODO: tambahkan error handling
- slot = face->glyph;
- pen.x = 0 * 64; // positif ke kanan
- pen.y = 0 * 64; // positif ke atas
- for(n = 0; n < num_chars; n++){
- /* no transformation */
- FT_Set_Transform(face, NULL, &pen);
- /* load glyph image into the slot (erase previous one) */
- error = FT_Load_Char(face, text[n], FT_LOAD_RENDER);
- if(error)
- continue; /* ignore errors, TODO: tambahakan error handling */
- /* now, draw to our target surface (convert position) */
- draw_bitmap(&slot->bitmap, slot->bitmap_left, target_height - slot->bitmap_top - 1);
- /* increment pen position */
- pen.x += slot->advance.x;
- pen.y += slot->advance.y;
- }
- show_image();
- FT_Done_Face (face);
- FT_Done_FreeType(library);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement