upload tizen2.0 source
[framework/uifw/xorg/lib/libxfont.git] / src / FreeType / ftenc.c
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 <string.h>
27
28 #include <X11/fonts/fntfilst.h>
29 #include <X11/fonts/fontutil.h>
30 #include <X11/fonts/FSproto.h>
31
32 #include <X11/fonts/fontmisc.h>
33 #include <X11/fonts/fontenc.h>
34 #include <ft2build.h>
35 #include FT_FREETYPE_H
36 #include FT_TRUETYPE_IDS_H
37 #include FT_TRUETYPE_TABLES_H
38 #include FT_TYPE1_TABLES_H
39 #include FT_BDF_H
40 #include FT_XFREE86_H
41 #include "ft.h"
42
43 static int find_cmap(int, int, int, FT_Face, FT_CharMap *);
44
45 static int
46 FTEncFontSpecific(const char *encoding)
47 {
48     const char *p = encoding;
49
50     if(strcasecmp(encoding, "microsoft-symbol") == 0)
51         return 1;
52
53     while(*p != '-') {
54         if(*p == '\0')
55             return 0;
56         p++;
57     }
58     p++;
59     return (strcasecmp(p, "fontspecific") == 0);
60 }
61
62 int
63 FTPickMapping(char *xlfd, int length, char *filename, FT_Face face,
64               FTMappingPtr tm)
65 {
66     FontEncPtr encoding;
67     FontMapPtr mapping;
68     FT_CharMap cmap;
69     int ftrc;
70     int symbol = 0;
71     const char *enc, *reg;
72     const char *encoding_name = 0;
73     char buf[20];
74
75     if(xlfd)
76       encoding_name = FontEncFromXLFD(xlfd, length);
77     if(!encoding_name)
78         encoding_name = "iso8859-1";
79
80     symbol = FTEncFontSpecific(encoding_name);
81
82 #if XFONT_BDFFORMAT
83     ftrc = FT_Get_BDF_Charset_ID(face, &enc, &reg);
84 #else
85     ftrc = -1;
86 #endif
87     if(ftrc == 0) {
88         /* Disable reencoding for non-Unicode fonts.  This will
89            currently only work for BDFs. */
90         if(strlen(enc) + strlen(reg) > 18)
91             goto native;
92         strcpy(buf, enc);
93         strcat(buf, "-");
94         strcat(buf, reg);
95         ErrorF("%s %s\n", buf, encoding_name);
96         if(strcasecmp(buf, "iso10646-1") != 0) {
97             if(strcasecmp(buf, encoding_name) == 0)
98                 goto native;
99             return BadFontFormat;
100         }
101     } else if(symbol) {
102         ftrc = FT_Select_Charmap(face, ft_encoding_adobe_custom);
103         if(ftrc == 0)
104             goto native;
105     }
106
107     encoding = FontEncFind(encoding_name, filename);
108     if(symbol && encoding == NULL)
109         encoding = FontEncFind("microsoft-symbol", filename);
110     if(encoding == NULL) {
111         ErrorF("FreeType: couldn't find encoding '%s' for '%s'\n",
112                encoding_name, filename);
113         return BadFontName;
114     }
115
116     if(FT_Has_PS_Glyph_Names(face)) {
117         for(mapping = encoding->mappings; mapping; mapping = mapping->next) {
118             if(mapping->type == FONT_ENCODING_POSTSCRIPT) {
119                 tm->named = 1;
120                 tm->base = 0;
121                 tm->mapping = mapping;
122                 return Successful;
123             }
124         }
125     }
126
127     for(mapping = encoding->mappings; mapping; mapping = mapping->next) {
128         if(find_cmap(mapping->type, mapping->pid, mapping->eid, face,
129                      &cmap)) {
130             tm->named = 0;
131             tm->cmap = cmap;
132             if(symbol) {
133                 /* deal with an undocumented ``feature'' of the
134                    Microsft-Symbol cmap */
135                 TT_OS2 *os2;
136                 os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
137                 if(os2)
138                     tm->base = os2->usFirstCharIndex - 0x20;
139                 else
140                     tm->base = 0;
141             } else
142                 tm->base = 0;
143             tm->mapping = mapping;
144             return Successful;
145         }
146     }
147
148     return BadFontFormat;
149
150   native:
151     tm->named = 0;
152     tm->cmap = face->charmap;
153     tm->base = 0;
154     tm->mapping = NULL;
155     return Successful;
156 }
157
158 static int
159 find_cmap(int type, int pid, int eid, FT_Face face, FT_CharMap *cmap_return)
160 {
161     int i, n;
162     FT_CharMap cmap = NULL;
163
164     n = face->num_charmaps;
165
166     switch(type) {
167     case FONT_ENCODING_TRUETYPE:  /* specific cmap */
168         for(i=0; i<n; i++) {
169             cmap = face->charmaps[i];
170             if(cmap->platform_id == pid && cmap->encoding_id == eid) {
171                 *cmap_return = cmap;
172                 return 1;
173             }
174         }
175         break;
176     case FONT_ENCODING_UNICODE:   /* any Unicode cmap */
177         /* prefer Microsoft Unicode */
178         for(i=0; i<n; i++) {
179             cmap = face->charmaps[i];
180             if(cmap->platform_id == TT_PLATFORM_MICROSOFT &&
181                cmap->encoding_id == TT_MS_ID_UNICODE_CS) {
182                 *cmap_return = cmap;
183                 return 1;
184             }
185         }
186         break;
187         /* Try Apple Unicode */
188         for(i=0; i<n; i++) {
189             cmap = face->charmaps[i];
190             if(cmap->platform_id == TT_PLATFORM_APPLE_UNICODE) {
191                 *cmap_return = cmap;
192                 return 1;
193             }
194         }
195         /* ISO Unicode? */
196         for(i=0; i<n; i++) {
197             cmap = face->charmaps[i];
198             if(cmap->platform_id == TT_PLATFORM_ISO) {
199                 *cmap_return = cmap;
200                 return 1;
201             }
202         }
203         break;
204     default:
205         return 0;
206     }
207     return 0;
208 }
209
210 unsigned
211 FTRemap(FT_Face face, FTMappingPtr tm, unsigned code)
212 {
213     unsigned index;
214     char *name;
215     unsigned glyph_index;
216
217     if(tm->mapping) {
218         if(tm->named) {
219             name = FontEncName(code, tm->mapping);
220             if(!name)
221                 return 0;
222             glyph_index = FT_Get_Name_Index(face, name);
223             return glyph_index;
224         } else {
225             index = FontEncRecode(code, tm->mapping) + tm->base;
226             FT_Set_Charmap(face, tm->cmap);
227             glyph_index = FT_Get_Char_Index(face, index);
228             return glyph_index;
229         }
230     } else {
231         if(code < 0x100) {
232             index = code;
233             FT_Set_Charmap(face, tm->cmap);
234             glyph_index = FT_Get_Char_Index(face, index);
235             return glyph_index;
236         } else
237             return 0;
238     }
239 }