FTDemo_Handle* FTDemo_New( void ) { FTDemo_Handle* handle; handle = (FTDemo_Handle *)malloc( sizeof ( FTDemo_Handle ) ); if ( !handle ) return NULL; memset( handle, 0, sizeof ( FTDemo_Handle ) ); error = FT_Init_FreeType( &handle->library ); if ( error ) PanicZ( "could not initialize FreeType" ); error = FTC_Manager_New( handle->library, 0, 0, 0, my_face_requester, 0, &handle->cache_manager ); if ( error ) PanicZ( "could not initialize cache manager" ); error = FTC_SBitCache_New( handle->cache_manager, &handle->sbits_cache ); if ( error ) PanicZ( "could not initialize small bitmaps cache" ); error = FTC_ImageCache_New( handle->cache_manager, &handle->image_cache ); if ( error ) PanicZ( "could not initialize glyph image cache" ); error = FTC_CMapCache_New( handle->cache_manager, &handle->cmap_cache ); if ( error ) PanicZ( "could not initialize charmap cache" ); FT_Bitmap_New( &handle->bitmap ); FT_Stroker_New( handle->library, &handle->stroker ); handle->encoding = FT_ENCODING_NONE; handle->hinted = 1; handle->antialias = 1; handle->use_sbits = 1; handle->autohint = 0; handle->lcd_mode = 0; handle->color = 1; handle->use_sbits_cache = 1; /* string_init */ memset( handle->string, 0, sizeof ( TGlyph ) * MAX_GLYPHS ); handle->string_length = 0; handle->string_reload = 1; return handle; }
static void Fatal( const char* message ) { FTDemo_Display_Done( display ); FTDemo_Done( handle ); PanicZ( message ); }
static void write_header( FT_Error error_code ) { FT_Face face; const char* basename; error = FTC_Manager_LookupFace( handle->cache_manager, handle->scaler.face_id, &face ); if ( error ) PanicZ( "can't access font file" ); if ( !status.header ) { basename = ft_basename( handle->current_font->filepathname ); switch ( error_code ) { case FT_Err_Ok: sprintf( status.header_buffer, "%.50s %.50s (file `%.100s')", face->family_name, face->style_name, basename ); break; case FT_Err_Invalid_Pixel_Size: sprintf( status.header_buffer, "Invalid pixel size (file `%.100s')", basename ); break; case FT_Err_Invalid_PPem: sprintf( status.header_buffer, "Invalid ppem value (file `%.100s')", basename ); break; default: sprintf( status.header_buffer, "File `%.100s': error 0x%04x", basename, (FT_UShort)error_code ); break; } status.header = status.header_buffer; } grWriteCellString( display->bitmap, 0, 0, status.header, display->fore_color ); sprintf( status.header_buffer, "at %g points, angle = %d", status.ptsize / 64.0, status.angle ); grWriteCellString( display->bitmap, 0, CELLSTRING_HEIGHT, status.header_buffer, display->fore_color ); grRefreshSurface( display->surface ); }
/* Initialize the display bitmap named `bit' */ static void Init_Display( void ) { grInitDevices(); bit.mode = gr_pixel_mode_gray; bit.width = width; bit.rows = height; bit.grays = 256; surface = grNewSurface( 0, &bit ); if ( !surface ) PanicZ( "could not allocate display surface\n" ); graph_init = 1; }
int main( void ) { FTDemo_Display* display; grEvent dummy; display = FTDemo_Display_New( gr_pixel_mode_rgb24 ); if ( !display ) { PanicZ( "could not allocate display surface" ); } grSetTitle( display->surface, "FreeType Gamma Matcher" ); Render_GammaGrid( display->bitmap ); grRefreshSurface( display->surface ); grListenSurface( display->surface, 0, &dummy ); exit( 0 ); /* for safety reasons */ return 0; /* never reached */ }
int main( int argc, char** argv ) { grEvent event; /* Initialize engine */ handle = FTDemo_New(); parse_cmdline( &argc, &argv ); handle->encoding = status.encoding; handle->use_sbits = 0; FTDemo_Update_Current_Flags( handle ); for ( ; argc > 0; argc--, argv++ ) { error = FTDemo_Install_Font( handle, argv[0], 0 ); if ( error ) { fprintf( stderr, "failed to install %s", argv[0] ); if ( error == FT_Err_Invalid_CharMap_Handle ) fprintf( stderr, ": missing valid charmap\n" ); else fprintf( stderr, "\n" ); } } if ( handle->num_fonts == 0 ) PanicZ( "could not open any font file" ); display = FTDemo_Display_New( gr_pixel_mode_gray, status.width, status.height ); display->back_color.value = 0; display->fore_color.value = 0xff; if ( !display ) PanicZ( "could not allocate display surface" ); grSetTitle( display->surface, "FreeType String Viewer - press ? for help" ); event_gamma_change( 0 ); event_font_change( 0 ); status.header = 0; for ( ;; ) { FTDemo_Display_Clear( display ); switch ( status.render_mode ) { case RENDER_MODE_STRING: status.sc.center = 1L << 15; error = FTDemo_String_Draw( handle, display, &status.sc, display->bitmap->width / 2, display->bitmap->rows / 2 ); break; case RENDER_MODE_KERNCMP: { FTDemo_String_Context sc = status.sc; FT_Int x, y; FT_Int height; x = 55; height = ( status.ptsize * status.res / 72 + 32 ) >> 6; if ( height < CELLSTRING_HEIGHT ) height = CELLSTRING_HEIGHT; /* First line: none */ sc.center = 0; sc.kerning_mode = 0; sc.kerning_degree = 0; sc.vertical = 0; sc.matrix = NULL; y = CELLSTRING_HEIGHT * 2 + display->bitmap->rows / 4 + height; grWriteCellString( display->bitmap, 5, y - ( height + CELLSTRING_HEIGHT ) / 2, "none", display->fore_color ); error = FTDemo_String_Draw( handle, display, &sc, x, y ); /* Second line: track kern only */ sc.kerning_degree = status.sc.kerning_degree; y += height; grWriteCellString( display->bitmap, 5, y - ( height + CELLSTRING_HEIGHT ) / 2, "track", display->fore_color ); error = FTDemo_String_Draw( handle, display, &sc, x, y ); /* Third line: track kern + pair kern */ sc.kerning_mode = status.sc.kerning_mode; y += height; grWriteCellString( display->bitmap, 5, y - ( height + CELLSTRING_HEIGHT ) / 2, "both", display->fore_color ); error = FTDemo_String_Draw( handle, display, &sc, x, y ); } break; } if ( !error && status.sc.gamma_ramp ) gamma_ramp_draw( status.gamma_ramp, display->bitmap ); write_header( error ); status.header = 0; grListenSurface( display->surface, 0, &event ); if ( Process_Event( &event ) ) break; } printf( "Execution completed successfully.\n" ); FTDemo_Display_Done( display ); FTDemo_Done( handle ); exit( 0 ); /* for safety reasons */ /* return 0; */ /* never reached */ }
int main( int argc, char* argv[] ) { int i, file; char filename[128 + 4]; char alt_filename[128 + 4]; char* execname; int num_faces; int option; FT_Library library; /* the FreeType library */ FT_Face face; /* the font face */ execname = ft_basename( argv[0] ); while ( 1 ) { option = getopt( argc, argv, "dl:nv" ); if ( option == -1 ) break; switch ( option ) { case 'd': debug = 1; break; case 'l': trace_level = atoi( optarg ); if ( trace_level < 1 || trace_level > 7 ) usage( execname ); break; case 'n': name_tables = 1; break; case 'v': verbose = 1; break; default: usage( execname ); break; } } argc -= optind; argv += optind; if ( argc != 1 ) usage( execname ); #if FREETYPE_MAJOR == 2 && FREETYPE_MINOR == 0 && FREETYPE_PATCH <= 8 if ( debug ) { # ifdef FT_DEBUG_LEVEL_TRACE FT_SetTraceLevel( trace_any, (FT_Byte)trace_level ); # else trace_level = 0; # endif } #elif 0 /* "setenv/putenv" is not ANSI and I don't want to mess */ /* with this portability issue right now */ if ( debug ) { char temp[32]; sprintf( temp, "any=%d", trace_level ); setenv( "FT2_DEBUG", temp ); } #endif file = 0; /* Initialize engine */ error = FT_Init_FreeType( &library ); if ( error ) PanicZ( "Could not initialize FreeType library" ); filename[128] = '\0'; alt_filename[128] = '\0'; strncpy( filename, argv[file], 128 ); strncpy( alt_filename, argv[file], 128 ); /* try to load the file name as is, first */ error = FT_New_Face( library, argv[file], 0, &face ); if ( !error ) goto Success; #ifndef macintosh i = strlen( argv[file] ); while ( i > 0 && argv[file][i] != '\\' && argv[file][i] != '/' ) { if ( argv[file][i] == '.' ) i = 0; i--; } if ( i >= 0 ) { strncpy( filename + strlen( filename ), ".ttf", 4 ); strncpy( alt_filename + strlen( alt_filename ), ".ttc", 4 ); } #endif /* Load face */ error = FT_New_Face( library, filename, 0, &face ); if ( error ) PanicZ( "Could not open face." ); Success: num_faces = face->num_faces; FT_Done_Face( face ); printf( "There %s %d %s in this file.\n", num_faces == 1 ? (char *)"is" : (char *)"are", num_faces, num_faces == 1 ? (char *)"face" : (char *)"faces" ); for ( i = 0; i < num_faces; i++ ) { error = FT_New_Face( library, filename, i, &face ); if ( error ) PanicZ( "Could not open face." ); printf( "\n----- Face number: %d -----\n\n", i ); Print_Name( face ); printf( "\n" ); Print_Type( face ); printf( " glyph count: %ld\n", face->num_glyphs ); if ( name_tables && FT_IS_SFNT( face ) ) { printf( "\n" ); Print_Sfnt_Names( face ); } if ( face->num_fixed_sizes ) { printf( "\n" ); Print_Fixed( face ); } if ( face->num_charmaps ) { printf( "\n" ); Print_Charmaps( face ); } FT_Done_Face( face ); } FT_Done_FreeType( library ); exit( 0 ); /* for safety reasons */ return 0; /* never reached */ }
FT_Error FTDemo_Glyph_To_Bitmap( FTDemo_Handle* handle, FT_Glyph glyf, grBitmap* target, int* left, int* top, int* x_advance, int* y_advance, FT_Glyph* aglyf ) { FT_BitmapGlyph bitmap; FT_Bitmap* source; *aglyf = NULL; error = FT_Err_Ok; if ( glyf->format == FT_GLYPH_FORMAT_OUTLINE ) { FT_Render_Mode render_mode = FT_RENDER_MODE_MONO; if ( handle->antialias ) { if ( handle->lcd_mode == 0 ) render_mode = FT_RENDER_MODE_NORMAL; else if ( handle->lcd_mode == 1 ) render_mode = FT_RENDER_MODE_LIGHT; else if ( handle->lcd_mode <= 3 ) render_mode = FT_RENDER_MODE_LCD; else render_mode = FT_RENDER_MODE_LCD_V; } /* render the glyph to a bitmap, don't destroy original */ error = FT_Glyph_To_Bitmap( &glyf, render_mode, NULL, 0 ); if ( error ) return error; *aglyf = glyf; } if ( glyf->format != FT_GLYPH_FORMAT_BITMAP ) PanicZ( "invalid glyph format returned!" ); bitmap = (FT_BitmapGlyph)glyf; source = &bitmap->bitmap; target->rows = source->rows; target->width = source->width; target->pitch = source->pitch; target->buffer = source->buffer; target->grays = source->num_grays; switch ( source->pixel_mode ) { case FT_PIXEL_MODE_MONO: target->mode = gr_pixel_mode_mono; break; case FT_PIXEL_MODE_GRAY: target->mode = gr_pixel_mode_gray; target->grays = source->num_grays; break; case FT_PIXEL_MODE_GRAY2: case FT_PIXEL_MODE_GRAY4: (void)FT_Bitmap_Convert( handle->library, source, &handle->bitmap, 1 ); target->pitch = handle->bitmap.pitch; target->buffer = handle->bitmap.buffer; target->mode = gr_pixel_mode_gray; target->grays = handle->bitmap.num_grays; break; case FT_PIXEL_MODE_LCD: target->mode = handle->lcd_mode == 2 ? gr_pixel_mode_lcd : gr_pixel_mode_lcd2; target->grays = source->num_grays; break; case FT_PIXEL_MODE_LCD_V: target->mode = handle->lcd_mode == 4 ? gr_pixel_mode_lcdv : gr_pixel_mode_lcdv2; target->grays = source->num_grays; break; case FT_PIXEL_MODE_BGRA: target->mode = gr_pixel_mode_bgra; target->grays = source->num_grays; break; default: return FT_Err_Invalid_Glyph_Format; } *left = bitmap->left; *top = bitmap->top; *x_advance = ( glyf->advance.x + 0x8000 ) >> 16; *y_advance = ( glyf->advance.y + 0x8000 ) >> 16; return error; }