File: gdevpdfm.c

package info (click to toggle)
gs 5.10-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 14,960 kB
  • ctags: 25,299
  • sloc: ansic: 164,376; makefile: 3,020; cpp: 2,237; sh: 1,219; asm: 684; tcl: 434; perl: 56
file content (793 lines) | stat: -rw-r--r-- 23,115 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/* Copyright (C) 1996, 1997 Aladdin Enterprises.  All rights reserved.
  
  This file is part of GNU Ghostscript.
  
  GNU Ghostscript is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  anyone for the consequences of using it or for whether it serves any
  particular purpose or works at all, unless he says so in writing.  Refer to
  the GNU General Public License for full details.
  
  Everyone is granted permission to copy, modify and redistribute GNU
  Ghostscript, but only under the conditions described in the GNU General
  Public License.  A copy of this license is supposed to have been given to
  you along with GNU Ghostscript so you can know your rights and
  responsibilities.  It should be in a file named COPYING.  Among other
  things, the copyright notice and this notice must be preserved on all
  copies.
  
  Aladdin Enterprises is not affiliated with the Free Software Foundation or
  the GNU Project.  GNU Ghostscript, as distributed by Aladdin Enterprises,
  does not depend on any other GNU software.
*/

/* gdevpdfm.c */
/* pdfmark processing for PDF-writing driver */
#include "memory_.h"
#include "string_.h"
#include "gx.h"
#include "gserrors.h"
#include "gsutil.h"		/* for bytes_compare */
#include "gdevpdfx.h"
#include "scanchar.h"

/* GC descriptors */
private_st_pdf_article();
private_st_pdf_named_dest();

/*
 * The pdfmark pseudo-parameter indicates the occurrence of a pdfmark
 * operator in the input file.  Its "value" is the arguments of the operator,
 * passed through essentially unchanged:
 *	(key, value)*, CTM, type
 */

/*
 * Define the pdfmark types we know about.
 */
#define pdfmark_proc(proc)\
  int proc(P4(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,\
	      const gs_matrix *pctm))
typedef struct pdfmark_name_s {
  const char *mname;
  pdfmark_proc((*proc));
} pdfmark_name;
private pdfmark_proc(pdfmark_ANN);
private pdfmark_proc(pdfmark_LNK);
private pdfmark_proc(pdfmark_OUT);
private pdfmark_proc(pdfmark_ARTICLE);
private pdfmark_proc(pdfmark_DEST);
private pdfmark_proc(pdfmark_PS);
private pdfmark_proc(pdfmark_PAGES);
private pdfmark_proc(pdfmark_PAGE);
private pdfmark_proc(pdfmark_DOCINFO);
private pdfmark_proc(pdfmark_DOCVIEW);
private const pdfmark_name mark_names[] = {
  { "ANN", pdfmark_ANN },
  { "LNK", pdfmark_LNK },
  { "OUT", pdfmark_OUT },
  { "ARTICLE", pdfmark_ARTICLE },
  { "DEST", pdfmark_DEST },
  { "PS", pdfmark_PS },
  { "PAGES", pdfmark_PAGES },
  { "PAGE", pdfmark_PAGE },
  { "DOCINFO", pdfmark_DOCINFO },
  { "DOCVIEW", pdfmark_DOCVIEW },
  { 0, 0 }
};

/* ---------------- Public entry points ---------------- */

/* Compare a C string and a gs_param_string. */
bool
pdf_key_eq(const gs_param_string *pcs, const char *str)
{	return (strlen(str) == pcs->size &&
		!strncmp(str, (const char *)pcs->data, pcs->size));
}

