[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Extracting name of font from file...
From: |
Dave Williss |
Subject: |
Re: Extracting name of font from file... |
Date: |
Tue, 7 Mar 2000 09:10:27 -0600 |
Another thing to consider...
When opening a font with FreeType, it loads a bunch of stuff, initializes
the hinting
engine, etc. If I just want to extract the font name to put in a menu or
something, that's
a lot of extra overhead. If the user has lots of fonts, I had to resort to
putting up a
status bar while I generate the list of font names.
I came up with a partial solution which involves doing this once and storing
the names
in a reference file. Then to generate the menu, I only have to pull the
name from the
font if it's not already known. This works fairly well, but I still have to
give a status
bar the first time as I generate the ref file.
It would be nice if the open function had a flag to only initialize the name
table(s).
----- Original Message -----
From: "Juliusz Chroboczek" <address@hidden>
To: "David Pollack" <address@hidden>; <address@hidden>
Sent: Monday, March 06, 2000 11:20 AM
Subject: Re: Extracting name of font from file...
> DP> I was wondering if there was a way to extract the name of a font from
a
> DP> file with freetype.
>
> Short answer: a version of the code that will work for most Western
> fonts follows. Doing that correctly in the general case is much more
> difficult difficult, and requires knowledge of things such as UTF-16
> surrogates and Unicode normalisation, as well as deciding on an
> ordering of the different languages a font may be named in.
>
> You are welcome to use this code in any way you find fit. Offered
> with no guarantee of any kind.
>
> Note that if your platform doesn't use ISO 8859-1, you will need to
> change the function ttf_u2a.
>
> Sincerely,
>
> J.
>
> #ifndef LSBFirst
> #define LSBFirst 0
> #define MSBFirst 1
> #endif
>
> #define LOBYTE(s,byte) ((byte)==LSBFirst?*(char*)(s):*((char*)(s)+1))
> #define HIBYTE(s,byte) ((byte)==LSBFirst?*((char*)(s)+1):*(char*)(s))
>
> /* Take slen bytes from a Unicode string and convert them to ISO
> * 8859-1. byte specifies the endianness of the string. */
> int
> ttf_u2a(int slen, char *from, char *to, int byte)
> {
> int i;
>
> for (i = 0; i < slen; i += 2) {
> if(HIBYTE(from+i,byte)!=0)
> *to++='?';
> else
> *to++ = LOBYTE(from+i,byte);
> }
> *to = 0;
> return (slen >> 1);
> }
>
> /* A generic routine to get a name from the TT name table. This
> * routine tries to get a name in English. The encoding will be
> * converted to ISO 8859-1.
> *
> * The particular name ID mut be provided (e.g. nameID = 0 for
> * copyright string, nameID = 6 for Postscript name, nameID = 1 for
> * typeface name.
> *
> * Returns the number of bytes added, -1 on failure. */
>
> int
> ttf_GetEnglishName(TT_Face face, char *name, int nameID)
> {
> int i, j, nrec;
> unsigned short slen;
> unsigned short nrPlatformID, nrEncodingID, nrLanguageID, nrNameID;
> char *s;
>
> nrec = TT_Get_Name_Count(face);
>
> for (i = 0; i < nrec; i++) {
> if(TT_Get_Name_ID(face, i, &nrPlatformID, &nrEncodingID,
> &nrLanguageID, &nrNameID))
> continue;
> if (/* check for Microsoft, Unicode, English */
> (nrPlatformID==3 && nrEncodingID==1 &&
> nrNameID==nameID &&
> (nrLanguageID==0x0409 || nrLanguageID==0x0809 ||
> nrLanguageID==0x0c09 || nrLanguageID==0x1009 ||
> nrLanguageID==0x1409 || nrLanguageID==0x1809)) ||
> /* or for Apple, Unicode, English */
> ((nrPlatformID==0 && nrNameID==nameID &
> nrLanguageID==0))) {
> if(!TT_Get_Name_String(face, i, &s, &slen))
> return ttf_u2a(slen, s, name, MSBFirst);
> }
> }
>
> /* Must be some dodgy font. Pretend that Apple Roman is ISO 8859-1. */
> for (i = 0; i < nrec; i++) {
> if(TT_Get_Name_ID(face, i, &nrPlatformID, &nrEncodingID,
> &nrLanguageID, &nrNameID))
> continue;
> /* Check for Apple, Roman, English */
> if (nrPlatformID==1 && nrEncodingID==0 &&
> nrLanguageID==0 && nrNameID==nameID) {
> TT_Get_Name_String(face, i, &s, &slen);
> memcpy(name,s,slen);
> name[slen]=0;
> return slen;
> }
> }
>
> /* Must be some font that can only be named in Polish or something. */
> return -1;
> }
>