freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Example Code


From: Pedriana, Paul
Subject: RE: Example Code
Date: Wed, 2 Aug 2000 16:27:23 -0700

Here's some example code that I wrote that loads a character 
and prints it to a console display. It even makes a small 
attempt to interpret the anti-aliasing values. The entire 
example is implemented in one function: main(). 

Paul




////////////////////////////////////////////////////////////////////////////
///
// Include files
//
#include <freetype/freetype.h>
#include <freetype/ftglyph.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>


////////////////////////////////////////////////////////////////////////////
///
// main
//
int main(int argc, char* argv[]){
   FT_Library   library;   // handle to library
   FT_Face      face;      // handle to face object
   FT_Error     nError;
   unsigned     nGlyphIndex;
   bool         bAntialias(false);

   nError = FT_Init_FreeType(&library);
   if(nError == 0){
      nError = FT_New_Face(library, "courier.ttf", 0, &face);
      if(nError == FT_Err_Unknown_File_Format){
        //.... the font file could be opened and read, but it 
        //.... appears that its font format is unsupported
      }
      else if(nError){
         //.... another error code means that the font file could not
         //.... be opened, read or simply that it is broken..
      }
      else{
         nError = FT_Set_Pixel_Sizes(face, 64,  64);
         if(nError == 0){
            nGlyphIndex = FT_Get_Char_Index(face, (FT_ULong)'a');
            if(nGlyphIndex){
               FT_Vector origin = { 0, 0 };
               FT_BitmapGlyph pBitmapglyph;
               nError = FT_Get_Glyph_Bitmap(face, nGlyphIndex,
FT_LOAD_DEFAULT, bAntialias ? 128 : 0, &origin, &pBitmapglyph);
               if(nError == 0){
                  printf("Char height: %d, Char width: %d.\n",
pBitmapglyph->bitmap.rows, pBitmapglyph->bitmap.width);
                  for(int y(0); y<pBitmapglyph->bitmap.rows; y++){
                     for(int x(0); x<pBitmapglyph->bitmap.width; x++){
                        char c =
((char*)pBitmapglyph->bitmap.buffer)[y*pBitmapglyph->bitmap.pitch+x];
                        if(c == 127)
                           printf("M"); 
                        else if(c > 110)
                           printf("W"); 
                        else if(c > 90)
                           printf("8");
                        else if(c > 70)
                           printf("5");
                        else if(c > 50)
                           printf("[");
                        else if(c > 30)
                           printf("+");
                        else if(c > 10)
                           printf("-");
                        else
                           printf(" ");
                     }
                     printf("\n");
                  }
                  printf("\n");

                  pBitmapglyph->bitmap.rows;
                  pBitmapglyph->bitmap.width;
                  pBitmapglyph->bitmap.pitch;
                  pBitmapglyph->bitmap.buffer;
                  pBitmapglyph->bitmap.num_grays;
                  pBitmapglyph->bitmap.pixel_mode;
                  pBitmapglyph->bitmap.palette_mode;
                  pBitmapglyph->bitmap.palette;

                  FT_Done_Glyph((FT_Glyph)pBitmapglyph);
               }
            }
         }
         FT_Done_Face(face);
      }
      FT_Done_FreeType(library);
   }

   //Shutdown
   printf("Press any key.\n");
   getchar();
   return 0;
}



















reply via email to

[Prev in Thread] Current Thread [Next in Thread]