X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/halibut/blobdiff_plain/e7d4c88fb39e6bada0a3e096bbb3670ba8f28bba..944c4044572da20ed1fb9bc01085221856b53c1f:/bk_pdf.c diff --git a/bk_pdf.c b/bk_pdf.c index a880872..6daebc6 100644 --- a/bk_pdf.c +++ b/bk_pdf.c @@ -50,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; @@ -196,8 +195,101 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, 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 "); - sfnt_cmap(fe, cmap); + 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); @@ -210,7 +302,7 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, objtext(cidfont, "/FontDescriptor "); objref(cidfont, fontdesc); objtext(cidfont, "\n/W[0["); - for (i = 0; i < sfnt_nglyphs(fe->font->info->fontfile); i++) { + for (i = 0; i < (int)sfnt_nglyphs(fe->font->info->fontfile); i++) { char buf[20]; double width; width = find_width(fe->font, @@ -334,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; @@ -537,7 +628,7 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, zbuf = snewn(zlen, char); memcpy(zbuf, o->stream.text, zlen); sprintf(text, "/Length %d\n>>\n", zlen); -#else +#else zcontext = deflate_compress_new(DEFLATE_TYPE_ZLIB); deflate_compress_data(zcontext, o->stream.text, o->stream.pos, DEFLATE_END_OF_DATA, &zbuf, &zlen); @@ -572,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; @@ -619,7 +713,8 @@ 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); }