X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/halibut/blobdiff_plain/44407fea05657f91da34fd83428b67e7100ab62b..7e2417cca16da830a1d0898380b256a77ec04de7:/bk_pdf.c diff --git a/bk_pdf.c b/bk_pdf.c index 0a36ad9..71307d2 100644 --- a/bk_pdf.c +++ b/bk_pdf.c @@ -5,6 +5,7 @@ #include #include "halibut.h" #include "paper.h" +#include "deflate.h" #define TREE_BRANCH 2 /* max branching factor in page tree */ @@ -112,7 +113,7 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, /* * The catalogue just contains references to the outlines and - * pages objects. + * pages objects, and the pagelabels dictionary. */ objtext(cat, "<<\n/Type /Catalog"); if (outlines) { @@ -121,6 +122,8 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, } objtext(cat, "\n/Pages "); objref(cat, pages); + /* Halibut just numbers pages 1, 2, 3, ... */ + objtext(cat, "\n/PageLabels<>]>>"); if (outlines) objtext(cat, "\n/PageMode /UseOutlines"); objtext(cat, "\n>>\n"); @@ -130,10 +133,10 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, * 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; + int i, prev; object *font; sprintf(fname, "f%d", font_index++); @@ -157,9 +160,13 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, 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"); + if (i != prev + 1) { + sprintf(buf, "\n%d", i); + objtext(font, buf); + } + objtext(font, i % 8 ? "/" : "\n/"); + objtext(font, fe->vector[i]); + prev = i; } objtext(font, "\n]\n>>\n"); @@ -418,31 +425,26 @@ 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, "<lx / FUNITS_PER_PT, xr->by / FUNITS_PER_PT, xr->rx / FUNITS_PER_PT, xr->ty / FUNITS_PER_PT); - objtext(annot, buf); - objtext(annot, "]\n/Border [0 0 0]\n"); + 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["); + objref(opage, (object *)xr->dest.page->spare); + objtext(opage, "/XYZ null null null]"); } 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<dest.url); + objtext(opage, ">>"); } - objtext(annot, ">>\n"); + objtext(opage, ">>\n"); } objtext(opage, "]\n"); @@ -471,12 +473,19 @@ 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); + 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, "<<\n/Filter/FlateDecode\n/Length %d\n>>\n", zlen); rdaddsc(&o->main, text); } @@ -488,15 +497,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"); @@ -517,9 +522,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);