| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <string.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <zlib.h> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#define CHR(ch1,ch2,ch3,ch4) (((ch1)<<24)|((ch2)<<16)|((ch3)<<8)|(ch4)) |
| 12 |
|
| 13 |
#define true 1 |
| 14 |
#define false 0 |
| 15 |
|
| 16 |
static int getushort(FILE *ttf) { |
| 17 |
int ch1 = getc(ttf); |
| 18 |
int ch2 = getc(ttf); |
| 19 |
if ( ch2==EOF ) |
| 20 |
return( EOF ); |
| 21 |
return( (ch1<<8)|ch2 ); |
| 22 |
} |
| 23 |
|
| 24 |
static int getlong(FILE *ttf) { |
| 25 |
int ch1 = getc(ttf); |
| 26 |
int ch2 = getc(ttf); |
| 27 |
int ch3 = getc(ttf); |
| 28 |
int ch4 = getc(ttf); |
| 29 |
if ( ch4==EOF ) |
| 30 |
return( EOF ); |
| 31 |
return( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 ); |
| 32 |
} |
| 33 |
|
| 34 |
static void putshort(FILE *file,int sval) { |
| 35 |
putc((sval>>8)&0xff,file); |
| 36 |
putc(sval&0xff,file); |
| 37 |
} |
| 38 |
|
| 39 |
static void putlong(FILE *file,int val) { |
| 40 |
putc((val>>24)&0xff,file); |
| 41 |
putc((val>>16)&0xff,file); |
| 42 |
putc((val>>8)&0xff,file); |
| 43 |
putc(val&0xff,file); |
| 44 |
} |
| 45 |
|
| 46 |
static void copydata(FILE *to,int off_to,FILE *from,int off_from, int len) { |
| 47 |
int ch, i; |
| 48 |
|
| 49 |
fseek(to ,off_to ,SEEK_SET); |
| 50 |
fseek(from,off_from,SEEK_SET); |
| 51 |
for ( i=0; i<len; ++i ) { |
| 52 |
ch = getc(from); |
| 53 |
putc(ch,to); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
#define CHUNK (128*1024) |
| 58 |
|
| 59 |
static int compressOrNot(FILE *to,int off_to, FILE *from,int off_from, |
| 60 |
int len, int forcecompress ) { |
| 61 |
char in[CHUNK]; |
| 62 |
char out[CHUNK]; |
| 63 |
z_stream strm; |
| 64 |
int ret, err=0; |
| 65 |
int amount; |
| 66 |
FILE *tmp; |
| 67 |
int uncompLen = len; |
| 68 |
|
| 69 |
|
| 70 |
if ( len==0 ) |
| 71 |
return(0); |
| 72 |
|
| 73 |
fseek(from,off_from,SEEK_SET); |
| 74 |
memset(&strm,0,sizeof(strm)); |
| 75 |
ret = deflateInit(&strm,Z_DEFAULT_COMPRESSION); |
| 76 |
if ( ret!=Z_OK ) { |
| 77 |
fprintf( stderr,"Compression initialization failed.\n" ); |
| 78 |
return(0); |
| 79 |
} |
| 80 |
tmp = tmpfile(); |
| 81 |
|
| 82 |
do { |
| 83 |
if ( len<=0 ) { |
| 84 |
(void)deflateEnd(&strm); |
| 85 |
break; |
| 86 |
} |
| 87 |
amount = len; |
| 88 |
if ( amount>CHUNK ) |
| 89 |
amount = CHUNK; |
| 90 |
strm.avail_in = fread(in, 1, amount, from); |
| 91 |
len -= strm.avail_in; |
| 92 |
if (ferror(from)) { |
| 93 |
(void)deflateEnd(&strm); |
| 94 |
fprintf( stderr, "IO error.\n" ); |
| 95 |
break; |
| 96 |
} |
| 97 |
if (strm.avail_in == 0) |
| 98 |
break; |
| 99 |
strm.next_in = (Bytef*)in; |
| 100 |
do { |
| 101 |
strm.avail_out = CHUNK; |
| 102 |
strm.next_out = (Bytef*)out; |
| 103 |
ret = deflate(&strm, len==0 ? Z_FINISH : Z_NO_FLUSH); |
| 104 |
if ( ret==Z_STREAM_ERROR ) { |
| 105 |
(void)deflateEnd(&strm); |
| 106 |
fprintf( stderr, "Compression failed somehow.\n"); |
| 107 |
err = 1; |
| 108 |
break; |
| 109 |
} |
| 110 |
amount = CHUNK - strm.avail_out; |
| 111 |
if ( fwrite(out,1,amount,tmp)!= amount || ferror(tmp) ) { |
| 112 |
(void)deflateEnd(&strm); |
| 113 |
fprintf( stderr, "IO Error.\n"); |
| 114 |
err=1; |
| 115 |
break; |
| 116 |
} |
| 117 |
} while ( strm.avail_out==0 ); |
| 118 |
if ( err ) |
| 119 |
break; |
| 120 |
} while ( ret!=Z_STREAM_END ); |
| 121 |
(void)deflateEnd(&strm); |
| 122 |
|
| 123 |
if ( strm.total_out>=uncompLen ) { |
| 124 |
|
| 125 |
fclose(tmp); |
| 126 |
copydata(to,off_to,from,off_from,uncompLen); |
| 127 |
return( uncompLen ); |
| 128 |
} else { |
| 129 |
copydata(to,off_to,tmp,0,strm.total_out); |
| 130 |
fclose(tmp); |
| 131 |
return( strm.total_out ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
static void woff(char *filename, char *metafile, char *privfile, |
| 136 |
int major, int minor) { |
| 137 |
int flavour, num_tabs, test; |
| 138 |
char *start, *ext, *outname; |
| 139 |
int filelen, len; |
| 140 |
int i,j; |
| 141 |
FILE *sfnt, *woff, *meta, *priv; |
| 142 |
int compLen, uncompLen, newoffset; |
| 143 |
int tag, checksum, offset; |
| 144 |
int tab_start, here; |
| 145 |
|
| 146 |
sfnt = fopen( filename,"rb"); |
| 147 |
if ( sfnt==NULL ) { |
| 148 |
fprintf( stderr, "Can't open %s\n", filename ); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
start = strrchr(filename,'/'); |
| 153 |
if ( start==NULL ) |
| 154 |
start = filename; |
| 155 |
else |
| 156 |
++start; |
| 157 |
ext = strrchr(start,'.'); |
| 158 |
if ( ext==NULL ) |
| 159 |
ext = start+strlen(start); |
| 160 |
|
| 161 |
fseek(sfnt,0,SEEK_END); |
| 162 |
filelen = ftell(sfnt); |
| 163 |
rewind(sfnt); |
| 164 |
|
| 165 |
flavour = getlong(sfnt); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
num_tabs = getushort(sfnt); |
| 170 |
for ( i=1, j=0; 2*i<=num_tabs; i<<=1, ++j ); |
| 171 |
test = getushort(sfnt); |
| 172 |
if ( test!=16*i ) { |
| 173 |
fprintf( stderr, "%s has an invalid sfnt header (1) %d!=%d\n", filename, test, 16*i ); |
| 174 |
fclose(sfnt); |
| 175 |
return; |
| 176 |
} |
| 177 |
test = getushort(sfnt); |
| 178 |
if ( test!=j ) { |
| 179 |
fprintf( stderr, "%s has an invalid sfnt header (2) %d!=%d\n", filename, test, j ); |
| 180 |
fclose(sfnt); |
| 181 |
return; |
| 182 |
} |
| 183 |
test = getushort(sfnt); |
| 184 |
if ( test!=(num_tabs-i)*16 ) { |
| 185 |
fprintf( stderr, "%s has an invalid sfnt header (3) %d!=%d\n", filename, test, (num_tabs-i)*16 ); |
| 186 |
fclose(sfnt); |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
outname = malloc(strlen(start)+20); |
| 191 |
strcpy(outname,start); |
| 192 |
strcpy(outname+(ext-start),".woff" ); |
| 193 |
woff = fopen(outname,"wb"); |
| 194 |
if ( woff==NULL ) { |
| 195 |
fprintf( stderr, "Failed to create output file: %s\n", outname ); |
| 196 |
fclose(sfnt); |
| 197 |
free(outname); |
| 198 |
return; |
| 199 |
} |
| 200 |
putlong(woff,CHR('w','O','F','F')); |
| 201 |
putlong(woff,flavour); |
| 202 |
putlong(woff,0); |
| 203 |
putshort(woff,num_tabs); |
| 204 |
putshort(woff,0); |
| 205 |
putlong(woff,filelen); |
| 206 |
putshort(woff,major); |
| 207 |
putshort(woff,minor); |
| 208 |
putlong(woff,0); |
| 209 |
putlong(woff,0); |
| 210 |
putlong(woff,0); |
| 211 |
putlong(woff,0); |
| 212 |
putlong(woff,0); |
| 213 |
|
| 214 |
tab_start = ftell(woff); |
| 215 |
for ( i=0; i<5*num_tabs; ++i ) |
| 216 |
putlong(woff,0); |
| 217 |
|
| 218 |
for ( i=0; i<num_tabs; ++i ) { |
| 219 |
tag = getlong(sfnt); |
| 220 |
checksum = getlong(sfnt); |
| 221 |
offset = getlong(sfnt); |
| 222 |
uncompLen = getlong(sfnt); |
| 223 |
here = ftell(sfnt); |
| 224 |
newoffset = ftell(woff); |
| 225 |
compLen = compressOrNot(woff,newoffset,sfnt,offset,uncompLen,false); |
| 226 |
if ( (ftell(woff)&3)!=0 ) { |
| 227 |
|
| 228 |
if ( ftell(woff)&1 ) |
| 229 |
putc('\0',woff); |
| 230 |
if ( ftell(woff)&2 ) |
| 231 |
putshort(woff,0); |
| 232 |
} |
| 233 |
fseek(sfnt,here,SEEK_SET); |
| 234 |
fseek(woff,tab_start,SEEK_SET); |
| 235 |
putlong(woff,tag); |
| 236 |
putlong(woff,newoffset); |
| 237 |
putlong(woff,compLen); |
| 238 |
putlong(woff,uncompLen); |
| 239 |
putlong(woff,checksum); |
| 240 |
tab_start = ftell(woff); |
| 241 |
fseek(woff,0,SEEK_END); |
| 242 |
} |
| 243 |
fclose(sfnt); |
| 244 |
|
| 245 |
meta = NULL; |
| 246 |
if ( metafile!=NULL ) { |
| 247 |
meta = fopen(metafile,"w"); |
| 248 |
if ( meta==NULL ) |
| 249 |
fprintf( stderr, "Failed to open meta data file: %s\n", metafile ); |
| 250 |
} |
| 251 |
if ( meta!=NULL ) { |
| 252 |
newoffset = ftell(woff); |
| 253 |
fseek(meta,0,SEEK_END); |
| 254 |
uncompLen = ftell(meta); |
| 255 |
rewind( meta ); |
| 256 |
compLen = compressOrNot(woff,newoffset,meta,0,uncompLen,true); |
| 257 |
if ( privfile!=NULL && (ftell(woff)&3)!=0 ) { |
| 258 |
|
| 259 |
if ( ftell(woff)&1 ) |
| 260 |
putc('\0',woff); |
| 261 |
if ( ftell(woff)&2 ) |
| 262 |
putshort(woff,0); |
| 263 |
} |
| 264 |
fseek(woff,24,SEEK_SET); |
| 265 |
putlong(woff,newoffset); |
| 266 |
putlong(woff,compLen); |
| 267 |
putlong(woff,uncompLen); |
| 268 |
fseek(woff,0,SEEK_END); |
| 269 |
fclose(meta); |
| 270 |
} |
| 271 |
|
| 272 |
priv = NULL; |
| 273 |
if ( privfile!=NULL ) { |
| 274 |
priv = fopen(privfile,"w"); |
| 275 |
if ( priv==NULL ) |
| 276 |
fprintf( stderr, "Failed to open private file: %s\n", privfile ); |
| 277 |
} |
| 278 |
if ( priv!=NULL ) { |
| 279 |
newoffset = ftell(woff); |
| 280 |
fseek(priv,0,SEEK_END); |
| 281 |
uncompLen = ftell(priv); |
| 282 |
rewind( priv ); |
| 283 |
copydata(woff,newoffset,priv,0,uncompLen); |
| 284 |
|
| 285 |
fseek(woff,36,SEEK_SET); |
| 286 |
putlong(woff,newoffset); |
| 287 |
putlong(woff,uncompLen); |
| 288 |
fseek(woff,0,SEEK_END); |
| 289 |
fclose(priv); |
| 290 |
} |
| 291 |
len = ftell(woff); |
| 292 |
fseek(woff,8,SEEK_SET); |
| 293 |
putlong(woff,len); |
| 294 |
fclose(woff); |
| 295 |
free(outname); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
static void usage(char *prog) { |
| 300 |
printf( "%s {[-meta xml-file] [-priv file] [-major num] [-minor num] otf/ttf-filename}\n", prog ); |
| 301 |
printf( " Takes one (or several sfnt files, and converts them into their equivalent\n" ); |
| 302 |
printf( "woff forms. The output files will be placed in the current directory\n" ); |
| 303 |
printf( "with the same name as the original file and an extension \".woff\". If\n" ); |
| 304 |
printf( "you wish to include a metadata table, or a private table put them in\n" ); |
| 305 |
printf( "their own files and specify them with -meta or -priv arguments\n" ); |
| 306 |
} |
| 307 |
|
| 308 |
int main(int argc, char **argv) { |
| 309 |
int i; |
| 310 |
char *metafile=NULL, *privfile=NULL, *pt; |
| 311 |
int major=1, minor=0; |
| 312 |
|
| 313 |
for ( i=1; i<argc; ++i ) { |
| 314 |
if ( *argv[i]=='-' ) { |
| 315 |
pt = argv[i]; |
| 316 |
if ( *pt=='-' && pt[1]=='-' ) |
| 317 |
++pt; |
| 318 |
if ( i+1<argc ) { |
| 319 |
if ( strcasecmp(pt,"-meta")==0 || strcasecmp(pt,"-metadata")==0 ) { |
| 320 |
metafile = argv[++i]; |
| 321 |
continue; |
| 322 |
} else if ( strcasecmp(pt,"-priv")==0 || strcasecmp(pt,"-private")==0 ) { |
| 323 |
privfile = argv[++i]; |
| 324 |
continue; |
| 325 |
} else if ( strcasecmp(pt,"-major")==0 ) { |
| 326 |
major = strtol(argv[++i],NULL,10); |
| 327 |
continue; |
| 328 |
} else if ( strcasecmp(pt,"-minor")==0 ) { |
| 329 |
minor = strtol(argv[++i],NULL,10); |
| 330 |
continue; |
| 331 |
} |
| 332 |
} |
| 333 |
usage(argv[0]); |
| 334 |
continue; |
| 335 |
} |
| 336 |
woff(argv[i],metafile,privfile,major,minor); |
| 337 |
metafile=NULL; privfile=NULL; |
| 338 |
major = 1; minor = 0; |
| 339 |
} |
| 340 |
return( 0 ); |
| 341 |
} |