File: woff.c

package info (click to toggle)
fontforge 1%3A20201107~dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 67,192 kB
  • sloc: ansic: 587,351; python: 4,932; perl: 315; sh: 266; cpp: 219; makefile: 55; xml: 11
file content (341 lines) | stat: -rw-r--r-- 9,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* Copyright (c) 2010 by George Williams */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <zlib.h>

/* Compile with: $ cc -o woff woff.c -lz */

/* http://people.mozilla.com/~jkew/woff/woff-2009-09-16.html */

#define CHR(ch1,ch2,ch3,ch4) (((ch1)<<24)|((ch2)<<16)|((ch3)<<8)|(ch4))

#define true	1
#define false	0

static int getushort(FILE *ttf) {
    int ch1 = getc(ttf);
    int ch2 = getc(ttf);
    if ( ch2==EOF )
return( EOF );
return( (ch1<<8)|ch2 );
}

static int getlong(FILE *ttf) {
    int ch1 = getc(ttf);
    int ch2 = getc(ttf);
    int ch3 = getc(ttf);
    int ch4 = getc(ttf);
    if ( ch4==EOF )
return( EOF );
return( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 );
}

static void putshort(FILE *file,int sval) {
    putc((sval>>8)&0xff,file);
    putc(sval&0xff,file);
}

static void putlong(FILE *file,int val) {
    putc((val>>24)&0xff,file);
    putc((val>>16)&0xff,file);
    putc((val>>8)&0xff,file);
    putc(val&0xff,file);
}
/* ************************************************************************** */
static void copydata(FILE *to,int off_to,FILE *from,int off_from, int len) {
    int ch, i;

    fseek(to  ,off_to  ,SEEK_SET);
    fseek(from,off_from,SEEK_SET);
    for ( i=0; i<len; ++i ) {
	ch = getc(from);
	putc(ch,to);
    }
}

#define CHUNK	(128*1024)

static int compressOrNot(FILE *to,int off_to, FILE *from,int off_from,
	int len, int forcecompress ) {
    char in[CHUNK];
    char out[CHUNK];
    z_stream strm;
    int ret, err=0;
    int amount;
    FILE *tmp;
    int uncompLen = len;

    /* Empty table, nothing to do */
    if ( len==0 )
return(0);

    fseek(from,off_from,SEEK_SET);
    memset(&strm,0,sizeof(strm));
    ret = deflateInit(&strm,Z_DEFAULT_COMPRESSION);
    if ( ret!=Z_OK ) {
	fprintf( stderr,"Compression initialization failed.\n" );
return(0);
    }
    tmp = tmpfile();

    do {
	if ( len<=0 ) {
            (void)deflateEnd(&strm);
    break;
        }
	amount = len;
	if ( amount>CHUNK )
	    amount = CHUNK;
        strm.avail_in = fread(in, 1, amount, from);
	len -= strm.avail_in;
        if (ferror(from)) {
            (void)deflateEnd(&strm);
	    fprintf( stderr, "IO error.\n" );
    break;
        }
        if (strm.avail_in == 0)
    break;
        strm.next_in = in;
        do {
            strm.avail_out = CHUNK;
            strm.next_out = out;
            ret = deflate(&strm, len==0 ? Z_FINISH : Z_NO_FLUSH);
	    if ( ret==Z_STREAM_ERROR ) {
		(void)deflateEnd(&strm);
		fprintf( stderr, "Compression failed somehow.\n");
		err = 1;
	break;
	    }
	    amount = CHUNK - strm.avail_out;
	    if ( fwrite(out,1,amount,tmp)!= amount || ferror(tmp) ) {
		(void)deflateEnd(&strm);
		fprintf( stderr, "IO Error.\n");
		err=1;
	break;
	    }
	} while ( strm.avail_out==0 );
	if ( err )
    break;
    } while ( ret!=Z_STREAM_END );
    (void)deflateEnd(&strm);

    if ( strm.total_out>=uncompLen ) {
	/* Didn't actually make the data smaller, so store uncompressed */
	fclose(tmp);
	copydata(to,off_to,from,off_from,uncompLen);
return( uncompLen );
    } else {
	copydata(to,off_to,tmp,0,strm.total_out);
	fclose(tmp);
return( strm.total_out );
    }
}

