-
Notifications
You must be signed in to change notification settings - Fork 1
/
embed.c
271 lines (248 loc) · 7.42 KB
/
embed.c
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
#include "embed.h"
#include "embed_sfnt_int.h"
#include "sfnt_int.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static inline int copy_file(FILE *f,OUTPUT_FN output,void *context) // {{{
{
assert(f);
assert(output);
char buf[4096];
int iA,ret=0;
ret=0;
rewind(f);
do {
iA=fread(buf,1,4096,f);
(*output)(buf,iA,context);
ret+=iA;
} while (iA>0);
return ret;
}
// }}}
static inline int copy_otf(OTF_FILE *otf,OUTPUT_FN output,void *context) // {{{
{
if (otf->flags&OTF_F_WOFF) {
return otf_copy_sfnt(otf,NULL,output,context);
} else {
return copy_file(otf->f,output,context);
}
}
// }}}
/* certain profiles: (=> constraints to be auto-applied in emb_new via >dest)
PSold: T1->T1, TTF->T1, OTF->CFF->T1, STD->STD // output limit: T1 (maybe length, binary/text, ... limit)
PS1: T1->T1, TTF->T1, OTF->CFF, STD->STD // output limit: T1,CFF [does this really exists?]
PS2: T1->T1, TTF->TTF, OTF->T1, STD->STD // output limit: T1,TTF
PS3: T1->T1, TTF->TTF, OTF->CFF, STD->STD
PDF12/13?: OTF->CFF
PDF16: OTF->OTF (,T1->CFF?)
--- rename KEEP_T1 to NEED_T1? NO_T42?
converters:
OTF->CFF, CFF->OTF (extract metrics, etc)
(T1->CFF, CFF->T1)
((TTF->T1 ['good'; T1->TTF: not good]))
[subsetTTF,subsetCFF,subsetT1]
output modes:
subset,CID(multibyte),(PS:)text/binary,(PS:)incremental
TODO: remove dest from emb_new, replace with EMB_ACTIONS constraints:
- bitfield mask which ACTIONS are allowed. (problem: we want to force certain ones, e.g. MULTIBYTE)
- e.g. currently EMB_C_PDF_OT has to functions
- the only (other) 'difference' to now is the subsetting spec
- another issue is, that emb_pdf_ might want some pdf version informatino (->extra flag?)
and funtion to determine appropriate mask for certain destination
EMB_ACTIONS emb_mask_for_dest(EMB_DESTINATION)
TODO? determine viability before trying emb_embed
(idea: use emb_embed(,NULL) -> will just return true/false [same codepath!])
TODO?! "always subset CJK"
*/
EMB_PARAMS *emb_new(FONTFILE *font,EMB_DESTINATION dest,EMB_CONSTRAINTS mode) // {{{
{
assert(font);
EMB_PARAMS *ret=calloc(1,sizeof(EMB_PARAMS));
if (!ret) {
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
if (mode&EMB_C_TAKE_FONTFILE) {
fontfile_close(font);
}
return NULL;
}
ret->dest=dest;
ret->font=font;
if (mode&EMB_C_TAKE_FONTFILE) {
ret->plan|=EMB_A_CLOSE_FONTFILE;
}
// check parameters
if ( (mode&EMB_C_KEEP_T1)&&(mode&EMB_C_FORCE_MULTIBYTE) ) {
fprintf(stderr,"Incompatible mode: KEEP_T1 and FORCE_MULTIBYTE\n");
emb_close(ret);
return NULL;
}
if ((mode&0x07)>5) {
fprintf(stderr,"Bad subset specification\n");
emb_close(ret);
return NULL;
}
// determine intype
int numGlyphs=0;
if (font->sfnt) {
if (font->sfnt->flags&OTF_F_FMT_CFF) {
ret->intype=EMB_FMT_OTF;
} else {
ret->intype=EMB_FMT_TTF;
}
ret->rights=emb_otf_get_rights(ret->font->sfnt);
numGlyphs=ret->font->sfnt->numGlyphs; // TODO
} else if (font->stdname) {
ret->intype=EMB_FMT_STDFONT;
ret->rights=EMB_RIGHT_NONE;
} else {
assert(0);
}
/*
if ( (ret->intype==EMB_FMT_CFF)&&
(ret->cffFont.is_cid()) ) {
?= || ( (ret->intype==EMB_FMT_OTF)&&(ret->sfnt->cffFont.is_cid()) ) // TODO?
ret->plan|=EMB_A_MULTIBYTE;
}
*/
// determine outtype
if (ret->intype==EMB_FMT_STDFONT) {
ret->outtype=ret->intype;
if (mode&EMB_C_FORCE_MULTIBYTE) {
fprintf(stderr,"Multibyte stdfonts are not possible\n");
emb_close(ret);
return NULL;
}
return ret; // never subset, no multibyte
} else if (ret->intype==EMB_FMT_T1) {
if (mode&EMB_C_KEEP_T1) {
ret->outtype=EMB_FMT_T1;
} else {
ret->plan|=EMB_A_T1_TO_CFF;
ret->outtype=EMB_FMT_CFF;
}
} else {
ret->outtype=ret->intype;
}
if (ret->outtype==EMB_FMT_CFF) {
if (mode&EMB_C_PDF_OT) {
ret->outtype=EMB_FMT_OTF;
ret->plan|=EMB_A_CFF_TO_OTF;
}
} else if (ret->outtype==EMB_FMT_OTF) {
// TODO: no support yet; but we want to get the FontDescriptor/Name right
mode|=EMB_C_NEVER_SUBSET;
if (!(mode&EMB_C_PDF_OT)) { // TODO!?!
ret->outtype=EMB_FMT_CFF;
ret->plan|=EMB_A_OTF_TO_CFF;
}
}
if (mode&EMB_C_FORCE_MULTIBYTE) {
ret->plan|=EMB_A_MULTIBYTE;
}
// check rights (for subsetting)
if ( (ret->rights&EMB_RIGHT_NONE)||
(ret->rights&EMB_RIGHT_BITMAPONLY)||
( (ret->rights&EMB_RIGHT_READONLY)&&(mode&EMB_C_EDITABLE_SUBSET) )||
( (ret->rights&EMB_RIGHT_NO_SUBSET)&&(mode&EMB_C_MUST_SUBSET) ) ) {
fprintf(stderr,"The font does not permit the requested embedding\n");
emb_close(ret);
return NULL;
} else if ( (!(ret->rights&EMB_RIGHT_NO_SUBSET))&&
(!(mode&EMB_C_NEVER_SUBSET)) ) {
ret->plan|=EMB_A_SUBSET;
}
// alloc subset
if (ret->plan&EMB_A_SUBSET) {
ret->subset=bitset_new(numGlyphs);
if (!ret->subset) {
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
emb_close(ret);
return NULL;
}
}
return ret;
}
// }}}
int emb_embed(EMB_PARAMS *emb,OUTPUT_FN output,void *context) // {{{
{
assert(emb);
if (emb->dest==EMB_DEST_NATIVE) {
} else if (emb->dest<=EMB_DEST_PS) {
int ret=-2;
const char *fontname=emb_otf_get_fontname(emb->font->sfnt); // TODO!!
(*output)("%%BeginFont: ",13,context);
(*output)(fontname,strlen(fontname),context);
(*output)("\n",1,context);
if (emb->outtype==EMB_FMT_T1) {
} else if (emb->outtype==EMB_FMT_TTF) { // emb->outtype==EMB_OUTPUT_OTF is stupid (?)
// do Type42
ret=emb_otf_ps(emb->font->sfnt,NULL,256,NULL,output,context); // TODO?
} else if (emb->outtype==EMB_FMT_CFF) {
} else if (emb->outtype==EMB_FMT_STDFONT) {
}
if (ret!=-2) {
if (ret!=-1) {
(*output)("%%EndFont\n",10,context);
} else {
fprintf(stderr,"Failed\n");
}
return ret;
}
} else if (emb->dest<=EMB_DEST_PDF16) {
if (emb->outtype==EMB_FMT_TTF) {
assert(emb->font->sfnt);
if (emb->plan&EMB_A_SUBSET) {
return otf_subset(emb->font->sfnt,emb->subset,output,context);
} else if (emb->font->sfnt->numTTC) { // simply copy the currently loaded numTTC
return otf_ttc_extract(emb->font->sfnt,output,context);
} else { // copy verbatim
return copy_otf(emb->font->sfnt,output,context);
}
} else if (emb->outtype==EMB_FMT_OTF) {
if (emb->plan&EMB_A_CFF_TO_OTF) {
if (emb->plan&EMB_A_T1_TO_CFF) {
// TODO
} else {
// assert(emb->font->cff);
// TODO
}
} else {
assert(emb->font->sfnt);
if (emb->plan&EMB_A_SUBSET) {
return otf_subset_cff(emb->font->sfnt,emb->subset,output,context);
} else {
return copy_otf(emb->font->sfnt,output,context);
}
}
} else if (emb->outtype==EMB_FMT_CFF) {
if (emb->plan&EMB_A_OTF_TO_CFF) {
assert(emb->font->sfnt);
if (emb->plan&EMB_A_SUBSET) {
// TODO
} else {
return otf_cff_extract(emb->font->sfnt,output,context);
}
} else {
// TODO
}
}
}
fprintf(stderr,"NOT IMPLEMENTED\n");
assert(0);
return -1;
}
// }}}
void emb_close(EMB_PARAMS *emb) // {{{
{
if (emb) {
free(emb->subset);
if (emb->plan&EMB_A_CLOSE_FONTFILE) {
fontfile_close(emb->font);
}
free(emb);
}
}
// }}}