Blame src/FreeType/ftenc.c

rpm-build 8fda73
/*
rpm-build 8fda73
Copyright (c) 1998-2003 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 <string.h>
rpm-build 8fda73
rpm-build 8fda73
#include <X11/fonts/fntfilst.h>
rpm-build 8fda73
#include <X11/fonts/fontutil.h>
rpm-build 8fda73
#include <X11/fonts/FSproto.h>
rpm-build 8fda73
rpm-build 8fda73
#include <X11/fonts/fontmisc.h>
rpm-build 8fda73
#include <X11/fonts/fontenc.h>
rpm-build 8fda73
#include <ft2build.h>
rpm-build 8fda73
#include FT_FREETYPE_H
rpm-build 8fda73
#include FT_TRUETYPE_IDS_H
rpm-build 8fda73
#include FT_TRUETYPE_TABLES_H
rpm-build 8fda73
#include FT_TYPE1_TABLES_H
rpm-build 8fda73
#include FT_BDF_H
rpm-build 8fda73
#include FT_XFREE86_H
rpm-build 8fda73
#include "ft.h"
rpm-build 8fda73
rpm-build 8fda73
static int find_cmap(int, int, int, FT_Face, FT_CharMap *);
rpm-build 8fda73
rpm-build 8fda73
static int
rpm-build 8fda73
FTEncFontSpecific(const char *encoding)
rpm-build 8fda73
{
rpm-build 8fda73
    const char *p = encoding;
rpm-build 8fda73
rpm-build 8fda73
    if(strcasecmp(encoding, "microsoft-symbol") == 0)
rpm-build 8fda73
        return 1;
rpm-build 8fda73
rpm-build 8fda73
    while(*p != '-') {
rpm-build 8fda73
        if(*p == '\0')
rpm-build 8fda73
            return 0;
rpm-build 8fda73
        p++;
rpm-build 8fda73
    }
rpm-build 8fda73
    p++;
rpm-build 8fda73
    return (strcasecmp(p, "fontspecific") == 0);
rpm-build 8fda73
}
rpm-build 8fda73
rpm-build 8fda73
int
rpm-build 8fda73
FTPickMapping(char *xlfd, int length, char *filename, FT_Face face,
rpm-build 8fda73
              FTMappingPtr tm)
rpm-build 8fda73
{
rpm-build 8fda73
    FontEncPtr encoding;
rpm-build 8fda73
    FontMapPtr mapping;
rpm-build 8fda73
    FT_CharMap cmap;
rpm-build 8fda73
    int ftrc;
rpm-build 8fda73
    int symbol = 0;
rpm-build 8fda73
    const char *enc, *reg;
rpm-build 8fda73
    const char *encoding_name = 0;
rpm-build 8fda73
    char buf[20];
rpm-build 8fda73
rpm-build 8fda73
    if(xlfd)
rpm-build 8fda73
      encoding_name = FontEncFromXLFD(xlfd, length);
rpm-build 8fda73
    if(!encoding_name)
rpm-build 8fda73
        encoding_name = "iso8859-1";
rpm-build 8fda73
rpm-build 8fda73
    symbol = FTEncFontSpecific(encoding_name);
rpm-build 8fda73
rpm-build 8fda73
#if XFONT_BDFFORMAT
rpm-build 8fda73
    ftrc = FT_Get_BDF_Charset_ID(face, &enc, ®);
rpm-build 8fda73
#else
rpm-build 8fda73
    ftrc = -1;
rpm-build 8fda73
#endif
rpm-build 8fda73
    if(ftrc == 0) {
rpm-build 8fda73
        /* Disable reencoding for non-Unicode fonts.  This will
rpm-build 8fda73
           currently only work for BDFs. */
rpm-build 8fda73
        if(strlen(enc) + strlen(reg) > 18)
rpm-build 8fda73
            goto native;
rpm-build 8fda73
        strcpy(buf, enc);
rpm-build 8fda73
        strcat(buf, "-");
rpm-build 8fda73
        strcat(buf, reg);
rpm-build 8fda73
        ErrorF("%s %s\n", buf, encoding_name);