/* Process a pdfmark. */
int
pdfmark_process(gx_device_pdf *pdev, const gs_param_string_array *pma)
{	const gs_param_string *data = pma->data;
	uint size = pma->size;
	const gs_param_string *pts = &data[size - 1];
	gs_matrix ctm;
	int i;

	if ( (size & 1) || size < 2 ||
	     sscanf((const char *)pts[-1].data, "[%g %g %g %g %g %g]",
		    &ctm.xx, &ctm.xy, &ctm.yx, &ctm.yy, &ctm.tx, &ctm.ty) != 6
	   )
	  return_error(gs_error_rangecheck);
	/*
	 * Our coordinate system is scaled so that user space is always
	 * default user space.  Adjust the CTM to match this.
	 */
	{ double xscale = 72.0 / pdev->HWResolution[0],
	    yscale = 72.0 / pdev->HWResolution[1];

	  ctm.xx *= xscale, ctm.xy *= yscale;
	  ctm.yx *= xscale, ctm.yy *= yscale;
	  ctm.tx *= xscale, ctm.ty *= yscale;
	}
	for ( i = 0; mark_names[i].mname != 0; ++i )
	  if ( pdf_key_eq(pts, mark_names[i].mname) )
	    return (*mark_names[i].proc)(pdev, data, size - 2, &ctm);
	return 0;
}

/* ---------------- Utilities ---------------- */

/* Find a key in a dictionary. */
private bool
pdfmark_find_key(const char *key, const gs_param_string *pairs, uint count,
  gs_param_string *pstr)
{	uint i;
	for ( i = 0; i < count; i += 2 )
	  if ( pdf_key_eq(&pairs[i], key) )
	    { *pstr = pairs[i + 1];
	      return true;
	    }
	pstr->data = 0;
	pstr->size = 0;
	return false;
}

/* Scan an integer out of a parameter string. */
private int
pdfmark_scan_int(const gs_param_string *pstr, int *pvalue)
{
#define max_int_str 20
	uint size = pstr->size;
	char str[max_int_str + 1];

	if ( size > max_int_str )
	  return_error(gs_error_limitcheck);
	memcpy(str, pstr->data, size);
	str[size] = 0;
	return(sscanf(str, "%d", pvalue) == 1 ? 0 :
	       gs_note_error(gs_error_rangecheck));
#undef max_int_str
}

/*
 * Get the page number for a page referenced by number or as /Next or /Prev.
 * The result may be 0 if the page number is 0 or invalid.
 */
private int
pdfmark_page_number(gx_device_pdf *pdev, const gs_param_string *pnstr)
{	int page = pdev->next_page + 1;

	if ( pnstr->data == 0 )
	  ;
	else if ( pdf_key_eq(pnstr, "/Next") )
	  ++page;
	else if ( pdf_key_eq(pnstr, "/Prev") )
	  --page;
	else if ( pdfmark_scan_int(pnstr, &page) < 0 )
	  page = 0;
	return page;
}

/* Construct a destination string specified by /Page and/or /View. */
/* Return 0 if none (but still fill in a default), 1 if present, */
/* <0 if error. */
private int
pdfmark_make_dest(char dstr[max_dest_string], gx_device_pdf *pdev,
  const gs_param_string *pairs, uint count)
{	gs_param_string page_string, view_string;
	bool present =
	  pdfmark_find_key("Page", pairs, count, &page_string) |
	    pdfmark_find_key("View", pairs, count, &view_string);
	int page = pdfmark_page_number(pdev, &page_string);
	gs_param_string action;
	int len;

	if ( view_string.size == 0 )
	  param_string_from_string(view_string, "[/XYZ 0 0 1]");
	if ( page == 0 )
	  strcpy(dstr, "[null ");
	else if ( pdfmark_find_key("Action", pairs, count, &action) &&
		  pdf_key_eq(&action, "/GoToR")
		)
	  sprintf(dstr, "[%d ", page - 1);
	else
	  sprintf(dstr, "[%ld 0 R ", pdf_page_id(pdev, page));
	len = strlen(dstr);
	if ( len + view_string.size > max_dest_string )
	  return_error(gs_error_limitcheck);
	if ( view_string.data[0] != '[' ||
	     view_string.data[view_string.size - 1] != ']'
	   )
	  return_error(gs_error_rangecheck);
	memcpy(dstr + len, view_string.data + 1, view_string.size - 1);
	dstr[len + view_string.size - 1] = 0;
	return present;
}

/* Write pairs for a dictionary. */
private void
pdfmark_write_pair(stream *s, const gs_param_string *pair)
{	pputc(s, '/');
	pwrite(s, pair->data, pair->size);
	pputc(s, ' ');
	pwrite(s, pair[1].data, pair[1].size);
	pputc(s, '\n');
}

