X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/halibut/blobdiff_plain/7e2417cca16da830a1d0898380b256a77ec04de7..39d3d9002fc5eb3a66e926af9e5350030762add9:/bk_pdf.c diff --git a/bk_pdf.c b/bk_pdf.c index 71307d2..74944f0 100644 --- a/bk_pdf.c +++ b/bk_pdf.c @@ -7,7 +7,7 @@ #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) { @@ -34,12 +34,13 @@ struct objlist_Tag { static object *new_object(objlist *list); static void objtext(object *o, char const *text); static void objstream(object *o, char const *text); +static void objstream_len(object *o, char const *text, size_t len); 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 char *pdf_outline_convert(wchar_t *s, int *len); +static void objdest(object *o, page_data *p); static int is_std_font(char const *name); @@ -158,14 +159,14 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, for (i = 0; i < 256; i++) { char buf[20]; - if (!fe->vector[i]) + 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, fe->vector[i]); + objtext(font, glyph_extern(fe->vector[i])); prev = i; } @@ -189,7 +190,7 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, font_info const *fi = fe->font->info; int flags; for (i = 0; i < 256; i++) - if (fe->indices[i] >= 0) { + if (fe->vector[i] != NOGLYPH) { if (firstchar < 0) firstchar = i; lastchar = i; } @@ -201,10 +202,10 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, objtext(widths, "[\n"); for (i = firstchar; i <= lastchar; i++) { double width; - if (fe->indices[i] < 0) + if (fe->vector[i] == NOGLYPH) width = 0.0; else - width = fi->widths[fe->indices[i]]; + width = find_width(fe->font, fe->vector[i]); sprintf(buf, "%g\n", 1000.0 * width / FUNITS_PER_PT); objtext(widths, buf); } @@ -235,16 +236,22 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, objtext(fontdesc, buf); sprintf(buf, "/StemV %g\n", fi->stemv); objtext(fontdesc, buf); - if (fi->fp) { + if (fi->fontfile && fi->filetype == TYPE1) { object *fontfile = new_object(&olist); - char buf[513]; size_t len; - rewind(fi->fp); - do { - len = fread(buf, 1, sizeof(buf)-1, fi->fp); - buf[len] = 0; - objstream(fontfile, buf); - } while (len == sizeof(buf)-1); + 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); } @@ -435,9 +442,8 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, objtext(opage, "]/Border[0 0 0]\n"); if (xr->dest.type == PAGE) { - objtext(opage, "/Dest["); - objref(opage, (object *)xr->dest.page->spare); - objtext(opage, "/XYZ null null null]"); + objtext(opage, "/Dest"); + objdest(opage, xr->dest.page); } else { objtext(opage, "/A<dest.url); @@ -480,12 +486,21 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, sprintf(text, "%d 0 obj\n", o->number); rdaddsc(&rs, text); - if (!o->main.text && o->stream.text) { + 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, "<<\n/Filter/FlateDecode\n/Length %d\n>>\n", zlen); + sprintf(text, "/Filter/FlateDecode\n/Length %d\n>>\n", zlen); +#endif rdaddsc(&o->main, text); } @@ -597,6 +612,11 @@ static void objtext(object *o, char const *text) rdaddsc(&o->main, text); } +static void objstream_len(object *o, char const *text, size_t len) +{ + rdaddsn(&o->stream, text, len); +} + static void objstream(object *o, char const *text) { rdaddsc(&o->stream, text); @@ -609,6 +629,12 @@ 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", @@ -719,7 +745,7 @@ static void make_pages_node(object *node, object *parent, page_data *first, * encodes in either PDFDocEncoding (a custom superset of * ISO-8859-1) or UTF-16BE. */ -static char *pdf_outline_convert(wchar_t *s, int *len) { +char *pdf_outline_convert(wchar_t *s, int *len) { char *ret; ret = utoa_careful_dup(s, CS_PDF); @@ -777,9 +803,9 @@ static int make_outline(object *parent, outline_element *items, int n, 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);