Debian Codegraph @

fontforge/encoding.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "encoding.h"
31
32 #include "bitmapchar.h"
33 #include "bvedit.h"
34 #include "dumppfa.h"
35 #include "encoding.h"
36 #include "ffglib_compat.h"
37 #include "fontforgevw.h"
38 #include "fvfonts.h"
39 #include "gfile.h"
40 #include "namelist.h"
41 #include "psfont.h"
42 #include "psread.h"
43 #include "pua.h"
44 #include "splinefill.h"
45 #include "splinefont.h"
46 #include "splinesaveafm.h"
47 #include "splineutil.h"
48 #include "splineutil2.h"
49 #include "ustring.h"
50 #include "utype.h"
51
52 #include "ffdir.h"
53 #include <math.h>
54 #include <sys/types.h>
55 #include "ffunistd.h"
56
57 Encoding *default_encoding = NULL;
58
59 /* Accessor functions for global encoding variables (needed for MSVC DLL exports) */
60 Encoding *GetDefaultEncoding(void) {
61 return default_encoding;
62 }
63
64 void SetDefaultEncoding(Encoding *enc) {
65 default_encoding = enc;
66 }
67
68 static int32_t tex_base_encoding[] = {
69 0x0000, 0x02d9, 0xfb01, 0xfb02, 0x2044, 0x02dd, 0x0141, 0x0142,
70 0x02db, 0x02da, 0x000a, 0x02d8, 0x2212, 0x000d, 0x017d, 0x017e,
71 0x02c7, 0x0131, 0xf6be, 0xfb00, 0xfb03, 0xfb04, 0x2260, 0x221e,
72 0x2264, 0x2265, 0x2202, 0x2211, 0x220f, 0x03c0, 0x0060, 0x0027,
73 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x2019,
74 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
75 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
76 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
77 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
78 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
79 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
80 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
81 0x2018, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
82 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
83 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
84 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f,
85 0x20ac, 0x222b, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
86 0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0x2126, 0x221a, 0x2248,
87 0x0090, 0x0091, 0x0092, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
88 0x02dc, 0x2122, 0x0161, 0x203a, 0x0153, 0x2206, 0x25ca, 0x0178,
89 0x0000, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
90 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x002d, 0x00ae, 0x00af,
91 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
92 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
93 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
94 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
95 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
96 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
97 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
98 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
99 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
100 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff
101 };
102
103 static int32_t unicode_from_MacSymbol[] = {
104 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
105 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
106 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
107 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
108 0x0020, 0x0021, 0x2200, 0x0023, 0x2203, 0x0025, 0x0026, 0x220d,
109 0x0028, 0x0029, 0x2217, 0x002b, 0x002c, 0x2212, 0x002e, 0x002f,
110 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
111 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
112 0x2245, 0x0391, 0x0392, 0x03a7, 0x0394, 0x0395, 0x03a6, 0x0393,
113 0x0397, 0x0399, 0x03d1, 0x039a, 0x039b, 0x039c, 0x039d, 0x039f,
114 0x03a0, 0x0398, 0x03a1, 0x03a3, 0x03a4, 0x03a5, 0x03c2, 0x03a9,
115 0x039e, 0x03a8, 0x0396, 0x005b, 0x2234, 0x005d, 0x22a5, 0x005f,
116 0xf8e5, 0x03b1, 0x03b2, 0x03c7, 0x03b4, 0x03b5, 0x03c6, 0x03b3,
117 0x03b7, 0x03b9, 0x03d5, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03bf,
118 0x03c0, 0x03b8, 0x03c1, 0x03c3, 0x03c4, 0x03c5, 0x03d6, 0x03c9,
119 0x03be, 0x03c8, 0x03b6, 0x007b, 0x007c, 0x007d, 0x223c, 0x007f,
120 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
121 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f,
122 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
123 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f,
124 0x0000, 0x03d2, 0x2032, 0x2264, 0x2044, 0x221e, 0x0192, 0x2663,
125 0x2666, 0x2665, 0x2660, 0x2194, 0x2190, 0x2191, 0x2192, 0x2193,
126 0x00b0, 0x00b1, 0x2033, 0x2265, 0x00d7, 0x221d, 0x2202, 0x2022,
127 0x00f7, 0x2260, 0x2261, 0x2248, 0x2026, 0xf8e6, 0xf8e7, 0x21b5,
128 0x2135, 0x2111, 0x211c, 0x2118, 0x2297, 0x2295, 0x2205, 0x2229,
129 0x222a, 0x2283, 0x2287, 0x2284, 0x2282, 0x2286, 0x2208, 0x2209,
130 0x2220, 0x2207, 0x00ae, 0x00a9, 0x2122, 0x220f, 0x221a, 0x22c5,
131 0x00ac, 0x2227, 0x2228, 0x21d4, 0x21d0, 0x21d1, 0x21d2, 0x21d3,
132 0x22c4, 0x2329, 0xf8e8, 0xf8e9, 0xf8ea, 0x2211, 0xf8eb, 0xf8ec,
133 0xf8ed, 0xf8ee, 0xf8ef, 0xf8f0, 0xf8f1, 0xf8f2, 0xf8f3, 0xf8f4,
134 0xf8ff, 0x232a, 0x222b, 0x2320, 0xf8f5, 0x2321, 0xf8f6, 0xf8f7,
135 0xf8f8, 0xf8f9, 0xf8fa, 0xf8fb, 0xf8fc, 0xf8fd, 0xf8fe, 0x02c7
136 };
137
138 /* I don't think iconv provides encodings for zapfdingbats nor jis201 */
139 /* Perhaps I should list them here for compatibility, but I think I'll just */
140 /* leave them out. I doubt they get used. */
141 static Encoding texbase = { "TeX-Base-Encoding", 256, tex_base_encoding, NULL, NULL, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 0, 0 };
142 Encoding custom = { "Custom", 0, NULL, NULL, &texbase, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "", 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 0, 0 };
143
144 Encoding *GetCustomEncoding(void) {
145 return &custom;
146 }
147
148 static Encoding original = { "Original", 0, NULL, NULL, &custom, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "", 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 0, 0 };
149 static Encoding unicodebmp = { "UnicodeBmp", 65536, NULL, NULL, &original, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "", 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 0, 0 };
150 static Encoding unicodefull = { "UnicodeFull", 17*65536, NULL, NULL, &unicodebmp, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, "", 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 0, 0 };
151 static Encoding adobestd = { "AdobeStandard", 256, unicode_from_adobestd, (char**)AdobeStandardEncoding, &unicodefull,
152 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 0, 0 };
153 static Encoding symbol = { "Symbol", 256, unicode_from_MacSymbol, NULL, &adobestd, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 0, 0 };
154
155 Encoding *enclist = &symbol;
156
157 const char *FindUnicharName(void) {
158 /* Iconv and libiconv use different names for UCS2. Just great. Perhaps */
159 /* different versions of each use still different names? */
160 /* Even worse, both accept UCS-2, but under iconv it means native byte */
161 /* ordering and under libiconv it means big-endian */
162 iconv_t test;
163 static const char *goodname = NULL;
164 static const char *names[] = { "UCS-4-INTERNAL", "UCS-4", "UCS4", "ISO-10646-UCS-4", "UTF-32", NULL };
165 static const char *namesle[] = { "UCS-4LE", "UTF-32LE", NULL };
166 static const char *namesbe[] = { "UCS-4BE", "UTF-32BE", NULL };
167 const char **testnames;
168 int i;
169 union {
170 short s;
171 char c[2];
172 } u;
173
174 if ( goodname!=NULL )
175 return( goodname );
176
177 u.c[0] = 0x1; u.c[1] = 0x2;
178 if ( u.s==0x201 ) { /* Little endian */
179 testnames = namesle;
180 } else {
181 testnames = namesbe;
182 }
183 for ( i=0; testnames[i]!=NULL; ++i ) {
184 test = iconv_open(testnames[i],"ISO-8859-1");
185 if ( test!=(iconv_t) -1 && test!=NULL ) {
186 iconv_close(test);
187 goodname = testnames[i];
188 break;
189 }
190 }
191
192 if ( goodname==NULL ) {
193 for ( i=0; names[i]!=NULL; ++i ) {
194 test = iconv_open(names[i],"ISO-8859-1");
195 if ( test!=(iconv_t) -1 && test!=NULL ) {
196 iconv_close(test);
197 goodname = names[i];
198 break;
199 }
200 }
201 }
202
203 if ( goodname==NULL ) {
204 IError( "I can't figure out your version of iconv(). I need a name for the UCS-4 encoding and I can't find one. Reconfigure --without-iconv. Bye.");
205 exit( 1 );
206 }
207
208 test = iconv_open(goodname,"Mac");
209 if ( test==(iconv_t) -1 || test==NULL ) {
210 IError( "Your version of iconv does not support the \"Mac Roman\" encoding.\nIf this causes problems, reconfigure --without-iconv." );
211 } else
212 iconv_close(test);
213
214 /* I really should check for ISO-2022-JP, KR, CN, and all the other encodings */
215 /* I might find in a ttf 'name' table. But those tables take too long to build */
216 return( goodname );
217 }
218
219 static int TryEscape( Encoding *enc, const char *escape_sequence ) {
220 char from[20], ucs[20];
221 size_t fromlen, tolen;
222 ICONV_CONST char *fpt;
223 char *upt;
224 int i, j, low;
225 int esc_len = strlen(escape_sequence);
226
227 strcpy(from,escape_sequence);
228
229 enc->has_2byte = false;
230 low = -1;
231 for ( i=0; i<256; ++i ) if ( i!=escape_sequence[0] ) {
232 for ( j=0; j<256; ++j ) {
233 from[esc_len] = i; from[esc_len+1] = j; from[esc_len+2] = 0;
234 fromlen = esc_len+2;
235 fpt = from;
236 upt = ucs;
237 tolen = sizeof(ucs);
238 if ( iconv( enc->tounicode , &fpt, &fromlen, &upt, &tolen )!= (size_t) (-1) &&
239 upt-ucs==sizeof(unichar_t) /* Exactly one character */ ) {
240 if ( low==-1 ) {
241 enc->low_page = low = i;
242 enc->has_2byte = true;
243 }
244 enc->high_page = i;
245 break;
246 }
247 }
248 }
249 if ( enc->low_page==enc->high_page )
250 enc->has_2byte = false;
251 if ( enc->has_2byte ) {
252 strcpy(enc->iso_2022_escape, escape_sequence);
253 enc->iso_2022_escape_len = esc_len;
254 }
255 return( enc->has_2byte );
256 }
257
258 Encoding *_FindOrMakeEncoding(const char *name,int make_it) {
259 Encoding *enc;
260 char buffer[20];
261 const char *iconv_name;
262 Encoding temp;
263 uint8_t good[256];
264 int i, j, any, all;
265 char from[8], ucs[20];
266 size_t fromlen, tolen;
267 ICONV_CONST char *fpt;
268 char *upt;
269 /* iconv is not case sensitive */
270
271 if ( strncasecmp(name,"iso8859_",8)==0 || strncasecmp(name,"koi8_",5)==0 ) {
272 /* Fixup for old naming conventions */
273 strncpy(buffer,name,sizeof(buffer));
274 buffer[sizeof(buffer)-1] = '\0';
275 *strchr(buffer,'_') = '-';
276 name = buffer;
277 } else if ( strcasecmp(name,"iso-8859")==0 ) {
278 /* Fixup for old naming conventions */
279 strncpy(buffer,name,3);
280 strncpy(buffer+3,name+4,sizeof(buffer)-3);
281 buffer[sizeof(buffer)-1] = '\0';
282 name = buffer;
283 } else if ( strcasecmp(name,"isolatin10")==0 || strcasecmp(name,"latin10")==0 ) {
284 name = "iso8859-16"; /* try 10 before trying 1 */
285 } else if ( strcasecmp(name,"isolatin1")==0 ) {
286 name = "iso8859-1";
287 } else if ( strcasecmp(name,"isocyrillic")==0 ) {
288 name = "iso8859-5";
289 } else if ( strcasecmp(name,"isoarabic")==0 ) {
290 name = "iso8859-6";
291 } else if ( strcasecmp(name,"isogreek")==0 ) {
292 name = "iso8859-7";
293 } else if ( strcasecmp(name,"isohebrew")==0 ) {
294 name = "iso8859-8";
295 } else if ( strcasecmp(name,"isothai")==0 ) {
296 name = "tis-620"; /* TIS doesn't define non-breaking space in 0xA0 */
297 } else if ( strcasecmp(name,"latin0")==0 || strcasecmp(name,"latin9")==0 ) {
298 name = "iso8859-15"; /* "latin-9" is supported (libiconv bug?) */
299 } else if ( strcasecmp(name,"koi8r")==0 ) {
300 name = "koi8-r";
301 } else if ( strncasecmp(name,"jis201",6)==0 || strncasecmp(name,"jisx0201",8)==0 ) {
302 name = "jis_x0201";
303 } else if ( strcasecmp(name,"AdobeStandardEncoding")==0 || strcasecmp(name,"Adobe")==0 )
304 name = "AdobeStandard";
305 for ( enc=enclist; enc!=NULL; enc=enc->next )
306 if ( strmatch(name,enc->enc_name)==0 ||
307 (enc->iconv_name!=NULL && strmatch(name,enc->iconv_name)==0))
308 return( enc );
309 if ( strmatch(name,"unicode")==0 || strmatch(name,"iso10646")==0 || strmatch(name,"iso10646-1")==0 )
310 return( &unicodebmp );
311 if ( strmatch(name,"unicode4")==0 || strmatch(name,"ucs4")==0 )
312 return( &unicodefull );
313
314 iconv_name = name;
315 /* Mac seems to work ok */
316 if ( strcasecmp(name,"win")==0 || strcasecmp(name,"ansi")==0 )
317 iconv_name = "MS-ANSI"; /* "WINDOWS-1252";*/
318 else if ( strncasecmp(name,"jis208",6)==0 || strncasecmp(name,"jisx0208",8)==0 )
319 iconv_name = "ISO-2022-JP";
320 else if ( strncasecmp(name,"jis212",6)==0 || strncasecmp(name,"jisx0212",8)==0 )
321 iconv_name = "ISO-2022-JP-2";
322 else if ( strncasecmp(name,"ksc5601",7)==0 )
323 iconv_name = "ISO-2022-KR";
324 else if ( strcasecmp(name,"gb2312pk")==0 || strcasecmp(name,"gb2312packed")==0 )
325 iconv_name = "EUC-CN";
326 else if ( strncasecmp(name,"gb2312",6)==0 )
327 iconv_name = "ISO-2022-CN";
328 else if ( strcasecmp(name,"wansung")==0 )
329 iconv_name = "EUC-KR";
330 else if ( strcasecmp(name,"EUC-CN")==0 ) {
331 iconv_name = name;
332 name = "gb2312pk";
333 } else if ( strcasecmp(name,"EUC-KR")==0 ) {
334 iconv_name = name;
335 name = "wansung";
336 }
337
338 /* Escape sequences: */
339 /* ISO-2022-CN: \e $ ) A ^N */
340 /* ISO-2022-KR: \e $ ) C ^N */
341 /* ISO-2022-JP: \e $ B */
342 /* ISO-2022-JP-2: \e $ ( D */
343 /* ISO-2022-JP-3: \e $ ( O */ /* Capital "O", not zero */
344 /* ISO-2022-CN-EXT: \e $ ) E ^N */ /* Not sure about this, also uses CN escape */
345
346 memset(&temp,0,sizeof(temp));
347 temp.builtin = true;
348 temp.tounicode = iconv_open(FindUnicharName(),iconv_name);
349 if ( temp.tounicode==(iconv_t) -1 || temp.tounicode==NULL )
350 return( NULL ); /* Iconv doesn't recognize this name */
351 temp.fromunicode = iconv_open(iconv_name,FindUnicharName());
352 if ( temp.fromunicode==(iconv_t) -1 || temp.fromunicode==NULL ) {
353 /* This should never happen, but if it does... */
354 iconv_close(temp.tounicode);
355 return( NULL );
356 }
357
358 memset(good,0,sizeof(good));
359 any = false; all = true;
360 for ( i=1; i<256; ++i ) {
361 from[0] = i; from[1] = 0;
362 fromlen = 1;
363 fpt = from;
364 upt = ucs;
365 tolen = sizeof(ucs);
366 if ( iconv( temp.tounicode , &fpt, &fromlen, &upt, &tolen )!= (size_t) (-1)) {
367 good[i] = true;
368 any = true;
369 } else
370 all = false;
371 }
372 if ( any )
373 temp.has_1byte = true;
374 if ( all )
375 temp.only_1byte = true;
376
377 if ( !all ) {
378 if ( strstr(iconv_name,"2022")==NULL ) {
379 for ( i=temp.has_1byte; i<256; ++i ) if ( !good[i] ) {
380 for ( j=0; j<256; ++j ) {
381 from[0] = i; from[1] = j; from[2] = 0;
382 fromlen = 2;
383 fpt = from;
384 upt = ucs;
385 tolen = sizeof(ucs);
386 if ( iconv( temp.tounicode , &fpt, &fromlen, &upt, &tolen )!= (size_t) (-1) &&
387 upt-ucs==sizeof(unichar_t) /* Exactly one character */ ) {
388 if ( temp.low_page==-1 )
389 temp.low_page = i;
390 temp.high_page = i;
391 temp.has_2byte = true;
392 break;
393 }
394 }
395 }
396 if ( temp.low_page==temp.high_page ) {
397 temp.has_2byte = false;
398 temp.low_page = temp.high_page = -1;
399 }
400 }
401 if ( !temp.has_2byte && !good[033]/* escape */ ) {
402 if ( strstr(iconv_name,"2022")!=NULL &&
403 strstr(iconv_name,"JP3")!=NULL &&
404 TryEscape( &temp,"\33$(O" )) {
405 ;
406 } else if ( strstr(iconv_name,"2022")!=NULL &&
407 strstr(iconv_name,"JP2")!=NULL &&
408 TryEscape( &temp,"\33$(D" )) {
409 ;
410 } else if ( strstr(iconv_name,"2022")!=NULL &&
411 strstr(iconv_name,"JP")!=NULL &&
412 TryEscape( &temp,"\33$B" )) {
413 ;
414 } else if ( strstr(iconv_name,"2022")!=NULL &&
415 strstr(iconv_name,"KR")!=NULL &&
416 TryEscape( &temp,"\33$)C\16" )) {
417 ;
418 } else if ( strstr(iconv_name,"2022")!=NULL &&
419 strstr(iconv_name,"CN")!=NULL &&
420 TryEscape( &temp,"\33$)A\16" )) {
421 ;
422 }
423 }
424 }
425 if ( !temp.has_1byte && !temp.has_2byte )
426 return( NULL );
427 if ( !make_it )
428 return( NULL );
429
430 enc = chunkalloc(sizeof(Encoding));
431 *enc = temp;
432 enc->enc_name = copy(name);
433 if ( iconv_name!=name )
434 enc->iconv_name = copy(iconv_name);
435 enc->next = enclist;
436 enc->builtin = true;
437 enclist = enc;
438 if ( enc->has_2byte )
439 enc->char_cnt = (enc->high_page<<8) + 256;
440 else {
441 enc->char_cnt = 256;
442 enc->only_1byte = true;
443 }
444 if ( strstrmatch(iconv_name,"JP")!=NULL ||
445 strstrmatch(iconv_name,"sjis")!=NULL ||
446 strstrmatch(iconv_name,"cp932")!=NULL )
447 enc->is_japanese = true;
448 else if ( strstrmatch(iconv_name,"KR")!=NULL )
449 enc->is_korean = true;
450 else if ( strstrmatch(iconv_name,"CN")!=NULL )
451 enc->is_simplechinese = true;
452 else if ( strstrmatch(iconv_name,"BIG")!=NULL && strstrmatch(iconv_name,"5")!=NULL )
453 enc->is_tradchinese = true;
454
455 if ( strstrmatch(name,"ISO8859")!=NULL &&
456 strtol(name+strlen(name)-2,NULL,10)>=16 )
457 /* Not in our menu, don't hide */;
458 else if ( iconv_name!=name || strmatch(name,"mac")==0 || strstrmatch(name,"ISO8859")!=NULL ||
459 strmatch(name,"koi8-r")==0 || strmatch(name,"sjis")==0 ||
460 strmatch(name,"big5")==0 || strmatch(name,"big5hkscs")==0 )
461 enc->hidden = true;
462
463 return( enc );
464 }
465
466 Encoding *FindOrMakeEncoding(const char *name) {
467 return( _FindOrMakeEncoding(name,true));
468 }
469
470 static char *getPfaEditEncodings(void) {
471 static char *encfile=NULL;
472 char buffer[1025];
473 char *ffdir;
474
475 if ( encfile!=NULL )
476 return encfile;
477 ffdir = getFontForgeUserDir(Config);
478 if ( ffdir==NULL )
479 return NULL;
480 sprintf(buffer,"%s/Encodings.ps", ffdir);
481 free(ffdir);
482 encfile = copy(buffer);
483 return encfile;
484 }
485
486 void EncodingFree(Encoding *item) {
487 int i;
488
489 if ( item==NULL )
490 return;
491
492 free(item->enc_name);
493 if ( item->psnames!=NULL ) {
494 for ( i=0; i<item->char_cnt; ++i )
495 free(item->psnames[i]);
496 free(item->psnames);
497 }
498 free(item->unicode);
499 free(item);
500 }
501
502 void DeleteEncoding(Encoding *me) {
503 FontViewBase *fv;
504 Encoding *prev;
505
506 if ( me->builtin )
507 return;
508
509 for ( fv = FontViewFirst(); fv!=NULL; fv = fv->next ) {
510 if ( fv->map->enc==me )
511 fv->map->enc = &custom;
512 }
513 if ( me==enclist )
514 enclist = me->next;
515 else {
516 for ( prev = enclist; prev!=NULL && prev->next!=me; prev=prev->next );
517 if ( prev!=NULL ) prev->next = me->next;
518 }
519 EncodingFree(me);
520 if ( default_encoding == me )
521 default_encoding = FindOrMakeEncoding("ISO8859-1");
522 if ( default_encoding == NULL )
523 default_encoding = &custom;
524 DumpPfaEditEncodings();
525 }
526
527 /* Parse a TXT file from the unicode consortium */
528 /* Unicode Consortium Format A */
529 /* List of lines with several fields, */
530 /* first is the encoding value (in hex), second the Unicode value (in hex) */
531 /* # is a comment character (to eol) */
532 static Encoding *ParseConsortiumEncodingFile(FILE *file) {
533 char buffer[200];
534 int32_t encs[0x10000];
535 int enc, unienc, max;
536 Encoding *item;
537
538 memset(encs, 0, sizeof(encs));
539 max = -1;
540
541 while ( fgets(buffer,sizeof(buffer),file)!=NULL ) {
542 if ( ishexdigit(buffer[0]) ) {
543 if ( sscanf(buffer, "%x %x", (unsigned *) &enc, (unsigned *) &unienc)==2 &&
544 enc<0x10000 && enc>=0 ) {
545 encs[enc] = unienc;
546 if ( enc>max ) max = enc;
547 }
548 }
549 }
550
551 if ( max==-1 )
552 return( NULL );
553
554 ++max;
555 if ( max<256 ) max = 256;
556 item = calloc(1,sizeof(Encoding));
557 item->only_1byte = item->has_1byte = true;
558 item->char_cnt = max;
559 item->unicode = malloc(max*sizeof(int32_t));
560 memcpy(item->unicode,encs,max*sizeof(int32_t));
561 return( item );
562 }
563
564 /**
565 * \brief Parses a GlyphOrderAndAliasDB encoding file
566 *
567 * \param [in] file The file handle to the encoding file
568 * \return The encoding, or NULL if none could be made
569 *
570 * \details Each line in the file contains one glyph name (equating to one slot).
571 * It is assumed that values are tab separated. There
572 * are at least two columns, with a third column being optional.
573 * The first column is the glyph name to be used on output. This
574 * is what will normally be used to determine the unicode value for
575 * this slot.
576 * The second column is the 'friendly' name - this is what will
577 * be used for the slot's name.
578 * The third, optional column specifies the unicode value that the
579 * slot should map to. When available, this will be used in preference
580 * to the first column.
581 */
582 static Encoding *ParseGlyphOrderAndAliasDB(FILE *file) {
583 FFArray *enc_arr = ff_array_sized_new(FALSE, TRUE, sizeof(int32_t), 256);
584 FFArray *names_arr = ff_array_sized_new(FALSE, TRUE, sizeof(char *), 256);
585 Encoding *item = NULL;
586 char buffer[BUFSIZ];
587 int enc, any = FALSE, has_1byte = FALSE;
588
589 while (fgets(buffer, sizeof(buffer), file)) {
590 char *split = strchr(buffer, '\t'), *split2, *enc_name = NULL;
591 if (split == NULL) {
592 // Skip entries that do not contain at least two (tab separated) values
593 LogError(_("ParseGlyphOrderAndAliasDB: Invalid (non-tab separated entry) at index %zu: %s"), ff_array_len(enc_arr), ff_strstrip(buffer));
594 //enc = -1;
595 //g_array_append_val(enc_arr, enc);
596 //g_array_append_val(names_arr, enc_name);
597 continue;
598 }
599 *split++ = '\0';
600 // Buffer now contains the first column
601 ff_strstrip(buffer);
602 // split contains the second, and possibly the third column
603 ff_strstrip(split);
604
605 // Optional third column
606 split2 = strchr(split, '\t');
607 if (split2 != NULL) {
608 *split2++ = '\0';
609 ff_strstrip(split2);
610 }
611
612 // Use the third column in preference to the first
613 enc = UniFromName((split2 != NULL) ? split2 : buffer, ui_none, &custom);
614 if (split[0] != '\0') {
615 /* Used not to do this, but there are several legal names */
616 /* for some slots and people get unhappy (rightly) if we */
617 /* use the wrong one */
618 enc_name = copy(split);
619 any = TRUE;
620 }
621
622 if (enc != -1 && ff_array_len(enc_arr) < 256) {
623 // We have a valid encoding within the first 256 entries
624 has_1byte = TRUE;
625 }
626
627 // Append entry to our arrays
628 ff_array_append(enc_arr, &enc);
629 ff_array_append(names_arr, &enc_name);
630 }
631
632 size_t enc_len = ff_array_len(enc_arr);
633 size_t names_len = ff_array_len(names_arr);
634 if (enc_len > 0) {
635 // If we have mappings, we make an encoding.
636 char *tmp_name = ff_ask_string(_("Encoding name"), "GlyphOrderAndAliasDB", _("Please name this encoding"));
637 if (tmp_name != NULL) {
638 if (tmp_name[0] == '\0') {
639 // Encodings must be named.
640 free(tmp_name);
641 tmp_name = NULL;
642 } else {
643 item = calloc(1, sizeof(Encoding));
644 item->enc_name = tmp_name;
645 // We pad the map in accordance with existing code in FindOrMakeEncoding and elsewhere.
646 // Nobody knows why.
647 item->char_cnt = (enc_len < 256) ? 256 : enc_len;
648 item->unicode = malloc(item->char_cnt * sizeof(int32_t));
649 memcpy(item->unicode, ff_array_data(enc_arr), enc_len * sizeof(int32_t));
650 if ((size_t)item->char_cnt > enc_len) {
651 // Pad the unfilled entries with -1
652 memset(item->unicode + enc_len, -1, sizeof(int32_t) * (item->char_cnt - enc_len));
653 }
654 if (any) {
655 item->psnames = calloc(item->char_cnt, sizeof(char *));
656 memcpy(item->psnames, ff_array_data(names_arr), names_len * sizeof(char *));
657 }
658
659 item->is_custom = TRUE;
660 item->has_1byte = has_1byte;
661 if (enc_len < 256) {
662 item->only_1byte = TRUE;
663 } else {
664 item->has_2byte = TRUE;
665 }
666 }
667 }
668 }
669
670 ff_array_free(&enc_arr, TRUE);
671 ff_array_free(&names_arr, TRUE);
672 return item;
673 }
674
675 void RemoveMultiples(Encoding *item) {
676 Encoding *test;
677
678 for ( test=enclist; test!=NULL; test = test->next ) {
679 if ( strcmp(test->enc_name,item->enc_name)==0 )
680 break;
681 }
682 if ( test!=NULL )
683 DeleteEncoding(test);
684 }
685
686 char *ParseEncodingFile(char *filename, char *encodingname) {
687 FILE *file;
688 char *orig = filename;
689 Encoding *head, *item, *prev, *next;
690 char *buf, *name;
691 int i,ch;
692
693 if ( filename==NULL ) filename = getPfaEditEncodings();
694 file = fopen(filename,"r");
695 if ( file==NULL ) {
696 if ( orig!=NULL )
697 ff_post_error(_("Couldn't open file"), _("Couldn't open file %.200s"), orig);
698 return( NULL );
699 }
700 ch = getc(file);
701 if ( ch==EOF ) {
702 fclose(file);
703 return( NULL );
704 }
705
706 /* Here we detect file format and decide which format
707 parsing routine to actually use.
708 */
709 ungetc(ch, file);
710
711
712 if(strlen(filename) >= 20
713 && !strcmp(filename + strlen(filename) - 20, "GlyphOrderAndAliasDB")){
714 head = ParseGlyphOrderAndAliasDB(file);
715 } else if (ch=='#' || ch=='0') {
716 head = ParseConsortiumEncodingFile(file);
717 if(encodingname)
718 head->enc_name = copy(encodingname);
719 } else {
720 head = PSSlurpEncodings(file);
721 }
722 fclose(file);
723 if ( head==NULL ) {
724 ff_post_error(_("Bad encoding file format"),_("Bad encoding file format") );
725 return( NULL );
726 }
727
728 for ( i=0, prev=NULL, item=head; item!=NULL; prev = item, item=next, ++i ) {
729 next = item->next;
730 if ( item->enc_name==NULL ) {
731 if ( no_windowing_ui ) {
732 ff_post_error(_("Bad encoding file format"),_("This file contains an unnamed encoding, which cannot be named in a script"));
733 EncodingFree(head);
734 return( NULL );
735 }
736 if ( item==head && item->next==NULL )
737 buf = strdup(_( "Please name this encoding" ));
738 else
739 buf = smprintf(_( "Please name encoding %d in this file" ), i );
740
741 name = ff_ask_string( buf, NULL, buf );
742
743 if ( name!=NULL ) {
744 item->enc_name = copy(name);
745 free(name);
746 } else {
747 if ( prev==NULL )
748 head = item->next;
749 else
750 prev->next = item->next;
751 EncodingFree(item);
752 }
753 }
754 }
755 for ( item=head; item!=NULL; item=item->next )
756 RemoveMultiples(item);
757
758 if ( enclist == NULL )
759 enclist = head;
760 else {
761 for ( item=enclist; item->next!=NULL; item=item->next );
762 item->next = head;
763 }
764 return( copy( head->enc_name ) );
765 }
766
767 void LoadPfaEditEncodings(void) {
768 free(ParseEncodingFile(NULL, NULL));
769 }
770
771 void DumpPfaEditEncodings(void) {
772 FILE *file;
773 Encoding *item;
774 int i;
775 char buffer[80];
776
777 for ( item=enclist; item!=NULL && item->builtin; item=item->next );
778 if ( item==NULL ) {
779 ff_unlink(getPfaEditEncodings());
780 return;
781 }
782
783 file = fopen( getPfaEditEncodings(), "w");
784 if ( file==NULL ) {
785 LogError( _("couldn't write encodings file") );
786 return;
787 }
788
789 for ( item=enclist; item!=NULL; item = item->next ) if ( !item->builtin && item->tounicode_func==NULL ) {
790 fprintf( file, "/%s [\n", item->enc_name );
791 if ( item->psnames==NULL )
792 fprintf( file, "%% Use codepoints.\n" );
793 for ( i=0; i<item->char_cnt; ++i ) {
794 if ( item->psnames!=NULL && item->psnames[i]!=NULL )
795 fprintf( file, " /%s", item->psnames[i]);
796 else if ( item->unicode[i]<' ' || (item->unicode[i]>=0x7f && item->unicode[i]<0xa0))
797 fprintf( file, " /.notdef" );
798 else
799 fprintf( file, " /%s", StdGlyphName(buffer,item->unicode[i],ui_none,(NameList *) -1));
800 if ( (i&0xf)==0 )
801 fprintf( file, "\t\t%% 0x%02x\n", i );
802 else
803 putc('\n',file);
804 }
805 fprintf( file, "] def\n\n" );
806 }
807 fclose(file);
808 }
809
810 /* ************************************************************************** */
811 /* ****************************** CID Encodings ***************************** */
812 /* ************************************************************************** */
813 struct cidmap *cidmaps = NULL;
814
815 int CIDFromName(char *name,SplineFont *cidmaster) {
816 /* We've had various conventions for encoding a cid inside a name */
817 /* I'm primarily interested in this when the name is something like */
818 /* Japan1.504.vert */
819 /* which tells me that the current glyph is the rotated version of */
820 /* cid 504 */
821 /* Other convention "cid-504.vert" */
822 int len = strlen( cidmaster->ordering );
823 int cid;
824 char *end;
825
826 if ( strncmp(name,cidmaster->ordering,len)==0 ) {
827 if ( name[len]=='.' ) ++len;
828 } else if ( strncmp(name,"cid-",4)==0 ) {
829 len = 4;
830 } else
831 len = 0;
832 cid = strtol(name+len,&end,10);
833 if ( end==name+len )
834 return( -1 );
835 if ( *end!='.' && *end!='\0' )
836 return( -1 );
837
838 return ( cid );
839 }
840
841 int CID2Uni(struct cidmap *map,int cid) {
842 unsigned int uni;
843
844 if ( map==NULL )
845 return( -1 );
846 else if ( cid==0 )
847 return( 0 );
848 else if ( cid<map->namemax && map->unicode[cid]!=0 )
849 return( map->unicode[cid] );
850 else if ( cid<map->namemax && map->name[cid]!=NULL ) {
851 if ( sscanf(map->name[cid],"uni%x", &uni )==1 )
852 return( uni );
853 }
854
855 return( -1 );
856 }
857
858 int CID2NameUni(struct cidmap *map,int cid, char *buffer, int len) {
859 int enc = -1;
860 const char *temp;
861
862 if ( map==NULL )
863 snprintf(buffer,len,"cid-%d", cid);
864 else if ( cid<map->namemax && map->name[cid]!=NULL ) {
865 strncpy(buffer,map->name[cid],len);
866 buffer[len-1] = '\0';
867 } else if ( cid==0 )
868 strcpy(buffer,".notdef");
869 else if ( cid<map->namemax && map->unicode[cid]!=0 ) {
870 if ( map->unicode==NULL || map->namemax==0 )
871 enc = 0;
872 else
873 enc = map->unicode[cid];
874 temp = StdGlyphName(buffer,enc,ui_none,(NameList *) -1);
875 if ( temp!=buffer )
876 strcpy(buffer,temp);
877 } else
878 snprintf(buffer,len,"%s.%d", map->ordering, cid);
879 return( enc );
880 }
881
882 int NameUni2CID(struct cidmap *map, int uni, const char *name) {
883 int i;
884 struct cidaltuni *alts;
885
886 if ( map==NULL )
887 return( -1 );
888 if ( uni!=-1 ) {
889 // Search for a matching code.
890 for ( i=0; i<map->namemax; ++i )
891 if ( map->unicode[i]==(uint32_t)uni )
892 return( i );
893 for ( alts=map->alts; alts!=NULL; alts=alts->next )
894 if ( alts->uni==uni )
895 return( alts->cid );
896 } else {
897 // Search for a matching name.
898 if ( name!=NULL )
899 for ( i=0; i<map->namemax; ++i )
900 if ( map->name[i]!=NULL && strcmp(map->name[i],name)==0 )
901 return( i );
902 }
903 return( -1 );
904 }
905
906 struct altuni *CIDSetAltUnis(struct cidmap *map,int cid) {
907 /* Some CIDs are mapped to several unicode code points, damn it */
908 struct altuni *sofar = NULL, *alt;
909 struct cidaltuni *alts;
910
911 for ( alts=map->alts; alts!=NULL; alts=alts->next ) {
912 if ( alts->cid==cid ) {
913 alt = chunkalloc(sizeof(struct altuni));
914 alt->next = sofar;
915 sofar = alt;
916 alt->unienc = alts->uni;
917 alt->vs = -1;
918 }
919 }
920 return( sofar );
921 }
922
923 int MaxCID(struct cidmap *map) {
924 return( map->cidmax );
925 }
926
927 static char *SearchDirForCidMap(const char *dir,char *registry,char *ordering,
928 int supplement,char **maybefile) {
929 char maybe[FILENAME_MAX+1];
930 FF_DirEntry *ent;
931 FF_Dir *d;
932 int len, rlen = strlen(registry), olen=strlen(ordering);
933 char *pt, *end, *ret;
934 int test, best = -1;
935
936 if ( dir==NULL )
937 return( NULL );
938
939 if ( *maybefile!=NULL ) {
940 char *pt = strrchr(*maybefile,'.');
941 while ( pt>*maybefile && isdigit(pt[-1]))
942 --pt;
943 best = strtol(pt,NULL,10);
944 }
945
946 d = ff_opendir(dir);
947 if ( d==NULL )
948 return( NULL );
949 while ( (ent = ff_readdir(d))!=NULL ) {
950 if ( (len = strlen(ent->name))<8 )
951 continue;
952 if ( strcmp(ent->name+len-7,".cidmap")!=0 )
953 continue;
954 if ( strncmp(ent->name,registry,rlen)!=0 || ent->name[rlen]!='-' )
955 continue;
956 pt = ent->name+rlen+1;
957 if ( strncmp(pt,ordering,olen)!=0 || pt[olen]!='-' )
958 continue;
959 pt += olen+1;
960 if ( !isdigit(*pt))
961 continue;
962 test = strtol(pt,&end,10);
963 if ( *end!='.' )
964 continue;
965 if ( test>=supplement ) {
966 ret = malloc(strlen(dir)+1+len+1);
967 strcpy(ret,dir);
968 strcat(ret,"/");
969 strcat(ret,ent->name);
970 ff_closedir(d);
971 return( ret );
972 } else if ( test>best ) {
973 best = test;
974 strcpy(maybe,ent->name);
975 }
976 }
977 ff_closedir(d);
978 if ( best>-1 ) {
979 ret = malloc(strlen(dir)+1+strlen(maybe)+1);
980 strcpy(ret,dir);
981 strcat(ret,"/");
982 strcat(ret,maybe);
983 *maybefile = ret;
984 }
985 return( NULL );
986 }
987
988 static struct cidmap *MakeDummyMap(char *registry,char *ordering,int supplement) {
989 struct cidmap *ret = malloc(sizeof(struct cidmap));
990
991 ret->registry = copy(registry);
992 ret->ordering = copy(ordering);
993 ret->supplement = ret->maxsupple = supplement;
994 ret->cidmax = ret->namemax = 0;
995 ret->unicode = NULL; ret->name = NULL;
996 ret->alts = NULL;
997 ret->next = cidmaps;
998 cidmaps = ret;
999 return( ret );
1000 }
1001
1002 struct cidmap *LoadMapFromFile(char *file,char *registry,char *ordering,
1003 int supplement) {
1004 struct cidmap *ret = malloc(sizeof(struct cidmap));
1005 char *pt = strrchr(file,'.');
1006 FILE *f;
1007 int cid1, cid2, uni, cnt, i, ch;
1008 char name[100];
1009
1010 while ( pt>file && isdigit(pt[-1]))
1011 --pt;
1012 ret->supplement = ret->maxsupple = strtol(pt,NULL,10);
1013 if ( supplement>ret->maxsupple )
1014 ret->maxsupple = supplement;
1015 ret->registry = copy(registry);
1016 ret->ordering = copy(ordering);
1017 ret->alts = NULL;
1018 ret->cidmax = ret->namemax = 0;
1019 ret->unicode = NULL; ret->name = NULL;
1020 ret->next = cidmaps;
1021 cidmaps = ret;
1022
1023 f = fopen( file,"r" );
1024 if ( f==NULL ) {
1025 ff_post_error(_("Missing cidmap file"),_("Couldn't open cidmap file: %s"), file );
1026 } else if ( fscanf( f, "%d %d", &ret->cidmax, &ret->namemax )!=2 ) {
1027 ff_post_error(_("Bad cidmap file"),_("%s is not a cidmap file, please download\nhttp://fontforge.sourceforge.net/cidmaps.tgz"), file );
1028 fprintf( stderr, _("%s is not a cidmap file, please download\nhttp://fontforge.sourceforge.net/cidmaps.tgz"), file );
1029 fclose(f);
1030 } else {
1031 ret->unicode = calloc(ret->namemax+1,sizeof(uint32_t));
1032 ret->name = calloc(ret->namemax+1,sizeof(char *));
1033 while ( 1 ) {
1034 cnt=fscanf( f, "%d..%d %x", &cid1, &cid2, (unsigned *) &uni );
1035 if ( cnt<=0 )
1036 break;
1037 if ( cid1>ret->namemax )
1038 continue;
1039 if ( cnt==3 ) {
1040 if ( cid2>ret->namemax ) cid2 = ret->namemax;
1041 for ( i=cid1; i<=cid2; ++i )
1042 ret->unicode[i] = uni++;
1043 } else if ( cnt==1 ) {
1044 if ( fscanf(f,"%x", (unsigned *) &uni )==1 ) {
1045 ret->unicode[cid1] = uni;
1046 ch = getc(f);
1047 while ( ch==',' ) {
1048 if ( fscanf(f,"%x", (unsigned *) &uni )==1 ) {
1049 struct cidaltuni *alt = chunkalloc(sizeof(struct cidaltuni));
1050 alt->next = ret->alts;
1051 ret->alts = alt;
1052 alt->uni = uni;
1053 alt->cid = cid1;
1054 }
1055 ch = getc(f);
1056 }
1057 ungetc(ch,f);
1058 } else if ( fscanf(f," /%s", name )==1 )
1059 ret->name[cid1] = copy(name);
1060 }
1061 }
1062 fclose(f);
1063 }
1064 return( ret );
1065 }
1066
1067 struct cidmap *FindCidMap(char *registry,char *ordering,int supplement,SplineFont *sf) {
1068 struct cidmap *map, *maybe=NULL;
1069 char *file;
1070 char *maybefile=NULL, *sharedir=NULL;
1071 int maybe_sup = -1;
1072 const char *buts[3], *buts2[3], *buts3[3];
1073 char *buf = NULL;
1074 int ret;
1075
1076 if ( sf!=NULL && sf->cidmaster ) sf = sf->cidmaster;
1077 if ( sf!=NULL && sf->loading_cid_map )
1078 return( NULL );
1079
1080 for ( map = cidmaps; map!=NULL; map = map->next ) {
1081 if ( strcmp(map->registry,registry)==0 && strcmp(map->ordering,ordering)==0 ) {
1082 if ( supplement<=map->supplement )
1083 return( map );
1084 else if ( maybe==NULL || maybe->supplement<map->supplement )
1085 maybe = map;
1086 }
1087 }
1088 if ( maybe!=NULL && supplement<=maybe->maxsupple )
1089 return( maybe ); /* User has said it's ok to use maybe at this supplement level */
1090
1091 file = SearchDirForCidMap(".",registry,ordering,supplement,&maybefile);
1092 if ( file==NULL ) {
1093 sharedir = smprintf("%s/cidmap", getShareDir());
1094 file = SearchDirForCidMap(sharedir,registry,ordering,supplement,&maybefile);
1095 }
1096
1097 if ( file==NULL && (maybe!=NULL || maybefile!=NULL)) {
1098 if ( maybefile!=NULL ) {
1099 char *pt = strrchr(maybefile,'.');
1100 while ( pt>maybefile && isdigit(pt[-1]))
1101 --pt;
1102 maybe_sup = strtol(pt,NULL,10);
1103 if ( maybe!=NULL && maybe->supplement >= maybe_sup ) {
1104 free(maybefile); maybefile = NULL;
1105 maybe_sup = maybe->supplement;
1106 } else
1107 maybe = NULL;
1108 }
1109 if ( maybe!=NULL )
1110 maybe_sup = maybe->supplement;
1111 if ( sf!=NULL ) sf->loading_cid_map = true;
1112 buts[0] = _("_Use It"); buts[1] = _("_Search"); buts[2] = NULL;
1113 ret = ff_ask(_("Use CID Map"),(const char **) buts,0,1,_("This font is based on the charset %1$.20s-%2$.20s-%3$d, but the best I've been able to find is %1$.20s-%2$.20s-%4$d.\nShall I use that or let you search?"),
1114 registry,ordering,supplement,maybe_sup);
1115 if ( sf!=NULL ) sf->loading_cid_map = false;
1116 if ( ret==0 ) {
1117 if ( maybe!=NULL ) {
1118 maybe->maxsupple = supplement;
1119 return( maybe );
1120 } else {
1121 file = maybefile;
1122 maybefile = NULL;
1123 }
1124 }
1125 }
1126
1127 if ( file==NULL && !(supplement==0 && strcmp(registry, "Adobe")==0 && strcmp(ordering, "Identity")==0) ) {
1128 char *uret;
1129 buf = smprintf( "%s-%s-*.cidmap", registry, ordering );
1130 if ( maybe==NULL && maybefile==NULL ) {
1131 buts3[0] = _("_Browse"); buts3[1] = _("_Give Up"); buts3[2] = NULL;
1132 ret = ff_ask(_("No cidmap file..."),(const char **)buts3,0,1,_("FontForge was unable to find a cidmap file for this font. The following path was searched:\n %.80s\n\nIt is not essential to have one, but some things will work better if you do. Would you like to search your local disk for an appropriate file?"),
1133 sharedir);
1134 if ( ret==1 || no_windowing_ui )
1135 buf = NULL;
1136 }
1137 uret = NULL;
1138 if ( ( buf != NULL ) && !no_windowing_ui ) {
1139 if ( sf!=NULL ) sf->loading_cid_map = true;
1140 uret = ff_open_filename(_("Find a cidmap file..."), NULL, (char *) buf );
1141 if ( sf!=NULL ) sf->loading_cid_map = false;
1142 }
1143 if ( uret==NULL ) {
1144 buts2[0] = "_Use It"; buts2[1] = "_Search"; buts2[2] = NULL;
1145 if ( maybe==NULL && maybefile==NULL )
1146 /* No luck */;
1147 else if ( no_windowing_ui && maybe!=NULL ) {
1148 maybe->maxsupple = supplement;
1149 return( maybe );
1150 } else if ( no_windowing_ui ) {
1151 file = maybefile;
1152 maybefile = NULL;
1153 } else if ( ff_ask(_("Use CID Map"),(const char **)buts2,0,1,_("Are you sure you don't want to use the cidmap I found?"))==0 ) {
1154 if ( maybe!=NULL ) {
1155 maybe->maxsupple = supplement;
1156 return( maybe );
1157 } else {
1158 file = maybefile;
1159 maybefile = NULL;
1160 }
1161 }
1162 } else {
1163 file = utf82def_copy(uret);
1164 free(uret);
1165 }
1166 }
1167
1168 free(sharedir);
1169 free(maybefile);
1170 if ( file!=NULL ) {
1171 map = LoadMapFromFile(file,registry,ordering,supplement);
1172 free(file);
1173 return( map );
1174 }
1175
1176 return( MakeDummyMap(registry,ordering,supplement));
1177 }
1178
1179 static void SFApplyOrdering(SplineFont *sf, int glyphcnt) {
1180 SplineChar **glyphs, *sc;
1181 int i;
1182 RefChar *refs, *rnext, *rprev;
1183 SplineSet *new, *spl;
1184
1185 /* Remove references to characters which aren't in the new map (if any) */
1186 /* Don't need to fix up dependencies, because we throw the char away */
1187 for ( i=0; i<sf->glyphcnt; ++i ) if ( (sc = sf->glyphs[i])!=NULL ) {
1188 for ( rprev = NULL, refs=sc->layers[ly_fore].refs; refs!=NULL; refs=rnext ) {
1189 rnext = refs->next;
1190 if ( refs->sc->orig_pos==-1 ) {
1191 new = refs->layers[0].splines;
1192 if ( new!=NULL ) {
1193 for ( spl = new; spl->next!=NULL; spl = spl->next );
1194 spl->next = sc->layers[ly_fore].splines;
1195 sc->layers[ly_fore].splines = new;
1196 }
1197 refs->layers[0].splines=NULL;
1198 RefCharFree(refs);
1199 if ( rprev==NULL )
1200 sc->layers[ly_fore].refs = rnext;
1201 else
1202 rprev->next = rnext;
1203 } else
1204 rprev = refs;
1205 }
1206 }
1207
1208 glyphs = calloc(glyphcnt+1,sizeof(SplineChar *));
1209 for ( i=0; i<sf->glyphcnt; ++i ) if ( (sc = sf->glyphs[i])!=NULL ) {
1210 if ( sc->orig_pos==-1 )
1211 SplineCharFree(sc);
1212 else
1213 glyphs[sc->orig_pos] = sc;
1214 }
1215
1216 free(sf->glyphs);
1217 sf->glyphcnt = sf->glyphmax = glyphcnt;
1218 sf->glyphs = glyphs;
1219 }
1220
1221 /* Convert a normal font to a cid font, rearranging glyphs into cid order */
1222 void SFEncodeToMap(SplineFont *sf,struct cidmap *map) {
1223 SplineChar *sc;
1224 int i,max=0, anyextras=0;
1225
1226 for ( i=0; i<sf->glyphcnt; ++i ) if ( SCWorthOutputting(sc = sf->glyphs[i]) ) {
1227 sc->orig_pos = NameUni2CID(map,sc->unicodeenc,sc->name);
1228 if ( sc->orig_pos>max ) max = sc->orig_pos;
1229 else if ( sc->orig_pos==-1 ) ++anyextras;
1230 } else if ( sc!=NULL )
1231 sc->orig_pos = -1;
1232
1233 if ( anyextras ) {
1234 char *buttons[3];
1235 buttons[0] = _("_Delete"); buttons[1] = _("_Add"); buttons[2] = NULL;
1236 if ( ff_ask(_("Extraneous glyphs"),(const char **) buttons,0,1,_("The current encoding contains glyphs which I cannot map to CIDs.\nShould I delete them or add them to the end (where they may conflict with future ros definitions)?"))==1 ) {
1237 if ( map!=NULL && max<map->cidmax ) max = map->cidmax;
1238 anyextras = 0;
1239 for ( i=0; i<sf->glyphcnt; ++i ) if ( SCWorthOutputting(sc = sf->glyphs[i]) ) {
1240 if ( sc->orig_pos == -1 ) sc->orig_pos = max + anyextras++;
1241 }
1242 max += anyextras;
1243 }
1244 }
1245 SFApplyOrdering(sf, max+1);
1246 }
1247
1248 enum cmaptype { cmt_out=-1, cmt_coderange, cmt_notdefs, cmt_cid, cmt_max };
1249 struct coderange { uint32_t first, last, cid; };
1250 struct cmap {
1251 struct {
1252 size_t n;
1253 struct coderange *ranges;
1254 } groups[cmt_max];
1255 char *registry;
1256 char *ordering;
1257 int supplement;
1258 struct remap *remap;
1259 int total;
1260 };
1261
1262 static void cmapfree(struct cmap *cmap) {
1263 free(cmap->registry);
1264 free(cmap->ordering);
1265 free(cmap->groups[cmt_coderange].ranges);
1266 free(cmap->groups[cmt_notdefs].ranges);
1267 free(cmap->groups[cmt_cid].ranges);
1268 free(cmap->remap);
1269 free(cmap);
1270 }
1271
1272 static struct coderange *ExtendArray(struct coderange *ranges, size_t *n, int val) {
1273 if ( *n == 0 )
1274 ranges = calloc(val,sizeof(struct coderange));
1275 else {
1276 ranges = realloc(ranges,(*n+val)*sizeof(struct coderange));
1277 memset(ranges+*n,0,val*sizeof(struct coderange));
1278 }
1279 *n += val;
1280 return( ranges );
1281 }
1282
1283 static char *readpsstr(char *str) {
1284 char *eos;
1285
1286 while ( isspace(*str)) ++str;
1287 if ( *str=='(' ) ++str;
1288 /* PostScript strings can be more complicated than this (hex, nested parens, Enc85...) */
1289 /* but none of those should show up here */
1290 for ( eos = str; *eos!=')' && *eos!='\0'; ++eos );
1291 return( copyn(str,eos-str));
1292 }
1293
1294 static struct cmap *ParseCMap(char *filename) {
1295 char buf2[200];
1296 FILE *file;
1297 struct cmap *cmap;
1298 char *end, *pt;
1299 int val;
1300 size_t pos;
1301 enum cmaptype in;
1302 int in_is_single; // We set this if we are to parse cidchars into cidranges.
1303 static const char *bcsr = "begincodespacerange", *bndr = "beginnotdefrange", *bcr = "begincidrange", *bcc = "begincidchar";
1304 static const char *reg = "/Registry", *ord = "/Ordering", *sup="/Supplement";
1305
1306 file = fopen(filename,"r");
1307 if ( file==NULL )
1308 return( NULL );
1309
1310 cmap = calloc(1,sizeof(struct cmap));
1311 in = cmt_out;
1312 while ( fgets(buf2,sizeof(buf2),file)!=NULL ) {
1313 for ( pt=buf2; isspace(*pt); ++pt);
1314 if ( in==cmt_out ) {
1315 if ( *pt=='/' ) {
1316 if ( strncmp(pt,reg,strlen(reg))==0 )
1317 cmap->registry = readpsstr(pt+strlen(reg));
1318 else if ( strncmp(pt,ord,strlen(ord))==0 )
1319 cmap->ordering = readpsstr(pt+strlen(ord));
1320 else if ( strncmp(pt,sup,strlen(sup))==0 ) {
1321 for ( pt += strlen(sup); isspace(*pt); ++pt );
1322 cmap->supplement = strtol(pt,NULL,10);
1323 }
1324 continue;
1325 } else if ( !isdigit(*pt) )
1326 continue;
1327 val = strtol(pt,&end,10);
1328 while ( isspace(*end)) ++end;
1329 in_is_single = 0;
1330 if ( strncmp(end,bcsr,strlen(bcsr))==0 ) {
1331 in = cmt_coderange;
1332 } else if ( strncmp(end,bndr,strlen(bndr))==0 ) {
1333 in = cmt_notdefs;
1334 } else if ( strncmp(end,bcr,strlen(bcr))==0 ) {
1335 in = cmt_cid;
1336 } else if ( strncmp(end,bcc,strlen(bcc))==0 ) {
1337 in = cmt_cid;
1338 in_is_single = 1;
1339 }
1340 if ( in!=cmt_out ) {
1341 pos = cmap->groups[in].n;
1342 cmap->groups[in].ranges = ExtendArray(cmap->groups[in].ranges,&cmap->groups[in].n,val);
1343 }
1344 } else if ( strncmp(pt,"end",3)== 0 )
1345 in = cmt_out;
1346 else if (pos >= cmap->groups[in].n) {
1347 LogError(_("cidmap entry out of bounds: %s"), buf2);
1348 }
1349 else {
1350 // Read the first bracketed code.
1351 if ( *pt!='<' )
1352 continue;
1353 cmap->groups[in].ranges[pos].first = strtoul(pt+1,&end,16);
1354 if ( *end=='>' ) ++end;
1355 while ( isspace(*end)) ++end;
1356 if (in_is_single) {
1357 cmap->groups[in].ranges[pos].last = cmap->groups[in].ranges[pos].first;
1358 } else {
1359 // Read the second bracketed code.
1360 if ( *end=='<' ) ++end;
1361 cmap->groups[in].ranges[pos].last = strtoul(end,&end,16);
1362 if ( *end=='>' ) ++end;
1363 }
1364 if ( in!=cmt_coderange ) {
1365 while ( isspace(*end)) ++end;
1366 // Read the unbracketed argument.
1367 cmap->groups[in].ranges[pos].cid = strtol(end,&end,10);
1368 }
1369 ++pos;
1370 }
1371 }
1372 fclose(file);
1373 return( cmap );
1374 }
1375
1376 static void CompressCMap(struct cmap *cmap) {
1377 int32_t i,j,k, pos, base;
1378 uint32_t min, oldmax;
1379 /* we can't really deal with three and four byte encodings */
1380 /* so if we get one arrange for the sf itself to do a remap */
1381
1382 cmap->total = 0x10000;
1383 for ( i=0; i<cmap->groups[cmt_coderange].n; ++i )
1384 if ( cmap->groups[cmt_coderange].ranges[i].last>0xfffff )
1385 break;
1386 if ( i==cmap->groups[cmt_coderange].n ) /* No need to remap */
1387 return;
1388
1389 cmap->remap = calloc(cmap->groups[cmt_coderange].n+1,sizeof(struct remap));
1390 base = 0;
1391 for ( i=0; i<cmap->groups[cmt_coderange].n; ++i )
1392 if ( cmap->groups[cmt_coderange].ranges[i].last<0xffff ) {
1393 base = 0x10000;
1394 break;
1395 }
1396
1397 pos=0;
1398 oldmax = base==0?0:0xffff;
1399 for ( i=0; i<cmap->groups[cmt_coderange].n; ++i ) {
1400 min = 0xffffffff; k=-1;
1401 for ( j=0; j<cmap->groups[cmt_coderange].n; ++j )
1402 if ( cmap->groups[cmt_coderange].ranges[j].first>oldmax &&
1403 cmap->groups[cmt_coderange].ranges[j].first<min ) {
1404 min = cmap->groups[cmt_coderange].ranges[j].first;
1405 k = j;
1406 }
1407 if ( k==-1 )
1408 break;
1409 cmap->remap[pos].firstenc = cmap->groups[cmt_coderange].ranges[k].first&~0xff;
1410 cmap->remap[pos].lastenc = cmap->groups[cmt_coderange].ranges[k].last|0xff;
1411 cmap->remap[pos].infont = base;
1412 base += cmap->remap[pos].lastenc-cmap->remap[pos].firstenc+1;
1413 oldmax = cmap->remap[pos].lastenc;
1414 ++pos;
1415 }
1416 cmap->remap[pos].infont = -1; /* Marks end */
1417 cmap->total = base;
1418 /* so cmap->remap will map sf indeces into the encoding in the cmap */
1419
1420 /* And now we want to change the groups[cmt_cid].ranges so that they will */
1421 /* map into sf indeces rather than into the encoding of the cmap */
1422 for ( i=0; i<cmap->groups[cmt_cid].n; ++i ) {
1423 for ( k=0; cmap->remap[k].infont!=-1; ++k )
1424 if ( cmap->groups[cmt_cid].ranges[i].first>=cmap->remap[k].firstenc &&
1425 cmap->groups[cmt_cid].ranges[i].first<=cmap->remap[k].lastenc )
1426 break;
1427 if ( cmap->remap[k].infont==-1 )
1428 continue;
1429 cmap->groups[cmt_cid].ranges[i].first += cmap->remap[k].infont-cmap->remap[k].firstenc;
1430 cmap->groups[cmt_cid].ranges[i].last += cmap->remap[k].infont-cmap->remap[k].firstenc;
1431 }
1432 }
1433
1434 SplineFont *CIDFlatten(SplineFont *cidmaster,SplineChar **glyphs,int charcnt) {
1435 FontViewBase *fvs;
1436 SplineFont *new;
1437 char buffer[20];
1438 BDFFont *bdf;
1439 int j;
1440
1441 if ( cidmaster==NULL )
1442 return(NULL);
1443 new = SplineFontEmpty();
1444 new->fontname = copy(cidmaster->fontname);
1445 new->fullname = copy(cidmaster->fullname);
1446 new->familyname = copy(cidmaster->familyname);
1447 new->weight = copy(cidmaster->weight);
1448 new->copyright = copy(cidmaster->copyright);
1449 sprintf(buffer,"%g", (double)cidmaster->cidversion);
1450 new->version = copy(buffer);
1451 new->italicangle = cidmaster->italicangle;
1452 new->upos = cidmaster->upos;
1453 new->uwidth = cidmaster->uwidth;
1454 new->ascent = cidmaster->ascent;
1455 new->descent = cidmaster->descent;
1456 new->changed = new->changed_since_autosave = true;
1457 new->display_antialias = cidmaster->display_antialias;
1458 new->hasvmetrics = cidmaster->hasvmetrics;
1459 new->fv = cidmaster->fv;
1460 /* Don't copy the grid splines, there won't be anything meaningful at top level */
1461 /* and won't know which font to copy from below */
1462 new->bitmaps = cidmaster->bitmaps; /* should already be flattened */
1463 cidmaster->bitmaps = NULL; /* don't free 'em */
1464 for ( bdf=new->bitmaps; bdf!=NULL; bdf=bdf->next )
1465 bdf->sf = new;
1466 new->gpos_lookups = cidmaster->gpos_lookups;
1467 cidmaster->gpos_lookups = NULL;
1468 new->gsub_lookups = cidmaster->gsub_lookups;
1469 cidmaster->gsub_lookups = NULL;
1470 new->kerns = cidmaster->kerns; new->vkerns = cidmaster->vkerns;
1471 cidmaster->kerns = cidmaster->vkerns = NULL;
1472 new->names = cidmaster->names; cidmaster->names = NULL;
1473 new->horiz_base = cidmaster->horiz_base; cidmaster->horiz_base = NULL;
1474 new->vert_base = cidmaster->vert_base; cidmaster->vert_base = NULL;
1475 new->pfminfo = cidmaster->pfminfo;
1476 new->texdata = cidmaster->texdata;
1477 new->possub = cidmaster->possub; cidmaster->possub = NULL;
1478 new->sm = cidmaster->sm; cidmaster->sm = NULL;
1479 new->features = cidmaster->features; cidmaster->features = NULL;
1480 new->macstyle = cidmaster->macstyle;
1481 new->origname = copy( cidmaster->origname );
1482 new->display_size = cidmaster->display_size;
1483 /* Don't copy private */
1484 new->xuid = copy(cidmaster->xuid);
1485 new->glyphs = glyphs;
1486 new->glyphcnt = new->glyphmax = charcnt;
1487 for ( j=0; j<charcnt; ++j ) if ( glyphs[j]!=NULL ) {
1488 glyphs[j]->parent = new;
1489 glyphs[j]->orig_pos = j;
1490 }
1491 for ( fvs=new->fv; fvs!=NULL; fvs=fvs->nextsame ) {
1492 fvs->cidmaster = NULL;
1493 if ( fvs->sf->glyphcnt!=new->glyphcnt ) {
1494 free(fvs->selected);
1495 fvs->selected = calloc(new->glyphcnt,sizeof(uint8_t));
1496 if ( fvs->map->encmax < new->glyphcnt )
1497 fvs->map->map = realloc(fvs->map->map,(fvs->map->encmax = new->glyphcnt)*sizeof(int32_t));
1498 fvs->map->enccount = new->glyphcnt;
1499 if ( fvs->map->backmax < new->glyphcnt )
1500 fvs->map->backmap = realloc(fvs->map->backmap,(fvs->map->backmax = new->glyphcnt)*sizeof(int32_t));
1501 for ( j=0; j<new->glyphcnt; ++j )
1502 fvs->map->map[j] = fvs->map->backmap[j] = j;
1503 }
1504 fvs->sf = new;
1505 FVSetTitle(fvs);
1506 }
1507 FontViewReformatAll(new);
1508 SplineFontFree(cidmaster);
1509 return( new );
1510 }
1511
1512 void SFFlatten(SplineFont **cidmasterpp) {
1513 SplineChar **glyphs;
1514 SplineFont *cidmaster = *cidmasterpp;
1515 int i,j,max;
1516
1517 if ( cidmaster==NULL )
1518 return;
1519
1520 if ( cidmaster->cidmaster!=NULL ) {
1521 cidmaster = cidmaster->cidmaster;
1522 cidmasterpp = &cidmaster;
1523 }
1524 /* This doesn't change the ordering, so no need for special tricks to */
1525 /* preserve scrolling location. */
1526 for ( i=max=0; i<cidmaster->subfontcnt; ++i ) {
1527 if ( max<cidmaster->subfonts[i]->glyphcnt )
1528 max = cidmaster->subfonts[i]->glyphcnt;
1529 }
1530 glyphs = calloc(max,sizeof(SplineChar *));
1531 for ( j=0; j<max; ++j ) {
1532 for ( i=0; i<cidmaster->subfontcnt; ++i ) {
1533 if ( j<cidmaster->subfonts[i]->glyphcnt && cidmaster->subfonts[i]->glyphs[j]!=NULL ) {
1534 glyphs[j] = cidmaster->subfonts[i]->glyphs[j];
1535 cidmaster->subfonts[i]->glyphs[j] = NULL;
1536 break;
1537 }
1538 }
1539 }
1540 *cidmasterpp = CIDFlatten(cidmaster,glyphs,max);
1541 }
1542
1543 int SFFlattenByCMap(SplineFont **sfpp,char *cmapname) {
1544 struct cmap *cmap;
1545 int i,j,k,l,m, extras, max, curmax, warned;
1546 int found[4];
1547 SplineChar **glyphs = NULL, *sc;
1548 SplineFont *sf = *sfpp;
1549 FontViewBase *fvs;
1550
1551 if ( sf->cidmaster!=NULL ) {
1552 sf = sf->cidmaster;
1553 sfpp = &sf;
1554 }
1555 if ( sf->subfontcnt==0 ) {
1556 ff_post_error(_("Not a CID-keyed font"),_("Not a CID-keyed font"));
1557 return( false );
1558 }
1559 if ( cmapname==NULL )
1560 return( false );
1561 cmap = ParseCMap(cmapname);
1562 if ( cmap==NULL )
1563 return( false );
1564 CompressCMap(cmap);
1565 max = 0;
1566 for ( i=0; i<cmap->groups[cmt_cid].n; ++i ) {
1567 if ( max<cmap->groups[cmt_cid].ranges[i].last )
1568 max = cmap->groups[cmt_cid].ranges[i].last;
1569 if ( cmap->groups[cmt_cid].ranges[i].last>0x100000 ) {
1570 ff_post_error(_("Encoding Too Large"),_("Encoding Too Large"));
1571 cmapfree(cmap);
1572 return( false );
1573 }
1574 }
1575
1576 curmax = 0;
1577 for ( k=0; k<sf->subfontcnt; ++k ) {
1578 if ( curmax < sf->subfonts[k]->glyphcnt )
1579 curmax = sf->subfonts[k]->glyphcnt;
1580 }
1581
1582 glyphs = calloc(curmax,sizeof(SplineChar *));
1583 for ( i=0; i<curmax; ++i ) {
1584 for ( k=0; k<sf->subfontcnt; ++k )
1585 if ( i<sf->subfonts[k]->glyphcnt && sf->subfonts[k]->glyphs[i]!=NULL ) {
1586 glyphs[i] = sf->subfonts[k]->glyphs[i];
1587 sf->subfonts[k]->glyphs[i] = NULL;
1588 break;
1589 }
1590 }
1591 *sfpp = sf = CIDFlatten(sf,glyphs,curmax);
1592
1593 warned = false;
1594 for ( fvs=sf->fv; fvs!=NULL; fvs=fvs->nextsame ) {
1595 EncMap *map = fvs->map;
1596 for ( j=0; j<2; ++j ) {
1597 extras = 0;
1598 for ( i=0; i<curmax; ++i ) {
1599 sc = glyphs[i];
1600 if ( sc!=NULL ) {
1601 m = 0;
1602 for ( l=0; l<cmap->groups[cmt_cid].n; ++l ) {
1603 if ( i>=cmap->groups[cmt_cid].ranges[l].cid &&
1604 i<=cmap->groups[cmt_cid].ranges[l].cid +
1605 cmap->groups[cmt_cid].ranges[l].last -
1606 cmap->groups[cmt_cid].ranges[l].first ) {
1607 if ( m<sizeof(found)/sizeof(found[0]) )
1608 found[m++] = l;
1609 else if ( !warned ) {
1610 ff_post_notice(_("MultipleEncodingIgnored"),
1611 _("The glyph at CID %d is mapped to more than %d encodings. Only the first %d are handled."), i,
1612 sizeof(found)/sizeof(found[0]),
1613 sizeof(found)/sizeof(found[0]));
1614 warned = true;
1615 }
1616 }
1617 }
1618 if ( m==0 ) {
1619 if ( j ) {
1620 map->map[max+extras] = sc->orig_pos;
1621 map->backmap[sc->orig_pos] = max+extras;
1622 }
1623 ++extras;
1624 } else {
1625 if ( j ) {
1626 int p = cmap->groups[cmt_cid].ranges[found[0]].first +
1627 i-cmap->groups[cmt_cid].ranges[found[0]].cid;
1628 map->map[p] = sc->orig_pos;
1629 map->backmap[sc->orig_pos] = p;
1630 for ( l=1; l<m; ++l ) {
1631 int pos = cmap->groups[cmt_cid].ranges[found[l]].first +
1632 i-cmap->groups[cmt_cid].ranges[found[l]].cid;
1633 map->map[pos] = sc->orig_pos;
1634 }
1635 }
1636 }
1637 }
1638 }
1639 if ( !j ) {
1640 map->map = realloc(map->map,(map->encmax = map->enccount = max+extras)*sizeof(int32_t));
1641 memset(map->map,-1,map->enccount*sizeof(int32_t));
1642 memset(map->backmap,-1,sf->glyphcnt*sizeof(int32_t));
1643 fvs->selected = realloc(fvs->selected, map->enccount*sizeof(uint8_t));
1644 if (map->enccount > sf->glyphcnt) {
1645 memset(fvs->selected+sf->glyphcnt, 0, map->enccount-sf->glyphcnt);
1646 }
1647 map->remap = cmap->remap; cmap->remap = NULL;
1648 }
1649 warned = true;
1650 }
1651 }
1652 cmapfree(cmap);
1653 FontViewReformatAll(sf);
1654 return( true );
1655 }
1656
1657 static int Enc2CMap(struct cmap *cmap,int enc) {
1658 int i;
1659
1660 for ( i=0; i<cmap->groups[cmt_cid].n; ++i )
1661 if ( enc>=cmap->groups[cmt_cid].ranges[i].first &&
1662 enc<=cmap->groups[cmt_cid].ranges[i].last )
1663 return( enc-cmap->groups[cmt_cid].ranges[i].first+
1664 cmap->groups[cmt_cid].ranges[i].cid );
1665
1666 return( -1 );
1667 }
1668
1669 static void SFEncodeToCMap(SplineFont *cidmaster,SplineFont *sf,EncMap *oldmap, struct cmap *cmap) {
1670 SplineChar *sc, *GID0=NULL;
1671 int i,max=0, anyextras=0;
1672
1673 cidmaster->cidregistry = cmap->registry; cmap->registry = NULL;
1674 cidmaster->ordering = cmap->ordering; cmap->ordering = NULL;
1675 cidmaster->supplement = cmap->supplement;
1676
1677 for ( i=0; i<sf->glyphcnt; ++i ) if ( (sc = sf->glyphs[i])!=NULL ) {
1678 if ( strcmp(sc->name,".notdef")==0 )
1679 sc->orig_pos = 0;
1680 else if ( oldmap->backmap[i]==-1 )
1681 sc->orig_pos = -1;
1682 else {
1683 sc->orig_pos = Enc2CMap(cmap,oldmap->backmap[i]);
1684 if ( sc->orig_pos==0 ) {
1685 if ( GID0==NULL )
1686 GID0 = sc;
1687 else
1688 sc->orig_pos = -1;
1689 }
1690 }
1691 if ( sc->orig_pos>max ) max = sc->orig_pos;
1692 else if ( sc->orig_pos==-1 ) ++anyextras;
1693 }
1694 if ( GID0!=NULL )
1695 GID0->orig_pos = ++max;
1696
1697 if ( anyextras ) {
1698 char *buttons[3];
1699 buttons[0] = _("_Delete");
1700 buttons[1] = _("_Add");
1701 buttons[2] = NULL;
1702 if ( ff_ask(_("Extraneous glyphs"),(const char **) buttons,0,1,_("The current encoding contains glyphs which I cannot map to CIDs.\nShould I delete them or add them to the end (where they may conflict with future ros definitions)?"))==1 ) {
1703 if ( cmap!=NULL && max<cmap->total ) max = cmap->total;
1704 anyextras = 0;
1705 for ( i=0; i<sf->glyphcnt; ++i ) if ( (sc = sf->glyphs[i])!=NULL ) {
1706 if ( sc->orig_pos == -1 ) sc->orig_pos = max + anyextras++;
1707 }
1708 max += anyextras;
1709 }
1710 }
1711 SFApplyOrdering(sf, max+1);
1712 }
1713
1714 /* If we change the ascent/descent of a sub font then consider changing the */
1715 /* as/ds of the master font. I used to think this irrelevant, but as the */
1716 /* typoAscent/Descent is based on the master's ascent/descent it actually */
1717 /* is meaningful. Set the master to the subfont with the most glyphs */
1718 void CIDMasterAsDes(SplineFont *sf) {
1719 SplineFont *cidmaster = sf->cidmaster;
1720 SplineFont *best;
1721 int i, cid, cnt, bcnt;
1722
1723 if ( cidmaster==NULL )
1724 return;
1725 best = NULL; bcnt = 0;
1726 for ( i=0; i<cidmaster->subfontcnt; ++i ) {
1727 sf = cidmaster->subfonts[i];
1728 for ( cid=cnt=0; cid<sf->glyphcnt; ++cid )
1729 if ( sf->glyphs[cid]!=NULL )
1730 ++cnt;
1731 if ( cnt>bcnt ) {
1732 best = sf;
1733 bcnt = cnt;
1734 }
1735 }
1736 if ( best==NULL && cidmaster->subfontcnt>0 )
1737 best = cidmaster->subfonts[0];
1738 if ( best!=NULL ) {
1739 double ratio = 1000.0/(best->ascent+best->descent);
1740 int ascent = rint(best->ascent*ratio);
1741 if ( cidmaster->ascent!=ascent || cidmaster->descent!=1000-ascent ) {
1742 cidmaster->ascent = ascent;
1743 cidmaster->descent = 1000-ascent;
1744 }
1745 }
1746 }
1747
1748 SplineFont *MakeCIDMaster(SplineFont *sf,EncMap *oldmap,int bycmap,char *cmapfilename, struct cidmap *cidmap) {
1749 SplineFont *cidmaster;
1750 struct cidmap *map;
1751 struct cmap *cmap;
1752 FontViewBase *fvs;
1753
1754 cidmaster = SplineFontEmpty();
1755 if ( bycmap ) {
1756 if ( cmapfilename==NULL ) {
1757 SplineFontFree(cidmaster);
1758 return(NULL);
1759 }
1760 cmap = ParseCMap(cmapfilename);
1761 if ( cmap==NULL ) {
1762 SplineFontFree(cidmaster);
1763 return(NULL);
1764 }
1765 CompressCMap(cmap);
1766 SFEncodeToCMap(cidmaster,sf,oldmap,cmap);
1767 cmapfree(cmap);
1768 } else {
1769 map = cidmap;
1770 if ( map==NULL ) {
1771 SplineFontFree(cidmaster);
1772 return(NULL);
1773 }
1774 cidmaster->cidregistry = copy(map->registry);
1775 cidmaster->ordering = copy(map->ordering);
1776 cidmaster->supplement = map->supplement;
1777 SFEncodeToMap(sf,map);
1778 }
1779 if ( sf->uni_interp!=ui_none && sf->uni_interp!=ui_unset )
1780 cidmaster->uni_interp = sf->uni_interp;
1781 else if ( strstrmatch(cidmaster->ordering,"japan")!=NULL )
1782 cidmaster->uni_interp = ui_japanese;
1783 else if ( strstrmatch(cidmaster->ordering,"CNS")!=NULL )
1784 cidmaster->uni_interp = ui_trad_chinese;
1785 else if ( strstrmatch(cidmaster->ordering,"GB")!=NULL )
1786 cidmaster->uni_interp = ui_simp_chinese;
1787 else if ( strstrmatch(cidmaster->ordering,"Korea")!=NULL )
1788 cidmaster->uni_interp = ui_korean;
1789 sf->uni_interp = cidmaster->uni_interp;
1790 cidmaster->fontname = copy(sf->fontname);
1791 cidmaster->fullname = copy(sf->fullname);
1792 cidmaster->familyname = copy(sf->familyname);
1793 cidmaster->weight = copy(sf->weight);
1794 cidmaster->copyright = copy(sf->copyright);
1795 cidmaster->cidversion = 1.0;
1796 cidmaster->display_antialias = sf->display_antialias;
1797 cidmaster->display_size = sf->display_size;
1798 cidmaster->ascent = sf->ascent /*880*/;
1799 cidmaster->descent = sf->descent /*120*/;
1800 cidmaster->changed = cidmaster->changed_since_autosave = true;
1801 for ( fvs=sf->fv; fvs!=NULL; fvs=fvs->nextsame )
1802 fvs->cidmaster = cidmaster;
1803 cidmaster->fv = sf->fv;
1804 sf->cidmaster = cidmaster;
1805 cidmaster->subfontcnt = 1;
1806 cidmaster->subfonts = calloc(2,sizeof(SplineFont *));
1807 cidmaster->subfonts[0] = sf;
1808 cidmaster->gpos_lookups = sf->gpos_lookups; sf->gpos_lookups = NULL;
1809 cidmaster->gsub_lookups = sf->gsub_lookups; sf->gsub_lookups = NULL;
1810 cidmaster->horiz_base = sf->horiz_base; sf->horiz_base = NULL;
1811 cidmaster->vert_base = sf->vert_base; sf->vert_base = NULL;
1812 cidmaster->possub = sf->possub; sf->possub = NULL;
1813 cidmaster->kerns = sf->kerns; sf->kerns = NULL;
1814 cidmaster->vkerns = sf->vkerns; sf->vkerns = NULL;
1815 if ( sf->private_dict==NULL )
1816 sf->private_dict = calloc(1,sizeof(struct psdict));
1817 if ( !PSDictHasEntry(sf->private_dict,"lenIV"))
1818 PSDictChangeEntry(sf->private_dict,"lenIV","1"); /* It's 4 by default, in CIDs the convention seems to be 1 */
1819 for ( fvs=sf->fv; fvs!=NULL; fvs=fvs->nextsame ) {
1820 free(fvs->selected);
1821 fvs->selected = calloc(fvs->sf->glyphcnt,sizeof(uint8_t));
1822 EncMapFree(fvs->map);
1823 fvs->map = EncMap1to1(fvs->sf->glyphcnt);
1824 FVSetTitle(fvs);
1825 }
1826 CIDMasterAsDes(sf);
1827 FontViewReformatAll(sf);
1828 return( cidmaster );
1829 }
1830
1831 int CountOfEncoding(Encoding *encoding_name) {
1832 return( encoding_name->char_cnt );
1833 }
1834
1835 char *SFEncodingName(SplineFont *sf,EncMap *map) {
1836 char buffer[130];
1837
1838 if ( sf->cidmaster!=NULL )
1839 sf = sf->cidmaster;
1840 if ( sf->subfontcnt!=0 ) {
1841 sprintf( buffer, "%.50s-%.50s-%d", sf->cidregistry, sf->ordering, sf->supplement );
1842 return( copy( buffer ));
1843 }
1844 return( copy( map->enc->enc_name ));
1845 }
1846
1847 /* ************************** Reencoding routines ************************** */
1848
1849 void BDFOrigFixup(BDFFont *bdf,int orig_cnt,SplineFont *sf) {
1850 BDFChar **glyphs;
1851 int i;
1852
1853 if ( bdf->glyphmax>=orig_cnt ) {
1854 if ( bdf->glyphcnt<orig_cnt ) {
1855 for ( i=bdf->glyphcnt; i<orig_cnt; ++i )
1856 bdf->glyphs[i] = NULL;
1857 bdf->glyphcnt = orig_cnt;
1858 }
1859 return;
1860 }
1861
1862 glyphs = calloc(orig_cnt,sizeof(BDFChar *));
1863 for ( i=0; i<bdf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL ) {
1864 glyphs[sf->glyphs[i]->orig_pos] = bdf->glyphs[i];
1865 if ( bdf->glyphs[i]!=NULL ) /* Not all glyphs exist in a piecemeal font */
1866 bdf->glyphs[i]->orig_pos = sf->glyphs[i]->orig_pos;
1867 }
1868 free(bdf->glyphs);
1869 bdf->glyphs = glyphs;
1870 bdf->glyphcnt = bdf->glyphmax = orig_cnt;
1871 bdf->ticked = true;
1872 }
1873
1874 static int _SFForceEncoding(SplineFont *sf,EncMap *old, Encoding *new_enc) {
1875 int enc_cnt,i;
1876 BDFFont *bdf;
1877 FontViewBase *fvs;
1878
1879 /* Normally we base our encoding process on unicode code points. */
1880 /* but encodings like AdobeStandard are more interested in names than */
1881 /* code points. It is perfectly possible to have a font properly */
1882 /* encoded by code point which is not properly encoded by name */
1883 /* (might have f_i where it should have fi). So even if it's got */
1884 /* the right encoding, we still may want to force the names */
1885 if ( new_enc->is_custom )
1886 return(false); /* Custom, it's whatever's there */
1887
1888 if ( new_enc->is_original ) {
1889 SplineChar **glyphs;
1890 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL )
1891 sf->glyphs[i]->orig_pos = -1;
1892 for ( i=enc_cnt=0; i<old->enccount; ++i )
1893 if ( old->map[i]!=-1 && sf->glyphs[old->map[i]]!=NULL &&
1894 sf->glyphs[old->map[i]]->orig_pos == -1 )
1895 sf->glyphs[old->map[i]]->orig_pos = enc_cnt++;
1896 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL )
1897 if ( sf->glyphs[i]->orig_pos==-1 )
1898 sf->glyphs[i]->orig_pos = enc_cnt++;
1899 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL ) {
1900 struct splinecharlist *scl;
1901 int layer;
1902 RefChar *ref;
1903
1904 for ( scl=sf->glyphs[i]->dependents; scl!=NULL; scl=scl->next ) {
1905 for ( layer=0; layer<scl->sc->layer_cnt; ++layer )
1906 for ( ref = scl->sc->layers[layer].refs; ref!=NULL; ref=ref->next )
1907 ref->orig_pos = ref->sc->orig_pos;
1908 }
1909 }
1910 for ( fvs=sf->fv; fvs!=NULL; fvs=fvs->nextsame ) {
1911 fvs->map->ticked = false;
1912 /*if ( fvs->filled!=NULL ) fvs->filled->ticked = false;*/
1913 }
1914 for ( fvs=sf->fv; fvs!=NULL; fvs=fvs->nextsame ) if ( !fvs->map->ticked ) {
1915 EncMap *map = fvs->map;
1916 for ( i=0; i<map->enccount; ++i ) if ( map->map[i]!=-1 )
1917 map->map[i] = sf->glyphs[map->map[i]]->orig_pos;
1918 if ( enc_cnt>map->backmax ) {
1919 free(map->backmap);
1920 map->backmax = enc_cnt;
1921 map->backmap = malloc(enc_cnt*sizeof(int32_t));
1922 }
1923 memset(map->backmap,-1,enc_cnt*sizeof(int32_t));
1924 for ( i=0; i<map->enccount; ++i ) if ( map->map[i]!=-1 )
1925 if ( map->backmap[map->map[i]]==-1 )
1926 map->backmap[map->map[i]] = i;
1927 map->ticked = true;
1928 }
1929 if ( !old->ticked )
1930 IError( "Unticked encmap" );
1931 for ( bdf=sf->bitmaps; bdf!=NULL; bdf=bdf->next )
1932 BDFOrigFixup(bdf,enc_cnt,sf);
1933 for ( fvs=sf->fv; fvs!=NULL ; fvs=fvs->nextsame )
1934 FVBiggerGlyphCache(fvs,enc_cnt);
1935 glyphs = calloc(enc_cnt,sizeof(SplineChar *));
1936 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL )
1937 glyphs[sf->glyphs[i]->orig_pos] = sf->glyphs[i];
1938 free(sf->glyphs);
1939 sf->glyphs = glyphs;
1940 sf->glyphcnt = sf->glyphmax = enc_cnt;
1941 return( true );
1942 }
1943
1944 enc_cnt = new_enc->char_cnt;
1945
1946 if ( old->enccount<enc_cnt ) {
1947 if ( old->encmax<enc_cnt ) {
1948 old->map = realloc(old->map,enc_cnt*sizeof(int32_t));
1949 old->encmax = enc_cnt;
1950 }
1951 memset(old->map+old->enccount,-1,(enc_cnt-old->enccount)*sizeof(int32_t));
1952 old->enccount = enc_cnt;
1953 }
1954 old->enc = new_enc;
1955 for ( i=0; i<old->enccount && i<enc_cnt; ++i ) if ( old->map[i]!=-1 && sf->glyphs[old->map[i]]!=NULL ) {
1956 SplineChar dummy;
1957 int j = old->map[i];
1958 SCBuildDummy(&dummy,sf,old,i);
1959 sf->glyphs[j]->unicodeenc = dummy.unicodeenc;
1960 free(sf->glyphs[j]->name);
1961 sf->glyphs[j]->name = copy(dummy.name);
1962 }
1963 /* We just changed the unicode values for most glyphs */
1964 /* but any references to them will have the old values, and that's bad, so fix 'em up */
1965 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL ) {
1966 struct splinecharlist *scl;
1967 int layer;
1968 RefChar *ref;
1969
1970 for ( scl=sf->glyphs[i]->dependents; scl!=NULL; scl=scl->next ) {
1971 for ( layer=0; layer<scl->sc->layer_cnt; ++layer )
1972 for ( ref = scl->sc->layers[layer].refs; ref!=NULL; ref=ref->next )
1973 ref->unicode_enc = ref->sc->unicodeenc;
1974 }
1975 }
1976 return( true );
1977 }
1978
1979 int SFForceEncoding(SplineFont *sf,EncMap *old, Encoding *new_enc) {
1980 if ( sf->mm!=NULL ) {
1981 MMSet *mm = sf->mm;
1982 int i;
1983 for ( i=0; i<mm->instance_count; ++i )
1984 _SFForceEncoding(mm->instances[i],old,new_enc);
1985 _SFForceEncoding(mm->normal,old,new_enc);
1986 } else
1987 return( _SFForceEncoding(sf,old,new_enc));
1988
1989 return( true );
1990 }
1991
1992 EncMap *EncMapFromEncoding(SplineFont *sf,Encoding *enc) {
1993 int i,j, extras, found, base, unmax;
1994 int32_t *encoded=NULL, *unencoded=NULL;
1995 EncMap *map;
1996 struct altuni *altuni;
1997 SplineChar *sc;
1998
1999 if ( enc==NULL )
2000 return( NULL );
2001
2002 base = enc->char_cnt;
2003 if ( enc->is_original )
2004 base = 0;
2005 else if ( enc->char_cnt<=256 )
2006 base = 256;
2007 else if ( enc->char_cnt<=0x10000 )
2008 base = 0x10000;
2009 if ( base>0 ) {
2010 encoded = malloc(base*sizeof(int32_t));
2011 memset(encoded,-1,base*sizeof(int32_t));
2012 }
2013 if ( sf->glyphcnt>0 ) {
2014 unencoded = malloc(sf->glyphcnt*sizeof(int32_t));
2015 unmax = sf->glyphcnt;
2016 }
2017
2018 for ( i=extras=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL ) {
2019 found = false;
2020 if ( enc->psnames!=NULL ) {
2021 for ( j=enc->char_cnt-1; j>=0; --j ) {
2022 if ( enc->psnames[j]!=NULL &&
2023 strcmp(enc->psnames[j],sc->name)==0 ) {
2024 found = true;
2025 encoded[j] = i;
2026 }
2027 }
2028 }
2029 if ( !found ) {
2030 if ( sc->unicodeenc!=-1 &&
2031 sc->unicodeenc < (int)unicode4_size &&
2032 (j = EncFromUni(sc->unicodeenc,enc))!= -1 )
2033 encoded[j] = i;
2034 else {
2035 /* I don't think extras can surpass unmax now, but it doesn't */
2036 /* hurt to leave the code (it's from when we encoded duplicates see below) */
2037 if ( extras>=unmax ) unencoded = realloc(unencoded,(unmax+=300)*sizeof(int32_t));
2038 unencoded[extras++] = i;
2039 }
2040 for ( altuni=sc->altuni; altuni!=NULL; altuni=altuni->next ) {
2041 if ( altuni->unienc!=-1 &&
2042 (uint32_t)altuni->unienc<unicode4_size &&
2043 altuni->vs==-1 &&
2044 altuni->fid==0 &&
2045 (j = EncFromUni(altuni->unienc,enc))!= -1 )
2046 encoded[j] = i;
2047 /* I used to have code here to add these unencoded duplicates */
2048 /* but I don't really see any reason to do so. The main unicode */
2049 /* will occur, and any encoded duplicates so the glyph won't */
2050 /* vanish */
2051 }
2052 }
2053 }
2054
2055 /* Some glyphs have both a pua encoding and an encoding in a non-bmp */
2056 /* plane. Big5HK does and the AMS glyphs do */
2057 if ( enc->is_unicodefull && (sf->uni_interp == ui_trad_chinese ||
2058 sf->uni_interp == ui_ams )) {
2059 const int *pua = sf->uni_interp == ui_ams? amspua : cns14pua;
2060 for ( i=0xe000; i<0xf8ff; ++i ) {
2061 if ( pua[i-0xe000]!=0 )
2062 encoded[pua[i-0xe000]] = encoded[i];
2063 }
2064 }
2065
2066 if ( enc->psnames != NULL ) {
2067 /* Names are more important than unicode code points for some encodings */
2068 /* AdobeStandard for instance which won't work if you have a glyph */
2069 /* named "f_i" (must be "fi") even though the code point is correct */
2070 /* The code above would match f_i where AS requires fi, so force the */
2071 /* names to be correct. */
2072 for ( j=0; j<enc->char_cnt; ++j ) {
2073 if ( encoded[j]!=-1 && enc->psnames[j]!=NULL &&
2074 strcmp(sf->glyphs[encoded[j]]->name,enc->psnames[j])!=0 ) {
2075 free(sf->glyphs[encoded[j]]->name);
2076 sf->glyphs[encoded[j]]->name = copy(enc->psnames[j]);
2077 }
2078 }
2079 }
2080
2081 map = chunkalloc(sizeof(EncMap));
2082 map->enccount = map->encmax = base + extras;
2083 if ( map->enccount>0 ) {
2084 map->map = malloc(map->enccount*sizeof(int32_t));
2085 if ( base>0 )
2086 memcpy(map->map,encoded,base*sizeof(int32_t));
2087 if ( extras>0 )
2088 memcpy(map->map+base,unencoded,extras*sizeof(int32_t));
2089 }
2090 map->backmax = sf->glyphcnt;
2091 if ( sf->glyphcnt>0 ) {
2092 map->backmap = malloc(sf->glyphcnt*sizeof(int32_t));
2093 memset(map->backmap,-1,sf->glyphcnt*sizeof(int32_t)); /* Just in case there are some unencoded glyphs (duplicates perhaps) */
2094 for ( i = map->enccount-1; i>=0; --i ) if ( map->map[i]!=-1 )
2095 map->backmap[map->map[i]] = i;
2096 }
2097 map->enc = enc;
2098
2099 free(encoded);
2100 free(unencoded);
2101
2102 return( map );
2103 }
2104
2105 EncMap *CompactEncMap(EncMap *map, SplineFont *sf) {
2106 int i, inuse, gid;
2107 int32_t *newmap;
2108
2109 for ( i=inuse=0; i<map->enccount ; ++i )
2110 if ( (gid = map->map[i])!=-1 && SCWorthOutputting(sf->glyphs[gid]))
2111 ++inuse;
2112 newmap = malloc(inuse*sizeof(int32_t));
2113 for ( i=inuse=0; i<map->enccount ; ++i )
2114 if ( (gid = map->map[i])!=-1 && SCWorthOutputting(sf->glyphs[gid]))
2115 newmap[inuse++] = gid;
2116 free(map->map);
2117 map->map = newmap;
2118 map->enccount = inuse;
2119 map->encmax = inuse;
2120 map->enc = &custom;
2121 memset(map->backmap,-1,sf->glyphcnt*sizeof(int32_t));
2122 for ( i=inuse-1; i>=0; --i )
2123 if ( (gid=map->map[i])!=-1 )
2124 map->backmap[gid] = i;
2125 return( map );
2126 }
2127
2128 static void BCProtectUndoes( Undoes *undo,BDFChar *bc ) {
2129 BDFRefChar *brhead, *brprev=NULL, *brnext;
2130
2131 for ( ; undo!=NULL; undo=undo->next ) {
2132 switch ( undo->undotype ) {
2133 case ut_bitmap:
2134 for ( brhead=undo->u.bmpstate.refs; brhead != NULL; brhead=brnext ) {
2135 brnext = brhead->next;
2136 if ( brhead->bdfc == bc ) {
2137 BCPasteInto( &undo->u.bmpstate,bc,brhead->xoff,brhead->yoff,false,false );
2138 if ( brprev == NULL )
2139 undo->u.bmpstate.refs = brnext;
2140 else
2141 brprev->next = brnext;
2142 free( brhead );
2143 } else
2144 brprev = brhead;
2145 }
2146 break;
2147 case ut_multiple:
2148 BCProtectUndoes( undo->u.multiple.mult,bc );
2149 break;
2150 case ut_composit:
2151 BCProtectUndoes( undo->u.composit.bitmaps,bc );
2152 break;
2153 default: break;
2154 }
2155 }
2156 }
2157
2158 int SFReencode(SplineFont *sf, const char *encname, int force) {
2159 Encoding *new_enc;
2160 FontViewBase *fv = sf->fv;
2161
2162 if ( strmatch(encname,"compacted")==0 ) {
2163 fv->normal = EncMapCopy(fv->map);
2164 CompactEncMap(fv->map,sf);
2165 } else {
2166 new_enc = FindOrMakeEncoding(encname);
2167 if ( new_enc==NULL )
2168 return -1;
2169 if ( force )
2170 SFForceEncoding(sf,fv->map,new_enc);
2171 else if ( new_enc==&custom )
2172 fv->map->enc = &custom;
2173 else {
2174 EncMap *map = EncMapFromEncoding(sf,new_enc);
2175 EncMapFree(fv->map);
2176 if (fv->sf != NULL && fv->map == fv->sf->map) { fv->sf->map = map; }
2177 fv->map = map;
2178 if ( !no_windowing_ui )
2179 FVSetTitle(fv);
2180 }
2181 if ( fv->normal!=NULL ) {
2182 EncMapFree(fv->normal);
2183 if (fv->sf != NULL && fv->map == fv->sf->map) { fv->sf->map = NULL; }
2184 fv->normal = NULL;
2185 }
2186 SFReplaceEncodingBDFProps(sf,fv->map);
2187 }
2188 free(fv->selected);
2189 fv->selected = calloc(fv->map->enccount,sizeof(uint8_t));
2190 if ( !no_windowing_ui )
2191 FontViewReformatAll(sf);
2192
2193 return 0;
2194 }
2195
2196 void SFRemoveGlyph( SplineFont *sf,SplineChar *sc ) {
2197 struct splinecharlist *dep, *dnext;
2198 struct bdfcharlist *bdep, *bdnext;
2199 RefChar *rf, *refs, *rnext;
2200 BDFRefChar *bref, *brnext, *brprev;
2201 KernPair *kp, *kprev;
2202 int i;
2203 BDFFont *bdf;
2204 BDFChar *bfc, *dbc;
2205 int layer;
2206
2207 if ( sc==NULL )
2208 return;
2209
2210 /* Close any open windows */
2211 SCCloseAllViews(sc);
2212
2213 /* Turn any references to this glyph into inline copies of it */
2214 for ( dep=sc->dependents; dep!=NULL; dep=dnext ) {
2215 SplineChar *dsc = dep->sc;
2216 dnext = dep->next;
2217 /* May be more than one reference to us, colon has two refs to period */
2218 /* but only one dlist entry */
2219 for ( layer=0; layer<dsc->layer_cnt; ++layer ) {
2220 for ( rf = dsc->layers[layer].refs; rf!=NULL; rf=rnext ) {
2221 rnext = rf->next;
2222 if ( rf->sc==sc )
2223 SCRefToSplines(dsc,rf,layer);
2224 }
2225 }
2226 }
2227
2228 for ( layer=0; layer<sc->layer_cnt; ++layer ) {
2229 for ( refs=sc->layers[layer].refs; refs!=NULL; refs = rnext ) {
2230 rnext = refs->next;
2231 SCRemoveDependent(sc,refs,layer);
2232 }
2233 }
2234
2235 /* Remove any kerning pairs that look at this character */
2236 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL ) {
2237 /* Remove matching horizontal kerning pairs */
2238 for ( kprev=NULL, kp=sf->glyphs[i]->kerns; kp!=NULL; kprev = kp, kp=kp->next ) {
2239 if ( kp->sc==sc ) {
2240 if ( kprev==NULL )
2241 sf->glyphs[i]->kerns = kp->next;
2242 else
2243 kprev->next = kp->next;
2244 kp->next = NULL;
2245 KernPairsFree(kp);
2246 break;
2247 }
2248 }
2249 /* Remove matching vertical kerning pairs */
2250 for ( kprev=NULL, kp=sf->glyphs[i]->vkerns; kp!=NULL; kprev = kp, kp=kp->next ) {
2251 if ( kp->sc==sc ) {
2252 if ( kprev==NULL )
2253 sf->glyphs[i]->vkerns = kp->next;
2254 else
2255 kprev->next = kp->next;
2256 kp->next = NULL;
2257 KernPairsFree(kp);
2258 break;
2259 }
2260 }
2261 }
2262
2263 sf->glyphs[sc->orig_pos] = NULL;
2264
2265 for ( bdf=sf->bitmaps; bdf!=NULL; bdf = bdf->next ) {
2266 if ( sc->orig_pos<bdf->glyphcnt && (bfc = bdf->glyphs[sc->orig_pos])!= NULL ) {
2267 /* Turn any references to this glyph into inline copies of it */
2268 for ( bdep=bfc->dependents; bdep!=NULL; bdep=bdnext ) {
2269 dbc = bdep->bc;
2270 bdnext = bdep->next;
2271 brprev = NULL;
2272 /* May be more than one reference to us, colon has two refs to period */
2273 /* but only one dlist entry */
2274 for ( bref = dbc->refs; bref!=NULL; bref=brnext ) {
2275 brnext = bref->next;
2276 if ( bref->bdfc==bfc ) {
2277 BCPasteInto( dbc,bref->bdfc,bref->xoff,bref->yoff,false,false );
2278 if ( brprev == NULL ) dbc->refs = brnext;
2279 else brprev->next = brnext;
2280 free( bref );
2281 } else
2282 brprev = bref;
2283 }
2284 }
2285 /* Suppose we have deleted a reference from a composite glyph and than
2286 * going to remove the previously referenced glyph from the font. The
2287 * void reference still remains in the undoes stack, so that executing Undo/Redo
2288 * on the first glyph may lead to unpredictable effects. It is also
2289 * impossible to detect such problematic undoes checking just our
2290 * going-to-be-deleted glyph's dependents, because the composite character
2291 * no longer contains the problematic reference and so is not listed
2292 * in the dependents. Thus the only solution seems to be checking
2293 * every single glyph in the font.
2294 */
2295 for ( i=0; i<bdf->glyphcnt; i++ ) if (( dbc = bdf->glyphs[i] ) != NULL ) {
2296 BCProtectUndoes( dbc->undoes,bfc );
2297 BCProtectUndoes( dbc->redoes,bfc );
2298 }
2299 for ( bref=bfc->refs; refs!=NULL; bref = brnext ) {
2300 brnext = bref->next;
2301 BCRemoveDependent( bfc,bref );
2302 }
2303 bdf->glyphs[sc->orig_pos] = NULL;
2304 BDFCharFree(bfc);
2305 }
2306 }
2307
2308 SplineCharFree(sc);
2309 GlyphHashFree(sf);
2310 }
2311
2312 static int MapAddEncodingSlot(EncMap *map,int gid) {
2313 int enc;
2314
2315 if ( map->enccount>=map->encmax )
2316 map->map = realloc(map->map,(map->encmax+=10)*sizeof(int32_t));
2317 enc = map->enccount++;
2318 map->map[enc] = gid;
2319 map->backmap[gid] = enc;
2320 return( enc );
2321 }
2322
2323 void FVAddEncodingSlot(FontViewBase *fv,int gid) {
2324 EncMap *map = fv->map;
2325 int enc;
2326
2327 enc = MapAddEncodingSlot(map,gid);
2328
2329 fv->selected = realloc(fv->selected,map->enccount);
2330 fv->selected[enc] = 0;
2331 FVAdjustScrollBarRows(fv,enc);
2332 }
2333
2334 void SFAddEncodingSlot(SplineFont *sf,int gid) {
2335 FontViewBase *fv;
2336 for ( fv=sf->fv; fv!=NULL; fv = fv->nextsame )
2337 FVAddEncodingSlot(fv,gid);
2338 }
2339
2340 static int MapAddEnc(SplineFont *sf,SplineChar *sc,EncMap *basemap, EncMap *map,int baseenc, int gid, FontViewBase *fv) {
2341 int any = false, enc;
2342
2343 if ( gid>=map->backmax ) {
2344 map->backmap = realloc(map->backmap,(map->backmax+=10)*sizeof(int32_t));
2345 memset(map->backmap+map->backmax-10,-1,10*sizeof(int32_t));
2346 }
2347 if ( map->enc->psnames!=NULL ) {
2348 /* Check for multiple encodings */
2349 for ( enc = map->enc->char_cnt-1; enc>=0; --enc ) {
2350 if ( map->enc->psnames[enc]!=NULL && strcmp(sc->name,map->enc->psnames[enc])==0 ) {
2351 if ( !any ) {
2352 map->backmap[gid] = enc;
2353 any = true;
2354 }
2355 map->map[enc] = gid;
2356 }
2357 }
2358 } else {
2359 enc = SFFindSlot(sf,map,sc->unicodeenc,sc->name);
2360 if ( enc!=-1 ) {
2361 map->map[enc] = gid;
2362 map->backmap[gid] = enc;
2363 any = true;
2364 }
2365 }
2366 if ( basemap!=NULL && map->enc==basemap->enc && baseenc!=-1 ) {
2367 if ( baseenc>=map->enccount ) {
2368 if ( fv && map==fv->map )
2369 FVAddEncodingSlot(fv,gid);
2370 else
2371 MapAddEncodingSlot(map,gid);
2372 } else {
2373 map->map[baseenc] = gid;
2374 if ( map->backmap[gid]==-1 )
2375 map->backmap[gid] = baseenc;
2376 }
2377 any = true;
2378 }
2379 return( any );
2380 }
2381
2382 void SFAddGlyphAndEncode(SplineFont *sf,SplineChar *sc,EncMap *basemap, int baseenc) {
2383 int j, gid, mapfound = false;
2384 FontViewBase *fv;
2385 BDFFont *bdf;
2386
2387 if ( sf->cidmaster==NULL ) {
2388 if (sf->glyphcnt + 1 > sf->glyphmax)
2389 ExpandBuffer((void**)&sf->glyphs, sizeof(SplineChar*), 10,
2390 &sf->glyphmax);
2391 gid = sf->glyphcnt++;
2392 for (bdf = sf->bitmaps; bdf != NULL; bdf = bdf->next) {
2393 if (sf->glyphcnt + 1 > bdf->glyphmax)
2394 ExpandBuffer((void**)&bdf->glyphs, sizeof(BDFChar*),
2395 sf->glyphmax - bdf->glyphmax, &bdf->glyphmax);
2396 }
2397 if (sf->mm != NULL) {
2398 if (sf->mm->normal->glyphmax < sf->glyphmax)
2399 ExpandBuffer((void**)&sf->mm->normal->glyphs,
2400 sizeof(SplineChar*),
2401 sf->glyphmax - sf->mm->normal->glyphmax,
2402 &sf->mm->normal->glyphmax);
2403 for (j = 0; j < sf->mm->instance_count; ++j)
2404 if (sf->mm->instances[j]->glyphmax < sf->glyphmax)
2405 ExpandBuffer((void**)&sf->mm->instances[j]->glyphs,
2406 sizeof(SplineChar*),
2407 sf->glyphmax - sf->mm->instances[j]->glyphmax,
2408 &sf->mm->instances[j]->glyphmax);
2409 }
2410 for ( fv=sf->fv; fv!=NULL; fv = fv->nextsame ) {
2411 EncMap *map = fv->map;
2412 if ( gid>=map->backmax )
2413 map->backmap = realloc(map->backmap,(map->backmax=gid+10)*sizeof(int32_t));
2414 map->backmap[gid] = -1;
2415 }
2416 } else {
2417 gid = baseenc < 0 ? sf->glyphcnt : baseenc;
2418 if ( gid+1>=sf->glyphmax )
2419 sf->glyphs = realloc(sf->glyphs,(sf->glyphmax = gid+10)*sizeof(SplineChar *));
2420 if ( gid>=sf->glyphcnt ) {
2421 memset(sf->glyphs+sf->glyphcnt,0,(gid+1-sf->glyphcnt)*sizeof(SplineChar *));
2422 sf->glyphcnt = gid+1;
2423 for ( bdf = sf->cidmaster->bitmaps; bdf!=NULL; bdf=bdf->next ) {
2424 if ( gid+1>=bdf->glyphmax )
2425 bdf->glyphs = realloc(bdf->glyphs,(bdf->glyphmax=gid+10)*sizeof(BDFChar *));
2426 if ( gid+1>bdf->glyphcnt ) {
2427 memset(bdf->glyphs+bdf->glyphcnt,0,(gid+1-bdf->glyphcnt)*sizeof(BDFChar *));
2428 bdf->glyphcnt = gid+1;
2429 }
2430 }
2431 for ( fv=sf->fv; fv!=NULL; fv = fv->nextsame ) if ( fv->sf==sf ) {
2432 EncMap *map = fv->map;
2433 if ( gid>=map->backmax )
2434 map->backmap = realloc(map->backmap,(map->backmax=gid+10)*sizeof(int32_t));
2435 map->backmap[gid] = -1;
2436 }
2437 }
2438 }
2439 sf->glyphs[gid] = NULL;
2440 for ( fv=sf->fv; fv!=NULL; fv = fv->nextsame ) {
2441 EncMap *map = fv->map;
2442
2443 FVBiggerGlyphCache(fv,gid);
2444
2445 if ( !MapAddEnc(sf,sc,basemap,map,baseenc,gid,fv) )
2446 FVAddEncodingSlot(fv,gid);
2447 if ( map==basemap ) mapfound = true;
2448 if ( fv->normal!=NULL ) {
2449 if ( !MapAddEnc(sf,sc,basemap,fv->normal,baseenc,gid,fv))
2450 MapAddEncodingSlot(fv->normal,gid);
2451 }
2452 }
2453 if ( !mapfound && basemap!=NULL )
2454 MapAddEnc(sf,sc,basemap,basemap,baseenc,gid,fv);
2455 sf->glyphs[gid] = sc;
2456 sc->orig_pos = gid;
2457 sc->parent = sf;
2458 SFHashGlyph(sf,sc);
2459 }
2460
2461 static SplineChar *SplineCharMatch(SplineFont *parent,SplineChar *sc) {
2462 SplineChar *scnew = SFSplineCharCreate(parent);
2463
2464 scnew->parent = parent;
2465 scnew->orig_pos = sc->orig_pos;
2466 scnew->name = copy(sc->name);
2467 scnew->unicodeenc = sc->unicodeenc;
2468 scnew->width = sc->width;
2469 scnew->vwidth = sc->vwidth;
2470 scnew->widthset = true;
2471 return( scnew );
2472 }
2473
2474 void SFMatchGlyphs(SplineFont *sf,SplineFont *target,int addempties) {
2475 /* reorder sf so that its glyphs array is the same as that in target */
2476 int i, j, cnt, cnt2;
2477 SplineChar **glyphs;
2478 BDFFont *bdf;
2479
2480 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL )
2481 sf->glyphs[i]->ticked = false;
2482 if (( cnt = target->glyphcnt )<sf->glyphcnt ) cnt = sf->glyphcnt;
2483 glyphs = calloc(cnt,sizeof(SplineChar *));
2484 for ( i=0; i<target->glyphcnt; ++i ) if ( target->glyphs[i]!=NULL ) {
2485 SplineChar *sc = SFGetChar(sf,target->glyphs[i]->unicodeenc,target->glyphs[i]->name );
2486 if ( sc==NULL && addempties )
2487 sc = SplineCharMatch(sf,target->glyphs[i]);
2488 if ( sc!=NULL ) {
2489 glyphs[i] = sc;
2490 sc->ticked = true;
2491 }
2492 }
2493 for ( i=cnt2=0; i<sf->glyphcnt; ++i )
2494 if ( sf->glyphs[i]!=NULL && !sf->glyphs[i]->ticked )
2495 ++cnt2;
2496 if ( target->glyphcnt+cnt2>cnt ) {
2497 glyphs = realloc(glyphs,(target->glyphcnt+cnt2)*sizeof(SplineChar *));
2498 memset(glyphs+cnt,0,(target->glyphcnt+cnt2-cnt)*sizeof(SplineChar *));
2499 cnt = target->glyphcnt+cnt2;
2500 }
2501 j = target->glyphcnt;
2502 for ( i=0; i<sf->glyphcnt; ++i )
2503 if ( sf->glyphs[i]!=NULL && !sf->glyphs[i]->ticked )
2504 glyphs[j++] = sf->glyphs[i];
2505 free(sf->glyphs);
2506 sf->glyphs = glyphs;
2507 sf->glyphcnt = sf->glyphmax = cnt;
2508 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL )
2509 sf->glyphs[i]->orig_pos = i;
2510 for ( bdf = sf->bitmaps; bdf!=NULL; bdf = bdf->next ) {
2511 BDFChar **glyphs;
2512 glyphs = calloc(sf->glyphcnt,sizeof(BDFChar *));
2513 for ( i=0; i<bdf->glyphcnt; ++i ) if ( bdf->glyphs[i]!=NULL )
2514 glyphs[bdf->glyphs[i]->sc->orig_pos] = bdf->glyphs[i];
2515 free(bdf->glyphs);
2516 bdf->glyphs = glyphs;
2517 bdf->glyphcnt = bdf->glyphmax = sf->glyphcnt;
2518 }
2519 }
2520
2521 void MMMatchGlyphs(MMSet *mm) {
2522 /* reorder all instances so that they have the same orig_pos */
2523 int i, j, index, lasthole;
2524 SplineFont *sf, *base = NULL;
2525 SplineChar *sc, *scnew, *sc2;
2526
2527 for ( i = 0; i<mm->instance_count; ++i ) if ( mm->instances[i]!=NULL ) {
2528 base = mm->instances[i];
2529 break;
2530 }
2531 if ( base==NULL )
2532 return;
2533
2534 /* First build up an ordering that uses all glyphs found in any of the */
2535 /* sub-fonts, "base" will be the start of it. We will add glyphs to */
2536 /* "base" as needed */
2537 lasthole = -1;
2538 for ( i = 0; i<mm->instance_count; ++i ) if ( (sf=mm->instances[i])!=NULL && sf!=NULL ) {
2539 for ( j=0; j<sf->glyphcnt; ++j ) if ( (sc=sf->glyphs[j])!=NULL ) {
2540 if ( j<base->glyphcnt && base->glyphs[j]!=NULL &&
2541 base->glyphs[j]->unicodeenc==sc->unicodeenc &&
2542 strcmp(base->glyphs[j]->name,sc->name)==0 )
2543 continue; /* It's good, and in the same place */
2544 else if ( (sc2=SFGetChar(base,sc->unicodeenc,sc->name))!=NULL &&
2545 sc2->unicodeenc==sc->unicodeenc &&
2546 strcmp(sc2->name,sc->name)==0 )
2547 continue; /* Well, it's in there somewhere */
2548 else {
2549 /* We need to add it */
2550 if ( j<base->glyphcnt && base->glyphs[j]==NULL )
2551 index = j;
2552 else {
2553 for ( ++lasthole ; lasthole<base->glyphcnt && base->glyphs[lasthole]!=NULL; ++lasthole );
2554 index = lasthole;
2555 if ( lasthole>=base->glyphmax )
2556 base->glyphs = realloc(base->glyphs,(base->glyphmax+=20)*sizeof(SplineChar *));
2557 if ( lasthole>=base->glyphcnt )
2558 base->glyphcnt = lasthole+1;
2559 }
2560 base->glyphs[index] = scnew = SplineCharMatch(base,sc);
2561 scnew->orig_pos = index;
2562 }
2563 }
2564 }
2565
2566 /* Now force all other instances to match */
2567 for ( i = 0; i<mm->instance_count; ++i ) if ( (sf=mm->instances[i])!=NULL && sf!=base )
2568 SFMatchGlyphs(sf,base,true);
2569 if ( mm->normal!=NULL )
2570 SFMatchGlyphs(mm->normal,base,true);
2571 }
2572
2573 int32_t UniFromEnc(int enc, Encoding *encname) {
2574 char from[20];
2575 unichar_t to[20];
2576 ICONV_CONST char *fpt;
2577 char *tpt;
2578 size_t fromlen, tolen;
2579
2580 if ( !encname || encname->is_custom || encname->is_original )
2581 return( -1 );
2582 if ( enc>=encname->char_cnt )
2583 return( -1 );
2584 if ( encname->is_unicodebmp || encname->is_unicodefull )
2585 return( enc );
2586 if ( encname->unicode!=NULL )
2587 return( encname->unicode[enc] );
2588 else if ( encname->tounicode ) {
2589 /* To my surprise, on RH9, doing a reset on conversion of CP1258->UCS2 */
2590 /* causes subsequent calls to return garbage */
2591 if ( encname->iso_2022_escape_len ) {
2592 tolen = sizeof(to); fromlen = 0;
2593 iconv(encname->tounicode,NULL,&fromlen,NULL,&tolen); /* Reset state */
2594 }
2595 fpt = from; tpt = (char *) to; tolen = sizeof(to);
2596 if ( encname->has_1byte && enc<256 ) {
2597 *(char *) fpt = enc;
2598 fromlen = 1;
2599 } else if ( encname->has_2byte ) {
2600 if ( encname->iso_2022_escape_len )
2601 strncpy(from,encname->iso_2022_escape,encname->iso_2022_escape_len );
2602 fromlen = encname->iso_2022_escape_len;
2603 from[fromlen++] = enc>>8;
2604 from[fromlen++] = enc&0xff;
2605 }
2606 if ( iconv(encname->tounicode,&fpt,&fromlen,&tpt,&tolen)==(size_t) -1 )
2607 return( -1 );
2608 if ( tpt-(char *) to == 0 ) {
2609 /* This strange call appears to be what we need to make CP1258->UCS2 */
2610 /* work. It's supposed to reset the state and give us the shift */
2611 /* out. As there is no state, and no shift out I have no idea why*/
2612 /* this works, but it does. */
2613 if ( iconv(encname->tounicode,NULL,&fromlen,&tpt,&tolen)==(size_t) -1 )
2614 return( -1 );
2615 }
2616 if ( tpt-(char *) to == sizeof(unichar_t) )
2617 {
2618 return( to[0] );
2619 }
2620 } else if ( encname->tounicode_func!=NULL ) {
2621 return( (encname->tounicode_func)(enc) );
2622 }
2623 return( -1 );
2624 }
2625
2626 int32_t EncFromUni(int32_t uni, Encoding *enc) {
2627 unichar_t from[20];
2628 unsigned char to[20];
2629 ICONV_CONST char *fpt;
2630 char *tpt;
2631 size_t fromlen, tolen;
2632 int i;
2633
2634 if ( enc->is_custom || enc->is_original || enc->is_compact || uni==-1 )
2635 return( -1 );
2636 if ( enc->is_unicodebmp || enc->is_unicodefull )
2637 return( uni<enc->char_cnt ? uni : -1 );
2638
2639 if ( enc->unicode!=NULL ) {
2640 for ( i=0; i<enc->char_cnt; ++i ) {
2641 if ( enc->unicode[i]==uni )
2642 return( i );
2643 }
2644 return( -1 );
2645 } else if ( enc->fromunicode!=NULL ) {
2646 /* I don't see how there can be any state to reset in this direction */
2647 /* So I don't reset it */
2648 from[0] = uni;
2649 fromlen = sizeof(unichar_t);
2650 fpt = (char *) from; tpt = (char *) to; tolen = sizeof(to);
2651 iconv(enc->fromunicode,NULL,NULL,NULL,NULL); /* reset shift in/out, etc. */
2652 if ( iconv(enc->fromunicode,&fpt,&fromlen,&tpt,&tolen)==(size_t) -1 )
2653 return( -1 );
2654 if ( tpt-(char *) to == 1 )
2655 return( to[0] );
2656 if ( enc->iso_2022_escape_len!=0 ) {
2657 if ( tpt-(char *) to == enc->iso_2022_escape_len+2 &&
2658 strncmp((char *) to,enc->iso_2022_escape,enc->iso_2022_escape_len)==0 )
2659 return( (to[enc->iso_2022_escape_len]<<8) | to[enc->iso_2022_escape_len+1] );
2660 } else {
2661 if ( tpt-(char *) to == sizeof(unichar_t) )
2662 return( (to[0]<<8) | to[1] );
2663 }
2664 } else if ( enc->fromunicode_func!=NULL ) {
2665 return( (enc->fromunicode_func)(uni) );
2666 }
2667 return( -1 );
2668 }
2669
2670 int32_t EncFromName(const char *name,enum uni_interp interp,Encoding *encname) {
2671 int i;
2672 if ( encname->psnames!=NULL ) {
2673 for ( i=0; i<encname->char_cnt; ++i )
2674 if ( encname->psnames[i]!=NULL && strcmp(name,encname->psnames[i])==0 )
2675 return( i );
2676 }
2677 i = UniFromName(name,interp,encname);
2678 if ( i==-1 && strlen(name)==4 ) {
2679 /* MS says use this kind of name, Adobe says use the one above */
2680 char *end;
2681 i = strtol(name,&end,16);
2682 if ( i<0 || i>0xffff || *end!='\0' )
2683 return( -1 );
2684 }
2685 return( EncFromUni(i,encname));
2686 }
2687
2688 void SFExpandGlyphCount(SplineFont *sf, int newcnt) {
2689 int old = sf->glyphcnt;
2690 FontViewBase *fv;
2691
2692 if ( old>=newcnt )
2693 return;
2694 if ( sf->glyphmax<newcnt ) {
2695 sf->glyphs = realloc(sf->glyphs,newcnt*sizeof(SplineChar *));
2696 sf->glyphmax = newcnt;
2697 }
2698 memset(sf->glyphs+sf->glyphcnt,0,(newcnt-sf->glyphcnt)*sizeof(SplineChar *));
2699 sf->glyphcnt = newcnt;
2700
2701 for ( fv=sf->fv; fv!=NULL; fv=fv->nextsame ) {
2702 if ( fv->sf==sf ) { /* Beware of cid keyed fonts which might look at a different subfont */
2703 if ( fv->normal!=NULL )
2704 continue; /* If compacted then we haven't added any glyphs so haven't changed anything */
2705 /* Don't display any of these guys, so not mapped. */
2706 /* No change to selection, or to map->map, but change to backmap */
2707 if ( newcnt>fv->map->backmax )
2708 fv->map->backmap = realloc(fv->map->backmap,(fv->map->backmax = newcnt+5)*sizeof(int32_t));
2709 memset(fv->map->backmap+old,-1,(newcnt-old)*sizeof(int32_t));
2710 }
2711 }
2712 }