/* Scan a Rect value. */
private int
pdfmark_scan_rect(gs_rect *prect, const gs_param_string *str,
  const gs_matrix *pctm)
{	uint size = str->size;
	double v[4];
#define max_rect_string_size 100
	char chars[max_rect_string_size + 3];
	int end_check;

	if ( str->size > max_rect_string_size )
	  return_error(gs_error_limitcheck);
	memcpy(chars, str->data, size);
	strcpy(chars + size, " 0");
	if ( sscanf(chars, "[%lg %lg %lg %lg]%d",
		    &v[0], &v[1], &v[2], &v[3], &end_check) != 5
	   )
	  return_error(gs_error_rangecheck);
	gs_point_transform(v[0], v[1], pctm, &prect->p);
	gs_point_transform(v[2], v[3], pctm, &prect->q);
	return 0;
}

/* Write a Rect value. */
private void
pdfmark_write_rect(stream *s, const char *key, const gs_rect *prect)
{	pprints1(s, "/%s ", key);
	pprintg4(s, "[%g %g %g %g]\n",
		 prect->p.x, prect->p.y, prect->q.x, prect->q.y);
}

/* Copy an annotation dictionary. */
private int
pdfmark_annot(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  const gs_matrix *pctm, const char *subtype)
{	pdf_resource *pres;
	int code = pdf_begin_aside(pdev, &pdev->annots, NULL, &pres);
	stream *s = pdev->strm;
	bool subtype_present = false;
	bool add_dest = false;
	bool dest_present = false;
	uint i;

	if ( code < 0 )
	  return code;
	pres->rid = pdev->next_page;
	pputs(s, "<< /Type /Annot\n");
	for ( i = 0; i < count; i += 2 )
	  { const gs_param_string *pair = &pairs[i];
	    long src_pg;

	    if ( pdf_key_eq(pair, "SrcPg") &&
		 sscanf((const char *)pair[1].data, "%ld", &src_pg) == 1
	       )
	      pres->rid = src_pg - 1;
	    else if ( pdf_key_eq(pair, "Page") || pdf_key_eq(pair, "View") )
	      add_dest = true;
	    /*
	     * For some unfathomable reason, PDF requires /A instead of
	     * /Action, and /S instead of /Subtype in the Action dictionary.
	     * Handle this here.
	     */
	    else if ( pdf_key_eq(pair, "Action") )
	      { const byte *astr = pair[1].data;
	        const uint asize = pair[1].size;
		uint i;

		pputs(s, "/A ");
		/* Search for the next occurrence of /Subtype. */
		for ( i = 0; i < asize; ++i )
		  if ( asize - i > 8 && !memcmp(astr + i, "/Subtype", 8) &&
		       scan_char_decoder[astr[i + 8]] > ctype_name
		     )
		    { pputs(s, "/S");
		      i += 7;
		    }
		  else
		    pputc(s, astr[i]);
	        pputc(s, '\n');
	      }
	    else if ( pdf_key_eq(pair, "Rect") )
	      { gs_rect rect;

	        code = pdfmark_scan_rect(&rect, pair + 1, pctm);
		if ( code < 0 )
		  return code;
		pdfmark_write_rect(s, "Rect", &rect);
	      }
	    else
	      { pdfmark_write_pair(s, pair);
	        if ( pdf_key_eq(pair, "Dest") )
		  dest_present = true;
		else if ( pdf_key_eq(pair, "Subtype") )
		  subtype_present = true;
	      }
	  }
	if ( add_dest && !dest_present )
	  { char dest[max_dest_string];
	    int code = pdfmark_make_dest(dest, pdev, pairs, count);
	    if ( code >= 0 )
	      pprints1(s, "/Dest %s\n", dest);
	  }
	if ( !subtype_present )
	  pprints1(s, "/Subtype /%s ", subtype);
	pputs(s, ">>\n");
	pdf_end_aside(pdev);
	return 0;
}

/* ANN pdfmark */
private int
pdfmark_ANN(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  const gs_matrix *pctm)
{	return pdfmark_annot(pdev, pairs, count, pctm, "Text");
}

/* LNK pdfmark (obsolescent) */
private int
pdfmark_LNK(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  const gs_matrix *pctm)
{	return pdfmark_annot(pdev, pairs, count, pctm, "Link");
}

/* Save pairs in a string. */
private bool
pdf_key_member(const gs_param_string *pcs, const char * const keys[])
{	const char * const *pkey;
	for ( pkey = keys; *pkey != 0; ++pkey )
	  if ( pdf_key_eq(pcs, *pkey) )
	    return true;
	return false;
}
#define pdfmark_size_pair(pair)\
  ((pair)[0].size + (pair)[1].size + 3)
private byte *
pdfmark_save_pair(byte *next, const gs_param_string *pair)
{	uint len;

	*next++ = '/';
	memcpy(next, pair[0].data, len = pair[0].size);
	next += len;
	*next++ = ' ';
	memcpy(next, pair[1].data, len = pair[1].size);
	next += len;
	*next++ = '\n';
	return next;
}
private int
pdfmark_save_edited_pairs(const gx_device_pdf *pdev,
  const gs_param_string *pairs, uint count, const char * const skip_keys[],
  const gs_param_string *add_pairs, uint add_count, gs_string *pstr)
{	uint i, size;
	byte *data;
	byte *next;

	for ( i = 0, size = 0; i < count; i += 2 )
	  if ( !pdf_key_member(&pairs[i], skip_keys) )
	    size += pdfmark_size_pair(&pairs[i]);
	for ( i = 0; i < add_count; i += 2 )
	  size += pdfmark_size_pair(&add_pairs[i]);
	if ( pstr->data == 0 )
	  data = gs_alloc_string(pdev->pdf_memory, size, "pdfmark_save_pairs");
	else
	  data = gs_resize_string(pdev->pdf_memory, pstr->data, pstr->size,
				  size, "pdfmark_save_pairs");
	if ( data == 0 )
	  return_error(gs_error_VMerror);
	next = data;
	for ( i = 0; i < count; i += 2 )
	  if ( !pdf_key_member(&pairs[i], skip_keys) )
	    next = pdfmark_save_pair(next, &pairs[i]);
	for ( i = 0; i < add_count; i += 2 )
	  next = pdfmark_save_pair(next, &add_pairs[i]);
	pstr->data = data;
	pstr->size = size;
	return 0;
}
static const char * const no_skip_pairs[] = { 0 };
#define pdfmark_save_pairs(pdev, pairs, count, pstr)\
  pdfmark_save_edited_pairs(pdev, pairs, count, no_skip_pairs, NULL, 0, pstr)

/* Write out one node of the outline tree. */
private int
pdfmark_write_outline(gx_device_pdf *pdev, pdf_outline_node *pnode,
  long next_id)
{	stream *s;

	pdf_open_separate(pdev, pnode->id);
	s = pdev->strm;
	pputs(s, "<< ");
	pdf_write_saved_string(pdev, &pnode->action_string);
	if ( pnode->count )
	  pprintd1(s, "/Count %d ", pnode->count);
	pprintld1(s, "/Parent %ld 0 R\n", pnode->parent_id);
	if ( pnode->prev_id )
	  pprintld1(s, "/Prev %ld 0 R\n", pnode->prev_id);
	if ( next_id )
	  pprintld1(s, "/Next %ld 0 R\n", next_id);
	if ( pnode->first_id )
	  pprintld2(s, "/First %ld 0 R /Last %ld 0 R\n",
		    pnode->first_id, pnode->last_id);
	pputs(s, ">>\n");
	pdf_end_separate(pdev);
	return 0;
}

/* Adjust the parent's count when writing an outline node. */
private void
pdfmark_adjust_parent_count(pdf_outline_level *plevel)
{	pdf_outline_level *parent = plevel - 1;
	int count = plevel->last.count;

	if ( count > 0 )
	  { if ( parent->last.count < 0 )
	      parent->last.count -= count;
	    else
	      parent->last.count += count;
	  }
}

