When I am doing web design, I frequently run into issues where I have a folder full of fonts, and while I see their names - they aren't always descriptive.

Some programs will give you a preview of them so that you know what type of font that you are looking at - but more often than not, you have to actually test it out if you want to know what it looks like.

I decided that I just wanted an image that I could leave on my webserver that listed out all of my fonts, printed out using that font. So "Verdana" as the font name, printed out using the verdana.ttf font.

So I quickly wrote up some code that uses GD. It creates a white background image, and black text. Then it scans your font directory and loads all of the names into an array. Then it just iterates that array and positions that font out onto your image.

I have an example test that I just did up at http://www.cardboardutopia.com/fontList/test.png. On there you can see that it doesn't like all of the fonts - I'm not sure what the issue is with that, but it doesn't matter too much to me since I just want a general idea of what the fonts look like - all of the ones that it is belching on are very similar to other fonts that I have.

Here is the code - it should be fairly self-explanitory - I've only tested it under Perl 5.6 on a FreeBSD system with GD installed. I uploaded my Windows TTF fonts into a directory and then ran the script on them - it is an Athlon 1G processor with half a gig of ram and the script took less than a second to complete.

#!/usr/local/bin/perl -w use GD; $fontDir = '/full/path/to/fonts/dir/'; @allFontNames = (); #will need to adjust these depending on how many names you will want t +o show up $height = 800; $width = 320; # create a new image #W,H #make it wide enough for whatever text you are putting in (2 rows) #make it long enough to allow all fonts to show up #$im = new GD::Image(150, 800);#single row $im = new GD::Image($width, $height);#double row #set the background color so we have something to contrast against $im->colorAllocate(255,255,255); #set what will be the font color $black = $im->colorResolve(0,0,0); #get all the fonts from your directory opendir(FONT_DIR,$fontDir); @allFontNames = grep { $_ ne "." and $_ ne ".." } readdir FONT_DIR; closedir(FONT_DIR); #now loop over that array and use that font, and also print out its na +me #check to see if we exceed the height that set, if so, then go over to + two rows $currentLeft = 5; $currentTop = 20; foreach(@allFontNames){ #check to see that we haven't passed our height if($currentTop > $height){ #reset it to write to a new row $currentLeft += 195; $currentTop = 20; } #color,full path, height in pixels, rotation, left, top, text to p +rint $im->stringTTF($black, $fontDir . $_, 12, 0, $currentLeft, $currentTop, $_) or die "Wassup ma scrollays:$!\n"; $currentTop += 20; } open(IMAGE, ">fonts.png") or die; binmode IMAGE; print IMAGE $im->png;

Replies are listed 'Best First'.
Re: creating an image of all of your fonts
by Aristotle (Chancellor) on Mar 11, 2003 at 02:38 UTC
    I suggest you spend a bit more effort on naming things better (and that you use strict when it doesn't hinder you, but that's another matter). As an example, try reading the main loop in this:
    #!/usr/bin/perl -w use strict; # untested; merely to prove a point use GD; use File::Spec; use constant FONT_PATH => '/full/path/to/fonts/dir/'; use constant HEIGHT => 800; use constant WIDTH => 320 use constant COL_WIDTH => 195; use constant LINE_HEIGHT => 20; use constant LEFT => 5; use constant TOP => 20; my $im = GD::Image->(WIDTH, HEIGHT); $im->colorAllocate(255,255,255); my $black = $im->colorResolve(0,0,0); my @font_name = do { opendir my($dh), FONT_PATH or die "Couldn't open ", FONT_PATH, ": $!\n"; grep { $_ ne "." and $_ ne ".." } readdir $dh; }; my $x = LEFT; my $y = TOP; for (@font_name) { if($y > HEIGHT) { $x += COL_WIDTH; $y = TOP; } my $ttf = File::Spec->catfile($fontDir, $_); $im->stringTTF($black, $tff, 12, 0, $x, $y, $_) or die "Failed trying to draw $_: $!\n"; $y += LINE_HEIGHT; } open my $fh, ">", "fonts.png" or die "Couldn't open fonts.png: $!\n"; binmode $fh; print $fh $im->png;

    In your code, it's a lot less obvious what's really going on. I like to believe that the renamed variables and the constants here made it extremly clear what's going on. Also, it's easy to find what "special values" a program uses that way, and they're also easy to customize.

    Also, try to use lexical filehandles wherever possible. It just makes sure you'll never accidentally step on anyone else's foot.

    Of course this is just a small script and wouldn't really justify the critique, but these things become second nature and happen without thinking even when you're writing small scripts, if you've gotten into the habit. And the habit will serve you well in large projects.

    Makeshifts last the longest.

      Thanks for the tips! I need to work on that I guess. That was obviously just a quick script to get the job done - but you are right, in terms of sharing with people I guess it was messy, I didn't thing of that as an issue - but I will defintely work on that for any future postings.
Re: creating an image of all of your fonts
by jasonk (Parson) on Mar 10, 2003 at 21:20 UTC

    It's likely that the fonts it doesn't like are symbol fonts, which contain useful symbols in some font positions, but don't contain anything interesting in the alphabetic positions you are using to print the font name.

Re: creating an image of all of your fonts
by zentara (Cardinal) on Mar 11, 2003 at 12:58 UTC
    I find this perl Tk script handy, it also gives a good description of the font name, which is usually needed when specifying them in scripts. showfonts