Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

TrueType font file informations (name that .TTF font!)


  • Please log in to reply
5 replies to this topic
PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
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.

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Nicely researched and presented. Thanks.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Posted Image
I will post my comments soon...
kWo4Lk1.png

shimanov
  • Members
  • 610 posts
  • Last active: Jul 18 2006 08:35 PM
  • Joined: 25 Sep 2005

Some fonts with problem


Try checking the following Name tables:

platform id, encoding id, language id, name id
3,1,0x409,1
3,1,0x409,2

or (less reliable)

3,1,0x409,3
3,1,0x409,4

These tables contain the correct information.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Posted Image

Great work! PhiLho

The following is FYI:
On my Computer (Intel Celeron 1.1 Ghz with 256Mb Ram, OS:Win98 SE)
ListTrueType.AHK took 26 seconds with progress bar (and 22 seconds
without it.) to list 441 TT Fonts. The resultant file: Fontlist.txt
This speed is satisfactory for me.

Please clarify: If no name can be returned (as in case of Martlett.ttf) why not the filename
(without extension) be returned. Photoshop, Paintshop, MS-Word & etc. shows Marlett.ttf
as Marlett in their font list. Will there be any complications?
kWo4Lk1.png

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
I am happy it pleases you. I know the progress bar slows down a little the script (but I didn't expected so much. Note that file cache may be a factor in performances) but it is much more pleasant with it than trying to guess if the script is doing anything (I first thought my script was stuck, that's why I added the progress bar).

Please clarify: If no name can be returned (as in case of Martlett.ttf) why not the filename
(without extension) be returned. Photoshop, Paintshop, MS-Word & etc. shows Marlett.ttf
as Marlett in their font list. Will there be any complications?

Well, I don't know...
1) I suppose this is the only font with this problem. Or the font designer is very bad, or only put strings in his language.
2) You can easily do that on the program calling GetTrueTypeFontName: if it gets only an empty string, it can take the font file name. I prefer to let the caller do that: it has already the information, and I don't disguise a workaround as real data.

@shimanov: thank you for the tip, but I believe I already addressed these problematic fonts :-)