Remove a bunch of unused variables spotted by Ubuntu 12.04's gcc.
[sgt/halibut] / bk_pdf.c
index f6babe5..6daebc6 100644 (file)
--- a/bk_pdf.c
+++ b/bk_pdf.c
@@ -5,40 +5,15 @@
 #include <assert.h>
 #include "halibut.h"
 #include "paper.h"
+#include "deflate.h"
 
-#define TREE_BRANCH 2                 /* max branching factor in page tree */
+#define TREE_BRANCH 8                 /* max branching factor in page tree */
 
 paragraph *pdf_config_filename(char *filename)
 {
-    paragraph *p;
-    wchar_t *ufilename, *up;
-    int len;
-
-    p = mknew(paragraph);
-    memset(p, 0, sizeof(*p));
-    p->type = para_Config;
-    p->next = NULL;
-    p->fpos.filename = "<command line>";
-    p->fpos.line = p->fpos.col = -1;
-
-    ufilename = ufroma_dup(filename);
-    len = ustrlen(ufilename) + 2 + lenof(L"pdf-filename");
-    p->keyword = mknewa(wchar_t, len);
-    up = p->keyword;
-    ustrcpy(up, L"pdf-filename");
-    up = uadv(up);
-    ustrcpy(up, ufilename);
-    up = uadv(up);
-    *up = L'\0';
-    assert(up - p->keyword < len);
-    sfree(ufilename);
-
-    return p;
+    return cmdline_cfg_simple("pdf-filename", filename, NULL);
 }
 
-typedef struct object_Tag object;
-typedef struct objlist_Tag objlist;
-
 struct object_Tag {
     objlist *list;
     object *next;
@@ -53,15 +28,18 @@ struct objlist_Tag {
     object *head, *tail;
 };
 
-static object *new_object(objlist *list);
-static void objtext(object *o, char const *text);
-static void objstream(object *o, char const *text);
 static void pdf_string(void (*add)(object *, char const *),
                       object *, char const *);
+static void pdf_string_len(void (*add)(object *, char const *),
+                          object *, char const *, int);
 static void objref(object *o, object *dest);
+static void objdest(object *o, page_data *p);
+
+static int is_std_font(char const *name);
 
 static void make_pages_node(object *node, object *parent, page_data *first,
-                           page_data *last, object *resources);
+                           page_data *last, object *resources,
+                           object *mediabox);
 static int make_outline(object *parent, outline_element *start, int n,
                        int open);
 static int pdf_versionid(FILE *fp, word *words);
@@ -72,12 +50,11 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
     int font_index;
     font_encoding *fe;
     page_data *page;
-    int pageno;
     FILE *fp;
     char *filename;
     paragraph *p;
     objlist olist;
-    object *o, *cat, *outlines, *pages, *resources;
+    object *o, *info, *cat, *outlines, *pages, *resources, *mediabox;
     int fileoff;
 
     IGNORE(keywords);