static void woff(char *filename, char *metafile, char *privfile,
	int major, int minor) {
    int flavour, num_tabs, test;
    char *start, *ext, *outname;
    int filelen, len;
    int i,j;
    FILE *sfnt, *woff, *meta, *priv;
    int compLen, uncompLen, newoffset;
    int tag, checksum, offset;
    int tab_start, here;

    sfnt = fopen( filename,"rb");
    if ( sfnt==NULL ) {
	fprintf( stderr, "Can't open %s\n", filename );
return;
    }

    start = strrchr(filename,'/');
    if ( start==NULL )
	start = filename;
    else
	++start;
    ext = strrchr(start,'.');
    if ( ext==NULL )
	ext = start+strlen(start);

    fseek(sfnt,0,SEEK_END);
    filelen = ftell(sfnt);
    rewind(sfnt);

    flavour = getlong(sfnt);
    /* The woff standard says we should accept all flavours of sfnt, so can't */
    /*  test flavour to make sure we've got a valid sfnt */
    /* But we can test the rest of the header for consistancy */
    num_tabs = getushort(sfnt);
    for ( i=1, j=0; 2*i<=num_tabs; i<<=1, ++j );
    test = getushort(sfnt);
    if ( test!=16*i ) {
	fprintf( stderr, "%s has an invalid sfnt header (1) %d!=%d\n", filename, test, 16*i );
	fclose(sfnt);
return;
    }
    test = getushort(sfnt);
    if ( test!=j ) {
	fprintf( stderr, "%s has an invalid sfnt header (2) %d!=%d\n", filename, test, j );
	fclose(sfnt);
return;
    }
    test = getushort(sfnt);
    if ( test!=(num_tabs-i)*16 ) {
	fprintf( stderr, "%s has an invalid sfnt header (3) %d!=%d\n", filename, test, (num_tabs-i)*16 );
	fclose(sfnt);
return;
    }

    outname = malloc(strlen(start)+20);
    strcpy(outname,start);
    strcpy(outname+(ext-start),".woff" );
    woff = fopen(outname,"wb");
    if ( woff==NULL ) {
	fprintf( stderr, "Failed to create output file: %s\n", outname );
	fclose(sfnt);
	free(outname);
return;
    }
    putlong(woff,CHR('w','O','F','F'));
    putlong(woff,flavour);
    putlong(woff,0);		/* Off: 8. total length of file, fill in later */
    putshort(woff,num_tabs);
    putshort(woff,0);		/* Must be zero */
    putlong(woff,filelen);
    putshort(woff,major);	/* Major and minor version numbers of font */
    putshort(woff,minor);
    putlong(woff,0);		/* Off: 24. Offset to metadata table */
    putlong(woff,0);		/* Off: 28. Length (compressed) of metadata */
    putlong(woff,0);		/* Off: 32. Length (uncompressed) */
    putlong(woff,0);		/* Off: 36. Offset to private data */
    putlong(woff,0);		/* Off: 40. Length of private data */

    tab_start = ftell(woff);
    for ( i=0; i<5*num_tabs; ++i )
	putlong(woff,0);

    for ( i=0; i<num_tabs; ++i ) {
	tag = getlong(sfnt);
	checksum = getlong(sfnt);
	offset = getlong(sfnt);
	uncompLen = getlong(sfnt);
	here = ftell(sfnt);
	newoffset = ftell(woff);
	compLen = compressOrNot(woff,newoffset,sfnt,offset,uncompLen,false);
	if ( (ftell(woff)&3)!=0 ) {
	    /* Pad to a 4 byte boundary */
	    if ( ftell(woff)&1 )
		putc('\0',woff);
	    if ( ftell(woff)&2 )
		putshort(woff,0);
	}
	fseek(sfnt,here,SEEK_SET);
	fseek(woff,tab_start,SEEK_SET);
	putlong(woff,tag);
	putlong(woff,newoffset);
	putlong(woff,compLen);
	putlong(woff,uncompLen);
	putlong(woff,checksum);
	tab_start = ftell(woff);
	fseek(woff,0,SEEK_END);
    }
    fclose(sfnt);

    meta = NULL;
    if ( metafile!=NULL ) {
	meta = fopen(metafile,"w");
	if ( meta==NULL )
	    fprintf( stderr, "Failed to open meta data file: %s\n", metafile );
    }
    if ( meta!=NULL ) {
	newoffset = ftell(woff);
	fseek(meta,0,SEEK_END);
	uncompLen = ftell(meta);
	rewind( meta );
	compLen = compressOrNot(woff,newoffset,meta,0,uncompLen,true);
	if ( privfile!=NULL && (ftell(woff)&3)!=0 ) {
	    /* Pad to a 4 byte boundary */
	    if ( ftell(woff)&1 )
		putc('\0',woff);
	    if ( ftell(woff)&2 )
		putshort(woff,0);
	}
	fseek(woff,24,SEEK_SET);
	putlong(woff,newoffset);
	putlong(woff,compLen);
	putlong(woff,uncompLen);
	fseek(woff,0,SEEK_END);
	fclose(meta);
    }

    priv = NULL;
    if ( privfile!=NULL ) {
	priv = fopen(privfile,"w");
	if ( priv==NULL )
	    fprintf( stderr, "Failed to open private file: %s\n", privfile );
    }
    if ( priv!=NULL ) {
	newoffset = ftell(woff);
	fseek(priv,0,SEEK_END);
	uncompLen = ftell(priv);
	rewind( priv );
	copydata(woff,newoffset,priv,0,uncompLen);
	/* Not padded */
	fseek(woff,36,SEEK_SET);
	putlong(woff,newoffset);
	putlong(woff,uncompLen);
	fseek(woff,0,SEEK_END);
	fclose(priv);
    }
    len = ftell(woff);
    fseek(woff,8,SEEK_SET);
    putlong(woff,len);
    fclose(woff);
    free(outname);
}
	

