A user recently reported that Halibut's font-file handling doesn't
[sgt/halibut] / bk_ps.c
diff --git a/bk_ps.c b/bk_ps.c
index e722b10..95c0451 100644 (file)
--- a/bk_ps.c
+++ b/bk_ps.c
@@ -13,7 +13,6 @@
 #define PS_MAXWIDTH 255
 
 static void ps_comment(FILE *fp, char const *leader, word *words);
-static void ps_token(FILE *fp, int *cc, char const *fmt, ...);
 static void ps_string_len(FILE *fp, int *cc, char const *str, int len);
 static void ps_string(FILE *fp, int *cc, char const *str);
 
@@ -49,7 +48,10 @@ void ps_backend(paragraph *sourceform, keywordlist *keywords,
        }
     }
 
-    fp = fopen(filename, "w");
+    if (!strcmp(filename, "-"))
+       fp = stdout;
+    else
+       fp = fopen(filename, "w");
     if (!fp) {
        error(err_cantopenw, filename);
        return;
@@ -68,17 +70,17 @@ void ps_backend(paragraph *sourceform, keywordlist *keywords,
     fprintf(fp, "%%%%DocumentNeededResources:\n");
     for (fe = doc->fonts->head; fe; fe = fe->next)
        /* XXX This may request the same font multiple times. */
-       if (!fe->font->info->fp)
+       if (!fe->font->info->fontfile)
            fprintf(fp, "%%%%+ font %s\n", fe->font->info->name);
-    fprintf(fp, "%%%%DocumentSuppliedResources: procset Halibut 0 2\n");
+    fprintf(fp, "%%%%DocumentSuppliedResources: procset Halibut 0 3\n");
     for (fe = doc->fonts->head; fe; fe = fe->next)
        /* XXX This may request the same font multiple times. */
-       if (fe->font->info->fp)
+       if (fe->font->info->fontfile)
            fprintf(fp, "%%%%+ font %s\n", fe->font->info->name);
     fprintf(fp, "%%%%EndComments\n");
 
     fprintf(fp, "%%%%BeginProlog\n");
-    fprintf(fp, "%%%%BeginResource: procset Halibut 0 2\n");
+    fprintf(fp, "%%%%BeginResource: procset Halibut 0 3\n");
     /*
      * Supply a prologue function which allows a reasonably
      * compressed representation of the text on the pages.
@@ -108,6 +110,8 @@ void ps_backend(paragraph *sourceform, keywordlist *keywords,
      * "p" generates a named destination referencing this page.
      * "x" generates a link to a named destination.
      * "u" generates a link to a URI.
+     * "o" generates an outline entry.
+     * "m" generates a general pdfmark.
      *
      * They all do nothing if pdfmark is undefined.
      */
@@ -120,10 +124,16 @@ void ps_backend(paragraph *sourceform, keywordlist *keywords,
            "  /u { 2 dict dup /Subtype /URI put dup /URI 4 -1 roll put\n"
            "       [ /Action 3 -1 roll /Rect 5 -1 roll /Border [0 0 0]\n"
            "       /Subtype /Link /ANN pdfmark } bind def\n"
-           "} {\n"
+           "  /o { [ /Count 3 -1 roll /Dest 5 -1 roll /Title 7 -1 roll\n"
+           "       /OUT pdfmark } bind def\n"
+           "  /m /pdfmark load def\n"
+           "}\n");
+    fprintf(fp, "{\n"
            "  /p { pop } bind def\n"
            "  /x { pop pop } bind def\n"
            "  /u /x load def\n"
+           "  /o { pop pop pop } bind def\n"
+           "  /m /cleartomark load def\n"
            "} ifelse\n");
 
     fprintf(fp, "%%%%EndResource\n");
@@ -163,9 +173,7 @@ void ps_backend(paragraph *sourceform, keywordlist *keywords,
             doc->paper_height / FUNITS_PER_PT);
     ps_token(fp, &cc, "} if\n");
 
-    /* Outline etc, only if pdfmark is supported */
-    ps_token(fp, &cc, "/pdfmark where { pop %% if\n");
-    ps_token(fp, &cc, "  [/PageMode/UseOutlines/DOCVIEW pdfmark\n");
+    ps_token(fp, &cc, "[/PageMode/UseOutlines/DOCVIEW m\n");
     noe = doc->n_outline_elements;
     for (oe = doc->outline_elements; noe; oe++, noe--) {
        char *title;
@@ -173,9 +181,9 @@ void ps_backend(paragraph *sourceform, keywordlist *keywords,
 
        title = pdf_outline_convert(oe->pdata->outline_title, &titlelen);
        if (oe->level == 0) {
-           ps_token(fp, &cc, "  [/Title");
+           ps_token(fp, &cc, "[/Title");
            ps_string_len(fp, &cc, title, titlelen);
-           ps_token(fp, &cc, "/DOCINFO pdfmark\n");
+           ps_token(fp, &cc, "/DOCINFO m\n");
        }
 
        count = 0;
@@ -184,25 +192,20 @@ void ps_backend(paragraph *sourceform, keywordlist *keywords,
                count++;
        if (oe->level > 0) count = -count;
 
-       ps_token(fp, &cc, "  [/Title");
        ps_string_len(fp, &cc, title, titlelen);
        sfree(title);
-       ps_token(fp, &cc, "/Dest%s/Count %d/OUT pdfmark\n",
+       ps_token(fp, &cc, "%s %d o\n",
                (char *)oe->pdata->first->page->spare, count);
     }
-    ps_token(fp, &cc, "} if\n");
 
     for (fe = doc->fonts->head; fe; fe = fe->next) {
        /* XXX This may request the same font multiple times. */
-       if (fe->font->info->fp) {
-           char buf[512];
-           size_t len;
+       if (fe->font->info->fontfile) {
            fprintf(fp, "%%%%BeginResource: font %s\n", fe->font->info->name);
-           rewind(fe->font->info->fp);
-           do {
-               len = fread(buf, 1, sizeof(buf), fe->font->info->fp);
-               fwrite(buf, 1, len, fp);
-           } while (len == sizeof(buf));
+           if (fe->font->info->filetype == TYPE1)
+               pf_writeps(fe->font->info, fp);
+           else
+               sfnt_writeps(fe->font->info, fp);
            fprintf(fp, "%%%%EndResource\n");
        } else {
            fprintf(fp, "%%%%IncludeResource: font %s\n",
@@ -226,8 +229,7 @@ void ps_backend(paragraph *sourceform, keywordlist *keywords,
        ps_token(fp, &cc, "{1 index /FID ne {def} {pop pop} ifelse} forall\n");
        ps_token(fp, &cc, "/Encoding [\n");
        for (i = 0; i < 256; i++)
-           ps_token(fp, &cc, "/%s",
-                    fe->vector[i] ? fe->vector[i] : ".notdef");
+           ps_token(fp, &cc, "/%s", glyph_extern(fe->vector[i]));
        ps_token(fp, &cc, "] def\n");
        ps_token(fp, &cc, "currentdict end\n");
        ps_token(fp, &cc, "/fontname-%s exch definefont /%s exch def\n",
@@ -305,7 +307,8 @@ void ps_backend(paragraph *sourceform, keywordlist *keywords,
 
     fprintf(fp, "%%%%EOF\n");
 
-    fclose(fp);
+    if (fp != stdout)
+       fclose(fp);
 
     sfree(filename);
 }
@@ -352,7 +355,7 @@ static void ps_comment(FILE *fp, char const *leader, word *words) {
     fprintf(fp, "\n");
 }
 
-static void ps_token(FILE *fp, int *cc, char const *fmt, ...) {
+void ps_token(FILE *fp, int *cc, char const *fmt, ...) {
     va_list ap;
 
     va_start(ap, fmt);