goyyah was
interested in retreiving the font name from a TrueType (.ttf) file.
He
provided a link to a
CodeProject article showing how to do this.
Actually, I was familiar with this article as I used this code to provide a file tip giving nice information on the font below the cursor in Explorer (see first comment).
He found a solution
using a FreeType DLL but it seems to have lot of holes, and the additional DLL is a constraint.
Since it was a nice challenge and a domain which interest me, I tried and made a pure AutoHotkey solution to this problem.
I had first to be able to read binary files, and since AHK doesn't provide natively such facility, I had to
adapt Laszlo routines for my needs.
Then, it was quite easy (almost!) to parse TrueType headers and figure out the relevant informations.
I had to deal with some oddities to be able to get relevant informations in all cases. I may have not covered all cases, but at least most of them: I analysed more than 400 fonts, recents and old, officials and amateur, and I got quite consistent results.
It works in WinXP and in Win98 (at least) and even achieve decent performance on my old Pentium II 300MHz...
Some fonts with problem:
marlett.ttf is a particular system font, that some font utilities like Arjan Mels' Font Viewer even skip.
It contains some window decorations for older systems, and seems to have no name inside.
Kartika &
Vrinda are fonts for Indic scripts. They don't follow OpenType's strong recommendation ("we strongly recommend that the 'name' table of all fonts include Macintosh platform strings") and only have Microsoft platform strings. On my old system, I found some other fonts with this problem.
MS Gothic is a font for Japanese script. Unlike its MS Mincho counterpart, it wrongly store the Macintosh platform strings, using Roman (0) encoding ID instead of Japanese (1) and Microsoft Japanese (1041) langage ID instead of Macintosh code (11).
To cope with these problems, I only read Macintosh font name if Roman and English (I could read if French, etc., but I think in 99.9% of cases, English is available...) and if not available, I read Microsoft US English font name, which seems to be always in Unicode encoding.
Note that it is more frequent to have a Mac name and no MS name (old fonts) than the reverse.
If you find a font where this scheme fails, let me know...
Note that the CodeProject code had a backup strategy: if the font name wasn't found, it tried to build it from the font family and subfamily (should not add it if it is "Regular"). I found that in all tested cases, I found the font name, so I dropped this backup solution to simplify code and accelerate the processing.
Also note that in most cases, font name is really identical to font family + font subfamily. Some exceptions: using "Normal" or "Medium" instead of "Regular", and some font family names being an abbreviated version of the font name.
So, to get this script running, you need the file shown in
Binary file reading and writing. You can download it from AutoHotkey.net (
BinReadWrite).
You can also download directly
ReadTrueTypeNameData.ahk which defines the "class" routines,
GetTrueTypeFontInfo.ahk which defines functions using these class routines, giving an example of use, and
ListTrueType.ahk which scans all the fonts installed in %WinDir%\fonts and lists their file name and the found font name in a FontList.txt file.
I don't put the code here as it is becoming quite big (around 300 lines for the main files...).
ReadTrueTypeNameData.ahk have been made modular to allow flexibility in the kind of data to fetch. Note that it is currently to get only the names (Naming Table), not other informations like metrics or mapping.
GetTrueTypeFontInfo.ahk provides, of course, the
GetTrueTypeFontName function to get the name, but also
ListTrueTypeFontNameRecords which dumps the list of name records and their value (approximative for real Unicode strings), useful for debugging purpose, and
ListTrueTypeFontInfo which does the same thing in a less technical fashion: no raw data, explicit field name, focussing only on English names, so it can be used to show useful information about a given font.
GetTrueTypeFontInfo.ahk can be used included in another script (like I do in ListTrueType) or run standalone, taking a font file name as parameter (it shows Arial's infos by default). One can write a hotkey to get the font file name selection in Explorer and display useful infos with
ListTrueTypeFontInfo.