@@ -85,10 +62,10 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
 
     filename = dupstr("output.pdf");
     for (p = sourceform; p; p = p->next) {
-       if (p->type == para_Config && p->parent) {
+       if (p->type == para_Config) {
            if (!ustricmp(p->keyword, L"pdf-filename")) {
                sfree(filename);
-               filename = utoa_dup(uadv(p->keyword));
+               filename = dupstr(adv(p->origkeyword));
            }
        }
     }
@@ -96,31 +73,67 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
     olist.head = olist.tail = NULL;
     olist.number = 1;
 
+    {
+       char buf[256];
+
+       info = new_object(&olist);
+       objtext(info, "<<\n");
+       if (doc->n_outline_elements > 0) {
+           char *title;
+           int titlelen;
+
+           title =
+              pdf_outline_convert(doc->outline_elements->pdata->outline_title,
+                                       &titlelen);
+           objtext(info, "/Title ");
+           pdf_string_len(objtext, info, title, titlelen);
+           sfree(title);
+           objtext(info, "\n");
+       }
+       objtext(info, "/Producer ");
+       sprintf(buf, "Halibut, %s", version);
+       pdf_string(objtext, info, buf);
+       objtext(info, "\n>>\n");
+    }
+
     cat = new_object(&olist);
-    outlines = new_object(&olist);
+    if (doc->n_outline_elements > 0)
+       outlines = new_object(&olist);
+    else
+       outlines = NULL;
     pages = new_object(&olist);
     resources = new_object(&olist);
 
     /*
      * The catalogue just contains references to the outlines and
-     * pages objects.
+     * pages objects, and the pagelabels dictionary.
      */
-    objtext(cat, "<<\n/Type /Catalog\n/Outlines ");
-    objref(cat, outlines);
+    objtext(cat, "<<\n/Type /Catalog");
+    if (outlines) {
+       objtext(cat, "\n/Outlines ");
+       objref(cat, outlines);
+    }
     objtext(cat, "\n/Pages ");
     objref(cat, pages);
-    objtext(cat, "\n/PageMode /UseOutlines\n>>\n");
+    /* Halibut just numbers pages 1, 2, 3, ... */
+    objtext(cat, "\n/PageLabels<</Nums[0<</S/D>>]>>");
+    if (outlines)
+       objtext(cat, "\n/PageMode /UseOutlines");
+    objtext(cat, "\n>>\n");
 
     /*
      * Set up the resources dictionary, which mostly means
      * providing all the font objects and names to call them by.
      */
     font_index = 0;
-    objtext(resources, "<<\n/Font <<\n");
+    objtext(resources, "<<\n/ProcSet [/PDF/Text]\n/Font <<\n");
     for (fe = doc->fonts->head; fe; fe = fe->next) {
        char fname[40];
-       int i;
-       object *font;
+       char buf[80];
+       int i, prev;
+       object *font, *fontdesc;
+       int flags;
+       font_info const *fi = fe->font->info;
 
        sprintf(fname, "f%d", font_index++);
        fe->name = dupstr(fname);
@@ -133,46 +146,266 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
        objref(resources, font);
        objtext(resources, "\n");
 
-       objtext(font, "<<\n/Type /Font\n/Subtype /Type1\n/Name /");
-       objtext(font, fe->name);
-       objtext(font, "\n/BaseFont /");
-       objtext(font, fe->font->name);
-       objtext(font, "\n/Encoding <<\n/Type /Encoding\n/Differences [");
-
-       for (i = 0; i < 256; i++) {
-           char buf[20];
-           if (!fe->vector[i])
-               continue;
-           sprintf(buf, "\n%d /", i);
-           objtext(font, buf);
-           objtext(font, fe->vector[i] ? fe->vector[i] : ".notdef");
-       }
 
-       objtext(font, "\n]\n>>\n");
+       /*
+        * Construct those parts of the font descriptor that don't dependd
+        * on the file format.
+        */
+       if (!is_std_font(fe->font->info->name)) {
+           fontdesc = new_object(&olist);
+
+#define FF_FIXEDPITCH  0x00000001
+#define FF_SERIF       0x00000002
+#define FF_SYMBOLIC    0x00000004
+#define FF_SCRIPT      0x00000008
+#define FF_NONSYMBOLIC 0x00000020
+#define FF_ITALIC      0x00000040
+#define FF_ALLCAP      0x00010000
+#define FF_SMALLCAP    0x00020000
+#define FF_FORCEBOLD   0x00040000
+
+           objtext(fontdesc, "<<\n/Type /FontDescriptor\n/Name /");
+           objtext(fontdesc, fi->name);
+           flags = 0;
+           if (fi->italicangle) flags |= FF_ITALIC;
+           flags |= FF_NONSYMBOLIC;
+           sprintf(buf, "\n/Flags %d\n", flags);
+           objtext(fontdesc, buf);
+           sprintf(buf, "/FontBBox [%g %g %g %g]\n", fi->fontbbox[0],
+                   fi->fontbbox[1], fi->fontbbox[2], fi->fontbbox[3]);
+           objtext(fontdesc, buf);
+           sprintf(buf, "/ItalicAngle %g\n", fi->italicangle);
+           objtext(fontdesc, buf);
+           sprintf(buf, "/Ascent %g\n", fi->ascent);
+           objtext(fontdesc, buf);
+           sprintf(buf, "/Descent %g\n", fi->descent);
+           objtext(fontdesc, buf);
+           sprintf(buf, "/CapHeight %g\n", fi->capheight);
+           objtext(fontdesc, buf);
+           sprintf(buf, "/XHeight %g\n", fi->xheight);
+           objtext(fontdesc, buf);
+           sprintf(buf, "/StemH %g\n", fi->stemh);
+           objtext(fontdesc, buf);
+           sprintf(buf, "/StemV %g\n", fi->stemv);
+           objtext(fontdesc, buf);
+       }
 
-       {
-           object *widths = new_object(&olist);
-           objtext(font, "/FirstChar 0\n/LastChar 255\n/Widths ");
-           objref(font, widths);
-           objtext(font, "\n");
-           objtext(widths, "[\n");
+       objtext(font, "<<\n/Type /Font\n/BaseFont /");
+       objtext(font, fe->font->info->name);
+       if (fe->font->info->filetype == TRUETYPE) {
+           object *cidfont = new_object(&olist);
+           object *cmap = new_object(&olist);
+           unsigned short ranges[256];
+           unsigned startidx, nranges, nchars;
+           int start;
+
+           objtext(font, "/Subtype/Type0\n/Encoding ");
+           objtext(cmap, "<</Type/CMap\n/CMapName/");
+           objtext(cmap, fe->name);
+           objtext(cmap, "\n/CIDSystemInfo<</Registry(Adobe)"
+                   "/Ordering(Identity)/Supplement 0>>\n");
+           objstream(cmap, "%!PS-Adobe-3.0 Resource-CMap\n"
+                     "%%DocumentNeededResources: procset CIDInit\n"
+                     "%%IncludeResource: procset CIDInit\n"
+                     "%%BeginResource: CMap ");
+           objstream(cmap, fe->name);
+           objstream(cmap, "\n%%Title (");
+           objstream(cmap, fe->name);
+           objstream(cmap, " Adobe Identity 0)\n%%Version: 1\n%%EndComments\n");
+           objstream(cmap, "/CIDInit/ProcSet findresource begin\n");
+           objstream(cmap, "12 dict begin begincmap\n");
+           objstream(cmap, "/CIDSystemInfo 3 dict dup begin\n"
+                     "/Registry(Adobe)def/Ordering(Identity)def"
+                     "/Supplement 0 def end def\n");
+           objstream(cmap, "/CMapName/");
+           objstream(cmap, fe->name);
+           objstream(cmap, " def/CMapType 0 def/WMode 0 def\n");
+           objstream(cmap, "1 begincodespacerange<00><FF>"
+                     "endcodespacerange\n");
+           start = -1; nranges = nchars = 0;
            for (i = 0; i < 256; i++) {
-               char buf[80];
+               unsigned idx;
+
+               ranges[i] = 0;
+               if (fe->vector[i] == NOGLYPH)
+                   continue;
+               idx = sfnt_glyphtoindex(fe->font->info->fontfile,
+                                       fe->vector[i]);
+               if (start >= 0 && idx - startidx == (unsigned)(i - start)) {
+                   if (ranges[start] == 1) {
+                       nranges++; nchars--;
+                   }
+                   ranges[start] = i - start + 1;
+               } else {
+                   ranges[i] = 1;
+                   start = i;
+                   startidx = idx;
+                   nchars++;
+               }
+           }
+           i = 0;
+           while (nranges) {
+               unsigned blk = nranges > 100 ? 100 : nranges;
+               nranges -= blk;
+               sprintf(buf, "%u ", blk);
+               objstream(cmap, buf);
+               objstream(cmap, "begincidrange\n");
+               while (blk) {
+                   if (ranges[i] > 1) {
+                       sprintf(buf, "<%02X>", i);
+                       objstream(cmap, buf);
+                       sprintf(buf, "<%02X>", i + ranges[i] - 1);
+                       objstream(cmap, buf);
+                       sprintf(buf, "%hu\n",
+                               sfnt_glyphtoindex(fe->font->info->fontfile,
+                                                 fe->vector[i]));
+                       objstream(cmap, buf);
+                       blk--;
+                   }
+                   i++;
+               }
+               objstream(cmap, "endcidrange\n");
+           }
+           i = 0;
+           while (nchars) {
+               unsigned blk = nchars > 100 ? 100 : nchars;
+               nchars -= blk;
+               sprintf(buf, "%u ", blk);
+               objstream(cmap, buf);
+               objstream(cmap, "begincidchar\n");
+               while (blk) {
+                   if (ranges[i] == 1) {
+                       sprintf(buf, "<%02X>", i);
+                       objstream(cmap, buf);
+                       sprintf(buf, "%hu\n",
+                               sfnt_glyphtoindex(fe->font->info->fontfile,
+                                                 fe->vector[i]));
+                       objstream(cmap, buf);
+                       blk--;
+                   }
+                   i++;
+               }
+               objstream(cmap, "endcidchar\n");
+           }
+           objstream(cmap, "endcmap CMapName currentdict /CMap "
+                     "defineresource pop end end\n%%EndResource\n%%EOF\n");
+
+           objref(font, cmap);
+           objtext(font, "\n/DescendantFonts[");
+           objref(font, cidfont);
+           objtext(font, "]\n");
+           objtext(cidfont, "<<\n/Type/Font\n/Subtype/CIDFontType2\n"
+                   "/BaseFont/");
+           objtext(cidfont, fe->font->info->name);
+           objtext(cidfont, "\n/CIDSystemInfo<</Registry(Adobe)"
+                   "/Ordering(Identity)/Supplement 0>>\n");
+           objtext(cidfont, "/FontDescriptor ");
+           objref(cidfont, fontdesc);
+           objtext(cidfont, "\n/W[0[");
+           for (i = 0; i < (int)sfnt_nglyphs(fe->font->info->fontfile); i++) {
+               char buf[20];
                double width;
-               if (fe->indices[i] < 0)
-                   width = 0.0;
-               else
-                   width = fe->font->widths[fe->indices[i]];
-               sprintf(buf, "%g\n", 1000.0 * width / 4096.0);
-               objtext(widths, buf);
+               width = find_width(fe->font,
+                              sfnt_indextoglyph(fe->font->info->fontfile, i));
+               sprintf(buf, "%g ", 1000.0 * width / FUNITS_PER_PT);
+               objtext(cidfont, buf);
+           }
+           objtext(cidfont, "]]>>\n");
+       } else {
+           objtext(font, "/Subtype /Type1\n");
+           objtext(font, "\n/Encoding <<\n/Type /Encoding\n/Differences [");
+
+           for (i = 0; i < 256; i++) {
+               char buf[20];
+               if (fe->vector[i] == NOGLYPH)
+                   continue;
+               if (i != prev + 1) {
+                   sprintf(buf, "\n%d", i);
+                   objtext(font, buf);
+               }
+               objtext(font, i % 8 ? "/" : "\n/");
+               objtext(font, glyph_extern(fe->vector[i]));
+               prev = i;
+           }
+
+           objtext(font, "\n]\n>>\n");
+           if (!is_std_font(fe->font->info->name)){
+               object *widths = new_object(&olist);
+               int firstchar = -1, lastchar = -1;
+               for (i = 0; i < 256; i++)
+                   if (fe->vector[i] != NOGLYPH) {
+                       if (firstchar < 0) firstchar = i;
+                       lastchar = i;
+                   }
+               sprintf(buf, "/FirstChar %d\n/LastChar %d\n/Widths ",
+                       firstchar, lastchar);
+               objtext(font, buf);
+               objref(font, widths);
+               objtext(font, "\n");
+               objtext(widths, "[\n");
+               for (i = firstchar; i <= lastchar; i++) {
+                   double width;
+                   if (fe->vector[i] == NOGLYPH)
+                       width = 0.0;
+                   else
+                       width = find_width(fe->font, fe->vector[i]);
+                   sprintf(buf, "%g\n", 1000.0 * width / FUNITS_PER_PT);
+                   objtext(widths, buf);
+               }
+               objtext(widths, "]\n");
+               objtext(font, "/FontDescriptor ");
+               objref(font, fontdesc);
            }
-           objtext(widths, "]\n");
+
        }
 
-       objtext(font, ">>\n");
+       if (!is_std_font(fe->font->info->name)) {
+           if (fi->fontfile && fi->filetype == TYPE1) {
+               object *fontfile = new_object(&olist);
+               size_t len;
+               char *ffbuf;
+
+               pf_part1((font_info *)fi, &ffbuf, &len);
+               objstream_len(fontfile, ffbuf, len);
+               sfree(ffbuf);
+               sprintf(buf, "<<\n/Length1 %lu\n", (unsigned long)len);
+               objtext(fontfile, buf);
+               pf_part2((font_info *)fi, &ffbuf, &len);
+               objstream_len(fontfile, ffbuf, len);
+               sfree(ffbuf);
+               sprintf(buf, "/Length2 %lu\n", (unsigned long)len);
+               objtext(fontfile, buf);
+               objtext(fontfile, "/Length3 0\n");
+               objtext(fontdesc, "/FontFile ");
+               objref(fontdesc, fontfile);
+           } else if (fi->fontfile && fi->filetype == TRUETYPE) {
+               object *fontfile = new_object(&olist);
+               size_t len;
+               char *ffbuf;
+
+               sfnt_data((font_info *)fi, &ffbuf, &len);
+               objstream_len(fontfile, ffbuf, len);
+               sprintf(buf, "<<\n/Length1 %lu\n", (unsigned long)len);
+               objtext(fontfile, buf);
+               objtext(fontdesc, "/FontFile2 ");
+               objref(fontdesc, fontfile);
+           }
+           objtext(fontdesc, "\n>>\n");
+       }
+
+       objtext(font, "\n>>\n");
     }
     objtext(resources, ">>\n>>\n");
 
+    {
+       char buf[255];
+       mediabox = new_object(&olist);
+       sprintf(buf, "[0 0 %g %g]\n",
+               doc->paper_width / FUNITS_PER_PT,
+               doc->paper_height / FUNITS_PER_PT);
+       objtext(mediabox, buf);
+    }
+
     /*
      * Define the page objects for each page, and get each one
      * ready to have a `Parent' specification added to it.
@@ -188,12 +421,11 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
     /*
      * Recursively build the page tree.
      */
-    make_pages_node(pages, NULL, doc->pages, NULL, resources);
+    make_pages_node(pages, NULL, doc->pages, NULL, resources, mediabox);
 
     /*
      * Create and render the individual pages.
      */
-    pageno = 0;
     for (page = doc->pages; page; page = page->next) {
        object *opage, *cstr;
        rect *r;
@@ -213,11 +445,9 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
         * that it's inheritable and may be omitted if it's present
         * in a Pages node. In our case it is: it's present in the
         * topmost /Pages node because we carefully put it there.
-        * So we don't need a /Resources entry here.
+        * So we don't need a /Resources entry here.  The same applies
+        * to /MediaBox.
         */
-       sprintf(buf, "/MediaBox [0 0 %g %g]\n",
-               doc->paper_width / 4096.0, doc->paper_height / 4096.0);
-       objtext(opage, buf);
 
        /*
         * Now we're ready to define a content stream containing
@@ -233,8 +463,9 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
         */
        for (r = page->first_rect; r; r = r->next) {
            char buf[512];
-           sprintf(buf, "%g %g %g %g re f\n", r->x / 4096.0,
-                   r->y / 4096.0, r->w / 4096.0, r->h / 4096.0);
+           sprintf(buf, "%g %g %g %g re f\n",
+                   r->x / FUNITS_PER_PT, r->y / FUNITS_PER_PT,
+                   r->w / FUNITS_PER_PT, r->h / FUNITS_PER_PT);
            objstream(cstr, buf);
        }
 
@@ -278,10 +509,11 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
                 */
                if (lx < 0) {
                    sprintf(buf, "1 0 0 1 %g %g Tm ",
-                           frag->x/4096.0, frag->y/4096.0);
+                           frag->x/FUNITS_PER_PT, frag->y/FUNITS_PER_PT);
                } else {
                    sprintf(buf, "%g %g Td ",
-                           (frag->x - lx)/4096.0, (frag->y - ly)/4096.0);
+                           (frag->x - lx)/FUNITS_PER_PT,
+                           (frag->y - ly)/FUNITS_PER_PT);
                }
                objstream(cstr, buf);
                lx = x = frag->x;
@@ -304,7 +536,7 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
                        if (frag->x != x) {
                            sprintf(buf, "%g",
                                    (x - frag->x) * 1000.0 /
-                                   (4096.0 * frag->fontsize));
+                                   (FUNITS_PER_PT * frag->fontsize));
                            objstream(cstr, buf);
                        }
                        pdf_string(objstream, cstr, frag->text);
