/* -*- coding: utf-8 -*- */ /* Copyright (C) 2000-2012 by George Williams */ /* * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include "bitmapchar.h" #include "encoding.h" #include "fontforgeui.h" #include "gfile.h" #include "ustring.h" #include "utype.h" #include #include #include static GTextInfo *EncodingList(void) { GTextInfo *ti; int i; Encoding *item; i = 0; for ( item=enclist; item!=NULL ; item=item->next ) if ( !item->builtin ) ++i; ti = calloc(i+1,sizeof(GTextInfo)); i = 0; for ( item=enclist; item!=NULL ; item=item->next ) if ( !item->builtin ) ti[i++].text = uc_copy(item->enc_name); if ( i!=0 ) ti[0].selected = true; return( ti ); } #define CID_Encodings 1001 static int DE_Delete(GGadget *g, GEvent *e) { GWindow gw; int *done; GGadget *list; int sel,i; Encoding *item; if ( e->type==et_controlevent && (e->u.control.subtype == et_buttonactivate || e->u.control.subtype == et_listdoubleclick )) { gw = GGadgetGetWindow(g); done = GDrawGetUserData(gw); list = GWidgetGetControl(gw,CID_Encodings); sel = GGadgetGetFirstListSelectedItem(list); i=0; for ( item=enclist; item!=NULL; item=item->next ) { if ( item->builtin ) /* Do Nothing */; else if ( i==sel ) break; else ++i; } if ( item!=NULL ) DeleteEncoding(item); *done = true; } return( true ); } static int DE_Cancel(GGadget *g, GEvent *e) { GWindow gw; int *done; if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) { gw = GGadgetGetWindow(g); done = GDrawGetUserData(gw); *done = true; } return( true ); } static int de_e_h(GWindow gw, GEvent *event) { if ( event->type==et_close ) { int *done = GDrawGetUserData(gw); *done = true; } else if ( event->type == et_char ) { return( false ); } return( true ); } void RemoveEncoding(void) { GRect pos; GWindow gw; GWindowAttrs wattrs; GGadgetCreateData gcd[5]; GTextInfo label[5]; Encoding *item; int done = 0; for ( item=enclist; item!=NULL && item->builtin; item=item->next ); if ( item==NULL ) return; memset(&gcd,0,sizeof(gcd)); memset(&label,0,sizeof(label)); memset(&wattrs,0,sizeof(wattrs)); wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_restrict|wam_isdlg; wattrs.event_masks = ~(1<enc!=&custom ) return(NULL); name = gwwv_ask_string(_("Please name this encoding"),NULL,_("Please name this encoding")); if ( name==NULL ) return(NULL); item = calloc(1,sizeof(Encoding)); item->enc_name = name; item->only_1byte = item->has_1byte = true; item->char_cnt = map->enccount; item->unicode = calloc(map->enccount,sizeof(int32_t)); for ( i=0; ienccount; ++i ) if ( (gid = map->map[i])!=-1 && (sc=sf->glyphs[gid])!=NULL ) { if ( sc->unicodeenc!=-1 ) item->unicode[i] = sc->unicodeenc; else if ( strcmp(sc->name,".notdef")!=0 ) { if ( item->psnames==NULL ) item->psnames = calloc(map->enccount,sizeof(unichar_t *)); item->psnames[i] = copy(sc->name); } } RemoveMultiples(item); if ( enclist == NULL ) enclist = item; else { for ( temp=enclist; temp->next!=NULL; temp=temp->next ); temp->next = item; } DumpPfaEditEncodings(); return( item ); } void LoadEncodingFile(void) { static char filter[] = "*{.ps,.PS,.txt,.TXT,.enc,.ENC,GlyphOrderAndAliasDB}"; char *fn; char *filename; fn = gwwv_open_filename(_("Load Encoding"), NULL, filter, NULL); if ( fn==NULL ) return; filename = utf82def_copy(fn); ParseEncodingFile(filename, NULL); free(fn); free(filename); DumpPfaEditEncodings(); } void SFFindNearTop(SplineFont *sf) { FontView *fv; EncMap *map; int i,k,gid; if ( sf->cidmaster!=NULL ) sf = sf->cidmaster; if ( sf->subfontcnt==0 ) { for ( fv=(FontView *) sf->fv; fv!=NULL; fv=(FontView *) (fv->b.nextsame) ) { map = fv->b.map; fv->sc_near_top = NULL; for ( i=fv->rowoff*fv->colcnt; ienccount && i<(fv->rowoff+fv->rowcnt)*fv->colcnt; ++i ) if ( (gid=map->map[i])!=-1 && sf->glyphs[gid]!=NULL ) { fv->sc_near_top = sf->glyphs[gid]; break; } } } else { for ( fv=(FontView *) sf->fv; fv!=NULL; fv=(FontView *) (fv->b.nextsame) ) { map = fv->b.map; fv->sc_near_top = NULL; for ( i=fv->rowoff*fv->colcnt; ienccount && i<(fv->rowoff+fv->rowcnt)*fv->colcnt; ++i ) { for ( k=0; ksubfontcnt; ++k ) if ( (gid=map->map[i])!=-1 && gidsubfonts[k]->glyphcnt && sf->subfonts[k]->glyphs[gid]!=NULL ) fv->sc_near_top = sf->subfonts[k]->glyphs[gid]; } } } } void SFRestoreNearTop(SplineFont *sf) { FontView *fv; for ( fv=(FontView *) sf->fv; fv!=NULL; fv=(FontView *) (fv->b.nextsame) ) if ( fv->sc_near_top!=NULL ) { /* Note: For CID keyed fonts, we don't care if sc is in the currently */ /* displayed font, all we care about is the CID */ int enc = fv->b.map->backmap[fv->sc_near_top->orig_pos]; if ( enc!=-1 ) { fv->rowoff = enc/fv->colcnt; GScrollBarSetPos(fv->vsb,fv->rowoff); /* Don't ask for an expose event yet. We'll get one soon enough */ } } } struct block { int cur, tot; char **maps; char **dirs; }; static void AddToBlock(struct block *block,char *mapname, char *dir) { int i, val, j; int len = strlen(mapname); if ( mapname[len-7]=='.' ) len -= 7; for ( i=0; icur; ++i ) { if ( (val=strncmp(block->maps[i],mapname,len))==0 ) return; /* Duplicate */ else if ( val>0 ) break; } if ( block->tot==0 ) { block->tot = 10; block->maps = malloc(10*sizeof(char *)); block->dirs = malloc(10*sizeof(char *)); } else if ( block->cur>=block->tot ) { block->tot += 10; block->maps = realloc(block->maps,block->tot*sizeof(char *)); block->dirs = realloc(block->dirs,block->tot*sizeof(char *)); } for ( j=block->cur; j>=i; --j ) { block->maps[j+1] = block->maps[j]; block->dirs[j+1] = block->dirs[j]; } block->maps[i] = copyn(mapname,len); block->dirs[i] = dir; ++block->cur; } static void FindMapsInDir(struct block *block,char *dir) { struct dirent *ent; DIR *d; int len; char *pt, *pt2; if ( dir==NULL ) return; /* format of cidmap filename "?*-?*-[0-9]*.cidmap" */ d = opendir(dir); if ( d==NULL ) return; while ( (ent = readdir(d))!=NULL ) { if ( (len = strlen(ent->d_name))<8 ) continue; if ( strcmp(ent->d_name+len-7,".cidmap")!=0 ) continue; pt = strchr(ent->d_name, '-'); if ( pt==NULL || pt==ent->d_name ) continue; pt2 = strchr(pt+1, '-' ); if ( pt2==NULL || pt2==pt+1 || !isdigit(pt2[1])) continue; AddToBlock(block,ent->d_name,dir); } closedir(d); } struct cidmap *AskUserForCIDMap(void) { struct block block; struct cidmap *map = NULL; char buffer[200]; char **choices; int i,ret; char *filename=NULL; char *reg, *ord, *pt; int supplement; memset(&block,'\0',sizeof(block)); for ( map = cidmaps; map!=NULL; map = map->next ) { sprintf(buffer,"%s-%s-%d", map->registry, map->ordering, map->supplement); AddToBlock(&block,buffer,NULL); } FindMapsInDir(&block,"."); char* sharedir = smprintf("%s/cidmap", getShareDir()); FindMapsInDir(&block,sharedir); free(sharedir); choices = calloc(block.cur+2,sizeof(unichar_t *)); choices[0] = copy(_("Browse...")); for ( i=0; inext ) if ( !item->hidden ) ++cnt; i = cnt+1; i += sizeof(encodingtypes)/sizeof(encodingtypes[0]); mi = calloc(i+1,sizeof(GMenuItem)); for ( i=0; ienc_name)==0 || (current->iconv_name!=NULL && strmatch(mi[i].ti.userdata,current->iconv_name)==0)) mi[i].ti.checked = true; } mi[i].ti.text_is_1byte = false; mi[i].ti.fg = mi[i].ti.bg = COLOR_DEFAULT; mi[i].invoke = func; } if ( cnt!=0 ) { mi[i].ti.fg = mi[i].ti.bg = COLOR_DEFAULT; mi[i++].ti.line = true; for ( item=enclist; item!=NULL ; item=item->next ) if ( !item->hidden ) { mi[i].ti.text = utf82u_copy(item->enc_name); mi[i].ti.userdata = (void *) item->enc_name; mi[i].ti.fg = mi[i].ti.bg = COLOR_DEFAULT; mi[i].ti.checkable = true; if ( item==current ) mi[i].ti.checked = true; mi[i++].invoke = func; } } return( mi ); } GTextInfo *GetEncodingTypes(void) { GTextInfo *ti; int i, cnt; Encoding *item; EncodingInit(); cnt = 0; for ( item=enclist; item!=NULL ; item=item->next ) if ( !item->hidden ) ++cnt; i = cnt + sizeof(encodingtypes)/sizeof(encodingtypes[0]); ti = calloc(i+1,sizeof(GTextInfo)); memcpy(ti,encodingtypes,sizeof(encodingtypes)-sizeof(encodingtypes[0])); for ( i=0; inext ) if ( !item->hidden ) { ti[i].text = uc_copy(item->enc_name); ti[i++].userdata = (void *) item->enc_name; } } return( ti ); } GTextInfo *EncodingTypesFindEnc(GTextInfo *encodingtypes, Encoding *enc) { int i; char *name; Encoding *new_enc; for ( i=0; encodingtypes[i].text!=NULL || encodingtypes[i].line; ++i ) { if ( encodingtypes[i].text==NULL ) continue; name = encodingtypes[i].userdata; new_enc = FindOrMakeEncoding(name); if ( new_enc==NULL ) continue; if ( enc==new_enc ) return( &encodingtypes[i] ); } return( NULL ); } Encoding *ParseEncodingNameFromList(GGadget *listfield) { const unichar_t *name = _GGadgetGetTitle(listfield); int32_t len; GTextInfo **ti = GGadgetGetList(listfield,&len); int i; Encoding *enc = NULL; for ( i=0; itext!=NULL ) { if ( u_strcmp(name,ti[i]->text)==0 ) { enc = FindOrMakeEncoding(ti[i]->userdata); break; } } if ( enc == NULL ) { char *temp = u2utf8_copy(name); enc = FindOrMakeEncoding(temp); free(temp); } if ( enc==NULL ) ff_post_error(_("Bad Encoding"),_("Bad Encoding")); return( enc ); }