Blame src/FreeType/fttools.c

rpm-build 8fda73
/*
rpm-build 8fda73
  Copyright (c) 1997 by Mark Leisher
rpm-build 8fda73
  Copyright (c) 1998-2002 by Juliusz Chroboczek
rpm-build 8fda73
rpm-build 8fda73
  Permission is hereby granted, free of charge, to any person obtaining a copy
rpm-build 8fda73
  of this software and associated documentation files (the "Software"), to deal
rpm-build 8fda73
  in the Software without restriction, including without limitation the rights
rpm-build 8fda73
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
rpm-build 8fda73
  copies of the Software, and to permit persons to whom the Software is
rpm-build 8fda73
  furnished to do so, subject to the following conditions:
rpm-build 8fda73
rpm-build 8fda73
  The above copyright notice and this permission notice shall be included in
rpm-build 8fda73
  all copies or substantial portions of the Software.
rpm-build 8fda73
rpm-build 8fda73
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
rpm-build 8fda73
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
rpm-build 8fda73
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
rpm-build 8fda73
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
rpm-build 8fda73
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
rpm-build 8fda73
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
rpm-build 8fda73
  THE SOFTWARE.
rpm-build 8fda73
*/
rpm-build 8fda73
rpm-build 8fda73
#ifdef HAVE_CONFIG_H
rpm-build 8fda73
#include <config.h>
rpm-build 8fda73
#endif
rpm-build 8fda73
#include "libxfontint.h"
rpm-build 8fda73
#include <X11/fonts/fontmisc.h>
rpm-build 8fda73
#include <ctype.h>
rpm-build 8fda73
#include <string.h>
rpm-build 8fda73
rpm-build 8fda73
#include <X11/fonts/font.h>
rpm-build 8fda73
#include <ft2build.h>
rpm-build 8fda73
#include FT_FREETYPE_H
rpm-build 8fda73
#include FT_SFNT_NAMES_H
rpm-build 8fda73
#include FT_TRUETYPE_IDS_H
rpm-build 8fda73
#include "ft.h"
rpm-build 8fda73
rpm-build 8fda73
#ifndef LSBFirst
rpm-build 8fda73
#define LSBFirst 0
rpm-build 8fda73
#define MSBFirst 1
rpm-build 8fda73
#endif
rpm-build 8fda73
rpm-build 8fda73
#define LOBYTE(s,byte) ((byte)==LSBFirst?*(char*)(s):*((char*)(s)+1))
rpm-build 8fda73
#define HIBYTE(s,byte) ((byte)==LSBFirst?*((char*)(s)+1):*(char*)(s))
rpm-build 8fda73
rpm-build 8fda73
int FTtoXReturnCode(int rc)
rpm-build 8fda73
{
rpm-build 8fda73
    if(rc == 0x40)
rpm-build 8fda73
        return AllocError;
rpm-build 8fda73
    /* Anything else stops the font matching mechanism */
rpm-build 8fda73
    else return BadFontName;
rpm-build 8fda73
rpm-build 8fda73
}
rpm-build 8fda73
rpm-build 8fda73
/* Convert slen bytes from UCS-2 to ISO 8859-1.  Byte specifies the
rpm-build 8fda73
   endianness of the string, max the maximum number of bytes written into
rpm-build 8fda73
   to. */
rpm-build 8fda73
static int
rpm-build 8fda73
FTu2a(int slen, FT_Byte *from, char *to, int byte, int max)
rpm-build 8fda73
{
rpm-build 8fda73
    int i, n;
rpm-build 8fda73
rpm-build 8fda73
    n = 0;
rpm-build 8fda73
    for (i = 0; i < slen; i += 2) {
rpm-build 8fda73
        if(n >= max - 1)
rpm-build 8fda73
            break;
rpm-build 8fda73
        if(HIBYTE(from+i, byte)!=0)
rpm-build 8fda73
            *to++='?';
rpm-build 8fda73
        else
rpm-build 8fda73
            *to++ = LOBYTE(from+i,byte);
rpm-build 8fda73
        n++;
rpm-build 8fda73
    }
rpm-build 8fda73
    *to = 0;
rpm-build 8fda73
    return n;
rpm-build 8fda73
}
rpm-build 8fda73
rpm-build 8fda73
static int
rpm-build 8fda73
FTGetName(FT_Face face, int nid, int pid, int eid, FT_SfntName *name_return)
rpm-build 8fda73
{
rpm-build 8fda73
    FT_SfntName name;
rpm-build 8fda73
    int n, i;
rpm-build 8fda73
rpm-build 8fda73
    n = FT_Get_Sfnt_Name_Count(face);
rpm-build 8fda73
    if(n <= 0)
rpm-build 8fda73
        return 0;
rpm-build 8fda73
rpm-build 8fda73
    for(i = 0; i < n; i++) {
rpm-build 8fda73
        if(FT_Get_Sfnt_Name(face, i, &name))
rpm-build 8fda73
            continue;
rpm-build 8fda73
        if(name.name_id == nid &&
rpm-build 8fda73
           name.platform_id == pid &&
rpm-build 8fda73
           (eid < 0 || name.encoding_id == eid)) {
rpm-build 8fda73
            switch(name.platform_id) {
rpm-build 8fda73
            case TT_PLATFORM_APPLE_UNICODE:
rpm-build 8fda73
            case TT_PLATFORM_MACINTOSH:
rpm-build 8fda73
                if(name.language_id != TT_MAC_LANGID_ENGLISH)
rpm-build 8fda73
                    continue;
rpm-build 8fda73
                break;
rpm-build 8fda73
            case TT_PLATFORM_MICROSOFT:
rpm-build 8fda73
                if(name.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES &&
rpm-build 8fda73
                   name.language_id != TT_MS_LANGID_ENGLISH_UNITED_KINGDOM)
rpm-build 8fda73
                    continue;
rpm-build 8fda73
                break;
rpm-build 8fda73
            default:
rpm-build 8fda73
                break;
rpm-build 8fda73
            }
rpm-build 8fda73
            *name_return = name;
rpm-build 8fda73
            return 1;
rpm-build 8fda73
        }
rpm-build 8fda73
    }
rpm-build 8fda73
    return 0;
rpm-build 8fda73
}
rpm-build 8fda73
rpm-build 8fda73
int
rpm-build 8fda73
FTGetEnglishName(FT_Face face, int nid, char *name_return, int name_len)
rpm-build 8fda73
{
rpm-build 8fda73
    FT_SfntName name;
rpm-build 8fda73
    int len;
rpm-build 8fda73
rpm-build 8fda73
    if(FTGetName(face, nid,
rpm-build 8fda73
                 TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, &name) ||
rpm-build 8fda73
       FTGetName(face, nid,
rpm-build 8fda73
                 TT_PLATFORM_APPLE_UNICODE, -1, &name))
rpm-build 8fda73
        return FTu2a(name.string_len, name.string, name_return,
rpm-build 8fda73
                     MSBFirst, name_len);
rpm-build 8fda73
rpm-build 8fda73
    /* Pretend that Apple Roman is ISO 8859-1. */
rpm-build 8fda73
    if(FTGetName(face, nid, TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, &name)) {
rpm-build 8fda73
        len = name.string_len;
rpm-build 8fda73
        if(len > name_len  - 1)
rpm-build 8fda73
            len = name_len - 1;
rpm-build 8fda73
        memcpy(name_return, name.string, len);
rpm-build 8fda73
        name_return[len] = '\0'; /* ensure nul terminaison */
rpm-build 8fda73
        return len;
rpm-build 8fda73
    }
rpm-build 8fda73
rpm-build 8fda73
    /* Must be some font that can only be named in Polish or something. */
rpm-build 8fda73
    return -1;
rpm-build 8fda73
}