Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.93 KB | None | 0 0
  1. /* example1.c                                                         */
  2. /*                                                                    */
  3. /* This small program shows how to print a string with the            */
  4. /* FreeType 2 library.                                                */
  5. /*                                                                    */
  6. /* sumber https://www.freetype.org/freetype2/docs/tutorial/example1.c */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <string.h>                       /* strlen() */
  11. #include <ft2build.h>
  12. #include FT_FREETYPE_H
  13.  
  14. #define WIDTH   32
  15. #define HEIGHT  32
  16.  
  17.  
  18. // ukuran gambar yang akan tampil pada konsol
  19. unsigned char image[HEIGHT][WIDTH];
  20.  
  21.  
  22. // mengambil data buffer pixel bitmap dan menyalin ke variabel image
  23. void draw_bitmap(FT_Bitmap* bitmap, FT_Int  x, FT_Int y){
  24.     FT_Int  i, j, p, q;
  25.     FT_Int  x_max = x + bitmap->width;
  26.     FT_Int  y_max = y + bitmap->rows;
  27.  
  28.  
  29.     for(i = x, p = 0; i < x_max; i++, p++){
  30.         for(j = y, q = 0; j < y_max; j++, q++){
  31.             if(i < 0 || j < 0 || i >= WIDTH || j >= HEIGHT)
  32.                 continue;
  33.  
  34.             image[j][i] = bitmap->buffer[q * bitmap->width + p];
  35.         }
  36.     }
  37. }
  38.  
  39. // mencetak variabel image ke konsol
  40. void show_image(){
  41.     int  i, j;
  42.    
  43.     printf("{\n");
  44.     for(i = 0; i < HEIGHT; i++){
  45.         for(j = 0; j < WIDTH; j++){
  46.  
  47.             if(j == 0)
  48.                 printf("    {");
  49.  
  50.             if(image[i][j] < 10)
  51.                 printf(" %d", image[i][j]);
  52.             else if(image[i][j] < 100)
  53.                 printf(" %d", image[i][j]);
  54.             else
  55.                 printf(" %d", image[i][j]);
  56.  
  57.             if(j != WIDTH - 1)
  58.                 printf(", ");
  59.         }
  60.        
  61.         if(i == HEIGHT - 1)
  62.             printf("}\n}\n");
  63.         else
  64.             printf("},\n");
  65.     }
  66. }
  67.  
  68. // main program
  69. int main(int argc, char**  argv){
  70.     FT_Library    library;
  71.     FT_Face       face;
  72.  
  73.     FT_GlyphSlot  slot;
  74.     FT_Vector     pen;                    /* untransformed origin  */
  75.     FT_Error      error;
  76.  
  77.     char*         filename;
  78.     char*         text;
  79.  
  80.     int           target_height;
  81.     int           n, num_chars;
  82.     int           font_size;
  83.  
  84.     if(argc != 3){
  85.         fprintf(stderr, "usage: %s font sample-text\n", argv[0]);
  86.         exit(1);
  87.     }
  88.  
  89.     filename      = argv[1];                            /* first argument     */
  90.     text          = argv[2];                            /* second argument    */
  91.     num_chars     = strlen(text);
  92.     font_size     = 32;                                 /* ukuran huruf       */
  93.     target_height = font_size;
  94.  
  95.     error = FT_Init_FreeType(&library);                 /* initialize library */
  96.  
  97.     // TODO: tambahkan error handling
  98.  
  99.     error = FT_New_Face(library, filename, 0, &face);   /* create face object */
  100.  
  101.     // TODO: tambahkan error handling
  102.  
  103.     // mengeset ukuran huruf dan density nya
  104.     // ukuran: 10 pt(titik)
  105.     // density: 100 dpi
  106.     error = FT_Set_Char_Size(face, font_size * 64, 0, 92, 0); /* set character size */
  107.    
  108.     // TODO: tambahkan error handling
  109.  
  110.     slot = face->glyph;
  111.  
  112.     pen.x = 0 * 64; // positif ke kanan
  113.     pen.y = 0 * 64; // positif ke atas
  114.  
  115.     for(n = 0; n < num_chars; n++){
  116.  
  117.         /* no transformation */
  118.         FT_Set_Transform(face, NULL, &pen);
  119.  
  120.         /* load glyph image into the slot (erase previous one) */
  121.         error = FT_Load_Char(face, text[n], FT_LOAD_RENDER);
  122.         if(error)
  123.             continue;            /* ignore errors, TODO: tambahakan error handling */
  124.  
  125.         /* now, draw to our target surface (convert position) */
  126.         draw_bitmap(&slot->bitmap, slot->bitmap_left, target_height - slot->bitmap_top - 1);
  127.  
  128.         /* increment pen position */
  129.         pen.x += slot->advance.x;
  130.         pen.y += slot->advance.y;
  131.     }
  132.  
  133.     show_image();
  134.  
  135.     FT_Done_Face   (face);
  136.     FT_Done_FreeType(library);
  137.  
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement