Debian Codegraph @

contrib/fonttools/acorn2sfd.c

raw symbols SCIP
1 /* acorn2sfd.c.
2 * This program converts outline fonts used with RISC OS (from
3 * http://www.riscosopen.org or http://riscos.com) to .sfd files.
4 *
5 * To build, run:
6 * make acorn2sfd
7 * in the fontforgeexe directory. It has been tested on Linux. I have
8 * not tried compiling it on Windows.
9 *
10 * To use:
11 * acorn2sfd [path_to_font_directory]
12 * eg:
13 * acorn2sfd \!Fonts/NewHall/Medium/Italic
14 *
15 * remove any ,ff6 suffix on any files in the font directory before running
16 * acorn2sfd.
17 */
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22
23 #include <fontforge-config.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <dirent.h>
28 #include <errno.h>
29 #include <gutils.h>
30
31 #include "splineutil.h"
32 #include "cvundoes.h"
33 #include "lookups.h"
34 #include "mem.h"
35 #include "namelist.h"
36 #include "sfd.h"
37 #include "splinefont.h"
38 #include "splineutil2.h"
39 #include "start.h"
40 #include "ustring.h" /* read fontforge inc/ustring.h */
41
42 static int includestrokes = false;
43
44 extern Encoding custom;
45 extern NameList *namelist_for_new_fonts;
46
47 #define true 1
48 #define false 0
49
50 struct r_kern {
51 int right;
52 int amount;
53 struct r_kern *next;
54 };
55
56 struct Outlines {
57 int version;
58 int design_size;
59 int nchunks; /* Each chunk holds outlines for 32 characters. I think */
60 int *chunk_offset; /* if chunk[i]==chunk[i+1] => empty. One extra chunk offset at end to give size of final chunk */
61 int scaf_flags;
62 int ns;
63 int scaffold_size;
64 int *scaffold_offset;
65 char *scaffold_indexsize;
66 char *fontname;
67 char *metrics_fontname;
68 int metrics_n;
69 int *xadvance, *yadvance;
70 int defxadvance, defyadvance;
71 struct r_kern **kerns;
72 SplineFont *sf;
73 };
74
75 /* Little-endian */
76 static int r_getushort(FILE *file) {
77 int ch1;
78
79 ch1 = getc(file);
80 return( (getc(file)<<8)|ch1 );
81 }
82
83 static int r_getshort(FILE *file) {
84 return( (short) r_getushort(file));
85 }
86
87 static int r_getint(FILE *file) {
88 int ch1, ch2, ch3;
89
90 ch1 = getc(file);
91 ch2 = getc(file);
92 ch3 = getc(file);
93 return( (((((getc(file)<<8)|ch3)<<8)|ch2)<<8)|ch1 );
94 }
95
96 static char *get_font_filename(const char *str)
97 {
98 char *fname = copy(str);
99 char *c;
100
101 for (c = fname; *c; c++) {
102 /* Change non-breaking space into a normal space */
103 if ((*c & 0xff) == 0xa0) {
104 *c = ' ';
105 }
106 }
107
108 return fname;
109 }
110
111 static const char *modifierlist[] = { "Ital", "Obli", "Kursive", "Cursive",
112 "Slanted", "Expa", "Cond", NULL };
113 static const char **mods[] = { knownweights, modifierlist, NULL };
114
115
116 static char *GuessFamily(char *fontname) {
117 char *fpt, *pt;
118 int i,j;
119
120 if ( (fpt=strchr(fontname,'-'))!=NULL && fpt!=fontname )
121 return( copyn(fontname,fpt-fontname));
122 if ( (fpt=strchr(fontname,'.'))!=NULL && fpt!=fontname )
123 return( copyn(fontname,fpt-fontname));
124
125 for ( i=0; mods[i]!=NULL; ++i ) for ( j=0; mods[i][j]!=NULL; ++j ) {
126 pt = strstr(fontname,mods[i][j]);
127 if ( pt!=NULL && (fpt==NULL || pt<fpt))
128 fpt = pt;
129 }
130
131 if ( fpt==NULL )
132 return( copy( fontname ));
133
134 return( copyn(fontname,fpt-fontname));
135 }
136
137 static char *GuessWeight(char *fontname) {
138 int i;
139
140 for ( i=0; knownweights[i]!=NULL; ++i )
141 if ( strstr(fontname,knownweights[i])!=NULL )
142 return( copy( realweights[i]));
143
144 return( copy( "Regular" ));
145 }
146
147 static char *despace(char *fontname) {
148 char *pt, *npt;
149
150 fontname = copy(fontname);
151 for ( pt=npt=fontname; *pt; ++pt )
152 if ( *pt!=' ' )
153 *npt++ = *pt;
154 *npt = '\0';
155 return( fontname );
156 }
157
158 static void readcoords(FILE *file,int is12, int *x, int *y) {
159 int ch1, ch2, ch3;
160
161 if ( is12 ) {
162 ch1 = getc(file);
163 ch2 = getc(file);
164 ch3 = getc(file);
165 ch1 = ch1|((ch2&0xf)<<8);
166 ch2 = (ch2>>4)|(ch3<<4);
167 *x = (ch1<<(32-12))>>(32-12); /* Sign extend */
168 *y = (ch2<<(32-12))>>(32-12); /* Sign extend */
169 } else {
170 *x = (signed char) getc(file);
171 *y = (signed char) getc(file);
172 }
173 }
174
175 static int readcharindex(FILE *file,int twobyte) {
176 if ( twobyte )
177 return( r_getushort(file));
178 else
179 return( getc(file));
180 }
181
182 static SplineSet *FinishSet(SplineSet *old,SplineSet *active,int closed) {
183 if ( active!=NULL ) {
184 if ( active->first->me.x==active->last->me.x &&
185 active->first->me.y==active->last->me.y &&
186 active->first!=active->last ) {
187 active->first->prevcp = active->last->prevcp;
188 active->first->noprevcp = active->last->noprevcp;
189 active->last->prev->to = active->first;
190 active->first->prev = active->last->prev;
191 SplinePointFree(active->last);
192 active->last = active->first;
193 } else if ( closed ) {
194 SplineMake3(active->last,active->first);
195 active->last = active->first;
196 }
197 active->next = old;
198 return( active );
199 }
200 return( old );
201 }
202
203 static SplineSet *ReadSplineSets(FILE *file,int flags,SplineSet *old,int closed) {
204 int verb;
205 int x1,y1, x2,y2, x3,y3;
206 SplineSet *active=NULL;
207 SplinePoint *next;
208
209 while ( (verb=getc(file))!=EOF && (verb&0x3)!=0 ) {
210 readcoords(file,flags&1,&x1,&y1);
211 if ( (verb&0x3)==1 ) { /* Move to */
212 old = FinishSet(old,active,closed);
213 active = calloc(1,sizeof(SplineSet));
214 active->first = active->last = calloc(1,sizeof(SplinePoint));
215 active->first->me.x = x1; active->first->me.y = y1;
216 active->first->nextcp = active->first->prevcp = active->first->me;
217 active->first->nonextcp = active->first->noprevcp = true;
218 } else {
219 if ( active==NULL ) {
220 fprintf( stderr, "No initial point, assuming 0,0\n" );
221 active = calloc(1,sizeof(SplineSet));
222 active->first = active->last = calloc(1,sizeof(SplinePoint));
223 }
224 next = calloc(1,sizeof(SplinePoint));
225 if ( (verb&3)==2 ) { /* Line to */
226 next->me.x = x1; next->me.y = y1;
227 next->nextcp = next->prevcp = next->me;
228 } else { /* Curve to */
229 readcoords(file,flags&1,&x2,&y2);
230 readcoords(file,flags&1,&x3,&y3);
231 active->last->nextcp.x = x1; active->last->nextcp.y = y1;
232 next->prevcp.x = x2; next->prevcp.y = y2;
233 next->me.x = x3; next->me.y = y3;
234 next->nextcp = next->me;
235 }
236 SplineMake3(active->last,next);
237 active->last = next;
238 }
239 }
240 ungetc(verb,file);
241 return( FinishSet(old,active,closed));
242 }
243
244 static SplineChar *ReadChar(FILE *file,struct Outlines *outline,int enc) {
245 char buffer[12];
246 int flags, x, y, verb, ch;
247 RefChar *r1, *r2;
248 SplineFont *sf = outline->sf;
249 SplineChar *sc = SFSplineCharCreate(sf);
250 SplineSet * spUnused = NULL;
251
252 flags = getc(file);
253 if ( !(flags&(1<<3)) ) {
254 fprintf( stderr, "Character %d is a bitmap character\n", enc );
255 free(sc);
256 return( NULL );
257 }
258
259 sc->orig_pos = sf->glyphcnt++;
260 sf->glyphs[sc->orig_pos] = sc;
261 sf->map->map[enc] = sc->orig_pos;
262 sf->map->backmap[sc->orig_pos] = enc;
263 sc->unicodeenc = enc;
264 sc->changedsincelasthinted = true; /* I don't understand the scaffold lines */
265 /* which I think are the same as hints. So no hints processed. PfaEdit */
266 /* should autohint the char */
267 sc->name = copy( StdGlyphName(buffer,enc,ui_none,NULL));
268 sc->width = (outline->defxadvance*(sf->ascent + sf->descent))/1000;
269 sc->vwidth = (outline->defyadvance*(sf->ascent + sf->descent))/1000;
270 sc->widthset = true;
271 if ( outline->xadvance!=NULL )
272 sc->width = (outline->xadvance[enc]*(sf->ascent+sf->descent))/1000;
273 if ( outline->yadvance!=NULL )
274 sc->vwidth = (outline->yadvance[enc]*(sf->ascent+sf->descent))/1000;
275
276 if ( flags&(1<<4) ) {
277 r1 = calloc(1,sizeof(RefChar));
278 r1->transform[0] = r1->transform[3] = 1;
279 r1->orig_pos = readcharindex(file,flags&(1<<6));
280 if ( flags&(1<<5) ) {
281 r2 = calloc(1,sizeof(RefChar));
282 r2->transform[0] = r2->transform[3] = 1;
283 r2->orig_pos = readcharindex(file,flags&(1<<6));
284 readcoords(file,flags&1,&x,&y);
285 r2->transform[4] = x; r2->transform[5] = y;
286 r1->next = r2;
287 }
288 sc->layers[ly_fore].refs = r1;
289 return(sc);
290 }
291
292 readcoords(file,flags&1,&x,&y); /* bounding box, lbearing, ybase */
293 readcoords(file,flags&1,&x,&y); /* bounding box, width,height */
294
295 sc->layers[ly_fore].splines = ReadSplineSets(file,flags,NULL,true);
296 verb = getc(file);
297 if ( verb!=EOF && verb&(1<<2) ) {
298 /* It looks to me as though the stroked paths are duplicates of the */
299 /* filled paths, I'm assuming they are some form of hinting (if pixel*/
300 /* size is too small then just stroke, otherwise fill?) */
301 /* Every character has them... */
302 /*fprintf( stderr, "There are some stroked paths in %s, you should run\n Element->Expand Stroke on this character\n", sc->name );*/
303 if ( includestrokes )
304 sc->layers[ly_fore].splines = ReadSplineSets(file,flags,sc->layers[ly_fore].splines,false);
305 else {
306 spUnused = ReadSplineSets(file,flags,NULL,false); /* read and ignore */
307 SplinePointListsFree(spUnused);
308 }
309 verb = getc(file);
310 }
311 if ( verb!=EOF && verb&(1<<3)) {
312 while ( (ch=readcharindex(file,flags&(1<<6)))!=0 && !feof(file)) {
313 r1 = calloc(1,sizeof(RefChar));
314 r1->transform[0] = r1->transform[3] = 1;
315 r1->orig_pos = ch;
316 readcoords(file,flags&1,&x,&y);
317 r1->transform[4] = x; r1->transform[5] = y;
318 r1->next = sc->layers[ly_fore].refs;
319 sc->layers[ly_fore].refs = r1;
320 }
321 }
322 return( sc );
323 }
324
325 static void ReadChunk(FILE *file,struct Outlines *outline,int chunk) {
326 int flag=0x80000000;
327 int i, offsets[32];
328 int index_start;
329
330 fseek(file,outline->chunk_offset[chunk],SEEK_SET);
331
332 if ( outline->version>=7 )
333 flag = r_getint(file);
334 else if ( outline->version==6 )
335 flag |= (1<<7);
336 if ( (~flag)&0x80000000 )
337 fprintf( stderr, "Bad flag at beginning of chunk %d\n", chunk );
338
339 index_start = ftell(file);
340 for ( i=0; i<32; ++i )
341 offsets[i] = r_getint(file);
342
343 if ( flag & (1<<7) ) {
344 /* Does this chunk have any composite characters which refer to things */
345 /* in other chunks? I don't really care so I ignore them all */
346 for ( i=0; i<((outline->nchunks+7)>>3); ++i )
347 getc(file);
348 }
349
350 for ( i=0; i<32; ++i ) if ( offsets[i]!=0 && 32*chunk+i<outline->metrics_n ) {
351 fseek(file,index_start+offsets[i],SEEK_SET);
352 ReadChar(file,outline,32*chunk+i);
353 }
354 }
355
356 /* Handles *?{}[] wildcards */
357 static int WildMatch(char *pattern, char *name,int ignorecase) {
358 char ch, *ppt, *npt, *ept;
359
360 if ( pattern==NULL )
361 return( true );
362
363 while (( ch = *pattern)!='\0' ) {
364 if ( ch=='*' ) {
365 for ( npt=name; ; ++npt ) {
366 if ( WildMatch(pattern+1,npt,ignorecase))
367 return( true );
368 if ( *npt=='\0' )
369 return( false );
370 }
371 } else if ( ch=='?' ) {
372 if ( *name=='\0' )
373 return( false );
374 ++name;
375 } else if ( ch=='[' ) {
376 /* [<char>...] matches the chars
377 * [<char>-<char>...] matches any char within the range (inclusive)
378 * the above may be concattenated and the resultant pattern matches
379 * anything thing which matches any of them.
380 * [^<char>...] matches any char which does not match the rest of
381 * the pattern
382 * []...] as a special case a ']' immediately after the '[' matches
383 * itself and does not end the pattern */
384 int found = 0, not=0;
385 ++pattern;
386 if ( pattern[0]=='^' ) { not = 1; ++pattern; }
387 for ( ppt = pattern; (ppt!=pattern || *ppt!=']') && *ppt!='\0' ; ++ppt ) {
388 ch = *ppt;
389 if ( ppt[1]=='-' && ppt[2]!=']' && ppt[2]!='\0' ) {
390 int ch2 = ppt[2];
391 if ( (*name>=ch && *name<=ch2) ||
392 (ignorecase && islower(ch) && islower(ch2) &&
393 *name>=toupper(ch) && *name<=toupper(ch2)) ||
394 (ignorecase && isupper(ch) && isupper(ch2) &&
395 *name>=tolower(ch) && *name<=tolower(ch2))) {
396 if ( !not ) {
397 found = 1;
398 break;
399 }
400 } else {
401 if ( not ) {
402 found = 1;
403 break;
404 }
405 }
406 ppt += 2;
407 } else if ( ch==*name || (ignorecase && tolower(ch)==tolower(*name)) ) {
408 if ( !not ) {
409 found = 1;
410 break;
411 }
412 } else {
413 if ( not ) {
414 found = 1;
415 break;
416 }
417 }
418 }
419 if ( !found )
420 return( false );
421 while ( *ppt!=']' && *ppt!='\0' ) ++ppt;
422 pattern = ppt;
423 ++name;
424 } else if ( ch=='{' ) {
425 /* matches any of a comma separated list of substrings */
426 for ( ppt = pattern+1; *ppt!='\0' ; ppt = ept ) {
427 for ( ept=ppt; *ept!='}' && *ept!=',' && *ept!='\0'; ++ept );
428 for ( npt = name; ppt<ept; ++npt, ++ppt ) {
429 if ( *ppt != *npt && (!ignorecase || tolower(*ppt)!=tolower(*npt)) )
430 break;
431 }
432 if ( ppt==ept ) {
433 char *ecurly = ept;
434 while ( *ecurly!='}' && *ecurly!='\0' ) ++ecurly;
435 if ( WildMatch(ecurly+1,npt,ignorecase))
436 return( true );
437 }
438 if ( *ept=='}' )
439 return( false );
440 if ( *ept==',' ) ++ept;
441 }
442 } else if ( ch==*name ) {
443 ++name;
444 } else if ( ignorecase && tolower(ch)==tolower(*name)) {
445 ++name;
446 } else
447 return( false );
448 ++pattern;
449 }
450 if ( *name=='\0' )
451 return( true );
452
453 return( false );
454 }
455
456 static int dirmatch(char *dirname, char *pattern,char *buffer) {
457 DIR *dir;
458 struct dirent *ent;
459
460 dir = opendir(dirname);
461 if ( dir==NULL )
462 return( -1 ); /* No dir */
463
464 while (( ent = readdir(dir))!=NULL ) {
465 if ( WildMatch(pattern,ent->d_name,true) ) {
466 strcpy(buffer,dirname);
467 strcat(buffer,"/");
468 strcat(buffer,ent->d_name);
469 closedir(dir);
470 return( 1 ); /* Good */
471 }
472 }
473 closedir(dir);
474 return( 0 ); /* Not found */
475 }
476
477 static int dirfind(char *dir, char *pattern,char *buffer) {
478 char *pt, *space;
479 int ret = dirmatch(dir,pattern,buffer);
480
481 if ( ret==-1 ) {
482 /* Just in case the give us the pathspec for the Outlines file rather than the dir containing it */
483 space = copy(dir);
484 pt = strrchr(space,'/');
485 if ( pt!=NULL ) {
486 *pt = '\0';
487 ret = dirmatch(space,pattern,buffer);
488 }
489 free( space );
490 }
491 if ( ret==-1 )
492 ret = 0;
493 if ( !ret ) {
494 strcpy(buffer,dir);
495 strcat(buffer,"/");
496 strcat(buffer,pattern);
497 }
498 return( ret );
499 }
500
501 static void ReadIntmetrics(char *dir, struct Outlines *outline) {
502 char *filename = malloc(strlen(dir)+strlen("/Intmetrics")+3);
503 FILE *file=NULL;
504 int i, flags, m, n, left, right;
505 int kern_offset, table_base, misc_offset;
506 char buffer[100];
507 uint8_t *mapping=NULL;
508 int *widths;
509 struct r_kern *kern;
510
511 if ( dirfind(dir, "IntMet?",filename) )
512 file = fopen(filename,"rb");
513 else if ( dirfind(dir, "Intmetric?",filename) )
514 file = fopen(filename,"rb");
515 if ( file==NULL ) {
516 fprintf(stderr,"Couldn't open %s (for advance width data)\n Oh well, advance widths will all be wrong.\n", filename );
517 free(filename);
518 return;
519 }
520 for ( i=0; i<40; ++i ) {
521 buffer[i] = getc(file);
522 if ( buffer[i]=='\r' ) buffer[i] = '\0';
523 }
524 buffer[i] = '\0';
525 outline->metrics_fontname = copy(buffer);
526 r_getint(file); /* Must be 16 */
527 r_getint(file); /* Must be 16 */
528 n = getc(file); /* low order byte */
529 /* version number = */ getc(file);
530 flags = getc(file);
531 n |= getc(file)<<8; /* high order byte */
532 if ( flags&(1<<5) )
533 m = r_getushort(file);
534 else
535 m = 256;
536 if ( m!=0 ) {
537 mapping = malloc(m);
538 for ( i=0; i<m; ++i )
539 mapping[i] = getc(file);
540 outline->metrics_n = m;
541 } else
542 outline->metrics_n = n;
543 if ( !(flags&1) ) {
544 /* I ignore bbox data */
545 for ( i=0; i<4*n; ++i )
546 r_getshort(file);
547 }
548 if ( !(flags&2) ) {
549 widths = malloc(n*sizeof(int));
550 for ( i=0; i<n; ++i )
551 widths[i] = r_getshort(file);
552 if ( mapping==0 )
553 outline->xadvance = widths;
554 else {
555 outline->xadvance = calloc(outline->metrics_n,sizeof(int));
556 for ( i=0; i<m; ++i )
557 outline->xadvance[i] = widths[mapping[i]];
558 free(widths);
559 }
560 }
561 if ( !(flags&4) ) {
562 widths = malloc(n*sizeof(int));
563 for ( i=0; i<n; ++i )
564 widths[i] = r_getshort(file);
565 if ( mapping==0 )
566 outline->yadvance = widths;
567 else {
568 outline->yadvance = calloc(outline->metrics_n,sizeof(int));
569 for ( i=0; i<m; ++i )
570 outline->yadvance[i] = widths[mapping[i]];
571 free(widths);
572 }
573 }
574 if (m != 0)
575 free(mapping);
576
577 outline->defxadvance = outline->defyadvance = 1000;
578
579 if ( flags&8 ) {
580 table_base = ftell(file);
581 misc_offset = r_getshort(file);
582 kern_offset = r_getshort(file);
583 if ( misc_offset!=0 ) {
584 fseek(file,misc_offset+table_base,SEEK_SET);
585 /* font bounding box */ r_getshort(file); r_getshort(file); r_getshort(file); r_getshort(file);
586 if ( (flags&2) )
587 outline->defxadvance = r_getshort(file);
588 if ( (flags&4) )
589 outline->defyadvance = r_getshort(file);
590 }
591 if ( kern_offset!=0 && !feof(file) && outline->metrics_n!=0 ) {
592 fseek(file,kern_offset+table_base,SEEK_SET);
593 outline->kerns = calloc(outline->metrics_n,sizeof(struct r_kern *));
594 if ( flags&(1<<6) ) {
595 /* 16 bit */
596 while ( (left=r_getshort(file))!=0 && !feof(file)) {
597 while ( (right=r_getshort(file))!=0 ) {
598 if ( !(flags&2) && !feof(file)) {
599 kern = malloc(sizeof(struct r_kern));
600 kern->amount = r_getshort(file);
601 kern->right = right;
602 kern->next = outline->kerns[left];
603 outline->kerns[left] = kern;
604 }
605 if ( !(flags&4) )
606 /* I don't care about vertical kerning */ r_getshort(file);
607 }
608 }
609 } else {
610 while ( (left=getc(file))!=0 && !feof(file)) {
611 while ( (right=getc(file))!=0 && !feof(file)) {
612 if ( !(flags&2) ) {
613 kern = malloc(sizeof(struct r_kern));
614 kern->amount = r_getshort(file);
615 kern->right = right;
616 kern->next = outline->kerns[left];
617 outline->kerns[left] = kern;
618 }
619 if ( !(flags&4) )
620 /* I don't care about vertical kerning */ r_getshort(file);
621 }
622 }
623 }
624 }
625 }
626 fclose(file);
627 free(filename);
628 }
629
630 static void FixupKerns(SplineFont *sf,struct Outlines *outline) {
631 int i;
632 struct r_kern *kern;
633 KernPair *kp;
634 int em = sf->ascent+sf->descent;
635 int gid1, gid2;
636 struct lookup_subtable *subtable;
637
638 if ( outline->kerns==NULL )
639 return;
640
641 subtable = SFSubTableFindOrMake(sf,
642 CHR('k','e','r','n'),CHR('l','a','t','n'), gpos_pair);
643
644 for ( i=0; i<outline->metrics_n; ++i ) {
645 gid1 = sf->map->map[i];
646 if (gid1 == -1) {
647 continue;
648 }
649 for ( kern = outline->kerns[i]; kern!=NULL; kern=kern->next ) {
650 kp = calloc(1,sizeof(KernPair));
651 kp->off = em*kern->amount/1000;
652 kp->subtable = subtable;
653 gid2 = sf->map->map[kern->right];
654 if (gid2 != -1) {
655 kp->sc = sf->glyphs[gid2];
656 }
657 kp->next = sf->glyphs[gid1]->kerns;
658 sf->glyphs[gid1]->kerns = kp;
659 }
660 }
661 }
662
663 static void FixupRefs(SplineChar *sc,SplineFont *sf) {
664 RefChar *rf, *prev, *next;
665 EncMap *map = sf->map;
666 int gid;
667
668 if ( sc==NULL || sc->layers[ly_fore].refs==NULL )
669 return;
670 prev = NULL;
671 for ( rf = sc->layers[ly_fore].refs; rf!=NULL; rf=next ) {
672 next = rf->next;
673 if ( rf->orig_pos<0 || rf->orig_pos>=map->enccount ||
674 (gid = map->map[rf->orig_pos])==-1 ||
675 sf->glyphs[gid]==NULL ) {
676 fprintf( stderr, "%s contains a reference to a character at index %d which does not exist.\n",
677 sc->name, rf->orig_pos );
678 if ( prev==NULL )
679 sc->layers[ly_fore].refs = next;
680 else
681 prev->next = next;
682 free(rf);
683 } else {
684 rf->orig_pos = gid;
685 rf->sc = sf->glyphs[gid];
686 rf->adobe_enc = getAdobeEnc(rf->sc->name);
687 prev = rf;
688 }
689 }
690 }
691
692 static void FindEncoding(SplineFont *sf,char *filename) {
693 char *pt, *end;
694 char pattern[12];
695 char *otherdir;
696 char *encfilename;
697 FILE *file=NULL;
698 char buffer[200];
699 int pos, gid;
700
701 strcpy(pattern,"Base *");
702 pattern[4] = filename[strlen(filename)-1];
703 pt = strrchr(filename,'/');
704 if ( pt!=NULL )
705 *pt = '\0';
706 otherdir = malloc(strlen(filename)+strlen("/../Encodings")+5);
707 strcpy(otherdir,filename);
708 strcat(otherdir,"/../Encodings");
709 encfilename = malloc(strlen(otherdir)+strlen("base0encoding")+20);
710
711 if ( dirfind(otherdir, pattern,encfilename) )
712 file = fopen(encfilename,"rb");
713 else if ( dirfind(filename, pattern,encfilename) )
714 file = fopen(encfilename,"rb");
715 free(otherdir);
716
717 if ( file==NULL ) {
718 fprintf(stderr,"Couldn't open %s\n", encfilename );
719 free(encfilename);
720 return;
721 }
722
723 pos = 0;
724 while ( fgets(buffer,sizeof(buffer),file)!=NULL ) {
725 if ( *buffer=='%' || *buffer=='\n' )
726 continue;
727 for ( pt = buffer; *pt!='\0' ; ) {
728 while ( isspace(*pt)) ++pt;
729 if ( *pt=='/' ) {
730 for ( end = ++pt; !isspace(*end) && *end!='\0'; ++end );
731 if ( (gid=sf->map->map[pos])!=-1 && sf->glyphs[gid]!=NULL ) {
732 free(sf->glyphs[gid]->name);
733 sf->glyphs[gid]->name = copyn(pt,end-pt);
734 sf->glyphs[gid]->unicodeenc = UniFromName(sf->glyphs[gid]->name,ui_none,&custom);
735 }
736 ++pos;
737 pt = end;
738 } else
739 break;
740 }
741 }
742 free(encfilename);
743 fclose(file);
744 }
745
746 static SplineFont *ReadOutline(char *dir) {
747 char *filename = malloc(strlen(dir)+strlen("/Outlines*")+3);
748 FILE *file = NULL;
749 struct Outlines outline;
750 int i, ch;
751 char buffer[100];
752
753 if ( dirfind(dir, "Outlines*",filename) )
754 file = fopen(filename,"rb");
755 if ( file==NULL ) {
756 fprintf(stderr,"Couldn't open %s\n", filename );
757 free(filename);
758 return( NULL );
759 }
760
761 if ( getc(file)!='F' || getc(file)!='O' || getc(file)!='N' || getc(file)!='T' ||
762 getc(file)!='\0') { /* Final null means outline font */
763 fprintf(stderr, "%s is not an acorn risc outline font file\n", filename );
764 free(filename);
765 fclose(file);
766 return( NULL );
767 }
768
769 memset(&outline,0,sizeof(outline));
770
771 ReadIntmetrics(dir,&outline);
772
773 outline.version = getc(file);
774 outline.design_size = r_getushort(file);
775
776 /* bounding box, I don't care */
777 /* minx = */ r_getshort(file);
778 /* miny = */ r_getshort(file);
779 /* width = */ r_getshort(file);
780 /* height = */ r_getshort(file);
781
782 if ( outline.version<8 ) {
783 outline.nchunks = 8;
784 outline.chunk_offset = malloc(9*sizeof(int));
785 for ( i=0; i<9; ++i )
786 outline.chunk_offset[i] = r_getint(file);
787 outline.scaf_flags = 0;
788 outline.ns = -1;
789 } else {
790 int chunk_off = r_getint(file), pos;
791 outline.nchunks = r_getint(file);
792 outline.ns = r_getint(file);
793 outline.scaf_flags = r_getint(file);
794 for ( i=0; i<5; ++i )
795 /* MBZ = */ r_getint(file);
796 pos = ftell(file);
797 fseek(file,chunk_off,SEEK_SET);
798 outline.chunk_offset = malloc((outline.nchunks+1)*sizeof(int));
799 for ( i=0; i<=outline.nchunks; ++i )
800 outline.chunk_offset[i] = r_getint(file);
801 fseek(file,pos,SEEK_SET);
802 }
803
804 /* I really have no idea what these scaffold thingies are */
805 outline.scaffold_size = r_getushort(file);
806 if ( outline.ns==-1 )
807 outline.ns = outline.scaffold_size/2;
808 else if ( 2*outline.ns+1 > outline.scaffold_size )
809 fprintf( stderr, "Inconsistent scaffold count\n" );
810 /* I don't understand the scaffold stuff. */
811 /* there should be outline.ns-1 shorts of offsets, followed by a byte */
812 /* followed by the data the offsets point to. I'm just going to skip */
813 /* all of it. */
814 for ( i=0; i<outline.scaffold_size-sizeof(short); ++i )
815 getc(file);
816 for ( i=0; (ch=getc(file))!='\0' && ch!=EOF; )
817 if ( i<sizeof(buffer)-1 ) buffer[i++] = ch;
818 buffer[i] = '\0';
819 outline.fontname = strdup(buffer);
820 for ( i=0; (ch=getc(file))!='\0' && ch!=EOF; )
821 if ( i<sizeof(buffer)-1 ) buffer[i++] = ch;
822 buffer[i] = '\0';
823 /* Docs say that the word "Outlines" appears here, followed by a nul */
824 /* That appears to be a lie. We seem to get a random comment */
825 /* or perhaps a copyright notice */
826 if ( outline.metrics_fontname!=NULL &&
827 strmatch(outline.fontname,outline.metrics_fontname)!=0 )
828 fprintf( stderr, "Warning: Fontname in metrics (%s) and fontname in outline (%s)\n do not match.\n", outline.metrics_fontname, outline.fontname );
829 i = (outline.nchunks<8?8:outline.nchunks)*32;
830 if ( outline.metrics_n==0 || outline.metrics_n>i )
831 outline.metrics_n = i;
832
833 outline.sf = SplineFontEmpty();
834 outline.sf->glyphmax = outline.metrics_n;
835 outline.sf->glyphcnt = 0;
836 outline.sf->glyphs = calloc(outline.sf->glyphmax,sizeof(SplineChar *));
837 outline.sf->map = calloc(1,sizeof(EncMap));
838 outline.sf->map->enc = &custom;
839 outline.sf->map->encmax = outline.sf->map->enccount = outline.sf->map->backmax = outline.sf->glyphmax;
840 outline.sf->map->map = malloc(outline.sf->glyphmax*sizeof(int32_t));
841 outline.sf->map->backmap = malloc(outline.sf->glyphmax*sizeof(int32_t));
842 memset(outline.sf->map->map,-1,outline.sf->glyphmax*sizeof(int32_t));
843 memset(outline.sf->map->backmap,-1,outline.sf->glyphmax*sizeof(int32_t));
844 outline.sf->for_new_glyphs = namelist_for_new_fonts;
845 outline.sf->fontname = despace(outline.fontname);
846 outline.sf->fullname = copy(outline.fontname);
847 outline.sf->familyname = GuessFamily(outline.fontname);
848 outline.sf->weight = GuessWeight(outline.fontname);
849 if ( strcmp(buffer,"Outlines")!=0 )
850 outline.sf->copyright = copy(buffer);
851 strcpy(buffer,outline.fontname);
852 strcat(buffer,".sfd");
853 outline.sf->filename = get_font_filename(buffer);
854
855 outline.sf->top_enc = -1;
856
857 outline.sf->ascent = 4*outline.design_size/5;
858 outline.sf->descent = outline.design_size - outline.sf->ascent;
859
860 outline.sf->display_antialias = true;
861 outline.sf->display_size = -24;
862
863 for ( i=0; i<outline.nchunks; ++i ) {
864 if ( outline.chunk_offset[i]!=outline.chunk_offset[i+1] ) {
865 ReadChunk(file,&outline,i);
866 }
867 }
868
869 FixupKerns(outline.sf,&outline);
870
871 for ( i=0; i<outline.sf->glyphcnt; ++i )
872 FixupRefs(outline.sf->glyphs[i],outline.sf);
873
874 if ( isdigit(filename[strlen(filename)-1]) )
875 FindEncoding(outline.sf,filename);
876
877 free(filename);
878 fclose(file);
879
880 if ( !SFDWrite(outline.sf->filename,outline.sf,outline.sf->map,NULL,false) )
881 fprintf( stderr, "Failed to write outputfile %s\n", outline.sf->filename );
882 else
883 fprintf( stderr, "Created: %s\n", outline.sf->filename );
884 return( outline.sf );
885 }
886
887 static void dousage(void) {
888 printf( "acorn2sfd [options] {acorndirs}\n" );
889 printf( "\t-version\t (prints the version of acorn2sfd and exits)\n" );
890 printf( "\t-help\t\t (prints a brief help text and exits)\n" );
891 printf( "For more information see:\n\thttp://github.com/fontforge/\n" );
892 printf( "Send bug reports to:\[email protected]\n" );
893 exit(0);
894 }
895
896 static void dohelp(void) {
897 printf( "acorn2sfd -- reads an acorn RISCOS font and creates an sfd file.\n" );
898 printf( " Acorn RISCOS fonts consist of two files \"Outlines\" and \"Intmetrics\" in a\n" );
899 printf( " directory. This program takes a list of directory names and attempts to\n" );
900 printf( " convert the font data within each directory to one of FontForge's sfd files\n\n" );
901 dousage();
902 }
903
904 int main(int argc, char **argv) {
905 int i, any=false;
906 char *pt;
907
908 for ( i=1; i<argc; ++i ) {
909 if ( *argv[i]=='-' ) {
910 pt = argv[i]+1;
911 if ( *pt=='-' )
912 ++pt;
913 if ( strcmp(pt,"includestrokes")==0 )
914 includestrokes = true;
915 else if ( strcmp(pt,"version")==0 )
916 doversion(NULL);
917 else if ( strlen(pt)<=4 && strncmp(pt,"help",strlen(pt))==0 )
918 dohelp();
919 else
920 dousage();
921 } else {
922 ReadOutline(argv[i]);
923 any = true;
924 }
925 }
926 if ( !any )
927 ReadOutline(".");
928
929 return( 0 );
930 }