X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/halibut/blobdiff_plain/f8194b2183dd1edcf5be1da2d2b9d32f1f2a6ce2..944c4044572da20ed1fb9bc01085221856b53c1f:/bk_pdf.c diff --git a/bk_pdf.c b/bk_pdf.c index ba19372..6daebc6 100644 --- a/bk_pdf.c +++ b/bk_pdf.c @@ -14,9 +14,6 @@ paragraph *pdf_config_filename(char *filename) 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; @@ -31,9 +28,6 @@ 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 *), @@ -56,7 +50,6 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, int font_index; font_encoding *fe; page_data *page; - int pageno; FILE *fp; char *filename; paragraph *p; @@ -136,8 +129,11 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, objtext(resources, "<<\n/ProcSet [/PDF/Text]\n/Font <<\n"); for (fe = doc->fonts->head; fe; fe = fe->next) { char fname[40]; + char buf[80]; int i, prev; - object *font; + object *font, *fontdesc; + int flags; + font_info const *fi = fe->font->info; sprintf(fname, "f%d", font_index++); fe->name = dupstr(fname); @@ -150,26 +146,13 @@ 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->info->name); - objtext(font, "\n/Encoding <<\n/Type /Encoding\n/Differences ["); - - for (i = 0; i < 256; i++) { - char buf[20]; - if (!fe->vector[i]) - continue; - 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"); + /* + * 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 @@ -181,36 +164,6 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, #define FF_SMALLCAP 0x00020000 #define FF_FORCEBOLD 0x00040000 - if (!is_std_font(fe->font->info->name)){ - object *widths = new_object(&olist); - object *fontdesc = new_object(&olist); - int firstchar = -1, lastchar = -1; - char buf[80]; - font_info const *fi = fe->font->info; - int flags; - for (i = 0; i < 256; i++) - if (fe->indices[i] >= 0) { - 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->indices[i] < 0) - width = 0.0; - else - width = fi->widths[fe->indices[i]]; - sprintf(buf, "%g\n", 1000.0 * width / FUNITS_PER_PT); - objtext(widths, buf); - } - objtext(widths, "]\n"); - objtext(font, "/FontDescriptor "); - objref(font, fontdesc); objtext(fontdesc, "<<\n/Type /FontDescriptor\n/Name /"); objtext(fontdesc, fi->name); flags = 0; @@ -235,18 +188,207 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, objtext(fontdesc, buf); sprintf(buf, "/StemV %g\n", fi->stemv); objtext(fontdesc, buf); - if (fi->fp) { + } + + 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, "<name); + objtext(cmap, "\n/CIDSystemInfo<>\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>" + "endcodespacerange\n"); + start = -1; nranges = nchars = 0; + for (i = 0; i < 256; i++) { + 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<>\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; + 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); + } + + } + + if (!is_std_font(fe->font->info->name)) { + 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); + } 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"); } @@ -284,7 +426,6 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, /* * Create and render the individual pages. */ - pageno = 0; for (page = doc->pages; page; page = page->next) { object *opage, *cstr; rect *r; @@ -479,12 +620,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); } @@ -513,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; @@ -560,12 +713,13 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, 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 = snew(object); @@ -591,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); }