2 * PDF backend for Halibut
10 #define TREE_BRANCH 8 /* max branching factor in page tree */
12 paragraph
*pdf_config_filename(char *filename
)
14 return cmdline_cfg_simple("pdf-filename", filename
, NULL
);
21 rdstringc main
, stream
;
31 static void pdf_string(void (*add
)(object
*, char const *),
32 object
*, char const *);
33 static void pdf_string_len(void (*add
)(object
*, char const *),
34 object
*, char const *, int);
35 static void objref(object
*o
, object
*dest
);
36 static void objdest(object
*o
, page_data
*p
);
38 static int is_std_font(char const *name
);
40 static void make_pages_node(object
*node
, object
*parent
, page_data
*first
,
41 page_data
*last
, object
*resources
,
43 static int make_outline(object
*parent
, outline_element
*start
, int n
,
45 static int pdf_versionid(FILE *fp
, word
*words
);
47 void pdf_backend(paragraph
*sourceform
, keywordlist
*keywords
,
48 indexdata
*idx
, void *vdoc
) {
49 document
*doc
= (document
*)vdoc
;
58 object
*o
, *info
, *cat
, *outlines
, *pages
, *resources
, *mediabox
;
64 filename
= dupstr("output.pdf");
65 for (p
= sourceform
; p
; p
= p
->next
) {
66 if (p
->type
== para_Config
) {
67 if (!ustricmp(p
->keyword
, L
"pdf-filename")) {
69 filename
= dupstr(adv(p
->origkeyword
));
74 olist
.head
= olist
.tail
= NULL
;
80 info
= new_object(&olist
);
81 objtext(info
, "<<\n");
82 if (doc
->n_outline_elements
> 0) {
87 pdf_outline_convert(doc
->outline_elements
->pdata
->outline_title
,
89 objtext(info
, "/Title ");
90 pdf_string_len(objtext
, info
, title
, titlelen
);
94 objtext(info
, "/Producer ");
95 sprintf(buf
, "Halibut, %s", version
);
96 pdf_string(objtext
, info
, buf
);
97 objtext(info
, "\n>>\n");
100 cat
= new_object(&olist
);
101 if (doc
->n_outline_elements
> 0)
102 outlines
= new_object(&olist
);
105 pages
= new_object(&olist
);
106 resources
= new_object(&olist
);
109 * The catalogue just contains references to the outlines and
110 * pages objects, and the pagelabels dictionary.
112 objtext(cat
, "<<\n/Type /Catalog");
114 objtext(cat
, "\n/Outlines ");
115 objref(cat
, outlines
);
117 objtext(cat
, "\n/Pages ");
119 /* Halibut just numbers pages 1, 2, 3, ... */
120 objtext(cat
, "\n/PageLabels<</Nums[0<</S/D>>]>>");
122 objtext(cat
, "\n/PageMode /UseOutlines");
123 objtext(cat
, "\n>>\n");
126 * Set up the resources dictionary, which mostly means
127 * providing all the font objects and names to call them by.
130 objtext(resources
, "<<\n/ProcSet [/PDF/Text]\n/Font <<\n");
131 for (fe
= doc
->fonts
->head
; fe
; fe
= fe
->next
) {
135 object
*font
, *fontdesc
;
137 font_info
const *fi
= fe
->font
->info
;
139 sprintf(fname
, "f%d", font_index
++);
140 fe
->name
= dupstr(fname
);
142 font
= new_object(&olist
);
144 objtext(resources
, "/");
145 objtext(resources
, fe
->name
);
146 objtext(resources
, " ");
147 objref(resources
, font
);
148 objtext(resources
, "\n");
152 * Construct those parts of the font descriptor that don't dependd
153 * on the file format.
155 if (!is_std_font(fe
->font
->info
->name
)) {
156 fontdesc
= new_object(&olist
);
158 #define FF_FIXEDPITCH 0x00000001
159 #define FF_SERIF 0x00000002
160 #define FF_SYMBOLIC 0x00000004
161 #define FF_SCRIPT 0x00000008
162 #define FF_NONSYMBOLIC 0x00000020
163 #define FF_ITALIC 0x00000040
164 #define FF_ALLCAP 0x00010000
165 #define FF_SMALLCAP 0x00020000
166 #define FF_FORCEBOLD 0x00040000
168 objtext(fontdesc
, "<<\n/Type /FontDescriptor\n/Name /");
169 objtext(fontdesc
, fi
->name
);
171 if (fi
->italicangle
) flags
|= FF_ITALIC
;
172 flags
|= FF_NONSYMBOLIC
;
173 sprintf(buf
, "\n/Flags %d\n", flags
);
174 objtext(fontdesc
, buf
);
175 sprintf(buf
, "/FontBBox [%g %g %g %g]\n", fi
->fontbbox
[0],
176 fi
->fontbbox
[1], fi
->fontbbox
[2], fi
->fontbbox
[3]);
177 objtext(fontdesc
, buf
);
178 sprintf(buf
, "/ItalicAngle %g\n", fi
->italicangle
);
179 objtext(fontdesc
, buf
);
180 sprintf(buf
, "/Ascent %g\n", fi
->ascent
);
181 objtext(fontdesc
, buf
);
182 sprintf(buf
, "/Descent %g\n", fi
->descent
);
183 objtext(fontdesc
, buf
);
184 sprintf(buf
, "/CapHeight %g\n", fi
->capheight
);
185 objtext(fontdesc
, buf
);
186 sprintf(buf
, "/XHeight %g\n", fi
->xheight
);
187 objtext(fontdesc
, buf
);
188 sprintf(buf
, "/StemH %g\n", fi
->stemh
);
189 objtext(fontdesc
, buf
);
190 sprintf(buf
, "/StemV %g\n", fi
->stemv
);
191 objtext(fontdesc
, buf
);
194 objtext(font
, "<<\n/Type /Font\n/BaseFont /");
195 objtext(font
, fe
->font
->info
->name
);
196 if (fe
->font
->info
->filetype
== TRUETYPE
) {
197 object
*cidfont
= new_object(&olist
);
198 object
*cmap
= new_object(&olist
);
199 objtext(font
, "/Subtype/Type0\n/Encoding ");
200 objtext(cmap
, "<</Type/CMap\n/CMapName/");
201 objtext(cmap
, fe
->name
);
202 objtext(cmap
, "\n/CIDSystemInfo<</Registry(Adobe)"
203 "/Ordering(Identity)/Supplement 0>>\n");
204 objstream(cmap
, "%!PS-Adobe-3.0 Resource-CMap\n"
205 "%%DocumentNeededResources: procset CIDInit\n"
206 "%%IncludeResource: procset CIDInit\n"
207 "%%BeginResource: CMap ");
208 objstream(cmap
, fe
->name
);
209 objstream(cmap
, "\n%%Title (");
210 objstream(cmap
, fe
->name
);
211 objstream(cmap
, " Adobe Identity 0)\n%%Version: 1\n%%EndComments\n");
212 objstream(cmap
, "/CIDInit/ProcSet findresource begin\n");
213 objstream(cmap
, "12 dict begin begincmap\n");
214 objstream(cmap
, "/CIDSystemInfo 3 dict dup begin\n"
215 "/Registry(Adobe)def/Ordering(Identity)def"
216 "/Supplement 0 def end def\n");
217 objstream(cmap
, "/CMapName/");
218 objstream(cmap
, fe
->name
);
219 objstream(cmap
, " def/CMapType 0 def/WMode 0 def\n");
220 objstream(cmap
, "1 begincodespacerange<00><FF>"
221 "endcodespacerange\n");
222 for (i
= 0; i
< 256; i
++) {
224 if (fe
->vector
[i
] == NOGLYPH
)
226 objstream(cmap
, "1 begincidchar");
227 sprintf(buf
, "<%02X>", i
);
228 objstream(cmap
, buf
);
229 sprintf(buf
, "%hu", sfnt_glyphtoindex(fe
->font
->info
->fontfile
,
231 objstream(cmap
, buf
);
232 objstream(cmap
, " endcidchar\n");
234 objstream(cmap
, "endcmap CMapName currentdict /CMap "
235 "defineresource pop end end\n%%EndResource\n%%EOF\n");
237 objtext(font
, "\n/DescendantFonts[");
238 objref(font
, cidfont
);
239 objtext(font
, "]\n");
240 objtext(cidfont
, "<<\n/Type/Font\n/Subtype/CIDFontType2\n"
242 objtext(cidfont
, fe
->font
->info
->name
);
243 objtext(cidfont
, "\n/CIDSystemInfo<</Registry(Adobe)"
244 "/Ordering(Identity)/Supplement 0>>\n");
245 objtext(cidfont
, "/FontDescriptor ");
246 objref(cidfont
, fontdesc
);
247 objtext(cidfont
, "\n/W[0[");
248 for (i
= 0; i
< sfnt_nglyphs(fe
->font
->info
->fontfile
); i
++) {
251 width
= find_width(fe
->font
,
252 sfnt_indextoglyph(fe
->font
->info
->fontfile
, i
));
253 sprintf(buf
, "%g ", 1000.0 * width
/ FUNITS_PER_PT
);
254 objtext(cidfont
, buf
);
256 objtext(cidfont
, "]]>>\n");
258 objtext(font
, "/Subtype /Type1\n");
259 objtext(font
, "\n/Encoding <<\n/Type /Encoding\n/Differences [");
261 for (i
= 0; i
< 256; i
++) {
263 if (fe
->vector
[i
] == NOGLYPH
)
266 sprintf(buf
, "\n%d", i
);
269 objtext(font
, i
% 8 ?
"/" : "\n/");
270 objtext(font
, glyph_extern(fe
->vector
[i
]));
274 objtext(font
, "\n]\n>>\n");
275 if (!is_std_font(fe
->font
->info
->name
)){
276 object
*widths
= new_object(&olist
);
277 int firstchar
= -1, lastchar
= -1;
278 for (i
= 0; i
< 256; i
++)
279 if (fe
->vector
[i
] != NOGLYPH
) {
280 if (firstchar
< 0) firstchar
= i
;
283 sprintf(buf
, "/FirstChar %d\n/LastChar %d\n/Widths ",
284 firstchar
, lastchar
);
286 objref(font
, widths
);
288 objtext(widths
, "[\n");
289 for (i
= firstchar
; i
<= lastchar
; i
++) {
291 if (fe
->vector
[i
] == NOGLYPH
)
294 width
= find_width(fe
->font
, fe
->vector
[i
]);
295 sprintf(buf
, "%g\n", 1000.0 * width
/ FUNITS_PER_PT
);
296 objtext(widths
, buf
);
298 objtext(widths
, "]\n");
299 objtext(font
, "/FontDescriptor ");
300 objref(font
, fontdesc
);
305 if (!is_std_font(fe
->font
->info
->name
)) {
306 if (fi
->fontfile
&& fi
->filetype
== TYPE1
) {
307 object
*fontfile
= new_object(&olist
);
311 pf_part1((font_info
*)fi
, &ffbuf
, &len
);
312 objstream_len(fontfile
, ffbuf
, len
);
314 sprintf(buf
, "<<\n/Length1 %lu\n", (unsigned long)len
);
315 objtext(fontfile
, buf
);
316 pf_part2((font_info
*)fi
, &ffbuf
, &len
);
317 objstream_len(fontfile
, ffbuf
, len
);
319 sprintf(buf
, "/Length2 %lu\n", (unsigned long)len
);
320 objtext(fontfile
, buf
);
321 objtext(fontfile
, "/Length3 0\n");
322 objtext(fontdesc
, "/FontFile ");
323 objref(fontdesc
, fontfile
);
324 } else if (fi
->fontfile
&& fi
->filetype
== TRUETYPE
) {
325 object
*fontfile
= new_object(&olist
);
329 sfnt_data((font_info
*)fi
, &ffbuf
, &len
);
330 objstream_len(fontfile
, ffbuf
, len
);
331 sprintf(buf
, "<<\n/Length1 %lu\n", (unsigned long)len
);
332 objtext(fontfile
, buf
);
333 objtext(fontdesc
, "/FontFile2 ");
334 objref(fontdesc
, fontfile
);
336 objtext(fontdesc
, "\n>>\n");
339 objtext(font
, "\n>>\n");
341 objtext(resources
, ">>\n>>\n");
345 mediabox
= new_object(&olist
);
346 sprintf(buf
, "[0 0 %g %g]\n",
347 doc
->paper_width
/ FUNITS_PER_PT
,
348 doc
->paper_height
/ FUNITS_PER_PT
);
349 objtext(mediabox
, buf
);
353 * Define the page objects for each page, and get each one
354 * ready to have a `Parent' specification added to it.
356 for (page
= doc
->pages
; page
; page
= page
->next
) {
359 opage
= new_object(&olist
);
361 objtext(opage
, "<<\n/Type /Page\n");
365 * Recursively build the page tree.
367 make_pages_node(pages
, NULL
, doc
->pages
, NULL
, resources
, mediabox
);
370 * Create and render the individual pages.
373 for (page
= doc
->pages
; page
; page
= page
->next
) {
374 object
*opage
, *cstr
;
376 text_fragment
*frag
, *frag_end
;
380 opage
= (object
*)page
->spare
;
382 * At this point the page dictionary is already
383 * half-written, with /Type and /Parent already present. We
384 * continue from there.
388 * The PDF spec says /Resources is required, but also says
389 * that it's inheritable and may be omitted if it's present
390 * in a Pages node. In our case it is: it's present in the
391 * topmost /Pages node because we carefully put it there.
392 * So we don't need a /Resources entry here. The same applies
397 * Now we're ready to define a content stream containing
398 * the actual text on the page.
400 cstr
= new_object(&olist
);
401 objtext(opage
, "/Contents ");
403 objtext(opage
, "\n");
406 * Render any rectangles on the page.
408 for (r
= page
->first_rect
; r
; r
= r
->next
) {
410 sprintf(buf
, "%g %g %g %g re f\n",
411 r
->x
/ FUNITS_PER_PT
, r
->y
/ FUNITS_PER_PT
,
412 r
->w
/ FUNITS_PER_PT
, r
->h
/ FUNITS_PER_PT
);
413 objstream(cstr
, buf
);
416 objstream(cstr
, "BT\n");
419 * PDF tracks two separate current positions: the position
420 * given in the `line matrix' and the position given in the
421 * `text matrix'. We must therefore track both as well.
422 * They start off at -1 (unset).
427 frag
= page
->first_text
;
430 * For compactness, I'm going to group text fragments
431 * into subsequences that use the same font+size. So
432 * first find the end of this subsequence.
434 for (frag_end
= frag
;
436 frag_end
->fe
== frag
->fe
&&
437 frag_end
->fontsize
== frag
->fontsize
);
438 frag_end
= frag_end
->next
);
441 * Now select the text fragment, and prepare to display
444 objstream(cstr
, "/");
445 objstream(cstr
, frag
->fe
->name
);
446 sprintf(buf
, " %d Tf ", frag
->fontsize
);
447 objstream(cstr
, buf
);
449 while (frag
&& frag
!= frag_end
) {
451 * Place the text position for the first piece of
455 sprintf(buf
, "1 0 0 1 %g %g Tm ",
456 frag
->x
/FUNITS_PER_PT
, frag
->y
/FUNITS_PER_PT
);
458 sprintf(buf
, "%g %g Td ",
459 (frag
->x
- lx
)/FUNITS_PER_PT
,
460 (frag
->y
- ly
)/FUNITS_PER_PT
);
462 objstream(cstr
, buf
);
467 * See if we're going to use Tj (show a single
468 * string) or TJ (show an array of strings with
469 * x-spacings between them). We determine this by
470 * seeing if there's more than one text fragment in
471 * sequence with the same y-coordinate.
473 if (frag
->next
&& frag
->next
!= frag_end
&&
474 frag
->next
->y
== y
) {
478 objstream(cstr
, "[");
479 while (frag
&& frag
!= frag_end
&& frag
->y
== y
) {
482 (x
- frag
->x
) * 1000.0 /
483 (FUNITS_PER_PT
* frag
->fontsize
));
484 objstream(cstr
, buf
);
486 pdf_string(objstream
, cstr
, frag
->text
);
487 x
= frag
->x
+ frag
->width
;
490 objstream(cstr
, "]TJ\n");
496 pdf_string(objstream
, cstr
, frag
->text
);
497 objstream(cstr
, "Tj\n");
502 objstream(cstr
, "ET");
505 * Also, we want an annotation dictionary containing the
506 * cross-references from this page.
508 if (page
->first_xref
) {
510 objtext(opage
, "/Annots [\n");
512 for (xr
= page
->first_xref
; xr
; xr
= xr
->next
) {
515 objtext(opage
, "<</Subtype/Link\n/Rect[");
516 sprintf(buf
, "%g %g %g %g",
517 xr
->lx
/ FUNITS_PER_PT
, xr
->by
/ FUNITS_PER_PT
,
518 xr
->rx
/ FUNITS_PER_PT
, xr
->ty
/ FUNITS_PER_PT
);
520 objtext(opage
, "]/Border[0 0 0]\n");
522 if (xr
->dest
.type
== PAGE
) {
523 objtext(opage
, "/Dest");
524 objdest(opage
, xr
->dest
.page
);
526 objtext(opage
, "/A<</S/URI/URI");
527 pdf_string(objtext
, opage
, xr
->dest
.url
);
528 objtext(opage
, ">>");
531 objtext(opage
, ">>\n");
534 objtext(opage
, "]\n");
537 objtext(opage
, ">>\n");
541 * Set up the outlines dictionary.
547 objtext(outlines
, "<<\n/Type /Outlines\n");
548 topcount
= make_outline(outlines
, doc
->outline_elements
,
549 doc
->n_outline_elements
, TRUE
);
550 sprintf(buf
, "/Count %d\n>>\n", topcount
);
551 objtext(outlines
, buf
);
555 * Assemble the final linear form of every object.
557 for (o
= olist
.head
; o
; o
= o
->next
) {
558 rdstringc rs
= {0, 0, NULL
};
560 deflate_compress_ctx
*zcontext
;
564 sprintf(text
, "%d 0 obj\n", o
->number
);
567 if (o
->stream
.text
) {
569 rdaddsc(&o
->main
, "<<\n");
570 #ifdef PDF_NOCOMPRESS
571 zlen
= o
->stream
.pos
;
572 zbuf
= snewn(zlen
, char);
573 memcpy(zbuf
, o
->stream
.text
, zlen
);
574 sprintf(text
, "/Length %d\n>>\n", zlen
);
576 zcontext
= deflate_compress_new(DEFLATE_TYPE_ZLIB
);
577 deflate_compress_data(zcontext
, o
->stream
.text
, o
->stream
.pos
,
578 DEFLATE_END_OF_DATA
, &zbuf
, &zlen
);
579 deflate_compress_free(zcontext
);
580 sprintf(text
, "/Filter/FlateDecode\n/Length %d\n>>\n", zlen
);
582 rdaddsc(&o
->main
, text
);
585 assert(o
->main
.text
);
586 rdaddsc(&rs
, o
->main
.text
);
589 if (rs
.text
[rs
.pos
-1] != '\n')
592 if (o
->stream
.text
) {
593 rdaddsc(&rs
, "stream\n");
594 rdaddsn(&rs
, zbuf
, zlen
);
595 rdaddsc(&rs
, "\nendstream\n");
596 sfree(o
->stream
.text
);
600 rdaddsc(&rs
, "endobj\n");
607 * Write out the PDF file.
610 fp
= fopen(filename
, "wb");
612 error(err_cantopenw
, filename
);
617 * Header. I'm going to put the version IDs in the header as
618 * well, simply in PDF comments. The PDF Reference also suggests
619 * that binary PDF files contain four top-bit-set characters in
622 fileoff
= fprintf(fp
, "%%PDF-1.3\n%% L\xc3\xba\xc3\xb0""a\n");
623 for (p
= sourceform
; p
; p
= p
->next
)
624 if (p
->type
== para_VersionID
)
625 fileoff
+= pdf_versionid(fp
, p
->words
);
630 for (o
= olist
.head
; o
; o
= o
->next
) {
631 o
->fileoff
= fileoff
;
632 fwrite(o
->final
, 1, o
->size
, fp
);
637 * Cross-reference table
639 fprintf(fp
, "xref\n");
640 assert(olist
.head
->number
== 1);
641 fprintf(fp
, "0 %d\n", olist
.tail
->number
+ 1);
642 fprintf(fp
, "0000000000 65535 f \n");
643 for (o
= olist
.head
; o
; o
= o
->next
) {
645 sprintf(entry
, "%010d 00000 n \n", o
->fileoff
);
646 assert(strlen(entry
) == 20);
653 fprintf(fp
, "trailer\n<<\n/Size %d\n/Root %d 0 R\n/Info %d 0 R\n>>\n",
654 olist
.tail
->number
+ 1, cat
->number
, info
->number
);
655 fprintf(fp
, "startxref\n%d\n%%%%EOF\n", fileoff
);
662 object
*new_object(objlist
*list
)
664 object
*obj
= snew(object
);
668 obj
->main
.text
= NULL
;
669 obj
->main
.pos
= obj
->main
.size
= 0;
670 obj
->stream
.text
= NULL
;
671 obj
->stream
.pos
= obj
->stream
.size
= 0;
673 obj
->number
= list
->number
++;
677 list
->tail
->next
= obj
;
688 void objtext(object
*o
, char const *text
)
690 rdaddsc(&o
->main
, text
);
693 void objstream_len(object
*o
, char const *text
, size_t len
)
695 rdaddsn(&o
->stream
, text
, len
);
698 void objstream(object
*o
, char const *text
)
700 rdaddsc(&o
->stream
, text
);
703 static void objref(object
*o
, object
*dest
)
706 sprintf(buf
, "%d 0 R", dest
->number
);
707 rdaddsc(&o
->main
, buf
);
710 static void objdest(object
*o
, page_data
*p
) {
712 objref(o
, (object
*)p
->spare
);
713 objtext(o
, "/XYZ null null null]");
716 static char const * const stdfonts
[] = {
717 "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic",
718 "Helvetica", "Helvetica-Bold", "Helvetica-Oblique","Helvetica-BoldOblique",
719 "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique",
720 "Symbol", "ZapfDingbats"
723 static int is_std_font(char const *name
) {
725 for (i
= 0; i
< lenof(stdfonts
); i
++)
726 if (strcmp(name
, stdfonts
[i
]) == 0)
731 static void make_pages_node(object
*node
, object
*parent
, page_data
*first
,
732 page_data
*last
, object
*resources
,
739 objtext(node
, "<<\n/Type /Pages\n");
741 objtext(node
, "/Parent ");
742 objref(node
, parent
);
747 * Count the pages in this stretch, to see if there are few
748 * enough to reference directly.
751 for (page
= first
; page
; page
= page
->next
) {
757 sprintf(buf
, "/Count %d\n/Kids [\n", count
);
760 if (count
> TREE_BRANCH
) {
762 page_data
*thisfirst
, *thislast
;
766 for (i
= 0; i
< TREE_BRANCH
; i
++) {
767 int number
= (i
+1) * count
/ TREE_BRANCH
- i
* count
/ TREE_BRANCH
;
774 if (thisfirst
== thislast
) {
775 objref(node
, (object
*)thisfirst
->spare
);
776 objtext((object
*)thisfirst
->spare
, "/Parent ");
777 objref((object
*)thisfirst
->spare
, node
);
778 objtext((object
*)thisfirst
->spare
, "\n");
780 object
*newnode
= new_object(node
->list
);
781 make_pages_node(newnode
, node
, thisfirst
, thislast
,
783 objref(node
, newnode
);
788 assert(thislast
== last
|| page
== NULL
);
791 for (page
= first
; page
; page
= page
->next
) {
792 objref(node
, (object
*)page
->spare
);
794 objtext((object
*)page
->spare
, "/Parent ");
795 objref((object
*)page
->spare
, node
);
796 objtext((object
*)page
->spare
, "\n");
802 objtext(node
, "]\n");
805 objtext(node
, "/Resources ");
806 objref(node
, resources
);
810 objtext(node
, "/MediaBox ");
811 objref(node
, mediabox
);
815 objtext(node
, ">>\n");
819 * In text on the page, PDF uses the PostScript font model, which
820 * means that glyphs are identified by PS strings and hence font
821 * encoding can be managed independently of the supplied encoding
822 * of the font. However, in the document outline, the PDF spec
823 * encodes in either PDFDocEncoding (a custom superset of
824 * ISO-8859-1) or UTF-16BE.
826 char *pdf_outline_convert(wchar_t *s
, int *len
) {
829 ret
= utoa_careful_dup(s
, CS_PDF
);
832 * Very silly special case: if the returned string begins with
833 * FE FF, then the PDF reader will mistake it for a UTF-16BE
834 * string. So in this case we give up on PDFDocEncoding and
835 * encode it in UTF-16 straight away.
837 if (ret
&& ret
[0] == '\xFE' && ret
[1] == '\xFF') {
843 ret
= utoa_dup_len(s
, CS_UTF16BE
, len
);
851 static int make_outline(object
*parent
, outline_element
*items
, int n
,
854 int level
, totalcount
= 0;
855 outline_element
*itemp
;
856 object
*curr
, *prev
= NULL
, *first
= NULL
, *last
= NULL
;
860 level
= items
->level
;
867 * Here we expect to be sitting on an item at the given
868 * level. So we start by constructing an outline entry for
871 assert(items
->level
== level
);
873 title
= pdf_outline_convert(items
->pdata
->outline_title
, &titlelen
);
876 curr
= new_object(parent
->list
);
877 if (!first
) first
= curr
;
879 objtext(curr
, "<<\n/Title ");
880 pdf_string_len(objtext
, curr
, title
, titlelen
);
882 objtext(curr
, "\n/Parent ");
883 objref(curr
, parent
);
884 objtext(curr
, "\n/Dest");
885 objdest(curr
, items
->pdata
->first
->page
);
888 objtext(curr
, "/Prev ");
892 objtext(prev
, "/Next ");
894 objtext(prev
, "\n>>\n");
899 for (itemp
= items
; itemp
< items
+n
&& itemp
->level
> level
;
904 int count
= make_outline(curr
, items
, itemp
- items
, FALSE
);
909 sprintf(buf
, "/Count %d\n", count
);
916 objtext(prev
, ">>\n");
918 assert(first
&& last
);
919 objtext(parent
, "/First ");
920 objref(parent
, first
);
921 objtext(parent
, "\n/Last ");
922 objref(parent
, last
);
923 objtext(parent
, "\n");
928 static int pdf_versionid(FILE *fp
, word
*words
)
932 ret
= fprintf(fp
, "%% ");
934 for (; words
; words
= words
->next
) {
938 switch (words
->type
) {
948 type
= removeattr(words
->type
);
952 text
= utoa_dup(words
->text
, CS_ASCII
);
954 case word_WhiteSpace
:
967 ret
+= fprintf(fp
, "\n");
972 static void pdf_string_len(void (*add
)(object
*, char const *),
973 object
*o
, char const *str
, int len
)
978 for (p
= str
; len
> 0; p
++, len
--) {
980 if (*p
< ' ' || *p
> '~') {
981 sprintf(c
, "\\%03o", 0xFF & (int)*p
);
984 if (*p
== '\\' || *p
== '(' || *p
== ')')
994 static void pdf_string(void (*add
)(object
*, char const *),
995 object
*o
, char const *str
)
997 pdf_string_len(add
, o
, str
, strlen(str
));