| 1 |
/* |
| 2 |
Copyright (c) 1997 by Mark Leisher |
| 3 |
Copyright (c) 1998-2002 by Juliusz Chroboczek |
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
of this software and associated documentation files (the "Software"), to deal |
| 7 |
in the Software without restriction, including without limitation the rights |
| 8 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
copies of the Software, and to permit persons to whom the Software is |
| 10 |
furnished to do so, subject to the following conditions: |
| 11 |
|
| 12 |
The above copyright notice and this permission notice shall be included in |
| 13 |
all copies or substantial portions of the Software. |
| 14 |
|
| 15 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 |
THE SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
#include "libxfontint.h" |
| 28 |
#include <X11/fonts/fontmisc.h> |
| 29 |
#include <ctype.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#include <X11/fonts/font.h> |
| 33 |
#include <ft2build.h> |
| 34 |
#include FT_FREETYPE_H |
| 35 |
#include FT_SFNT_NAMES_H |
| 36 |
#include FT_TRUETYPE_IDS_H |
| 37 |
#include "ft.h" |
| 38 |
|
| 39 |
#ifndef LSBFirst |
| 40 |
#define LSBFirst 0 |
| 41 |
#define MSBFirst 1 |
| 42 |
#endif |
| 43 |
|
| 44 |
#define LOBYTE(s,byte) ((byte)==LSBFirst?*(char*)(s):*((char*)(s)+1)) |
| 45 |
#define HIBYTE(s,byte) ((byte)==LSBFirst?*((char*)(s)+1):*(char*)(s)) |
| 46 |
|
| 47 |
int FTtoXReturnCode(int rc) |
| 48 |
{ |
| 49 |
if(rc == 0x40) |
| 50 |
return AllocError; |
| 51 |
/* Anything else stops the font matching mechanism */ |
| 52 |
else return BadFontName; |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/* Convert slen bytes from UCS-2 to ISO 8859-1. Byte specifies the |
| 57 |
endianness of the string, max the maximum number of bytes written into |
| 58 |
to. */ |
| 59 |
static int |
| 60 |
FTu2a(int slen, FT_Byte *from, char *to, int byte, int max) |
| 61 |
{ |
| 62 |
int i, n; |
| 63 |
|
| 64 |
n = 0; |
| 65 |
for (i = 0; i < slen; i += 2) { |
| 66 |
if(n >= max - 1) |
| 67 |
break; |
| 68 |
if(HIBYTE(from+i, byte)!=0) |
| 69 |
*to++='?'; |
| 70 |
else |
| 71 |
*to++ = LOBYTE(from+i,byte); |
| 72 |
n++; |
| 73 |
} |
| 74 |
*to = 0; |
| 75 |
return n; |
| 76 |
} |
| 77 |
|
| 78 |
static int |
| 79 |
FTGetName(FT_Face face, int nid, int pid, int eid, FT_SfntName *name_return) |
| 80 |
{ |
| 81 |
FT_SfntName name; |
| 82 |
int n, i; |
| 83 |
|
| 84 |
n = FT_Get_Sfnt_Name_Count(face); |
| 85 |
if(n <= 0) |
| 86 |
return 0; |
| 87 |
|
| 88 |
for(i = 0; i < n; i++) { |
| 89 |
if(FT_Get_Sfnt_Name(face, i, &name)) |
| 90 |
continue; |
| 91 |
if(name.name_id == nid && |
| 92 |
name.platform_id == pid && |
| 93 |
(eid < 0 || name.encoding_id == eid)) { |
| 94 |
switch(name.platform_id) { |
| 95 |
case TT_PLATFORM_APPLE_UNICODE: |
| 96 |
case TT_PLATFORM_MACINTOSH: |
| 97 |
if(name.language_id != TT_MAC_LANGID_ENGLISH) |
| 98 |
continue; |
| 99 |
break; |
| 100 |
case TT_PLATFORM_MICROSOFT: |
| 101 |
if(name.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES && |
| 102 |
name.language_id != TT_MS_LANGID_ENGLISH_UNITED_KINGDOM) |
| 103 |
continue; |
| 104 |
break; |
| 105 |
default: |
| 106 |
break; |
| 107 |
} |
| 108 |
*name_return = name; |
| 109 |
return 1; |
| 110 |
} |
| 111 |
} |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
int |
| 116 |
FTGetEnglishName(FT_Face face, int nid, char *name_return, int name_len) |
| 117 |
{ |
| 118 |
FT_SfntName name; |
| 119 |
int len; |
| 120 |
|
| 121 |
if(FTGetName(face, nid, |
| 122 |
TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, &name) || |
| 123 |
FTGetName(face, nid, |
| 124 |
TT_PLATFORM_APPLE_UNICODE, -1, &name)) |
| 125 |
return FTu2a(name.string_len, name.string, name_return, |
| 126 |
MSBFirst, name_len); |
| 127 |
|
| 128 |
/* Pretend that Apple Roman is ISO 8859-1. */ |
| 129 |
if(FTGetName(face, nid, TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, &name)) { |
| 130 |
len = name.string_len; |
| 131 |
if(len > name_len - 1) |
| 132 |
len = name_len - 1; |
| 133 |
memcpy(name_return, name.string, len); |
| 134 |
name_return[len] = '\0'; /* ensure nul terminaison */ |
| 135 |
return len; |
| 136 |
} |
| 137 |
|
| 138 |
/* Must be some font that can only be named in Polish or something. */ |
| 139 |
return -1; |
| 140 |
} |