freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Extracting name of font from file...


From: Juliusz Chroboczek
Subject: Re: Extracting name of font from file...
Date: 06 Mar 2000 17:20:10 +0000

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;
}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]