Debian Codegraph @

src/FreeType/ftenc.c

raw
1 /*
2 Copyright (c) 1998-2003 by Juliusz Chroboczek
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include "libxfontint.h"
27 #include <string.h>
28
29 #include <X11/fonts/fntfilst.h>
30 #include <X11/fonts/fontutil.h>
31 #include <X11/fonts/FSproto.h>
32
33 #include <X11/fonts/fontmisc.h>
34 #include <X11/fonts/fontenc.h>
35 #include <ft2build.h>
36 #include FT_FREETYPE_H
37 #include FT_TRUETYPE_IDS_H
38 #include FT_TRUETYPE_TABLES_H
39 #include FT_TYPE1_TABLES_H
40 #include FT_BDF_H
41 #include FT_XFREE86_H
42 #include "ft.h"
43
44 static int find_cmap(int, int, int, FT_Face, FT_CharMap *);
45
46 static int
47 FTEncFontSpecific(const char *encoding)
48 {
49 const char *p = encoding;
50
51 if(strcasecmp(encoding, "microsoft-symbol") == 0)
52 return 1;
53
54 while(*p != '-') {
55 if(*p == '\0')
56 return 0;
57 p++;
58 }
59 p++;
60 return (strcasecmp(p, "fontspecific") == 0);
61 }
62
63 int
64 FTPickMapping(char *xlfd, int length, char *filename, FT_Face face,
65 FTMappingPtr tm)
66 {
67 FontEncPtr encoding;
68 FontMapPtr mapping;
69 FT_CharMap cmap;
70 int ftrc;
71 int symbol = 0;
72 const char *enc, *reg;
73 const char *encoding_name = 0;
74 char buf[20];
75
76 if(xlfd)
77 encoding_name = FontEncFromXLFD(xlfd, length);
78 if(!encoding_name)
79 encoding_name = "iso8859-1";
80
81 symbol = FTEncFontSpecific(encoding_name);
82
83 #if XFONT_BDFFORMAT
84 ftrc = FT_Get_BDF_Charset_ID(face, &enc, &reg);
85 #else
86 ftrc = -1;
87 #endif
88 if(ftrc == 0) {
89 /* Disable reencoding for non-Unicode fonts. This will
90 currently only work for BDFs. */
91 if(strlen(enc) + strlen(reg) > 18)
92 goto native;
93 snprintf(buf, sizeof(buf), "%s-%s", enc, reg);
94 ErrorF("%s %s\n", buf, encoding_name);
95 if(strcasecmp(buf, "iso10646-1") != 0) {
96 if(strcasecmp(buf, encoding_name) == 0)
97 goto native;
98 return BadFontFormat;
99 }
100 } else if(symbol) {
101 ftrc = FT_Select_Charmap(face, ft_encoding_adobe_custom);
102 if(ftrc == 0)
103 goto native;
104 }
105
106 encoding = FontEncFind(encoding_name, filename);
107 if(symbol && encoding == NULL)
108 encoding = FontEncFind("microsoft-symbol", filename);
109 if(encoding == NULL) {
110 ErrorF("FreeType: couldn't find encoding '%s' for '%s'\n",
111 encoding_name, filename);
112 return BadFontName;
113 }
114
115 if(FT_Has_PS_Glyph_Names(face)) {
116 for(mapping = encoding->mappings; mapping; mapping = mapping->next) {
117 if(mapping->type == FONT_ENCODING_POSTSCRIPT) {
118 tm->named = 1;
119 tm->base = 0;
120 tm->mapping = mapping;
121 return Successful;
122 }
123 }
124 }
125
126 for(mapping = encoding->mappings; mapping; mapping = mapping->next) {
127 if(find_cmap(mapping->type, mapping->pid, mapping->eid, face,
128 &cmap)) {
129 tm->named = 0;
130 tm->cmap = cmap;
131 if(symbol) {
132 /* deal with an undocumented ``feature'' of the
133 Microsft-Symbol cmap */
134 TT_OS2 *os2;
135 os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
136 if(os2)
137 tm->base = os2->usFirstCharIndex - 0x20;
138 else
139 tm->base = 0;
140 } else
141 tm->base = 0;
142 tm->mapping = mapping;
143 return Successful;
144 }
145 }
146
147 return BadFontFormat;
148
149 native:
150 tm->named = 0;
151 tm->cmap = face->charmap;
152 tm->base = 0;
153 tm->mapping = NULL;
154 return Successful;
155 }
156
157 static int
158 find_cmap(int type, int pid, int eid, FT_Face face, FT_CharMap *cmap_return)
159 {
160 int i, n;
161 FT_CharMap cmap = NULL;
162
163 n = face->num_charmaps;
164
165 switch(type) {
166 case FONT_ENCODING_TRUETYPE: /* specific cmap */
167 for(i=0; i<n; i++) {
168 cmap = face->charmaps[i];
169 if(cmap->platform_id == pid && cmap->encoding_id == eid) {
170 *cmap_return = cmap;
171 return 1;
172 }
173 }
174 break;
175 case FONT_ENCODING_UNICODE: /* any Unicode cmap */
176 /* prefer Microsoft Unicode */
177 for(i=0; i<n; i++) {
178 cmap = face->charmaps[i];
179 if(cmap->platform_id == TT_PLATFORM_MICROSOFT &&
180 cmap->encoding_id == TT_MS_ID_UNICODE_CS) {
181 *cmap_return = cmap;
182 return 1;
183 }
184 }
185 break;
186 /* Try Apple Unicode */
187 for(i=0; i<n; i++) {
188 cmap = face->charmaps[i];
189 if(cmap->platform_id == TT_PLATFORM_APPLE_UNICODE) {
190 *cmap_return = cmap;
191 return 1;
192 }
193 }
194 /* ISO Unicode? */
195 for(i=0; i<n; i++) {
196 cmap = face->charmaps[i];
197 if(cmap->platform_id == TT_PLATFORM_ISO) {
198 *cmap_return = cmap;
199 return 1;
200 }
201 }
202 break;
203 default:
204 return 0;
205 }
206 return 0;
207 }
208
209 unsigned
210 FTRemap(FT_Face face, FTMappingPtr tm, unsigned code)
211 {
212 unsigned index;
213 char *name;
214 unsigned glyph_index;
215
216 if(tm->mapping) {
217 if(tm->named) {
218 name = FontEncName(code, tm->mapping);
219 if(!name)
220 return 0;
221 glyph_index = FT_Get_Name_Index(face, name);
222 return glyph_index;
223 } else {
224 index = FontEncRecode(code, tm->mapping) + tm->base;
225 FT_Set_Charmap(face, tm->cmap);
226 glyph_index = FT_Get_Char_Index(face, index);
227 return glyph_index;
228 }
229 } else {
230 if(code < 0x100) {
231 index = code;
232 FT_Set_Charmap(face, tm->cmap);
233 glyph_index = FT_Get_Char_Index(face, index);
234 return glyph_index;
235 } else
236 return 0;
237 }
238 }