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 <X11/fonts/fontmisc.h>
28 #include <ctype.h>
29 #include <string.h>
30
31 #include <X11/fonts/font.h>
32 #include <ft2build.h>
33 #include FT_FREETYPE_H
34 #include FT_SFNT_NAMES_H
35 #include FT_TRUETYPE_IDS_H
36 #include "ft.h"
37
38 #ifndef LSBFirst
39 #define LSBFirst 0
40 #define MSBFirst 1
41 #endif
42
43 #define LOBYTE(s,byte) ((byte)==LSBFirst?*(char*)(s):*((char*)(s)+1))
44 #define HIBYTE(s,byte) ((byte)==LSBFirst?*((char*)(s)+1):*(char*)(s))
45
FTtoXReturnCode(int rc)46 int FTtoXReturnCode(int rc)
47 {
48 if(rc == 0x40)
49 return AllocError;
50 /* Anything else stops the font matching mechanism */
51 else return BadFontName;
52
53 }
54
55 /* Convert slen bytes from UCS-2 to ISO 8859-1. Byte specifies the
56 endianness of the string, max the maximum number of bytes written into
57 to. */
58 static int
FTu2a(int slen,FT_Byte * from,char * to,int byte,int max)59 FTu2a(int slen, FT_Byte *from, char *to, int byte, int max)
60 {
61 int i, n;
62
63 n = 0;
64 for (i = 0; i < slen; i += 2) {
65 if(n >= max - 1)
66 break;
67 if(HIBYTE(from+i, byte)!=0)
68 *to++='?';
69 else
70 *to++ = LOBYTE(from+i,byte);
71 n++;
72 }
73 *to = 0;
74 return n;
75 }
76
77 static int
FTGetName(FT_Face face,int nid,int pid,int eid,FT_SfntName * name_return)78 FTGetName(FT_Face face, int nid, int pid, int eid, FT_SfntName *name_return)
79 {
80 FT_SfntName name;
81 int n, i;
82
83 n = FT_Get_Sfnt_Name_Count(face);
84 if(n <= 0)
85 return 0;
86
87 for(i = 0; i < n; i++) {
88 if(FT_Get_Sfnt_Name(face, i, &name))
89 continue;
90 if(name.name_id == nid &&
91 name.platform_id == pid &&
92 (eid < 0 || name.encoding_id == eid)) {
93 switch(name.platform_id) {
94 case TT_PLATFORM_APPLE_UNICODE:
95 case TT_PLATFORM_MACINTOSH:
96 if(name.language_id != TT_MAC_LANGID_ENGLISH)
97 continue;
98 break;
99 case TT_PLATFORM_MICROSOFT:
100 if(name.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES &&
101 name.language_id != TT_MS_LANGID_ENGLISH_UNITED_KINGDOM)
102 break;
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
FTGetEnglishName(FT_Face face,int nid,char * name_return,int name_len)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 }
141