Project

General

Profile

embed.c

Sergey Ivanovskiy, 01/02/2016 03:34 PM

Download (2.17 KB)

 
1
/*
2
 * This program is for setting TTF files to Installable Embedding mode.
3
 *
4
 * Note that using this to embed fonts which you are not licensed to embed
5
 * does not make it legal.
6
 *
7
 * This code was written by Tom Murphy 7, and is public domain. Use at your
8
 * own risk...
9
*/
10

    
11
#include <stdio.h>
12
#include <stdlib.h>
13

    
14
void fatal();
15

    
16
int main (int argc, char**argv) {
17
     FILE * inways;
18
     if (argc != 2)
19
        printf("Usage: %s font.ttf\n\nPublic Domain software by Tom 7. Use at your own risk.\n",argv[0]);
20
     else if (inways = fopen(argv[1],"rb+")) {
21
        int a,x;
22
        char type[5];
23
        type[4]=0;
24
        fseek(inways,12,0);
25
        for (;;) {
26
           for (x=0;x<4;x++) if (EOF == (type[x] = getc(inways))) fatal();
27
           if (!strcmp(type,"OS/2")) {
28
              int length;
29
              unsigned long loc, fstype, sum=0;
30
              loc=ftell(inways); /* location for checksum */
31
              for (x=4;x--;) if (EOF == getc(inways)) fatal();
32
              fstype  = fgetc(inways) << 24;
33
              fstype |= fgetc(inways) << 16;
34
              fstype |= fgetc(inways) << 8 ;
35
              fstype |= fgetc(inways)      ;
36
              length  = fgetc(inways) << 24;
37
              length |= fgetc(inways) << 16;
38
              length |= fgetc(inways) << 8 ;
39
              length |= fgetc(inways)      ;
40
/*              printf("fstype: %d length: %d\n",fstype,length);*/
41
              if (fseek(inways,fstype+8,0)) fatal();
42
              fputc(0,inways);
43
              fputc(0,inways);
44
              fseek(inways,fstype,0);
45
              for (x=length;x--;)
46
                  sum += fgetc(inways);
47
              fseek(inways,loc,0); /* write checksum */
48
              fputc(sum>>24,inways);
49
              fputc(255&(sum>>16),inways);
50
              fputc(255&(sum>>8), inways);
51
              fputc(255&sum    ,  inways);
52
              fclose(inways);
53
              exit(0);
54
           }
55
           for (x=12;x--;) if (EOF == getc(inways)) fatal();
56
        }
57

    
58
     } else
59
        printf("I wasn't able to open the file %s.\n", argv[1]);
60
}
61

    
62
void fatal() { fprintf(stderr,"Malformed TTF file.\n");
63
               exit(-1); }