rpm-build 8fda73
        if(strcasecmp(buf, "iso10646-1") != 0) {
rpm-build 8fda73
            if(strcasecmp(buf, encoding_name) == 0)
rpm-build 8fda73
                goto native;
rpm-build 8fda73
            return BadFontFormat;
rpm-build 8fda73
        }
rpm-build 8fda73
    } else if(symbol) {
rpm-build 8fda73
        ftrc = FT_Select_Charmap(face, ft_encoding_adobe_custom);
rpm-build 8fda73
        if(ftrc == 0)
rpm-build 8fda73
            goto native;
rpm-build 8fda73
    }
rpm-build 8fda73
rpm-build 8fda73
    encoding = FontEncFind(encoding_name, filename);
rpm-build 8fda73
    if(symbol && encoding == NULL)
rpm-build 8fda73
        encoding = FontEncFind("microsoft-symbol", filename);
rpm-build 8fda73
    if(encoding == NULL) {
rpm-build 8fda73
        ErrorF("FreeType: couldn't find encoding '%s' for '%s'\n",
rpm-build 8fda73
               encoding_name, filename);
rpm-build 8fda73
        return BadFontName;
rpm-build 8fda73
    }
rpm-build 8fda73
rpm-build 8fda73
    if(FT_Has_PS_Glyph_Names(face)) {
rpm-build 8fda73
        for(mapping = encoding->mappings; mapping; mapping = mapping->next) {
rpm-build 8fda73
            if(mapping->type == FONT_ENCODING_POSTSCRIPT) {
rpm-build 8fda73
                tm->named = 1;
rpm-build 8fda73
                tm->base = 0;
rpm-build 8fda73
                tm->mapping = mapping;
rpm-build 8fda73
                return Successful;
rpm-build 8fda73
            }
rpm-build 8fda73
        }
rpm-build 8fda73
    }
rpm-build 8fda73
rpm-build 8fda73
    for(mapping = encoding->mappings; mapping; mapping = mapping->next) {
rpm-build 8fda73
        if(find_cmap(mapping->type, mapping->pid, mapping->eid, face,
rpm-build 8fda73
                     &cmap)) {
rpm-build 8fda73
            tm->named = 0;
rpm-build 8fda73
            tm->cmap = cmap;
rpm-build 8fda73
            if(symbol) {
rpm-build 8fda73
                /* deal with an undocumented ``feature'' of the
rpm-build 8fda73
                   Microsft-Symbol cmap */
rpm-build 8fda73
                TT_OS2 *os2;
rpm-build 8fda73
                os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
rpm-build 8fda73
                if(os2)
rpm-build 8fda73
                    tm->base = os2->usFirstCharIndex - 0x20;
rpm-build 8fda73
                else
rpm-build 8fda73
                    tm->base = 0;
rpm-build 8fda73
            } else
rpm-build 8fda73
                tm->base = 0;
rpm-build 8fda73
            tm->mapping = mapping;
rpm-build 8fda73
            return Successful;
rpm-build 8fda73
        }
rpm-build 8fda73
    }
rpm-build 8fda73
rpm-build 8fda73
    return BadFontFormat;
rpm-build 8fda73
rpm-build 8fda73
  native:
rpm-build 8fda73
    tm->named = 0;
rpm-build 8fda73
    tm->cmap = face->charmap;
rpm-build 8fda73
    tm->base = 0;
rpm-build 8fda73
    tm->mapping = NULL;
rpm-build 8fda73
    return Successful;
