1 #include "pdfutils.h"
2 #include "config.h"
3 #include <assert.h>
4 #include "fontembed/embed.h"
5 #include "fontembed/sfnt.h"
6
7 #include <stdio.h>
8
write_string(pdfOut * pdf,EMB_PARAMS * emb,const char * str)9 static inline void write_string(pdfOut *pdf,EMB_PARAMS *emb,const char *str) // {{{
10 {
11 assert(pdf);
12 assert(emb);
13 int iA;
14
15 if (emb->plan&EMB_A_MULTIBYTE) {
16 putc('<',stdout);
17 for (iA=0;str[iA];iA++) {
18 const unsigned short gid=emb_get(emb,(unsigned char)str[iA]);
19 fprintf(stdout,"%04x",gid);
20 }
21 putc('>',stdout);
22 pdf->filepos+=4*iA+2;
23 } else {
24 for (iA=0;str[iA];iA++) {
25 emb_get(emb,(unsigned char)str[iA]);
26 // TODO: pdf: otf_from_pdf_default_encoding
27 }
28 pdfOut_putString(pdf,str,-1);
29 }
30 }
31 // }}}
32
main()33 int main()
34 {
35 pdfOut *pdf;
36
37 pdf=pdfOut_new();
38 assert(pdf);
39
40 pdfOut_begin_pdf(pdf);
41
42 // font, pt.1
43 const char *fn=TESTFONT;
44 OTF_FILE *otf=NULL;
45 /*
46 if (argc==2) {
47 fn=argv[1];
48 }
49 */
50 otf=otf_load(fn);
51 if (!otf)
52 {
53 printf("Font %s was not loaded, exiting.\n", TESTFONT);
54 return 1;
55 }
56 assert(otf);
57 FONTFILE *ff=fontfile_open_sfnt(otf);
58 EMB_PARAMS *emb=emb_new(ff,
59 EMB_DEST_PDF16,
60 EMB_C_FORCE_MULTIBYTE|
61 EMB_C_TAKE_FONTFILE);
62
63 // test
64 const int PageWidth=595,PageLength=842;
65 const int cobj=pdfOut_add_xref(pdf);
66 pdfOut_printf(pdf,"%d 0 obj\n"
67 "<</Length %d 0 R\n"
68 ">>\n"
69 "stream\n"
70 ,cobj,cobj+1);
71 long streamlen=-pdf->filepos;
72 pdfOut_printf(pdf,"BT /a 10 Tf ");
73 write_string(pdf,emb,"Test");
74 pdfOut_printf(pdf," Tj ET");
75
76 streamlen+=pdf->filepos;
77 pdfOut_printf(pdf,"\nendstream\n"
78 "endobj\n");
79 const int clobj=pdfOut_add_xref(pdf);
80 assert(clobj==cobj+1);
81 pdfOut_printf(pdf,"%d 0 obj\n"
82 "%d\n"
83 "endobj\n"
84 ,clobj,streamlen);
85
86 // font
87 int font_obj=pdfOut_write_font(pdf,emb);
88 assert(font_obj);
89
90 int obj=pdfOut_add_xref(pdf);
91 pdfOut_printf(pdf,"%d 0 obj\n"
92 "<</Type/Page\n"
93 " /Parent 1 0 R\n"
94 " /MediaBox [0 0 %d %d]\n"
95 " /Contents %d 0 R\n"
96 " /Resources << /Font << /a %d 0 R >> >>\n"
97 ">>\n"
98 "endobj\n"
99 ,obj,PageWidth,PageLength,cobj,font_obj); // TODO: into pdf->
100 pdfOut_add_page(pdf,obj);
101 pdfOut_finish_pdf(pdf);
102
103 pdfOut_free(pdf);
104
105 emb_close(emb);
106
107 return 0;
108 }
109