Debian Codegraph @

fontforgeexe/encodingui.c

raw symbols SCIP
1 /* -*- coding: utf-8 -*- */
2 /* Copyright (C) 2000-2012 by George Williams */
3 /*
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13
14 * The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <fontforge-config.h>
30
31 #include "bitmapchar.h"
32 #include "encoding.h"
33 #include "fontforgeui.h"
34 #include "gfile.h"
35 #include "ustring.h"
36 #include "utype.h"
37
38 #include <dirent.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41
42 static GTextInfo *EncodingList(void) {
43 GTextInfo *ti;
44 int i;
45 Encoding *item;
46
47 i = 0;
48 for ( item=enclist; item!=NULL ; item=item->next )
49 if ( !item->builtin )
50 ++i;
51 ti = calloc(i+1,sizeof(GTextInfo));
52 i = 0;
53 for ( item=enclist; item!=NULL ; item=item->next )
54 if ( !item->builtin )
55 ti[i++].text = uc_copy(item->enc_name);
56 if ( i!=0 )
57 ti[0].selected = true;
58 return( ti );
59 }
60
61 #define CID_Encodings 1001
62
63 static int DE_Delete(GGadget *g, GEvent *e) {
64 GWindow gw;
65 int *done;
66 GGadget *list;
67 int sel,i;
68 Encoding *item;
69
70 if ( e->type==et_controlevent &&
71 (e->u.control.subtype == et_buttonactivate ||
72 e->u.control.subtype == et_listdoubleclick )) {
73 gw = GGadgetGetWindow(g);
74 done = GDrawGetUserData(gw);
75 list = GWidgetGetControl(gw,CID_Encodings);
76 sel = GGadgetGetFirstListSelectedItem(list);
77 i=0;
78 for ( item=enclist; item!=NULL; item=item->next ) {
79 if ( item->builtin )
80 /* Do Nothing */;
81 else if ( i==sel )
82 break;
83 else
84 ++i;
85 }
86 if ( item!=NULL )
87 DeleteEncoding(item);
88 *done = true;
89 }
90 return( true );
91 }
92
93 static int DE_Cancel(GGadget *g, GEvent *e) {
94 GWindow gw;
95 int *done;
96
97 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
98 gw = GGadgetGetWindow(g);
99 done = GDrawGetUserData(gw);
100 *done = true;
101 }
102 return( true );
103 }
104
105 static int de_e_h(GWindow gw, GEvent *event) {
106 if ( event->type==et_close ) {
107 int *done = GDrawGetUserData(gw);
108 *done = true;
109 } else if ( event->type == et_char ) {
110 return( false );
111 }
112 return( true );
113 }
114
115 void RemoveEncoding(void) {
116 GRect pos;
117 GWindow gw;
118 GWindowAttrs wattrs;
119 GGadgetCreateData gcd[5];
120 GTextInfo label[5];
121 Encoding *item;
122 int done = 0;
123
124 for ( item=enclist; item!=NULL && item->builtin; item=item->next );
125 if ( item==NULL )
126 return;
127
128 memset(&gcd,0,sizeof(gcd));
129 memset(&label,0,sizeof(label));
130 memset(&wattrs,0,sizeof(wattrs));
131 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_restrict|wam_isdlg;
132 wattrs.event_masks = ~(1<<et_charup);
133 wattrs.restrict_input_to_me = 1;
134 wattrs.is_dlg = 1;
135 wattrs.undercursor = 1;
136 wattrs.cursor = ct_pointer;
137 wattrs.utf8_window_title = _("Remove Encoding");
138 pos.x = pos.y = 0;
139 pos.width = GGadgetScale(GDrawPointsToPixels(NULL,150));
140 pos.height = GDrawPointsToPixels(NULL,110);
141 gw = GDrawCreateTopWindow(NULL,&pos,de_e_h,&done,&wattrs);
142
143 gcd[0].gd.pos.x = 10; gcd[0].gd.pos.y = 6;
144 gcd[0].gd.pos.width = 130; gcd[0].gd.pos.height = 5*12+10;
145 gcd[0].gd.flags = gg_visible | gg_enabled;
146 gcd[0].gd.cid = CID_Encodings;
147 gcd[0].gd.u.list = EncodingList();
148 gcd[0].gd.handle_controlevent = DE_Delete;
149 gcd[0].creator = GListCreate;
150
151 gcd[2].gd.pos.x = -10; gcd[2].gd.pos.y = gcd[0].gd.pos.y+gcd[0].gd.pos.height+5;
152 gcd[2].gd.pos.width = -1; gcd[2].gd.pos.height = 0;
153 gcd[2].gd.flags = gg_visible | gg_enabled | gg_but_cancel ;
154 label[2].text = (unichar_t *) _("_Cancel");
155 label[2].text_is_1byte = true;
156 label[2].text_in_resource = true;
157 gcd[2].gd.label = &label[2];
158 gcd[2].gd.mnemonic = 'C';
159 gcd[2].gd.handle_controlevent = DE_Cancel;
160 gcd[2].creator = GButtonCreate;
161
162 gcd[1].gd.pos.x = 10-3; gcd[1].gd.pos.y = gcd[2].gd.pos.y-3;
163 gcd[1].gd.pos.width = -1; gcd[1].gd.pos.height = 0;
164 gcd[1].gd.flags = gg_visible | gg_enabled | gg_but_default ;
165 label[1].text = (unichar_t *) _("_Delete");
166 label[1].text_is_1byte = true;
167 label[1].text_in_resource = true;
168 gcd[1].gd.mnemonic = 'D';
169 gcd[1].gd.label = &label[1];
170 gcd[1].gd.handle_controlevent = DE_Delete;
171 gcd[1].creator = GButtonCreate;
172
173 gcd[3].gd.pos.x = 2; gcd[3].gd.pos.y = 2;
174 gcd[3].gd.pos.width = pos.width-4; gcd[3].gd.pos.height = pos.height-2;
175 gcd[3].gd.flags = gg_enabled | gg_visible | gg_pos_in_pixels;
176 gcd[3].creator = GGroupCreate;
177
178 GGadgetsCreate(gw,gcd);
179 GTextInfoListFree(gcd[0].gd.u.list);
180
181 GDrawSetVisible(gw,true);
182 while ( !done )
183 GDrawProcessOneEvent(NULL);
184 GDrawDestroyWindow(gw);
185 }
186
187 Encoding *MakeEncoding(SplineFont *sf,EncMap *map) {
188 char *name;
189 int i, gid;
190 Encoding *item, *temp;
191 SplineChar *sc;
192
193 if ( map->enc!=&custom )
194 return(NULL);
195
196 name = gwwv_ask_string(_("Please name this encoding"),NULL,_("Please name this encoding"));
197 if ( name==NULL )
198 return(NULL);
199 item = calloc(1,sizeof(Encoding));
200 item->enc_name = name;
201 item->only_1byte = item->has_1byte = true;
202 item->char_cnt = map->enccount;
203 item->unicode = calloc(map->enccount,sizeof(int32_t));
204 for ( i=0; i<map->enccount; ++i ) if ( (gid = map->map[i])!=-1 && (sc=sf->glyphs[gid])!=NULL ) {
205 if ( sc->unicodeenc!=-1 )
206 item->unicode[i] = sc->unicodeenc;
207 else if ( strcmp(sc->name,".notdef")!=0 ) {
208 if ( item->psnames==NULL )
209 item->psnames = calloc(map->enccount,sizeof(unichar_t *));
210 item->psnames[i] = copy(sc->name);
211 }
212 }
213 RemoveMultiples(item);
214
215 if ( enclist == NULL )
216 enclist = item;
217 else {
218 for ( temp=enclist; temp->next!=NULL; temp=temp->next );
219 temp->next = item;
220 }
221 DumpPfaEditEncodings();
222 return( item );
223 }
224
225 void LoadEncodingFile(void) {
226 static char filter[] = "*{.ps,.PS,.txt,.TXT,.enc,.ENC,GlyphOrderAndAliasDB}";
227 char *fn;
228 char *filename;
229
230 fn = gwwv_open_filename(_("Load Encoding"), NULL, filter, NULL);
231 if ( fn==NULL )
232 return;
233 filename = utf82def_copy(fn);
234 ParseEncodingFile(filename, NULL);
235 free(fn); free(filename);
236 DumpPfaEditEncodings();
237 }
238
239 void SFFindNearTop(SplineFont *sf) {
240 FontView *fv;
241 EncMap *map;
242 int i,k,gid;
243
244 if ( sf->cidmaster!=NULL )
245 sf = sf->cidmaster;
246 if ( sf->subfontcnt==0 ) {
247 for ( fv=(FontView *) sf->fv; fv!=NULL; fv=(FontView *) (fv->b.nextsame) ) {
248 map = fv->b.map;
249 fv->sc_near_top = NULL;
250 for ( i=fv->rowoff*fv->colcnt; i<map->enccount && i<(fv->rowoff+fv->rowcnt)*fv->colcnt; ++i )
251 if ( (gid=map->map[i])!=-1 && sf->glyphs[gid]!=NULL ) {
252 fv->sc_near_top = sf->glyphs[gid];
253 break;
254 }
255 }
256 } else {
257 for ( fv=(FontView *) sf->fv; fv!=NULL; fv=(FontView *) (fv->b.nextsame) ) {
258 map = fv->b.map;
259 fv->sc_near_top = NULL;
260 for ( i=fv->rowoff*fv->colcnt; i<map->enccount && i<(fv->rowoff+fv->rowcnt)*fv->colcnt; ++i ) {
261 for ( k=0; k<sf->subfontcnt; ++k )
262 if ( (gid=map->map[i])!=-1 &&
263 gid<sf->subfonts[k]->glyphcnt &&
264 sf->subfonts[k]->glyphs[gid]!=NULL )
265 fv->sc_near_top = sf->subfonts[k]->glyphs[gid];
266 }
267 }
268 }
269 }
270
271 void SFRestoreNearTop(SplineFont *sf) {
272 FontView *fv;
273
274 for ( fv=(FontView *) sf->fv; fv!=NULL; fv=(FontView *) (fv->b.nextsame) ) if ( fv->sc_near_top!=NULL ) {
275 /* Note: For CID keyed fonts, we don't care if sc is in the currently */
276 /* displayed font, all we care about is the CID */
277 int enc = fv->b.map->backmap[fv->sc_near_top->orig_pos];
278 if ( enc!=-1 ) {
279 fv->rowoff = enc/fv->colcnt;
280 GScrollBarSetPos(fv->vsb,fv->rowoff);
281 /* Don't ask for an expose event yet. We'll get one soon enough */
282 }
283 }
284 }
285
286 struct block {
287 int cur, tot;
288 char **maps;
289 char **dirs;
290 };
291
292 static void AddToBlock(struct block *block,char *mapname, char *dir) {
293 int i, val, j;
294 int len = strlen(mapname);
295
296 if ( mapname[len-7]=='.' ) len -= 7;
297 for ( i=0; i<block->cur; ++i ) {
298 if ( (val=strncmp(block->maps[i],mapname,len))==0 )
299 return; /* Duplicate */
300 else if ( val>0 )
301 break;
302 }
303 if ( block->tot==0 ) {
304 block->tot = 10;
305 block->maps = malloc(10*sizeof(char *));
306 block->dirs = malloc(10*sizeof(char *));
307 } else if ( block->cur>=block->tot ) {
308 block->tot += 10;
309 block->maps = realloc(block->maps,block->tot*sizeof(char *));
310 block->dirs = realloc(block->dirs,block->tot*sizeof(char *));
311 }
312 for ( j=block->cur; j>=i; --j ) {
313 block->maps[j+1] = block->maps[j];
314 block->dirs[j+1] = block->dirs[j];
315 }
316 block->maps[i] = copyn(mapname,len);
317 block->dirs[i] = dir;
318 ++block->cur;
319 }
320
321 static void FindMapsInDir(struct block *block,char *dir) {
322 struct dirent *ent;
323 DIR *d;
324 int len;
325 char *pt, *pt2;
326
327 if ( dir==NULL )
328 return;
329 /* format of cidmap filename "?*-?*-[0-9]*.cidmap" */
330 d = opendir(dir);
331 if ( d==NULL )
332 return;
333 while ( (ent = readdir(d))!=NULL ) {
334 if ( (len = strlen(ent->d_name))<8 )
335 continue;
336 if ( strcmp(ent->d_name+len-7,".cidmap")!=0 )
337 continue;
338 pt = strchr(ent->d_name, '-');
339 if ( pt==NULL || pt==ent->d_name )
340 continue;
341 pt2 = strchr(pt+1, '-' );
342 if ( pt2==NULL || pt2==pt+1 || !isdigit(pt2[1]))
343 continue;
344 AddToBlock(block,ent->d_name,dir);
345 }
346 closedir(d);
347 }
348
349 struct cidmap *AskUserForCIDMap(void) {
350 struct block block;
351 struct cidmap *map = NULL;
352 char buffer[200];
353 char **choices;
354 int i,ret;
355 char *filename=NULL;
356 char *reg, *ord, *pt;
357 int supplement;
358
359 memset(&block,'\0',sizeof(block));
360 for ( map = cidmaps; map!=NULL; map = map->next ) {
361 sprintf(buffer,"%s-%s-%d", map->registry, map->ordering, map->supplement);
362 AddToBlock(&block,buffer,NULL);
363 }
364 FindMapsInDir(&block,".");
365 char* sharedir = smprintf("%s/cidmap", getShareDir());
366 FindMapsInDir(&block,sharedir);
367 free(sharedir);
368
369 choices = calloc(block.cur+2,sizeof(unichar_t *));
370 choices[0] = copy(_("Browse..."));
371 for ( i=0; i<block.cur; ++i )
372 choices[i+1] = copy(block.maps[i]);
373 ret = gwwv_choose(_("Find a cidmap file..."),(const char **) choices,i+1,0,_("Please select a CID ordering"));
374 for ( i=0; i<=block.cur; ++i )
375 free( choices[i] );
376 free(choices);
377 if ( ret==0 ) {
378 filename = gwwv_open_filename(_("Find a cidmap file..."),NULL,
379 "?*-?*-[0-9]*.cidmap",NULL);
380 if ( filename==NULL )
381 ret = -1;
382 }
383 if ( ret!=-1 ) {
384 if ( filename!=NULL )
385 /* Do nothing for now */;
386 else if ( block.dirs[ret-1]!=NULL ) {
387 filename = malloc(strlen(block.dirs[ret-1])+strlen(block.maps[ret-1])+3+8);
388 strcpy(filename,block.dirs[ret-1]);
389 strcat(filename,"/");
390 strcat(filename,block.maps[ret-1]);
391 strcat(filename,".cidmap");
392 }
393 if ( ret!=0 )
394 reg = block.maps[ret-1];
395 else {
396 reg = strrchr(filename,'/');
397 if ( reg==NULL ) reg = filename;
398 else ++reg;
399 reg = copy(reg);
400 }
401 pt = strchr(reg,'-');
402 if ( pt==NULL )
403 ret = -1;
404 else {
405 *pt = '\0';
406 ord = pt+1;
407 pt = strchr(ord,'-');
408 if ( pt==NULL )
409 ret = -1;
410 else {
411 *pt = '\0';
412 supplement = strtol(pt+1,NULL,10);
413 }
414 }
415 if ( ret == -1 )
416 /* No map */;
417 else if ( filename==NULL )
418 map = FindCidMap(reg,ord,supplement,NULL);
419 else {
420 map = LoadMapFromFile(filename,reg,ord,supplement);
421 free(filename);
422 }
423 if ( ret!=0 && reg!=block.maps[ret-1] )
424 free(reg);
425 /*free(filename);*/ /* Freed by loadmap */
426 }
427 for ( i=0; i<block.cur; ++i )
428 free( block.maps[i]);
429 free(block.maps);
430 free(block.dirs);
431 return( map );
432 }
433
434 GTextInfo encodingtypes[] = {
435 { (unichar_t *) N_("Custom"), NULL, 0, 0, (void *) "Custom", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
436 { (unichar_t *) N_("Encoding|Glyph Order"), NULL, 0, 0, (void *) "Original", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
437 { NULL, NULL, 0, 0, NULL, NULL, 1, 0, 0, 0, 0, 1, 0, 0, 0, '\0'}, /* Line */
438 { (unichar_t *) N_("ISO 8859-1 (Latin1)"), NULL, 0, 0, (void *) "iso8859-1", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
439 { (unichar_t *) N_("ISO 8859-15 (Latin0)"), NULL, 0, 0, (void *) "iso8859-15", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
440 { (unichar_t *) N_("ISO 8859-2 (Latin2)"), NULL, 0, 0, (void *) "iso8859-2", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
441 { (unichar_t *) N_("ISO 8859-3 (Latin3)"), NULL, 0, 0, (void *) "iso8859-3", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
442 { (unichar_t *) N_("ISO 8859-4 (Latin4)"), NULL, 0, 0, (void *) "iso8859-4", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
443 { (unichar_t *) N_("ISO 8859-9 (Latin5)"), NULL, 0, 0, (void *) "iso8859-9", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
444 { (unichar_t *) N_("ISO 8859-10 (Latin6)"), NULL, 0, 0, (void *) "iso8859-10", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
445 { (unichar_t *) N_("ISO 8859-13 (Latin7)"), NULL, 0, 0, (void *) "iso8859-13", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
446 { (unichar_t *) N_("ISO 8859-14 (Latin8)"), NULL, 0, 0, (void *) "iso8859-14", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
447 { (unichar_t *) N_("ISO 8859-16 (Latin10)"), NULL, 0, 0, (void *) "iso8859-16", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
448 { NULL, NULL, 0, 0, NULL, NULL, 1, 0, 0, 0, 0, 1, 0, 0, 0, '\0'}, /* Line */
449 { (unichar_t *) N_("ISO 8859-5 (Cyrillic)"), NULL, 0, 0, (void *) "iso8859-5", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
450 { (unichar_t *) N_("KOI8-R (Cyrillic)"), NULL, 0, 0, (void *) "koi8-r", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
451 { (unichar_t *) N_("ISO 8859-6 (Arabic)"), NULL, 0, 0, (void *) "iso8859-6", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
452 { (unichar_t *) N_("ISO 8859-7 (Greek)"), NULL, 0, 0, (void *) "iso8859-7", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
453 { (unichar_t *) N_("ISO 8859-8 (Hebrew)"), NULL, 0, 0, (void *) "iso8859-8", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
454 { (unichar_t *) N_("ISO 8859-11 (Thai)"), NULL, 0, 0, (void *) "iso8859-11", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
455 { NULL, NULL, 0, 0, NULL, NULL, 1, 0, 0, 0, 0, 1, 0, 0, 0, '\0'}, /* Line */
456 { (unichar_t *) N_("Macintosh Latin"), NULL, 0, 0, (void *) "mac", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
457 { (unichar_t *) N_("Windows Latin (\"ANSI\")"), NULL, 0, 0, (void *) "win", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
458 { (unichar_t *) N_("Adobe Standard"), NULL, 0, 0, (void *) "AdobeStandard", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
459 { (unichar_t *) N_("Symbol"), NULL, 0, 0, (void *) "Symbol", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
460 { (unichar_t *) NU_("ΤεΧ Base (8r)"), NULL, 0, 0, (void *) "TeX-Base-Encoding", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
461 { NULL, NULL, 0, 0, NULL, NULL, 1, 0, 0, 0, 0, 1, 0, 0, 0, '\0'}, /* Line */
462 { (unichar_t *) N_("ISO 10646-1 (Unicode, BMP)"), NULL, 0, 0, (void *) "UnicodeBmp", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
463 { (unichar_t *) N_("ISO 10646-1 (Unicode, Full)"), NULL, 0, 0, (void *) "UnicodeFull", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
464 /*{ (unichar_t *) N_("ISO 10646-? (by plane) ..."), NULL, 0, 0, (void *) em_unicodeplanes, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' }, */
465 { NULL, NULL, 0, 0, NULL, NULL, 1, 0, 0, 0, 0, 1, 0, 0, 0, '\0'}, /* Line */
466 { (unichar_t *) N_("SJIS (Kanji)"), NULL, 0, 0, (void *) "sjis", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
467 { (unichar_t *) N_("JIS 208 (Kanji)"), NULL, 0, 0, (void *) "jis208", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
468 { (unichar_t *) N_("JIS 212 (Kanji)"), NULL, 0, 0, (void *) "jis212", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
469 { (unichar_t *) N_("Wansung (Korean)"), NULL, 0, 0, (void *) "wansung", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
470 { (unichar_t *) N_("KSC 5601-1987 (Korean)"), NULL, 0, 0, (void *) "ksc5601", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
471 { (unichar_t *) N_("Johab (Korean)"), NULL, 0, 0, (void *) "johab", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
472 { (unichar_t *) N_("GB 2312 (Simp. Chinese)"), NULL, 0, 0, (void *) "gb2312", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
473 { (unichar_t *) N_("EUC GB 2312 (Chinese)"), NULL, 0, 0, (void *) "gb2312pk", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
474 { (unichar_t *) N_("Big5 (Trad. Chinese)"), NULL, 0, 0, (void *) "big5", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
475 { (unichar_t *) N_("Big5 HKSCS (Trad. Chinese)"), NULL, 0, 0, (void *) "big5hkscs", NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
476 GTEXTINFO_EMPTY
477 };
478
479 static void EncodingInit(void) {
480 int i;
481 static int done = false;
482
483 if ( done )
484 return;
485 done = true;
486 for ( i=0; i<sizeof(encodingtypes)/sizeof(encodingtypes[0])-1; ++i ) {
487 if ( !encodingtypes[i].line )
488 encodingtypes[i].text = (unichar_t *) S_((char *) encodingtypes[i].text);
489 }
490 }
491
492 GMenuItem *GetEncodingMenu(void (*func)(GWindow,GMenuItem *,GEvent *),
493 Encoding *current) {
494 GMenuItem *mi;
495 int i, cnt;
496 Encoding *item;
497
498 EncodingInit();
499
500 cnt = 0;
501 for ( item=enclist; item!=NULL ; item=item->next )
502 if ( !item->hidden )
503 ++cnt;
504 i = cnt+1;
505 i += sizeof(encodingtypes)/sizeof(encodingtypes[0]);
506 mi = calloc(i+1,sizeof(GMenuItem));
507 for ( i=0; i<sizeof(encodingtypes)/sizeof(encodingtypes[0])-1; ++i ) {
508 mi[i].ti = encodingtypes[i];
509 if ( !mi[i].ti.line ) {
510 mi[i].ti.text = utf82u_copy((char *) (mi[i].ti.text));
511 mi[i].ti.checkable = true;
512 if ( strmatch(mi[i].ti.userdata,current->enc_name)==0 ||
513 (current->iconv_name!=NULL && strmatch(mi[i].ti.userdata,current->iconv_name)==0))
514 mi[i].ti.checked = true;
515 }
516 mi[i].ti.text_is_1byte = false;
517 mi[i].ti.fg = mi[i].ti.bg = COLOR_DEFAULT;
518 mi[i].invoke = func;
519 }
520 if ( cnt!=0 ) {
521 mi[i].ti.fg = mi[i].ti.bg = COLOR_DEFAULT;
522 mi[i++].ti.line = true;
523 for ( item=enclist; item!=NULL ; item=item->next )
524 if ( !item->hidden ) {
525 mi[i].ti.text = utf82u_copy(item->enc_name);
526 mi[i].ti.userdata = (void *) item->enc_name;
527 mi[i].ti.fg = mi[i].ti.bg = COLOR_DEFAULT;
528 mi[i].ti.checkable = true;
529 if ( item==current )
530 mi[i].ti.checked = true;
531 mi[i++].invoke = func;
532 }
533 }
534 return( mi );
535 }
536
537 GTextInfo *GetEncodingTypes(void) {
538 GTextInfo *ti;
539 int i, cnt;
540 Encoding *item;
541
542 EncodingInit();
543
544 cnt = 0;
545 for ( item=enclist; item!=NULL ; item=item->next )
546 if ( !item->hidden )
547 ++cnt;
548 i = cnt + sizeof(encodingtypes)/sizeof(encodingtypes[0]);
549 ti = calloc(i+1,sizeof(GTextInfo));
550 memcpy(ti,encodingtypes,sizeof(encodingtypes)-sizeof(encodingtypes[0]));
551 for ( i=0; i<sizeof(encodingtypes)/sizeof(encodingtypes[0])-1; ++i ) {
552 ti[i].text = (unichar_t *) copy((char *) ti[i].text);
553 }
554 if ( cnt!=0 ) {
555 ti[i++].line = true;
556 for ( item=enclist; item!=NULL ; item=item->next )
557 if ( !item->hidden ) {
558 ti[i].text = uc_copy(item->enc_name);
559 ti[i++].userdata = (void *) item->enc_name;
560 }
561 }
562 return( ti );
563 }
564
565 GTextInfo *EncodingTypesFindEnc(GTextInfo *encodingtypes, Encoding *enc) {
566 int i;
567 char *name;
568 Encoding *new_enc;
569
570 for ( i=0; encodingtypes[i].text!=NULL || encodingtypes[i].line; ++i ) {
571 if ( encodingtypes[i].text==NULL )
572 continue;
573 name = encodingtypes[i].userdata;
574 new_enc = FindOrMakeEncoding(name);
575 if ( new_enc==NULL )
576 continue;
577 if ( enc==new_enc )
578 return( &encodingtypes[i] );
579 }
580 return( NULL );
581 }
582
583 Encoding *ParseEncodingNameFromList(GGadget *listfield) {
584 const unichar_t *name = _GGadgetGetTitle(listfield);
585 int32_t len;
586 GTextInfo **ti = GGadgetGetList(listfield,&len);
587 int i;
588 Encoding *enc = NULL;
589
590 for ( i=0; i<len; ++i ) if ( ti[i]->text!=NULL ) {
591 if ( u_strcmp(name,ti[i]->text)==0 ) {
592 enc = FindOrMakeEncoding(ti[i]->userdata);
593 break;
594 }
595 }
596
597 if ( enc == NULL ) {
598 char *temp = u2utf8_copy(name);
599 enc = FindOrMakeEncoding(temp);
600 free(temp);
601 }
602 if ( enc==NULL )
603 ff_post_error(_("Bad Encoding"),_("Bad Encoding"));
604 return( enc );
605 }