1 #include "embed.h"
2 #include "config.h"
3 #include "sfnt.h"
4 #include <assert.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
example_outfn(const char * buf,int len,void * context)8 static void example_outfn(const char *buf,int len,void *context) // {{{
9 {
10 FILE *f=(FILE *)context;
11 if (fwrite(buf,1,len,f)!=len) {
12 fprintf(stderr,"Short write: %m\n");
13 assert(0);
14 return;
15 }
16 }
17 // }}}
18
19 #define OBJ \
20 xref[xrefpos++]=ftell(f); \
21 fprintf(f,"%d 0 obj\n",xrefpos);
22
23 #define ENDOBJ \
24 fprintf(f,"endobj\n");
25
26 #define STREAMDICT \
27 OBJ; \
28 fprintf(f,"<<\n" \
29 " /Length %d 0 R\n",xrefpos+1);
30
31 #define STREAMDATA \
32 fprintf(f,">>\n" \
33 "stream\n"); \
34 stream_len=-ftell(f);
35
36 #define STREAM \
37 STREAMDICT \
38 STREAMDATA
39
40 #define ENDSTREAM \
41 stream_len+=ftell(f); \
42 fprintf(f,"endstream\n" \
43 "endobj\n"); \
44 OBJ; \
45 fprintf(f,"%d\n",stream_len); \
46 ENDOBJ;
47
write_string(FILE * f,EMB_PARAMS * emb,const char * str)48 static inline void write_string(FILE *f,EMB_PARAMS *emb,const char *str) // {{{
49 {
50 assert(f);
51 assert(emb);
52 int iA;
53
54 if (emb->plan&EMB_A_MULTIBYTE) {
55 putc('<',f);
56 for (iA=0;str[iA];iA++) {
57 const unsigned short gid=emb_get(emb,(unsigned char)str[iA]);
58 fprintf(f,"%04x",gid);
59 }
60 putc('>',f);
61 } else {
62 putc('(',f);
63 for (iA=0;str[iA];iA++) {
64 emb_get(emb,(unsigned char)str[iA]);
65 }
66 fprintf(f,"%s",str); // TODO
67 putc(')',f);
68 }
69 }
70 // }}}
71
main(int argc,char ** argv)72 int main(int argc,char **argv)
73 {
74 const char *fn=TESTFONT;
75 OTF_FILE *otf=NULL;
76 if (argc==2) {
77 fn=argv[1];
78 }
79 otf=otf_load(fn);
80 if (!otf)
81 {
82 printf("Font %s was not loaded, exiting.\n", TESTFONT);
83 return 1;
84 }
85 assert(otf);
86 FONTFILE *ff=fontfile_open_sfnt(otf);
87 EMB_PARAMS *emb=emb_new(ff,
88 EMB_DEST_PDF16,
89 EMB_C_FORCE_MULTIBYTE|
90 EMB_C_TAKE_FONTFILE);
91
92 FILE *f=fopen("test.pdf","w");
93 assert(f);
94 int xref[100],xrefpos=3;
95 int stream_len;
96
97 fprintf(f,"%%PDF-1.3\n");
98 // content
99 STREAM;
100 fprintf(f,"BT\n" // content
101 " 100 100 Td\n"
102 " /F1 10 Tf\n");
103 write_string(f,emb,"Hallo");
104 fprintf(f," Tj\n"
105 "ET\n");
106 ENDSTREAM;
107
108 emb_get(emb,'a');
109
110 // {{{ do font
111 EMB_PDF_FONTDESCR *fdes=emb_pdf_fontdescr(emb);
112 assert(fdes);
113 EMB_PDF_FONTWIDTHS *fwid=emb_pdf_fontwidths(emb);
114 assert(fwid);
115
116 STREAMDICT;
117 int ff_ref=xrefpos;
118 if (emb_pdf_get_fontfile_subtype(emb)) {
119 fprintf(f," /Subtype /%s\n",
120 emb_pdf_get_fontfile_subtype(emb));
121 }
122 if (emb->outtype==EMB_FMT_T1) {
123 fprintf(f," /Length1 ?\n"
124 " /Length2 ?\n"
125 " /Length3 ?\n");
126 } else if (emb->outtype==EMB_FMT_TTF) {
127 fprintf(f," /Length1 %d 0 R\n",xrefpos+2);
128 }
129 STREAMDATA;
130 const int outlen=emb_embed(emb,example_outfn,f);
131 ENDSTREAM;
132 if (emb->outtype==EMB_FMT_TTF) {
133 OBJ;
134 fprintf(f,"%d\n",outlen);
135 ENDOBJ;
136 }
137
138 OBJ;
139 const int fd_ref=xrefpos;
140 char *res=emb_pdf_simple_fontdescr(emb,fdes,ff_ref);
141 assert(res);
142 fputs(res,f);
143 free(res);
144 ENDOBJ;
145
146 OBJ;
147 int f_ref=xrefpos;
148 res=emb_pdf_simple_font(emb,fdes,fwid,fd_ref);
149 assert(res);
150 fputs(res,f);
151 free(res);
152 ENDOBJ;
153
154 if (emb->plan&EMB_A_MULTIBYTE) {
155 OBJ;
156 res=emb_pdf_simple_cidfont(emb,fdes->fontname,f_ref);
157 f_ref=xrefpos;
158 assert(res);
159 fputs(res,f);
160 free(res);
161 ENDOBJ;
162 }
163
164 free(fdes);
165 free(fwid);
166 // }}}
167
168 int iA;
169
170 xref[2]=ftell(f);
171 fprintf(f,"3 0 obj\n"
172 "<</Type/Page\n"
173 " /Parent 2 0 R\n"
174 " /MediaBox [0 0 595 842]\n"
175 " /Contents 4 0 R\n"
176 " /Resources <<\n"
177 " /Font <<\n"
178 " /F1 %d 0 R\n"
179 " >>\n"
180 " >>\n"
181 ">>\n"
182 "endobj\n",
183 f_ref);
184 xref[1]=ftell(f);
185 fprintf(f,"2 0 obj\n"
186 "<</Type/Pages\n"
187 " /Count 1\n"
188 " /Kids [3 0 R]"
189 ">>\n"
190 "endobj\n");
191 xref[0]=ftell(f);
192 fprintf(f,"1 0 obj\n"
193 "<</Type/Catalog\n"
194 " /Pages 2 0 R\n"
195 ">>\n"
196 "endobj\n");
197 // {{{ pdf trailer
198 int xref_start=ftell(f);
199 fprintf(f,"xref\n"
200 "0 %d\n"
201 "%010d 65535 f \n",
202 xrefpos+1,0);
203 for (iA=0;iA<xrefpos;iA++) {
204 fprintf(f,"%010d 00000 n \n",xref[iA]);
205 }
206 fprintf(f,"trailer\n"
207 "<<\n"
208 " /Size %d\n"
209 " /Root 1 0 R\n"
210 ">>\n"
211 "startxref\n"
212 "%d\n"
213 "%%%%EOF\n",
214 xrefpos+1,xref_start);
215 // }}}
216 fclose(f);
217
218 emb_close(emb);
219
220 return 0;
221 }
222