?? ftrandom.c
字號:
struct dirent* ent; int i, max; char buffer[1025]; struct stat statb; max = 0; fcnt = 0; for ( i = 0; fontdirs[i] != NULL; ++i ) { examples = opendir( fontdirs[i] ); if ( examples == NULL ) { fprintf( stderr, "Can't open example font directory `%s'\n", fontdirs[i] ); exit( 1 ); } while ( ( ent = readdir( examples ) ) != NULL ) { snprintf( buffer, sizeof ( buffer ), "%s/%s", fontdirs[i], ent->d_name ); if ( stat( buffer, &statb ) == -1 || S_ISDIR( statb.st_mode ) ) continue; if ( extensions == NULL || extmatch( buffer, extensions ) ) { if ( fcnt >= max ) { max += 100; fontlist = realloc( fontlist, max * sizeof ( struct fontlist ) ); if ( fontlist == NULL ) { fprintf( stderr, "Can't allocate memory\n" ); exit( 1 ); } } fontlist[fcnt].name = strdup( buffer ); fontlist[fcnt].len = statb.st_size; figurefiletype( &fontlist[fcnt] ); ++fcnt; } } closedir( examples ); } if ( fcnt == 0 ) { fprintf( stderr, "Can't find matching font files.\n" ); exit( 1 ); } fontlist[fcnt].name = NULL; } static int getErrorCnt( struct fontlist* item ) { if ( error_count == 0 && error_fraction == 0 ) return 0; return error_count + ceil( error_fraction * item->len ); } static int getRandom( int low, int high ) { if ( low - high < 0x10000L ) return low + ( ( random() >> 8 ) % ( high + 1 - low ) ); return low + ( random() % ( high + 1 - low ) ); } static int copyfont( struct fontlist* item, char* newfont ) { static char buffer[8096]; FILE *good, *new; int len; int i, err_cnt; good = fopen( item->name, "r" ); if ( good == NULL ) { fprintf( stderr, "Can't open `%s'\n", item->name ); return false; } new = fopen( newfont, "w+" ); if ( new == NULL ) { fprintf( stderr, "Can't create temporary output file `%s'\n", newfont ); exit( 1 ); } while ( ( len = fread( buffer, 1, sizeof ( buffer ), good ) ) > 0 ) fwrite( buffer, 1, len, new ); fclose( good ); err_cnt = getErrorCnt( item ); for ( i = 0; i < err_cnt; ++i ) { fseek( new, getRandom( 0, item->len - 1 ), SEEK_SET ); if ( item->isbinary ) putc( getRandom( 0, 0xff ), new ); else if ( item->isascii ) putc( getRandom( 0x20, 0x7e ), new ); else { int hex = getRandom( 0, 15 ); if ( hex < 10 ) hex += '0'; else hex += 'A' - 10; putc( hex, new ); } } if ( ferror( new ) ) { fclose( new ); unlink( newfont ); return false; } fclose( new ); return true; } static int child_pid; static void abort_test( int sig ) { /* If a time-out happens, then kill the child */ kill( child_pid, SIGFPE ); write( 2, "Timeout... ", 11 ); } static void do_test( void ) { int i = getRandom( 0, fcnt - 1 ); static int test_num = 0; char buffer[1024]; sprintf( buffer, "%s/test%d", results_dir, test_num++ ); if ( copyfont ( &fontlist[i], buffer ) ) { signal( SIGALRM, abort_test ); /* Anything that takes more than 20 seconds */ /* to parse and/or rasterize is an error. */ alarm( 20 ); if ( ( child_pid = fork() ) == 0 ) ExecuteTest( buffer ); else if ( child_pid != -1 ) { int status; waitpid( child_pid, &status, 0 ); alarm( 0 ); if ( WIFSIGNALED ( status ) ) printf( "Error found in file `%s'\n", buffer ); else unlink( buffer ); } else { fprintf( stderr, "Can't fork test case.\n" ); exit( 1 ); } alarm( 0 ); } } static void usage( FILE* out, char* name ) { fprintf( out, "%s [options] -- Generate random erroneous fonts\n" " and attempt to parse them with FreeType.\n\n", name ); fprintf( out, " --all All non-directory files are assumed to be fonts.\n" ); fprintf( out, " --check-outlines Make sure we can parse the outlines of each glyph.\n" ); fprintf( out, " --dir <path> Append <path> to list of font search directories.\n" ); fprintf( out, " --error-count <cnt> Introduce <cnt> single byte errors into each font.\n" ); fprintf( out, " --error-fraction <frac> Introduce <frac>*filesize single byte errors\n" " into each font.\n" ); fprintf( out, " --ext <ext> Add <ext> to list of extensions indicating fonts.\n" ); fprintf( out, " --help Print this.\n" ); fprintf( out, " --nohints Turn off hinting.\n" ); fprintf( out, " --rasterize Attempt to rasterize each glyph.\n" ); fprintf( out, " --results <dir> Directory in which to place the test fonts.\n" ); fprintf( out, " --size <float> Use the given font size for the tests.\n" ); fprintf( out, " --test <file> Run a single test on an already existing file.\n" ); } int main( int argc, char** argv ) { char **dirs, **exts; char *pt, *end; int dcnt = 0, ecnt = 0, rset = false, allexts = false; int i; time_t now; char* testfile = NULL; dirs = calloc( argc + 1, sizeof ( char ** ) ); exts = calloc( argc + 1, sizeof ( char ** ) ); for ( i = 1; i < argc; ++i ) { pt = argv[i]; if ( pt[0] == '-' && pt[1] == '-' ) ++pt; if ( strcmp( pt, "-all" ) == 0 ) allexts = true; else if ( strcmp( pt, "-check-outlines" ) == 0 ) check_outlines = true; else if ( strcmp( pt, "-dir" ) == 0 ) dirs[dcnt++] = argv[++i]; else if ( strcmp( pt, "-error-count" ) == 0 ) { if ( !rset ) error_fraction = 0; rset = true; error_count = strtol( argv[++i], &end, 10 ); if ( *end != '\0' ) { fprintf( stderr, "Bad value for error-count: %s\n", argv[i] ); exit( 1 ); } } else if ( strcmp( pt, "-error-fraction" ) == 0 ) { if ( !rset ) error_count = 0; rset = true; error_fraction = strtod( argv[++i], &end ); if ( *end != '\0' ) { fprintf( stderr, "Bad value for error-fraction: %s\n", argv[i] ); exit( 1 ); } } else if ( strcmp( pt, "-ext" ) == 0 ) exts[ecnt++] = argv[++i]; else if ( strcmp( pt, "-help" ) == 0 ) { usage( stdout, argv[0] ); exit( 0 ); } else if ( strcmp( pt, "-nohints" ) == 0 ) nohints = true; else if ( strcmp( pt, "-rasterize" ) == 0 ) rasterize = true; else if ( strcmp( pt, "-results" ) == 0 ) results_dir = argv[++i]; else if ( strcmp( pt, "-size" ) == 0 ) { font_size = (FT_F26Dot6)( strtod( argv[++i], &end ) * 64 ); if ( *end != '\0' || font_size < 64 ) { fprintf( stderr, "Bad value for size: %s\n", argv[i] ); exit( 1 ); } } else if ( strcmp( pt, "-test" ) == 0 ) testfile = argv[++i]; else { usage( stderr, argv[0] ); exit( 1 ); } } if ( allexts ) exts = NULL; else if ( ecnt == 0 ) exts = default_ext_list; if ( dcnt == 0 ) dirs = default_dir_list; if ( testfile != NULL ) ExecuteTest( testfile ); /* This should never return */ time( &now ); srandom( now ); FindFonts( dirs, exts ); mkdir( results_dir, 0755 ); forever do_test(); return 0; }/* EOF */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -