xref: /plan9/sys/src/cmd/gs/src/gdevpdfx.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1996, 2000, 2001 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gdevpdfx.h,v 1.138 2005/10/18 07:58:42 leonardo Exp $ */
18 /* Internal definitions for PDF-writing driver. */
19 
20 #ifndef gdevpdfx_INCLUDED
21 #  define gdevpdfx_INCLUDED
22 
23 #include "gsparam.h"
24 #include "gsuid.h"
25 #include "gxdevice.h"
26 #include "gxfont.h"
27 #include "gxline.h"
28 #include "stream.h"
29 #include "spprint.h"
30 #include "gdevpsdf.h"
31 #include "gxdevmem.h"
32 
33 #define FINE_GLYPH_USAGE 1 /* Old code = 0, new code = 1 */
34 
35 #ifndef stream_arcfour_state_DEFINED
36 #define stream_arcfour_state_DEFINED
37 typedef struct stream_arcfour_state_s stream_arcfour_state;
38 #endif
39 
40 /* ---------------- Acrobat limitations ---------------- */
41 
42 /*
43  * The PDF reference manual, 2nd ed., claims that the limit for coordinates
44  * is +/- 32767. However, testing indicates that Acrobat Reader 4 for
45  * Windows and Linux fail with coordinates outside +/- 16383. Hence, we
46  * limit coordinates to 16k, with a little slop.
47  */
48 #define MAX_USER_COORD 16300
49 
50 /* ---------------- Statically allocated sizes ---------------- */
51 /* These should really be dynamic.... */
52 
53 /* Define the maximum depth of an outline tree. */
54 /* Note that there is no limit on the breadth of the tree. */
55 #define MAX_OUTLINE_DEPTH 32
56 
57 /* Define the maximum size of a destination array string. */
58 #define MAX_DEST_STRING 80
59 
60 /* ================ Types and structures ================ */
61 
62 /* Define the possible contexts for the output stream. */
63 typedef enum {
64     PDF_IN_NONE,
65     PDF_IN_STREAM,
66     PDF_IN_TEXT,
67     PDF_IN_STRING
68 } pdf_context_t;
69 
70 /* ---------------- Cos objects ---------------- */
71 
72 /*
73  * These are abstract types for cos objects.  The concrete types are in
74  * gdevpdfo.h.
75  */
76 typedef struct cos_object_s cos_object_t;
77 typedef struct cos_stream_s cos_stream_t;
78 typedef struct cos_dict_s cos_dict_t;
79 typedef struct cos_array_s cos_array_t;
80 typedef struct cos_value_s cos_value_t;
81 typedef struct cos_object_procs_s cos_object_procs_t;
82 typedef const cos_object_procs_t *cos_type_t;
83 #define cos_types_DEFINED
84 
85 #ifndef pdf_text_state_DEFINED
86 #  define pdf_text_state_DEFINED
87 typedef struct pdf_text_state_s pdf_text_state_t;
88 #endif
89 
90 #ifndef pdf_char_glyph_pairs_DEFINED
91 #  define pdf_char_glyph_pairs_DEFINED
92 typedef struct pdf_char_glyph_pairs_s pdf_char_glyph_pairs_t;
93 #endif
94 
95 
96 /* ---------------- Resources ---------------- */
97 
98 typedef enum {
99     /*
100      * Standard PDF resources.  Font must be last, because resources
101      * up to but not including Font are written page-by-page.
102      */
103     resourceColorSpace,
104     resourceExtGState,
105     resourcePattern,
106     resourceShading,
107     resourceXObject,
108     resourceOther, /* Anything else that needs to be stored for a time. */
109     resourceFont,
110     /*
111      * Internally used (pseudo-)resources.
112      */
113     resourceCharProc,
114     resourceCIDFont,
115     resourceCMap,
116     resourceFontDescriptor,
117     resourceGroup,
118     resourceSoftMaskDict,
119     resourceFunction,
120     resourcePage,
121     NUM_RESOURCE_TYPES
122 } pdf_resource_type_t;
123 
124 #define PDF_RESOURCE_TYPE_NAMES\
125   "/ColorSpace", "/ExtGState", "/Pattern", "/Shading", "/XObject", 0, "/Font",\
126   0, "/Font", "/CMap", "/FontDescriptor", "/Group", "/Mask", 0, 0
127 #define PDF_RESOURCE_TYPE_STRUCTS\
128   &st_pdf_color_space,		/* gdevpdfg.h / gdevpdfc.c */\
129   &st_pdf_resource,		/* see below */\
130   &st_pdf_pattern,\
131   &st_pdf_resource,\
132   &st_pdf_x_object,		/* see below */\
133   &st_pdf_resource,\
134   &st_pdf_font_resource,	/* gdevpdff.h / gdevpdff.c */\
135   &st_pdf_char_proc,		/* gdevpdff.h / gdevpdff.c */\
136   &st_pdf_font_resource,	/* gdevpdff.h / gdevpdff.c */\
137   &st_pdf_resource,\
138   &st_pdf_font_descriptor,	/* gdevpdff.h / gdevpdff.c */\
139   &st_pdf_resource,\
140   &st_pdf_resource,\
141   &st_pdf_resource,\
142   &st_pdf_resource
143 
144 /*
145  * rname is currently R<id> for all resources other than synthesized fonts;
146  * for synthesized fonts, rname is A, B, ....  The string is null-terminated.
147  */
148 
149 #define pdf_resource_common(typ)\
150     typ *next;			/* next resource of this type */\
151     pdf_resource_t *prev;	/* previously allocated resource */\
152     gs_id rid;			/* optional ID key */\
153     bool named;\
154     bool global;                /* ps2write only */\
155     char rname[1/*R*/ + (sizeof(long) * 8 / 3 + 1) + 1/*\0*/];\
156     ulong where_used;		/* 1 bit per level of content stream */\
157     cos_object_t *object
158 typedef struct pdf_resource_s pdf_resource_t;
159 struct pdf_resource_s {
160     pdf_resource_common(pdf_resource_t);
161 };
162 
163 /* The descriptor is public for subclassing. */
164 extern_st(st_pdf_resource);
165 #define public_st_pdf_resource()  /* in gdevpdfu.c */\
166   gs_public_st_ptrs3(st_pdf_resource, pdf_resource_t, "pdf_resource_t",\
167     pdf_resource_enum_ptrs, pdf_resource_reloc_ptrs, next, prev, object)
168 
169 /*
170  * We define XObject resources here because they are used for Image,
171  * Form, and PS XObjects, which are implemented in different files.
172  */
173 typedef struct pdf_x_object_s pdf_x_object_t;
174 struct pdf_x_object_s {
175     pdf_resource_common(pdf_x_object_t);
176     int width, height;		/* specified width and height for images */
177     int data_height;		/* actual data height for images */
178 };
179 #define private_st_pdf_x_object()  /* in gdevpdfu.c */\
180   gs_private_st_suffix_add0(st_pdf_x_object, pdf_x_object_t,\
181     "pdf_x_object_t", pdf_x_object_enum_ptrs, pdf_x_object_reloc_ptrs,\
182     st_pdf_resource)
183 
184 /* Define the mask for which procsets have been used on a page. */
185 typedef enum {
186     NoMarks = 0,
187     ImageB = 1,
188     ImageC = 2,
189     ImageI = 4,
190     Text = 8
191 } pdf_procset_t;
192 
193 /* ------ Fonts ------ */
194 
195 /* Define abstract types. */
196 typedef struct pdf_char_proc_s pdf_char_proc_t;	/* gdevpdff.h */
197 typedef struct pdf_font_s pdf_font_t;  /* gdevpdff.h */
198 typedef struct pdf_text_data_s pdf_text_data_t;  /* gdevpdft.h */
199 
200 /* ---------------- Other auxiliary structures ---------------- */
201 
202 /* Outline nodes and levels */
203 typedef struct pdf_outline_node_s {
204     long id, parent_id, prev_id, first_id, last_id;
205     int count;
206     cos_dict_t *action;
207 } pdf_outline_node_t;
208 typedef struct pdf_outline_level_s {
209     pdf_outline_node_t first;
210     pdf_outline_node_t last;
211     int left;
212 } pdf_outline_level_t;
213 /*
214  * The GC descriptor is implicit, since outline levels occur only in an
215  * embedded array in the gx_device_pdf structure.
216  */
217 
218 /* Articles */
219 typedef struct pdf_bead_s {
220     long id, article_id, prev_id, next_id, page_id;
221     gs_rect rect;
222 } pdf_bead_t;
223 typedef struct pdf_article_s pdf_article_t;
224 struct pdf_article_s {
225     pdf_article_t *next;
226     cos_dict_t *contents;
227     pdf_bead_t first;
228     pdf_bead_t last;
229 };
230 
231 #define private_st_pdf_article()\
232   gs_private_st_ptrs2(st_pdf_article, pdf_article_t,\
233     "pdf_article_t", pdf_article_enum_ptrs, pdf_article_reloc_ptrs,\
234     next, contents)
235 
236 /* ---------------- The device structure ---------------- */
237 
238 /* Resource lists */
239 #define NUM_RESOURCE_CHAINS 16
240 typedef struct pdf_resource_list_s {
241     pdf_resource_t *chains[NUM_RESOURCE_CHAINS];
242 } pdf_resource_list_t;
243 
244 /* Define the hash function for gs_ids. */
245 #define gs_id_hash(rid) ((rid) + ((rid) / NUM_RESOURCE_CHAINS))
246 /* Define the accessor for the proper hash chain. */
247 #define PDF_RESOURCE_CHAIN(pdev, type, rid)\
248   (&(pdev)->resources[type].chains[gs_id_hash(rid) % NUM_RESOURCE_CHAINS])
249 
250 /* Define the bookkeeping for an open stream. */
251 typedef struct pdf_stream_position_s {
252     long length_id;
253     long start_pos;
254 } pdf_stream_position_t;
255 
256 /*
257  * Define the structure for keeping track of text rotation.
258  * There is one for the current page (for AutoRotate /PageByPage)
259  * and one for the whole document (for AutoRotate /All).
260  */
261 typedef struct pdf_text_rotation_s {
262     long counts[5];		/* 0, 90, 180, 270, other */
263     int Rotate;			/* computed rotation, -1 means none */
264 } pdf_text_rotation_t;
265 #define pdf_text_rotation_angle_values 0, 90, 180, 270, -1
266 
267 /*
268  * Define document and page information derived from DSC comments.
269  */
270 typedef struct pdf_page_dsc_info_s {
271     int orientation;		/* -1 .. 3 */
272     int viewing_orientation;	/* -1 .. 3 */
273     gs_rect bounding_box;
274 } pdf_page_dsc_info_t;
275 
276 /*
277  * Define the stored information for a page.  Because pdfmarks may add
278  * information to any page anywhere in the document, we have to wait
279  * until the end to write the page dictionaries.
280  */
281 typedef struct pdf_page_s {
282     cos_dict_t *Page;
283     gs_point MediaBox;
284     pdf_procset_t procsets;
285     long contents_id;
286     long resource_ids[resourceFont + 1]; /* resources thru Font, see above */
287     long group_id;
288     cos_array_t *Annots;
289     pdf_text_rotation_t text_rotation;
290     pdf_page_dsc_info_t dsc_info;
291     bool NumCopies_set; /* ps2write only. */
292     int NumCopies;      /* ps2write only. */
293 } pdf_page_t;
294 #define private_st_pdf_page()	/* in gdevpdf.c */\
295   gs_private_st_ptrs2(st_pdf_page, pdf_page_t, "pdf_page_t",\
296     pdf_page_enum_ptrs, pdf_page_reloc_ptrs, Page, Annots)
297 
298 /*
299  * Define the structure for the temporary files used while writing.
300  * There are 4 of these, described below.
301  */
302 typedef struct pdf_temp_file_s {
303     char file_name[gp_file_name_sizeof];
304     FILE *file;
305     stream *strm;
306     byte *strm_buf;
307     stream *save_strm;		/* save pdev->strm while writing here */
308 } pdf_temp_file_t;
309 
310 #ifndef gx_device_pdf_DEFINED
311 #  define gx_device_pdf_DEFINED
312 typedef struct gx_device_pdf_s gx_device_pdf;
313 #endif
314 
315 /*
316  * Define the structure for PDF font cache element.
317  */
318 typedef struct pdf_font_cache_elem_s pdf_font_cache_elem_t;
319 struct pdf_font_cache_elem_s {
320     pdf_font_cache_elem_t *next;
321     gs_id font_id;
322     int num_chars;		/* safety purpose only */
323     int num_widths;		/* safety purpose only */
324     struct pdf_font_resource_s *pdfont;
325     byte *glyph_usage;
326     double *real_widths;	/* [count] (not used for Type 0) */
327     gx_device_pdf *pdev;	/* For pdf_remove_font_cache_elem */
328 };
329 
330 #define private_st_pdf_font_cache_elem()\
331     gs_private_st_ptrs5(st_pdf_font_cache_elem, pdf_font_cache_elem_t,\
332 	"pdf_font_cache_elem_t", pdf_font_cache_elem_enum,\
333 	pdf_font_cache_elem_reloc, next, pdfont,\
334 	glyph_usage, real_widths, pdev)
335 
336 /*
337  * pdf_viewer_state tracks the graphic state of a viewer,
338  * which would interpret the generated PDF file
339  * immediately when it is generated.
340  */
341 typedef struct pdf_viewer_state_s {
342     int transfer_not_identity;	/* bitmask */
343     gs_id transfer_ids[4];
344     float opacity_alpha; /* state.opacity.alpha */
345     float shape_alpha; /* state.shape.alpha */
346     gs_blend_mode_t blend_mode; /* state.blend_mode */
347     gs_id halftone_id;
348     gs_id black_generation_id;
349     gs_id undercolor_removal_id;
350     int overprint_mode;
351     float smoothness; /* state.smoothness */
352     float flatness;
353     bool text_knockout; /* state.text_knockout */
354     bool fill_overprint;
355     bool stroke_overprint;
356     bool stroke_adjust; /* state.stroke_adjust */
357     bool fill_used_process_color;
358     bool stroke_used_process_color;
359     gx_hl_saved_color saved_fill_color;
360     gx_hl_saved_color saved_stroke_color;
361     gx_line_params line_params;
362     float dash_pattern[max_dash];
363     gs_id soft_mask_id;
364 } pdf_viewer_state;
365 
366 /*
367  * Define a structure for saving context when entering
368  * a contents stream accumulation mode (charproc, Type 1 pattern).
369  */
370 typedef struct pdf_substream_save_s {
371     pdf_context_t	context;
372     pdf_text_state_t	*text_state;
373     gx_path		*clip_path;
374     gs_id		clip_path_id;
375     int			vgstack_bottom;
376     stream		*strm;
377     cos_dict_t		*substream_Resources;
378     pdf_procset_t	procsets;
379     bool		skip_colors;
380     pdf_resource_t      *font3;
381     pdf_resource_t	*accumulating_substream_resource;
382     bool		charproc_just_accumulated;
383     bool		accumulating_a_global_object;
384     pdf_resource_t      *pres_soft_mask_dict;
385     gs_const_string		objname;
386 } pdf_substream_save;
387 
388 #define private_st_pdf_substream_save()\
389     gs_private_st_strings1_ptrs7(st_pdf_substream_save, pdf_substream_save,\
390 	"pdf_substream_save", pdf_substream_save_enum,\
391 	pdf_substream_save_reloc, objname, text_state, clip_path, strm, \
392 	substream_Resources, font3, accumulating_substream_resource, pres_soft_mask_dict)
393 #define private_st_pdf_substream_save_element()\
394   gs_private_st_element(st_pdf_substream_save_element, pdf_substream_save,\
395     "pdf_substream_save[]", pdf_substream_save_elt_enum_ptrs,\
396     pdf_substream_save_elt_reloc_ptrs, st_pdf_substream_save)
397 
398 typedef enum {
399     pdf_compress_none,
400     pdf_compress_LZW,	/* not currently used, thanks to Unisys */
401     pdf_compress_Flate
402 } pdf_compression_type;
403 
404 
405 /* Define the device structure. */
406 struct gx_device_pdf_s {
407     gx_device_psdf_common;
408     /* PDF-specific distiller parameters */
409     double CompatibilityLevel;
410     int EndPage;
411     int StartPage;
412     bool Optimize;
413     bool ParseDSCCommentsForDocInfo;
414     bool ParseDSCComments;
415     bool EmitDSCWarnings;
416     bool CreateJobTicket;
417     bool PreserveEPSInfo;
418     bool AutoPositionEPSFiles;
419     bool PreserveCopyPage;
420     bool UsePrologue;
421     int OffOptimizations;
422     /* End of distiller parameters */
423     /* Other parameters */
424     bool ReAssignCharacters;
425     bool ReEncodeCharacters;
426     long FirstObjectNumber;
427     bool CompressFonts;
428     bool PrintStatistics;
429     bool ForOPDFRead;          /* PS2WRITE only. */
430     gs_param_string OPDFReadProcsetPath; /* PS2WRITE only. */
431     bool CompressEntireFile;  /* PS2WRITE only. */
432     bool ResourcesBeforeUsage; /* PS2WRITE only. */
433     bool HavePDFWidths;        /* PS2WRITE only. */
434     bool HaveStrokeColor;      /* PS2WRITE only. */
435     bool HaveTransparency;
436     bool PatternImagemask; /* The target viewer|printer handles imagemask
437 			      with pattern color. */
438     bool PDFX;		   /* Generate PDF/X */
439     long MaxClipPathSize;  /* The maximal number of elements of a clipping path
440 			      that the target viewer|printer can handle. */
441     long MaxViewerMemorySize;
442     long MaxShadingBitmapSize; /* The maximal number of bytes in
443 			      a bitmap representation of a shading.
444 			      (Bigger shadings to be downsampled). */
445     long MaxInlineImageSize;
446     /* Encryption parameters */
447     gs_param_string OwnerPassword;
448     gs_param_string UserPassword;
449     uint KeyLength;
450     uint Permissions;
451     uint EncryptionR;
452     /* End of parameters */
453     /* Encryption data */
454     byte EncryptionO[32];
455     byte EncryptionU[32];
456     byte EncryptionKey[16];
457     uint EncryptionV;
458     bool EncryptMetadata;
459     gs_param_string NoEncrypt;
460     /* Values derived from DSC comments */
461     bool is_EPS;
462     pdf_page_dsc_info_t doc_dsc_info; /* document default */
463     pdf_page_dsc_info_t page_dsc_info; /* current page */
464     /* Additional graphics state */
465     bool fill_overprint, stroke_overprint;
466     int overprint_mode;
467     gs_id halftone_id;
468     gs_id transfer_ids[4];
469     int transfer_not_identity;	/* bitmask */
470     gs_id black_generation_id, undercolor_removal_id;
471     /* Following are set when device is opened. */
472     pdf_compression_type compression;
473     pdf_compression_type compression_at_page_start;
474 #define pdf_memory v_memory
475     /*
476      * The xref temporary file is logically an array of longs.
477      * xref[id - FirstObjectNumber] is the position in the output file
478      * of the object with the given id.
479      *
480      * Note that xref, unlike the other temporary files, does not have
481      * an associated stream or stream buffer.
482      */
483     pdf_temp_file_t xref;
484     /*
485      * asides holds resources and other "aside" objects.  It is
486      * copied verbatim to the output file at the end of the document.
487      */
488     pdf_temp_file_t asides;
489     /*
490      * streams holds data for stream-type Cos objects.  The data is
491      * copied to the output file at the end of the document.
492      *
493      * Note that streams.save_strm is not used, since we don't interrupt
494      * normal output when saving stream data.
495      */
496     pdf_temp_file_t streams;
497     /*
498      * pictures holds graphic objects being accumulated between BP and EP.
499      * The object is moved to streams when the EP is reached: since BP and
500      * EP nest, we delete the object from the pictures file at that time.
501      */
502     pdf_temp_file_t pictures;
503     /* ................ */
504     long next_id;
505     /* The following 3 objects, and only these, are allocated */
506     /* when the file is opened. */
507     cos_dict_t *Catalog;
508     cos_dict_t *Info;
509     cos_dict_t *Pages;
510 #define pdf_num_initial_ids 3
511     long outlines_id;
512     int next_page;
513     int max_referred_page;
514     long contents_id;
515     pdf_context_t context;
516     long contents_length_id;
517     long contents_pos;
518     pdf_procset_t procsets;	/* used on this page */
519     pdf_text_data_t *text;
520     pdf_text_rotation_t text_rotation;
521 #define initial_num_pages 50
522     pdf_page_t *pages;
523     int num_pages;
524     ulong used_mask;		/* for where_used: page level = 1 */
525     pdf_resource_list_t resources[NUM_RESOURCE_TYPES];
526     /* cs_Patterns[0] is colored; 1,3,4 are uncolored + Gray,RGB,CMYK */
527     pdf_resource_t *cs_Patterns[5];
528     pdf_resource_t *Identity_ToUnicode_CMaps[2]; /* WMode = 0,1 */
529     pdf_resource_t *last_resource;
530     pdf_outline_level_t outline_levels[MAX_OUTLINE_DEPTH];
531     int outline_depth;
532     int closed_outline_depth;
533     int outlines_open;
534     pdf_article_t *articles;
535     cos_dict_t *Dests;
536     byte fileID[16];
537     /*
538      * global_named_objects holds named objects that are independent of
539      * the current namespace: {Catalog}, {DocInfo}, {Page#}, {ThisPage},
540      * {PrevPage}, {NextPage}.
541      */
542     cos_dict_t *global_named_objects;
543     /*
544      * local_named_objects holds named objects in the current namespace.
545      */
546     cos_dict_t *local_named_objects;
547     /*
548      * NI_stack is a last-in, first-out stack in which each element is a
549      * (named) cos_stream_t object that eventually becomes the object of an
550      * image XObject resource.
551      */
552     cos_array_t *NI_stack;
553     /*
554      * Namespace_stack is a last-in, first-out stack in which each pair of
555      * elements is, respectively, a saved value of local_named_objects and
556      * a saved value of NI_stack.  (The latter is not documented by Adobe,
557      * but it was confirmed by them.)
558      */
559     cos_array_t *Namespace_stack;
560     pdf_font_cache_elem_t *font_cache;
561     /*
562      * char_width is used by pdf_text_set_cache to communicate
563      * with assign_char_code around gdev_pdf_fill_mask.
564      */
565     gs_point char_width;
566     /*
567      * We need a stable copy of clipping path to prevent writing
568      * redundant clipping paths when PS document generates such ones.
569      */
570     gx_path *clip_path;
571     /*
572      * Page labels.
573      */
574     cos_array_t *PageLabels;
575     int PageLabels_current_page;
576     cos_dict_t *PageLabels_current_label;
577     /*
578      * The following is a dangerous pointer, which pdf_text_process
579      * uses to communicate with assign_char_code.
580      * It is a pointer from global memory to local memory.
581      * The garbager must not proceess this pointer, and it must
582      * not be listed in st_device_pdfwrite.
583      * It's life time terminates on garbager invocation.
584      */
585     gs_text_enum_t *pte;
586     /*
587      * The viewer's graphic state stack.
588      * We restrict its length with the strongest PDF spec limitation.
589      * Usually 5 levels is enough, but patterns and charprocs may be nested recursively.
590      */
591     pdf_viewer_state vgstack[11];
592     int vgstack_depth;
593     int vgstack_bottom;		 /* Stack bottom for the current substream. */
594     pdf_viewer_state vg_initial; /* Initial values for viewer's graphic state */
595     bool vg_initial_set;
596 
597     /* The substream context stack. */
598     int sbstack_size;
599     int sbstack_depth;
600     pdf_substream_save *sbstack;
601 
602     /* Accessories */
603     cos_dict_t *substream_Resources;     /* Substream resources */
604     gs_color_space_index pcm_color_info_index; /* Index of the ProcessColorModel space. */
605     bool skip_colors; /* Skip colors while a pattern/charproc accumulation. */
606     bool AR4_save_bug; /* See pdf_put_uncolored_pattern */
607     pdf_resource_t *font3; /* The owner of the accumulated charstring. */
608     pdf_resource_t *accumulating_substream_resource;
609     gs_matrix_fixed charproc_ctm;
610     bool charproc_just_accumulated; /* A flag for controlling
611 			the glyph variation recognition.
612 			Used only with uncached charprocs. */
613     bool accumulating_a_global_object; /* ps2write only.
614 			Accumulating a global object (such as a named Form,
615 			so that resources used in it must also be global.
616 			Important for viewers with small memory,
617 			which drops resources per page. */
618     const pdf_char_glyph_pairs_t *cgp; /* A temporary pointer
619 			for pdf_is_same_charproc1.
620 			Must be NULL when the garbager is invoked,
621 			because it points from global to local memory. */
622     int substituted_pattern_count;
623     int substituted_pattern_drop_page;
624     /* Temporary data for use_image_as_pattern,
625        They pass an information about a mask of a masked image,
626        (which is being converted into a pattern)
627        between 2 consecutive calls to pdf_image_end_image_data. */
628     gs_id     image_mask_id;
629     bool      image_mask_is_SMask;
630     bool      image_mask_skip; /* A flag for pdf_begin_transparency_mask */
631     gs_matrix converting_image_matrix;
632     double    image_mask_scale;
633     /* Temporary data for soft mask form. */
634     pdf_resource_t *pres_soft_mask_dict;
635     /* Temporary data for pdfmark_BP. */
636     gs_const_string objname;
637 };
638 
639 #define is_in_page(pdev)\
640   ((pdev)->contents_id != 0)
641 #define is_in_document(pdev)\
642   (is_in_page(pdev) || (pdev)->last_resource != 0)
643 
644 /* Enumerate the individual pointers in a gx_device_pdf. */
645 #define gx_device_pdf_do_ptrs(m)\
646  m(0,asides.strm) m(1,asides.strm_buf) m(2,asides.save_strm)\
647  m(3,streams.strm) m(4,streams.strm_buf)\
648  m(5,pictures.strm) m(6,pictures.strm_buf) m(7,pictures.save_strm)\
649  m(8,Catalog) m(9,Info) m(10,Pages)\
650  m(11,text) m(12,pages)\
651  m(13,cs_Patterns[0])\
652  m(14,cs_Patterns[1]) m(15,cs_Patterns[3]) m(16,cs_Patterns[4])\
653  m(17,last_resource)\
654  m(18,articles) m(19,Dests) m(20,global_named_objects)\
655  m(21, local_named_objects) m(22,NI_stack) m(23,Namespace_stack)\
656  m(24,font_cache) m(25,clip_path)\
657  m(26,PageLabels) m(27,PageLabels_current_label)\
658  m(28,sbstack) m(29,substream_Resources) m(30,font3)\
659  m(31,accumulating_substream_resource) \
660  m(32,pres_soft_mask_dict)
661 #define gx_device_pdf_num_ptrs 33
662 #define gx_device_pdf_do_param_strings(m)\
663     m(0, OPDFReadProcsetPath) m(1, OwnerPassword) m(2, UserPassword) m(3, NoEncrypt)
664 #define gx_device_pdf_num_param_strings 4
665 #define gx_device_pdf_do_const_strings(m)\
666     m(0, objname)
667 #define gx_device_pdf_num_const_strings 1
668 #define st_device_pdf_max_ptrs\
669   (st_device_psdf_max_ptrs + gx_device_pdf_num_ptrs +\
670    gx_device_pdf_num_param_strings + gx_device_pdf_num_const_strings +\
671    NUM_RESOURCE_TYPES * NUM_RESOURCE_CHAINS /* resources[].chains[] */ +\
672    MAX_OUTLINE_DEPTH * 2 /* outline_levels[].{first,last}.action */
673 
674 #define private_st_device_pdfwrite()	/* in gdevpdf.c */\
675   gs_private_st_composite_final(st_device_pdfwrite, gx_device_pdf,\
676     "gx_device_pdf", device_pdfwrite_enum_ptrs, device_pdfwrite_reloc_ptrs,\
677     device_pdfwrite_finalize)
678 
679 /* ================ Driver procedures ================ */
680 
681     /* In gdevpdfb.c */
682 dev_proc_copy_mono(gdev_pdf_copy_mono);
683 dev_proc_copy_color(gdev_pdf_copy_color);
684 dev_proc_fill_mask(gdev_pdf_fill_mask);
685 dev_proc_strip_tile_rectangle(gdev_pdf_strip_tile_rectangle);
686     /* In gdevpdfd.c */
687 extern const gx_device_vector_procs pdf_vector_procs;
688 dev_proc_fill_rectangle(gdev_pdf_fill_rectangle);
689 dev_proc_fill_path(gdev_pdf_fill_path);
690 dev_proc_stroke_path(gdev_pdf_stroke_path);
691     /* In gdevpdfi.c */
692 dev_proc_begin_typed_image(gdev_pdf_begin_typed_image);
693     /* In gdevpdfp.c */
694 dev_proc_get_params(gdev_pdf_get_params);
695 dev_proc_put_params(gdev_pdf_put_params);
696     /* In gdevpdft.c */
697 dev_proc_text_begin(gdev_pdf_text_begin);
698 dev_proc_pattern_manage(gdev_pdf_pattern_manage);
699 dev_proc_fill_rectangle_hl_color(gdev_pdf_fill_rectangle_hl_color);
700     /* In gdevpdfv.c */
701 dev_proc_include_color_space(gdev_pdf_include_color_space);
702     /* In gdevpdft.c */
703 dev_proc_create_compositor(gdev_pdf_create_compositor);
704 dev_proc_begin_transparency_group(gdev_pdf_begin_transparency_group);
705 dev_proc_end_transparency_group(gdev_pdf_end_transparency_group);
706 dev_proc_begin_transparency_mask(gdev_pdf_begin_transparency_mask);
707 dev_proc_end_transparency_mask(gdev_pdf_end_transparency_mask);
708 dev_proc_discard_transparency_layer(gdev_pdf_discard_transparency_layer);
709 
710 /* ================ Utility procedures ================ */
711 
712 /* ---------------- Exported by gdevpdf.c ---------------- */
713 
714 /* Initialize the IDs allocated at startup. */
715 void pdf_initialize_ids(gx_device_pdf * pdev);
716 
717 /* Update the color mapping procedures after setting ProcessColorModel. */
718 void pdf_set_process_color_model(gx_device_pdf * pdev, int index);
719 
720 /* Reset the text state parameters to initial values. */
721 void pdf_reset_text(gx_device_pdf *pdev);
722 
723 /* ---------------- Exported by gdevpdfu.c ---------------- */
724 
725 /* ------ Document ------ */
726 
727 /* Open the document if necessary. */
728 int pdf_open_document(gx_device_pdf * pdev);
729 
730 /* ------ Objects ------ */
731 
732 /* Allocate an ID for a future object. */
733 long pdf_obj_ref(gx_device_pdf * pdev);
734 
735 /* Read the current position in the output stream. */
736 long pdf_stell(gx_device_pdf * pdev);
737 
738 /* Begin an object, optionally allocating an ID. */
739 long pdf_open_obj(gx_device_pdf * pdev, long id);
740 long pdf_begin_obj(gx_device_pdf * pdev);
741 
742 /* End an object. */
743 int pdf_end_obj(gx_device_pdf * pdev);
744 
745 /* ------ Page contents ------ */
746 
747 /* Open a page contents part. */
748 /* Return an error if the page has too many contents parts. */
749 int pdf_open_contents(gx_device_pdf * pdev, pdf_context_t context);
750 
751 /* Close the current contents part if we are in one. */
752 int pdf_close_contents(gx_device_pdf * pdev, bool last);
753 
754 /* ------ Resources et al ------ */
755 
756 extern const char *const pdf_resource_type_names[];
757 extern const gs_memory_struct_type_t *const pdf_resource_type_structs[];
758 
759 /*
760  * Define the offset that indicates that a file position is in the
761  * asides file rather than the main (contents) file.
762  * Must be a power of 2, and larger than the largest possible output file.
763  */
764 #define ASIDES_BASE_POSITION min_long
765 
766 /* Begin an object logically separate from the contents. */
767 /* (I.e., an object in the resource file.) */
768 long pdf_open_separate(gx_device_pdf * pdev, long id);
769 long pdf_begin_separate(gx_device_pdf * pdev);
770 
771 /* Reserve object id. */
772 void pdf_reserve_object_id(gx_device_pdf * pdev, pdf_resource_t *ppres, long id);
773 
774 /* Begin an aside (resource, annotation, ...). */
775 int pdf_alloc_aside(gx_device_pdf * pdev, pdf_resource_t ** plist,
776 		const gs_memory_struct_type_t * pst, pdf_resource_t **ppres,
777 		long id);
778 /* Begin an aside (resource, annotation, ...). */
779 int pdf_begin_aside(gx_device_pdf * pdev, pdf_resource_t **plist,
780 		    const gs_memory_struct_type_t * pst,
781 		    pdf_resource_t **ppres);
782 
783 /* Begin a resource of a given type. */
784 int pdf_begin_resource(gx_device_pdf * pdev, pdf_resource_type_t rtype,
785 		       gs_id rid, pdf_resource_t **ppres);
786 
787 /* Begin a resource body of a given type. */
788 int pdf_begin_resource_body(gx_device_pdf * pdev, pdf_resource_type_t rtype,
789 			    gs_id rid, pdf_resource_t **ppres);
790 
791 /* Allocate a resource, but don't open the stream. */
792 int pdf_alloc_resource(gx_device_pdf * pdev, pdf_resource_type_t rtype,
793 		       gs_id rid, pdf_resource_t **ppres, long id);
794 
795 /* Find same resource. */
796 int pdf_find_same_resource(gx_device_pdf * pdev,
797 	pdf_resource_type_t rtype, pdf_resource_t **ppres,
798 	int (*eq)(gx_device_pdf * pdev, pdf_resource_t *pres0, pdf_resource_t *pres1));
799 
800 
801 /* Find resource by resource id. */
802 pdf_resource_t *pdf_find_resource_by_resource_id(gx_device_pdf * pdev,
803 						pdf_resource_type_t rtype, gs_id id);
804 
805 /* Find a resource of a given type by gs_id. */
806 pdf_resource_t *pdf_find_resource_by_gs_id(gx_device_pdf * pdev,
807 					   pdf_resource_type_t rtype,
808 					   gs_id rid);
809 
810 void pdf_drop_resources(gx_device_pdf * pdev, pdf_resource_type_t rtype,
811 	int (*cond)(gx_device_pdf * pdev, pdf_resource_t *pres));
812 
813 /* Print resource statistics. */
814 void pdf_print_resource_statistics(gx_device_pdf * pdev);
815 
816 
817 /* Cancel a resource (do not write it into PDF). */
818 int pdf_cancel_resource(gx_device_pdf * pdev, pdf_resource_t *pres,
819 	pdf_resource_type_t rtype);
820 
821 /* Remove a resource. */
822 void pdf_forget_resource(gx_device_pdf * pdev, pdf_resource_t *pres1,
823 	pdf_resource_type_t rtype);
824 
825 /* Substitute a resource with a same one. */
826 int pdf_substitute_resource(gx_device_pdf *pdev, pdf_resource_t **ppres,
827 	    pdf_resource_type_t rtype,
828 	    int (*eq)(gx_device_pdf *pdev, pdf_resource_t *pres0, pdf_resource_t *pres1),
829 	    bool write);
830 
831 /* Get the object id of a resource. */
832 long pdf_resource_id(const pdf_resource_t *pres);
833 
834 /* End a separate object. */
835 int pdf_end_separate(gx_device_pdf * pdev);
836 
837 /* End an aside. */
838 int pdf_end_aside(gx_device_pdf * pdev);
839 
840 /* End a resource. */
841 int pdf_end_resource(gx_device_pdf * pdev);
842 
843 /*
844  * Write the Cos objects for resources local to a content stream.
845  */
846 int pdf_write_resource_objects(gx_device_pdf *pdev, pdf_resource_type_t rtype);
847 
848 /*
849  * Reverse resource chains.
850  * ps2write uses it with page resources.
851  * Assuming only the 0th chain contauns something.
852  */
853 void pdf_reverse_resource_chain(gx_device_pdf *pdev, pdf_resource_type_t rtype);
854 
855 /*
856  * Free unnamed Cos objects for resources local to a content stream.
857  */
858 int pdf_free_resource_objects(gx_device_pdf *pdev, pdf_resource_type_t rtype);
859 
860 /* Write and free all resource objects. */
861 
862 int pdf_write_and_free_all_resource_objects(gx_device_pdf *pdev);
863 
864 /*
865  * Store the resource sets for a content stream (page or XObject).
866  * Sets page->{procsets, resource_ids[], fonts_id}.
867  */
868 int pdf_store_page_resources(gx_device_pdf *pdev, pdf_page_t *page);
869 
870 /* Copy data from a temporary file to a stream. */
871 void pdf_copy_data(stream *s, FILE *file, long count, stream_arcfour_state *ss);
872 void pdf_copy_data_safe(stream *s, FILE *file, long position, long count);
873 
874 /* Add the encryption filter. */
875 int pdf_begin_encrypt(gx_device_pdf * pdev, stream **s, gs_id object_id);
876 /* Remove the encryption filter. */
877 void pdf_end_encrypt(gx_device_pdf * pdev);
878 /* Initialize encryption. */
879 int pdf_encrypt_init(const gx_device_pdf * pdev, gs_id object_id, stream_arcfour_state *psarc4);
880 
881 
882 /* ------ Pages ------ */
883 
884 /* Get or assign the ID for a page. */
885 /* Returns 0 if the page number is out of range. */
886 long pdf_page_id(gx_device_pdf * pdev, int page_num);
887 
888 /* Get the page structure for the current page. */
889 pdf_page_t *pdf_current_page(gx_device_pdf *pdev);
890 
891 /* Get the dictionary object for the current page. */
892 cos_dict_t *pdf_current_page_dict(gx_device_pdf *pdev);
893 
894 /* Open a page for writing. */
895 int pdf_open_page(gx_device_pdf * pdev, pdf_context_t context);
896 
897 /*  Go to the unclipped stream context. */
898 int pdf_unclip(gx_device_pdf * pdev);
899 
900 /* Write saved page- or document-level information. */
901 int pdf_write_saved_string(gx_device_pdf * pdev, gs_string * pstr);
902 
903 /* ------ Path drawing ------ */
904 
905 /* Store a copy of clipping path. */
906 int pdf_remember_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath);
907 
908 /* Test whether the clip path needs updating. */
909 bool pdf_must_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath);
910 
911 /* Write and update the clip path. */
912 int pdf_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath);
913 
914 /* ------ Masked image convertion ------ */
915 
916 typedef struct pdf_lcvd_s {
917     gx_device_memory mdev;
918     gx_device_memory *mask;
919     gx_device_pdf *pdev;
920     dev_t_proc_fill_rectangle((*std_fill_rectangle), gx_device);
921     dev_t_proc_close_device((*std_close_device), gx_device);
922     bool mask_is_empty;
923     bool path_is_empty;
924     bool mask_is_clean;
925     bool write_matrix;
926     bool has_background;
927     gs_matrix m;
928     gs_point path_offset;
929 } pdf_lcvd_t;
930 
931 #define public_st_pdf_lcvd_t()\
932   gs_public_st_suffix_add2(st_pdf_lcvd_t, pdf_lcvd_t,\
933     "pdf_lcvd_t", pdf_lcvd_t_enum_ptrs,\
934     pdf_lcvd_t_reloc_ptrs, st_device_memory, mask, pdev)
935 #define pdf_lcvd_t_max_ptrs (gx_device_memory_max_ptrs + 2)
936 
937 
938 int pdf_setup_masked_image_converter(gx_device_pdf *pdev, gs_memory_t *mem, const gs_matrix *m, pdf_lcvd_t **pcvd,
939 				 bool need_mask, int x, int y, int w, int h, bool write_on_close);
940 int pdf_dump_converted_image(gx_device_pdf *pdev, pdf_lcvd_t *cvd);
941 void pdf_remove_masked_image_converter(gx_device_pdf *pdev, pdf_lcvd_t *cvd, bool need_mask);
942 
943 
944 /* ------ Miscellaneous output ------ */
945 
946 #define PDF_MAX_PRODUCER 200	/* adhoc */
947 /* Generate the default Producer string. */
948 void pdf_store_default_Producer(char buf[PDF_MAX_PRODUCER]);
949 
950 /* Define the strings for filter names and parameters. */
951 typedef struct pdf_filter_names_s {
952     const char *ASCII85Decode;
953     const char *ASCIIHexDecode;
954     const char *CCITTFaxDecode;
955     const char *DCTDecode;
956     const char *DecodeParms;
957     const char *Filter;
958     const char *FlateDecode;
959     const char *LZWDecode;
960     const char *RunLengthDecode;
961 } pdf_filter_names_t;
962 #define PDF_FILTER_NAMES\
963   "/ASCII85Decode", "/ASCIIHexDecode", "/CCITTFaxDecode",\
964   "/DCTDecode",  "/DecodeParms", "/Filter", "/FlateDecode",\
965   "/LZWDecode", "/RunLengthDecode"
966 #define PDF_FILTER_NAMES_SHORT\
967   "/A85", "/AHx", "/CCF", "/DCT", "/DP", "/F", "/Fl", "/LZW", "/RL"
968 
969 /* Write matrix values. */
970 void pdf_put_matrix(gx_device_pdf *pdev, const char *before,
971 		    const gs_matrix *pmat, const char *after);
972 
973 /* Write a name, with escapes for unusual characters. */
974 typedef int (*pdf_put_name_chars_proc_t)(stream *, const byte *, uint);
975 pdf_put_name_chars_proc_t
976     pdf_put_name_chars_proc(const gx_device_pdf *pdev);
977 int pdf_put_name_chars(const gx_device_pdf *pdev, const byte *nstr,
978 			uint size);
979 int pdf_put_name(const gx_device_pdf *pdev, const byte *nstr, uint size);
980 
981 /* Write a string in its shortest form ( () or <> ). */
982 int pdf_put_string(const gx_device_pdf *pdev, const byte *str, uint size);
983 
984 /* Write a value, treating names specially. */
985 int pdf_write_value(const gx_device_pdf *pdev, const byte *vstr, uint size, gs_id object_id);
986 
987 /* Store filters for a stream. */
988 int pdf_put_filters(cos_dict_t *pcd, gx_device_pdf *pdev, stream *s,
989 		    const pdf_filter_names_t *pfn);
990 
991 /* Define a possibly encoded and compressed data stream. */
992 typedef struct pdf_data_writer_s {
993     psdf_binary_writer binary;
994     long start;
995     long length_pos;
996     pdf_resource_t *pres;
997     gx_device_pdf *pdev; /* temporary for backward compatibility of pdf_end_data prototype. */
998     long length_id;
999     bool encrypted;
1000 } pdf_data_writer_t;
1001 /*
1002  * Begin a data stream.  The client has opened the object and written
1003  * the << and any desired dictionary keys.
1004  */
1005 #define DATA_STREAM_NOT_BINARY 0  /* data are text, not binary */
1006 #define DATA_STREAM_BINARY 1	/* data are binary */
1007 #define DATA_STREAM_COMPRESS 2	/* OK to compress data */
1008 #define DATA_STREAM_NOLENGTH 4	/* Skip the length reference and filter names writing. */
1009 #define DATA_STREAM_ENCRYPT  8	/* Encrypt data. */
1010 int pdf_begin_data_stream(gx_device_pdf *pdev, pdf_data_writer_t *pdw,
1011 			  int options, gs_id object_id);
1012 int pdf_append_data_stream_filters(gx_device_pdf *pdev, pdf_data_writer_t *pdw,
1013 		      int orig_options, gs_id object_id);
1014 /* begin_data = begin_data_binary with both options = true. */
1015 int pdf_begin_data(gx_device_pdf *pdev, pdf_data_writer_t *pdw);
1016 
1017 /* End a data stream. */
1018 int pdf_end_data(pdf_data_writer_t *pdw);
1019 
1020 /* ------ Functions ------ */
1021 
1022 /* Define the maximum size of a Function reference. */
1023 #define MAX_REF_CHARS ((sizeof(long) * 8 + 2) / 3)
1024 
1025 /*
1026  * Create a Function object with or without range scaling.  Scaling means
1027  * that if x[i] is the i'th output value from the original Function,
1028  * the i'th output value from the Function object will be (x[i] -
1029  * ranges[i].rmin) / (ranges[i].rmax - ranges[i].rmin).  Note that this is
1030  * the inverse of the scaling convention for Functions per se.
1031  */
1032 #ifndef gs_function_DEFINED
1033 typedef struct gs_function_s gs_function_t;
1034 #  define gs_function_DEFINED
1035 #endif
1036 int pdf_function(gx_device_pdf *pdev, const gs_function_t *pfn,
1037 		 cos_value_t *pvalue);
1038 int pdf_function_scaled(gx_device_pdf *pdev, const gs_function_t *pfn,
1039 			const gs_range_t *pranges, cos_value_t *pvalue);
1040 
1041 /* Write a Function object, returning its object ID. */
1042 int pdf_write_function(gx_device_pdf *pdev, const gs_function_t *pfn,
1043 		       long *pid);
1044 
1045 /* ------ Fonts ------ */
1046 
1047 /* Write a FontBBox dictionary element. */
1048 int pdf_write_font_bbox(gx_device_pdf *pdev, const gs_int_rect *pbox);
1049 
1050 /* ---------------- Exported by gdevpdfm.c ---------------- */
1051 
1052 /*
1053  * Define the type for a pdfmark-processing procedure.
1054  * If nameable is false, the objname argument is always NULL.
1055  */
1056 #define pdfmark_proc(proc)\
1057   int proc(gx_device_pdf *pdev, gs_param_string *pairs, uint count,\
1058 	   const gs_matrix *pctm, const gs_param_string *objname)
1059 
1060 /* Compare a C string and a gs_param_string. */
1061 bool pdf_key_eq(const gs_param_string * pcs, const char *str);
1062 
1063 /* Scan an integer out of a parameter string. */
1064 int pdfmark_scan_int(const gs_param_string * pstr, int *pvalue);
1065 
1066 /* Process a pdfmark (called from pdf_put_params). */
1067 int pdfmark_process(gx_device_pdf * pdev, const gs_param_string_array * pma);
1068 
1069 /* Close the current level of the outline tree. */
1070 int pdfmark_close_outline(gx_device_pdf * pdev);
1071 
1072 /* Close the pagelabel numtree. */
1073 int pdfmark_end_pagelabels(gx_device_pdf * pdev);
1074 
1075 /* Finish writing an article. */
1076 int pdfmark_write_article(gx_device_pdf * pdev, const pdf_article_t * part);
1077 
1078 /* ---------------- Exported by gdevpdfr.c ---------------- */
1079 
1080 /* Test whether an object name has valid syntax, {name}. */
1081 bool pdf_objname_is_valid(const byte *data, uint size);
1082 
1083 /*
1084  * Look up a named object.  Return e_rangecheck if the syntax is invalid,
1085  * e_undefined if no object by that name exists.
1086  */
1087 int pdf_find_named(gx_device_pdf * pdev, const gs_param_string * pname,
1088 		   cos_object_t **ppco);
1089 
1090 /*
1091  * Create a named object.  id = -1L means do not assign an id.  pname = 0
1092  * means just create the object, do not name it.
1093  */
1094 int pdf_create_named(gx_device_pdf *pdev, const gs_param_string *pname,
1095 		     cos_type_t cotype, cos_object_t **ppco, long id);
1096 int pdf_create_named_dict(gx_device_pdf *pdev, const gs_param_string *pname,
1097 			  cos_dict_t **ppcd, long id);
1098 
1099 /*
1100  * Look up a named object as for pdf_find_named.  If the object does not
1101  * exist, create it (as a dictionary if it is one of the predefined names
1102  * {ThisPage}, {NextPage}, {PrevPage}, or {Page<#>}, otherwise as a
1103  * generic object) and return 1.
1104  */
1105 int pdf_refer_named(gx_device_pdf *pdev, const gs_param_string *pname,
1106 		    cos_object_t **ppco);
1107 
1108 /*
1109  * Look up a named object as for pdf_refer_named.  If the object already
1110  * exists and is not simply a forward reference, return e_rangecheck;
1111  * if it exists as a forward reference, set its type and return 0;
1112  * otherwise, create the object with the given type and return 1.
1113  * pname = 0 is allowed: in this case, simply create the object.
1114  */
1115 int pdf_make_named(gx_device_pdf * pdev, const gs_param_string * pname,
1116 		   cos_type_t cotype, cos_object_t **ppco, bool assign_id);
1117 int pdf_make_named_dict(gx_device_pdf * pdev, const gs_param_string * pname,
1118 			cos_dict_t **ppcd, bool assign_id);
1119 
1120 /*
1121  * Look up a named object as for pdf_refer_named.  If the object does not
1122  * exist, or is a forward reference, return e_undefined; if the object
1123  * exists has the wrong type, return e_typecheck.
1124  */
1125 int pdf_get_named(gx_device_pdf * pdev, const gs_param_string * pname,
1126 		  cos_type_t cotype, cos_object_t **ppco);
1127 
1128 /*
1129  * Push the current local namespace onto the namespace stack, and reset it
1130  * to an empty namespace.
1131  */
1132 int pdf_push_namespace(gx_device_pdf *pdev);
1133 
1134 /*
1135  * Pop the top local namespace from the namespace stack.  Return an error if
1136  * the stack is empty.
1137  */
1138 int pdf_pop_namespace(gx_device_pdf *pdev);
1139 
1140 /*
1141  * Scan a string for a token.  <<, >>, [, and ] are treated as tokens.
1142  * Return 1 if a token was scanned, 0 if we reached the end of the string,
1143  * or an error.  On a successful return, the token extends from *ptoken up
1144  * to but not including *pscan.
1145  */
1146 int pdf_scan_token(const byte **pscan, const byte * end, const byte **ptoken);
1147 
1148 /*
1149  * Scan a possibly composite token: arrays and dictionaries are treated as
1150  * single tokens.
1151  */
1152 int pdf_scan_token_composite(const byte **pscan, const byte * end,
1153 			     const byte **ptoken);
1154 
1155 /* Replace object names with object references in a (parameter) string. */
1156 int pdf_replace_names(gx_device_pdf *pdev, const gs_param_string *from,
1157 		      gs_param_string *to);
1158 
1159 /* ================ Text module procedures ================ */
1160 
1161 /* ---------------- Exported by gdevpdfw.c ---------------- */
1162 
1163 /* For gdevpdf.c */
1164 
1165 /*
1166  * Close the text-related parts of a document, including writing out font
1167  * and related resources.
1168  */
1169 int pdf_close_text_document(gx_device_pdf *pdev);
1170 
1171 /* ---------------- Exported by gdevpdft.c ---------------- */
1172 
1173 /* For gdevpdf.c */
1174 
1175 pdf_text_data_t *pdf_text_data_alloc(gs_memory_t *mem);
1176 void pdf_set_text_state_default(pdf_text_state_t *pts);
1177 void pdf_text_state_copy(pdf_text_state_t *pts_to, pdf_text_state_t *pts_from);
1178 void pdf_reset_text_page(pdf_text_data_t *ptd);
1179 void pdf_reset_text_state(pdf_text_data_t *ptd);
1180 void pdf_close_text_page(gx_device_pdf *pdev);
1181 
1182 /* For gdevpdfb.c */
1183 
1184 int pdf_char_image_y_offset(const gx_device_pdf *pdev, int x, int y, int h);
1185 
1186 /* Begin a CharProc for an embedded (bitmap) font. */
1187 int pdf_begin_char_proc(gx_device_pdf * pdev, int w, int h, int x_width,
1188 			int y_offset, gs_id id, pdf_char_proc_t **ppcp,
1189 			pdf_stream_position_t * ppos);
1190 
1191 /* End a CharProc. */
1192 int pdf_end_char_proc(gx_device_pdf * pdev, pdf_stream_position_t * ppos);
1193 
1194 /* Put out a reference to an image as a character in an embedded font. */
1195 int pdf_do_char_image(gx_device_pdf * pdev, const pdf_char_proc_t * pcp,
1196 		      const gs_matrix * pimat);
1197 
1198 /* Start charproc accumulation for a Type 3 font. */
1199 int pdf_start_charproc_accum(gx_device_pdf *pdev);
1200 /* Install charproc accumulator for a Type 3 font. */
1201 int pdf_set_charproc_attrs(gx_device_pdf *pdev, gs_font *font, const double *pw, int narg,
1202 		gs_text_cache_control_t control, gs_char ch, gs_const_string *gnstr);
1203 /* Complete charproc accumulation for aType 3 font. */
1204 int pdf_end_charproc_accum(gx_device_pdf *pdev, gs_font *font,
1205 		const pdf_char_glyph_pairs_t *cgp);
1206 
1207 /* Open a stream object in the temporary file. */
1208 int pdf_open_aside(gx_device_pdf *pdev, pdf_resource_type_t rtype,
1209 	gs_id id, pdf_resource_t **ppres, bool reserve_object_id, int options);
1210 
1211 /* Close a stream object in the temporary file. */
1212 int pdf_close_aside(gx_device_pdf *pdev);
1213 
1214 /* Enter the substream accumulation mode. */
1215 int pdf_enter_substream(gx_device_pdf *pdev, pdf_resource_type_t rtype,
1216 		gs_id id, pdf_resource_t **ppres, bool reserve_object_id, bool compress);
1217 
1218 /* Exit the substream accumulation mode. */
1219 int pdf_exit_substream(gx_device_pdf *pdev);
1220 /* Add procsets to substream Resources. */
1221 int pdf_add_procsets(cos_dict_t *pcd, pdf_procset_t procsets);
1222 /* Add a resource to substream Resources. */
1223 int pdf_add_resource(gx_device_pdf *pdev, cos_dict_t *pcd, const char *key, pdf_resource_t *pres);
1224 
1225 
1226 /* For gdevpdfu.c */
1227 
1228 int pdf_from_stream_to_text(gx_device_pdf *pdev);
1229 int pdf_from_string_to_text(gx_device_pdf *pdev);
1230 void pdf_close_text_contents(gx_device_pdf *pdev);
1231 
1232 #endif /* gdevpdfx_INCLUDED */
1233