rpm-build 8fda73
}
rpm-build 8fda73
rpm-build 8fda73
static int
rpm-build 8fda73
find_cmap(int type, int pid, int eid, FT_Face face, FT_CharMap *cmap_return)
rpm-build 8fda73
{
rpm-build 8fda73
    int i, n;
rpm-build 8fda73
    FT_CharMap cmap = NULL;
rpm-build 8fda73
rpm-build 8fda73
    n = face->num_charmaps;
rpm-build 8fda73
rpm-build 8fda73
    switch(type) {
rpm-build 8fda73
    case FONT_ENCODING_TRUETYPE:  /* specific cmap */
rpm-build 8fda73
        for(i=0; i
rpm-build 8fda73
            cmap = face->charmaps[i];
rpm-build 8fda73
            if(cmap->platform_id == pid && cmap->encoding_id == eid) {
rpm-build 8fda73
                *cmap_return = cmap;
rpm-build 8fda73
                return 1;
rpm-build 8fda73
            }
rpm-build 8fda73
        }
rpm-build 8fda73
        break;
rpm-build 8fda73
    case FONT_ENCODING_UNICODE:   /* any Unicode cmap */
rpm-build 8fda73
        /* prefer Microsoft Unicode */
rpm-build 8fda73
        for(i=0; i
rpm-build 8fda73
            cmap = face->charmaps[i];
rpm-build 8fda73
            if(cmap->platform_id == TT_PLATFORM_MICROSOFT &&
rpm-build 8fda73
               cmap->encoding_id == TT_MS_ID_UNICODE_CS) {
rpm-build 8fda73
                *cmap_return = cmap;
rpm-build 8fda73
                return 1;
rpm-build 8fda73
            }
rpm-build 8fda73
        }
rpm-build 8fda73
        break;
rpm-build 8fda73
        /* Try Apple Unicode */
rpm-build 8fda73
        for(i=0; i
rpm-build 8fda73
            cmap = face->charmaps[i];
rpm-build 8fda73
            if(cmap->platform_id == TT_PLATFORM_APPLE_UNICODE) {
rpm-build 8fda73
                *cmap_return = cmap;
rpm-build 8fda73
                return 1;
rpm-build 8fda73
            }
rpm-build 8fda73
        }
rpm-build 8fda73
        /* ISO Unicode? */
rpm-build 8fda73
        for(i=0; i
rpm-build 8fda73
            cmap = face->charmaps[i];
rpm-build 8fda73
            if(cmap->platform_id == TT_PLATFORM_ISO) {
rpm-build 8fda73
                *cmap_return = cmap;
rpm-build 8fda73
                return 1;
rpm-build 8fda73
            }
rpm-build 8fda73
        }
rpm-build 8fda73
        break;
rpm-build 8fda73
    default:
rpm-build 8fda73
        return 0;
rpm-build 8fda73
    }
rpm-build 8fda73
    return 0;
rpm-build 8fda73
}
rpm-build 8fda73
rpm-build 8fda73
unsigned
rpm-build 8fda73
FTRemap(FT_Face face, FTMappingPtr tm, unsigned code)
rpm-build 8fda73
{
rpm-build 8fda73
    unsigned index;
rpm-build 8fda73
    char *name;
rpm-build 8fda73
    unsigned glyph_index;
rpm-build 8fda73
rpm-build 8fda73
    if(tm->mapping) {
rpm-build 8fda73
        if(tm->named) {
rpm-build 8fda73
            name = FontEncName(code, tm->mapping);
rpm-build 8fda73
            if(!name)
rpm-build 8fda73
                return 0;
rpm-build 8fda73
            glyph_index = FT_Get_Name_Index(face, name);
rpm-build 8fda73
            return glyph_index;
rpm-build 8fda73
        } else {
rpm-build 8fda73
            index = FontEncRecode(code, tm->mapping) + tm->base;
rpm-build 8fda73
            FT_Set_Charmap(face, tm->cmap);
rpm-build 8fda73
            glyph_index = FT_Get_Char_Index(face, index);
rpm-build 8fda73
            return glyph_index;
rpm-build 8fda73
        }
rpm-build 8fda73
    } else {
rpm-build 8fda73
        if(code < 0x100) {
rpm-build 8fda73
            index = code;
rpm-build 8fda73
            FT_Set_Charmap(face, tm->cmap);
rpm-build 8fda73
            glyph_index = FT_Get_Char_Index(face, index);
rpm-build 8fda73
            return glyph_index;
rpm-build 8fda73
        } else
rpm-build 8fda73
            return 0;
rpm-build 8fda73
    }
rpm-build 8fda73
}