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;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.