static void usage(char *prog) {
    printf( "%s {[-meta xml-file] [-priv file] [-major num] [-minor num] otf/ttf-filename}\n", prog );
    printf( " Takes one (or several sfnt files, and converts them into their equivalent\n" );
    printf( "woff forms. The output files will be placed in the current directory\n" );
    printf( "with the same name as the original file and an extension \".woff\". If\n" );
    printf( "you wish to include a metadata table, or a private table put them in\n" );
    printf( "their own files and specify them with -meta or -priv arguments\n" );
}

int main(int argc, char **argv) {
    int i;
    char *metafile=NULL, *privfile=NULL, *pt;
    int major=1, minor=0;

    for ( i=1; i<argc; ++i ) {
	if ( *argv[i]=='-' ) {
	    pt = argv[i];
	    if ( *pt=='-' && pt[1]=='-' )
		++pt;
	    if ( i+1<argc ) {
		if ( strcasecmp(pt,"-meta")==0 || strcasecmp(pt,"-metadata")==0 ) {
		    metafile = argv[++i];
    continue;
		} else if ( strcasecmp(pt,"-priv")==0 || strcasecmp(pt,"-private")==0 ) {
		    privfile = argv[++i];
    continue;
		} else if ( strcasecmp(pt,"-major")==0 ) {
		    major = strtol(argv[++i],NULL,10);
    continue;
		} else if ( strcasecmp(pt,"-minor")==0 ) {
		    minor = strtol(argv[++i],NULL,10);
    continue;
		}
	    }
	    usage(argv[0]);
    continue;
	}
	woff(argv[i],metafile,privfile,major,minor);
	metafile=NULL; privfile=NULL;
	major = 1; minor = 0;
    }
return( 0 );
}