1 #include "embed.h"
2 #include "embed_sfnt_int.h"
3 #include <assert.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8
copy_file(FILE * f,OUTPUT_FN output,void * context)9 static inline int copy_file(FILE *f,OUTPUT_FN output,void *context) // {{{
10 {
11 assert(f);
12 assert(output);
13
14 char buf[4096];
15 int iA,ret=0;
16
17 ret=0;
18 rewind(f);
19 do {
20 iA=fread(buf,1,4096,f);
21 (*output)(buf,iA,context);
22 ret+=iA;
23 } while (iA>0);
24 return ret;
25 }
26 // }}}
27
28 /* certain profiles: (=> constraints to be auto-applied in emb_new via >dest)
29 PSold: T1->T1, TTF->T1, OTF->CFF->T1, STD->STD // output limit: T1 (maybe length, binary/text, ... limit)
30 PS1: T1->T1, TTF->T1, OTF->CFF, STD->STD // output limit: T1,CFF [does this really exists?]
31 PS2: T1->T1, TTF->TTF, OTF->T1, STD->STD // output limit: T1,TTF
32 PS3: T1->T1, TTF->TTF, OTF->CFF, STD->STD
33 PDF12/13?: OTF->CFF
34 PDF16: OTF->OTF (,T1->CFF?)
35 --- rename KEEP_T1 to NEED_T1? NO_T42?
36
37 converters:
38 OTF->CFF, CFF->OTF (extract metrics, etc)
39 (T1->CFF, CFF->T1)
40 ((TTF->T1 ['good'; T1->TTF: not good]))
41 [subsetTTF,subsetCFF,subsetT1]
42
43 output modes:
44 subset,CID(multibyte),(PS:)text/binary,(PS:)incremental
45
46 TODO: remove dest from emb_new, replace with EMB_ACTIONS constraints:
47 - bitfield mask which ACTIONS are allowed. (problem: we want to force certain ones, e.g. MULTIBYTE)
48 - e.g. currently EMB_C_PDF_OT has to functions
49 - the only (other) 'difference' to now is the subsetting spec
50 - another issue is, that emb_pdf_ might want some pdf version informatino (->extra flag?)
51 and funtion to determine appropriate mask for certain destination
52 EMB_ACTIONS emb_mask_for_dest(EMB_DESTINATION)
53 TODO? determine viability before trying emb_embed
54 (idea: use emb_embed(,NULL) -> will just return true/false [same codepath!])
55
56 TODO?! "always subset CJK"
57 */
58
emb_new(FONTFILE * font,EMB_DESTINATION dest,EMB_CONSTRAINTS mode)59 EMB_PARAMS *emb_new(FONTFILE *font,EMB_DESTINATION dest,EMB_CONSTRAINTS mode) // {{{
60 {
61 assert(font);
62
63 EMB_PARAMS *ret=calloc(1,sizeof(EMB_PARAMS));
64 if (!ret) {
65 fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
66 if (mode&EMB_C_TAKE_FONTFILE) {
67 fontfile_close(font);
68 }
69 return NULL;
70 }
71 ret->dest=dest;
72 ret->font=font;
73 if (mode&EMB_C_TAKE_FONTFILE) {
74 ret->plan|=EMB_A_CLOSE_FONTFILE;
75 }
76
77 // check parameters
78 if ( (mode&EMB_C_KEEP_T1)&&(mode&EMB_C_FORCE_MULTIBYTE) ) {
79 fprintf(stderr,"Incompatible mode: KEEP_T1 and FORCE_MULTIBYTE\n");
80 emb_close(ret);
81 return NULL;
82 }
83 if ((mode&0x07)>5) {
84 fprintf(stderr,"Bad subset specification\n");
85 emb_close(ret);
86 return NULL;
87 }
88
89 // determine intype
90 int numGlyphs=0;
91 if (font->sfnt) {
92 if (font->sfnt->flags&OTF_F_FMT_CFF) {
93 ret->intype=EMB_FMT_OTF;
94 } else {
95 ret->intype=EMB_FMT_TTF;
96 }
97 ret->rights=emb_otf_get_rights(ret->font->sfnt);
98 numGlyphs=ret->font->sfnt->numGlyphs; // TODO
99 } else if (font->stdname) {
100 ret->intype=EMB_FMT_STDFONT;
101 ret->rights=EMB_RIGHT_NONE;
102 } else {
103 assert(0);
104 }
105 /*
106 if ( (ret->intype==EMB_FMT_CFF)&&
107 (ret->cffFont.is_cid()) ) {
108 ?= || ( (ret->intype==EMB_FMT_OTF)&&(ret->sfnt->cffFont.is_cid()) ) // TODO?
109 ret->plan|=EMB_A_MULTIBYTE;
110 }
111 */
112
113 // determine outtype
114 if (ret->intype==EMB_FMT_STDFONT) {
115 ret->outtype=ret->intype;
116 if (mode&EMB_C_FORCE_MULTIBYTE) {
117 fprintf(stderr,"Multibyte stdfonts are not possible\n");
118 emb_close(ret);
119 return NULL;
120 }
121 return ret; // never subset, no multibyte
122 } else if (ret->intype==EMB_FMT_T1) {
123 if (mode&EMB_C_KEEP_T1) {
124 ret->outtype=EMB_FMT_T1;
125 } else {
126 ret->plan|=EMB_A_T1_TO_CFF;
127 ret->outtype=EMB_FMT_CFF;
128 }
129 } else {
130 ret->outtype=ret->intype;
131 }
132 if (ret->outtype==EMB_FMT_CFF) {
133 if (mode&EMB_C_PDF_OT) {
134 ret->outtype=EMB_FMT_OTF;
135 ret->plan|=EMB_A_CFF_TO_OTF;
136 }
137 } else if (ret->outtype==EMB_FMT_OTF) {
138 // TODO: no support yet; but we want to get the FontDescriptor/Name right
139 mode|=EMB_C_NEVER_SUBSET;
140 if (!(mode&EMB_C_PDF_OT)) { // TODO!?!
141 ret->outtype=EMB_FMT_CFF;
142 ret->plan|=EMB_A_OTF_TO_CFF;
143 }
144 }
145
146 if (mode&EMB_C_FORCE_MULTIBYTE) {
147 ret->plan|=EMB_A_MULTIBYTE;
148 }
149
150 // check rights (for subsetting)
151 if ( (ret->rights&EMB_RIGHT_NONE)||
152 (ret->rights&EMB_RIGHT_BITMAPONLY)||
153 ( (ret->rights&EMB_RIGHT_READONLY)&&(mode&EMB_C_EDITABLE_SUBSET) )||
154 ( (ret->rights&EMB_RIGHT_NO_SUBSET)&&(mode&EMB_C_MUST_SUBSET) ) ) {
155 fprintf(stderr,"The font does not permit the requested embedding\n");
156 emb_close(ret);
157 return NULL;
158 } else if ( (!(ret->rights&EMB_RIGHT_NO_SUBSET))&&
159 (!(mode&EMB_C_NEVER_SUBSET)) ) {
160 ret->plan|=EMB_A_SUBSET;
161 }
162
163 // alloc subset
164 if (ret->plan&EMB_A_SUBSET) {
165 ret->subset=bitset_new(numGlyphs);
166 if (!ret->subset) {
167 fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
168 emb_close(ret);
169 return NULL;
170 }
171 }
172
173 return ret;
174 }
175 // }}}
176
emb_embed(EMB_PARAMS * emb,OUTPUT_FN output,void * context)177 int emb_embed(EMB_PARAMS *emb,OUTPUT_FN output,void *context) // {{{
178 {
179 assert(emb);
180
181 if (emb->dest==EMB_DEST_NATIVE) {
182 } else if (emb->dest<=EMB_DEST_PS) {
183 int ret=-2;
184 const char *fontname=emb_otf_get_fontname(emb->font->sfnt); // TODO!!
185 (*output)("%%BeginFont: ",13,context);
186 (*output)(fontname,strlen(fontname),context);
187 (*output)("\n",1,context);
188 if (emb->outtype==EMB_FMT_T1) {
189 } else if (emb->outtype==EMB_FMT_TTF) { // emb->outtype==EMB_OUTPUT_OTF is stupid (?)
190 // do Type42
191 ret=emb_otf_ps(emb->font->sfnt,NULL,256,NULL,output,context); // TODO?
192 } else if (emb->outtype==EMB_FMT_CFF) {
193 } else if (emb->outtype==EMB_FMT_STDFONT) {
194 }
195 if (ret!=-2) {
196 if (ret!=-1) {
197 (*output)("%%EndFont\n",10,context);
198 } else {
199 fprintf(stderr,"Failed\n");
200 }
201 return ret;
202 }
203 } else if (emb->dest<=EMB_DEST_PDF16) {
204 if (emb->outtype==EMB_FMT_TTF) {
205 assert(emb->font->sfnt);
206 if (emb->plan&EMB_A_SUBSET) {
207 return otf_subset(emb->font->sfnt,emb->subset,output,context);
208 } else if (emb->font->sfnt->numTTC) { //
209 return otf_ttc_extract(emb->font->sfnt,output,context);
210 } else { // copy verbatim
211 return copy_file(emb->font->sfnt->f,output,context);
212 }
213 } else if (emb->outtype==EMB_FMT_OTF) {
214 if (emb->plan&EMB_A_CFF_TO_OTF) {
215 if (emb->plan&EMB_A_T1_TO_CFF) {
216 // TODO
217 } else {
218 // assert(emb->font->cff);
219 // TODO
220 }
221 } else {
222 assert(emb->font->sfnt);
223 if (emb->plan&EMB_A_SUBSET) {
224 return otf_subset_cff(emb->font->sfnt,emb->subset,output,context);
225 } else {
226 return copy_file(emb->font->sfnt->f,output,context);
227 }
228 }
229 } else if (emb->outtype==EMB_FMT_CFF) {
230 if (emb->plan&EMB_A_OTF_TO_CFF) {
231 assert(emb->font->sfnt);
232 if (emb->plan&EMB_A_SUBSET) {
233 // TODO
234 } else {
235 return otf_cff_extract(emb->font->sfnt,output,context);
236 }
237 } else {
238 // TODO
239 }
240 }
241 }
242
243 fprintf(stderr,"NOT IMPLEMENTED\n");
244 assert(0);
245 return -1;
246 }
247 // }}}
248
emb_close(EMB_PARAMS * emb)249 void emb_close(EMB_PARAMS *emb) // {{{
250 {
251 if (emb) {
252 free(emb->subset);
253 if (emb->plan&EMB_A_CLOSE_FONTFILE) {
254 fontfile_close(emb->font);
255 }
256 free(emb);
257 }
258 }
259 // }}}
260
261