Jason (jcreed) wrote,
Jason
jcreed

Using freetype isn't as scary as I thought. You just do something like

#include <ft2build.h>  
#include FT_FREETYPE_H

void handle_error(int error) {
  if (error) {
    printf("Error: %d\n", error);
    exit(1);
  }
}

int main(void) {
  FT_Library library; 
  FT_Face face;
  int glyph_index;

  handle_error(FT_Init_FreeType( &library ));
  handle_error(FT_New_Face(library, "Austin.ttf", 0, &face));
  handle_error(FT_Set_Pixel_Sizes( face,  0, 36 ));
  glyph_index = FT_Get_Char_Index( face, '@' );
  handle_error(FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT));
  handle_error(FT_Render_Glyph( face->glyph, FT_RENDER_MODE_NORMAL));
  {
    int pitch = face->glyph->bitmap.pitch;
    int rows = face->glyph->bitmap.rows;
    unsigned char *buffer = face->glyph->bitmap.buffer;
    int x, y;
    for (y = 0; y < rows; y++) {
      for (x = 0; x < pitch; x++) {
	unsigned char c = buffer[y * pitch + x];
	if (c > 192) printf("#");
	else if (c > 128) printf ("*");
	else if (c > 64) printf (".");
	else printf (" ");
	printf(" ");
      } 
      printf("\n");
    } 
  }
}

and you get
                    . * * * * .               
                * # # # # # # # # *           
            . # # * .       . * # # # .       
          * # *                   # # # .     
        * # .                       # # #     
      * # .           . .     .       # # *   
      # *         * # # # # # # .     * # #   
    * #         * # # * . # # #       . # #   
    # #       . # # .     * # #         # # . 
  * # *       # # #       # # *         # # . 
  # # *       # # .       # # .         # # . 
  # # .     . # #         # # .         # # . 
  # # .     . # #       . # #           # #   
  # # *     . # #       . # #         . # #   
  # # #     . # # .     * # # .       # # .   
  # # #       # # # * * # # # # . . # # #     
  * # # *     . # # # # . . # # # # # #       
    # # # .       . .         * * * .         
    * # # #                                   
      * # # # .                               
        * # # # # . .       . . * # # # .     
          . # # # # # # # # # # # # # .       
              . * # # # # # # * .             
                                             
Tags: freetype, hacking, wasting time
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 5 comments