@@ -334,31 +566,25 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
            objtext(opage, "/Annots [\n");
 
            for (xr = page->first_xref; xr; xr = xr->next) {
-               object *annot;
                char buf[256];
 
-               annot = new_object(&olist);
-               objref(opage, annot);
-               objtext(opage, "\n");
-
-               objtext(annot, "<<\n/Type /Annot\n/Subtype /Link\n/Rect [");
+               objtext(opage, "<</Subtype/Link\n/Rect[");
                sprintf(buf, "%g %g %g %g",
-                       xr->lx / 4096.0, xr->by / 4096.0,
-                       xr->rx / 4096.0, xr->ty / 4096.0);
-               objtext(annot, buf);
-               objtext(annot, "]\n/Border [0 0 0]\n");
+                       xr->lx / FUNITS_PER_PT, xr->by / FUNITS_PER_PT,
+                       xr->rx / FUNITS_PER_PT, xr->ty / FUNITS_PER_PT);
+               objtext(opage, buf);
+               objtext(opage, "]/Border[0 0 0]\n");
 
                if (xr->dest.type == PAGE) {
-                   objtext(annot, "/Dest [");
-                   objref(annot, (object *)xr->dest.page->spare);
-                   objtext(annot, " /XYZ null null null]\n");
+                   objtext(opage, "/Dest");
+                   objdest(opage, xr->dest.page);
                } else {
-                   objtext(annot, "/A <<\n/Type /Action\n/S /URI\n/URI ");
-                   pdf_string(objtext, annot, xr->dest.url);
-                   objtext(annot, "\n>>\n");
+                   objtext(opage, "/A<</S/URI/URI");
+                   pdf_string(objtext, opage, xr->dest.url);
+                   objtext(opage, ">>");
                }
 
-               objtext(annot, ">>\n");
+               objtext(opage, ">>\n");
            }
 
            objtext(opage, "]\n");
@@ -370,7 +596,7 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
     /*
      * Set up the outlines dictionary.
      */
-    {
+    if (outlines) {
        int topcount;
        char buf[80];
 
@@ -387,12 +613,28 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
     for (o = olist.head; o; o = o->next) {
        rdstringc rs = {0, 0, NULL};
        char text[80];
+       deflate_compress_ctx *zcontext;
+       void *zbuf;
+       int zlen;
 
        sprintf(text, "%d 0 obj\n", o->number);
        rdaddsc(&rs, text);
 
-       if (!o->main.text && o->stream.text) {
-           sprintf(text, "<<\n/Length %d\n>>\n", o->stream.pos);
+       if (o->stream.text) {
+           if (!o->main.text)
+               rdaddsc(&o->main, "<<\n");
+#ifdef PDF_NOCOMPRESS
+           zlen = o->stream.pos;
+           zbuf = snewn(zlen, char);
+           memcpy(zbuf, o->stream.text, zlen);
+           sprintf(text, "/Length %d\n>>\n", zlen);
+#else
+           zcontext = deflate_compress_new(DEFLATE_TYPE_ZLIB);
+           deflate_compress_data(zcontext, o->stream.text, o->stream.pos,
+                                 DEFLATE_END_OF_DATA, &zbuf, &zlen);
+           deflate_compress_free(zcontext);
+           sprintf(text, "/Filter/FlateDecode\n/Length %d\n>>\n", zlen);
+#endif
            rdaddsc(&o->main, text);
        }
 
@@ -404,15 +646,11 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
            rdaddc(&rs, '\n');
 
        if (o->stream.text) {
-           /*
-            * FIXME: If we ever start compressing stream data then
-            * it will have zero bytes in it, so we'll have to be
-            * more careful than this.
-            */
            rdaddsc(&rs, "stream\n");
-           rdaddsc(&rs, o->stream.text);
+           rdaddsn(&rs, zbuf, zlen);
            rdaddsc(&rs, "\nendstream\n");
            sfree(o->stream.text);
+           sfree(zbuf);
        }
 
        rdaddsc(&rs, "endobj\n");
@@ -425,7 +663,10 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
      * Write out the PDF file.
      */
 
-    fp = fopen(filename, "wb");
+    if (!strcmp(filename, "-"))
+       fp = stdout;
+    else
+       fp = fopen(filename, "wb");
     if (!fp) {
        error(err_cantopenw, filename);
        return;
@@ -433,9 +674,11 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
 
     /*
      * Header. I'm going to put the version IDs in the header as
-     * well, simply in PDF comments.
+     * well, simply in PDF comments.  The PDF Reference also suggests
+     * that binary PDF files contain four top-bit-set characters in
+     * the second line.
      */
-    fileoff = fprintf(fp, "%%PDF-1.3\n");
+    fileoff = fprintf(fp, "%%PDF-1.3\n%% L\xc3\xba\xc3\xb0""a\n");
     for (p = sourceform; p; p = p->next)
        if (p->type == para_VersionID)
            fileoff += pdf_versionid(fp, p->words);
@@ -466,18 +709,19 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords,
     /*
      * Trailer
      */
-    fprintf(fp, "trailer\n<<\n/Size %d\n/Root %d 0 R\n>>\n",
-           olist.tail->number + 1, cat->number);
+    fprintf(fp, "trailer\n<<\n/Size %d\n/Root %d 0 R\n/Info %d 0 R\n>>\n",
+           olist.tail->number + 1, cat->number, info->number);
     fprintf(fp, "startxref\n%d\n%%%%EOF\n", fileoff);
 
-    fclose(fp);
+    if (fp != stdout)
+       fclose(fp);
 
     sfree(filename);
 }
 
-static object *new_object(objlist *list)
+object *new_object(objlist *list)
 {
-    object *obj = mknew(object);
+    object *obj = snew(object);
 
     obj->list = list;
 
@@ -501,12 +745,17 @@ static object *new_object(objlist *list)
     return obj;
 }
 
-static void objtext(object *o, char const *text)
+void objtext(object *o, char const *text)
 {
     rdaddsc(&o->main, text);
 }
 
-static void objstream(object *o, char const *text)
+void objstream_len(object *o, char const *text, size_t len)
+{
+    rdaddsn(&o->stream, text, len);
+}
+
+void objstream(object *o, char const *text)
 {
     rdaddsc(&o->stream, text);
 }
@@ -518,8 +767,30 @@ static void objref(object *o, object *dest)
     rdaddsc(&o->main, buf);
 }
 
+static void objdest(object *o, page_data *p) {
+    objtext(o, "[");
+    objref(o, (object *)p->spare);
+    objtext(o, "/XYZ null null null]");
+}
+
+static char const * const stdfonts[] = {
+    "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic",
+    "Helvetica", "Helvetica-Bold", "Helvetica-Oblique","Helvetica-BoldOblique",
+    "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique",
+    "Symbol", "ZapfDingbats"
+};
+
+static int is_std_font(char const *name) {
+    unsigned i;
+    for (i = 0; i < lenof(stdfonts); i++)
+       if (strcmp(name, stdfonts[i]) == 0)
+           return TRUE;
+    return FALSE;
+}
+
 static void make_pages_node(object *node, object *parent, page_data *first,
-                           page_data *last, object *resources)
+                           page_data *last, object *resources,
+                           object *mediabox)
 {
     int count;
     page_data *page;
@@ -567,7 +838,8 @@ static void make_pages_node(object *node, object *parent, page_data *first,
                objtext((object *)thisfirst->spare, "\n");
            } else {
                object *newnode = new_object(node->list);
-               make_pages_node(newnode, node, thisfirst, thislast, NULL);
+               make_pages_node(newnode, node, thisfirst, thislast,
+                               NULL, NULL);
                objref(node, newnode);
            }
            objtext(node, "\n");
@@ -594,6 +866,11 @@ static void make_pages_node(object *node, object *parent, page_data *first,
        objref(node, resources);
        objtext(node, "\n");
     }
+    if (mediabox) {
+       objtext(node, "/MediaBox ");
+       objref(node, mediabox);
+       objtext(node, "\n");
+    }
 
     objtext(node, ">>\n");
 }
@@ -603,44 +880,32 @@ static void make_pages_node(object *node, object *parent, page_data *first,
  * means that glyphs are identified by PS strings and hence font
  * encoding can be managed independently of the supplied encoding
  * of the font. However, in the document outline, the PDF spec
- * simply asks for ordinary text strings without mentioning what
- * character set they are supposed to be interpreted in.
- * 
- * Therefore, for the moment, I'm going to assume they're US-ASCII
- * only. If anyone knows better, they should let me know :-/
+ * encodes in either PDFDocEncoding (a custom superset of
+ * ISO-8859-1) or UTF-16BE.
  */
-static int pdf_convert(wchar_t *s, char **result) {
-    int doing = (result != 0);
-    int ok = TRUE;
-    char *p = NULL;
-    int plen = 0, psize = 0;
-
-    for (; *s; s++) {
-       wchar_t c = *s;
-       char outc;
-
-       if (c >= 32 && c <= 126) {
-           /* Char is OK. */
-           outc = (char)c;
-       } else {
-           /* Char is not OK. */
-           ok = FALSE;
-           outc = 0xBF;               /* approximate the good old DEC `uh?' */
-       }
-       if (doing) {
-           if (plen >= psize) {
-               psize = plen + 256;
-               p = resize(p, psize);
-           }
-           p[plen++] = outc;
-       }
+char *pdf_outline_convert(wchar_t *s, int *len) {
+    char *ret;
+
+    ret = utoa_careful_dup(s, CS_PDF);
+
+    /*
+     * Very silly special case: if the returned string begins with
+     * FE FF, then the PDF reader will mistake it for a UTF-16BE
+     * string. So in this case we give up on PDFDocEncoding and
+     * encode it in UTF-16 straight away.
+     */
+    if (ret && ret[0] == '\xFE' && ret[1] == '\xFF') {
+       sfree(ret);
+       ret = NULL;
     }
-    if (doing) {
-       p = resize(p, plen+1);
-       p[plen] = '\0';
-       *result = p;
+
+    if (!ret) {
+       ret = utoa_dup_len(s, CS_UTF16BE, len);
+    } else {
+       *len = strlen(ret);
     }
-    return ok;
+
+    return ret;
 }
 
 static int make_outline(object *parent, outline_element *items, int n,
@@ -656,6 +921,7 @@ static int make_outline(object *parent, outline_element *items, int n,
 
     while (n > 0) {
        char *title;
+       int titlelen;
 
        /*
         * Here we expect to be sitting on an item at the given
@@ -664,19 +930,20 @@ static int make_outline(object *parent, outline_element *items, int n,
         */
        assert(items->level == level);
 
-       pdf_convert(items->pdata->outline_title, &title);
+       title = pdf_outline_convert(items->pdata->outline_title, &titlelen);
 
        totalcount++;
        curr = new_object(parent->list);
        if (!first) first = curr;
        last = curr;
        objtext(curr, "<<\n/Title ");
-       pdf_string(objtext, curr, title);
+       pdf_string_len(objtext, curr, title, titlelen);
+       sfree(title);
        objtext(curr, "\n/Parent ");
        objref(curr, parent);
-       objtext(curr, "\n/Dest [");
-       objref(curr, (object *)items->pdata->first->page->spare);
-       objtext(curr, " /XYZ null null null]\n");
+       objtext(curr, "\n/Dest");
+       objdest(curr, items->pdata->first->page);
+       objtext(curr, "\n");
        if (prev) {
            objtext(curr, "/Prev ");
            objref(curr, prev);
@@ -742,7 +1009,7 @@ static int pdf_versionid(FILE *fp, word *words)
 
        switch (type) {
          case word_Normal:
-           text = utoa_dup(words->text);
+           text = utoa_dup(words->text, CS_ASCII);
            break;
          case word_WhiteSpace:
            text = dupstr(" ");
@@ -762,19 +1029,30 @@ static int pdf_versionid(FILE *fp, word *words)
     return ret;
 }
 
-static void pdf_string(void (*add)(object *, char const *),
-                      object *o, char const *str)
+static void pdf_string_len(void (*add)(object *, char const *),
+                          object *o, char const *str, int len)
 {
     char const *p;
 
     add(o, "(");
-    for (p = str; *p; p++) {
-       char c[2];
-       if (*p == '\\' || *p == '(' || *p == ')')
-           add(o, "\\");
-       c[0] = *p;
-       c[1] = '\0';
+    for (p = str; len > 0; p++, len--) {
+       char c[10];
+       if (*p < ' ' || *p > '~') {
+           sprintf(c, "\\%03o", 0xFF & (int)*p);
+       } else {
+           int n = 0;
+           if (*p == '\\' || *p == '(' || *p == ')')
+               c[n++] = '\\';
+           c[n++] = *p;
+           c[n] = '\0';
+       }
        add(o, c);
     }
     add(o, ")");
 }
+
+static void pdf_string(void (*add)(object *, char const *),
+                      object *o, char const *str)
+{
+    pdf_string_len(add, o, str, strlen(str));
+}