Debian Codegraph @

contrib/fonttools/pcl2ttf.c

raw symbols SCIP
1 /* Copyright (C) 2004 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 /* This program reads a pcl file (for an HP printer) and extracts any truetype*/
29 /* fonts nested within it */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <math.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38
39 #define true 1
40 #define false 0
41 #define CHR(ch1,ch2,ch3,ch4) (((ch1)<<24)|((ch2)<<16)|((ch3)<<8)|(ch4))
42
43 /* ************************************************************************** */
44 /* ************************** Parsing of PCL files ************************** */
45 /* ************************************************************************** */
46
47 static int escape_char = '\33';
48
49 struct ttf_header {
50 int header_size;
51 int fd_size;
52 int header_format;
53 int fonttype;
54 int style;
55 int baseline_pos;
56 int cell_width;
57 int cell_height;
58 int orientation;
59 int spacing;
60 int symbol_set;
61 int pitch;
62 int height;
63 int xheight;
64 int widthtype;
65 int strokeweight;
66 int typeface;
67 int serifstyle;
68 int quality;
69 int placement;
70 int upos, uthick;
71 int textheight;
72 int textwidth;
73 int firstcode;
74 int num_chars;
75 int actual_num; /* Not all glyphs are present. tt fonts seem to be subset */
76 /* there also seems some confusion between glyphs and encodings (two entries for glyph 3, space and nbspace) */
77 int pitchx, heightx;
78 int capheight;
79 int fontnumber;
80 char fontname[17];
81 int xres, yres; /* Bitmap fonts only */
82 int scale_factor;
83 int mupos, muthick;
84 int fontscaletech;
85 int variety;
86 struct subtables {
87 int len;
88 int offset, newoff;
89 int checksum;
90 int tag;
91 FILE *file;
92 int closeme;
93 } *subtables;
94 int subtable_cnt;
95 struct glyph {
96 int len;
97 int offset;
98 int unicode; /* 0xffff means unencoded glyph */
99 int glyph;
100 struct glyph *continuation;
101 } *glyphs;
102 /* the glyph count is the num_chars field above */
103 char *copyright;
104 unsigned char panose[10];
105 unsigned char charcompl[8];
106 };
107
108 static unsigned short getshort(FILE *pcl) {
109 int ch1, ch2;
110 ch1 = getc(pcl);
111 ch2 = getc(pcl);
112 return( (ch1<<8)|ch2 );
113 }
114
115 static unsigned int getlong(FILE *pcl) {
116 int ch1, ch2, ch3, ch4;
117 ch1 = getc(pcl);
118 ch2 = getc(pcl);
119 ch3 = getc(pcl);
120 ch4 = getc(pcl);
121 return( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 );
122 }
123
124 static int skip2thingamy(FILE *pcl,const char *thingamy) {
125 int ch;
126
127 for (;;) {
128 while ( (ch=getc(pcl))!=EOF && ch!=escape_char );
129 if ( ch==EOF )
130 return( false );
131 while ( (ch=getc(pcl))==escape_char );
132 if ( ch!=thingamy[0] )
133 continue;
134 ch = getc(pcl);
135 if ( ch==thingamy[1] )
136 return( true );
137 if ( ch==escape_char )
138 ungetc(ch,pcl);
139 }
140 }
141
142 static int skip2charcode(FILE *pcl) {
143 return( skip2thingamy(pcl,"*c"));
144 }
145
146 static int skip2glyph(FILE *pcl) {
147 return( skip2thingamy(pcl,"(s"));
148 }
149
150 static int slurpglyph(FILE *pcl,struct ttf_header *hdr, int slot) {
151 int unicode, size, ch;
152 int desc_size, i;
153 int cd_size;
154 int glyph_start;
155 int restart = ftell(pcl);
156
157 if ( !skip2charcode(pcl)) {
158 fprintf( stderr, "Fewer glyphs in font than expected\n" );
159 fseek(pcl,restart,SEEK_SET);
160 return( false );
161 }
162 fscanf(pcl,"%d", &unicode );
163 ch = getc(pcl);
164 if ( ch!='E' ) {
165 fprintf( stderr, "Fewer glyphs in font than expected\n" );
166 fseek(pcl,restart,SEEK_SET);
167 return( false );
168 }
169
170 if ( !skip2glyph(pcl))
171 return( false );
172 fscanf(pcl,"%d", &size );
173 ch = getc(pcl);
174 if ( ch!='W' ) {
175 fprintf( stderr, "Bad font, glyph size does not end in \"W\"\n" );
176 return( false );
177 }
178 glyph_start = ftell(pcl);
179
180 if ( getc(pcl)!=15 ) {
181 fprintf( stderr, "Bad font, glyph format must be 15\n" );
182 return( false );
183 }
184 if ( getc(pcl)!=0 ) {
185 fprintf( stderr, "Bad font, continuation may not be specified here\n" );
186 return( false );
187 }
188 desc_size = getc(pcl);
189 if ( getc(pcl)!=15 ) {
190 fprintf( stderr, "Bad font, glyph class must be 15\n" );
191 return( false );
192 }
193 for ( i=2; i<desc_size; ++i )
194 getc(pcl);
195 cd_size = getshort(pcl);
196 hdr->glyphs[slot].glyph = getshort(pcl);
197 hdr->glyphs[slot].unicode = unicode;
198 hdr->glyphs[slot].len = cd_size-4;
199 hdr->glyphs[slot].offset = ftell(pcl);
200 hdr->glyphs[slot].continuation = NULL;
201
202 fseek(pcl,glyph_start+size,SEEK_SET);
203 if ( size<cd_size+4+desc_size ) {
204 /* The docs only allow for one continuation glyph */
205 if ( !skip2glyph(pcl))
206 return( false );
207 fscanf(pcl,"%d", &size );
208 ch = getc(pcl);
209 if ( ch!='W' ) {
210 fprintf( stderr, "Bad font, glyph size does not end in \"W\"\n" );
211 return( false );
212 }
213 if ( getc(pcl)!=15 ) {
214 fprintf( stderr, "Bad font, glyph format must be 15\n" );
215 return( false );
216 }
217 if ( getc(pcl)!=1 ) {
218 fprintf( stderr, "Bad font, continuation must be specified here\n" );
219 return( false );
220 }
221 hdr->glyphs[slot].continuation = calloc(1,sizeof(struct glyph));
222 hdr->glyphs[slot].continuation->offset = ftell(pcl);
223 hdr->glyphs[slot].continuation->len = size-4;
224 fseek(pcl,size-2,SEEK_CUR);
225 }
226 return( true );
227 }
228
229 static int slurpglyphs(FILE *pcl,struct ttf_header *hdr) {
230 int i;
231
232 hdr->glyphs = calloc(hdr->num_chars,sizeof(struct glyph));
233
234 for ( i=0; i<hdr->num_chars; ++i )
235 if ( !slurpglyph(pcl,hdr,i)) {
236 hdr->actual_num = i;
237 return( false );
238 }
239 hdr->actual_num = i;
240
241 return( true );
242 }
243
244 static int skip2fontheader(FILE *pcl) {
245 return( skip2thingamy(pcl,")s"));
246 }
247
248 static void readttftables(FILE *pcl,struct ttf_header *hdr,int seg_size) {
249 int header_base = ftell(pcl);
250 int end_at = header_base+seg_size;
251 int num, i,j;
252
253 /* version = */ getlong(pcl);
254 num = getshort(pcl);
255 /* srange = */ getshort(pcl);
256 /* esel = */ getshort(pcl);
257 /* rshift = */ getshort(pcl);
258
259 hdr->subtable_cnt = num;
260 hdr->subtables = malloc(num*sizeof(struct subtables));
261 for ( i=j=0; i<num; ++i ) {
262 hdr->subtables[j].tag = getlong(pcl);
263 /* checksum = */ getlong(pcl);
264 hdr->subtables[j].offset = header_base+getlong(pcl);
265 hdr->subtables[j].len = getlong(pcl);
266 hdr->subtables[j].file = pcl;
267 hdr->subtables[j].closeme = false;
268 if ( hdr->subtables[j].tag != CHR('g','d','i','r'))
269 ++j;
270 else
271 --hdr->subtable_cnt;
272 }
273 /* head, hhea, hmtx and maxp required to be present */
274 /* We must synthesize: OS/2, name, cmap, glyf, loca, post */
275 /* We should remove 'gdir' (not part of truetype) */
276 fseek(pcl,end_at,SEEK_SET);
277 }
278
279 static struct subtables *gettable(struct ttf_header *hdr,int tag) {
280 int j;
281 for ( j=0; j<hdr->subtable_cnt && tag!=hdr->subtables[j].tag; ++j );
282 if ( j==hdr->subtable_cnt )
283 return( NULL );
284
285 return(&hdr->subtables[j]);
286 }
287
288 static int hasRequiredTables(struct ttf_header *hdr) {
289 static struct { int name, present; } tables[] = {
290 { CHR('h','e','a','d'),0 },
291 { CHR('h','h','e','a'),0 },
292 { CHR('h','m','t','x'),0 },
293 { CHR('m','a','x','p'),0 },
294 /* They can leave out 'gdir' if they want, I don't need it */
295 /* (and I'll remove it anyway) */
296 { 0,0 }};
297 int i;
298
299 for ( i=0; tables[i].name!=0; ++i ) {
300 if ( gettable(hdr,tables[i].name)==NULL )
301 return( false );
302 }
303 return( true );
304 }
305
306 static int readheaderformat(FILE *pcl,struct ttf_header *hdr) {
307 int ch, i;
308
309 memset(hdr,0,sizeof(*hdr));
310
311 fscanf(pcl,"%d", &hdr->header_size );
312 ch = getc(pcl);
313 if ( ch!='W' ) {
314 fprintf( stderr, "Bad font, font header command doesn't end in \"W\"\n" );
315 return( false );
316 }
317 hdr->fd_size = getshort(pcl);
318 hdr->header_format = getc(pcl);
319 hdr->fonttype = getc(pcl);
320 hdr->style = getc(pcl)<<8;
321 (void) getc(pcl);
322 hdr->baseline_pos = getshort(pcl);
323 hdr->cell_width = getshort(pcl);
324 hdr->cell_height = getshort(pcl);
325 hdr->orientation = getc(pcl);
326 hdr->spacing = getc(pcl);
327 hdr->symbol_set = getshort(pcl);
328 hdr->pitch = getshort(pcl);
329 hdr->height = getshort(pcl);
330 hdr->xheight = getshort(pcl);
331 hdr->widthtype = getc(pcl);
332 hdr->style |= getc(pcl);
333 hdr->strokeweight = getc(pcl);
334 hdr->typeface = getc(pcl);
335 hdr->typeface |= getc(pcl)<<8;
336 hdr->serifstyle = getc(pcl);
337 hdr->quality = getc(pcl);
338 hdr->placement = getc(pcl);
339 hdr->upos = getc(pcl);
340 hdr->uthick = getc(pcl);
341 hdr->textheight = getshort(pcl);
342 hdr->textwidth = getshort(pcl);
343 hdr->firstcode = getshort(pcl);
344 hdr->num_chars = getshort(pcl);
345 hdr->pitchx = getc(pcl);
346 hdr->heightx = getc(pcl);
347 hdr->capheight = getshort(pcl);
348 hdr->fontnumber = getlong(pcl);
349 for ( i=0; i<16; ++i )
350 hdr->fontname[i] = getc(pcl);
351 while ( i>1 && hdr->fontname[i-1]==' ' ) --i;
352 hdr->fontname[i] = '\0';
353 return( false );
354 }
355
356 static int readheaderttf(FILE *pcl,struct ttf_header *hdr) {
357 int i;
358 int seg_id, seg_size;
359
360 hdr->scale_factor = getshort(pcl);
361 hdr->mupos = (short) getshort(pcl);
362 hdr->muthick = getshort(pcl);
363 hdr->fontscaletech = getc(pcl);
364 hdr->variety = getc(pcl);
365
366 if ( hdr->fd_size<72 || hdr->header_format!=15 ) {
367 fprintf( stderr, "%s is not a PCL truetype font\n", hdr->fontname );
368 return( false );
369 }
370 /* Manual says there font descriptor size is at least 72, but no indication */
371 /* given about what these variable data might contain. The examples I've */
372 /* been given have the fd_size==72 */
373 for ( i=0 ; i<hdr->fd_size-72; ++i )
374 getc(pcl);
375
376 for (;;) {
377 seg_id = getshort(pcl);
378 seg_size = getshort(pcl);
379 if ( seg_id == (('C'<<8)|'P') ) {
380 hdr->copyright = malloc(seg_size+1);
381 fread(hdr->copyright,1,seg_size,pcl);
382 hdr->copyright[seg_size] = '\0';
383 } else if ( seg_id == (('C'<<8)|'C') ) {
384 if ( seg_size==8 )
385 fread(hdr->charcompl,1,8,pcl);
386 else
387 fseek(pcl,seg_size,SEEK_CUR);
388 } else if ( seg_id == (('P'<<8)|'A') ) {
389 if ( seg_size==10 )
390 fread(hdr->panose,1,10,pcl);
391 else
392 fseek(pcl,seg_size,SEEK_CUR);
393 } else if ( seg_id == (('G'<<8)|'T') ) {
394 readttftables(pcl,hdr,seg_size);
395 } else if ( seg_id == 0xffff || seg_id==EOF ) {
396 getshort(pcl);
397 break;
398 } else
399 fseek(pcl,seg_size,SEEK_CUR);
400 }
401 if ( !hasRequiredTables(hdr)) {
402 fprintf( stderr, "%s is missing a required ttf table\n", hdr->fontname );
403 return( false );
404 }
405
406 return( slurpglyphs(pcl,hdr));
407 }
408
409 /* ************************************************************************** */
410 /* *********************** Generate Missing ttf tables ********************** */
411 /* ************************************************************************** */
412
413 static void putshort(FILE *file,int sval) {
414 putc((sval>>8)&0xff,file);
415 putc(sval&0xff,file);
416 }
417
418 static void putlong(FILE *file,int val) {
419 putc((val>>24)&0xff,file);
420 putc((val>>16)&0xff,file);
421 putc((val>>8)&0xff,file);
422 putc(val&0xff,file);
423 }
424
425 static unsigned short gettableshort(struct ttf_header *hdr, int tag, int offset) {
426 /* Get a short at the indicated offset of the indicated table */
427 struct subtables *tbl = gettable(hdr,tag);
428 if ( tbl==NULL ) {
429 fprintf( stderr, "Missing required table: '%c%c%c%c'\n",
430 (char)((tag>>24)&0xff), (char)((tag>>16)&0xff),
431 (char)((tag>>8)&0xff), (char)(tag&0xff) );
432 return( -1 );
433 }
434 if ( offset+1>=tbl->len ) {
435 fprintf( stderr, "Attempt to read beyond the end of a table\n" );
436 return( -1 );
437 }
438 fseek(tbl->file,tbl->offset+offset,SEEK_SET);
439 return(getshort(tbl->file));
440 }
441
442 static void copydata(FILE *to,FILE *from,int offset,int len) {
443 int i, ch;
444
445 fseek( from,offset,SEEK_SET );
446 for ( i=0; i<len; ++i ) {
447 ch = getc(from);
448 putc(ch,to);
449 }
450 }
451
452 static void AddTable(struct ttf_header *hdr,struct subtables *table) {
453 struct subtables *new = malloc((hdr->subtable_cnt+1)*sizeof(struct subtables));
454 int i;
455
456 for ( i=0; i<hdr->subtable_cnt && hdr->subtables[i].tag<table->tag; ++i ) {
457 new[i] = hdr->subtables[i];
458 }
459 new[i] = *table;
460 for ( ; i<hdr->subtable_cnt ; ++i ) {
461 new[i+1] = hdr->subtables[i];
462 }
463 free(hdr->subtables);
464 hdr->subtables = new;
465 ++hdr->subtable_cnt;
466 }
467
468 static void regen_head(struct ttf_header *hdr, int forcelongloca) {
469 /* Just on the off chance the 'head' table says that we can use a short */
470 /* loca format, when we can't, we might need to redo head */
471 /* Oops, no. We always need to redo head to set checksumAdj to 0 */
472 struct subtables *head_tab = gettable(hdr,CHR('h','e','a','d'));
473 FILE *head;
474
475 if ( head_tab==NULL )
476 return;
477 if ( head_tab->len<54 )
478 return;
479
480 head = tmpfile();
481 copydata(head,head_tab->file,head_tab->offset,head_tab->len);
482 fseek(head,8,SEEK_SET);
483 putlong(head,0);
484 if ( forcelongloca ) {
485 fseek(head,50,SEEK_SET);
486 putshort(head,true);
487 fseek(head,0,SEEK_END);
488 }
489 head_tab->file = head;
490 head_tab->offset = 0;
491 head_tab->closeme = true;
492 }
493
494 static void create_cmap(struct ttf_header *hdr,struct glyph *glyphs,int numGlyphs) {
495 FILE *cmap = tmpfile();
496 struct subtables table;
497 /* We are given data for a unicode table */
498 /* I'm not going to try to figure out a mac roman table */
499 /* I suppose I might as well add a mac unicode table */
500 /* taken pretty much directly from pfaedit */
501
502 {
503 int i,j,l, mspos;
504 int segcnt, delta, rpos;
505 struct cmapseg { unsigned short start, end; unsigned short delta; unsigned short rangeoff; } *cmapseg;
506 unsigned short *ranges;
507 unsigned int *avail = malloc(65536*sizeof(unsigned int));
508 memset(avail,0xff,65536*sizeof(unsigned int));
509 for ( i=0; i<numGlyphs; ++i ) {
510 if ( glyphs[i].unicode<0xffff )
511 avail[glyphs[i].unicode] = glyphs[i].glyph;
512 }
513
514 j = -1;
515 for ( i=segcnt=0; i<65536; ++i ) {
516 if ( avail[i]!=0xffffffff && j==-1 ) {
517 j=i;
518 ++segcnt;
519 } else if ( j!=-1 && avail[i]==0xffffffff )
520 j = -1;
521 }
522 cmapseg = calloc(segcnt+1,sizeof(struct cmapseg));
523 ranges = malloc(hdr->num_chars*sizeof(short));
524 j = -1;
525 for ( i=segcnt=0; i<65536; ++i ) {
526 if ( avail[i]!=0xffffffff && j==-1 ) {
527 j=i;
528 cmapseg[segcnt].start = j;
529 ++segcnt;
530 } else if ( j!=-1 && avail[i]==0xffffffff ) {
531 cmapseg[segcnt-1].end = i-1;
532 j = -1;
533 }
534 }
535 if ( j!=-1 )
536 cmapseg[segcnt-1].end = i-1;
537 /* create a dummy segment to mark the end of the table */
538 cmapseg[segcnt].start = cmapseg[segcnt].end = 0xffff;
539 cmapseg[segcnt++].delta = 1;
540 rpos = 0;
541 for ( i=0; i<segcnt-1; ++i ) {
542 l = avail[cmapseg[i].start];
543 delta = l-cmapseg[i].start;
544 for ( j=cmapseg[i].start; j<=cmapseg[i].end; ++j ) {
545 l = avail[j];
546 if ( delta != l-j )
547 break;
548 }
549 if ( j>cmapseg[i].end )
550 cmapseg[i].delta = delta;
551 else {
552 cmapseg[i].rangeoff = (rpos + (segcnt-i)) * sizeof(short);
553 for ( j=cmapseg[i].start; j<=cmapseg[i].end; ++j ) {
554 l = avail[j];
555 ranges[rpos++] = l;
556 }
557 }
558 }
559 free(avail);
560
561 /* Two encoding table pointers, one for ms, one for mac unicode */
562 /* (same as ms) */
563 putshort(cmap,0); /* version */
564 putshort(cmap,2); /* num tables */
565
566 mspos = 2*sizeof(unsigned short)+2*(2*sizeof(unsigned short)+sizeof(unsigned int));
567 /* big mac table, just a copy of the ms table */
568 putshort(cmap,0); /* mac unicode platform */
569 putshort(cmap,3); /* Unicode 2.0 */
570 putlong(cmap,mspos);
571
572 putshort(cmap,3); /* ms platform */
573 putshort(cmap,1); /* plat specific enc, Unicode */
574 putlong(cmap,mspos); /* offset from tab start to sub tab start */
575
576 if ( ftell(cmap)!=mspos )
577 fprintf( stderr, "Internal error, encoding table not where expected\n" );
578
579 putshort(cmap,4); /* format */
580 putshort(cmap,(8+4*segcnt+rpos)*sizeof(short));
581 putshort(cmap,0); /* language/version */
582 putshort(cmap,2*segcnt); /* segcnt */
583 for ( j=0,i=1; i<=segcnt; i<<=1, ++j );
584 putshort(cmap,i); /* 2*2^floor(log2(segcnt)) */
585 putshort(cmap,j-1);
586 putshort(cmap,2*segcnt-i);
587 for ( i=0; i<segcnt; ++i )
588 putshort(cmap,cmapseg[i].end);
589 putshort(cmap,0);
590 for ( i=0; i<segcnt; ++i )
591 putshort(cmap,cmapseg[i].start);
592 for ( i=0; i<segcnt; ++i )
593 putshort(cmap,cmapseg[i].delta);
594 for ( i=0; i<segcnt; ++i )
595 putshort(cmap,cmapseg[i].rangeoff);
596 for ( i=0; i<rpos; ++i )
597 putshort(cmap,ranges[i]);
598 free(ranges);
599 free(cmapseg);
600 }
601
602 table.len = ftell(cmap);
603 table.offset = 0;
604 table.tag = CHR('c','m','a','p');
605 table.file = cmap;
606 table.closeme = true;
607 AddTable(hdr,&table);
608 }
609
610 /* this table taken from PfaEdit */
611 static int uniranges[][3] = {
612 { 0x0, 0x7f, 0 }, /* Basic Latin */
613 { 0x80, 0xff, 1 }, /* Latin-1 Supplement */
614 { 0x100, 0x17f, 2 }, /* Latin Extended-A */
615 { 0x180, 0x24f, 3 }, /* Latin Extended-B */
616 { 0x250, 0x2af, 4 }, /* IPA Extensions */
617 { 0x2b0, 0x2ff, 5 }, /* Spacing Modifier Letters */
618 { 0x300, 0x36f, 6 }, /* Combining Diacritical Marks */
619 { 0x370, 0x3ff, 7 }, /* Greek and Coptic */
620 { 0x400, 0x52f, 9 }, /* Cyrillic, Cyrillic Supplement */
621 { 0x530, 0x58f, 10 }, /* Armenian */
622 { 0x590, 0x5ff, 11 }, /* Hebrew */
623 { 0x600, 0x6ff, 13 }, /* Arabic */
624 { 0x700, 0x74f, 71 }, /* Syriac */
625 { 0x750, 0x77f, 13 }, /* Arabic Supplement */
626 { 0x780, 0x7bf, 72 }, /* Thaana */
627 { 0x7c0, 0x7ff, 14 }, /* NKo */
628 { 0x900, 0x97f, 15 }, /* Devanagari */
629 { 0x980, 0x9ff, 16 }, /* Bengali */
630 { 0xa00, 0xa7f, 17 }, /* Gurmukhi */
631 { 0xa80, 0xaff, 18 }, /* Gujarati */
632 { 0xb00, 0xb7f, 19 }, /* Oriya */
633 { 0xb80, 0xbff, 20 }, /* Tamil */
634 { 0xc00, 0xc7f, 21 }, /* Telugu */
635 { 0xc80, 0xcff, 22 }, /* Kannada */
636 { 0xd00, 0xd7f, 23 }, /* Malayalam */
637 { 0xd80, 0xdff, 73 }, /* Sinhala */
638 { 0xe00, 0xe7f, 24 }, /* Thai */
639 { 0xe80, 0xeff, 25 }, /* Lao */
640 { 0xf00, 0xfff, 70 }, /* Tibetan */
641 { 0x1000, 0x109f, 74 }, /* Myanmar */
642 { 0x10a0, 0x10ff, 26 }, /* Georgian */
643 { 0x1100, 0x11ff, 28 }, /* Hangul Jamo */
644 { 0x1200, 0x139f, 75 }, /* Ethiopic, Ethiopic Supplement */
645 { 0x13a0, 0x13ff, 76 }, /* Cherokee */
646 { 0x1400, 0x167f, 77 }, /* Unified Canadian Aboriginal Syllabics */
647 { 0x1680, 0x169f, 78 }, /* Ogham */
648 { 0x16a0, 0x16ff, 79 }, /* Runic */
649 { 0x1700, 0x177f, 84 }, /* Tagalog, Hanunoo, Buhid, Tagbanwa */
650 { 0x1780, 0x17ff, 80 }, /* Khmer */
651 { 0x1800, 0x18af, 81 }, /* Mongolian */
652 { 0x1900, 0x194f, 93 }, /* Limbu */
653 { 0x1950, 0x197f, 94 }, /* Tai Le */
654 { 0x1980, 0x19df, 95 }, /* New Tai Lue */
655 { 0x19e0, 0x19ff, 80 }, /* Khmer Symbols */
656 { 0x1a00, 0x1a1f, 96 }, /* Buginese */
657 { 0x1b00, 0x1b7f, 27 }, /* Balinese */
658 { 0x1b80, 0x1bbf, 112 }, /* Sundanese */
659 { 0x1c00, 0x1c4f, 113 }, /* Lepcha */
660 { 0x1c50, 0x1c7f, 114 }, /* Ol Chiki */
661 { 0x1d00, 0x1dbf, 4 }, /* Phonetic Extensions, Phonetic Extensions Supplement */
662 { 0x1dc0, 0x1dff, 6 }, /* Combining Diacritical Marks Supplement */
663 { 0x1e00, 0x1eff, 29 }, /* Latin Extended Additional */
664 { 0x1f00, 0x1fff, 30 }, /* Greek Extended */
665 { 0x2000, 0x206f, 31 }, /* General Punctuation */
666 { 0x2070, 0x209f, 32 }, /* Superscripts and Subscripts */
667 { 0x20a0, 0x20cf, 33 }, /* Currency Symbols */
668 { 0x20d0, 0x20ff, 34 }, /* Combining Diacritical Marks for Symbols */
669 { 0x2100, 0x214f, 35 }, /* Letterlike Symbols */
670 { 0x2150, 0x218f, 36 }, /* Number Forms */
671 { 0x2190, 0x21ff, 37 }, /* Arrows */
672 { 0x2200, 0x22ff, 38 }, /* Mathematical Operators */
673 { 0x2300, 0x23ff, 39 }, /* Miscellaneous Technical */
674 { 0x2400, 0x243f, 40 }, /* Control Pictures */
675 { 0x2440, 0x245f, 41 }, /* Optical Character Recognition */
676 { 0x2460, 0x24ff, 42 }, /* Enclosed Alphanumerics */
677 { 0x2500, 0x257f, 43 }, /* Box Drawing */
678 { 0x2580, 0x259f, 44 }, /* Block Elements */
679 { 0x25a0, 0x25ff, 45 }, /* Geometric Shapes */
680 { 0x2600, 0x26ff, 46 }, /* Miscellaneous Symbols */
681 { 0x2700, 0x27bf, 47 }, /* Dingbats */
682 { 0x27c0, 0x27ef, 38 }, /* Miscellaneous Mathematical Symbols-A */
683 { 0x27f0, 0x27ff, 37 }, /* Supplemental Arrows-A */
684 { 0x2800, 0x28ff, 82 }, /* Braille Patterns */
685 { 0x2900, 0x297f, 37 }, /* Supplemental Arrows-B */
686 { 0x2980, 0x2aff, 38 }, /* Miscellaneous Mathematical Symbols-B, Supplemental Mathematical Operators */
687 { 0x2b00, 0x2bff, 37 }, /* Miscellaneous Symbols and Arrows */
688 { 0x2c00, 0x2c5f, 97 }, /* Glagolitic */
689 { 0x2c60, 0x2c7f, 29 }, /* Latin Extended-C */
690 { 0x2c80, 0x2cff, 8 }, /* Coptic */
691 { 0x2d00, 0x2d2f, 26 }, /* Georgian Supplement */
692 { 0x2d30, 0x2d7f, 98 }, /* Tifinagh */
693 { 0x2d80, 0x2ddf, 75 }, /* Ethiopic Extended */
694 { 0x2de0, 0x2dff, 9 }, /* Cyrillic Extended-A */
695 { 0x2e00, 0x2e7f, 31 }, /* Supplemental Punctuation */
696 { 0x2e80, 0x2fdf, 59 }, /* CJK Radicals Supplement, Kangxi Radicals */
697 { 0x2ff0, 0x303f, 59 }, /* Ideographic Description Characters, CJK Symbols and Punctuation */
698 { 0x3040, 0x309f, 49 }, /* Hiragana */
699 { 0x30a0, 0x30ff, 50 }, /* Katakana */
700 { 0x3100, 0x312f, 51 }, /* Bopomofo */
701 { 0x3130, 0x318f, 52 }, /* Hangul Compatibility Jamo */
702 { 0x3190, 0x319f, 59 }, /* Kanbun */
703 { 0x31a0, 0x31bf, 51 }, /* Bopomofo Extended */
704 { 0x31c0, 0x31ef, 61 }, /* CJK Strokes */
705 { 0x31f0, 0x31ff, 50 }, /* Katakana Phonetic Extensions */
706 { 0x3200, 0x32ff, 54 }, /* Enclosed CJK Letters and Months */
707 { 0x3300, 0x33ff, 55 }, /* CJK Compatibility */
708 { 0x3400, 0x4dbf, 59 }, /* CJK Unified Ideographs Extension A */
709 { 0x4dc0, 0x4dff, 99 }, /* Yijing Hexagram Symbols */
710 { 0x4e00, 0x9fff, 59 }, /* CJK Unified Ideographs */
711 { 0xa000, 0xa4cf, 83 }, /* Yi Syllables, Yi Radicals */
712 { 0xa500, 0xa63f, 12 }, /* Vai */
713 { 0xa640, 0xa69f, 9 }, /* Cyrillic Extended-B */
714 { 0xa700, 0xa71f, 5 }, /* Modifier Tone Letters */
715 { 0xa720, 0xa7ff, 29 }, /* Latin Extended-D */
716 { 0xa800, 0xa82f, 100 }, /* Syloti Nagri */
717 { 0xa840, 0xa87f, 53 }, /* Phags-pa */
718 { 0xa880, 0xa8df, 115 }, /* Saurashtra */
719 { 0xa900, 0xa92f, 116 }, /* Kayah Li */
720 { 0xa930, 0xa95f, 117 }, /* Rejang */
721 { 0xaa00, 0xaa5f, 118 }, /* Cham */
722 { 0xac00, 0xd7af, 56 }, /* Hangul Syllables */
723 { 0xe000, 0xf8ff, 60 }, /* Private Use Area */
724 { 0xf900, 0xfaff, 61 }, /* CJK Compatibility Ideographs */
725 { 0xfb00, 0xfb4f, 62 }, /* Alphabetic Presentation Forms */
726 { 0xfb50, 0xfdff, 63 }, /* Arabic Presentation Forms-A */
727 { 0xfe00, 0xfe0f, 91 }, /* Variation Selectors */
728 { 0xfe10, 0xfe1f, 65 }, /* Vertical Forms */
729 { 0xfe20, 0xfe2f, 64 }, /* Combining Half Marks */
730 { 0xfe30, 0xfe4f, 65 }, /* CJK Compatibility Forms */
731 { 0xfe50, 0xfe6f, 66 }, /* Small Form Variants */
732 { 0xfe70, 0xfeff, 67 }, /* Arabic Presentation Forms-B */
733 { 0xff00, 0xffef, 68 }, /* Halfwidth and Fullwidth Forms */
734 { 0xfff0, 0xffff, 69 }, /* Specials */
735 { 0x10000, 0x1013f, 101 }, /* Linear B Syllabary, Linear B Ideograms, Aegean Numbers */
736 { 0x10190, 0x101cf, 119 }, /* Ancient Symbols */
737 { 0x101d0, 0x101ff, 120 }, /* Phaistos Disc */
738 { 0x10280, 0x102df, 121 }, /* Lycian, Carian */
739 { 0x10300, 0x1032f, 85 }, /* Old Italic */
740 { 0x10330, 0x1034f, 86 }, /* Gothic */
741 { 0x10380, 0x1039f, 103 }, /* Ugaritic */
742 { 0x103a0, 0x103df, 104 }, /* Old Persian */
743 { 0x10400, 0x1044f, 87 }, /* Deseret */
744 { 0x10450, 0x1047f, 105 }, /* Shavian */
745 { 0x10480, 0x104af, 106 }, /* Osmanya */
746 { 0x10800, 0x1083f, 107 }, /* Cypriot Syllabary */
747 { 0x10900, 0x1091f, 58 }, /* Phoenician */
748 { 0x10920, 0x1093f, 121 }, /* Lydian */
749 { 0x10a00, 0x10a5f, 108 }, /* Kharoshthi */
750 { 0x12000, 0x1247f, 110 }, /* Cuneiform, Cuneiform Numbers and Punctuation */
751 { 0x1d000, 0x1d24f, 88 }, /* Byzantine Musical Symbols, Musical Symbols, Ancient Greek Musical Notation */
752 { 0x1d300, 0x1d35f, 109 }, /* Tai Xuan Jing Symbols */
753 { 0x1d360, 0x1d37f, 111 }, /* Counting Rod Numerals */
754 { 0x1d400, 0x1d7ff, 89 }, /* Mathematical Alphanumeric Symbols */
755 { 0x1f000, 0x1f09f, 122 }, /* Mahjong Tiles, Domino Tiles */
756 { 0x20000, 0x2a6df, 59 }, /* CJK Unified Ideographs Extension B */
757 { 0x2f800, 0x2fa1f, 61 }, /* CJK Compatibility Ideographs Supplement */
758 { 0xe0000, 0xe007f, 92 }, /* Tags */
759 { 0xe0100, 0xe01ef, 91 }, /* Variation Selectors Supplement */
760 { 0xf0000, 0xffffd, 90 }, /* Supplementary Private Use Area-A */
761 { 0x100000, 0x10fffd, 90 }, /* Supplementary Private Use Area-B */
762 };
763
764 /* this routine taken from PfaEdit */
765 static void SetRanges(int urange[4],int CodePage[2],int unicode) {
766 int j;
767
768 for ( j=0; j<sizeof(uniranges)/sizeof(uniranges[0]); ++j )
769 if ( unicode>=uniranges[j][0] && unicode<=uniranges[j][1] ) {
770 int bit = uniranges[j][2];
771 urange[bit>>5] |= (1<<(bit&31));
772 break;
773 }
774
775 if ( unicode=='A' )
776 CodePage[1] |= 1<<31; /* US (Ascii I assume) */
777 else if ( unicode==0xde ) {
778 CodePage[0] |= 1<<0; /* latin1 */
779 CodePage[1] |= 1<<30; /* WE/Latin1 */
780 } else if ( unicode==0xc3 )
781 CodePage[1] |= 1<<18; /* MS-DOS Nordic */
782 else if ( unicode==0xe9 )
783 CodePage[1] |= 1<<20; /* MS-DOS Canadian French */
784 else if ( unicode==0xf5 )
785 CodePage[1] |= 1<<23; /* MS-DOS Portuguese */
786 else if ( unicode==0xfe )
787 CodePage[1] |= 1<<22; /* MS-DOS Icelandic */
788 else if ( unicode==0x13d ) {
789 CodePage[0] |= 1<<1; /* latin2 */
790 CodePage[1] |= 1<<26; /* latin2 */
791 } else if ( unicode==0x411 ) {
792 CodePage[0] |= 1<<2; /* cyrillic */
793 CodePage[1] |= 1<<17; /* MS DOS Russian */
794 CodePage[1] |= 1<<25; /* IBM Cyrillic */
795 } else if ( unicode==0x386 ) {
796 CodePage[0] |= 1<<3; /* greek */
797 CodePage[1] |= 1<<16; /* IBM Greek */
798 CodePage[1] |= 1<<28; /* Greek, former 437 G */
799 } else if ( unicode==0x130 ) {
800 CodePage[0] |= 1<<4; /* turkish */
801 CodePage[1] |= 1<<24; /* IBM turkish */
802 } else if ( unicode==0x5d0 ) {
803 CodePage[0] |= 1<<5; /* hebrew */
804 CodePage[1] |= 1<<21; /* hebrew */
805 } else if ( unicode==0x631 ) {
806 CodePage[0] |= 1<<6; /* arabic */
807 CodePage[1] |= 1<<19; /* arabic */
808 CodePage[1] |= 1<<29; /* arabic; ASMO 708 */
809 } else if ( unicode==0x157 ) {
810 CodePage[0] |= 1<<7; /* baltic */
811 CodePage[1] |= 1<<27; /* baltic */
812 } else if ( unicode==0xe45 )
813 CodePage[0] |= 1<<16; /* thai */
814 else if ( unicode==0x30a8 )
815 CodePage[0] |= 1<<17; /* japanese */
816 else if ( unicode==0x3105 )
817 CodePage[0] |= 1<<18; /* simplified chinese */
818 else if ( unicode==0x3131 )
819 CodePage[0] |= 1<<19; /* korean wansung */
820 else if ( unicode==0xacf4 )
821 CodePage[0] |= 1<<21; /* korean Johab */
822 else if ( unicode==0x21d4 )
823 CodePage[0] |= 1<<31; /* symbol */
824 }
825
826 static void create_OS2(struct ttf_header *hdr,struct glyph **orderedglyphs,int numGlyphs) {
827 FILE *os2 = tmpfile();
828 struct subtables table;
829 int i;
830 int uranges[4], codeRanges[2];
831 int minu, maxu;
832 int fsstyle=0;
833
834 if ( strstr(hdr->fontname,"Bd")!=NULL )
835 fsstyle |= (1<<5);
836 if ( strstr(hdr->fontname,"It")!=NULL )
837 fsstyle |= 1;
838
839 putshort(os2,0x0002); /* version 2 */
840 putshort(os2,hdr->textwidth); /* supposed to be average character width, but average lower case is close */
841 putshort(os2,hdr->strokeweight==0?400:
842 hdr->strokeweight==1?500:
843 hdr->strokeweight==2?600:
844 hdr->strokeweight==3?700:
845 hdr->strokeweight==4?800:
846 hdr->strokeweight==5?900:
847 hdr->strokeweight==6?950:
848 hdr->strokeweight>=7?990:
849 hdr->strokeweight>=-3?300:
850 hdr->strokeweight==-4?200:
851 hdr->strokeweight==-5?100:
852 hdr->strokeweight==-6? 50: 10); /* Weight class */
853 putshort(os2,hdr->widthtype+5); /* Width class */
854 putshort(os2,0x0000); /* Installable embedding (fstype) */
855 putshort(os2,hdr->scale_factor*2/3); /* ySubscript XSize */
856 putshort(os2,hdr->scale_factor*2/3); /* ySubscript YSize */
857 putshort(os2,0); /* ySubscript XOffset */
858 putshort(os2,hdr->scale_factor*1/3); /* ySubscript YOffset */
859 putshort(os2,hdr->scale_factor*2/3); /* ySuperscript XSize */
860 putshort(os2,hdr->scale_factor*2/3); /* ySuperscript YSize */
861 putshort(os2,0); /* ySuperscript XOffset */
862 putshort(os2,-hdr->scale_factor*1/3); /* ySuperscript YOffset */
863 putshort(os2,hdr->scale_factor*102/2048); /* strikeout width */
864 putshort(os2,hdr->scale_factor*530/2048); /* strikeout pos */
865 putshort(os2,0); /* no idea about ibm families */
866 /* if we have panose data, use it, else the field is initted to 0 which */
867 /* is the default (or unknown, or something like that) */
868 for ( i=0; i<10; ++i )
869 putc(hdr->panose[i],os2);
870
871 memset(uranges,0,sizeof(uranges)); memset(codeRanges,0,sizeof(codeRanges));
872 minu = 0xffff;
873 maxu = 0x0000;
874 for ( i=0; i<hdr->actual_num; ++i ) if ( hdr->glyphs[i].unicode<0xffff ) {
875 SetRanges(uranges,codeRanges,hdr->glyphs[i].unicode);
876 if ( hdr->glyphs[i].unicode<minu )
877 minu = hdr->glyphs[i].unicode;
878 if ( hdr->glyphs[i].unicode>maxu && hdr->glyphs[i].unicode<0xffff )
879 maxu = hdr->glyphs[i].unicode;
880 }
881
882 putlong(os2,uranges[0]);
883 putlong(os2,uranges[1]);
884 putlong(os2,uranges[2]);
885 putlong(os2,uranges[3]);
886 putc(' ',os2);putc(' ',os2);putc(' ',os2);putc(' ',os2); /* give up on vendor id */
887 putshort(os2,fsstyle);
888 putshort(os2,minu); /* glyph index of minimum unicode */
889 putshort(os2,maxu); /* glyph index of maximum unicode */
890 putshort(os2,gettableshort(hdr,CHR('h','h','e','a'),4)); /* Typo ascender, approx with apple's ascender (even though that's wrong) */
891 putshort(os2,gettableshort(hdr,CHR('h','h','e','a'),6)); /* Typo descender, approx with apple's descender (even though that's wrong) */
892 putshort(os2,gettableshort(hdr,CHR('h','h','e','a'),8)); /* Typo linegap, approx with apple's linegap (even though that's wrong) */
893 putshort(os2,gettableshort(hdr,CHR('h','e','a','d'),42)); /* win ascent, approx with apple's ascender (even though that's wrong) */
894 putshort(os2,-gettableshort(hdr,CHR('h','e','a','d'),38)); /* win descent, approx with apple's descender (even though that's wrong) */
895 putlong(os2,codeRanges[0]);
896 putlong(os2,codeRanges[1]);
897 putshort(os2,hdr->heightx); /* xheight */
898 putshort(os2,hdr->capheight); /* cap height */
899 putshort(os2,0); /* default char */
900 putshort(os2,' '); /* break enc */
901 putshort(os2,1); /* maxcontext */
902
903 table.len = ftell(os2);
904 table.offset = 0;
905 table.tag = CHR('O','S','/','2');
906 table.file = os2;
907 table.closeme = true;
908 AddTable(hdr,&table);
909 }
910
911 static void create_glyp_loca(FILE *pcl,struct ttf_header *hdr) {
912 int numGlyphs = gettableshort(hdr,CHR('m','a','x','p'),4); /* num glyph offset in maxp */
913 int loca_is_big = gettableshort(hdr,CHR('h','e','a','d'),50);
914 int *locas = malloc((numGlyphs+1)*sizeof(int));
915 struct glyph **orderedglyphs = calloc(numGlyphs,sizeof(struct glyph *));
916 int i;
917 FILE *glyf = tmpfile(), *loca = tmpfile();
918 struct subtables table;
919
920 for ( i=0; i<hdr->actual_num; ++i ) {
921 if ( hdr->glyphs[i].glyph>=numGlyphs ) {
922 fprintf( stderr, "Glyph outside of range specified by maxp.\n" );
923 } else
924 orderedglyphs[hdr->glyphs[i].glyph] = &hdr->glyphs[i];
925 }
926
927 for ( i=0; i<numGlyphs; ++i ) {
928 locas[i] = ftell(glyf);
929 if ( orderedglyphs[i]!=NULL ) {
930 copydata(glyf,pcl,orderedglyphs[i]->offset,orderedglyphs[i]->len);
931 /* At most one continuation... */
932 if ( orderedglyphs[i]->continuation != NULL )
933 copydata(glyf,pcl,orderedglyphs[i]->continuation->offset,orderedglyphs[i]->continuation->len);
934 }
935 if ( ftell(glyf)&1 ) /* short loca tables need everything word aligned */
936 putc('\0',glyf);
937 }
938
939 table.len = ftell(glyf);
940 table.offset = 0;
941 table.tag = CHR('g','l','y','f');
942 table.file = glyf;
943 table.closeme = true;
944 AddTable(hdr,&table);
945
946 locas[i] = ftell(glyf);
947 regen_head(hdr,!loca_is_big && locas[i]>2*65535);
948 if ( locas[i]>2*65535 )
949 loca_is_big = true;
950 if ( loca_is_big ) {
951 for ( i=0; i<=numGlyphs; ++i )
952 putlong(loca,locas[i]);
953 } else {
954 for ( i=0; i<=numGlyphs; ++i )
955 putshort(loca,locas[i]/2);
956 }
957
958 table.len = ftell(loca);
959 table.offset = 0;
960 table.tag = CHR('l','o','c','a');
961 table.file = loca;
962 table.closeme = true;
963 AddTable(hdr,&table);
964
965 create_cmap(hdr,hdr->glyphs,hdr->actual_num);
966 create_OS2(hdr,orderedglyphs,numGlyphs);
967 free(orderedglyphs);
968 free(locas);
969 }
970
971 static void create_post(struct ttf_header *hdr) {
972 FILE *post = tmpfile();
973 struct subtables table;
974
975 putlong(post,0x00030000); /* version 3, no glyph name data */
976 putlong(post,0x00000000); /* assume italic angle zero */
977 putshort(post,hdr->mupos);
978 putshort(post,hdr->muthick);
979 putlong(post,!hdr->spacing); /* monospace = !proportional*/
980 putlong(post,0); /* no idea about vm requirements */
981 putlong(post,0); /* no idea about vm requirements */
982 putlong(post,0); /* no idea about vm requirements */
983 putlong(post,0); /* no idea about vm requirements */
984
985 table.len = ftell(post);
986 table.offset = 0;
987 table.tag = CHR('p','o','s','t');
988 table.file = post;
989 table.closeme = true;
990 AddTable(hdr,&table);
991 }
992
993 static void create_name(struct ttf_header *hdr) {
994 FILE *name = tmpfile();
995 struct subtables table;
996 /* Tiny name table with one entry containing the font's name (that's all we know) */
997 /* oops, we might have a copyright */
998 int cnt = 5+(hdr->copyright!=NULL);
999 int off = 0;
1000 char *pt, *pt2;
1001 char family[20], style[20], *version="Version 1.0", unique[32];
1002
1003 /* Guess at font family, style */
1004 strcpy(family,hdr->fontname);
1005 for ( pt=pt2=family; *pt!='\0'; ++pt )
1006 if ( *pt>' ' && *pt<0x7f && *pt!='(' && *pt!=')' && *pt!='{' && *pt!='}' &&
1007 *pt!='[' && *pt!=']' && *pt!='<' && *pt!='>' && *pt!='%' &&
1008 *pt!='/' )
1009 *pt2++ = *pt;
1010 *pt2 = '\0';
1011 if ( (pt=strstr(family,"BdIt"))!=NULL )
1012 strcpy(style,"Bold Italic");
1013 else if ( (pt=strstr(family,"Bd"))!=NULL )
1014 strcpy(style,"Bold");
1015 else if ( (pt=strstr(family,"It"))!=NULL )
1016 strcpy(style,"Italic");
1017 else
1018 strcpy(style,"Regular");
1019 if ( pt!=NULL ) *pt='\0';
1020 sprintf( unique, "pcl2ttf: %s", hdr->fontname );
1021
1022 putshort(name,0); /* format 0 */
1023 putshort(name,cnt); /* only one name */
1024 putshort(name,6+cnt*12); /* offset to strings. 6 bytes of header, 12bytes for name records */
1025
1026 if ( hdr->copyright!=NULL ) {
1027 putshort(name,3); /* Platform = MS */
1028 putshort(name,1); /* MS Unicode */
1029 putshort(name,0x409); /* US English */
1030 putshort(name,0); /* Copyright string */
1031 putshort(name,2*strlen(hdr->copyright)); /* It's unicode so 2 bytes/char */
1032 putshort(name,0); /* start of string table */
1033 off = 2*strlen(hdr->copyright);
1034 }
1035 putshort(name,3); /* Platform = MS */
1036 putshort(name,1); /* MS Unicode */
1037 putshort(name,0x409); /* US English */
1038 putshort(name,1); /* family */
1039 putshort(name,2*strlen(family)); /* It's unicode so 2 bytes/char */
1040 putshort(name,off); /* offset */
1041 off += 2*strlen(family);
1042 putshort(name,3); /* Platform = MS */
1043 putshort(name,1); /* MS Unicode */
1044 putshort(name,0x409); /* US English */
1045 putshort(name,2); /* style */
1046 putshort(name,2*strlen(style)); /* It's unicode so 2 bytes/char */
1047 putshort(name,off); /* offset */
1048 off += 2*strlen(style);
1049 putshort(name,3); /* Platform = MS */
1050 putshort(name,1); /* MS Unicode */
1051 putshort(name,0x409); /* US English */
1052 putshort(name,3); /* style */
1053 putshort(name,2*strlen(unique)); /* It's unicode so 2 bytes/char */
1054 putshort(name,off); /* offset */
1055 off += 2*strlen(unique);
1056 putshort(name,3); /* Platform = MS */
1057 putshort(name,1); /* MS Unicode */
1058 putshort(name,0x409); /* US English */
1059 putshort(name,4); /* full fontname */
1060 putshort(name,2*strlen(hdr->fontname)); /* It's unicode so 2 bytes/char */
1061 putshort(name,off); /* offset */
1062 off += 2*strlen(hdr->fontname);
1063 putshort(name,3); /* Platform = MS */
1064 putshort(name,1); /* MS Unicode */
1065 putshort(name,0x409); /* US English */
1066 putshort(name,5); /* style */
1067 putshort(name,2*strlen(version)); /* It's unicode so 2 bytes/char */
1068 putshort(name,off); /* offset */
1069 off += 2*strlen(version);
1070
1071
1072 if ( hdr->copyright!=NULL ) {
1073 for ( pt=hdr->copyright; *pt; ++pt ) {
1074 putc('\0',name);
1075 putc(*pt,name);
1076 }
1077 }
1078 for ( pt=family; *pt; ++pt ) {
1079 putc('\0',name);
1080 putc(*pt,name);
1081 }
1082 for ( pt=style; *pt; ++pt ) {
1083 putc('\0',name);
1084 putc(*pt,name);
1085 }
1086 for ( pt=unique; *pt; ++pt ) {
1087 putc('\0',name);
1088 putc(*pt,name);
1089 }
1090 for ( pt=hdr->fontname; *pt; ++pt ) {
1091 putc('\0',name);
1092 putc(*pt,name);
1093 }
1094 for ( pt=version; *pt; ++pt ) {
1095 putc('\0',name);
1096 putc(*pt,name);
1097 }
1098
1099 table.len = ftell(name);
1100 table.offset = 0;
1101 table.tag = CHR('n','a','m','e');
1102 table.file = name;
1103 table.closeme = true;
1104 AddTable(hdr,&table);
1105 }
1106
1107 static void create_missingtables(FILE *pcl,struct ttf_header *hdr) {
1108 create_glyp_loca(pcl,hdr); /* and cmap and os2 */
1109 create_post(hdr);
1110 create_name(hdr);
1111 }
1112
1113 /* ************************************************************************** */
1114 /* ************************** Output final ttf file ************************* */
1115 /* ************************************************************************** */
1116
1117 static int filecheck(FILE *file, int off, int len) {
1118 unsigned int sum = 0, chunk;
1119 int i, len4 = len/4;
1120
1121 fseek(file,off,SEEK_SET);
1122 for ( i=0; i<len4; ++i ) {
1123 chunk = getlong(file);
1124 if ( feof(file))
1125 break;
1126 sum += chunk;
1127 }
1128 if ( (len&3)==1 )
1129 chunk = getc(file)<<24;
1130 else if ( (len&3)==2 )
1131 chunk = getshort(file)<<16;
1132 else if ( (len&3)==3 ) {
1133 chunk = getshort(file)<<16;
1134 chunk |= getc(file)<<8;
1135 } else
1136 chunk = 0;
1137 sum += chunk;
1138 return( sum );
1139 }
1140
1141 static void dumpfont(struct ttf_header *hdr) {
1142 int offset, i,n=hdr->subtable_cnt, sr, head_index;
1143 FILE *ttf;
1144 char buffer[32], *pt, *fpt;
1145 struct stat statb;
1146
1147 fpt = hdr->fontname;
1148 for ( pt=buffer; *fpt; ++fpt )
1149 if ( *fpt!=' ' && *fpt!='/' )
1150 *pt++ = *fpt;
1151 strcpy(pt,".ttf");
1152 ttf = fopen(buffer,"wb+");
1153 if ( ttf==NULL ) {
1154 fprintf( stderr, "Failed to open %s\n", buffer );
1155 return;
1156 } else
1157 printf( "Created %s\n", buffer );
1158 for ( i=0; i<n; ++i )
1159 hdr->subtables[i].checksum = filecheck(hdr->subtables[i].file,
1160 hdr->subtables[i].offset,hdr->subtables[i].len);
1161 offset = 12+16*n;
1162 for ( i=0; i<n; ++i ) {
1163 hdr->subtables[i].newoff = offset;
1164 offset += (hdr->subtables[i].len+3)&~3;
1165 }
1166
1167 /* Dump truetype table header */
1168 sr = (n<16?8:n<32?16:n<64?32:64)*16;
1169 putlong(ttf,0x00010000); /* Version */
1170 putshort(ttf,n); /* table count */
1171 putshort(ttf,sr); /* Search range */
1172 putshort(ttf,(n<16?3:n<32?4:n<64?5:6));
1173 putshort(ttf,n*16-sr);
1174
1175 /* Dump table of contents */
1176 head_index = -1;
1177 for ( i=0; i<n; ++i ) {
1178 if ( hdr->subtables[i].tag==CHR('h','e','a','d') )
1179 head_index = i;
1180 putlong(ttf,hdr->subtables[i].tag);
1181 putlong(ttf,hdr->subtables[i].checksum);
1182 putlong(ttf,hdr->subtables[i].newoff);
1183 putlong(ttf,hdr->subtables[i].len);
1184 }
1185
1186 /* Dump each table */
1187 for ( i=0; i<n; ++i ) {
1188 if ( ftell(ttf)!=hdr->subtables[i].newoff )
1189 fprintf( stderr, "Internal error, file offset wrong in final table dump\n" );
1190
1191 copydata(ttf,hdr->subtables[i].file,
1192 hdr->subtables[i].offset,hdr->subtables[i].len);
1193 if ( ftell(ttf)&1 )
1194 putc('\0',ttf);
1195 if ( ftell(ttf)&2 )
1196 putshort(ttf,0);
1197
1198 if ( hdr->subtables[i].closeme )
1199 fclose(hdr->subtables[i].file);
1200 }
1201
1202 if ( head_index==-1 )
1203 fprintf(stderr, "Missing 'head' table\n" );
1204 else {
1205 int checksum = 0xb1b0afba - filecheck(ttf,0,ftell(ttf));
1206 fseek(ttf,hdr->subtables[head_index].newoff+8,SEEK_SET);
1207 putlong(ttf,checksum);
1208 }
1209 if ( ferror(ttf) )
1210 fprintf( stderr, "Error writing ttf file.\n" );
1211 fclose(ttf);
1212 stat(buffer,&statb);
1213 chmod(buffer,statb.st_mode|S_IXUSR|S_IXGRP|S_IXOTH); /* Set the execute bits (in case it's windows) */
1214 }
1215
1216 /* ************************************************************************** */
1217 /* ******************************* bitmap font ****************************** */
1218 /* ************************************************************************** */
1219
1220 static int readheaderbitmap(FILE *pcl,struct ttf_header *hdr) {
1221 int i, base;
1222
1223 if ( hdr->header_format==20 ) {
1224 hdr->xres = getshort(pcl);
1225 hdr->yres = getshort(pcl);
1226 } else
1227 hdr->xres = hdr->yres = 300;
1228
1229 if ( (hdr->header_format==0 && hdr->fd_size<64) ||
1230 (hdr->header_format==20 && hdr->fd_size<68) ||
1231 (hdr->header_format!=0 && hdr->header_format!=20)) {
1232 fprintf( stderr, "%s is not a PCL bitmap font\n", hdr->fontname );
1233 return( false );
1234 }
1235 /* Variable data contain the copyright */
1236 base = hdr->header_format==0 ? 64 : 68;
1237 if ( hdr->fd_size>base ) {
1238 char *pt;
1239 hdr->copyright = pt = malloc(hdr->fd_size-base+1);
1240 for ( i=0; i<hdr->fd_size-base; ++i )
1241 *pt++ = getc(pcl);
1242 *pt = '\0';
1243 }
1244 return( true );
1245 }
1246
1247 static int slurpbdf_glyph(FILE *pcl,struct ttf_header *hdr, FILE *bdf,
1248 int fbbox[4], int *cnt) {
1249 int encoding, size, ch;
1250 int desc_size, i;
1251 int class;
1252 int restart = ftell(pcl);
1253 int left_off, top_off, width, height, deltax, bpl;
1254
1255 if ( !skip2charcode(pcl)) {
1256 fprintf( stderr, "Fewer glyphs in font than expected\n" );
1257 fseek(pcl,restart,SEEK_SET);
1258 return( false );
1259 }
1260 fscanf(pcl,"%d", &encoding );
1261 ch = getc(pcl);
1262 if ( ch!='E' ) {
1263 fprintf( stderr, "Fewer glyphs in font than expected\n" );
1264 fseek(pcl,restart,SEEK_SET);
1265 return( false );
1266 }
1267
1268 if ( !skip2glyph(pcl))
1269 return( false );
1270 fscanf(pcl,"%d", &size );
1271 ch = getc(pcl);
1272 if ( ch!='W' ) {
1273 fprintf( stderr, "Bad font, glyph size does not end in \"W\"\n" );
1274 return( false );
1275 }
1276
1277 if ( getc(pcl)!=4 ) {
1278 fprintf( stderr, "Bad font, glyph format must be 4\n" );
1279 return( false );
1280 }
1281 if ( getc(pcl)!=0 ) {
1282 fprintf( stderr, "Bad font, I don't support continuations\n" );
1283 return( false );
1284 }
1285 desc_size = getc(pcl);
1286 if ( desc_size!=14 ) {
1287 fprintf( stderr, "Bad font, glyph descriptor size must be 14\n" );
1288 return( false );
1289 }
1290 class = getc(pcl);
1291 if ( class!=1 && class!=2 ) {
1292 fprintf( stderr, "Bad font, glyph class must be 1 or 2\n" );
1293 return( false );
1294 }
1295 getc(pcl); /* orientation */
1296 getc(pcl); /* reserved */
1297 left_off = getshort(pcl);
1298 top_off = getshort(pcl);
1299 width = getshort(pcl);
1300 height = getshort(pcl);
1301 deltax = getshort(pcl);
1302
1303 if ( left_off<fbbox[2]) fbbox[2] = left_off;
1304 if ( left_off+width > fbbox[0] ) fbbox[0] = left_off+width;
1305 if ( top_off-height < fbbox[3] ) fbbox[3] = top_off-height;
1306 if ( top_off > fbbox[1] ) fbbox[1] = top_off;
1307
1308 fprintf( bdf, "STARTCHAR glyph%d\n", *cnt++ );
1309 fprintf( bdf, "ENCODING %d\n", encoding );
1310 fprintf( bdf, "DWIDTH %d 0\n", deltax );
1311 fprintf( bdf, "BBX %d %d %d %d\n", width, height, left_off, top_off-height );
1312 fprintf( bdf, "BITMAP\n" );
1313 bpl = (width+7)/8;
1314 if ( class==2 ) {
1315 fprintf( stderr, "Warning: I don't support run length encoded glyphs\n" );
1316 i = 0;
1317 } else {
1318 for ( i=0; i<size-14 && i<height*bpl ; ++i ) {
1319 ch = getc(pcl);
1320 if ( (ch>>4) >= 10 )
1321 putc((ch>>4)-10+'A',bdf);
1322 else
1323 putc((ch>>4)+'0',bdf);
1324 if ( (ch&0xf) >= 10 )
1325 putc((ch&0xf)-10+'A',bdf);
1326 else
1327 putc((ch&0xf)+'0',bdf);
1328 if ( i % bpl == bpl-1 )
1329 putc('\n',bdf);
1330 }
1331 }
1332 for ( ; i<height*bpl; ++i ) {
1333 putc('0',bdf);
1334 putc('0',bdf);
1335 if ( i % bpl == bpl-1 )
1336 putc('\n',bdf);
1337 }
1338
1339 return( true );
1340 }
1341
1342 static int slurpbdf_glyphs(FILE *pcl,struct ttf_header *hdr, FILE *bdf,
1343 int fbbox[4], int *cnt) {
1344 int i;
1345
1346 for ( i=0; i<hdr->num_chars; ++i )
1347 if ( !slurpbdf_glyph(pcl,hdr,bdf,fbbox,cnt)) {
1348 hdr->actual_num = i;
1349 return( false );
1350 }
1351 hdr->actual_num = i;
1352
1353 return( true );
1354 }
1355
1356 static int dumpbdffont(FILE *pcl,struct ttf_header *hdr) {
1357 FILE *bdf;
1358 char buffer[32], *pt, *fpt;
1359 char *weight, *expansion;
1360 int fbboxpos;
1361 int fbbox[4]; /* xmax, ymax, xmin, ymin */
1362 int cntpos;
1363 int cnt;
1364
1365 fpt = hdr->fontname;
1366 for ( pt=buffer; *fpt; ++fpt )
1367 if ( *fpt!=' ' && *fpt!='/' )
1368 *pt++ = *fpt;
1369 strcpy(pt,".bdf");
1370 bdf = fopen(buffer,"wb+");
1371 if ( bdf==NULL ) {
1372 fprintf( stderr, "Failed to open %s\n", buffer );
1373 return( false );
1374 } else
1375 printf( "Created %s\n", buffer );
1376
1377 fprintf( bdf, "STARTFONT 2.1\n" );
1378 *pt = '\0';
1379 weight = (char *)((
1380 hdr->strokeweight<=-7 ? "UltraThin" :
1381 hdr->strokeweight==-6 ? "ExtraThin" :
1382 hdr->strokeweight==-5 ? "Thin" :
1383 hdr->strokeweight==-4 ? "ExtraLight" :
1384 hdr->strokeweight==-3 ? "Light" :
1385 hdr->strokeweight==-2 ? "DemiLight" :
1386 hdr->strokeweight==-1 ? "SemiLight" :
1387 hdr->strokeweight==0 ? "Medium" :
1388 hdr->strokeweight==1 ? "SemiBold" :
1389 hdr->strokeweight==2 ? "DemiBold" :
1390 hdr->strokeweight==3 ? "Bold" :
1391 hdr->strokeweight==4 ? "ExtraBold" :
1392 hdr->strokeweight==5 ? "Black" :
1393 hdr->strokeweight==6 ? "ExtraBlack" : "UltraBlack"));
1394 expansion = (char *)((
1395 hdr->widthtype<=-5 ? "UltraCompressed" :
1396 hdr->widthtype==-4 ? "ExtraCompressed" :
1397 hdr->widthtype==-3 ? "Compressed" :
1398 hdr->strokeweight<=-1 ? "Condensed" :
1399 hdr->strokeweight==0 ? "Normal" :
1400 hdr->strokeweight<=2 ? "Expanded" :
1401 "ExtraExpanded"));
1402 fprintf( bdf, "FONT -pcl2ttf-%s-%s-%c-%s--%d-%d-%d-%d-%c-%d-FontSpecific-1\n",
1403 buffer,
1404 weight,
1405 (hdr->style&3)==1 || (hdr->style&3)==2 ? 'I' : 'R',
1406 expansion,
1407 hdr->height/4, /* pixelsize in dots*/
1408 (int) rint(720.0*hdr->height/hdr->yres/4), /* pointsize * 10 */
1409 hdr->xres,
1410 hdr->yres,
1411 hdr->spacing ? 'M' : 'P', /* Monospace, proportional */
1412 hdr->height/8 /* Guess. average char width */
1413 );
1414 fprintf( bdf, "SIZE %d %d %d\n", (int) rint(72.0*hdr->height/hdr->yres/4),
1415 hdr->xres, hdr->yres);
1416 fbboxpos = ftell(bdf);
1417 fprintf( bdf, "FONTBOUNDINGBOX %5d %5d %5d %5d\n", 0, 0, 0, 0 );
1418 if ( hdr->copyright!=NULL )
1419 fprintf( bdf, "COMMENT \"%s\"\n", hdr->copyright );
1420 fprintf( bdf, "STARTPROPERTIES %d\n", 15+( hdr->xres==hdr->yres )+( hdr->copyright!=NULL ) );
1421 fprintf( bdf, "FONT_NAME \"%s\"\n", buffer );
1422 fprintf( bdf, "FAMILY_NAME \"%s\"\n", buffer );
1423 fprintf( bdf, "FULL_NAME \"%s\"\n", buffer );
1424 fprintf( bdf, "FONT_ASCENT %d\n", hdr->baseline_pos );
1425 fprintf( bdf, "FONT_DESCENT %d\n", hdr->height/4-hdr->baseline_pos );
1426 fprintf( bdf, "UNDERLINE_POSITION %d\n", hdr->upos );
1427 fprintf( bdf, "UNDERLINE_THICKNESS %d\n", hdr->uthick );
1428 fprintf( bdf, "FOUNDRY \"pcl2ttf\"\n" );
1429 fprintf( bdf, "WEIGHT_NAME \"%s\"\n", weight );
1430 fprintf( bdf, "SETWIDTH_NAME \"%s\"\n", expansion );
1431 fprintf( bdf, "SLANT \"%c\"\n", (hdr->style&3)==1 || (hdr->style&3)==2 ? 'I' : 'R');
1432 fprintf( bdf, "PIXEL_SIZE %d\n", hdr->height/4);
1433 fprintf( bdf, "POINT_SIZE %d\n", (int) rint(720.0*hdr->height/hdr->yres/4));
1434 fprintf( bdf, "RESOLUTION_X %d\n", hdr->xres );
1435 fprintf( bdf, "RESOLUTION_Y %d\n", hdr->yres );
1436 if ( hdr->xres==hdr->yres )
1437 fprintf( bdf, "RESOLUTION %d\n", hdr->xres );
1438 fprintf( bdf, "SPACING \"%c\"\n", hdr->spacing ? 'M' : 'P');
1439 if ( hdr->copyright!=NULL )
1440 fprintf( bdf, "COPYRIGHT \"%s\"\n", hdr->copyright );
1441 fprintf( bdf, "ENDPROPERTIES\n" );
1442
1443 fbbox[0] = fbbox[1] = -10000;
1444 fbbox[2] = fbbox[3] = 10000;
1445
1446 cntpos = ftell(bdf);
1447 fprintf( bdf, "CHARS %3d\n", 0 );
1448 cnt = 0;
1449
1450 slurpbdf_glyphs(pcl,hdr,bdf,fbbox, &cnt);
1451
1452 fprintf( bdf, "ENDFONT\n" );
1453
1454 if ( fbbox[0]==-10000 )
1455 fbbox[0] = fbbox[1] = fbbox[2] = fbbox[3] = 0;
1456 fseek(bdf,fbboxpos,SEEK_SET);
1457 fprintf( bdf, "FONTBOUNDINGBOX %5d %5d %5d %5d\n", fbbox[0], fbbox[1], fbbox[2], fbbox[3] );
1458 fseek(bdf,cntpos,SEEK_SET );
1459 fprintf( bdf, "CHARS %3d\n", cnt );
1460 fclose(bdf);
1461 return( true );
1462 }
1463
1464 /* ************************************************************************** */
1465 /* **************************** ui, such as it is *************************** */
1466 /* ************************************************************************** */
1467
1468 static void freedata(struct ttf_header *hdr) {
1469 int i;
1470
1471 free(hdr->subtables);
1472 if ( hdr->glyphs!=NULL ) {
1473 for ( i=0; i<hdr->num_chars; ++i )
1474 free( hdr->glyphs[i].continuation );
1475 }
1476 free(hdr->glyphs);
1477 free(hdr->copyright);
1478 }
1479
1480 static void parsepclfile(char *filename) {
1481 FILE *pcl = fopen(filename,"rb");
1482 struct ttf_header hdr;
1483
1484 if ( pcl==NULL ) {
1485 fprintf( stderr, "Can't open %s\n", filename );
1486 return;
1487 }
1488 while ( skip2fontheader(pcl)) {
1489 readheaderformat(pcl,&hdr);
1490 if ( hdr.header_format==15 ) { /* TrueType */
1491 if ( readheaderttf(pcl,&hdr) ) {
1492 create_missingtables(pcl,&hdr);
1493 dumpfont(&hdr);
1494 }
1495 } else if ( hdr.header_format==0 || hdr.header_format==20 ) { /* Bitmap */
1496 if ( readheaderbitmap(pcl,&hdr))
1497 dumpbdffont(pcl,&hdr);
1498 } else if ( hdr.header_format==10 || hdr.header_format==11 ) {
1499 fprintf( stderr, "This file contains an Intellifont Scalable font.\nI have no docs on that format and can't help you.\n" );
1500 } else {
1501 fprintf( stderr, "I don't recognize this font format. It isn't mentioned in my documentation.\nSorry.\n" );
1502 }
1503 freedata(&hdr);
1504 }
1505 fclose(pcl);
1506 }
1507
1508 int main( int argc, char *argv[]) {
1509 int i;
1510
1511 if ( argc==1 ) {
1512 fprintf( stderr, "Usage: %s file.pcl\n", argv[0] );
1513 exit(1);
1514 }
1515 for ( i=1; i<argc; ++i )
1516 parsepclfile(argv[i]);
1517 return( 0 );
1518 }