Debian Codegraph @

fontforge/splinesaveafm.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 "splinesaveafm.h"
31
32 #include "autohint.h"
33 #include "featurefile.h"
34 #include "fontforgevw.h" /* For Error */
35 #include "gfile.h"
36 #include "fvcomposite.h"
37 #include "fvfonts.h"
38 #include "gutils.h"
39 #include "lookups.h"
40 #include "macbinary.h"
41 #include "mem.h"
42 #include "mm.h"
43 #include "namelist.h"
44 #include "splinefont.h"
45 #include "splinesave.h"
46 #include "splineutil.h"
47 #include "tottf.h"
48 #include "tottfgpos.h"
49 #include "ttf.h" /* For AnchorClassDecompose */
50 #include "ustring.h"
51 #include "utype.h"
52 #include "zapfnomen.h"
53
54 #include <math.h>
55 #include <stdio.h>
56 #include <time.h>
57 #include "ffunistd.h"
58
59 static void *mygets(FILE *file,char *buffer,int size) {
60 char *end = buffer+size-1;
61 char *pt = buffer;
62 int ch;
63
64 while ( (ch=getc(file))!=EOF && ch!='\r' && ch!='\n' && pt<end )
65 *pt++ = ch;
66 *pt = '\0';
67 if ( ch==EOF && pt==buffer )
68 return( NULL );
69 if ( ch=='\r' ) {
70 ch = getc(file);
71 if ( ch!='\n' )
72 ungetc(ch,file);
73 }
74 return( buffer );
75 }
76
77 /* ************************************************************************** */
78 /* **************************** Reading AFM files *************************** */
79 /* ************************************************************************** */
80 static void KPInsert( SplineChar *sc1, SplineChar *sc2, int off, int isv ) {
81 KernPair *kp;
82 int32_t script;
83
84 if ( sc1!=NULL && sc2!=NULL ) {
85 for ( kp=sc1->kerns; kp!=NULL && kp->sc!=sc2; kp = kp->next );
86 if ( kp!=NULL )
87 kp->off = off;
88 else if ( off!=0 ) {
89 kp = chunkalloc(sizeof(KernPair));
90 kp->sc = sc2;
91 kp->off = off;
92 script = SCScriptFromUnicode(sc1);
93 if ( script==DEFAULT_SCRIPT )
94 script = SCScriptFromUnicode(sc2);
95 kp->subtable = SFSubTableFindOrMake(sc1->parent,
96 isv?CHR('v','k','r','n'):CHR('k','e','r','n'),
97 script, gpos_pair);
98 if ( isv ) {
99 kp->next = sc1->vkerns;
100 sc1->vkerns = kp;
101 } else {
102 kp->next = sc1->kerns;
103 sc1->kerns = kp;
104 }
105 }
106 }
107 }
108
109 int LoadKerningDataFromAfm(SplineFont *sf, char *filename) {
110 FILE *file = fopen(filename,"r");
111 char buffer[200], *pt, *ept, ch;
112 SplineChar *sc1, *sc2;
113 int off;
114 char name[44], second[44], lig[44], buf2[100];
115 PST *liga;
116 bigreal scale = (sf->ascent+sf->descent)/1000.0;
117
118 if ( file==NULL )
119 return( 0 );
120 ff_progress_change_line2(_("Reading AFM file"));
121 while ( mygets(file,buffer,sizeof(buffer))!=NULL ) {
122 if ( strncmp(buffer,"KPX",3)==0 || strncmp(buffer,"KPY",3)==0 ) {
123 int isv = strncmp(buffer,"KPY",3)==0;
124 for ( pt=buffer+3; isspace(*pt); ++pt);
125 for ( ept = pt; *ept!='\0' && !isspace(*ept); ++ept );
126 ch = *ept; *ept = '\0';
127 sc1 = SFGetChar(sf,-1,pt);
128 *ept = ch;
129 for ( pt=ept; isspace(*pt); ++pt);
130 for ( ept = pt; *ept!='\0' && !isspace(*ept); ++ept );
131 ch = *ept; *ept = '\0';
132 sc2 = SFGetChar(sf,-1,pt);
133 *ept = ch;
134 off = strtol(ept,NULL,10);
135 KPInsert(sc1,sc2,rint(off*scale),isv);
136 } else if ( buffer[0]=='C' && isspace(buffer[1])) {
137 char *pt;
138 sc2 = NULL;
139 for ( pt= strchr(buffer,';'); pt!=NULL; pt=strchr(pt+1,';') ) {
140 if ( sscanf( pt, "; N %40s", name )==1 )
141 sc2 = SFGetChar(sf,-1,name);
142 else if ( sc2!=NULL &&
143 sscanf( pt, "; L %40s %40s", second, lig)==2 ) {
144 sc1 = SFGetChar(sf,-1,lig);
145 if ( sc1!=NULL ) {
146 sprintf( buf2, "%s %s", name, second);
147 for ( liga=sc1->possub; liga!=NULL; liga=liga->next ) {
148 if ( liga->type == pst_ligature && strcmp(liga->u.lig.components,buf2)==0 )
149 break;
150 }
151 if ( liga==NULL ) {
152 liga = chunkalloc(sizeof(PST));
153 liga->subtable = SFSubTableFindOrMake(sf,
154 CHR('l','i','g','a'),SCScriptFromUnicode(sc2),
155 gsub_ligature);
156 liga->subtable->lookup->store_in_afm = true;
157 liga->type = pst_ligature;
158 liga->next = sc1->possub;
159 sc1->possub = liga;
160 liga->u.lig.lig = sc1;
161 liga->u.lig.components = copy( buf2 );
162 }
163 }
164 }
165 }
166 }
167 }
168 fclose(file);
169 return( 1 );
170 }
171
172 static void CheckMMAfmFile(SplineFont *sf,char *amfm_filename,char *fontname) {
173 /* the afm file should be in the same directory as the amfm file */
174 /* with the fontname as the filename */
175 char *temp, *pt;
176
177 free(sf->fontname);
178 sf->fontname = copy(fontname);
179
180 temp = malloc(strlen(amfm_filename)+strlen(fontname)+strlen(".afm")+1);
181 strcpy(temp, amfm_filename);
182 pt = strrchr(temp,'/');
183 if ( pt==NULL ) pt = temp;
184 else ++pt;
185 strcpy(pt,fontname);
186 pt += strlen(pt);
187 strcpy(pt,".afm");
188 if ( !LoadKerningDataFromAfm(sf,temp) ) {
189 strcpy(pt,".AFM");
190 LoadKerningDataFromAfm(sf,temp);
191 }
192 free(temp);
193 }
194
195 int LoadKerningDataFromAmfm(SplineFont *sf, char *filename) {
196 FILE *file=NULL;
197 char buffer[280], *pt, lastname[257];
198 int index, i;
199 MMSet *mm = sf->mm;
200
201 if ( mm!=NULL )
202 file = fopen(filename,"r");
203 pt = strstrmatch(filename,".amfm");
204 if ( pt!=NULL ) {
205 char *afmname = copy(filename);
206 strcpy(afmname+(pt-filename),isupper(pt[1])?".AFM":".afm");
207 LoadKerningDataFromAfm(mm->normal,afmname);
208 free(afmname);
209 }
210 if ( file==NULL )
211 return( 0 );
212
213 ff_progress_change_line2(_("Reading AFM file"));
214 while ( fgets(buffer,sizeof(buffer),file)!=NULL ) {
215 if ( strstrmatch(buffer,"StartMaster")!=NULL )
216 break;
217 }
218 index = -1; lastname[0] = '\0';
219 while ( fgets(buffer,sizeof(buffer),file)!=NULL ) {
220 if ( strstrmatch(buffer,"EndMaster")!=NULL ) {
221 if ( lastname[0]!='\0' && index!=-1 && index<mm->instance_count )
222 CheckMMAfmFile(mm->instances[index],filename,lastname);
223 index = -1; lastname[0] = '\0';
224 } else if ( sscanf(buffer,"FontName %256s", lastname )== 1 ) {
225 /* Do Nothing, all done */
226 } else if ( (pt = strstr(buffer,"WeightVector"))!=NULL ) {
227 pt += strlen("WeightVector");
228 while ( *pt==' ' || *pt=='[' ) ++pt;
229 i = 0;
230 while ( *pt!=']' && *pt!='\0' ) {
231 if ( *pt=='0' )
232 ++i;
233 else if ( *pt=='1' ) {
234 index = i;
235 break;
236 }
237 ++pt;
238 }
239 }
240 }
241 fclose(file);
242 return( true );
243 }
244
245 int CheckAfmOfPostScript(SplineFont *sf,char *psname) {
246 char *new, *pt;
247 int ret;
248 int wasuc=false;
249
250 new = malloc(strlen(psname)+6);
251 strcpy(new,psname);
252 pt = strrchr(new,'.');
253 if ( pt==NULL ) pt = new+strlen(new);
254 else wasuc = isupper(pt[1]);
255
256 if ( sf->mm!=NULL ) {
257 strcpy(pt,wasuc?".AMFM":".amfm");
258 if ( !LoadKerningDataFromAmfm(sf,new)) {
259 strcpy(pt,wasuc?".amfm":".AMFM");
260 ret = LoadKerningDataFromAmfm(sf,new);
261 } else
262 ret = true;
263 /* The above routine reads from the afm file if one exist */
264 } else {
265 strcpy(pt,wasuc?".AFM":".afm");
266 if ( !LoadKerningDataFromAfm(sf,new)) {
267 strcpy(pt,wasuc?".afm":".AFM");
268 ret = LoadKerningDataFromAfm(sf,new);
269 } else
270 ret = true;
271 }
272 free(new);
273 return( ret );
274 }
275
276 void SubsNew(SplineChar *to,enum possub_type type,int tag,char *components,
277 SplineChar *default_script) {
278 PST *pst;
279
280 pst = chunkalloc(sizeof(PST));
281 pst->type = type;
282 pst->subtable = SFSubTableFindOrMake(to->parent,tag,SCScriptFromUnicode(default_script),
283 type==pst_substitution ? gsub_single :
284 type==pst_alternate ? gsub_alternate :
285 type==pst_multiple ? gsub_multiple : gsub_ligature );
286 pst->u.alt.components = components;
287 if ( type == pst_ligature ) {
288 pst->u.lig.lig = to;
289 pst->subtable->lookup->store_in_afm = true;
290 }
291 pst->next = to->possub;
292 to->possub = pst;
293 }
294
295 void PosNew(SplineChar *to,int tag,int dx, int dy, int dh, int dv) {
296 PST *pst;
297
298 pst = chunkalloc(sizeof(PST));
299 pst->type = pst_position;
300 pst->subtable = SFSubTableFindOrMake(to->parent,tag,SCScriptFromUnicode(to),
301 gpos_single );
302 pst->u.pos.xoff = dx;
303 pst->u.pos.yoff = dy;
304 pst->u.pos.h_adv_off = dh;
305 pst->u.pos.v_adv_off = dv;
306 pst->next = to->possub;
307 to->possub = pst;
308 }
309
310 static void LigatureNew(SplineChar *sc3,SplineChar *sc1,SplineChar *sc2) {
311 char *components = malloc(strlen(sc1->name)+strlen(sc2->name)+2);
312 strcpy(components,sc1->name);
313 strcat(components," ");
314 strcat(components,sc2->name);
315 SubsNew(sc3,pst_ligature,CHR('l','i','g','a'),components,sc1);
316 }
317
318 /* ************************************************************************** */
319 /* **************************** Reading TFM files *************************** */
320 /* ************************************************************************** */
321 struct tfmdata {
322 int file_len;
323 int head_len;
324 int first, last;
325 int width_size;
326 int height_size;
327 int depth_size;
328 int italic_size;
329 int ligkern_size;
330 int kern_size;
331 int esize;
332 int param_size;
333
334 uint8_t *kerntab;
335 uint8_t *ligkerntab;
336 uint8_t *ext;
337 uint8_t *ictab;
338 uint8_t *dptab;
339 uint8_t *httab;
340 uint8_t *widtab;
341
342 int *charlist;
343 };
344
345 static void tfmDoLigKern(SplineFont *sf, int enc, int lk_index,
346 struct tfmdata *tfmd,EncMap *map) {
347 int enc2, k_index;
348 SplineChar *sc1, *sc2, *sc3;
349 real off;
350
351 if ( enc>=map->enccount || map->map[enc]==-1 || (sc1=sf->glyphs[map->map[enc]])==NULL )
352 return;
353 if ( enc<tfmd->first || enc>tfmd->last )
354 return;
355 if ( lk_index>=tfmd->ligkern_size )
356 return;
357
358 if ( tfmd->ligkerntab[lk_index*4+0]>128 ) /* Special case to get big indeces */
359 lk_index = 256*tfmd->ligkerntab[lk_index*4+2] + tfmd->ligkerntab[lk_index*4+3];
360
361 while ( 1 ) {
362 if ( lk_index>=tfmd->ligkern_size )
363 return;
364 enc2 = tfmd->ligkerntab[lk_index*4+1];
365 if ( enc2>=map->enccount || map->map[enc2]==-1 || (sc2=sf->glyphs[map->map[enc2]])==NULL ||
366 enc2<tfmd->first || enc2>tfmd->last )
367 /* Not much we can do. can't kern to a non-existant char */;
368 else if ( tfmd->ligkerntab[lk_index*4+2]>=128 ) {
369 k_index = ((tfmd->ligkerntab[lk_index*4+2]-128)*256+
370 tfmd->ligkerntab[lk_index*4+3])*4;
371 if ( k_index>4*tfmd->kern_size )
372 return;
373 off = (sf->ascent+sf->descent) *
374 ((tfmd->kerntab[k_index]<<24) + (tfmd->kerntab[k_index+1]<<16) +
375 (tfmd->kerntab[k_index+2]<<8) + tfmd->kerntab[k_index+3])/
376 (bigreal) 0x100000;
377 /* printf( "%s(%d) %s(%d) -> %g\n", sc1->name, sc1->enc, sc2->name, sc2->enc, off); */
378 KPInsert(sc1,sc2,rint(off),false);
379 } else if ( tfmd->ligkerntab[lk_index*4+2]==0 &&
380 tfmd->ligkerntab[lk_index*4+3]<map->enccount &&
381 tfmd->ligkerntab[lk_index*4+3]>=tfmd->first &&
382 tfmd->ligkerntab[lk_index*4+3]<=tfmd->last &&
383 map->map[tfmd->ligkerntab[lk_index*4+3]]!=-1 &&
384 (sc3=sf->glyphs[map->map[tfmd->ligkerntab[lk_index*4+3]]])!=NULL ) {
385 LigatureNew(sc3,sc1,sc2);
386 }
387 if ( tfmd->ligkerntab[lk_index*4]>=128 )
388 break;
389 lk_index += tfmd->ligkerntab[lk_index*4] + 1;
390 }
391 }
392
393 int doesGlyphExpandHorizontally(SplineChar *UNUSED(sc)) {
394 return( false );
395 }
396
397 static void tfmDoCharList(SplineFont *sf,int i,struct tfmdata *tfmd,EncMap *map) {
398 int used[256], ucnt, len, was;
399 char *components;
400 SplineChar *sc;
401 struct glyphvariants **gvbase;
402
403 if ( i>=map->enccount || map->map[i]==-1 || sf->glyphs[map->map[i]]==NULL ||
404 i<tfmd->first || i>tfmd->last )
405 return;
406
407 ucnt = 0, len=0;
408 while ( i!=-1 ) {
409 if ( i<map->enccount && map->map[i]!=-1 && sf->glyphs[map->map[i]]!=NULL &&
410 i>=tfmd->first && i<=tfmd->last ) {
411 used[ucnt++] = map->map[i];
412 len += strlen(sf->glyphs[map->map[i]]->name)+1;
413 /*sf->glyphs[map->map[i]]->is_extended_shape = true;*/ /* MS does not do this in their fonts */
414 }
415 was = i;
416 i = tfmd->charlist[i];
417 tfmd->charlist[was] = -1;
418 }
419 if ( ucnt<=1 )
420 return;
421
422 sc = sf->glyphs[used[0]];
423 if ( sc==NULL )
424 return;
425 components = malloc(len+1); components[0] = '\0';
426 for ( i=1; i<ucnt; ++i ) {
427 strcat(components,sf->glyphs[used[i]]->name);
428 if ( i!=ucnt-1 )
429 strcat(components," ");
430 }
431 gvbase = doesGlyphExpandHorizontally(sc)?&sc->horiz_variants: &sc->vert_variants;
432 if ( *gvbase==NULL )
433 *gvbase = chunkalloc(sizeof(struct glyphvariants));
434 (*gvbase)->variants = components;
435 }
436
437 static void tfmDoExten(SplineFont *sf,int i,struct tfmdata *tfmd,int left,EncMap *map) {
438 int j, k, gid, gid2, cnt;
439 SplineChar *bits[4], *bats[5];
440 uint8_t *ext;
441 SplineChar *sc;
442 struct glyphvariants **gvbase;
443 int is_horiz;
444
445 if ( i>=map->enccount || (gid=map->map[i])==-1 || sf->glyphs[gid]==NULL )
446 return;
447 if ( left>=tfmd->esize )
448 return;
449
450 ext = tfmd->ext + 4*left;
451
452 sc = sf->glyphs[gid];
453 if ( sc==NULL )
454 return;
455 is_horiz = doesGlyphExpandHorizontally(sc);
456 gvbase = is_horiz?&sc->horiz_variants: &sc->vert_variants;
457 if ( *gvbase==NULL )
458 *gvbase = chunkalloc(sizeof(struct glyphvariants));
459
460 memset(bits,0,sizeof(bits));
461 for ( j=0; j<4; ++j ) {
462 k = ext[j];
463 if ( k!=0 && k<map->enccount && (gid2 = map->map[k])!=-1 && sf->glyphs[gid2]!=NULL ) {
464 bits[j] = sf->glyphs[gid2];
465 /* bits[j]->is_extended_shape = true;*/ /* MS does not do this in their fonts */
466 }
467 }
468 /* 0=>bottom, 1=>middle, 2=>top, 3=>extender */
469 cnt = 0;
470 if ( bits[0]!=NULL )
471 bats[cnt++] = bits[0];
472 if ( bits[3]!=NULL )
473 bats[cnt++] = bits[3];
474 if ( bits[1]!=NULL ) {
475 bats[cnt++] = bits[1];
476 if ( bits[3]!=NULL )
477 bats[cnt++] = bits[3];
478 }
479 if ( bits[2]!=NULL )
480 bats[cnt++] = bits[2];
481
482 (*gvbase)->part_cnt = cnt;
483 (*gvbase)->parts = calloc(cnt,sizeof(struct gv_part));
484 for ( j=0; j<cnt; ++j ) {
485 DBounds b;
486 bigreal len;
487 SplineCharFindBounds(bats[j],&b);
488 if ( is_horiz )
489 len = b.maxx;
490 else
491 len = b.maxy;
492 (*gvbase)->parts[j].component = copy(bats[j]->name);
493 (*gvbase)->parts[j].is_extender = bats[j]==bits[3];
494 (*gvbase)->parts[j].startConnectorLength = len/4;
495 (*gvbase)->parts[j].endConnectorLength = len/4;
496 (*gvbase)->parts[j].fullAdvance = len;
497 }
498 }
499
500 #define BigEndianWord(pt) ((((uint8_t *) pt)[0]<<24) | (((uint8_t *) pt)[1]<<16) | (((uint8_t *) pt)[2]<<8) | (((uint8_t *) pt)[3]))
501
502 int LoadKerningDataFromTfm(SplineFont *sf, char *filename,EncMap *map) {
503 FILE *file = fopen(filename,"rb");
504 int i, tag, left, ictag;
505 struct tfmdata tfmd;
506 int charlist[256];
507 int height, depth;
508 real scale = (sf->ascent+sf->descent)/(bigreal) (1<<20);
509
510 if ( file==NULL )
511 return( 0 );
512 tfmd.file_len = getushort(file);
513 tfmd.head_len = getushort(file);
514 tfmd.first = getushort(file);
515 tfmd.last = getushort(file);
516 tfmd.width_size = getushort(file);
517 tfmd.height_size = getushort(file);
518 tfmd.depth_size = getushort(file);
519 tfmd.italic_size = getushort(file);
520 tfmd.ligkern_size = getushort(file);
521 tfmd.kern_size = getushort(file);
522 tfmd.esize = getushort(file);
523 tfmd.param_size = getushort(file);
524 if ( tfmd.first-1>tfmd.last || tfmd.last>=256 ) {
525 fclose(file);
526 return( 0 );
527 }
528 tfmd.kerntab = calloc(tfmd.kern_size,4*sizeof(uint8_t));
529 tfmd.ligkerntab = calloc(tfmd.ligkern_size,4*sizeof(uint8_t));
530 tfmd.ext = calloc(tfmd.esize,4*sizeof(uint8_t));
531 tfmd.ictab = calloc(tfmd.italic_size,4*sizeof(uint8_t));
532 tfmd.dptab = calloc(tfmd.depth_size,4*sizeof(uint8_t));
533 tfmd.httab = calloc(tfmd.height_size,4*sizeof(uint8_t));
534 tfmd.widtab = calloc(tfmd.width_size,4*sizeof(uint8_t));
535 tfmd.charlist = charlist;
536
537 fseek( file,(6+1)*sizeof(int32_t),SEEK_SET);
538 sf->design_size = (5*getlong(file)+(1<<18))>>19; /* TeX stores as <<20, adobe in decipoints */
539 fseek( file,
540 (6+tfmd.head_len+(tfmd.last-tfmd.first+1))*sizeof(int32_t),
541 SEEK_SET);
542 fread( tfmd.widtab,1,tfmd.width_size*sizeof(int32_t),file);
543 fread( tfmd.httab,1,tfmd.height_size*sizeof(int32_t),file);
544 fread( tfmd.dptab,1,tfmd.depth_size*sizeof(int32_t),file);
545 fread( tfmd.ictab,1,tfmd.italic_size*sizeof(int32_t),file);
546 fread( tfmd.ligkerntab,1,tfmd.ligkern_size*sizeof(int32_t),file);
547 fread( tfmd.kerntab,1,tfmd.kern_size*sizeof(int32_t),file);
548 fread( tfmd.ext,1,tfmd.esize*sizeof(int32_t),file);
549 for ( i=0; i<22 && i<tfmd.param_size; ++i )
550 sf->texdata.params[i] = getlong(file);
551 if ( tfmd.param_size==22 ) sf->texdata.type = tex_math;
552 else if ( tfmd.param_size==13 ) sf->texdata.type = tex_mathext;
553 else if ( tfmd.param_size>=7 ) sf->texdata.type = tex_text;
554
555 memset(charlist,-1,sizeof(charlist));
556
557 fseek( file, (6+tfmd.head_len)*sizeof(int32_t), SEEK_SET);
558 for ( i=tfmd.first; i<=tfmd.last; ++i ) {
559 /* width = */ getc(file);
560 height = getc(file);
561 depth = height&0xf; height >>= 4;
562 ictag = getc(file);
563 tag = (ictag&3);
564 ictag>>=2;
565 left = getc(file);
566 if ( tag==1 )
567 tfmDoLigKern(sf,i,left,&tfmd,map);
568 else if ( tag==2 )
569 charlist[i] = left;
570 else if ( tag==3 )
571 tfmDoExten(sf,i,&tfmd,left,map);
572 if ( map->map[i]!=-1 && sf->glyphs[map->map[i]]!=NULL ) {
573 SplineChar *sc = sf->glyphs[map->map[i]];
574 /* TFtoPL says very clearly: The actual width of a character is */
575 /* width[width_index], in design-size units. It is not in design units */
576 /* it is stored as a fraction of the design size in a fixed word */
577 if ( height<tfmd.height_size )
578 sc->tex_height = BigEndianWord(tfmd.httab+4*height)*scale;
579 if ( depth<tfmd.depth_size )
580 sc->tex_depth = BigEndianWord(tfmd.dptab+4*depth)*scale;
581 if ( ictag!=0 && ictag<tfmd.italic_size )
582 sc->italic_correction = BigEndianWord(tfmd.ictab+4*ictag)*scale;
583 }
584 }
585
586 for ( i=tfmd.first; i<=tfmd.last; ++i ) {
587 if ( charlist[i]!=-1 )
588 tfmDoCharList(sf,i,&tfmd,map);
589 }
590
591 free( tfmd.ligkerntab); free(tfmd.kerntab); free(tfmd.ext); free(tfmd.ictab);
592 free( tfmd.dptab ); free( tfmd.httab ); free( tfmd.widtab );
593 fclose(file);
594 return( 1 );
595 }
596
597 /* ************************************************************************** */
598 /* **************************** Reading OFM files *************************** */
599 /* ************************************************************************** */
600 #define LKShort(index,off) (((tfmd->ligkerntab+8*index+2*off)[0]<<8)|(tfmd->ligkerntab+8*index+2*off)[1])
601
602 /* almost the same as the tfm version except that we deal with shorts rather */
603 /* than bytes */
604 static void ofmDoLigKern(SplineFont *sf, int enc, int lk_index,
605 struct tfmdata *tfmd,EncMap *map) {
606 int enc2, k_index;
607 SplineChar *sc1, *sc2, *sc3;
608 real off;
609
610 if ( enc>=map->enccount || map->map[enc]==-1 || (sc1=sf->glyphs[map->map[enc]])==NULL )
611 return;
612 if ( enc<tfmd->first || enc>tfmd->last )
613 return;
614 if ( lk_index>=2*tfmd->ligkern_size )
615 return;
616
617 if ( LKShort(lk_index,0)>128 ) /* Special case to get big indeces */
618 lk_index = 65536*LKShort(lk_index,2) + LKShort(lk_index,3);
619
620 while ( 1 ) {
621 if ( lk_index>=2*tfmd->ligkern_size )
622 return;
623 enc2 = LKShort(lk_index,1);
624 if ( enc2>=map->enccount || map->map[enc2]==-1 || (sc2=sf->glyphs[map->map[enc2]])==NULL ||
625 enc2<tfmd->first || enc2>tfmd->last )
626 /* Not much we can do. can't kern to a non-existant char */;
627 else if ( LKShort(lk_index,2)>=128 ) {
628 k_index = ((LKShort(lk_index,2)-128)*65536+LKShort(lk_index,3))*4;
629 if ( k_index>tfmd->kern_size )
630 return;
631 off = (sf->ascent+sf->descent) *
632 ((tfmd->kerntab[k_index]<<24) + (tfmd->kerntab[k_index+1]<<16) +
633 (tfmd->kerntab[k_index+2]<<8) + tfmd->kerntab[k_index+3])/
634 (bigreal) 0x100000;
635 /* printf( "%s(%d) %s(%d) -> %g\n", sc1->name, sc1->enc, sc2->name, sc2->enc, off); */
636 KPInsert(sc1,sc2,rint(off),false);
637 } else if ( LKShort(lk_index,2)==0 &&
638 LKShort(lk_index,3)<map->enccount &&
639 LKShort(lk_index,3)>=tfmd->first &&
640 LKShort(lk_index,3)<=tfmd->last &&
641 map->map[LKShort(lk_index,3)]!=-1 &&
642 (sc3=sf->glyphs[map->map[LKShort(lk_index,3)]])!=NULL ) {
643 LigatureNew(sc3,sc1,sc2);
644 }
645 if ( LKShort(lk_index,0)>=128 )
646 break;
647 lk_index += LKShort(lk_index,0) + 1;
648 }
649 }
650 #undef LKShort
651
652
653 #define ExtShort(off) (((ext+2*off)[0]<<8)|(ext+2*off)[1])
654
655 /* almost the same as the tfm version except that we deal with shorts rather */
656 /* than bytes */
657 static void ofmDoExten(SplineFont *sf,int i,struct tfmdata *tfmd,int left,EncMap *map) {
658 int j, k, gid, gid2, cnt;
659 uint8_t *ext;
660 SplineChar *sc, *bits[4], *bats[4];
661 struct glyphvariants **gvbase;
662 int is_horiz;
663
664 if ( i>=map->enccount || (gid=map->map[i])==-1 || sf->glyphs[gid]==NULL )
665 return;
666 if ( left>=2*tfmd->esize )
667 return;
668
669 ext = tfmd->ext + 4*left;
670
671 sc = sf->glyphs[gid];
672 if ( sc==NULL )
673 return;
674 is_horiz = doesGlyphExpandHorizontally(sc);
675 gvbase = is_horiz?&sc->horiz_variants: &sc->vert_variants;
676 if ( *gvbase==NULL )
677 *gvbase = chunkalloc(sizeof(struct glyphvariants));
678
679 memset(bits,0,sizeof(bits));
680 for ( j=0; j<4; ++j ) {
681 k = ExtShort(j);
682 if ( k!=0 && k<map->enccount && (gid2 = map->map[k])!=-1 && sf->glyphs[gid2]!=NULL ) {
683 bits[j] = sf->glyphs[gid2];
684 /* bits[j]->is_extended_shape = true;*/ /* MS does not do this in their fonts */
685 }
686 }
687 /* 0=>bottom, 1=>middle, 2=>top, 3=>extender */
688 cnt = 0;
689 if ( bits[0]!=NULL )
690 bats[cnt++] = bits[0];
691 if ( bits[3]!=NULL )
692 bats[cnt++] = bits[3];
693 if ( bits[1]!=NULL ) {
694 bats[cnt++] = bits[1];
695 if ( bits[3]!=NULL )
696 bats[cnt++] = bits[3];
697 }
698 if ( bits[2]!=NULL )
699 bats[cnt++] = bits[1];
700
701 (*gvbase)->part_cnt = cnt;
702 (*gvbase)->parts = calloc(cnt,sizeof(struct gv_part));
703 for ( j=0; j<cnt; ++j ) {
704 DBounds b;
705 bigreal len;
706 SplineCharFindBounds(bats[j],&b);
707 if ( is_horiz )
708 len = b.maxx;
709 else
710 len = b.maxy;
711 (*gvbase)->parts[j].component = copy(bats[j]->name);
712 (*gvbase)->parts[j].is_extender = bats[j]==bits[3];
713 (*gvbase)->parts[j].startConnectorLength = len/4;
714 (*gvbase)->parts[j].endConnectorLength = len/4;
715 (*gvbase)->parts[j].fullAdvance = len;
716 }
717 }
718
719 int LoadKerningDataFromOfm(SplineFont *sf, char *filename,EncMap *map) {
720 FILE *file = fopen(filename,"rb");
721 int i, tag, left, ictag;
722 int level;
723 int height, depth;
724 real scale = (sf->ascent+sf->descent)/(bigreal) (1<<20);
725 struct tfmdata tfmd;
726
727 if ( file==NULL )
728 return( 0 );
729 /* No one bothered to mention this in the docs, but there appears to be */
730 /* an initial 0 in an ofm file. I wonder if that is the level? */
731 /* according to the ofm2opl source, it is the level */
732 level = getlong(file);
733 tfmd.file_len = getlong(file);
734 tfmd.head_len = getlong(file);
735 tfmd.first = getlong(file);
736 tfmd.last = getlong(file);
737 tfmd.width_size = getlong(file);
738 tfmd.height_size = getlong(file);
739 tfmd.depth_size = getlong(file);
740 tfmd.italic_size = getlong(file);
741 tfmd.ligkern_size = getlong(file);
742 tfmd.kern_size = getlong(file);
743 tfmd.esize = getlong(file);
744 tfmd.param_size = getlong(file);
745 /* font_dir = */ getlong(file);
746 if ( tfmd.first-1>tfmd.last || tfmd.last>=65536 ) {
747 fclose(file);
748 return( 0 );
749 }
750 if ( tfmd.file_len!=14+tfmd.head_len+2*(tfmd.last-tfmd.first+1)+tfmd.width_size+tfmd.height_size+tfmd.depth_size+
751 tfmd.italic_size+2*tfmd.ligkern_size+tfmd.kern_size+2*tfmd.esize+tfmd.param_size || level!=0 ) {
752 int ncw, nki, nwi, nkf, nwf, nkm,nwm, nkr, nwr, nkg, nwg, nkp, nwp;
753 int level1=0;
754 /* nco = */ getlong(file);
755 ncw = getlong(file);
756 /* npc = */ getlong(file);
757 nki = getlong(file);
758 nwi = getlong(file);
759 nkf = getlong(file);
760 nwf = getlong(file);
761 nkm = getlong(file);
762 nwm = getlong(file);
763 nkr = getlong(file);
764 nwr = getlong(file);
765 nkg = getlong(file);
766 nwg = getlong(file);
767 nkp = getlong(file);
768 nwp = getlong(file);
769 level1 = tfmd.file_len==29+tfmd.head_len+ncw+tfmd.width_size+tfmd.height_size+tfmd.depth_size+
770 tfmd.italic_size+2*tfmd.ligkern_size+tfmd.kern_size+2*tfmd.esize+tfmd.param_size +
771 nki + nwi + nkf + nwf + nkm + nwm + nkr + nwr + nkg + nwg + nkp + nwp;
772 /* Level 2 appears to have the same structure as level 1 */
773 if ( level1 || level==1 || level==2 )
774 ff_post_error(_("Unlikely Ofm File"),_("This looks like a level1 (or level2) ofm. FontForge only supports level0 files, and can't read a real level1 file."));
775 else
776 ff_post_error(_("Unlikely Ofm File"),_("This doesn't look like an ofm file, I don't know how to read it."));
777 fclose(file);
778 return( 0 );
779 }
780
781 tfmd.kerntab = calloc(tfmd.kern_size,4*sizeof(uint8_t));
782 tfmd.ligkerntab = calloc(tfmd.ligkern_size,4*2*sizeof(uint8_t));
783 tfmd.ext = calloc(tfmd.esize,4*2*sizeof(uint8_t));
784 tfmd.ictab = calloc(tfmd.italic_size,4*sizeof(uint8_t));
785 tfmd.dptab = calloc(tfmd.depth_size,4*sizeof(uint8_t));
786 tfmd.httab = calloc(tfmd.height_size,4*sizeof(uint8_t));
787 tfmd.widtab = calloc(tfmd.width_size,4*sizeof(uint8_t));
788 fseek( file,(14+1)*sizeof(int32_t),SEEK_SET);
789 sf->design_size = (5*getlong(file)+(1<<18))>>19; /* TeX stores as <<20, adobe in decipoints */
790 fseek( file,
791 (14+tfmd.head_len+2*(tfmd.last-tfmd.first+1))*sizeof(int32_t),
792 SEEK_SET);
793 fread( tfmd.widtab,1,tfmd.width_size*sizeof(int32_t),file);
794 fread( tfmd.httab,1,tfmd.height_size*sizeof(int32_t),file);
795 fread( tfmd.dptab,1,tfmd.depth_size*sizeof(int32_t),file);
796 fread( tfmd.ictab,1,tfmd.italic_size*sizeof(int32_t),file);
797 fread( tfmd.ligkerntab,1,tfmd.ligkern_size*2*sizeof(int32_t),file);
798 fread( tfmd.kerntab,1,tfmd.kern_size*sizeof(int32_t),file);
799 fread( tfmd.ext,1,tfmd.esize*2*sizeof(int32_t),file);
800 for ( i=0; i<22 && i<tfmd.param_size; ++i )
801 sf->texdata.params[i] = getlong(file);
802 if ( tfmd.param_size==22 ) sf->texdata.type = tex_math;
803 else if ( tfmd.param_size==13 ) sf->texdata.type = tex_mathext;
804 else if ( tfmd.param_size>=7 ) sf->texdata.type = tex_text;
805
806 tfmd.charlist = malloc(65536*sizeof(int32_t));
807 memset(tfmd.charlist,-1,65536*sizeof(int32_t));
808
809 fseek( file, (14+tfmd.head_len)*sizeof(int32_t), SEEK_SET);
810 for ( i=tfmd.first; i<=tfmd.last; ++i ) {
811 /* width = */ getushort(file);
812 height = getc(file);
813 depth = getc(file);
814 ictag = getc(file);
815 tag = getc(file)&0x3; /* Remaining 6 bytes are "reserved for future use" I think */
816 left = getushort(file);
817 if ( tag==1 )
818 ofmDoLigKern(sf,i,left,&tfmd,map);
819 else if ( tag==2 )
820 tfmd.charlist[i] = left;
821 else if ( tag==3 )
822 ofmDoExten(sf,i,&tfmd,left,map);
823 if ( map->map[i]!=-1 && sf->glyphs[map->map[i]]!=NULL ) {
824 SplineChar *sc = sf->glyphs[map->map[i]];
825 /* TFtoPL says very clearly: The actual width of a character is */
826 /* width[width_index], in design-size units. It is not in design units */
827 /* it is stored as a fraction of the design size in a fixed word */
828 if ( height<tfmd.height_size )
829 sc->tex_height = BigEndianWord(tfmd.httab+4*height)*scale;
830 if ( depth<tfmd.depth_size )
831 sc->tex_depth = BigEndianWord(tfmd.dptab+4*depth)*scale;
832 if ( ictag!=0 && ictag<tfmd.italic_size )
833 sc->italic_correction = BigEndianWord(tfmd.ictab+4*ictag)*scale;
834 }
835 }
836
837 for ( i=tfmd.first; i<=tfmd.last; ++i ) {
838 if ( tfmd.charlist[i]!=-1 )
839 tfmDoCharList(sf,i,&tfmd,map);
840 }
841
842 free( tfmd.ligkerntab); free(tfmd.kerntab); free(tfmd.ext); free(tfmd.ictab);
843 free( tfmd.dptab ); free( tfmd.httab ); free( tfmd.widtab );
844 free( tfmd.charlist );
845 fclose(file);
846 return( 1 );
847 }
848 /* ************************************************************************** */
849
850 const char *EncodingName(Encoding *map) {
851 const char *name = map->iconv_name != NULL ? map->iconv_name : map->enc_name;
852 int len = strlen(name);
853 char *pt;
854
855 if ( strmatch(name,"AdobeStandard")==0 )
856 return( "AdobeStandardEncoding" );
857 if (( strstr(name,"8859")!=NULL && name[len-1]=='1' &&
858 (!isdigit(name[len-2]) || name[len-2]=='9') ) ||
859 strstrmatch(name,"latin1")!=NULL )
860 return( "ISOLatin1Encoding" );
861 else if ( map->is_unicodebmp || map->is_unicodefull )
862 return( "ISO10646-1" );
863 else if ( strmatch(name,"mac")==0 || strmatch(name,"macintosh")==0 ||
864 strmatch(name,"macroman")==0 )
865 return( "MacRoman" );
866 else if ( strmatch(name,"ms-ansi")==0 || strstrmatch(name,"1252")!=NULL )
867 return( "WinRoman" );
868 else if ( strmatch(name,"sjis")==0 ||
869 ((pt = strstrmatch(name,"jp"))!=NULL && pt[2]=='\0' &&
870 strstr(name,"646")==NULL ))
871 return( "JISX0208.1997" );
872 else if ( map->is_japanese )
873 return( "JISX0212.1990" );
874 else if ( strmatch(name,"johab")==0 )
875 return( "Johab" );
876 else if ( map->is_korean )
877 return( "KSC5601.1992" );
878 else if ( map->is_simplechinese )
879 return( "GB2312.1980" );
880 else if ( strstrmatch(name,"hkscs")!=NULL )
881 return( "BIG5HKSCS.2001" );
882 else if ( map->is_tradchinese )
883 return( "BIG5" ); /* 2002? */
884
885 if ( map->is_custom || map->is_original || map->is_compact )
886 return( "FontSpecific" );
887
888 return( name );
889 }
890
891 static void AfmLigOut(FILE *afm, SplineChar *sc) {
892 LigList *ll;
893 PST *lig;
894 char *pt, *eos;
895
896 for ( ll=sc->ligofme; ll!=NULL; ll=ll->next ) {
897 lig = ll->lig;
898 if ( !lig->subtable->lookup->store_in_afm )
899 continue;
900 pt = strchr(lig->u.lig.components,' ');
901 eos = strrchr(lig->u.lig.components,' ');
902 if ( pt!=NULL && eos==pt )
903 /* AFM files don't seem to support 3 (or more) letter combos */
904 fprintf( afm, " L %s %s ;", pt+1, lig->u.lig.lig->name );
905 }
906 }
907
908 static void AfmSplineCharX(FILE *afm, SplineChar *sc, int enc, int layer) {
909 DBounds b;
910 int em = (sc->parent->ascent+sc->parent->descent);
911
912 SplineCharLayerFindBounds(sc,layer,&b);
913 /*fprintf( afm, "CX <%04x> ; WX %d ; N %s ; B %d %d %d %d ;",*/
914 fprintf( afm, "C %d ; WX %d ; ", enc, sc->width*1000/em );
915 if ( sc->parent->hasvmetrics )
916 fprintf( afm, "WY %d ; ", sc->vwidth*1000/em );
917 fprintf( afm, "N %s ; B %d %d %d %d ;",
918 sc->name,
919 (int) floor(b.minx*1000/em), (int) floor(b.miny*1000/em),
920 (int) ceil(b.maxx*1000/em), (int) ceil(b.maxy*1000/em) );
921 if (sc->ligofme!=NULL)
922 AfmLigOut(afm,sc);
923 putc('\n',afm);
924 ff_progress_next();
925 }
926
927 static void AfmZapfCharX(FILE *afm, int zi) {
928
929 /*fprintf( afm, "CX <%04x> ; WX %d ; N %s ; B %d %d %d %d ;\n",*/
930 fprintf( afm, "C %d ; WX %d ; N %s ; B %d %d %d %d ;\n",
931 0x2700+zi, zapfwx[zi], zapfnomen[zi],
932 zapfbb[zi][0], zapfbb[zi][1], zapfbb[zi][2], zapfbb[zi][3]);
933 }
934
935 static void AfmSplineChar(FILE *afm, SplineChar *sc, int enc,int layer) {
936 DBounds b;
937 int em = (sc->parent->ascent+sc->parent->descent);
938
939 SplineCharLayerFindBounds(sc,layer,&b);
940 fprintf( afm, "C %d ; WX %d ; ", enc, sc->width*1000/em );
941 if ( sc->parent->hasvmetrics )
942 fprintf( afm, "WY %d ; ", sc->vwidth*1000/em );
943 fprintf( afm, "N %s ; B %d %d %d %d ;",
944 sc->name,
945 (int) floor(b.minx*1000/em), (int) floor(b.miny*1000/em),
946 (int) ceil(b.maxx*1000/em), (int) ceil(b.maxy*1000/em) );
947 if (sc->ligofme!=NULL)
948 AfmLigOut(afm,sc);
949 putc('\n',afm);
950 ff_progress_next();
951 }
952
953 static void AfmCIDChar(FILE *afm, SplineChar *sc, int enc,int layer) {
954 DBounds b;
955 int em = (sc->parent->ascent+sc->parent->descent);
956
957 SplineCharLayerFindBounds(sc,layer,&b);
958 fprintf( afm, "C -1 ; WX %d ; ", sc->width*1000/em );
959 if ( sc->parent->hasvmetrics )
960 fprintf( afm, "WY %d ; ", sc->vwidth*1000/em );
961 fprintf( afm, "N %d ; B %d %d %d %d ;",
962 enc,
963 (int) floor(b.minx*1000/em), (int) floor(b.miny*1000/em),
964 (int) ceil(b.maxx*1000/em), (int) ceil(b.maxy*1000/em) );
965 putc('\n',afm);
966 ff_progress_next();
967 }
968
969 static int anykerns(SplineFont *sf,int isv) {
970 int i, cnt = 0;
971 KernPair *kp;
972
973 for ( i=0; i<sf->glyphcnt; ++i ) {
974 if ( sf->glyphs[i]!=NULL && strcmp(sf->glyphs[i]->name,".notdef")!=0 ) {
975 for ( kp = isv ? sf->glyphs[i]->vkerns : sf->glyphs[i]->kerns; kp!=NULL; kp = kp->next )
976 if ( kp->off!=0 && strcmp(kp->sc->name,".notdef")!=0 &&
977 (kp->sc->parent==sf || sf->cidmaster!=NULL) )
978 ++cnt;
979 }
980 }
981 return( cnt );
982 }
983
984 static void AfmKernPairs(FILE *afm, SplineChar *sc, int isv) {
985 KernPair *kp;
986 int em = (sc->parent->ascent+sc->parent->descent);
987
988 if ( strcmp(sc->name,".notdef")==0 )
989 return;
990
991 for ( kp = isv ? sc->vkerns : sc->kerns; kp!=NULL; kp=kp->next ) {
992 if ( kp->sc->parent!=sc->parent && sc->parent->cidmaster==NULL )
993 continue; /* Can happen when saving multiple pfbs */
994 if ( strcmp(kp->sc->name,".notdef")!=0 && kp->off!=0 ) {
995 if ( isv )
996 fprintf( afm, "KPY %s %s %d\n", sc->name, kp->sc->name, kp->off*1000/em );
997 else
998 fprintf( afm, "KPX %s %s %d\n", sc->name, kp->sc->name, kp->off*1000/em );
999 }
1000 }
1001 }
1002
1003 int SFFindNotdef(SplineFont *sf, int fixed) {
1004 int notdefpos = -1, i, width=-1;
1005 /* If all glyphs have the same known width set fixed to it */
1006 /* If glyphs are proportional set fixed to -1 */
1007 /* If we don't know yet, set fixed to -2 */
1008
1009 if ( fixed==-2 ) { /* Unknown */
1010 for ( i=0; i<sf->glyphcnt; ++i ) if ( SCWorthOutputting(sf->glyphs[i]) ) {
1011 if ( strcmp(sf->glyphs[i]->name,".notdef")==0 ) {
1012 if ( notdefpos==-1 ) notdefpos = i;
1013 } else if ( width==-1 )
1014 width = sf->glyphs[i]->width;
1015 else if ( width!=sf->glyphs[i]->width )
1016 width = -2;
1017 }
1018 if ( width>=0 && sf->glyphcnt>2 && notdefpos>=0) {
1019 if ( width!=sf->glyphs[notdefpos]->width ) {
1020 notdefpos = -1;
1021 for ( i=0; i<sf->glyphcnt; ++i ) if ( SCWorthOutputting(sf->glyphs[i]) ) {
1022 if ( strcmp(sf->glyphs[i]->name,".notdef")==0 &&
1023 sf->glyphs[i]->width == width ) {
1024 notdefpos = i;
1025 break;
1026 }
1027 }
1028 }
1029 }
1030 } else if ( fixed>=0 ) { /* Known, fixed width */
1031 for ( i=0; i<sf->glyphcnt; ++i ) if ( SCWorthOutputting(sf->glyphs[i]) ) {
1032 if ( strcmp(sf->glyphs[i]->name,".notdef")==0 &&
1033 sf->glyphs[i]->width == fixed )
1034 return( i );
1035 }
1036 } else { /* Known, variable width */
1037 for ( i=0; i<sf->glyphcnt; ++i ) if ( SCWorthOutputting(sf->glyphs[i]) ) {
1038 if ( strcmp(sf->glyphs[i]->name,".notdef")==0 )
1039 return( i );
1040 }
1041 }
1042
1043 return( notdefpos );
1044 }
1045
1046 int SCDrawsSomething(SplineChar *sc) {
1047 int layer,l;
1048 RefChar *ref;
1049
1050 if ( sc==NULL )
1051 return( false );
1052 for ( layer = 0; layer<sc->layer_cnt; ++layer ) if ( !sc->layers[layer].background ) {
1053 if ( sc->layers[layer].splines!=NULL || sc->layers[layer].images!=NULL )
1054 return( true );
1055 for ( ref = sc->layers[layer].refs; ref!=NULL; ref=ref->next )
1056 for ( l=0; l<ref->layer_cnt; ++l )
1057 if ( ref->layers[l].splines!=NULL )
1058 return( true );
1059 }
1060 return( false );
1061 }
1062
1063 int SCDrawsSomethingOnLayer(SplineChar *sc, int layer) {
1064 int l;
1065 RefChar *ref;
1066
1067 if ( sc==NULL )
1068 return( false );
1069 if (layer<sc->layer_cnt) {
1070 if ( sc->layers[layer].splines!=NULL || sc->layers[layer].images!=NULL )
1071 return( true );
1072 for ( ref = sc->layers[layer].refs; ref!=NULL; ref=ref->next )
1073 for ( l=0; l<ref->layer_cnt; ++l )
1074 if ( ref->layers[l].splines!=NULL )
1075 return( true );
1076 }
1077 return( false );
1078 }
1079
1080 int SCWorthOutputting(SplineChar *sc) {
1081 return( sc!=NULL &&
1082 ( SCDrawsSomething(sc) || sc->widthset || sc->anchor!=NULL ||
1083 #if HANYANG
1084 sc->compositionunit ||
1085 #endif
1086 sc->dependents!=NULL /*||
1087 sc->width!=sc->parent->ascent+sc->parent->descent*/ ) );
1088 }
1089
1090 int SCHasData(SplineChar *sc) {
1091 int layer;
1092
1093 if ( sc==NULL )
1094 return( false );
1095 for ( layer = 0; layer<sc->layer_cnt; ++layer ) if ( sc->layers[layer].python_persistent ) return true;
1096 return false;
1097 }
1098
1099 int LayerWorthOutputting(SplineFont *sf, int layer) {
1100 int glyphpos = 0;
1101 for (glyphpos = 0; glyphpos < sf->glyphcnt; glyphpos++) {
1102 if (SCDrawsSomethingOnLayer(sf->glyphs[glyphpos], layer)) return 1;
1103 }
1104 return 0;
1105 }
1106
1107 int SCLWorthOutputtingOrHasData(SplineChar *sc, int layer) {
1108 if (sc == NULL) return 0;
1109 if (layer >= sc->layer_cnt) return 0;
1110 if (SCDrawsSomethingOnLayer(sc, layer)) return 1;
1111 if (sc->layers[layer].python_persistent) return 1;
1112 return 0;
1113 }
1114
1115 int CIDWorthOutputting(SplineFont *cidmaster, int enc) {
1116 int i;
1117
1118 if ( enc<0 )
1119 return( -1 );
1120
1121 if ( cidmaster->subfontcnt==0 )
1122 return( enc>=cidmaster->glyphcnt?-1:SCWorthOutputting(cidmaster->glyphs[enc])?0:-1 );
1123
1124 for ( i=0; i<cidmaster->subfontcnt; ++i )
1125 if ( enc<cidmaster->subfonts[i]->glyphcnt &&
1126 SCWorthOutputting(cidmaster->subfonts[i]->glyphs[enc]))
1127 return( i );
1128
1129 return( -1 );
1130 }
1131
1132 static void AfmSplineFontHeader(FILE *afm, SplineFont *sf, int formattype,
1133 EncMap *map, SplineFont *fullsf,int layer) {
1134 DBounds b;
1135 real width;
1136 int i, j, cnt, max;
1137 bigreal caph, xh, ash, dsh;
1138 int iscid = ( formattype==ff_cid || formattype==ff_otfcid );
1139 int ismm = ( formattype==ff_mma || formattype==ff_mmb );
1140 time_t now;
1141 SplineChar *sc;
1142 int em = (sf->ascent+sf->descent);
1143
1144 if ( iscid && sf->cidmaster!=NULL ) sf = sf->cidmaster;
1145
1146 max = sf->glyphcnt;
1147 if ( iscid ) {
1148 max = 0;
1149 for ( i=0; i<sf->subfontcnt; ++i )
1150 if ( sf->subfonts[i]->glyphcnt>max )
1151 max = sf->subfonts[i]->glyphcnt;
1152 }
1153 caph = SFCapHeight(sf,layer,true);
1154 xh = SFXHeight(sf,layer,true);
1155 ash = SFAscender(sf,layer,true);
1156 dsh = SFDescender(sf,layer,true);
1157 cnt = 0;
1158 for ( i=0; i<max; ++i ) {
1159 sc = NULL;
1160 if ( iscid ) {
1161 for ( j=0; j<sf->subfontcnt; ++j )
1162 if ( i<sf->subfonts[j]->glyphcnt && sf->subfonts[j]->glyphs[i]!=NULL ) {
1163 sc = sf->subfonts[j]->glyphs[i];
1164 break;
1165 }
1166 } else
1167 sc = sf->glyphs[i];
1168 if ( sc!=NULL ) {
1169 if ( SCWorthOutputting(sc) || (iscid && i==0 && sc!=NULL))
1170 ++cnt;
1171 }
1172 }
1173
1174 fprintf( afm, ismm ? "StartMasterFontMetrics 4.0\n" :
1175 iscid ? "StartFontMetrics 4.1\n" :
1176 "StartFontMetrics 2.0\n" );
1177 fprintf( afm, "Comment Generated by FontForge %s\n", FONTFORGE_VERSION );
1178 now = GetTime();
1179 fprintf(afm,"Comment Creation Date: %s", asctime(gmtime(&now)));
1180 fprintf( afm, "FontName %s\n", sf->fontname );
1181 if ( sf->fullname!=NULL ) fprintf( afm, "FullName %s\n", sf->fullname );
1182 if ( sf->familyname!=NULL ) fprintf( afm, "FamilyName %s\n", sf->familyname );
1183 if ( sf->weight!=NULL ) fprintf( afm, "Weight %s\n", sf->weight );
1184 /* AFM lines are limited to 256 characters and US ASCII */
1185 if ( sf->copyright!=NULL ) {
1186 char *pt, *start, *p;
•1187 for ( pt=start=sf->copyright; *pt && pt-start<200 && *pt!='\n'; ++pt );
1188 fprintf( afm, "Notice (" );
1189 for ( p=start; p<pt; ++p )
1190 if ( *p=='\xa9' ) fprintf(afm,"(C)");
1191 else if ( *p=='\t' || (*p>=' ' && *p<0x7f))
1192 putc(*p,afm);
1193 fprintf( afm, ")\n" );
1194 while ( *pt ) {
1195 start = pt;
1196 if ( *start=='\n' ) ++start;
1197 for ( pt=start; *pt && pt-start<200 && *pt!='\n'; ++pt );
1198 fprintf( afm, "Comment " );
1199 for ( p=start; p<pt; ++p )
1200 if ( *p=='\xa9' ) fprintf(afm,"(C)");
1201 else if ( *p=='\t' || (*p>=' ' && *p<0x7f))
1202 putc(*p,afm);
1203 fprintf( afm, "\n" );
1204 }
1205 }
1206 if ( iscid ) {
1207 fprintf( afm, "Characters %d\n", cnt );
1208 fprintf( afm, "Version %g\n", (double)sf->cidversion );
1209 fprintf( afm, "CharacterSet %s-%s-%d\n", sf->cidregistry, sf->ordering, sf->supplement );
1210 fprintf( afm, "IsBaseFont true\n" );
1211 fprintf( afm, "IsCIDFont true\n" );
1212 }
1213 fprintf( afm, "ItalicAngle %g\n", (double) sf->italicangle );
1214 width = SFOneWidth(sf);
1215 fprintf( afm, "IsFixedPitch %s\n", width==-1?"false":"true" );
1216 fprintf( afm, "UnderlinePosition %g\n", (double) sf->upos );
1217 fprintf( afm, "UnderlineThickness %g\n", (double) sf->uwidth );
1218 if ( !iscid ) {
1219 if ( sf->version!=NULL ) fprintf( afm, "Version %s\n", sf->version );
1220 fprintf( afm, "EncodingScheme %s\n", EncodingName(map->enc));
1221 }
1222 if ( iscid ) CIDLayerFindBounds(sf,layer,&b); else SplineFontLayerFindBounds(fullsf!=NULL?fullsf:sf,layer,&b);
1223 fprintf( afm, "FontBBox %d %d %d %d\n",
1224 (int) floor(b.minx*1000/em), (int) floor(b.miny*1000/em),
1225 (int) ceil(b.maxx*1000/em), (int) ceil(b.maxy*1000/em) );
1226 if ( caph!=-1e23 )
1227 fprintf( afm, "CapHeight %d\n", (int) rint(caph*1000/em) );
1228 if ( xh!=-1e23 )
1229 fprintf( afm, "XHeight %d\n", (int) rint(xh*1000/em) );
1230 if ( ash!=-1e23 )
1231 fprintf( afm, "Ascender %d\n", (int) rint(ash*1000/em) );
1232 if ( dsh!=1e23 )
1233 fprintf( afm, "Descender %d\n", (int) rint(dsh*1000/em) );
1234 }
1235
1236 struct cc_accents {
1237 SplineChar *accent;
1238 real xoff, yoff;
1239 struct cc_accents *next;
1240 };
1241
1242 struct cc_data {
1243 char *name;
1244 SplineChar *base;
1245 int acnt;
1246 struct cc_accents *accents;
1247 };
1248
1249 struct cc_container {
1250 struct cc_data *ccs;
1251 int cnt, max; /* total number of ccs used/allocated */
1252 SplineChar ***marks; /* For each ac this is a list of all mark glyphs in it */
1253 int *mcnt; /* Count of the above list */
1254 int *mpos;
1255 SplineFont *sf;
1256 };
1257
1258 #define AC_MAX 5 /* At most 5 Anchor classes may be used/glyph */
1259
1260 static int FigureName(int *unicode,const char *name,int u) {
1261 char *upt, *start, *end, ch;
1262
1263 start = copy(name);
1264 if ( strchr(start,'_')!=NULL ) {
1265 while ( (upt=strchr(start,'_'))!=NULL ) {
1266 *upt='\0';
1267 u = FigureName(unicode,start,u);
1268 *upt = '_';
1269 if ( u==-1 )
1270 return( -1 );
1271 start = upt+1;
1272 }
1273 }
1274 if ( strncmp(start,"uni",3)==0 && (strlen(start)-3)%4==0 ) {
1275 start+=3;
1276 while ( *start ) {
1277 ch = start[4]; start[4] = '\0';
1278 unicode[u++] = strtol(start,&end,16);
1279 start[4] = ch;
1280 if ( *end!='\0' )
1281 return( -1 );
1282 start += 4;
1283 }
1284 return( u );
1285 }
1286 unicode[u] = UniFromName(start,ui_none,&custom);
1287 if ( unicode[u]==-1 )
1288 return( -1 );
1289
1290 return( u+1 );
1291 }
1292
1293 static int FigureUnicodes(int *unicode,SplineChar *sc,int u) {
1294 const unichar_t *upt;
1295
1296 if ( u==-1 )
1297 return( -1 );
1298 if ( sc->unicodeenc==-1 )
1299 return( FigureName(unicode,sc->name,u));
1300 if ( isdecompositionnormative(sc->unicodeenc) &&
1301 (upt = unialt(sc->unicodeenc))!=NULL ) {
1302 while ( *upt!='\0' )
1303 unicode[u++] = *upt++;
1304 } else
1305 unicode[u++] = sc->unicodeenc;
1306 return( u );
1307 }
1308
1309 static int FindDecomposition(int *unicode, int u) {
1310 int uni;
1311 const unichar_t *upt;
1312 int i;
1313
1314 for ( uni=0; uni<0x10000; ++uni ) {
1315 if ( (upt = unialt(uni))!=NULL ) {
1316 for ( i=0; *upt!='\0' && i<u && *upt==(unichar_t)unicode[i]; ++i, ++upt );
1317 if ( *upt=='\0' && i==u )
1318 return( uni );
1319 }
1320 }
1321 return( -1 );
1322 }
1323
1324 static void sortunis(int *unicode,int u) {
1325 int i, j;
1326
1327 for ( i=0; i<u; ++i ) {
1328 unicode[i] = CanonicalCombiner(unicode[i]);
1329 }
1330
1331 for ( i=0; i<u; ++i ) for ( j=i+1; j<u; ++j ) {
1332 if ( unicode[i]>unicode[j] ) {
1333 int temp = unicode[i];
1334 unicode[i] = unicode[j];
1335 unicode[j] = temp;
1336 }
1337 }
1338 }
1339
1340 static char *NameFrom(struct cc_data *this,int *unicode,int u,int uni) {
1341 char *ret, *pt;
1342 char buffer[60];
1343 struct cc_accents *cca;
1344 int i,len;
1345
1346 if ( uni!=-1 )
1347 return( copy( StdGlyphName(buffer,uni,ui_none,NULL)) );
1348 if ( u!=-1 && (unicode[0]<0x370 || unicode[0]>0x3ff) ) {
1349 /* Don't use the unicode decomposition to get a name for greek */
1350 /* glyphs. We'd get acute for tonos, etc. */
1351 ret = malloc(4+4*u);
1352 strcpy(ret,"uni");
1353 pt = ret+3;
1354 for ( i=0; i<u; ++i ) {
1355 sprintf( pt, "%04X", unicode[i] );
1356 pt += 4;
1357 }
1358 return( ret );
1359 }
1360 len = strlen( this->base->name ) +1;
1361 for ( cca = this->accents; cca!=NULL; cca = cca->next )
1362 len += strlen( cca->accent->name ) +1;
1363 ret = malloc(len);
1364 strcpy(ret,this->base->name);
1365 pt = ret + strlen(ret);
1366 for ( cca = this->accents; cca!=NULL; cca = cca->next ) {
1367 *pt++ = '_';
1368 strcpy(pt,cca->accent->name);
1369 pt = pt + strlen(pt);
1370 }
1371 return( ret );
1372 }
1373
1374 static int AfmBuildCCName(struct cc_data *this,struct cc_container *cc) {
1375 int unicode[4*AC_MAX];
1376 int u;
1377 int uni;
1378 struct cc_accents *cca;
1379
1380 u = FigureUnicodes(unicode,this->base,0);
1381 for ( cca = this->accents; cca!=NULL; cca = cca->next )
1382 u=FigureUnicodes(unicode,cca->accent,u);
1383 if ( u!=-1 ) {
1384 unicode[u]= -1;
1385 if ( unicode[0]==0x131 ) unicode[0] = 'i';
1386 if ( unicode[0]==0x237 || unicode[0]==0xf6be ) unicode[0] = 'j';
1387 sortunis(unicode+1,u-1); /* Only sort the accents */
1388 }
1389 uni = FindDecomposition(unicode,u);
1390 if ( uni!=-1 )
1391 if ( SFGetChar(cc->sf,uni,NULL)!=NULL )
1392 return( false ); /* Character already exists in font */
1393 this->name = NameFrom(this,unicode,u,uni);
1394 if ( SFGetChar(cc->sf,-1,this->name)!=NULL ) {
1395 free(this->name);
1396 return( false ); /* Character already exists in font */
1397 }
1398 return( true );
1399 }
1400
1401 static void AfmBuildMarkCombos(SplineChar *sc,AnchorPoint *ap, struct cc_container *cc) {
1402 if ( ap==NULL ) {
1403 AnchorPoint *ap;
1404 struct cc_data *this = &cc->ccs[cc->cnt++];
1405 int acnt=0;
1406 AnchorPoint *map;
1407
1408 this->base = sc;
1409 this->accents = NULL;
1410 for ( ap=sc->anchor; ap!=NULL; ap=ap->next ) if ( ap->ticked ) {
1411 struct cc_accents *cca = chunkalloc(sizeof(struct cc_accents));
1412 cca->accent = cc->marks[ap->anchor->ac_num][cc->mpos[ap->anchor->ac_num]];
1413 for ( map = cca->accent->anchor; map->anchor!=ap->anchor || map->type!=at_mark;
1414 map = map->next );
1415 if ( map!=NULL ) {
1416 cca->xoff = ap->me.x - map->me.x;
1417 cca->yoff = ap->me.y - map->me.y;
1418 }
1419 cca->next = this->accents;
1420 this->accents = cca;
1421 ++acnt;
1422 }
1423 if ( !AfmBuildCCName(this,cc)) {
1424 struct cc_accents *cca, *next;
1425 --cc->cnt;
1426 for ( cca = this->accents; cca!=NULL; cca = next ) {
1427 next = cca->next;
1428 chunkfree(cca,sizeof(struct cc_accents));
1429 }
1430 } else
1431 this->acnt = acnt;
1432 } else if ( ap->ticked ) {
1433 int ac_num = ap->anchor->ac_num;
1434 for ( cc->mpos[ac_num] = 0; cc->mpos[ac_num]<cc->mcnt[ac_num]; ++cc->mpos[ac_num] )
1435 AfmBuildMarkCombos(sc,ap->next,cc);
1436 } else {
1437 AfmBuildMarkCombos(sc,ap->next,cc);
1438 }
1439 }
1440
1441 static void AfmBuildCombos(SplineChar *sc,AnchorPoint *ap, struct cc_container *cc) {
1442
1443 if ( ap!=NULL ) {
1444 AfmBuildCombos(sc,ap->next,cc);
1445 if ( ap->type==at_basechar ) {
1446 ap->ticked = true;
1447 AfmBuildCombos(sc,ap->next,cc);
1448 ap->ticked = false;
1449 }
1450 } else {
1451 int ticks, cnt;
1452 AnchorPoint *ap;
1453
1454 for ( ap=sc->anchor, ticks=0, cnt=1; ap!=NULL; ap=ap->next ) {
1455 if ( ap->ticked ) {
1456 ++ticks;
1457 cnt *= cc->mcnt[ap->anchor->ac_num];
1458 }
1459 }
1460 if ( ticks==0 ) /* No accents classes selected */
1461 return;
1462 if ( ticks>AC_MAX || cnt>200 ) /* Too many selected. I fear combinatorial explosion */
1463 return;
1464 if ( cc->cnt+cnt >= cc->max )
1465 cc->ccs = realloc(cc->ccs,(cc->max += cnt+200)*sizeof(struct cc_data));
1466 AfmBuildMarkCombos(sc,sc->anchor,cc);
1467 }
1468 }
1469
1470 static struct cc_data *AfmFigureCCdata(SplineFont *sf,int *total) {
1471 int ac_cnt, i;
1472 AnchorClass *ac;
1473 AnchorPoint *ap;
1474 SplineChar *sc;
1475 int *mmax;
1476 struct cc_container cc;
1477
1478 memset(&cc,0,sizeof(cc));
1479 cc.sf = sf;
1480 for ( ac=sf->anchor, ac_cnt=0; ac!=NULL; ac=ac->next, ++ac_cnt)
1481 ac->ac_num = ac_cnt;
1482 cc.mcnt = calloc(ac_cnt,sizeof(int));
1483 cc.mpos = calloc(ac_cnt,sizeof(int));
1484 mmax = calloc(ac_cnt,sizeof(int));
1485 cc.marks = calloc(ac_cnt,sizeof(SplineChar **));
1486 for ( i=0; i<sf->glyphcnt; ++i ) if ( (sc = sf->glyphs[i])!=NULL ) {
1487 for ( ap = sc->anchor; ap!=NULL; ap=ap->next ) if ( ap->type==at_mark )
1488 ++mmax[ap->anchor->ac_num];
1489 }
1490 for ( i=0; i<ac_cnt; ++i )
1491 cc.marks[i] = calloc(mmax[i],sizeof(SplineChar *));
1492 for ( i=0; i<sf->glyphcnt; ++i ) if ( (sc = sf->glyphs[i])!=NULL ) {
1493 for ( ap = sc->anchor; ap!=NULL; ap=ap->next ) if ( ap->type==at_mark )
1494 cc.marks[ap->anchor->ac_num][cc.mcnt[ap->anchor->ac_num]++] = sc;
1495 }
1496 for ( i=0; i<sf->glyphcnt; ++i )
1497 if ( (sc = sf->glyphs[i])!=NULL && sc->anchor!=NULL ) {
1498 for ( ap = sc->anchor; ap!=NULL; ap=ap->next )
1499 ap->ticked = false;
1500 AfmBuildCombos(sc,sc->anchor,&cc);
1501 }
1502 if ( cc.cnt+1 >= cc.max )
1503 cc.ccs = realloc(cc.ccs,(cc.max += 1)*sizeof(struct cc_data));
1504 cc.ccs[cc.cnt].base = NULL; /* End of list mark */
1505 for ( i=0; i<ac_cnt; ++i )
1506 free(cc.marks[i]);
1507 free(cc.marks);
1508 free(cc.mcnt);
1509 free(cc.mpos);
1510 free(mmax);
1511 *total = cc.cnt;
1512 return( cc.ccs );
1513 }
1514
1515 static void CCFree(struct cc_data *cc) {
1516 int i;
1517 struct cc_accents *cca, *next;
1518
1519 if ( cc==NULL )
1520 return;
1521 for ( i=0; cc[i].base!=NULL; ++i ) {
1522 free(cc[i].name);
1523 for ( cca = cc[i].accents; cca!=NULL; cca = next ) {
1524 next = cca->next;
1525 chunkfree(cca,sizeof(struct cc_accents));
1526 }
1527 }
1528 free( cc );
1529 }
1530
1531 static void AfmComposites(FILE *afm, SplineFont *sf, struct cc_data *cc, int cc_cnt) {
1532 int i;
1533 struct cc_accents *cca;
1534 bigreal em = (sf->ascent+sf->descent);
1535
1536 fprintf( afm, "StartComposites %d\n", cc_cnt );
1537 for ( i=0; i<cc_cnt; ++i ) {
1538 fprintf( afm, "CC %s %d; PCC %s 0 0;",
1539 cc[i].name, cc[i].acnt+1, cc[i].base->name );
1540 for ( cca = cc[i].accents; cca!=NULL; cca = cca->next ) {
1541 fprintf( afm, " PCC %s %g %g;",
1542 cca->accent->name, rint(1000.0*cca->xoff/em), rint(1000.0*cca->yoff/em) );
1543 }
1544 putc('\n',afm);
1545 }
1546 fprintf( afm, "EndComposites\n" );
1547 }
1548
1549 static void AfmCompositeChar(FILE *afm,struct cc_data *cc,int layer) {
1550 DBounds b, b1;
1551 SplineChar *sc = cc->base;
1552 SplineFont *sf = sc->parent;
1553 int em = (sf->ascent+sf->descent);
1554 struct cc_accents *cca;
1555
1556 SplineCharLayerFindBounds(sc,layer,&b);
1557 for ( cca = cc->accents; cca!=NULL; cca = cca->next ) {
1558 SplineCharLayerFindBounds(cca->accent,layer,&b1);
1559 if ( b1.minx+cca->xoff<b.minx ) b.minx = b1.minx+cca->xoff;
1560 if ( b1.maxx+cca->xoff>b.maxx ) b.maxx = b1.maxx+cca->xoff;
1561 if ( b1.miny+cca->yoff<b.miny ) b.miny = b1.miny+cca->yoff;
1562 if ( b1.maxy+cca->yoff>b.maxy ) b.maxy = b1.maxy+cca->yoff;
1563 }
1564 fprintf( afm, "C %d ; WX %d ; ", -2, sc->width*1000/em );
1565 if ( sf->hasvmetrics )
1566 fprintf( afm, "WY %d ; ", sc->vwidth*1000/em );
1567 fprintf( afm, "N %s ; B %d %d %d %d ;",
1568 cc->name,
1569 (int) floor(b.minx*1000/em), (int) floor(b.miny*1000/em),
1570 (int) ceil(b.maxx*1000/em), (int) ceil(b.maxy*1000/em) );
1571 putc('\n',afm);
1572 }
1573
1574 static void LigatureClosure(SplineFont *sf);
1575
1576 int AfmSplineFont(FILE *afm, SplineFont *sf, int formattype,EncMap *map,
1577 int docc, SplineFont *fullsf, int layer) {
1578 int i, j, cnt, vcnt;
1579 int type0 = ( formattype==ff_ptype0 );
1580 int otf = (formattype==ff_otf);
1581 int encmax=(!type0 && !otf)?256:65536;
1582 int anyzapf;
1583 int iscid = ( formattype==ff_cid || formattype==ff_otfcid );
1584 SplineChar *sc;
1585 int cc_cnt = 0;
1586 struct cc_data *cc= NULL;
1587
1588 SFLigaturePrepare(sf);
1589 LigatureClosure(sf); /* Convert 3 character ligs to a set of two character ones when possible */
1590 SFKernClassTempDecompose(sf,false); /* Undoes kern classes */
1591 SFKernClassTempDecompose(sf,true);
1592 if ( sf->anchor!=NULL && docc )
1593 cc = AfmFigureCCdata(sf,&cc_cnt);
1594
1595 if ( iscid && sf->cidmaster!=NULL ) sf = sf->cidmaster;
1596
1597 AfmSplineFontHeader(afm,sf,formattype,map,fullsf,layer);
1598
1599 cnt = 0;
1600 for ( i=0; i<map->enccount; ++i ) {
1601 int gid = map->map[i];
1602 if ( gid==-1 )
1603 continue;
1604 sc = NULL;
1605 if ( iscid ) {
1606 for ( j=0; j<sf->subfontcnt; ++j )
1607 if ( gid<sf->subfonts[j]->glyphcnt && sf->subfonts[j]->glyphs[gid]!=NULL ) {
1608 sc = sf->subfonts[j]->glyphs[gid];
1609 break;
1610 }
1611 } else
1612 sc = sf->glyphs[gid];
1613 if ( SCWorthOutputting(sc) || (iscid && i==0 && sc!=NULL))
1614 ++cnt;
1615 }
1616
1617 anyzapf = false;
1618 if ( type0 && (map->enc->is_unicodebmp ||
1619 map->enc->is_unicodefull)) {
1620 for ( i=0x2700; i<map->enccount && i<encmax && i<=0x27ff; ++i ) {
1621 int gid = map->map[i];
1622 if ( gid!=-1 && SCWorthOutputting(sf->glyphs[gid]) )
1623 anyzapf = true;
1624 }
1625 if ( !anyzapf )
1626 for ( i=0; i<0xc0; ++i )
1627 if ( zapfexists[i])
1628 ++cnt;
1629 }
1630
1631 fprintf( afm, "StartCharMetrics %d\n", cnt+cc_cnt );
1632 if ( iscid ) {
1633 for ( i=0; i<map->enccount; ++i ) {
1634 int gid = map->map[i];
1635 if ( gid==-1 )
1636 continue;
1637 sc = NULL;
1638 for ( j=0; j<sf->subfontcnt; ++j )
1639 if ( gid<sf->subfonts[j]->glyphcnt && sf->subfonts[j]->glyphs[gid]!=NULL ) {
1640 sc = sf->subfonts[j]->glyphs[gid];
1641 break;
1642 }
1643 if ( SCWorthOutputting(sc) || (i==0 && sc!=NULL) )
1644 AfmCIDChar(afm, sc, i,layer);
1645 }
1646 } else if ( type0 && (map->enc->is_unicodebmp ||
1647 map->enc->is_unicodefull)) {
1648 for ( i=0; i<map->enccount && i<encmax && i<0x2700; ++i ) {
1649 int gid = map->map[i];
1650 if ( gid!=-1 && SCWorthOutputting(sf->glyphs[gid]) ) {
1651 AfmSplineCharX(afm,sf->glyphs[gid],i,layer);
1652 }
1653 }
1654 if ( !anyzapf ) {
1655 for ( i=0; i<0xc0; ++i ) if ( zapfexists[i] )
1656 AfmZapfCharX(afm,i);
1657 i += 0x2700;
1658 }
1659 for ( ; i<map->enccount && i<encmax; ++i ) {
1660 int gid = map->map[i];
1661 if ( gid!=-1 && SCWorthOutputting(sf->glyphs[gid]) ) {
1662 AfmSplineCharX(afm,sf->glyphs[gid],i,layer);
1663 }
1664 }
1665 } else if ( type0 ) {
1666 for ( i=0; i<map->enccount && i<encmax; ++i ) {
1667 int gid = map->map[i];
1668 if ( gid!=-1 && SCWorthOutputting(sf->glyphs[gid]) ) {
1669 AfmSplineCharX(afm,sf->glyphs[gid],i,layer);
1670 }
1671 }
1672 } else {
1673 for ( i=0; i<map->enccount && i<encmax; ++i ) {
1674 int gid = map->map[i];
1675 if ( gid!=-1 && SCWorthOutputting(sf->glyphs[gid]) ) {
1676 AfmSplineChar(afm,sf->glyphs[gid],i,layer);
1677 }
1678 }
1679 }
1680 for ( ; i<map->enccount ; ++i ) {
1681 int gid = map->map[i];
1682 if ( gid!=-1 && SCWorthOutputting(sf->glyphs[gid]) ) {
1683 AfmSplineChar(afm,sf->glyphs[gid],-1,layer);
1684 }
1685 }
1686 for ( i=0; i<cc_cnt; ++i )
1687 AfmCompositeChar(afm,&cc[i],layer);
1688 fprintf( afm, "EndCharMetrics\n" );
1689 vcnt = anykerns(sf,true);
1690 if ( (cnt = anykerns(sf,false))>0 || vcnt>0 ) {
1691 fprintf( afm, "StartKernData\n" );
1692 if ( cnt>0 ) {
1693 fprintf( afm, "StartKernPairs%s %d\n", vcnt==0?"":"0", cnt );
1694 for ( i=0; i<map->enccount ; ++i ) {
1695 int gid = map->map[i];
1696 if ( gid!=-1 && SCWorthOutputting(sf->glyphs[gid]) ) {
1697 AfmKernPairs(afm,sf->glyphs[gid],false);
1698 }
1699 }
1700 fprintf( afm, "EndKernPairs\n" );
1701 }
1702 if ( vcnt>0 ) {
1703 fprintf( afm, "StartKernPairs1 %d\n", vcnt );
1704 for ( i=0; i<map->enccount ; ++i ) {
1705 int gid = map->map[i];
1706 if ( gid!=-1 && SCWorthOutputting(sf->glyphs[gid]) ) {
1707 AfmKernPairs(afm,sf->glyphs[gid],true);
1708 }
1709 }
1710 fprintf( afm, "EndKernPairs\n" );
1711 }
1712 fprintf( afm, "EndKernData\n" );
1713 }
1714 if ( cc!=NULL )
1715 AfmComposites(afm,sf,cc,cc_cnt);
1716 fprintf( afm, "EndFontMetrics\n" );
1717
1718 SFLigatureCleanup(sf);
1719 SFKernCleanup(sf,false);
1720 SFKernCleanup(sf,true);
1721 CCFree(cc);
1722
1723 return( !ferror(afm));
1724 }
1725
1726 int AmfmSplineFont(FILE *amfm, MMSet *mm, int formattype,EncMap *map,int layer) {
1727 int i,j;
1728
1729 AfmSplineFontHeader(amfm,mm->normal,formattype,map,NULL,layer);
1730 fprintf( amfm, "Masters %d\n", mm->instance_count );
1731 fprintf( amfm, "Axes %d\n", mm->axis_count );
1732
1733 fprintf( amfm, "WeightVector [%g", (double) mm->defweights[0] );
1734 for ( i=1; i<mm->instance_count; ++i )
1735 fprintf( amfm, " %g", (double) mm->defweights[i]);
1736 fprintf( amfm, "]\n" );
1737
1738 fprintf( amfm, "BlendDesignPositions [" );
1739 for ( i=0; i<mm->instance_count; ++i ) {
1740 fprintf(amfm, "[%g", (double) mm->positions[i*mm->axis_count+0]);
1741 for ( j=1; j<mm->axis_count; ++j )
1742 fprintf( amfm, " %g", (double) mm->positions[i*mm->axis_count+j]);
1743 fprintf(amfm, i==mm->instance_count-1 ? "]" : "] " );
1744 }
1745 fprintf( amfm, "]\n" );
1746
1747 fprintf( amfm, "BlendDesignMap [" );
1748 for ( i=0; i<mm->axis_count; ++i ) {
1749 putc('[',amfm);
1750 for ( j=0; j<mm->axismaps[i].points; ++j )
1751 fprintf(amfm, "[%g %g]", (double) mm->axismaps[i].designs[j], (double) mm->axismaps[i].blends[j]);
1752 fprintf(amfm, i==mm->axis_count-1 ? "]" : "] " );
1753 }
1754 fprintf( amfm, "]\n" );
1755
1756 fprintf( amfm, "BlendAxisTypes [/%s", mm->axes[0] );
1757 for ( j=1; j<mm->axis_count; ++j )
1758 fprintf( amfm, " /%s", mm->axes[j]);
1759 fprintf( amfm, "]\n" );
1760
1761 for ( i=0; i<mm->axis_count; ++i ) {
1762 fprintf( amfm, "StartAxis\n" );
1763 fprintf( amfm, "AxisType %s\n", mm->axes[i] );
1764 fprintf( amfm, "AxisLabel %s\n", MMAxisAbrev(mm->axes[i]) );
1765 fprintf( amfm, "EndAxis\n" );
1766 }
1767
1768 for ( i=0; i<mm->instance_count; ++i ) {
1769 fprintf( amfm, "StartMaster\n" );
1770 fprintf( amfm, "FontName %s\n", mm->instances[i]->fontname );
1771 if ( mm->instances[i]->fullname!=NULL )
1772 fprintf( amfm, "FullName %s\n", mm->instances[i]->fullname );
1773 if ( mm->instances[i]->familyname!=NULL )
1774 fprintf( amfm, "FamilyName %s\n", mm->instances[i]->familyname );
1775 if ( mm->instances[i]->version!=NULL )
1776 fprintf( amfm, "Version %s\n", mm->instances[i]->version );
1777 fprintf( amfm, "WeightVector [%d", i==0 );
1778 for ( j=1; j<mm->instance_count; ++j )
1779 fprintf( amfm, " %d", i==j );
1780 fprintf( amfm, "]\n" );
1781 fprintf( amfm, "EndMaster\n" );
1782 }
1783 fprintf( amfm, "EndMasterFontMetrics\n" );
1784 return( !ferror(amfm));
1785 }
1786
1787 void SFLigatureCleanup(SplineFont *sf) {
1788 LigList *l, *next;
1789 struct splinecharlist *scl, *sclnext;
1790 int j;
1791
1792 if (sf->internal_temp)
1793 return;
1794
1795 for ( j=0; j<sf->glyphcnt; ++j ) if ( sf->glyphs[j]!=NULL ) {
1796 for ( l = sf->glyphs[j]->ligofme; l!=NULL; l = next ) {
1797 next = l->next;
1798 for ( scl = l->components; scl!=NULL; scl = sclnext ) {
1799 sclnext = scl->next;
1800 chunkfree(scl,sizeof(struct splinecharlist));
1801 }
1802 if ( l->lig->temporary ) {
1803 free(l->lig->u.lig.components);
1804 chunkfree(l->lig,sizeof(PST));
1805 }
1806 free( l );
1807 }
1808 sf->glyphs[j]->ligofme = NULL;
1809 }
1810 }
1811
1812 void SFLigaturePrepare(SplineFont *sf) {
1813 PST *lig;
1814 LigList *ll;
1815 int i,j,k,ch;
1816 char *pt, *ligstart;
1817 SplineChar *sc, *tsc;
1818 struct splinecharlist *head, *last;
1819 int ccnt, lcnt, lmax=20;
1820 LigList **all = malloc(lmax*sizeof(LigList *));
1821
1822 /* First clear out any old stuff */
1823 for ( j=0; j<sf->glyphcnt; ++j ) if ( sf->glyphs[j]!=NULL )
1824 sf->glyphs[j]->ligofme = NULL;
1825
1826 /* Attach all the ligatures to the first character of their components */
1827 /* Figure out what the components are, and if they all exist */
1828 /* we're only interested in the lig if all components are worth outputting */
1829 for ( i=0 ; i<sf->glyphcnt; ++i ) if ( SCWorthOutputting(sf->glyphs[i]) && sf->glyphs[i]->possub!=NULL ) {
1830 for ( lig = sf->glyphs[i]->possub; lig!=NULL; lig=lig->next ) if ( lig->type==pst_ligature ) {
1831 ligstart = lig->u.lig.components;
1832 last = head = NULL; sc = NULL;
1833 for ( pt = ligstart; *pt!='\0'; ) {
1834 char *start = pt;
1835 for ( ; *pt!='\0' && *pt!=' '; ++pt );
1836 ch = *pt; *pt = '\0';
1837 tsc = SFGetChar(sf,-1,start);
1838 *pt = ch;
1839 if ( tsc!=NULL ) {
1840 if ( !SCWorthOutputting(tsc)) {
1841 sc = NULL;
1842 break;
1843 }
1844 if ( sc==NULL ) {
1845 sc = tsc;
1846 ccnt = 1;
1847 } else {
1848 struct splinecharlist *cur = chunkalloc(sizeof(struct splinecharlist));
1849 if ( head==NULL )
1850 head = cur;
1851 else
1852 last->next = cur;
1853 last = cur;
1854 cur->sc = tsc;
1855 cur->next = NULL;
1856 ++ccnt;
1857 }
1858 } else {
1859 sc = NULL;
1860 break;
1861 }
1862 while ( *pt==' ' ) ++pt;
1863 }
1864 if ( sc!=NULL ) {
1865 ll = malloc(sizeof(LigList));
1866 ll->lig = lig;
1867 ll->next = sc->ligofme;
1868 ll->first = sc;
1869 ll->components = head;
1870 ll->ccnt = ccnt;
1871 sc->ligofme = ll;
1872 } else {
1873 while ( head!=NULL ) {
1874 last = head->next;
1875 chunkfree(head,sizeof(*head));
1876 head = last;
1877 }
1878 }
1879 }
1880 }
1881 for ( i=0 ; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL && sc->ligofme!=NULL ) {
1882 for ( ll=sc->ligofme, lcnt=0; ll!=NULL; ll=ll->next, ++lcnt );
1883 /* Finally, order the list so that the longest ligatures are first */
1884 if ( lcnt>1 ) {
1885 if ( lcnt>=lmax )
1886 all = realloc(all,(lmax=lcnt+30)*sizeof(LigList *));
1887 for ( ll=sc->ligofme, k=0; ll!=NULL; ll=ll->next, ++k )
1888 all[k] = ll;
1889 for ( k=0; k<lcnt-1; ++k ) for ( j=k+1; j<lcnt; ++j )
1890 if ( all[k]->ccnt<all[j]->ccnt ) {
1891 ll = all[k];
1892 all[k] = all[j];
1893 all[j] = ll;
1894 }
1895 sc->ligofme = all[0];
1896 for ( k=0; k<lcnt-1; ++k )
1897 all[k]->next = all[k+1];
1898 all[k]->next = NULL;
1899 }
1900 }
1901 free( all );
1902 }
1903
1904 static void LigatureClosure(SplineFont *sf) {
1905 /* AFM files can only deal with 2 character ligatures */
1906 /* Look for any three character ligatures (like ffi) which can be made up */
1907 /* of a two character ligature and their final letter (like "ff" and "i")*/
1908 /* I'm not going to bother with 4 character ligatures which could be made */
1909 /* of 2+1+1 because that doesn't happen in latin that I know of, and arabic */
1910 /* really should be using a type2 font */
1911 int i;
1912 LigList *l, *l2, *l3;
1913 PST *lig;
1914 SplineChar *sc, *sublig;
1915
1916 for ( i=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL ) {
1917 for ( l=sc->ligofme; l!=NULL; l=l->next ) if ( l->lig->subtable->lookup->store_in_afm ) {
1918 if ( l->ccnt==3 ) {
1919 /* we've got a 3 character ligature */
1920 for ( l2=l->next; l2!=NULL; l2=l2->next ) {
1921 if ( l2->lig->subtable==l->lig->subtable &&
1922 l2->ccnt == 2 && l2->components->sc==l->components->sc ) {
1923 /* We've found a two character sub ligature */
1924 sublig = l2->lig->u.lig.lig;
1925 for ( l3=sublig->ligofme; l3!=NULL; l3=l3->next ) {
1926 if ( l3->ccnt==2 && l3->components->sc==l->components->next->sc &&
1927 l3->lig->subtable->lookup->store_in_afm)
1928 break;
1929 }
1930 if ( l3!=NULL ) /* The ligature we want to add already exists */
1931 break;
1932 lig = chunkalloc(sizeof(PST));
1933 *lig = *l->lig;
1934 lig->temporary = true;
1935 lig->next = NULL;
1936 lig->u.lig.components = malloc(strlen(sublig->name)+
1937 strlen(l->components->next->sc->name)+
1938 2);
1939 sprintf(lig->u.lig.components,"%s %s",sublig->name,
1940 l->components->next->sc->name);
1941 l3 = malloc(sizeof(LigList));
1942 l3->lig = lig;
1943 l3->next = sublig->ligofme;
1944 l3->first = sublig;
1945 l3->ccnt = 2;
1946 sublig->ligofme = l3;
1947 l3->components = chunkalloc(sizeof(struct splinecharlist));
1948 l3->components->sc = l->components->next->sc;
1949 break;
1950 }
1951 }
1952 }
1953 }
1954 }
1955 }
1956
1957 void SFKernCleanup(SplineFont *sf,int isv) {
1958 int i;
1959 KernPair *kp, *p, *n;
1960 OTLookup *otl, *otlp, *otln;
1961
1962 if (sf->internal_temp)
1963 return;
1964
1965 if ( (!isv && sf->kerns==NULL) || (isv && sf->vkerns==NULL) ) /* can't have gotten messed up */
1966 return;
1967 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL ) {
1968 for ( kp = isv ? sf->glyphs[i]->vkerns : sf->glyphs[i]->kerns, p=NULL; kp!=NULL; kp = n ) {
1969 n = kp->next;
1970 if ( kp->kcid!=0 ) {
1971 if ( p!=NULL )
1972 p->next = n;
1973 else if ( isv )
1974 sf->glyphs[i]->vkerns = n;
1975 else
1976 sf->glyphs[i]->kerns = n;
1977 chunkfree(kp,sizeof(*kp));
1978 } else
1979 p = kp;
1980 }
1981 }
1982 for ( otl=sf->gpos_lookups, otlp = NULL; otl!=NULL; otl = otln ) {
1983 otln = otl->next;
1984 if ( otl->temporary_kern ) {
1985 if ( otlp!=NULL )
1986 otlp->next = otln;
1987 else
1988 sf->gpos_lookups = otln;
1989 OTLookupFree(otl);
1990 } else
1991 otlp = otl;
1992 }
1993 }
1994
1995 static void KCSfree(SplineChar ***scs,int cnt) {
1996 int i;
1997 for ( i=1; i<cnt; ++i )
1998 free( scs[i]);
1999 free(scs);
2000 }
2001
2002 static SplineChar ***KernClassToSC(SplineFont *sf, char **classnames, int cnt) {
2003 SplineChar ***scs, *sc;
2004 int i,j;
2005 char *pt, *end, ch;
2006
2007 scs = malloc(cnt*sizeof(SplineChar **));
2008 for ( i=1; i<cnt; ++i ) {
2009 for ( pt=classnames[i]-1, j=0; pt!=NULL; pt=strchr(pt+1,' ') )
2010 ++j;
2011 scs[i] = malloc((j+1)*sizeof(SplineChar *));
2012 for ( pt=classnames[i], j=0; *pt!='\0'; pt=end+1 ) {
2013 end = strchr(pt,' ');
2014 if ( end==NULL )
2015 end = pt+strlen(pt);
2016 ch = *end;
2017 *end = '\0';
2018 sc = SFGetChar(sf,-1,pt);
2019 if ( sc!=NULL )
2020 scs[i][j++] = sc;
2021 if ( ch=='\0' )
2022 break;
2023 *end = ch;
2024 }
2025 scs[i][j] = NULL;
2026 }
2027 return( scs );
2028 }
2029
2030 static void AddTempKP(SplineChar *first,SplineChar *second,
2031 int16_t offset, struct lookup_subtable *sub,uint16_t kcid,int isv) {
2032 KernPair *kp;
2033
2034 for ( kp=first->kerns; kp!=NULL; kp=kp->next )
2035 if ( kp->sc == second )
2036 break;
2037 if ( kp==NULL ) {
2038 kp = chunkalloc(sizeof(KernPair));
2039 kp->sc = second;
2040 kp->off = offset;
2041 kp->subtable = sub;
2042 kp->kcid = kcid;
2043 if ( isv ) {
2044 kp->next = first->vkerns;
2045 first->vkerns = kp;
2046 } else {
2047 kp->next = first->kerns;
2048 first->kerns = kp;
2049 }
2050 }
2051 }
2052
2053 void SFKernClassTempDecompose(SplineFont *sf,int isv) {
2054 KernClass *kc, *head= isv ? sf->vkerns : sf->kerns;
2055 KernPair *kp;
2056 SplineChar ***first, ***last;
2057 int i, j, k, l;
2058 OTLookup *otl;
2059
2060 /* Make sure the temporary field is cleaned up. Otherwise we may lose kerning data */
2061 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL ) {
2062 for ( kp = isv ? sf->glyphs[i]->vkerns : sf->glyphs[i]->kerns; kp!=NULL; kp = kp->next ) {
2063 kp->kcid = false;
2064 }
2065 }
2066 for ( kc = head, i=0; kc!=NULL; kc = kc->next )
2067 kc->kcid = ++i;
2068 for ( kc = head; kc!=NULL; kc = kc->next ) {
2069
2070 otl = chunkalloc(sizeof(OTLookup));
2071 otl->next = sf->gpos_lookups;
2072 sf->gpos_lookups = otl;
2073 otl->lookup_type = gpos_pair;
2074 otl->lookup_flags = kc->subtable->lookup->lookup_flags;
2075 otl->features = FeatureListCopy(kc->subtable->lookup->features);
2076 otl->lookup_name = copy(_("<Temporary kerning>"));
2077 otl->temporary_kern = otl->store_in_afm = true;
2078 otl->subtables = chunkalloc(sizeof(struct lookup_subtable));
2079 otl->subtables->lookup = otl;
2080 otl->subtables->per_glyph_pst_or_kern = true;
2081 otl->subtables->subtable_name = copy(_("<Temporary kerning>"));
2082
2083 first = KernClassToSC(sf,kc->firsts,kc->first_cnt);
2084 last = KernClassToSC(sf,kc->seconds,kc->second_cnt);
2085 for ( i=1; i<kc->first_cnt; ++i ) for ( j=1; j<kc->second_cnt; ++j ) {
2086 if ( kc->offsets[i*kc->second_cnt+j]!=0 ) {
2087 for ( k=0; first[i][k]!=NULL; ++k )
2088 for ( l=0; last[j][l]!=NULL; ++l )
2089 AddTempKP(first[i][k],last[j][l],
2090 kc->offsets[i*kc->second_cnt+j],
2091 otl->subtables,kc->kcid,isv);
2092 }
2093 }
2094 KCSfree(first,kc->first_cnt);
2095 KCSfree(last,kc->second_cnt);
2096 }
2097 }
2098
2099 /* ************************************************************************** */
2100 /* **************************** Writing PFM files *************************** */
2101 /* ************************************************************************** */
2102 static int getlshort(FILE *pfm) {
2103 int ch1;
2104 ch1 = getc(pfm);
2105 return( (getc(pfm)<<8)|ch1 );
2106 }
2107
2108 static int getlint(FILE *pfm) {
2109 int ch1, ch2, ch3;
2110 ch1 = getc(pfm);
2111 ch2 = getc(pfm);
2112 ch3 = getc(pfm);
2113 return( (getc(pfm)<<24)|(ch3<<16)|(ch2<<8)|ch1 );
2114 }
2115
2116 static void putlshort(short val,FILE *pfm) {
2117 putc(val&0xff,pfm);
2118 putc(val>>8,pfm);
2119 }
2120
2121 static void putlint(int val,FILE *pfm) {
2122 putc(val&0xff,pfm);
2123 putc((val>>8)&0xff,pfm);
2124 putc((val>>16)&0xff,pfm);
2125 putc((val>>24)&0xff,pfm);
2126 }
2127
2128 static const unsigned short local_unicode_from_win[] = {
2129 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
2130 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
2131 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
2132 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
2133 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
2134 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
2135 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
2136 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
2137 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
2138 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
2139 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
2140 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
2141 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
2142 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
2143 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
2144 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f,
2145 0x20ac, 0x0081, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
2146 0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008d, 0x008e, 0x008f,
2147 0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
2148 0x02dc, 0x2122, 0x0161, 0x203a, 0x0153, 0x009d, 0x009e, 0x0178,
2149 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
2150 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
2151 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
2152 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
2153 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
2154 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
2155 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
2156 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
2157 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
2158 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
2159 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
2160 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff
2161 };
2162
2163 static int inwin(SplineFont *sf, int winmap[256]) {
2164 int i, j;
2165 int cnt;
2166
2167 memset(winmap,-1,sizeof(int[256]));
2168 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL ) {
2169 int unienc = sf->glyphs[i]->unicodeenc;
2170 if ( unienc==-1 || unienc>0x3000 )
2171 continue;
2172 for ( j=255; j>=0; --j ) {
2173 if ( local_unicode_from_win[j]==unienc ) {
2174 winmap[j] = i;
2175 break;
2176 }
2177 }
2178 }
2179 for ( i=0x80, cnt=0; i<0x100; ++i )
2180 if ( winmap[i]!=-1 )
2181 ++cnt;
2182 return( cnt>64 );
2183 }
2184
2185 static int revwinmap(int winmap[256], int gid) {
2186 int i;
2187 for ( i=255; i>=0; --i )
2188 if ( winmap[i]==gid )
2189 break;
2190 return( i );
2191 }
2192
2193 int PfmSplineFont(FILE *pfm, SplineFont *sf, EncMap *map,int layer) {
2194 int caph=0, xh=0, ash=0, dsh=0, cnt=0, first=-1, samewid=-1, maxwid= -1, last=0, wid=0, ymax=0, ymin=0;
2195 int kerncnt=0, spacepos=0x20;
2196 int i, ii;
2197 const char *pt;
2198 KernPair *kp;
2199 int winmap[256];
2200 /* my docs imply that pfm files can only handle 1byte fonts */
2201 /* I think they must be output as if the font were encoded in winansi */
2202 /* Ah, but Adobe's technical note 5178.PFM.PDF gives the two byte format */
2203 long size, devname, facename, extmetrics, exttable, driverinfo, kernpairs, pos;
2204 DBounds b;
2205 int style;
2206 int windows_encoding;
2207
2208 if ( map->enc->is_japanese ||
2209 (sf->cidmaster!=NULL && strnmatch(sf->cidmaster->ordering,"Japan",5)==0 ))
2210 windows_encoding = 128;
2211 else if ( map->enc->is_korean ||
2212 (sf->cidmaster!=NULL && strnmatch(sf->cidmaster->ordering,"Korea",5)==0 ))
2213 windows_encoding = 129;
2214 else if ( map->enc->is_tradchinese ||
2215 (sf->cidmaster!=NULL && strnmatch(sf->cidmaster->ordering,"CNS",3)==0 ))
2216 windows_encoding = 136;
2217 else if ( map->enc->is_simplechinese ||
2218 (sf->cidmaster!=NULL && strnmatch(sf->cidmaster->ordering,"GB",2)==0 ))
2219 windows_encoding = 134;
2220 else if ( map->enc->is_custom )
2221 windows_encoding = 2;
2222 else
2223 windows_encoding = strmatch(map->enc->enc_name,"symbol")==0?2:0;
2224 if ( windows_encoding==0 )
2225 if ( !inwin(sf,winmap))
2226 windows_encoding = 0xff;
2227 if ( windows_encoding!=0 ) {
2228 memset(winmap,-1,sizeof(winmap));
2229 for ( i=0; i<sf->glyphcnt; ++i )
2230 if ( (ii=map->backmap[i])!=-1 && ii<256 )
2231 winmap[ii] = i;
2232 }
2233
2234 SFKernClassTempDecompose(sf,false);
2235 for ( ii=0; ii<256; ++ii ) if ( (i=winmap[ii])!=-1 ) {
2236 if ( sf->glyphs[i]!=NULL && sf->glyphs[i]->unicodeenc==' ' )
2237 spacepos = ii;
2238 if ( SCWorthOutputting(sf->glyphs[i]) ) {
2239 ++cnt;
2240 if ( sf->glyphs[i]->unicodeenc=='I' || sf->glyphs[i]->unicodeenc=='x' ||
2241 sf->glyphs[i]->unicodeenc=='H' || sf->glyphs[i]->unicodeenc=='d' ||
2242 sf->glyphs[i]->unicodeenc=='p' || sf->glyphs[i]->unicodeenc=='l' ) {
2243 SplineCharLayerFindBounds(sf->glyphs[i],layer,&b);
2244 if ( ymax<b.maxy ) ymax = b.maxy;
2245 if ( ymin>b.miny ) ymin = b.miny;
2246 if ( sf->glyphs[i]->unicodeenc=='I' || sf->glyphs[i]->unicodeenc=='H' )
2247 caph = b.maxy;
2248 else if ( sf->glyphs[i]->unicodeenc=='x' )
2249 xh = b.maxy;
2250 else if ( sf->glyphs[i]->unicodeenc=='d' || sf->glyphs[i]->unicodeenc=='l' )
2251 ash = b.maxy;
2252 else
2253 dsh = b.miny;
2254 }
2255 if ( samewid==-1 )
2256 samewid = sf->glyphs[i]->width;
2257 else if ( samewid!=sf->glyphs[i]->width )
2258 samewid = -2;
2259 if ( maxwid<sf->glyphs[i]->width )
2260 maxwid = sf->glyphs[i]->width;
2261 if ( first==-1 )
2262 first = ii;
2263 wid += sf->glyphs[i]->width;
2264 last = ii;
2265 for ( kp=sf->glyphs[i]->kerns; kp!=NULL; kp = kp->next )
2266 if ( revwinmap(winmap,kp->sc->orig_pos)!=-1 )
2267 ++kerncnt;
2268 }
2269 }
2270 if ( spacepos<first ) first = spacepos;
2271 if ( first==-1 ) first = 0;
2272 if ( spacepos>last ) last = spacepos;
2273 if ( cnt!=0 ) wid /= cnt;
2274 if ( kerncnt>=512 ) kerncnt = 512;
2275
2276 SFDefaultOS2Info(&sf->pfminfo,sf,sf->fontname);
2277
2278 putlshort(0x100,pfm); /* format version number */
2279 size = ftell(pfm);
2280 putlint(-1,pfm); /* file size, fill in later */
2281 i=0;
2282 if ( sf->copyright!=NULL ) for ( pt=sf->copyright; *pt && i<60; ++i, ++pt )
2283 putc(*pt,pfm);
2284 for ( ; i<60; ++i )
2285 putc(' ',pfm);
2286 putlshort(0x81,pfm); /* type flags */
2287 putlshort(10,pfm); /* point size, not really meaningful */
2288 putlshort(300,pfm); /* vert resolution */
2289 putlshort(300,pfm); /* hor resolution */
2290 putlshort(ymax,pfm); /* ascent (Adobe says the "ascent" is the max high in the font's bounding box) */
2291 if ( caph==0 ) caph = ash;
2292 if ( ymax-ymin>=sf->ascent+sf->descent )
2293 putlshort(0,pfm); /* Adobe says so */
2294 else
2295 putlshort(sf->ascent+sf->descent-(ymax-ymin),pfm); /* Internal leading */
2296 putlshort(196*(sf->ascent+sf->descent)/1000,pfm); /* External leading, Adobe says 196 */
2297 style = MacStyleCode(sf,NULL);
2298 putc(style&sf_italic?1:0,pfm); /* is italic */
2299 putc(0,pfm); /* underline */
2300 putc(0,pfm); /* strikeout */
2301 putlshort(sf->pfminfo.weight,pfm); /* weight */
2302 putc(windows_encoding,pfm); /* Some indication of the encoding */
2303 putlshort(/*samewid<0?sf->ascent+sf->descent:samewid*/0,pfm); /* width */
2304 putlshort(sf->ascent+sf->descent,pfm); /* height */
2305 putc(sf->pfminfo.pfmfamily,pfm); /* family */
2306 putlshort(wid,pfm); /* average width, Docs say "Width of "X", but that's wrong */
2307 putlshort(maxwid,pfm); /* max width */
2308
2309 if ( first>255 ) first=last = 0;
2310 else if ( last>255 ) { first=32; last = 255;}/* Yes, even for 2 byte fonts we do this (Adobe says so)*/
2311 putc(first,pfm); /* first char */
2312 putc(last,pfm);
2313 if ( spacepos>=first && spacepos<=last ) {
2314 putc(spacepos-first,pfm); /* default char */
2315 putc(spacepos-first,pfm); /* wordbreak */
2316 } else {
2317 putc(0,pfm); /* default character. I set to space */
2318 putc(0,pfm); /* word break character. I set to space */
2319 }
2320 putlshort(0,pfm); /* width bytes. Not meaningful for ps */
2321
2322 devname = ftell(pfm);
2323 putlint(-1,pfm); /* offset to device name, fill in later */
2324 facename = ftell(pfm);
2325 putlint(-1,pfm); /* offset to face name, fill in later */
2326 putlint(0,pfm); /* bits pointer. not used */
2327 putlint(0,pfm); /* bits offset. not used */
2328
2329 /* No width table */
2330
2331 /* extensions */
2332 putlshort(0x1e,pfm); /* size of extensions table */
2333 extmetrics = ftell(pfm);
2334 putlint(-1,pfm); /* extent metrics. fill in later */
2335 exttable = ftell(pfm);
2336 putlint(-1,pfm); /* extent table. fill in later */
2337 putlint(0,pfm); /* origin table. not used */
2338 kernpairs = ftell(pfm);
2339 putlint(kerncnt==0?0:-1,pfm); /* kern pairs. fill in later */
2340 putlint(0,pfm); /* kern track. I don't understand it so I'm leaving it out */
2341 driverinfo = ftell(pfm);
2342 putlint(-1,pfm); /* driver info. fill in later */
2343 putlint(0,pfm); /* reserved. mbz */
2344
2345 /* devicename */
2346 pos = ftell(pfm);
2347 fseek(pfm,devname,SEEK_SET);
2348 putlint(pos,pfm);
2349 fseek(pfm,pos,SEEK_SET);
2350 for ( pt="PostScript"/*"\273PostScript\253"*/; *pt; ++pt )
2351 putc(*pt,pfm);
2352 putc('\0',pfm);
2353
2354 /* facename */
2355 pos = ftell(pfm);
2356 fseek(pfm,facename,SEEK_SET);
2357 putlint(pos,pfm);
2358 fseek(pfm,pos,SEEK_SET);
2359 if ( sf->familyname!=NULL ) {
2360 for ( pt=sf->familyname; *pt; ++pt )
2361 putc(*pt,pfm);
2362 } else {
2363 for ( pt=sf->fontname; *pt; ++pt )
2364 putc(*pt,pfm);
2365 }
2366 putc('\0',pfm);
2367
2368 /* extmetrics */
2369 pos = ftell(pfm);
2370 fseek(pfm,extmetrics,SEEK_SET);
2371 putlint(pos,pfm);
2372 fseek(pfm,pos,SEEK_SET);
2373 putlshort(0x34,pfm); /* size */
2374 putlshort(240,pfm); /* 12 point in twentieths of a point */
2375 putlshort(0,pfm); /* any orientation */
2376 putlshort(sf->ascent+sf->descent,pfm); /* master height */
2377 putlshort(3,pfm); /* min scale */
2378 putlshort(1000,pfm); /* max scale */
2379 putlshort(sf->ascent+sf->descent,pfm); /* master units */
2380 putlshort(caph,pfm); /* cap height */
2381 putlshort(xh,pfm); /* x height */
2382 putlshort(ash,pfm); /* lower case ascent height */
2383 putlshort(-dsh,pfm); /* lower case descent height */
2384 putlshort((int) (10*sf->italicangle),pfm); /* italic angle */
2385 putlshort(-xh,pfm); /* super script */
2386 putlshort(xh/2,pfm); /* sub script */
2387 putlshort(2*(sf->ascent+sf->descent)/3,pfm); /* super size */
2388 putlshort(2*(sf->ascent+sf->descent)/3,pfm); /* sub size */
2389 putlshort(-sf->upos,pfm); /* underline pos */
2390 putlshort(sf->uwidth,pfm); /* underline width */
2391 putlshort(-sf->upos,pfm); /* real underline pos */
2392 putlshort(-sf->upos+2*sf->uwidth,pfm); /* real underline second line pos */
2393 putlshort(sf->uwidth,pfm); /* underline width */
2394 putlshort(sf->uwidth,pfm); /* underline width */
2395 putlshort((xh+sf->uwidth)/2,pfm); /* strike out top */
2396 putlshort(sf->uwidth,pfm); /* strike out width */
2397 putlshort(kerncnt,pfm); /* number of kerning pairs <= 512 */
2398 putlshort(0,pfm); /* kerning tracks <= 16 */
2399
2400 /* extent table */
2401 pos = ftell(pfm);
2402 fseek(pfm,exttable,SEEK_SET);
2403 putlint(pos,pfm);
2404 fseek(pfm,pos,SEEK_SET);
2405 for ( ii=first; ii<=last; ++ii ) {
2406 if ( (i=winmap[ii])==-1 || sf->glyphs[i]==NULL )
2407 putlshort(0,pfm);
2408 else
2409 putlshort(sf->glyphs[i]->width,pfm);
2410 }
2411
2412 /* driver info */ /* == ps font name */
2413 pos = ftell(pfm);
2414 fseek(pfm,driverinfo,SEEK_SET);
2415 putlint(pos,pfm);
2416 fseek(pfm,pos,SEEK_SET);
2417 for ( pt=sf->fontname; *pt; ++pt )
2418 putc(*pt,pfm);
2419 putc('\0',pfm);
2420
2421 /* kern pairs */
2422 if ( kerncnt!=0 ) {
2423 pos = ftell(pfm);
2424 fseek(pfm,kernpairs,SEEK_SET);
2425 putlint(pos,pfm);
2426 fseek(pfm,pos,SEEK_SET);
2427 putlshort(kerncnt,pfm); /* number of kerning pairs <= 512 */
2428 kerncnt = 0;
2429 for ( ii=first; ii<last; ++ii ) if ( (i=winmap[ii])!=-1 && sf->glyphs[i]!=NULL ) {
2430 if ( SCWorthOutputting(sf->glyphs[i]) ) {
2431 for ( kp=sf->glyphs[i]->kerns; kp!=NULL; kp = kp->next )
2432 if ( kerncnt<512 && revwinmap(winmap,kp->sc->orig_pos)!=-1 ) {
2433 putc(ii,pfm);
2434 putc(revwinmap(winmap,kp->sc->orig_pos),pfm);
2435 putlshort(kp->off,pfm);
2436 ++kerncnt;
2437 }
2438 }
2439 }
2440 }
2441
2442 /* kern track */
2443 /* I'm ignoring this */
2444
2445 /* file size */
2446 pos = ftell(pfm);
2447 fseek(pfm,size,SEEK_SET);
2448 putlint(pos,pfm);
2449
2450 SFKernCleanup(sf,false);
2451
2452 #ifdef __CygWin
2453 /* Modern versions of windows want the execute bit set on a pfm file */
2454 /* I've no idea what this corresponds to in windows, nor any idea on */
2455 /* how to set it from the windows UI, but this seems to work */
2456 {
2457 struct stat buf;
2458 fstat(fileno(pfm),&buf);
2459 fchmod(fileno(pfm),S_IXUSR | buf.st_mode );
2460 }
2461 #endif
2462
2463 return( !ferror(pfm));
2464 }
2465
2466 /* ************************************************************************** */
2467 /* **************************** Reading PFM files *************************** */
2468 /* ************************************************************************** */
2469 int LoadKerningDataFromPfm(SplineFont *sf, char *filename,EncMap *map) {
2470 FILE *file = fopen(filename,"rb");
2471 int widthbytes, kernoff, i, kerncnt;
2472 int ch1, ch2, offset;
2473 int winmap[256];
2474 int encoding;
2475
2476 if ( file==NULL )
2477 return( 0 );
2478 if ( getlshort(file)!=0x100 ) {
2479 fclose(file);
2480 return( false );
2481 }
2482 /* filesize = */ getlint(file);
2483 for ( i=0; i<60; ++i ) getc(file); /* Skip the copyright */
2484 /* flags = */ getlshort(file);
2485 /* ptsize = */ getlshort(file);
2486 /* vertres = */ getlshort(file);
2487 /* horres = */ getlshort(file);
2488 /* ascent = */ getlshort(file);
2489 /* int leading = */ getlshort(file);
2490 /* ext leading = */ getlshort(file);
2491 /* italic = */ getc(file);
2492 /* underline = */ getc(file);
2493 /* strikeout = */ getc(file);
2494 /* weight = */ getlshort(file);
2495 encoding = getc(file);
2496 /* width = */ getlshort(file);
2497 /* height = */ getlshort(file);
2498 /* family = */ getc(file);
2499 /* avgwid = */ getlshort(file);
2500 /* maxwid = */ getlshort(file);
2501 /* first = */ getc(file);
2502 /* last = */ getc(file);
2503 /* space = */ getc(file);
2504 /* word break = */ getc(file);
2505 widthbytes = getlshort(file);
2506 /* off to devname */getlint(file);
2507 /* off to facename */getlint(file);
2508 /* bitspointer */ getlint(file);
2509 /* bitsoffset */ getlint(file);
2510
2511 for ( i=0; i<widthbytes; ++i ) /* Ignore the width table */
2512 getc(file);
2513 if ( getlshort(file)<0x12 )
2514 kernoff = 0; /* Extensions table is too short to hold a kern pointer */
2515 else {
2516 /* extmetrics = */ getlint(file);
2517 /* exttable = */ getlint(file);
2518 /* origin table = */ getlint(file);
2519 kernoff = getlint(file);
2520 }
2521 if ( kernoff!=0 && !feof(file) ) {
2522 fseek(file,kernoff,SEEK_SET);
2523 if ( encoding==0 )
2524 inwin(sf,winmap);
2525 else {
2526 for ( i=0; i<256 && i<map->enccount; ++i )
2527 winmap[i] = map->map[i];
2528 for ( i=0; i<256; ++i )
2529 winmap[i] = -1;
2530 }
2531 kerncnt = getlshort(file);
2532 /* Docs say at most 512 kerning pairs. Not worth posting a progress */
2533 /* dlg */
2534 for ( i=0; i<kerncnt; ++i ) {
2535 ch1 = getc(file);
2536 ch2 = getc(file);
2537 offset = (short) getlshort(file);
2538 if ( !feof(file) && winmap[ch1]!=-1 && winmap[ch2]!=-1 )
2539 KPInsert(sf->glyphs[winmap[ch1]],sf->glyphs[winmap[ch2]],offset,false);
2540 /* No support for vertical kerning that I'm aware of */
2541 }
2542 }
2543 fclose(file);
2544 return( true );
2545 }
2546
2547 /* ************************************************************************** */
2548 /* **************************** Writing TFM files *************************** */
2549 /* ************************************************************************** */
2550 typedef uint32_t fix_word;
2551
2552 struct tfm_header {
2553 uint32_t checksum; /* don't know how to calculate this, use 0 to ignore it */
2554 fix_word design_size; /* in points (10<<20 seems to be default) */
2555 char encoding[40]; /* first byte is length, rest are a string that names the encoding */
2556 /* ASCII, TeX text, TeX math extension, XEROX, GRAPHIC, UNSPECIFIED */
2557 char family[20]; /* Font Family, preceded by a length byte */
2558 uint8_t seven_bit_safe_flag;
2559 uint8_t ignored[2];
2560 uint8_t face; /* 0=>roman, 1=>italic */
2561 /* 4=>light, 0=>medium, 2=>bold */
2562 /* 6=>condensed, 0=>regular, 12=>extended */
2563 };
2564
2565 struct tfm_params {
2566 fix_word slant; /* -sin(italic_angle) (a small positive number) */
2567 fix_word space; /* inter-word spacing (advance width of space) */
2568 fix_word space_stretch; /* inter-word glue stretching */
2569 /* About 1/2 space for cmr */
2570 fix_word space_shrink; /* inter-word glue shrinking */
2571 /* About 1/3 space for cmr */
2572 fix_word x_height;
2573 fix_word quad; /* == 1.0 */
2574 fix_word extra_space; /* added at end of sentences */
2575 /* same as space_shrink for cmr */
2576 /* tex math and tex math extension have extra parameters. They are not */
2577 /* explained (page 7) */
2578 };
2579 /* tfm files use uint8s, ofm files use uint16s */
2580 struct ligkern {
2581 uint16_t skip;
2582 uint16_t other_char;
2583 uint16_t op;
2584 uint16_t remainder;
2585 struct ligkern *next;
2586 };
2587 struct extension {
2588 uint16_t extens[4]; /* top, mid, bottom & repeat */
2589 };
2590
2591 static struct ligkern *TfmAddKern(KernPair *kp,struct ligkern *last,double *kerns,
2592 int *_kcnt, EncMap *map,int maxc) {
2593 struct ligkern *new = calloc(1,sizeof(struct ligkern));
2594 int i;
2595
2596 new->other_char = map->backmap[kp->sc->orig_pos];
2597 for ( i=*_kcnt-1; i>=0 ; --i )
2598 if ( kerns[i] == kp->off )
2599 break;
2600 if ( i<0 ) {
2601 i = (*_kcnt)++;
2602 kerns[i] = kp->off;
2603 }
2604 if ( maxc==256 ) {
2605 new->remainder = i&0xff;
2606 new->op = 128 + (i>>8);
2607 } else {
2608 new->remainder = i&0xffff;
2609 new->op = 128 + (i>>16);
2610 }
2611 new->next = last;
2612 return( new );
2613 }
2614
2615 static struct ligkern *TfmAddLiga(LigList *l,struct ligkern *last,EncMap *map,
2616 int maxc) {
2617 struct ligkern *new;
2618
2619 if ( !l->lig->subtable->lookup->store_in_afm )
2620 return( last );
2621 if ( map->backmap[l->lig->u.lig.lig->orig_pos]>=maxc )
2622 return( last );
2623 if ( l->components==NULL || map->backmap[l->components->sc->orig_pos]>=maxc ||
2624 l->components->next!=NULL )
2625 return( last );
2626 new = calloc(1,sizeof(struct ligkern));
2627 new->other_char = map->backmap[l->components->sc->orig_pos];
2628 new->remainder = map->backmap[l->lig->u.lig.lig->orig_pos];
2629 new->next = last;
2630 new->op = 0*4 + 0*2 + 0;
2631 /* delete next char, delete current char, start over and check the resultant ligature for more ligs */
2632 return( new );
2633 }
2634
2635 static void FindCharlists(SplineFont *sf,int *charlistindex,EncMap *map,int maxc) {
2636 int i, last, ch;
2637 char *pt, *end, *variants;
2638
2639 memset(charlistindex,-1,(maxc+1)*sizeof(int));
2640 for ( i=0; i<maxc && i<map->enccount; ++i ) if ( map->map[i]!=-1 && SCWorthOutputting(sf->glyphs[map->map[i]])) {
2641 SplineChar *sc = sf->glyphs[map->map[i]];
2642
2643 variants = NULL;
2644 if ( sc->vert_variants!=NULL && sc->vert_variants->variants!=NULL )
2645 variants = sc->vert_variants->variants;
2646 else if ( sc->horiz_variants!=NULL && sc->horiz_variants->variants!=NULL )
2647 variants = sc->horiz_variants->variants;
2648 if ( variants!=NULL ) {
2649 last = i;
2650 for ( pt=variants; *pt; pt = end ) {
2651 end = strchr(pt,' ');
2652 if ( end==NULL )
2653 end = pt+strlen(pt);
2654 ch = *end;
2655 *end = '\0';
2656 sc = SFGetChar(sf,-1,pt);
2657 *end = ch;
2658 while ( *end==' ' ) ++end;
2659 if ( sc!=NULL && map->backmap[sc->orig_pos]<maxc ) {
2660 charlistindex[last] = map->backmap[sc->orig_pos];
2661 last = map->backmap[sc->orig_pos];
2662 }
2663 }
2664 }
2665 }
2666 }
2667
2668 static int FindExtensions(SplineFont *sf,struct extension *extensions,int *extenindex,
2669 EncMap *map,int maxc) {
2670 int i;
2671 int j,k;
2672 char *foundnames[4];
2673 int16_t founds[4]; int ecnt=0;
2674
2675 memset(extenindex,-1,(maxc+1)*sizeof(int));
2676 for ( i=0; i<maxc && i<map->enccount; ++i ) if ( map->map[i]!=-1 && SCWorthOutputting(sf->glyphs[map->map[i]])) {
2677 SplineChar *sc = sf->glyphs[map->map[i]];
2678 struct glyphvariants *gv;
2679
2680 gv=NULL;
2681 if ( sc->vert_variants!=NULL && sc->vert_variants->part_cnt>0 )
2682 gv = sc->vert_variants;
2683 else if ( sc->horiz_variants!=NULL && sc->horiz_variants->part_cnt>0 )
2684 gv = sc->horiz_variants;
2685 if ( gv!=NULL ) {
2686 foundnames[0] = foundnames[1] = foundnames[2] = foundnames[3] = NULL;
2687 for ( j=k=0; j<gv->part_cnt; ++j ) {
2688 if ( !gv->parts[j].is_extender ) {
2689 if ( k>=3 ) {
2690 k=4;
2691 break;
2692 } else if ( k==1 && j==gv->part_cnt-1 )
2693 foundnames[2] = gv->parts[j].component;
2694 else
2695 foundnames[k++] = gv->parts[j].component;
2696 } else {
2697 if ( foundnames[3]==NULL || strcmp(foundnames[3],gv->parts[j].component)==0 ) {
2698 foundnames[3] = gv->parts[j].component;
2699 if ( k==0 )
2700 k=1; /* Can't be first */
2701 } else {
2702 k = 4;
2703 break;
2704 }
2705 }
2706 }
2707 founds[0] = founds[1] = founds[2] = founds[3] = 0;
2708 for ( j=0; j<4; ++j ) if ( foundnames[j]!=NULL ) {
2709 sc = SFGetChar(sf,-1,foundnames[j]);
2710 if ( sc==NULL )
2711 k=4;
2712 else if ( map->backmap[sc->orig_pos]<maxc &&
2713 map->backmap[sc->orig_pos]!=-1 )
2714 founds[j] = map->backmap[sc->orig_pos];
2715 else
2716 k=4;
2717 }
2718 if ( k!=4 ) {
2719 extenindex[i] = ecnt;
2720 memcpy(extensions[ecnt].extens,founds,sizeof(founds));
2721 ++ecnt;
2722 }
2723 }
2724 }
2725 return( ecnt );
2726 }
2727
2728 static int CoalesceValues(double *values,int max,int *index,int maxc) {
2729 int i,j,k,top, offpos,diff,eoz;
2730 int _backindex[257];
2731 double off, test;
2732 double _topvalues[257], _totvalues[257], *topvalues, *totvalues;
2733 int _cnt[257];
2734 int *backindex, *cnt;
2735
2736 if ( maxc<=256 ) {
2737 backindex = _backindex;
2738 topvalues = _topvalues;
2739 totvalues = _totvalues;
2740 cnt = _cnt;
2741 } else {
2742 backindex = malloc((maxc+1)*sizeof(int));
2743 topvalues = malloc((maxc+1)*sizeof(double));
2744 totvalues = malloc((maxc+1)*sizeof(double));
2745 cnt = malloc((maxc+1)*sizeof(int));
2746 }
2747
2748 values[maxc] = 0;
2749 for ( i=0; i<=maxc; ++i )
2750 backindex[i] = i;
2751
2752 /* Coalesce zeroes first. This speeds up the sort immensely */
2753 eoz = 0;
2754 for ( i=1; i<maxc; ++i ) {
2755 if ( values[i]==0 ) {
2756 int l = backindex[i];
2757 backindex[i] = backindex[eoz];
2758 values[i] = values[eoz];
2759 backindex[eoz] = l;
2760 values[eoz] = 0;
2761 eoz++;
2762 }
2763 }
2764 /* sort */
2765 for ( i=eoz; i<maxc; ++i ) for ( j=i+1; j<=maxc; ++j ) {
2766 if ( values[i]>values[j] ) {
2767 int l = backindex[i];
2768 double val = values[i];
2769 backindex[i] = backindex[j];
2770 values[i] = values[j];
2771 backindex[j] = l;
2772 values[j] = val;
2773 }
2774 }
2775 for ( i=0; i<=maxc; ++i )
2776 index[backindex[i]] = i;
2777 top = maxc+1;
2778 for ( i=0; i<top; ++i ) {
2779 for ( j=i+1; j<top && values[i]==values[j]; ++j );
2780 if ( j>i+1 ) {
2781 int diff = j-i-1;
2782 for ( k=i+1; k+diff<top; ++k )
2783 values[k] = values[k+diff];
2784 for ( k=0; k<=maxc; ++k ) {
2785 if ( index[k]>=j )
2786 index[k] -= diff;
2787 else if ( index[k]>i )
2788 index[k] = i;
2789 }
2790 top -= diff;
2791 }
2792 cnt[i] = j-i;
2793 }
2794 if ( top<=max ) {
2795 if ( values[0]!=0 ) {
2796 for ( i=0; i<top && values[i]!=0; ++i );
2797 if ( i==top )
2798 IError("zero must be present in tfm arrays");
2799 else {
2800 values[i] = values[0];
2801 values[0] = 0;
2802 for ( k=0; k<=maxc; ++k ) {
2803 if ( index[k]==0 ) index[k] = i;
2804 else if ( index[k]==i ) index[k] = 0;
2805 }
2806 }
2807 }
2808 if ( maxc>256 ) {
2809 free(backindex);
2810 free(topvalues);
2811 free(totvalues);
2812 free(cnt);
2813 }
2814 return( top );
2815 }
2816
2817 for ( i=0; i<top; ++i ) {
2818 topvalues[i] = values[i];
2819 totvalues[i] = cnt[i]*values[i];
2820 }
2821
2822 while ( top>max ) {
2823 off = fabs(topvalues[0]-values[1]);
2824 offpos = 0;
2825 for ( i=1; i<top-1; ++i ) {
2826 test = fabs(topvalues[i]-values[i+1]);
2827 if ( test<off ) {
2828 off = test;
2829 offpos = i;
2830 }
2831 }
2832 topvalues[offpos] = topvalues[offpos+1];
2833 cnt[offpos] += cnt[offpos+1];
2834 totvalues[offpos] += totvalues[offpos+1];
2835 diff = 1;
2836 for ( k=offpos+1; k+diff<top; ++k ) {
2837 values[k] = values[k+diff];
2838 topvalues[k] = topvalues[k+diff];
2839 cnt[k] = cnt[k+diff];
2840 totvalues[k] = totvalues[k+diff];
2841 }
2842 for ( k=0; k<=maxc; ++k ) {
2843 if ( index[k]>offpos )
2844 index[k] -= diff;
2845 }
2846 top -= diff;
2847 }
2848 values[0] = 0;
2849 for ( i=1; i<top; ++i )
2850 values[i] = totvalues[i]/cnt[i];
2851 if ( maxc>256 ) {
2852 free(backindex);
2853 free(topvalues);
2854 free(totvalues);
2855 free(cnt);
2856 }
2857 return( top );
2858 }
2859
2860 void TeXDefaultParams(SplineFont *sf) {
2861 int i, spacew;
2862 BlueData bd;
2863
2864 if ( sf->texdata.type!=tex_unset )
2865 return;
2866
2867 spacew = rint(.33*(1<<20)); /* 1/3 em for a space seems like a reasonable size */
2868 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL && sf->glyphs[i]->unicodeenc==' ' ) {
2869 spacew = rint((sf->glyphs[i]->width<<20)/(sf->ascent+sf->descent));
2870 break;
2871 }
2872 QuickBlues(sf,ly_fore,&bd);
2873
2874 memset(sf->texdata.params,0,sizeof(sf->texdata.params));
2875 sf->texdata.params[0] = rint( -sin(sf->italicangle)*(1<<20) ); /* slant */
2876 sf->texdata.params[1] = spacew; /* space */
2877 sf->texdata.params[2] = rint(spacew/2); /* stretch_space */
2878 sf->texdata.params[3] = rint(spacew/3); /* shrink space */
2879 if ( bd.xheight>0 )
2880 sf->texdata.params[4] = rint((((double) bd.xheight)*(1<<20))/(sf->ascent+sf->descent));
2881 sf->texdata.params[5] = 1<<20; /* quad */
2882 sf->texdata.params[6] = rint(spacew/3); /* extra space after sentence period */
2883
2884 /* Let's provide some vaguely reasonable defaults for math fonts */
2885 /* I'll ignore math ext fonts */
2886 /* hmm. TeX math fonts have space, stretch, shrink set to 0 */
2887 sf->texdata.params[7] = rint(.747*(1<<20));
2888 sf->texdata.params[8] = rint(.424*(1<<20));
2889 sf->texdata.params[9] = rint(.474*(1<<20));
2890 sf->texdata.params[10] = rint(.756*(1<<20));
2891 sf->texdata.params[11] = rint(.375*(1<<20));
2892 sf->texdata.params[12] = rint(.413*(1<<20));
2893 sf->texdata.params[13] = rint(.363*(1<<20));
2894 sf->texdata.params[14] = rint(.289*(1<<20));
2895 sf->texdata.params[15] = rint(.15*(1<<20));
2896 sf->texdata.params[16] = rint(.309*(1<<20));
2897 sf->texdata.params[17] = rint(.386*(1<<20));
2898 sf->texdata.params[18] = rint(.05*(1<<20));
2899 sf->texdata.params[19] = rint(2.39*(1<<20));
2900 sf->texdata.params[20] = rint(1.01*(1<<20));
2901 sf->texdata.params[21] = rint(.25*(1<<20));
2902 }
2903
2904 static int _OTfmSplineFont(FILE *tfm, SplineFont *sf,EncMap *map,int maxc,int layer) {
2905 struct tfm_header header;
2906 char *full=NULL;
2907 const char *encname;
2908 int i;
2909 DBounds b;
2910 struct ligkern *_ligkerns[256], **ligkerns, *lk, *lknext;
2911 double _widths[257], _heights[257], _depths[257], _italics[257];
2912 double *widths, *heights, *depths, *italics;
2913 uint8_t _tags[256], *tags;
2914 uint16_t _lkindex[256], *lkindex;
2915 int _former[256], *former;
2916 struct extension _extensions[257], *extensions;
2917 int _charlistindex[257], _extenindex[257];
2918 int *charlistindex, *extenindex;
2919 int _widthindex[257], _heightindex[257], _depthindex[257], _italicindex[257];
2920 int *widthindex, *heightindex, *depthindex, *italicindex;
2921 double *kerns;
2922 int widcnt, hcnt, dcnt, icnt, kcnt, lkcnt, pcnt, ecnt, sccnt;
2923 int first, last;
2924 KernPair *kp;
2925 LigList *l;
2926 int style, any;
2927 uint32_t *lkarray;
2928 struct ligkern *o_lkarray=NULL;
2929 char *familyname;
2930 int anyITLC;
2931 real scale = (1<<20)/(double) (sf->ascent+sf->descent);
2932 int toobig_warn = false;
2933
2934 if ( maxc==256 ) {
2935 ligkerns = _ligkerns;
2936 widths = _widths;
2937 heights = _heights;
2938 depths = _depths;
2939 italics = _italics;
2940 tags = _tags;
2941 lkindex = _lkindex;
2942 former = _former;
2943 charlistindex = _charlistindex;
2944 extensions = _extensions;
2945 extenindex = _extenindex;
2946 widthindex = _widthindex;
2947 heightindex = _heightindex;
2948 depthindex = _depthindex;
2949 italicindex = _italicindex;
2950 } else {
2951 ligkerns = malloc(maxc*sizeof(struct ligkern *));
2952 widths = malloc((maxc+1)*sizeof(double));
2953 heights = malloc((maxc+1)*sizeof(double));
2954 depths = malloc((maxc+1)*sizeof(double));
2955 italics = malloc((maxc+1)*sizeof(double));
2956 tags = malloc(maxc*sizeof(uint8_t));
2957 lkindex = malloc(maxc*sizeof(uint16_t));
2958 former = malloc(maxc*sizeof(int));
2959 charlistindex = malloc((maxc+1)*sizeof(int));
2960 extensions = malloc((maxc+1)*sizeof(struct extension));
2961 extenindex = malloc((maxc+1)*sizeof(int));
2962 widthindex = malloc((maxc+1)*sizeof(int));
2963 heightindex = malloc((maxc+1)*sizeof(int));
2964 depthindex = malloc((maxc+1)*sizeof(int));
2965 italicindex = malloc((maxc+1)*sizeof(int));
2966 }
2967 SFLigaturePrepare(sf);
2968 LigatureClosure(sf); /* Convert 3 character ligs to a set of two character ones when possible */
2969 SFKernClassTempDecompose(sf,false); /* Undoes kern classes */
2970 TeXDefaultParams(sf);
2971
2972 memset(&header,0,sizeof(header));
2973 header.checksum = 0; /* don't check checksum (I don't know how to calculate it) */
2974 header.design_size = ((sf->design_size<<19)+2)/5;
2975 if ( header.design_size==0 ) header.design_size = 10<<20;
2976 encname=NULL;
2977 /* These first two encoding names appear magic to txtopl */
2978 /* I tried checking that the encodings were correct, name by name, but */
2979 /* that doesn't work. People use non-standard names */
2980 if ( sf->texdata.type==tex_math ) encname = "TEX MATH SYMBOLS";
2981 else if ( sf->texdata.type==tex_mathext ) encname = "TEX MATH EXTENSION";
2982 else if ( sf->subfontcnt==0 && map->enc!=&custom )
2983 encname = EncodingName( map->enc );
2984 if ( encname==NULL ) {
2985 full = malloc(strlen(sf->fontname)+10);
2986 strcpy(full,sf->fontname);
2987 strcat(full,"-Enc");
2988 encname = full;
2989 }
2990 header.encoding[0] = strlen(encname);
2991 if ( header.encoding[0]>39 ) {
2992 header.encoding[0] = 39;
2993 memcpy(header.encoding+1,encname,39);
2994 } else
2995 strcpy(header.encoding+1,encname);
2996 if ( full ) free(full);
2997
2998 familyname = sf->cidmaster ? sf->cidmaster->familyname : sf->familyname;
2999 header.family[0] = strlen(familyname);
3000 if ( header.family[0]>=19 ) {
3001 header.family[0] = 19;
3002 memcpy(header.family+1,familyname,19);
3003 } else
3004 strcpy(header.family+1,familyname);
3005 for ( i=128; i<map->enccount && i<maxc; ++i )
3006 if ( map->map[i]!=-1 && SCWorthOutputting(sf->glyphs[map->map[i]]))
3007 break;
3008 if ( i>=map->enccount || i>=maxc )
3009 header.seven_bit_safe_flag = true;
3010 style = MacStyleCode(sf,NULL);
3011 if ( style&sf_italic )
3012 header.face = 1;
3013 if ( style&sf_bold )
3014 header.face+=2;
3015 else if ( strstrmatch(sf->fontname,"Ligh") ||
3016 strstrmatch(sf->fontname,"Thin") ||
3017 strstrmatch(sf->fontname,"Maigre") ||
3018 strstrmatch(sf->fontname,"Mager") )
3019 header.face += 4;
3020 if ( style&sf_condense )
3021 header.face += 6;
3022 else if ( style&sf_extend )
3023 header.face += 12;
3024
3025 pcnt = sf->texdata.type==tex_math ? 22 : sf->texdata.type==tex_mathext ? 13 : 7;
3026
3027 memset(widths,0,maxc*sizeof(double));
3028 memset(heights,0,maxc*sizeof(double));
3029 memset(depths,0,maxc*sizeof(double));
3030 memset(italics,0,maxc*sizeof(double));
3031 memset(tags,0,maxc*sizeof(uint8_t));
3032 first = last = -1;
3033 /* Note: Text fonts for TeX and math fonts use the italic correction & width */
3034 /* fields to mean different things */
3035 anyITLC = false;
3036 for ( i=0; i<maxc && i<map->enccount; ++i )
3037 if ( map->map[i]!=-1 && SCWorthOutputting(sf->glyphs[map->map[i]])) {
3038 if ( sf->glyphs[map->map[i]]->italic_correction!=TEX_UNDEF ) {
3039 anyITLC = true;
3040 break;
3041 }
3042 }
3043 for ( i=0; i<maxc && i<map->enccount; ++i ) {
3044 if ( map->map[i]!=-1 && SCWorthOutputting(sf->glyphs[map->map[i]])) {
3045 SplineChar *sc = sf->glyphs[map->map[i]];
3046 if ( sc->tex_height==TEX_UNDEF || sc->tex_depth==TEX_UNDEF ||
3047 sc->italic_correction==TEX_UNDEF ) {
3048 SplineCharLayerFindBounds(sc,layer,&b);
3049 heights[i] = b.maxy*scale; depths[i] = -b.miny*scale;
3050 }
3051 if ( sc->tex_height!=TEX_UNDEF )
3052 heights[i] = sc->tex_height*scale;
3053 if ( sc->tex_depth!=TEX_UNDEF )
3054 depths[i] = sc->tex_depth*scale;
3055 if ( depths[i]<0 ) depths[i] = 0; /* Werner says depth should never be negative. Something about how accents are positioned */
3056 if ( sc->width==0 )
3057 widths[i] = 1; /* TeX/Omega use a 0 width as a flag for non-existant character. Stupid. So zero width glyphs must be given the smallest possible non-zero width, to ensure they exists. GRRR. */
3058 else if ( sc->width*scale >= (16<<20) ) {
3059 LogError( _("The width of %s is too big to fit in a tfm fix_word, it shall be truncated to the largest size allowed."), sc->name );
3060 widths[i] = (16<<20)-1;
3061 } else
3062 widths[i] = sc->width*scale;
3063 if ( sc->italic_correction!=TEX_UNDEF )
3064 italics[i] = sc->italic_correction*scale;
3065 else if ( (style&sf_italic) && b.maxx>sc->width && !anyITLC )
3066 italics[i] = ((b.maxx-sc->width) +
3067 (sf->ascent+sf->descent)/16.0)*scale;
3068 /* With a 1/16 em white space after it */
3069 if ( widths[i]<0 ) widths[i] = 0;
3070 if ( first==-1 ) first = i;
3071 last = i;
3072 if ( widths[i]>=(16<<20) || heights[i]>=(16<<20) ||
3073 depths[i]>=(16<<20) || italics[i]>=(16<<20) ) {
3074 if ( !toobig_warn ) {
3075 ff_post_error(_("Value exceeds tfm limitations"),_("The width, height, depth or italic correction of %s is too big. Tfm files may not contain values bigger than 16 times the em-size of the font. Width=%g, height=%g, depth=%g, italic correction=%g"),
3076 sc->name, widths[i]/(1<<20), heights[i]/(1<<20), depths[i]/(1<<20), italics[i]/(1<<20) );
3077 toobig_warn = true;
3078 }
3079 if ( widths[i]>(16<<20)-1 )
3080 widths[i] = (16<<20)-1;
3081 if ( heights[i]>(16<<20)-1 )
3082 heights[i] = (16<<20)-1;
3083 if ( depths[i]>(16<<20)-1 )
3084 depths[i] = (16<<20)-1;
3085 if ( italics[i]>(16<<20)-1 )
3086 italics[i] = (16<<20)-1;
3087 }
3088 }
3089 }
3090 widcnt = CoalesceValues(widths,maxc,widthindex,maxc);
3091 hcnt = CoalesceValues(heights,maxc<=256?16:256,heightindex,maxc);
3092 dcnt = CoalesceValues(depths,maxc<=256?16:256,depthindex,maxc);
3093 icnt = CoalesceValues(italics,maxc<=256?64:256,italicindex,maxc);
3094 if ( last==-1 ) { first = 1; last = 0; }
3095
3096 FindCharlists(sf,charlistindex,map,maxc);
3097 ecnt = FindExtensions(sf,extensions,extenindex,map,maxc);
3098
3099 kcnt = 0;
3100 for ( i=0; i<maxc && i<map->enccount; ++i ) {
3101 if ( map->map[i]!=-1 && SCWorthOutputting(sf->glyphs[map->map[i]])) {
3102 SplineChar *sc = sf->glyphs[map->map[i]];
3103 for ( kp=sc->kerns; kp!=NULL; kp=kp->next )
3104 if ( kp->sc->orig_pos<sf->glyphcnt && /* Can happen when saving multiple pfbs */
3105 map->backmap[kp->sc->orig_pos]<maxc ) ++kcnt;
3106 }
3107 }
3108 kerns = NULL;
3109 if ( kcnt!=0 )
3110 kerns = malloc(kcnt*sizeof(double));
3111 kcnt = lkcnt = 0;
3112 memset(ligkerns,0,maxc*sizeof(struct ligkern *));
3113 for ( i=0; i<maxc && i<map->enccount; ++i ) {
3114 if ( map->map[i]!=-1 && SCWorthOutputting(sf->glyphs[map->map[i]])) {
3115 SplineChar *sc = sf->glyphs[map->map[i]];
3116 for ( kp=sc->kerns; kp!=NULL; kp=kp->next )
3117 if ( kp->sc->orig_pos<sf->glyphcnt && /* Can happen when saving multiple pfbs */
3118 map->backmap[kp->sc->orig_pos]<maxc )
3119 ligkerns[i] = TfmAddKern(kp,ligkerns[i],kerns,&kcnt,map,maxc);
3120 for ( l=sc->ligofme; l!=NULL; l=l->next )
3121 ligkerns[i] = TfmAddLiga(l,ligkerns[i],map,maxc);
3122 if ( ligkerns[i]!=NULL ) {
3123 tags[i] = 1;
3124 for ( lk=ligkerns[i]; lk!=NULL; lk=lk->next )
3125 ++lkcnt;
3126 }
3127 if ( extenindex[i]!=-1 )
3128 tags[i] = 3;
3129 else if ( charlistindex[i]!=-1 )
3130 tags[i] = 2;
3131 }
3132 }
3133 for ( i=sccnt=0; i<maxc && i<map->enccount; ++i )
3134 if ( ligkerns[i]!=NULL )
3135 ++sccnt;
3136 if ( sccnt>=128 ) /* We need to use the special extension mechanism */
3137 lkcnt += sccnt;
3138 memset(former,-1,maxc*sizeof(int));
3139 memset(lkindex,0,maxc*sizeof(uint16_t));
3140 if ( maxc==256 ) {
3141 lkarray = malloc(lkcnt*sizeof(uint32_t));
3142 if ( sccnt<128 ) {
3143 lkcnt = 0;
3144 do {
3145 any = false;
3146 for ( i=0; i<maxc; ++i ) if ( ligkerns[i]!=NULL ) {
3147 lk = ligkerns[i];
3148 ligkerns[i] = lk->next;
3149 if ( former[i]==-1 )
3150 lkindex[i] = lkcnt;
3151 else {
3152 lkarray[former[i]] |= (lkcnt-former[i]-1)<<24;
3153 if ( lkcnt-former[i]-1 >= 128 )
3154 IError( " generating lig/kern array, jump too far.\n" );
3155 }
3156 former[i] = lkcnt;
3157 lkarray[lkcnt++] = ((lk->next==NULL?128U:0U)<<24) |
3158 (lk->other_char<<16) |
3159 (lk->op<<8) |
3160 lk->remainder;
3161 free( lk );
3162 any = true;
3163 }
3164 } while ( any );
3165 } else {
3166 int lkcnt2 = sccnt;
3167 lkcnt = 0;
3168 for ( i=0; i<256; ++i ) if ( ligkerns[i]!=NULL ) {
3169 lkindex[i] = lkcnt;
3170 /* long pointer into big ligkern array */
3171 lkarray[lkcnt++] = (129U<<24) |
3172 (0<<16) |
3173 lkcnt2;
3174 for ( lk = ligkerns[i]; lk!=NULL; lk=lknext ) {
3175 lknext = lk->next;
3176 /* Here we will always skip to the next record, so the */
3177 /* skip_byte will always be 0 (well, or 128 for stop) */
3178 lkarray[lkcnt2++] = ((lknext==NULL?128:0)<<24) |
3179 (lk->other_char<<16) |
3180 (lk->op<<8) |
3181 lk->remainder;
3182 free( lk );
3183 }
3184 }
3185 if ( lkcnt>sccnt )
3186 IError( "lig/kern mixup\n" );
3187 lkcnt = lkcnt2;
3188 }
3189 } else {
3190 o_lkarray = calloc(lkcnt,sizeof(struct ligkern));
3191 if ( sccnt<128 ) {
3192 lkcnt = 0;
3193 do {
3194 any = false;
3195 for ( i=0; i<maxc; ++i ) if ( ligkerns[i]!=NULL ) {
3196 lk = ligkerns[i];
3197 ligkerns[i] = lk->next;
3198 if ( former[i]==-1 )
3199 lkindex[i] = lkcnt;
3200 else {
3201 o_lkarray[former[i]].skip |= lkcnt-former[i]-1;
3202 if ( lkcnt-former[i]-1 >= 128 )
3203 IError( " generating lig/kern array, jump too far.\n" );
3204 }
3205 former[i] = lkcnt;
3206 o_lkarray[lkcnt].skip = lk->next==NULL ? 128U : 0U;
3207 o_lkarray[lkcnt].other_char = lk->other_char;
3208 o_lkarray[lkcnt].op = lk->op;
3209 o_lkarray[lkcnt++].remainder = lk->remainder;
3210 free( lk );
3211 any = true;
3212 }
3213 } while ( any );
3214 } else {
3215 int lkcnt2 = sccnt;
3216 lkcnt = 0;
3217 for ( i=0; i<maxc; ++i ) if ( ligkerns[i]!=NULL ) {
3218 lkindex[i] = lkcnt;
3219 /* long pointer into big ligkern array */
3220 o_lkarray[lkcnt].skip = 128+1;
3221 o_lkarray[lkcnt].other_char = 0;
3222 o_lkarray[lkcnt].op = lkcnt2>>16;
3223 o_lkarray[lkcnt++].remainder = lkcnt2&0xffff;
3224 for ( lk = ligkerns[i]; lk!=NULL; lk=lknext ) {
3225 lknext = lk->next;
3226 /* Here we will always skip to the next record, so the */
3227 /* skip_byte will always be 0 (well, or 128 for stop) */
3228 former[i] = lkcnt;
3229 o_lkarray[lkcnt2].skip = (lknext==NULL?128:0);
3230 o_lkarray[lkcnt2].other_char = lk->other_char;
3231 o_lkarray[lkcnt2].op = lk->op;
3232 o_lkarray[lkcnt2++].remainder = lk->remainder;
3233 free( lk );
3234 }
3235 }
3236 if ( lkcnt>sccnt )
3237 IError( "lig/kern mixup\n" );
3238 lkcnt = lkcnt2;
3239 }
3240 }
3241
3242 /* Now output the file */
3243 /* Table of contents */
3244 if ( maxc==256 ) {
3245 putshort(tfm,
3246 6+ /* Table of contents size */
3247 18 + /* header size */
3248 (last-first+1) + /* Per glyph data */
3249 widcnt + /* entries in the width table */
3250 hcnt + /* entries in the height table */
3251 dcnt + /* entries in the depth table */
3252 icnt + /* entries in the italic correction table */
3253 lkcnt + /* entries in the lig/kern table */
3254 kcnt + /* entries in the kern table */
3255 ecnt + /* No extensible characters here */
3256 pcnt); /* font parameter words */
3257 putshort(tfm,18);
3258 putshort(tfm,first);
3259 putshort(tfm,last);
3260 putshort(tfm,widcnt);
3261 putshort(tfm,hcnt);
3262 putshort(tfm,dcnt);
3263 putshort(tfm,icnt);
3264 putshort(tfm,lkcnt);
3265 putshort(tfm,kcnt);
3266 putshort(tfm,ecnt);
3267 putshort(tfm,pcnt);
3268 } else {
3269 int font_dir = 0;
3270 /* According to ofm2opl sources fontdir is: */
3271 /* 0=>TL, 1=>LT, 2=>TR, 3=>LB, 4=>BL, 5=>RT, 6=>BR, 7=>RB */
3272 /* And if bit 3 (8) is set then we have something called NFONTDIR */
3273 /* no idea what that means */
3274 /* TL Means start at the top left of the page (standard L 2 R) */
3275 /* TR Means start at the top right of the page (standard R 2 L) */
3276 /* And I think RT is appropriate for vertical Japanese */
3277 /* (bit three might mean the reversed direction of English inside */
3278 /* mongolian. It doesn't seem appropriate in a font though) */
3279 /* HOWEVER, all the ofm files I've seen, including arabic only ones*/
3280 /* set fontdir to 0 (TL). So I shall too */
3281 putlong(tfm,0); /* Undocumented entry. Perhaps the level? */
3282 /* ofm2opl says it is the level */
3283 putlong(tfm,
3284 14+ /* Table of contents size */
3285 18 + /* header size */
3286 2*(last-first+1) + /* Per glyph data */
3287 widcnt + /* entries in the width table */
3288 hcnt + /* entries in the height table */
3289 dcnt + /* entries in the depth table */
3290 icnt + /* entries in the italic correction table */
3291 2*lkcnt + /* entries in the lig/kern table */
3292 kcnt + /* entries in the kern table */
3293 2*ecnt + /* No extensible characters here */
3294 pcnt); /* font parameter words */
3295 putlong(tfm,18);
3296 putlong(tfm,first);
3297 putlong(tfm,last);
3298 putlong(tfm,widcnt);
3299 putlong(tfm,hcnt);
3300 putlong(tfm,dcnt);
3301 putlong(tfm,icnt);
3302 putlong(tfm,lkcnt);
3303 putlong(tfm,kcnt);
3304 putlong(tfm,ecnt);
3305 putlong(tfm,pcnt);
3306 putlong(tfm,font_dir);
3307 }
3308 /* header */
3309 putlong(tfm,header.checksum);
3310 putlong(tfm,header.design_size);
3311 fwrite(header.encoding,1,sizeof(header.encoding),tfm);
3312 fwrite(header.family,1,sizeof(header.family),tfm);
3313 fwrite(&header.seven_bit_safe_flag,1,4,tfm);
3314 /* per-glyph data */
3315 for ( i=first; i<=last ; ++i ) {
3316 if ( map->map[i]!=-1 && SCWorthOutputting(sf->glyphs[map->map[i]])) {
3317 if ( maxc==256 ) {
3318 putc(widthindex[i],tfm);
3319 putc((heightindex[i]<<4)|depthindex[i],tfm);
3320 putc((italicindex[i]<<2)|tags[i],tfm);
3321 putc(tags[i]==0?0 :
3322 tags[i]==1?lkindex[i] :
3323 tags[i]==2?charlistindex[i] :
3324 extenindex[i],tfm);
3325 } else {
3326 putshort(tfm,widthindex[i]);
3327 putc(heightindex[i],tfm);
3328 putc(depthindex[i],tfm);
3329 putc(italicindex[i],tfm);
3330 putc(tags[i],tfm);
3331 putshort(tfm,tags[i]==0?0 :
3332 tags[i]==1?lkindex[i] :
3333 tags[i]==2?charlistindex[i] :
3334 extenindex[i]);
3335 }
3336 } else {
3337 putlong(tfm,0);
3338 if ( maxc>256 )
3339 putlong(tfm,0);
3340 }
3341 }
3342 /* width table */
3343 for ( i=0; i<widcnt; ++i )
3344 putlong(tfm,widths[i]);
3345 /* height table */
3346 for ( i=0; i<hcnt; ++i )
3347 putlong(tfm,heights[i]);
3348 /* depth table */
3349 for ( i=0; i<dcnt; ++i )
3350 putlong(tfm,depths[i]);
3351 /* italic correction table */
3352 for ( i=0; i<icnt; ++i )
3353 putlong(tfm,italics[i]);
3354
3355 /* lig/kern table */
3356 if ( maxc==256 ) {
3357 for ( i=0; i<lkcnt; ++i )
3358 putlong(tfm,lkarray[i]);
3359 } else {
3360 for ( i=0; i<lkcnt; ++i ) {
3361 putshort(tfm,o_lkarray[i].skip);
3362 putshort(tfm,o_lkarray[i].other_char);
3363 putshort(tfm,o_lkarray[i].op);
3364 putshort(tfm,o_lkarray[i].remainder);
3365 }
3366 }
3367
3368 /* kern table */
3369 for ( i=0; i<kcnt; ++i )
3370 putlong(tfm,(kerns[i]*(1<<20))/(sf->ascent+sf->descent));
3371
3372 /* extensible table */
3373 if ( maxc==256 ) {
3374 for ( i=0; i<ecnt; ++i ) {
3375 putc(extensions[i].extens[0],tfm);
3376 putc(extensions[i].extens[1],tfm);
3377 putc(extensions[i].extens[2],tfm);
3378 putc(extensions[i].extens[3],tfm);
3379 }
3380 } else {
3381 for ( i=0; i<ecnt; ++i ) {
3382 putshort(tfm,extensions[i].extens[0]);
3383 putshort(tfm,extensions[i].extens[1]);
3384 putshort(tfm,extensions[i].extens[2]);
3385 putshort(tfm,extensions[i].extens[3]);
3386 }
3387 }
3388
3389 /* font parameters */
3390 for ( i=0; i<pcnt; ++i )
3391 putlong(tfm,sf->texdata.params[i]);
3392
3393 SFLigatureCleanup(sf);
3394 SFKernCleanup(sf,false);
3395
3396 if ( maxc>256 ) {
3397 free( o_lkarray );
3398 free( ligkerns );
3399 free( widths );
3400 free( heights );
3401 free( depths );
3402 free( italics );
3403 free( tags );
3404 free( lkindex );
3405 free( former );
3406 free( charlistindex );
3407 free( extensions );
3408 free( extenindex );
3409 free( widthindex );
3410 free( heightindex );
3411 free( depthindex );
3412 free( italicindex );
3413 } else
3414 free( lkarray );
3415 return( !ferror(tfm));
3416 }
3417
3418 int TfmSplineFont(FILE *tfm, SplineFont *sf, EncMap *map,int layer) {
3419 return( _OTfmSplineFont(tfm,sf,map,256,layer));
3420 }
3421 /* ************************************************************************** */
3422 /* **************************** Writing OFM files *************************** */
3423 /* ************************************************************************** */
3424
3425 int OfmSplineFont(FILE *tfm, SplineFont *sf, EncMap *map,int layer) {
3426 return( _OTfmSplineFont(tfm,sf,map,65536,layer));
3427 }
3428
3429 /* ************************************************************************** */
3430
3431 enum metricsformat { mf_none, mf_afm, mf_amfm, mf_tfm, mf_ofm, mf_pfm, mf_feat };
3432
3433 static enum metricsformat MetricsFormatType(char *filename) {
3434 FILE *file = fopen(filename,"rb");
3435 unsigned char buffer[200];
3436 off_t filesize;
3437 int len;
3438
3439 if ( file==NULL )
3440 return( mf_none );
3441
3442 len = fread(buffer,1,sizeof(buffer)-1,file);
3443 buffer[len] = '\0';
3444 filesize = GFileGetSizeF(file);
3445 fclose(file);
3446
3447 if ( strstr((char *) buffer,"StartFontMetrics")!=NULL )
3448 return( mf_afm );
3449 if ( strstr((char *) buffer,"StartMasterFontMetrics")!=NULL ||
3450 strstr((char *) buffer,"StarMasterFontMetrics")!=NULL ) /* ff had a bug and used this file header by mistake */
3451 return( mf_amfm );
3452
3453 if ( len >= 48 && filesize == 4*((buffer[0]<<8)|buffer[1]) &&
3454 ((buffer[0]<<8)|buffer[1]) == 6 +
3455 ((buffer[2]<<8)|buffer[3]) +
3456 ( ((buffer[6]<<8)|buffer[7]) - ((buffer[4]<<8)|buffer[5]) + 1 ) +
3457 ((buffer[8]<<8)|buffer[9]) +
3458 ((buffer[10]<<8)|buffer[11]) +
3459 ((buffer[12]<<8)|buffer[13]) +
3460 ((buffer[14]<<8)|buffer[15]) +
3461 ((buffer[16]<<8)|buffer[17]) +
3462 ((buffer[18]<<8)|buffer[19]) +
3463 ((buffer[20]<<8)|buffer[21]) +
3464 ((buffer[22]<<8)|buffer[23]) )
3465 return( mf_tfm );
3466
3467 if ( len >= 48 && filesize == 4*BigEndianWord(buffer+4) &&
3468 BigEndianWord(buffer) == 0 &&
3469 BigEndianWord(buffer+4) == 14 +
3470 BigEndianWord(buffer+8) +
3471 2*( BigEndianWord(buffer+16) - BigEndianWord(buffer+12) + 1 ) +
3472 BigEndianWord(buffer+20) +
3473 BigEndianWord(buffer+24) +
3474 BigEndianWord(buffer+28) +
3475 BigEndianWord(buffer+32) +
3476 2*BigEndianWord(buffer+36) +
3477 BigEndianWord(buffer+40) +
3478 2*BigEndianWord(buffer+44) +
3479 BigEndianWord(buffer+48) )
3480 return( mf_ofm );
3481
3482 if ( len>= 6 && buffer[0]==0 && buffer[1]==1 &&
3483 (buffer[2]|(buffer[3]<<8)|(buffer[4]<<16)|(buffer[5]<<24))== filesize )
3484 return( mf_pfm );
3485
3486 /* I don't see any distinguishing marks for a feature file */
3487
3488 if ( strstrmatch(filename,".afm")!=NULL )
3489 return( mf_afm );
3490 if ( strstrmatch(filename,".amfm")!=NULL )
3491 return( mf_amfm );
3492 if ( strstrmatch(filename,".tfm")!=NULL )
3493 return( mf_tfm );
3494 if ( strstrmatch(filename,".ofm")!=NULL )
3495 return( mf_ofm );
3496 if ( strstrmatch(filename,".pfm")!=NULL )
3497 return( mf_pfm );
3498 if ( strstrmatch(filename,".fea")!=NULL )
3499 return( mf_feat );
3500
3501 return( mf_none );
3502 }
3503
3504 int LoadKerningDataFromMetricsFile(SplineFont *sf,char *filename,EncMap *map, bool ignore_invalid_replacement) {
3505 int ret;
3506
3507 switch ( MetricsFormatType(filename)) {
3508 case mf_afm:
3509 ret = LoadKerningDataFromAfm(sf,filename);
3510 break;
3511 case mf_amfm:
3512 ret = LoadKerningDataFromAmfm(sf,filename);
3513 break;
3514 case mf_tfm:
3515 ret = LoadKerningDataFromTfm(sf,filename,map);
3516 break;
3517 case mf_ofm:
3518 ret = LoadKerningDataFromOfm(sf,filename,map);
3519 break;
3520 case mf_pfm:
3521 ret = LoadKerningDataFromPfm(sf,filename,map);
3522 break;
3523 case mf_feat:
3524 SFApplyFeatureFilename(sf,filename, ignore_invalid_replacement);
3525 ret = true;
3526 break;
3527 case mf_none:
3528 default:
3529 /* If all else fails try a mac resource */
3530 /* mac resources can be in so many different formats and even files */
3531 /* that I'm not even going to try to check for it here */
3532 ret = LoadKerningDataFromMacFOND(sf,filename,map);
3533 break;
3534 }
3535 if ( ret ) {
3536 FontInfo_Destroy(sf);
3537 MVReKernAll(sf);
3538 }
3539 return( ret );
3540 }