1 /* Copyright (C) 2005, 2007, 2008, 2013 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 FT_IS_SCALABLE( face
) )
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 FT_IS_SCALABLE( face
) )
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
)
189 if ( FT_Init_FreeType( &context
) )
191 fprintf( stderr
, "Can't initialize FreeType.\n" );
195 if ( FT_New_Face( context
, testfont
, 0, &face
) )
197 /* The font is erroneous, so if this fails that's ok. */
201 if ( face
->num_faces
== 1 )
208 num
= face
->num_faces
;
209 FT_Done_Face( face
);
211 for ( i
= 0; i
< num
; ++i
)
213 if ( !FT_New_Face( context
, testfont
, i
, &face
) )
223 extmatch( char* filename
,
230 if ( extensions
== NULL
)
233 pt
= strrchr( filename
, '.' );
236 if ( pt
< strrchr( filename
, '/' ) )
239 for ( i
= 0; extensions
[i
] != NULL
; ++i
)
240 if ( strcasecmp( pt
+ 1, extensions
[i
] ) == 0 ||
241 strcasecmp( pt
, extensions
[i
] ) == 0 )
249 figurefiletype( struct fontlist
* item
)
254 item
->isbinary
= item
->isascii
= item
->ishex
= false;
256 foo
= fopen( item
->name
, "rb" );
259 /* Try to guess the file type from the first few characters... */
260 int ch1
= getc( foo
);
261 int ch2
= getc( foo
);
262 int ch3
= getc( foo
);
263 int ch4
= getc( foo
);
268 if ( ( ch1
== 0 && ch2
== 1 && ch3
== 0 && ch4
== 0 ) ||
269 ( ch1
== 'O' && ch2
== 'T' && ch3
== 'T' && ch4
== 'O' ) ||
270 ( ch1
== 't' && ch2
== 'r' && ch3
== 'u' && ch4
== 'e' ) ||
271 ( ch1
== 't' && ch2
== 't' && ch3
== 'c' && ch4
== 'f' ) )
273 /* ttf, otf, ttc files */
274 item
->isbinary
= true;
276 else if ( ch1
== 0x80 && ch2
== '\01' )
279 item
->isbinary
= true;
281 else if ( ch1
== '%' && ch2
== '!' )
283 /* Random PostScript */
284 if ( strstr( item
->name
, ".pfa" ) != NULL
||
285 strstr( item
->name
, ".PFA" ) != NULL
)
288 item
->isascii
= true;
290 else if ( ch1
== 1 && ch2
== 0 && ch3
== 4 )
293 item
->isbinary
= true;
295 else if ( ch1
== 'S' && ch2
== 'T' && ch3
== 'A' && ch4
== 'R' )
300 else if ( ch1
== 'P' && ch2
== 'F' && ch3
== 'R' && ch4
== '0' )
303 item
->isbinary
= true;
305 else if ( ( ch1
== '\1' && ch2
== 'f' && ch3
== 'c' && ch4
== 'p' ) ||
306 ( ch1
== 'M' && ch2
== 'Z' ) )
309 item
->isbinary
= true;
314 "Can't recognize file type of `%s', assuming binary\n",
316 item
->isbinary
= true;
321 fprintf( stderr
, "Can't open `%s' for typing the file.\n",
323 item
->isbinary
= true;
329 FindFonts( char** fontdirs
,
340 for ( i
= 0; fontdirs
[i
] != NULL
; ++i
)
346 examples
= opendir( fontdirs
[i
] );
347 if ( examples
== NULL
)
350 "Can't open example font directory `%s'\n",
355 while ( ( ent
= readdir( examples
) ) != NULL
)
357 snprintf( buffer
, sizeof ( buffer
),
358 "%s/%s", fontdirs
[i
], ent
->d_name
);
359 if ( stat( buffer
, &statb
) == -1 || S_ISDIR( statb
.st_mode
) )
361 if ( extensions
== NULL
|| extmatch( buffer
, extensions
) )
366 fontlist
= realloc( fontlist
, max
* sizeof ( struct fontlist
) );
367 if ( fontlist
== NULL
)
369 fprintf( stderr
, "Can't allocate memory\n" );
374 fontlist
[fcnt
].name
= strdup( buffer
);
375 fontlist
[fcnt
].len
= statb
.st_size
;
377 figurefiletype( &fontlist
[fcnt
] );
382 closedir( examples
);
387 fprintf( stderr
, "Can't find matching font files.\n" );
391 fontlist
[fcnt
].name
= NULL
;
396 getErrorCnt( struct fontlist
* item
)
398 if ( error_count
== 0 && error_fraction
== 0 )
401 return error_count
+ ceil( error_fraction
* item
->len
);
409 if ( low
- high
< 0x10000L
)
410 return low
+ ( ( random() >> 8 ) % ( high
+ 1 - low
) );
412 return low
+ ( random() % ( high
+ 1 - low
) );
417 copyfont( struct fontlist
* item
,
420 static char buffer
[8096];
426 good
= fopen( item
->name
, "r" );
429 fprintf( stderr
, "Can't open `%s'\n", item
->name
);
433 new = fopen( newfont
, "w+" );
436 fprintf( stderr
, "Can't create temporary output file `%s'\n",
441 while ( ( len
= fread( buffer
, 1, sizeof ( buffer
), good
) ) > 0 )
442 fwrite( buffer
, 1, len
, new );
446 err_cnt
= getErrorCnt( item
);
447 for ( i
= 0; i
< err_cnt
; ++i
)
449 fseek( new, getRandom( 0, item
->len
- 1 ), SEEK_SET
);
451 if ( item
->isbinary
)
452 putc( getRandom( 0, 0xff ), new );
453 else if ( item
->isascii
)
454 putc( getRandom( 0x20, 0x7e ), new );
457 int hex
= getRandom( 0, 15 );
482 static int child_pid
;
485 abort_test( int sig
)
487 /* If a time-out happens, then kill the child */
488 kill( child_pid
, SIGFPE
);
489 write( 2, "Timeout... ", 11 );
496 int i
= getRandom( 0, fcnt
- 1 );
497 static int test_num
= 0;
501 sprintf( buffer
, "%s/test%d", results_dir
, test_num
++ );
503 if ( copyfont ( &fontlist
[i
], buffer
) )
505 signal( SIGALRM
, abort_test
);
506 /* Anything that takes more than 20 seconds */
507 /* to parse and/or rasterize is an error. */
509 if ( ( child_pid
= fork() ) == 0 )
510 ExecuteTest( buffer
);
511 else if ( child_pid
!= -1 )
516 waitpid( child_pid
, &status
, 0 );
518 if ( WIFSIGNALED ( status
) )
519 printf( "Error found in file `%s'\n", buffer
);
525 fprintf( stderr
, "Can't fork test case.\n" );
537 fprintf( out
, "%s [options] -- Generate random erroneous fonts\n"
538 " and attempt to parse them with FreeType.\n\n", name
);
540 fprintf( out
, " --all All non-directory files are assumed to be fonts.\n" );
541 fprintf( out
, " --check-outlines Make sure we can parse the outlines of each glyph.\n" );
542 fprintf( out
, " --dir <path> Append <path> to list of font search directories.\n" );
543 fprintf( out
, " --error-count <cnt> Introduce <cnt> single byte errors into each font.\n" );
544 fprintf( out
, " --error-fraction <frac> Introduce <frac>*filesize single byte errors\n"
545 " into each font.\n" );
546 fprintf( out
, " --ext <ext> Add <ext> to list of extensions indicating fonts.\n" );
547 fprintf( out
, " --help Print this.\n" );
548 fprintf( out
, " --nohints Turn off hinting.\n" );
549 fprintf( out
, " --rasterize Attempt to rasterize each glyph.\n" );
550 fprintf( out
, " --results <dir> Directory in which to place the test fonts.\n" );
551 fprintf( out
, " --size <float> Use the given font size for the tests.\n" );
552 fprintf( out
, " --test <file> Run a single test on an already existing file.\n" );
561 int dcnt
= 0, ecnt
= 0, rset
= false, allexts
= false;
564 char* testfile
= NULL
;
567 dirs
= calloc( argc
+ 1, sizeof ( char ** ) );
568 exts
= calloc( argc
+ 1, sizeof ( char ** ) );
570 for ( i
= 1; i
< argc
; ++i
)
576 if ( pt
[0] == '-' && pt
[1] == '-' )
579 if ( strcmp( pt
, "-all" ) == 0 )
581 else if ( strcmp( pt
, "-check-outlines" ) == 0 )
582 check_outlines
= true;
583 else if ( strcmp( pt
, "-dir" ) == 0 )
584 dirs
[dcnt
++] = argv
[++i
];
585 else if ( strcmp( pt
, "-error-count" ) == 0 )
590 error_count
= strtol( argv
[++i
], &end
, 10 );
593 fprintf( stderr
, "Bad value for error-count: %s\n", argv
[i
] );
597 else if ( strcmp( pt
, "-error-fraction" ) == 0 )
602 error_fraction
= strtod( argv
[++i
], &end
);
605 fprintf( stderr
, "Bad value for error-fraction: %s\n", argv
[i
] );
609 else if ( strcmp( pt
, "-ext" ) == 0 )
610 exts
[ecnt
++] = argv
[++i
];
611 else if ( strcmp( pt
, "-help" ) == 0 )
613 usage( stdout
, argv
[0] );
616 else if ( strcmp( pt
, "-nohints" ) == 0 )
618 else if ( strcmp( pt
, "-rasterize" ) == 0 )
620 else if ( strcmp( pt
, "-results" ) == 0 )
621 results_dir
= argv
[++i
];
622 else if ( strcmp( pt
, "-size" ) == 0 )
624 font_size
= (FT_F26Dot6
)( strtod( argv
[++i
], &end
) * 64 );
625 if ( *end
!= '\0' || font_size
< 64 )
627 fprintf( stderr
, "Bad value for size: %s\n", argv
[i
] );
631 else if ( strcmp( pt
, "-test" ) == 0 )
632 testfile
= argv
[++i
];
635 usage( stderr
, argv
[0] );
645 else if ( ecnt
== 0 )
648 exts
= default_ext_list
;
654 dirs
= default_dir_list
;
657 if ( testfile
!= NULL
)
658 ExecuteTest( testfile
); /* This should never return */
663 FindFonts( dirs
, exts
);
664 mkdir( results_dir
, 0755 );