home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / source / gs5.10 / gdevpdfm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-30  |  22.3 KB  |  789 lines

  1. /* Copyright (C) 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevpdfm.c */
  20. /* pdfmark processing for PDF-writing driver */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsutil.h"        /* for bytes_compare */
  26. #include "gdevpdfx.h"
  27. #include "scanchar.h"
  28.  
  29. /* GC descriptors */
  30. private_st_pdf_article();
  31. private_st_pdf_named_dest();
  32.  
  33. /*
  34.  * The pdfmark pseudo-parameter indicates the occurrence of a pdfmark
  35.  * operator in the input file.  Its "value" is the arguments of the operator,
  36.  * passed through essentially unchanged:
  37.  *    (key, value)*, CTM, type
  38.  */
  39.  
  40. /*
  41.  * Define the pdfmark types we know about.
  42.  */
  43. #define pdfmark_proc(proc)\
  44.   int proc(P4(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,\
  45.           const gs_matrix *pctm))
  46. typedef struct pdfmark_name_s {
  47.   const char *mname;
  48.   pdfmark_proc((*proc));
  49. } pdfmark_name;
  50. private pdfmark_proc(pdfmark_ANN);
  51. private pdfmark_proc(pdfmark_LNK);
  52. private pdfmark_proc(pdfmark_OUT);
  53. private pdfmark_proc(pdfmark_ARTICLE);
  54. private pdfmark_proc(pdfmark_DEST);
  55. private pdfmark_proc(pdfmark_PS);
  56. private pdfmark_proc(pdfmark_PAGES);
  57. private pdfmark_proc(pdfmark_PAGE);
  58. private pdfmark_proc(pdfmark_DOCINFO);
  59. private pdfmark_proc(pdfmark_DOCVIEW);
  60. private const pdfmark_name mark_names[] = {
  61.   { "ANN", pdfmark_ANN },
  62.   { "LNK", pdfmark_LNK },
  63.   { "OUT", pdfmark_OUT },
  64.   { "ARTICLE", pdfmark_ARTICLE },
  65.   { "DEST", pdfmark_DEST },
  66.   { "PS", pdfmark_PS },
  67.   { "PAGES", pdfmark_PAGES },
  68.   { "PAGE", pdfmark_PAGE },
  69.   { "DOCINFO", pdfmark_DOCINFO },
  70.   { "DOCVIEW", pdfmark_DOCVIEW },
  71.   { 0, 0 }
  72. };
  73.  
  74. /* ---------------- Public entry points ---------------- */
  75.  
  76. /* Compare a C string and a gs_param_string. */
  77. bool
  78. pdf_key_eq(const gs_param_string *pcs, const char *str)
  79. {    return (strlen(str) == pcs->size &&
  80.         !strncmp(str, (const char *)pcs->data, pcs->size));
  81. }
  82.  
  83. /* Process a pdfmark. */
  84. int
  85. pdfmark_process(gx_device_pdf *pdev, const gs_param_string_array *pma)
  86. {    const gs_param_string *data = pma->data;
  87.     uint size = pma->size;
  88.     const gs_param_string *pts = &data[size - 1];
  89.     gs_matrix ctm;
  90.     int i;
  91.  
  92.     if ( (size & 1) || size < 2 ||
  93.          sscanf((const char *)pts[-1].data, "[%g %g %g %g %g %g]",
  94.             &ctm.xx, &ctm.xy, &ctm.yx, &ctm.yy, &ctm.tx, &ctm.ty) != 6
  95.        )
  96.       return_error(gs_error_rangecheck);
  97.     /*
  98.      * Our coordinate system is scaled so that user space is always
  99.      * default user space.  Adjust the CTM to match this.
  100.      */
  101.     { double xscale = 72.0 / pdev->HWResolution[0],
  102.         yscale = 72.0 / pdev->HWResolution[1];
  103.  
  104.       ctm.xx *= xscale, ctm.xy *= yscale;
  105.       ctm.yx *= xscale, ctm.yy *= yscale;
  106.       ctm.tx *= xscale, ctm.ty *= yscale;
  107.     }
  108.     for ( i = 0; mark_names[i].mname != 0; ++i )
  109.       if ( pdf_key_eq(pts, mark_names[i].mname) )
  110.         return (*mark_names[i].proc)(pdev, data, size - 2, &ctm);
  111.     return 0;
  112. }
  113.  
  114. /* ---------------- Utilities ---------------- */
  115.  
  116. /* Find a key in a dictionary. */
  117. private bool
  118. pdfmark_find_key(const char *key, const gs_param_string *pairs, uint count,
  119.   gs_param_string *pstr)
  120. {    uint i;
  121.     for ( i = 0; i < count; i += 2 )
  122.       if ( pdf_key_eq(&pairs[i], key) )
  123.         { *pstr = pairs[i + 1];
  124.           return true;
  125.         }
  126.     pstr->data = 0;
  127.     pstr->size = 0;
  128.     return false;
  129. }
  130.  
  131. /* Scan an integer out of a parameter string. */
  132. private int
  133. pdfmark_scan_int(const gs_param_string *pstr, int *pvalue)
  134. {
  135. #define max_int_str 20
  136.     uint size = pstr->size;
  137.     char str[max_int_str + 1];
  138.  
  139.     if ( size > max_int_str )
  140.       return_error(gs_error_limitcheck);
  141.     memcpy(str, pstr->data, size);
  142.     str[size] = 0;
  143.     return(sscanf(str, "%d", pvalue) == 1 ? 0 :
  144.            gs_note_error(gs_error_rangecheck));
  145. #undef max_int_str
  146. }
  147.  
  148. /*
  149.  * Get the page number for a page referenced by number or as /Next or /Prev.
  150.  * The result may be 0 if the page number is 0 or invalid.
  151.  */
  152. private int
  153. pdfmark_page_number(gx_device_pdf *pdev, const gs_param_string *pnstr)
  154. {    int page = pdev->next_page + 1;
  155.  
  156.     if ( pnstr->data == 0 )
  157.       ;
  158.     else if ( pdf_key_eq(pnstr, "/Next") )
  159.       ++page;
  160.     else if ( pdf_key_eq(pnstr, "/Prev") )
  161.       --page;
  162.     else if ( pdfmark_scan_int(pnstr, &page) < 0 )
  163.       page = 0;
  164.     return page;
  165. }
  166.  
  167. /* Construct a destination string specified by /Page and/or /View. */
  168. /* Return 0 if none (but still fill in a default), 1 if present, */
  169. /* <0 if error. */
  170. private int
  171. pdfmark_make_dest(char dstr[max_dest_string], gx_device_pdf *pdev,
  172.   const gs_param_string *pairs, uint count)
  173. {    gs_param_string page_string, view_string;
  174.     bool present =
  175.       pdfmark_find_key("Page", pairs, count, &page_string) |
  176.         pdfmark_find_key("View", pairs, count, &view_string);
  177.     int page = pdfmark_page_number(pdev, &page_string);
  178.     gs_param_string action;
  179.     int len;
  180.  
  181.     if ( view_string.size == 0 )
  182.       param_string_from_string(view_string, "[/XYZ 0 0 1]");
  183.     if ( page == 0 )
  184.       strcpy(dstr, "[null ");
  185.     else if ( pdfmark_find_key("Action", pairs, count, &action) &&
  186.           pdf_key_eq(&action, "/GoToR")
  187.         )
  188.       sprintf(dstr, "[%d ", page - 1);
  189.     else
  190.       sprintf(dstr, "[%ld 0 R ", pdf_page_id(pdev, page));
  191.     len = strlen(dstr);
  192.     if ( len + view_string.size > max_dest_string )
  193.       return_error(gs_error_limitcheck);
  194.     if ( view_string.data[0] != '[' ||
  195.          view_string.data[view_string.size - 1] != ']'
  196.        )
  197.       return_error(gs_error_rangecheck);
  198.     memcpy(dstr + len, view_string.data + 1, view_string.size - 1);
  199.     dstr[len + view_string.size - 1] = 0;
  200.     return present;
  201. }
  202.  
  203. /* Write pairs for a dictionary. */
  204. private void
  205. pdfmark_write_pair(stream *s, const gs_param_string *pair)
  206. {    pputc(s, '/');
  207.     pwrite(s, pair->data, pair->size);
  208.     pputc(s, ' ');
  209.     pwrite(s, pair[1].data, pair[1].size);
  210.     pputc(s, '\n');
  211. }
  212.  
  213. /* Scan a Rect value. */
  214. private int
  215. pdfmark_scan_rect(gs_rect *prect, const gs_param_string *str,
  216.   const gs_matrix *pctm)
  217. {    uint size = str->size;
  218.     double v[4];
  219. #define max_rect_string_size 100
  220.     char chars[max_rect_string_size + 3];
  221.     int end_check;
  222.  
  223.     if ( str->size > max_rect_string_size )
  224.       return_error(gs_error_limitcheck);
  225.     memcpy(chars, str->data, size);
  226.     strcpy(chars + size, " 0");
  227.     if ( sscanf(chars, "[%lg %lg %lg %lg]%d",
  228.             &v[0], &v[1], &v[2], &v[3], &end_check) != 5
  229.        )
  230.       return_error(gs_error_rangecheck);
  231.     gs_point_transform(v[0], v[1], pctm, &prect->p);
  232.     gs_point_transform(v[2], v[3], pctm, &prect->q);
  233.     return 0;
  234. }
  235.  
  236. /* Write a Rect value. */
  237. private void
  238. pdfmark_write_rect(stream *s, const char *key, const gs_rect *prect)
  239. {    pprints1(s, "/%s ", key);
  240.     pprintg4(s, "[%g %g %g %g]\n",
  241.          prect->p.x, prect->p.y, prect->q.x, prect->q.y);
  242. }
  243.  
  244. /* Copy an annotation dictionary. */
  245. private int
  246. pdfmark_annot(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  247.   const gs_matrix *pctm, const char *subtype)
  248. {    pdf_resource *pres;
  249.     int code = pdf_begin_aside(pdev, &pdev->annots, NULL, &pres);
  250.     stream *s = pdev->strm;
  251.     bool subtype_present = false;
  252.     bool add_dest = false;
  253.     bool dest_present = false;
  254.     uint i;
  255.  
  256.     if ( code < 0 )
  257.       return code;
  258.     pres->rid = pdev->next_page;
  259.     pputs(s, "<< /Type /Annot\n");
  260.     for ( i = 0; i < count; i += 2 )
  261.       { const gs_param_string *pair = &pairs[i];
  262.         long src_pg;
  263.  
  264.         if ( pdf_key_eq(pair, "SrcPg") &&
  265.          sscanf((const char *)pair[1].data, "%ld", &src_pg) == 1
  266.            )
  267.           pres->rid = src_pg - 1;
  268.         else if ( pdf_key_eq(pair, "Page") || pdf_key_eq(pair, "View") )
  269.           add_dest = true;
  270.         /*
  271.          * For some unfathomable reason, PDF requires /A instead of
  272.          * /Action, and /S instead of /Subtype in the Action dictionary.
  273.          * Handle this here.
  274.          */
  275.         else if ( pdf_key_eq(pair, "Action") )
  276.           { const byte *astr = pair[1].data;
  277.             const uint asize = pair[1].size;
  278.         uint i;
  279.  
  280.         pputs(s, "/A ");
  281.         /* Search for the next occurrence of /Subtype. */
  282.         for ( i = 0; i < asize; ++i )
  283.           if ( asize - i > 8 && !memcmp(astr + i, "/Subtype", 8) &&
  284.                scan_char_decoder[astr[i + 8]] > ctype_name
  285.              )
  286.             { pputs(s, "/S");
  287.               i += 7;
  288.             }
  289.           else
  290.             pputc(s, astr[i]);
  291.             pputc(s, '\n');
  292.           }
  293.         else if ( pdf_key_eq(pair, "Rect") )
  294.           { gs_rect rect;
  295.  
  296.             code = pdfmark_scan_rect(&rect, pair + 1, pctm);
  297.         if ( code < 0 )
  298.           return code;
  299.         pdfmark_write_rect(s, "Rect", &rect);
  300.           }
  301.         else
  302.           { pdfmark_write_pair(s, pair);
  303.             if ( pdf_key_eq(pair, "Dest") )
  304.           dest_present = true;
  305.         else if ( pdf_key_eq(pair, "Subtype") )
  306.           subtype_present = true;
  307.           }
  308.       }
  309.     if ( add_dest && !dest_present )
  310.       { char dest[max_dest_string];
  311.         int code = pdfmark_make_dest(dest, pdev, pairs, count);
  312.         if ( code >= 0 )
  313.           pprints1(s, "/Dest %s\n", dest);
  314.       }
  315.     if ( !subtype_present )
  316.       pprints1(s, "/Subtype /%s ", subtype);
  317.     pputs(s, ">>\n");
  318.     pdf_end_aside(pdev);
  319.     return 0;
  320. }
  321.  
  322. /* ANN pdfmark */
  323. private int
  324. pdfmark_ANN(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  325.   const gs_matrix *pctm)
  326. {    return pdfmark_annot(pdev, pairs, count, pctm, "Text");
  327. }
  328.  
  329. /* LNK pdfmark (obsolescent) */
  330. private int
  331. pdfmark_LNK(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  332.   const gs_matrix *pctm)
  333. {    return pdfmark_annot(pdev, pairs, count, pctm, "Link");
  334. }
  335.  
  336. /* Save pairs in a string. */
  337. private bool
  338. pdf_key_member(const gs_param_string *pcs, const char * const keys[])
  339. {    const char * const *pkey;
  340.     for ( pkey = keys; *pkey != 0; ++pkey )
  341.       if ( pdf_key_eq(pcs, *pkey) )
  342.         return true;
  343.     return false;
  344. }
  345. #define pdfmark_size_pair(pair)\
  346.   ((pair)[0].size + (pair)[1].size + 3)
  347. private byte *
  348. pdfmark_save_pair(byte *next, const gs_param_string *pair)
  349. {    uint len;
  350.  
  351.     *next++ = '/';
  352.     memcpy(next, pair[0].data, len = pair[0].size);
  353.     next += len;
  354.     *next++ = ' ';
  355.     memcpy(next, pair[1].data, len = pair[1].size);
  356.     next += len;
  357.     *next++ = '\n';
  358.     return next;
  359. }
  360. private int
  361. pdfmark_save_edited_pairs(const gx_device_pdf *pdev,
  362.   const gs_param_string *pairs, uint count, const char * const skip_keys[],
  363.   const gs_param_string *add_pairs, uint add_count, gs_string *pstr)
  364. {    uint i, size;
  365.     byte *data;
  366.     byte *next;
  367.  
  368.     for ( i = 0, size = 0; i < count; i += 2 )
  369.       if ( !pdf_key_member(&pairs[i], skip_keys) )
  370.         size += pdfmark_size_pair(&pairs[i]);
  371.     for ( i = 0; i < add_count; i += 2 )
  372.       size += pdfmark_size_pair(&add_pairs[i]);
  373.     if ( pstr->data == 0 )
  374.       data = gs_alloc_string(pdev->pdf_memory, size, "pdfmark_save_pairs");
  375.     else
  376.       data = gs_resize_string(pdev->pdf_memory, pstr->data, pstr->size,
  377.                   size, "pdfmark_save_pairs");
  378.     if ( data == 0 )
  379.       return_error(gs_error_VMerror);
  380.     next = data;
  381.     for ( i = 0; i < count; i += 2 )
  382.       if ( !pdf_key_member(&pairs[i], skip_keys) )
  383.         next = pdfmark_save_pair(next, &pairs[i]);
  384.     for ( i = 0; i < add_count; i += 2 )
  385.       next = pdfmark_save_pair(next, &add_pairs[i]);
  386.     pstr->data = data;
  387.     pstr->size = size;
  388.     return 0;
  389. }
  390. static const char * const no_skip_pairs[] = { 0 };
  391. #define pdfmark_save_pairs(pdev, pairs, count, pstr)\
  392.   pdfmark_save_edited_pairs(pdev, pairs, count, no_skip_pairs, NULL, 0, pstr)
  393.  
  394. /* Write out one node of the outline tree. */
  395. private int
  396. pdfmark_write_outline(gx_device_pdf *pdev, pdf_outline_node *pnode,
  397.   long next_id)
  398. {    stream *s;
  399.  
  400.     pdf_open_separate(pdev, pnode->id);
  401.     s = pdev->strm;
  402.     pputs(s, "<< ");
  403.     pdf_write_saved_string(pdev, &pnode->action_string);
  404.     if ( pnode->count )
  405.       pprintd1(s, "/Count %d ", pnode->count);
  406.     pprintld1(s, "/Parent %ld 0 R\n", pnode->parent_id);
  407.     if ( pnode->prev_id )
  408.       pprintld1(s, "/Prev %ld 0 R\n", pnode->prev_id);
  409.     if ( next_id )
  410.       pprintld1(s, "/Next %ld 0 R\n", next_id);
  411.     if ( pnode->first_id )
  412.       pprintld2(s, "/First %ld 0 R /Last %ld 0 R\n",
  413.             pnode->first_id, pnode->last_id);
  414.     pputs(s, ">>\n");
  415.     pdf_end_separate(pdev);
  416.     return 0;
  417. }
  418.  
  419. /* Adjust the parent's count when writing an outline node. */
  420. private void
  421. pdfmark_adjust_parent_count(pdf_outline_level *plevel)
  422. {    pdf_outline_level *parent = plevel - 1;
  423.     int count = plevel->last.count;
  424.  
  425.     if ( count > 0 )
  426.       { if ( parent->last.count < 0 )
  427.           parent->last.count -= count;
  428.         else
  429.           parent->last.count += count;
  430.       }
  431. }
  432.  
  433. /* Close the current level of the outline tree. */
  434. int
  435. pdfmark_close_outline(gx_device_pdf *pdev)
  436. {    int depth = pdev->outline_depth;
  437.     pdf_outline_level *plevel = &pdev->outline_levels[depth];
  438.     int code;
  439.  
  440.     code = pdfmark_write_outline(pdev, &plevel->last, 0);
  441.     if ( code < 0 )
  442.       return code;
  443.     if ( depth > 0 )
  444.       { plevel[-1].last.last_id = plevel->last.id;
  445.         pdfmark_adjust_parent_count(plevel);
  446.         --plevel;
  447.         if ( plevel->last.count < 0 )
  448.           pdev->closed_outline_depth--;
  449.         pdev->outline_depth--;
  450.       }
  451.     return 0;
  452. }
  453.  
  454. /* OUT pdfmark */
  455. private int
  456. pdfmark_OUT(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  457.   const gs_matrix *pctm)
  458. {    int depth = pdev->outline_depth;
  459.     pdf_outline_level *plevel = &pdev->outline_levels[depth];
  460.     int sub_count = 0;
  461.     uint i;
  462.     pdf_outline_node node;
  463.     int code;
  464.  
  465.     for ( i = 0; i < count; i += 2 )
  466.       { const gs_param_string *pair = &pairs[i];
  467.  
  468.         if ( pdf_key_eq(pair, "Count") )
  469.           pdfmark_scan_int(pair + 1, &sub_count);
  470.       }
  471.     if ( sub_count != 0 && depth == max_outline_depth - 1 )
  472.       return_error(gs_error_limitcheck);
  473.     node.action_string.data = 0;
  474.     { static const char * const skip_out[] =
  475.         { "Page", "View", "Count", 0 };
  476.       char dest[max_dest_string];
  477.  
  478.       if ( pdfmark_make_dest(dest, pdev, pairs, count) ) {
  479.         gs_param_string add_dest[2];
  480.  
  481.         param_string_from_string(add_dest[0], "Dest");
  482.         param_string_from_string(add_dest[1], dest);
  483.         code = pdfmark_save_edited_pairs(pdev, pairs, count, skip_out,
  484.                          add_dest, 2,
  485.                          &node.action_string);
  486.       } else {
  487.         code = pdfmark_save_edited_pairs(pdev, pairs, count, skip_out + 2,
  488.                          NULL, 0, &node.action_string);
  489.       }
  490.       if ( code < 0 )
  491.         return code;
  492.     }
  493.     if ( pdev->outlines_id == 0 )
  494.       pdev->outlines_id = pdf_obj_ref(pdev);
  495.     node.id = pdf_obj_ref(pdev);
  496.     node.parent_id =
  497.       (depth == 0 ? pdev->outlines_id : plevel[-1].last.id);
  498.     node.prev_id = plevel->last.id;
  499.     node.first_id = node.last_id = 0;
  500.     node.count = sub_count;
  501.     /* Add this node to the outline at the current level. */
  502.     if ( plevel->first.id == 0 )
  503.       { /* First node at this level. */
  504.         if ( depth > 0 )
  505.           plevel[-1].last.first_id = node.id;
  506.         node.prev_id = 0;
  507.         plevel->first = node;
  508.       }
  509.     else
  510.       { /* Write out the previous node. */
  511.         if ( depth > 0 )
  512.           pdfmark_adjust_parent_count(plevel);
  513.         pdfmark_write_outline(pdev, &plevel->last, node.id);
  514.       }
  515.     plevel->last = node;
  516.     plevel->left--;
  517.     if ( !pdev->closed_outline_depth )
  518.       pdev->outlines_open++;
  519.     /* If this node has sub-nodes, descend one level. */
  520.     if ( sub_count != 0 )
  521.       { pdev->outline_depth++;
  522.         ++plevel;
  523.         plevel->left = (sub_count > 0 ? sub_count : -sub_count);
  524.         plevel->first.id = 0;
  525.         if ( sub_count < 0 )
  526.           pdev->closed_outline_depth++;
  527.       }
  528.     else
  529.       { while ( (depth = pdev->outline_depth) > 0 &&
  530.             pdev->outline_levels[depth].left == 0
  531.           )
  532.           pdfmark_close_outline(pdev);
  533.       }
  534.     return 0;
  535. }
  536.  
  537. /* Write an article bead. */
  538. int
  539. pdfmark_write_article(gx_device_pdf *pdev, const pdf_bead *pbead)
  540. {    stream *s;
  541.  
  542.     pdf_open_separate(pdev, pbead->id);
  543.     s = pdev->strm;
  544.     pprintld3(s,
  545.           "<<\n/T %ld 0 R\n/V %ld 0 R\n/N %ld 0 R\n",
  546.           pbead->article_id, pbead->prev_id, pbead->next_id);
  547.     pprints1(s, "/Dest %s\n", pbead->dest);
  548.     pdfmark_write_rect(s, "R", &pbead->rect);
  549.     pputs(s, ">>\n");
  550.     return pdf_end_separate(pdev);
  551. }
  552.  
  553. /* ARTICLE pdfmark */
  554. private int
  555. pdfmark_ARTICLE(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  556.   const gs_matrix *pctm)
  557. {    gs_param_string title;
  558.     gs_param_string rectstr;
  559.     gs_rect rect;
  560.     long bead_id;
  561.     pdf_article *part;
  562.     int code;
  563.  
  564.     if ( !pdfmark_find_key("Title", pairs, count, &title) ||
  565.          !pdfmark_find_key("Rect", pairs, count, &rectstr)
  566.        )
  567.       return_error(gs_error_rangecheck);
  568.     if ( (code = pdfmark_scan_rect(&rect, &rectstr, pctm)) < 0 )
  569.       return code;
  570.     /****** Should save other keys for Info dictionary ******/
  571.  
  572.     /* Find the article with this title, or create one. */
  573.     bead_id = pdf_obj_ref(pdev);
  574.     for ( part = pdev->articles; part != 0; part = part->next )
  575.       if ( !bytes_compare(part->title.data, part->title.size,
  576.                   title.data, title.size) )
  577.         break;
  578.     if ( part == 0 )
  579.       { /* Create the article. */
  580.         byte *str;
  581.  
  582.         part = gs_alloc_struct(pdev->pdf_memory, pdf_article,
  583.                    &st_pdf_article, "pdfmark_ARTICLE");
  584.         str = gs_alloc_string(pdev->pdf_memory, title.size,
  585.                   "article title");
  586.         if ( part == 0 || str == 0 )
  587.           return_error(gs_error_VMerror);
  588.         part->next = pdev->articles;
  589.         pdev->articles = part;
  590.         memcpy(str, title.data, title.size);
  591.         part->title.data = str;
  592.         part->title.size = title.size;
  593.         part->id = pdf_begin_separate(pdev);
  594.         part->first.id = part->last.id = 0;
  595.         pprintld1(pdev->strm, "<</F %ld 0 R>>\n", bead_id);
  596.         pdf_end_separate(pdev);
  597.       }
  598.  
  599.     /* Add the bead to the article. */
  600.     /* This is similar to what we do for outline nodes. */
  601.     if ( part->last.id == 0 )
  602.       { part->first.next_id = bead_id;
  603.         part->last.id = part->first.id;
  604.       }
  605.     else
  606.       { part->last.next_id = bead_id;
  607.         pdfmark_write_article(pdev, &part->last);
  608.       }
  609.     part->last.prev_id = part->last.id;
  610.     part->last.id = bead_id;
  611.     part->last.article_id = part->id;
  612.     part->last.next_id = 0;
  613.     part->last.rect = rect;
  614.     pdfmark_make_dest(part->last.dest, pdev, pairs, count);
  615.     if ( part->first.id == 0 )
  616.       { /* This is the first bead of the article. */
  617.         part->first = part->last;
  618.         part->last.id = 0;
  619.       }
  620.  
  621.     return 0;
  622. }
  623.  
  624. /* DEST pdfmark */
  625. private int
  626. pdfmark_DEST(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  627.   const gs_matrix *pctm)
  628. {    char dest[max_dest_string];
  629.     gs_param_string key;
  630.     pdf_named_dest *pnd;
  631.     byte *str;
  632.  
  633.     if ( !pdfmark_find_key("Dest", pairs, count, &key) ||
  634.          !pdfmark_make_dest(dest, pdev, pairs, count)
  635.        )
  636.       return_error(gs_error_rangecheck);
  637.     pnd = gs_alloc_struct(pdev->pdf_memory, pdf_named_dest,
  638.                   &st_pdf_named_dest, "pdfmark_DEST");
  639.     str = gs_alloc_string(pdev->pdf_memory, key.size,
  640.                   "named_dest key");
  641.     if ( pnd == 0 || str == 0 )
  642.       return_error(gs_error_VMerror);
  643.     pnd->next = pdev->named_dests;
  644.     memcpy(str, key.data, key.size);
  645.     pnd->key.data = str;
  646.     pnd->key.size = key.size;
  647.     strcpy(pnd->dest, dest);
  648.     pdev->named_dests = pnd;
  649.     return 0;
  650. }
  651.  
  652. /* Write the contents of pass-through code. */
  653. /* We are inside the stream dictionary, in a 'separate' object. */
  654. private int
  655. pdfmark_write_ps(gx_device_pdf *pdev, const gs_param_string *psource)
  656. {    stream *s = pdev->strm;
  657.     long length_id = pdf_obj_ref(pdev);
  658.     long start_pos, length;
  659.  
  660.     pprintld1(s, " /Length %ld 0 R >> stream\n", length_id);
  661.     start_pos = pdf_stell(pdev);
  662.     if ( psource->size < 2 || psource->data[0] != '(' ||
  663.          psource->data[psource->size - 1] != ')'
  664.        )
  665.       lprintf1("bad PS passthrough: %s\n", psource->data);
  666.     else
  667.       { /****** SHOULD REMOVE ESCAPES ******/
  668.         pwrite(s, psource->data + 1, psource->size - 2);
  669.       }
  670.     pputs(s, "\n");
  671.     length = pdf_stell(pdev) - start_pos;
  672.     pputs(s, "endstream\n");
  673.     pdf_end_separate(pdev);
  674.     pdf_open_separate(pdev, length_id);
  675.     pprintld1(s, "%ld\n", length);
  676.     pdf_end_separate(pdev);
  677.     return 0;
  678. }
  679.  
  680. /* PS pdfmark */
  681. private int
  682. pdfmark_PS(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  683.   const gs_matrix *pctm)
  684. {    stream *s;
  685.     gs_param_string source;
  686.     gs_param_string level1;
  687.  
  688.     if ( !pdfmark_find_key("DataSource", pairs, count, &source) )
  689.       return_error(gs_error_rangecheck);
  690.     pdfmark_find_key("Level1", pairs, count, &level1);
  691.     if ( level1.data == 0 && source.size <= 100 &&
  692.          pdev->CompatibilityLevel >= 1.2
  693.        )
  694.       { /* Insert the PostScript code in-line */
  695.         int code = pdf_open_contents(pdev, pdf_in_stream);
  696.  
  697.         if ( code < 0 )
  698.           return code;
  699.         s = pdev->strm;
  700.         pwrite(s, source.data, source.size);
  701.         pputs(s, " PS\n");
  702.       }
  703.     else
  704.       { /* Put the PostScript code in a resource. */
  705.         pdf_resource *pres;
  706.         int code = pdf_begin_resource(pdev, resourceXObject, gs_no_id,
  707.                       &pres);
  708.  
  709.         if ( code < 0 )
  710.           return code;
  711.         s = pdev->strm;
  712.         pputs(s, " /Subtype /PS");
  713.         if ( level1.data != 0 )
  714.           { long level1_id = pdf_obj_ref(pdev);
  715.  
  716.         pprintld1(s, " /Level1 %ld 0 R", level1_id);
  717.         pdfmark_write_ps(pdev, &source);
  718.         pdf_open_separate(pdev, level1_id);
  719.         pputs(pdev->strm, "<<");
  720.         pdfmark_write_ps(pdev, &level1);
  721.           }
  722.         else
  723.           pdfmark_write_ps(pdev, &source);
  724.         code = pdf_open_contents(pdev, pdf_in_stream);
  725.         if ( code < 0 )
  726.           return code;
  727.         pprintld1(pdev->strm, "/R%ld Do\n", pres->id);
  728.       }
  729.     return 0;
  730. }
  731.  
  732. /* PAGES pdfmark */
  733. private int
  734. pdfmark_PAGES(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  735.   const gs_matrix *pctm)
  736. {    return pdfmark_save_pairs(pdev, pairs, count, &pdev->pages_string);
  737. }
  738.  
  739. /* PAGE pdfmark */
  740. private int
  741. pdfmark_PAGE(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  742.   const gs_matrix *pctm)
  743. {    return pdfmark_save_pairs(pdev, pairs, count, &pdev->page_string);
  744. }
  745.  
  746. /* DOCINFO pdfmark */
  747. private int
  748. pdfmark_DOCINFO(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  749.   const gs_matrix *pctm)
  750. {    long info_id = pdf_begin_separate(pdev);
  751.     stream *s = pdev->strm;
  752.     uint i;
  753.  
  754.     if ( info_id < 0 )
  755.       return info_id;
  756.     pputs(s, "<<\n");
  757.     for ( i = 0; i < count; i += 2 )
  758.       if ( !pdf_key_eq(&pairs[i], "CreationDate") &&
  759.            !pdf_key_eq(&pairs[i], "Producer")
  760.          )
  761.         pdfmark_write_pair(s, &pairs[i]);
  762.     pdf_write_default_info(pdev);
  763.     pputs(s, ">>\n");
  764.     pdf_end_separate(pdev);
  765.     pdev->info_id = info_id;
  766.     return 0;
  767. }
  768.  
  769. /* DOCVIEW pdfmark */
  770. private int
  771. pdfmark_DOCVIEW(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  772.   const gs_matrix *pctm)
  773. {    char dest[max_dest_string];
  774.  
  775.     if ( pdfmark_make_dest(dest, pdev, pairs, count) )
  776.       { gs_param_string add_dest[2];
  777.         static const char * const skip_dest[] =
  778.           { "Page", "View", 0 };
  779.  
  780.         param_string_from_string(add_dest[0], "OpenAction");
  781.         param_string_from_string(add_dest[1], dest);
  782.         return pdfmark_save_edited_pairs(pdev, pairs, count, skip_dest,
  783.                          add_dest, 2,
  784.                          &pdev->catalog_string);
  785.       }
  786.     else
  787.       return pdfmark_save_pairs(pdev, pairs, count, &pdev->catalog_string);
  788. }
  789.