Debian Codegraph @

contrib/fonttools/stripttc.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 /* This program takes a ttc file and turns it into its component ttf files */
6 /* This makes two changes to the data: */
7 /* * The tables are placed at different offsets, therefore */
8 /* the offset fields in the table header are also different. */
9 /* * the 'head' table checksumAdjustment field is set correctly */
10
11 #define CHR(ch1,ch2,ch3,ch4) (((ch1)<<24)|((ch2)<<16)|((ch3)<<8)|(ch4))
12
13 static void putshort(FILE *file,int sval) {
14 putc((sval>>8)&0xff,file);
15 putc(sval&0xff,file);
16 }
17
18 static void putlong(FILE *file,int val) {
19 putc((val>>24)&0xff,file);
20 putc((val>>16)&0xff,file);
21 putc((val>>8)&0xff,file);
22 putc(val&0xff,file);
23 }
24
25 static int getushort(FILE *ttf) {
26 int ch1 = getc(ttf);
27 int ch2 = getc(ttf);
28 if ( ch2==EOF )
29 return( EOF );
30 return( (ch1<<8)|ch2 );
31 }
32
33 static int getlong(FILE *ttf) {
34 int ch1 = getc(ttf);
35 int ch2 = getc(ttf);
36 int ch3 = getc(ttf);
37 int ch4 = getc(ttf);
38 if ( ch4==EOF )
39 return( EOF );
40 return( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 );
41 }
42
43 static unsigned int filecheck(FILE *file) {
44 unsigned int sum = 0, chunk;
45
46 rewind(file);
47 while ( 1 ) {
48 chunk = getlong(file);
49 if ( feof(file) || ferror(file))
50 break;
51 sum += chunk;
52 }
53 return( sum );
54 }
55
56 static void copytable(FILE *ttf,FILE *ttc,int offset,int length) {
57 int i, ch;
58
59 fseek(ttc,offset,SEEK_SET);
60 for ( i=0; i<length && (ch=getc(ttc))!=EOF ; ++i )
61 putc(ch,ttf);
62 if ( ch==EOF )
63 fprintf( stderr, "File ended before table\n" );
64 if ( length&1 ) {
65 putc('\0',ttf);
66 ++length;
67 }
68 if ( length & 2 ) {
69 putshort(ttf,0);
70 }
71 }
72
73 static int handlefont(char *filename,int which,FILE *ttc,int offset) {
74 char outfile[2000], *pt;
75 FILE *ttf;
76 int i, cnt, *offsets, *lengths, head, tag, pos, headpos;
77
78 strcpy(outfile,filename);
79 pt = strrchr(outfile,'.');
80 if ( pt==NULL )
81 pt = outfile + strlen(outfile);
82 sprintf( pt, "_%02d.ttf", which );
83
84 ttf = fopen( outfile,"w+b");
85 if ( ttf==NULL ) {
86 fprintf( stderr, "Failed to open %s for output.\n", outfile );
87 return( -3 );
88 }
89 printf ( "%s ", outfile );
90
91 fseek(ttc,offset,SEEK_SET);
92 putlong(ttf,getlong(ttc)); /* sfnt version */
93 putshort(ttf,cnt = getushort(ttc)); /* table cnt */
94 putshort(ttf,getushort(ttc)); /* binary search header */
95 putshort(ttf,getushort(ttc));
96 putshort(ttf,getushort(ttc));
97
98 offsets = malloc(cnt*sizeof(int));
99 lengths = malloc(cnt*sizeof(int));
100 head = -1;
101 for ( i=0; i<cnt; ++i ) {
102 tag = getlong(ttc);
103 if ( tag==CHR('h','e','a','d'))
104 head = i;
105 putlong(ttf,tag);
106 putlong(ttf,getlong(ttc)); /* checksum */
107 putlong(ttf,offsets[i] = getlong(ttc)); /* Reserve space for offset, will fix later */
108 putlong(ttf,lengths[i] = getlong(ttc));
109 }
110 headpos = -1;
111 for ( i=0; i<cnt; ++i ) {
112 pos = ftell(ttf);
113 copytable(ttf,ttc,offsets[i],lengths[i]);
114 if ( i==head ) {
115 fseek(ttf,pos+8,SEEK_SET);
116 putlong(ttf,0);
117 headpos = pos;
118 }
119 fseek(ttf,12+i*16+8,SEEK_SET);
120 putlong(ttf,pos); /* Fix offset here */
121 fseek(ttf,0,SEEK_END);
122 }
123 if ( headpos!=-1 ) {
124 unsigned int checksum;
125 checksum = filecheck(ttf);
126 checksum = 0xb1b0afba-checksum;
127 fseek(ttf,headpos+2*sizeof(int),SEEK_SET);
128 putlong(ttf,checksum);
129 }
130 fclose(ttf);
131 free(offsets); free(lengths);
132 return( 0 );
133 }
134
135 static int handlefile(char *filename) {
136 FILE *ttc = fopen(filename,"rb");
137 int version, cnt, e, i;
138 int *offsets;
139
140 if ( ttc==NULL ) {
141 fprintf( stderr, "Could not open %s\n", filename );
142 return( -1 );
143 }
144
145 version = getlong(ttc);
146 if ( version!=CHR('t','t','c','f')) {
147 fprintf( stderr, "%s does not look like a ttc file, bad version.\n", filename );
148 fclose(ttc);
149 return( -2 );
150 }
151
152 version = getlong(ttc);
153 if ( version!=0x10000 && version != 0x20000 )
154 fprintf( stderr, "Unexpected ttc version number: %08x\n", (unsigned int)(version) );
155 cnt = getlong(ttc);
156 offsets = malloc(cnt*sizeof(int));
157 for ( i=0; i<cnt; ++i )
158 offsets[i] = getlong(ttc);
159 printf( "%s => ", filename );
160 for ( i=0; i<cnt; ++i )
161 if ( (e = handlefont(filename,i,ttc,offsets[i])) ) {
162 fclose(ttc);
163 free(offsets);
164 return( e );
165 };
166 printf( "\n" );
167 fclose(ttc);
168 free(offsets);
169 return( 0 );
170 }
171
172 int main(int argc, char *argv[]) {
173 int e, i;
174
175 for ( i=1; i<argc; ++i )
176 if ( (e = handlefile(argv[i])) )
177 return( e );
178 return( 0 );
179 }