1
/* Copyright (C) 2005 by George Williams */
3
* Redistribution and use in source and binary forms, with or without
4
* modification, are permitted provided that the following conditions are met:
6
* Redistributions of source code must retain the above copyright notice, this
7
* list of conditions and the following disclaimer.
9
* Redistributions in binary form must reproduce the above copyright notice,
10
* this list of conditions and the following disclaimer in the documentation
11
* and/or other materials provided with the distribution.
13
* The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
/* modified by Werner Lemberg <wl@gnu.org> */
29
/* This file is now part of the FreeType library */
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;
87
static FT_F26Dot6 font_size = 12 * 64;
89
static struct fontlist
93
unsigned int isbinary: 1;
94
unsigned int isascii: 1;
95
unsigned int ishex: 1;
103
FT_MoveTo( const FT_Vector *to,
111
FT_LineTo( const FT_Vector *to,
119
FT_ConicTo( const FT_Vector *_cp,
128
FT_CubicTo( const FT_Vector *cp1,
129
const FT_Vector *cp2,
137
static FT_Outline_Funcs outlinefuncs =
143
0, 0 /* No shift, no delta */
148
TestFace( FT_Face face )
151
int load_flags = FT_LOAD_DEFAULT;
154
if ( check_outlines &&
155
( face->face_flags & FT_FACE_FLAG_SCALABLE ) )
156
load_flags = FT_LOAD_NO_BITMAP;
159
load_flags |= FT_LOAD_NO_HINTING;
161
FT_Set_Char_Size( face, 0, font_size, 72, 72 );
163
for ( gid = 0; gid < face->num_glyphs; ++gid )
165
if ( check_outlines &&
166
( face->face_flags & FT_FACE_FLAG_SCALABLE ) )
168
if ( !FT_Load_Glyph( face, gid, load_flags ) )
169
FT_Outline_Decompose( &face->glyph->outline, &outlinefuncs, NULL );
172
FT_Load_Glyph( face, gid, load_flags );
175
FT_Render_Glyph( face->glyph, ft_render_mode_normal );
178
FT_Done_Face( face );
183
ExecuteTest( char* testfont )
190
if ( FT_Init_FreeType( &context ) )
192
fprintf( stderr, "Can't initialize FreeType.\n" );
196
if ( FT_New_Face( context, testfont, 0, &face ) )
198
/* The font is erroneous, so if this fails that's ok. */
202
if ( face->num_faces == 1 )
206
num = face->num_faces;
207
FT_Done_Face( face );
209
for ( i = 0; i < num; ++i )
211
if ( !FT_New_Face( context, testfont, i, &face ) )
221
extmatch( char* filename,
228
if ( extensions == NULL )
231
pt = strrchr( filename, '.' );
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" );
257
/* Try to guess the file type from the first few characters... */
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' ) )
271
/* ttf, otf, ttc files */
272
item->isbinary = true;
274
else if ( ch1 == 0x80 && ch2 == '\01' )
277
item->isbinary = true;
279
else if ( ch1 == '%' && ch2 == '!' )
281
/* Random PostScript */
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,
417
static char buffer[8096];
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 )
484
/* If a time-out happens, then kill the child */
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 );
503
/* Anything that takes more than 20 seconds */
504
/* to parse and/or rasterize is an error. */
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 ); /* This should never return */
649
FindFonts( dirs, exts );
650
mkdir( results_dir, 0755 );