36 #include <sys/types.h> 46 #include FT_FREETYPE_H 51 #define forever for (;;) 54 static int check_outlines =
false;
55 static int nohints =
false;
56 static int rasterize =
false;
57 static char* results_dir =
"results";
59 #define GOOD_FONTS_DIR "/home/wl/freetype-testfonts" 61 static char* default_dir_list[] =
67 static char* default_ext_list[] =
84 static int error_count = 1;
85 static int error_fraction = 0;
89 static struct fontlist
93 unsigned int isbinary: 1;
94 unsigned int isascii: 1;
95 unsigned int ishex: 1;
154 if ( check_outlines &&
163 for ( gid = 0; gid < face->
num_glyphs; ++gid )
165 if ( check_outlines &&
183 ExecuteTest(
char* testfont )
192 fprintf( stderr,
"Can't initialize FreeType.\n" );
209 for ( i = 0; i <
num; ++
i )
228 if ( extensions ==
NULL )
234 if ( pt < strrchr(
filename,
'/' ) )
237 for ( i = 0; extensions[
i] !=
NULL; ++
i )
238 if ( strcasecmp( pt + 1, extensions[i] ) == 0 ||
239 strcasecmp( pt, extensions[i] ) == 0 )
247 figurefiletype(
struct fontlist* item )
252 item->isbinary = item->isascii = item->ishex =
false;
254 foo = fopen( item->name,
"rb" );
258 int ch1 = getc( foo );
259 int ch2 = getc( foo );
260 int ch3 = getc( foo );
261 int ch4 = getc( foo );
266 if ( ( ch1 == 0 && ch2 == 1 && ch3 == 0 && ch4 == 0 ) ||
267 ( ch1 ==
'O' && ch2 ==
'T' && ch3 ==
'T' && ch4 ==
'O' ) ||
268 ( ch1 ==
't' && ch2 ==
'r' && ch3 ==
'u' && ch4 ==
'e' ) ||
269 ( ch1 ==
't' && ch2 ==
't' && ch3 ==
'c' && ch4 ==
'f' ) )
272 item->isbinary =
true;
274 else if ( ch1 == 0x80 && ch2 ==
'\01' )
277 item->isbinary =
true;
279 else if ( ch1 ==
'%' && ch2 ==
'!' )
282 if ( strstr( item->name,
".pfa" ) !=
NULL ||
283 strstr( item->name,
".PFA" ) !=
NULL )
286 item->isascii =
true;
288 else if ( ch1 == 1 && ch2 == 0 && ch3 == 4 )
291 item->isbinary =
true;
293 else if ( ch1 ==
'S' && ch2 ==
'T' && ch3 ==
'A' && ch4 ==
'R' )
298 else if ( ch1 ==
'P' && ch2 ==
'F' && ch3 ==
'R' && ch4 ==
'0' )
301 item->isbinary =
true;
303 else if ( ( ch1 ==
'\1' && ch2 ==
'f' && ch3 ==
'c' && ch4 ==
'p' ) ||
304 ( ch1 ==
'M' && ch2 ==
'Z' ) )
307 item->isbinary =
true;
312 "Can't recognize file type of `%s', assuming binary\n",
314 item->isbinary =
true;
319 fprintf( stderr,
"Can't open `%s' for typing the file.\n",
321 item->isbinary =
true;
327 FindFonts(
char** fontdirs,
341 for ( i = 0; fontdirs[
i] !=
NULL; ++
i )
343 examples = opendir( fontdirs[i] );
344 if ( examples ==
NULL )
347 "Can't open example font directory `%s'\n",
352 while ( ( ent = readdir( examples ) ) !=
NULL )
354 snprintf( buffer,
sizeof ( buffer ),
355 "%s/%s", fontdirs[i], ent->d_name );
356 if ( stat( buffer, &statb ) == -1 || S_ISDIR( statb.st_mode ) )
358 if ( extensions ==
NULL || extmatch( buffer, extensions ) )
363 fontlist = realloc( fontlist, max *
sizeof (
struct fontlist ) );
364 if ( fontlist ==
NULL )
366 fprintf( stderr,
"Can't allocate memory\n" );
371 fontlist[fcnt].name = strdup( buffer );
372 fontlist[fcnt].len = statb.st_size;
374 figurefiletype( &fontlist[fcnt] );
379 closedir( examples );
384 fprintf( stderr,
"Can't find matching font files.\n" );
388 fontlist[fcnt].name =
NULL;
393 getErrorCnt(
struct fontlist* item )
395 if ( error_count == 0 && error_fraction == 0 )
398 return error_count +
ceil( error_fraction * item->len );
406 if ( low - high < 0x10000L )
407 return low + ( (
random() >> 8 ) % ( high + 1 - low ) );
409 return low + (
random() % ( high + 1 - low ) );
414 copyfont(
struct fontlist* item,
423 good = fopen( item->name,
"r" );
426 fprintf( stderr,
"Can't open `%s'\n", item->name );
430 new = fopen( newfont,
"w+" );
433 fprintf( stderr,
"Can't create temporary output file `%s'\n",
438 while ( ( len = fread( buffer, 1,
sizeof ( buffer ), good ) ) > 0 )
439 fwrite( buffer, 1, len,
new );
443 err_cnt = getErrorCnt( item );
444 for ( i = 0; i < err_cnt; ++
i )
446 fseek(
new, getRandom( 0, item->len - 1 ),
SEEK_SET );
448 if ( item->isbinary )
449 putc( getRandom( 0, 0xff ),
new );
450 else if ( item->isascii )
451 putc( getRandom( 0x20, 0x7e ),
new );
454 int hex = getRandom( 0, 15 );
479 static int child_pid;
482 abort_test(
int sig )
485 kill( child_pid, SIGFPE );
486 write( 2,
"Timeout... ", 11 );
493 int i = getRandom( 0, fcnt - 1 );
494 static int test_num = 0;
498 sprintf( buffer,
"%s/test%d", results_dir, test_num++ );
500 if ( copyfont ( &fontlist[i], buffer ) )
502 signal( SIGALRM, abort_test );
506 if ( ( child_pid = fork() ) == 0 )
507 ExecuteTest( buffer );
508 else if ( child_pid != -1 )
513 waitpid( child_pid, &status, 0 );
515 if ( WIFSIGNALED ( status ) )
516 printf(
"Error found in file `%s'\n", buffer );
522 fprintf( stderr,
"Can't fork test case.\n" );
534 fprintf(
out,
"%s [options] -- Generate random erroneous fonts\n" 535 " and attempt to parse them with FreeType.\n\n", name );
537 fprintf(
out,
" --all All non-directory files are assumed to be fonts.\n" );
538 fprintf(
out,
" --check-outlines Make sure we can parse the outlines of each glyph.\n" );
539 fprintf(
out,
" --dir <path> Append <path> to list of font search directories.\n" );
540 fprintf(
out,
" --error-count <cnt> Introduce <cnt> single byte errors into each font.\n" );
541 fprintf(
out,
" --error-fraction <frac> Introduce <frac>*filesize single byte errors\n" 542 " into each font.\n" );
543 fprintf(
out,
" --ext <ext> Add <ext> to list of extensions indicating fonts.\n" );
544 fprintf(
out,
" --help Print this.\n" );
545 fprintf(
out,
" --nohints Turn off hinting.\n" );
546 fprintf(
out,
" --rasterize Attempt to rasterize each glyph.\n" );
547 fprintf(
out,
" --results <dir> Directory in which to place the test fonts.\n" );
548 fprintf(
out,
" --size <float> Use the given font size for the tests.\n" );
549 fprintf(
out,
" --test <file> Run a single test on an already existing file.\n" );
559 int dcnt = 0, ecnt = 0, rset =
false, allexts =
false;
562 char* testfile =
NULL;
565 dirs = calloc( argc + 1,
sizeof (
char ** ) );
566 exts = calloc( argc + 1,
sizeof (
char ** ) );
568 for ( i = 1; i < argc; ++
i )
571 if ( pt[0] ==
'-' && pt[1] ==
'-' )
574 if ( strcmp( pt,
"-all" ) == 0 )
576 else if ( strcmp( pt,
"-check-outlines" ) == 0 )
577 check_outlines =
true;
578 else if ( strcmp( pt,
"-dir" ) == 0 )
579 dirs[dcnt++] = argv[++
i];
580 else if ( strcmp( pt,
"-error-count" ) == 0 )
585 error_count = strtol( argv[++i], &end, 10 );
588 fprintf( stderr,
"Bad value for error-count: %s\n", argv[i] );
592 else if ( strcmp( pt,
"-error-fraction" ) == 0 )
597 error_fraction = strtod( argv[++i], &end );
600 fprintf( stderr,
"Bad value for error-fraction: %s\n", argv[i] );
604 else if ( strcmp( pt,
"-ext" ) == 0 )
605 exts[ecnt++] = argv[++
i];
606 else if ( strcmp( pt,
"-help" ) == 0 )
608 usage( stdout, argv[0] );
611 else if ( strcmp( pt,
"-nohints" ) == 0 )
613 else if ( strcmp( pt,
"-rasterize" ) == 0 )
615 else if ( strcmp( pt,
"-results" ) == 0 )
616 results_dir = argv[++
i];
617 else if ( strcmp( pt,
"-size" ) == 0 )
619 font_size = (
FT_F26Dot6)( strtod( argv[++i], &end ) * 64 );
620 if ( *end !=
'\0' || font_size < 64 )
622 fprintf( stderr,
"Bad value for size: %s\n", argv[i] );
626 else if ( strcmp( pt,
"-test" ) == 0 )
627 testfile = argv[++
i];
630 usage( stderr, argv[0] );
637 else if ( ecnt == 0 )
638 exts = default_ext_list;
641 dirs = default_dir_list;
643 if ( testfile !=
NULL )
644 ExecuteTest( testfile );
649 FindFonts( dirs, exts );
650 mkdir( results_dir, 0755 );
int write(int fd, const char *buf, int nbytes)
#define FT_LOAD_NO_HINTING
FT_Init_FreeType(FT_Library *alibrary)
GLenum GLuint GLint GLenum face
FT_Load_Glyph(FT_Face face, FT_UInt glyph_index, FT_Int32 load_flags)
int main(int argc, char **argv)
VLCORE_EXPORT real random(real min, real max)
Returns a random number N between 'min' and 'max' (included) with 53 bits of randomness generated usi...
GLsizeiptr const GLvoid GLenum usage
FT_Set_Char_Size(FT_Face face, FT_F26Dot6 char_width, FT_F26Dot6 char_height, FT_UInt horz_resolution, FT_UInt vert_resolution)
local int out(void *out_desc, unsigned char *buf, unsigned len)
#define ft_render_mode_normal
FT_Render_Glyph(FT_GlyphSlot slot, FT_Render_Mode render_mode)
FT_BEGIN_HEADER FT_Outline_Decompose(FT_Outline *outline, const FT_Outline_Funcs *func_interface, void *user)
GLuint const GLchar * name
FT_New_Face(FT_Library library, const char *filepathname, FT_Long face_index, FT_Face *aface)
FT_Done_Face(FT_Face face)
#define FT_LOAD_NO_BITMAP
#define FT_IS_SCALABLE(face)