-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttf2eot.cpp
103 lines (89 loc) · 2.61 KB
/
ttf2eot.cpp
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
/* Trivial utility to create EOT files on Linux */
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <vector>
#ifndef _MSC_VER
# include <stdint.h>
#else
typedef unsigned char uint8_t;
#endif
#if defined(_MSC_VER) || defined(__MINGW32__)
# include <fcntl.h>
# ifndef _O_BINARY
# define _O_BINARY 0x8000
# endif
# ifndef _O_TEXT
# define _O_TEXT 0x4000
# endif
#endif
#include "OpenTypeUtilities.h"
#ifndef SIZE_MAX
# define SIZE_MAX UINT_MAX
#endif
using std::vector;
int main(int argc, char **argv)
{
const size_t kFontInitSize = 8192;
vector<uint8_t> eotHeader(512);
size_t overlayDst = 0;
size_t overlaySrc = 0;
size_t overlayLength = 0;
size_t fontSize = kFontInitSize;
size_t fontOff = 0;
FILE *input, *output;
unsigned char *fontData;
#if defined(_MSC_VER) || defined(__MINGW32__)
setmode(_fileno(stdin), _O_BINARY);
setmode(_fileno(stdout), _O_BINARY);
setmode(_fileno(stderr), _O_TEXT);
#endif
if (argv[1] == NULL || (argv[1][0] == '-' && argv[1][1] == '\0')) {
input = stdin;
} else {
input = fopen(argv[1], "rb");
if (input == NULL) {
fprintf(stderr, "could not open input file %s, %m\n", argv[1]);
return 1;
}
}
if (argv[1] == NULL || argv[2] == NULL || (argv[2][0] == '-' && argv[2][1] == '\0')) {
output = stdout;
} else {
output = fopen(argv[2], "wb");
if (output == NULL) {
fprintf(stderr, "could not open output file %s, %m\n", argv[2]);
return 1;
}
}
if ((fontData = (unsigned char *) malloc(fontSize)) == NULL) {
fprintf(stderr, "Allocation failure, %m\n");
return 1;
}
do {
size_t ret = fread(fontData + fontOff, 1, fontSize - fontOff, input);
if (ret && fontSize <= SIZE_MAX / 2) {
fontOff += ret;
if ((fontData = (unsigned char *) realloc(fontData, fontSize *= 2)) == NULL) {
fprintf(stderr, "Allocation failure, %m\n");
return 1;
}
} else if (ret) {
fprintf(stderr, "Too much data, %m\n");
return 1;
} else {
fontData = (unsigned char *) realloc(fontData, fontSize = fontOff);
break;
}
} while (true);
if (getEOTHeader(fontData, fontSize, eotHeader, overlayDst, overlaySrc, overlayLength)) {
fwrite(&eotHeader[0], eotHeader.size(), 1, output);
fwrite(fontData, fontSize, 1, output);
return 0;
} else {
fprintf(stderr, "unknown error parsing input font, %m\n");
return 1;
}
}