/* Close the current level of the outline tree. */
int
pdfmark_close_outline(gx_device_pdf *pdev)
{	int depth = pdev->outline_depth;
	pdf_outline_level *plevel = &pdev->outline_levels[depth];
	int code;

	code = pdfmark_write_outline(pdev, &plevel->last, 0);
	if ( code < 0 )
	  return code;
	if ( depth > 0 )
	  { plevel[-1].last.last_id = plevel->last.id;
	    pdfmark_adjust_parent_count(plevel);
	    --plevel;
	    if ( plevel->last.count < 0 )
	      pdev->closed_outline_depth--;
	    pdev->outline_depth--;
	  }
	return 0;
}

/* OUT pdfmark */
private int
pdfmark_OUT(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  const gs_matrix *pctm)
{	int depth = pdev->outline_depth;
	pdf_outline_level *plevel = &pdev->outline_levels[depth];
	int sub_count = 0;
	uint i;
	pdf_outline_node node;
	int code;

	for ( i = 0; i < count; i += 2 )
	  { const gs_param_string *pair = &pairs[i];

	    if ( pdf_key_eq(pair, "Count") )
	      pdfmark_scan_int(pair + 1, &sub_count);
	  }
	if ( sub_count != 0 && depth == max_outline_depth - 1 )
	  return_error(gs_error_limitcheck);
	node.action_string.data = 0;
	{ static const char * const skip_out[] =
	    { "Page", "View", "Count", 0 };
	  char dest[max_dest_string];

	  if ( pdfmark_make_dest(dest, pdev, pairs, count) ) {
	    gs_param_string add_dest[2];

	    param_string_from_string(add_dest[0], "Dest");
	    param_string_from_string(add_dest[1], dest);
	    code = pdfmark_save_edited_pairs(pdev, pairs, count, skip_out,
					     add_dest, 2,
					     &node.action_string);
	  } else {
	    code = pdfmark_save_edited_pairs(pdev, pairs, count, skip_out + 2,
					     NULL, 0, &node.action_string);
	  }
	  if ( code < 0 )
	    return code;
	}
	if ( pdev->outlines_id == 0 )
	  pdev->outlines_id = pdf_obj_ref(pdev);
	node.id = pdf_obj_ref(pdev);
	node.parent_id =
	  (depth == 0 ? pdev->outlines_id : plevel[-1].last.id);
	node.prev_id = plevel->last.id;
	node.first_id = node.last_id = 0;
	node.count = sub_count;
	/* Add this node to the outline at the current level. */
	if ( plevel->first.id == 0 )
	  { /* First node at this level. */
	    if ( depth > 0 )
	      plevel[-1].last.first_id = node.id;
	    node.prev_id = 0;
	    plevel->first = node;
	  }
	else
	  { /* Write out the previous node. */
	    if ( depth > 0 )
	      pdfmark_adjust_parent_count(plevel);
	    pdfmark_write_outline(pdev, &plevel->last, node.id);
	  }
	plevel->last = node;
	plevel->left--;
	if ( !pdev->closed_outline_depth )
	  pdev->outlines_open++;
	/* If this node has sub-nodes, descend one level. */
	if ( sub_count != 0 )
	  { pdev->outline_depth++;
	    ++plevel;
	    plevel->left = (sub_count > 0 ? sub_count : -sub_count);
	    plevel->first.id = 0;
	    if ( sub_count < 0 )
	      pdev->closed_outline_depth++;
	  }
	else
	  { while ( (depth = pdev->outline_depth) > 0 &&
		    pdev->outline_levels[depth].left == 0
		  )
	      pdfmark_close_outline(pdev);
	  }
	return 0;
}

/* Write an article bead. */
int
pdfmark_write_article(gx_device_pdf *pdev, const pdf_bead *pbead)
{	stream *s;

	pdf_open_separate(pdev, pbead->id);
	s = pdev->strm;
	pprintld3(s,
		  "<<\n/T %ld 0 R\n/V %ld 0 R\n/N %ld 0 R\n",
		  pbead->article_id, pbead->prev_id, pbead->next_id);
	pprints1(s, "/Dest %s\n", pbead->dest);
	pdfmark_write_rect(s, "R", &pbead->rect);
	pputs(s, ">>\n");
	return pdf_end_separate(pdev);
}

/* ARTICLE pdfmark */
private int
pdfmark_ARTICLE(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  const gs_matrix *pctm)
{	gs_param_string title;
	gs_param_string rectstr;
	gs_rect rect;
	long bead_id;
	pdf_article *part;
	int code;

	if ( !pdfmark_find_key("Title", pairs, count, &title) ||
	     !pdfmark_find_key("Rect", pairs, count, &rectstr)
	   )
	  return_error(gs_error_rangecheck);
	if ( (code = pdfmark_scan_rect(&rect, &rectstr, pctm)) < 0 )
	  return code;
	/****** Should save other keys for Info dictionary ******/

	/* Find the article with this title, or create one. */
	bead_id = pdf_obj_ref(pdev);
	for ( part = pdev->articles; part != 0; part = part->next )
	  if ( !bytes_compare(part->title.data, part->title.size,
			      title.data, title.size) )
	    break;
	if ( part == 0 )
	  { /* Create the article. */
	    byte *str;

	    part = gs_alloc_struct(pdev->pdf_memory, pdf_article,
				   &st_pdf_article, "pdfmark_ARTICLE");
	    str = gs_alloc_string(pdev->pdf_memory, title.size,
				  "article title");
	    if ( part == 0 || str == 0 )
	      return_error(gs_error_VMerror);
	    part->next = pdev->articles;
	    pdev->articles = part;
	    memcpy(str, title.data, title.size);
	    part->title.data = str;
	    part->title.size = title.size;
	    part->id = pdf_begin_separate(pdev);
	    part->first.id = part->last.id = 0;
	    pprintld1(pdev->strm, "<</F %ld 0 R>>\n", bead_id);
	    pdf_end_separate(pdev);
	  }

	/* Add the bead to the article. */
	/* This is similar to what we do for outline nodes. */
	if ( part->last.id == 0 )
	  { part->first.next_id = bead_id;
	    part->last.id = part->first.id;
	  }
	else
	  { part->last.next_id = bead_id;
	    pdfmark_write_article(pdev, &part->last);
	  }
	part->last.prev_id = part->last.id;
	part->last.id = bead_id;
	part->last.article_id = part->id;
	part->last.next_id = 0;
	part->last.rect = rect;
	pdfmark_make_dest(part->last.dest, pdev, pairs, count);
	if ( part->first.id == 0 )
	  { /* This is the first bead of the article. */
	    part->first = part->last;
	    part->last.id = 0;
	  }

	return 0;
}

/* DEST pdfmark */
private int
pdfmark_DEST(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  const gs_matrix *pctm)
{	char dest[max_dest_string];
	gs_param_string key;
	pdf_named_dest *pnd;
	byte *str;

	if ( !pdfmark_find_key("Dest", pairs, count, &key) ||
	     !pdfmark_make_dest(dest, pdev, pairs, count)
	   )
	  return_error(gs_error_rangecheck);
	pnd = gs_alloc_struct(pdev->pdf_memory, pdf_named_dest,
			      &st_pdf_named_dest, "pdfmark_DEST");
	str = gs_alloc_string(pdev->pdf_memory, key.size,
			      "named_dest key");
	if ( pnd == 0 || str == 0 )
	  return_error(gs_error_VMerror);
	pnd->next = pdev->named_dests;
	memcpy(str, key.data, key.size);
	pnd->key.data = str;
	pnd->key.size = key.size;
	strcpy(pnd->dest, dest);
	pdev->named_dests = pnd;
	return 0;
}

/* Write the contents of pass-through code. */
/* We are inside the stream dictionary, in a 'separate' object. */
private int
pdfmark_write_ps(gx_device_pdf *pdev, const gs_param_string *psource)
{	stream *s = pdev->strm;
	long length_id = pdf_obj_ref(pdev);
	long start_pos, length;

	pprintld1(s, " /Length %ld 0 R >> stream\n", length_id);
	start_pos = pdf_stell(pdev);
	if ( psource->size < 2 || psource->data[0] != '(' ||
	     psource->data[psource->size - 1] != ')'
	   )
	  lprintf1("bad PS passthrough: %s\n", psource->data);
	else
	  { /****** SHOULD REMOVE ESCAPES ******/
	    pwrite(s, psource->data + 1, psource->size - 2);
	  }
	pputs(s, "\n");
	length = pdf_stell(pdev) - start_pos;
	pputs(s, "endstream\n");
	pdf_end_separate(pdev);
	pdf_open_separate(pdev, length_id);
	pprintld1(s, "%ld\n", length);
	pdf_end_separate(pdev);
	return 0;
}

/* PS pdfmark */
private int
pdfmark_PS(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  const gs_matrix *pctm)
{	stream *s;
	gs_param_string source;
	gs_param_string level1;

	if ( !pdfmark_find_key("DataSource", pairs, count, &source) )
	  return_error(gs_error_rangecheck);
	pdfmark_find_key("Level1", pairs, count, &level1);
	if ( level1.data == 0 && source.size <= 100 &&
	     pdev->CompatibilityLevel >= 1.2
	   )
	  { /* Insert the PostScript code in-line */
	    int code = pdf_open_contents(pdev, pdf_in_stream);

	    if ( code < 0 )
	      return code;
	    s = pdev->strm;
	    pwrite(s, source.data, source.size);
	    pputs(s, " PS\n");
	  }
	else
	  { /* Put the PostScript code in a resource. */
	    pdf_resource *pres;
	    int code = pdf_begin_resource(pdev, resourceXObject, gs_no_id,
					  &pres);

	    if ( code < 0 )
	      return code;
	    s = pdev->strm;
	    pputs(s, " /Subtype /PS");
	    if ( level1.data != 0 )
	      { long level1_id = pdf_obj_ref(pdev);

		pprintld1(s, " /Level1 %ld 0 R", level1_id);
		pdfmark_write_ps(pdev, &source);
		pdf_open_separate(pdev, level1_id);
		pputs(pdev->strm, "<<");
		pdfmark_write_ps(pdev, &level1);
	      }
	    else
	      pdfmark_write_ps(pdev, &source);
	    code = pdf_open_contents(pdev, pdf_in_stream);
	    if ( code < 0 )
	      return code;
	    pprintld1(pdev->strm, "/R%ld Do\n", pres->id);
	  }
	return 0;
}

/* PAGES pdfmark */
private int
pdfmark_PAGES(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  const gs_matrix *pctm)
{	return pdfmark_save_pairs(pdev, pairs, count, &pdev->pages_string);
}

/* PAGE pdfmark */
private int
pdfmark_PAGE(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  const gs_matrix *pctm)
{	return pdfmark_save_pairs(pdev, pairs, count, &pdev->page_string);
}

/* DOCINFO pdfmark */
private int
pdfmark_DOCINFO(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  const gs_matrix *pctm)
{	long info_id = pdf_begin_separate(pdev);
	stream *s = pdev->strm;
	uint i;

	if ( info_id < 0 )
	  return info_id;
	pputs(s, "<<\n");
	for ( i = 0; i < count; i += 2 )
	  if ( !pdf_key_eq(&pairs[i], "CreationDate") &&
	       !pdf_key_eq(&pairs[i], "Producer")
	     )
	    pdfmark_write_pair(s, &pairs[i]);
	pdf_write_default_info(pdev);
	pputs(s, ">>\n");
	pdf_end_separate(pdev);
	pdev->info_id = info_id;
	return 0;
}

/* DOCVIEW pdfmark */
private int
pdfmark_DOCVIEW(gx_device_pdf *pdev, const gs_param_string *pairs, uint count,
  const gs_matrix *pctm)
{	char dest[max_dest_string];

	if ( pdfmark_make_dest(dest, pdev, pairs, count) )
	  { gs_param_string add_dest[2];
	    static const char * const skip_dest[] =
	      { "Page", "View", 0 };

	    param_string_from_string(add_dest[0], "OpenAction");
	    param_string_from_string(add_dest[1], dest);
	    return pdfmark_save_edited_pairs(pdev, pairs, count, skip_dest,
					     add_dest, 2,
					     &pdev->catalog_string);
	  }
	else
	  return pdfmark_save_pairs(pdev, pairs, count, &pdev->catalog_string);
}