Coverage Report

Created: 2025-12-31 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/devices/vector/gdevpdfx.h
Line
Count
Source
1
/* Copyright (C) 2001-2025 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Internal definitions for PDF-writing driver. */
18
19
#ifndef gdevpdfx_INCLUDED
20
#  define gdevpdfx_INCLUDED
21
22
#include "gsparam.h"
23
#include "gsuid.h"
24
#include "gxdevice.h"
25
#include "gxfont.h"
26
#include "gxline.h"
27
#include "stream.h"
28
#include "spprint.h"
29
#include "gdevpsdf.h"
30
#include "gxdevmem.h"
31
#include "sarc4.h"
32
#include "gsfunc.h"
33
34
#define FINE_GLYPH_USAGE 1 /* Old code = 0, new code = 1 */
35
36
/* ---------------- Acrobat limitations ---------------- */
37
38
/*
39
 * The PDF reference manual, 2nd ed., claims that the limit for coordinates
40
 * is +/- 32767. However, testing indicates that Acrobat Reader 4 for
41
 * Windows and Linux fail with coordinates outside +/- 16383. Hence, we
42
 * limit coordinates to 16k, with a little slop.
43
 */
44
/* 28/05/2020 This was only being applied to text and a subset of paths. Since
45
 * Acrobat 4 is now more than 20 years old, lets just drop support for it. The
46
 * PDF specification never had this limit, just Adobe's software.
47
 */
48
/* 31/05/2021 However, it transpires that the PDF/A-1 specification adopted the
49
 * implementation limits of Acrobat 5 as part of the specification. So we need
50
 * to handle that, but we don't need to limit ourselves to the more restrictive
51
 * Acrobat 4 limit of 16384 ( MAX_USER_COORD 16300), we can use 32767. THis is only
52
 * used when creating PDF/A-1 files.
53
 */
54
71.9M
#define MAX_USER_COORD 32700
55
56
/* ---------------- Statically allocated sizes ---------------- */
57
/* These should really be dynamic.... */
58
59
/* Define the maximum depth of an outline tree. */
60
/* Note that there is no limit on the breadth of the tree. */
61
78.9k
#define INITIAL_MAX_OUTLINE_DEPTH 32
62
63
/* Define the maximum size of a destination array string. */
64
15.9k
#define MAX_DEST_STRING 80
65
66
/* Define the maximum number of objects stored in an ObjStm */
67
276k
#define MAX_OBJSTM_OBJECTS 200
68
69
/* ================ Types and structures ================ */
70
71
typedef struct pdf_base_font_s pdf_base_font_t;
72
73
/* Define the possible contexts for the output stream. */
74
typedef enum {
75
    PDF_IN_NONE,
76
    PDF_IN_STREAM,
77
    PDF_IN_TEXT,
78
    PDF_IN_STRING
79
} pdf_context_t;
80
81
/* ---------------- Cos objects ---------------- */
82
83
/*
84
 * These are abstract types for cos objects.  The concrete types are in
85
 * gdevpdfo.h.
86
 */
87
typedef struct cos_object_s cos_object_t;
88
typedef struct cos_stream_s cos_stream_t;
89
typedef struct cos_dict_s cos_dict_t;
90
typedef struct cos_array_s cos_array_t;
91
typedef struct cos_value_s cos_value_t;
92
typedef struct cos_object_procs_s cos_object_procs_t;
93
typedef const cos_object_procs_t *cos_type_t;
94
#define cos_types_DEFINED
95
96
typedef struct pdf_text_state_s pdf_text_state_t;
97
98
typedef struct pdf_char_glyph_pairs_s pdf_char_glyph_pairs_t;
99
100
/* ---------------- Resources ---------------- */
101
102
typedef enum {
103
    /*
104
     * Standard PDF resources.  Font must be last, because resources
105
     * up to but not including Font are written page-by-page.
106
     */
107
    resourceColorSpace,
108
    resourceExtGState,
109
    resourcePattern,
110
    resourceShading,
111
    resourceXObject,
112
    resourceProperties,
113
    resourceOther, /* Anything else that needs to be stored for a time.
114
                    * Can be any of the types defined below NUM_RESOURCE_TYPES
115
                    * but this is the type used to identify the object.
116
                    */
117
    resourceFont,
118
    /*
119
     * Internally used (pseudo-)resources.
120
     */
121
    resourceCharProc,
122
    resourceCIDFont,
123
    resourceCMap,
124
    resourceFontDescriptor,
125
    resourceGroup,
126
    resourceSoftMaskDict,
127
    resourceFunction,
128
    resourcePage,
129
    NUM_RESOURCE_TYPES,
130
    /* These resource types were 'resourceOther', but we want to track them
131
     * for ps2write. They are not stored in the pdf device structure, unlike
132
     * the reource types above.
133
     */
134
    resourceEncoding,
135
    resourceCIDSystemInfo,
136
    resourceHalftone,
137
    resourceLength,
138
    resourceStream,
139
    resourceOutline,
140
    resourceArticle,
141
    resourceDests,
142
    resourceLabels,
143
    resourceThread,
144
    resourceCatalog,
145
    resourceEncrypt,
146
    resourcePagesTree,
147
    resourceMetadata,
148
    resourceICC,
149
    resourceAnnotation,
150
    resourceEmbeddedFiles,
151
    resourceFontFile,
152
    resourceNone        /* Special, used when this isn't a resource at all
153
                         * eg when we execute a resource we've just written, such as
154
                         * a Pattern.
155
                         */
156
} pdf_resource_type_t;
157
158
#define PDF_RESOURCE_TYPE_NAMES\
159
0
  "/ColorSpace", "/ExtGState", "/Pattern", "/Shading", "/XObject", "/Properties", 0, "/Font",\
160
0
  0, "/Font", "/CMap", "/FontDescriptor", "/Group", "/Mask", 0, 0, 0, 0, 0,\
161
0
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
162
#define PDF_RESOURCE_TYPE_STRUCTS\
163
  &st_pdf_color_space,                /* gdevpdfg.h / gdevpdfc.c */\
164
  &st_pdf_resource,                /* see below */\
165
  &st_pdf_pattern,\
166
  &st_pdf_resource,\
167
  &st_pdf_x_object,                /* see below */\
168
  &st_pdf_resource,\
169
  &st_pdf_resource,\
170
  &st_pdf_font_resource,        /* gdevpdff.h / gdevpdff.c */\
171
  &st_pdf_char_proc,                /* gdevpdff.h / gdevpdff.c */\
172
  &st_pdf_font_resource,        /* gdevpdff.h / gdevpdff.c */\
173
  &st_pdf_resource,\
174
  &st_pdf_font_descriptor,        /* gdevpdff.h / gdevpdff.c */\
175
  &st_pdf_resource,\
176
  &st_pdf_resource,\
177
  &st_pdf_resource,\
178
  &st_pdf_resource
179
180
/*
181
 * rname is currently R<id> for all resources other than synthesized fonts;
182
 * for synthesized fonts, rname is A, B, ....  The string is null-terminated.
183
 */
184
185
    /* WARNING WILL ROBINSON!
186
     * these 2 pointers may *look* like a double linked list but in fact these are pointers
187
     * to two *different* single-linked lists. 'next' points to the next resource
188
     * of this type, which is stored in the resource chains pointed to by the array
189
     * 'resources' in the device structure. 'prev' is a pointer
190
     * to the list of stored resources of all types, pointed to by the
191
     * 'last_resource' member of the device structure.
192
     */
193
#define pdf_resource_common(typ)\
194
    typ *next;                        /* next resource of this type */\
195
    pdf_resource_t *prev;        /* previously allocated resource */\
196
    gs_id rid;                        /* optional ID key */\
197
    bool named;\
198
    bool global;                /* ps2write only */\
199
    char rname[1/*R*/ + (sizeof(int64_t) * 8 / 3 + 1) + 1/*\0*/];\
200
    uint64_t where_used;                /* 1 bit per level of content stream */\
201
    cos_object_t *object
202
typedef struct pdf_resource_s pdf_resource_t;
203
struct pdf_resource_s {
204
    pdf_resource_common(pdf_resource_t);
205
};
206
207
/* The descriptor is public for subclassing. */
208
extern_st(st_pdf_resource);
209
#define public_st_pdf_resource()  /* in gdevpdfu.c */\
210
  gs_public_st_ptrs3(st_pdf_resource, pdf_resource_t, "pdf_resource_t",\
211
    pdf_resource_enum_ptrs, pdf_resource_reloc_ptrs, next, prev, object)
212
213
/*
214
 * We define XObject resources here because they are used for Image,
215
 * Form, and PS XObjects, which are implemented in different files.
216
 */
217
typedef struct pdf_x_object_s pdf_x_object_t;
218
struct pdf_x_object_s {
219
    pdf_resource_common(pdf_x_object_t);
220
    int width, height;                /* specified width and height for images */
221
    int data_height;                /* actual data height for images */
222
};
223
#define private_st_pdf_x_object()  /* in gdevpdfu.c */\
224
  gs_private_st_suffix_add0(st_pdf_x_object, pdf_x_object_t,\
225
    "pdf_x_object_t", pdf_x_object_enum_ptrs, pdf_x_object_reloc_ptrs,\
226
    st_pdf_resource)
227
228
/* Define the mask for which procsets have been used on a page. */
229
typedef enum {
230
    NoMarks = 0,
231
    ImageB = 1,
232
    ImageC = 2,
233
    ImageI = 4,
234
    Text = 8
235
} pdf_procset_t;
236
237
/* ------ Fonts ------ */
238
239
/* Define abstract types. */
240
typedef struct pdf_char_proc_s pdf_char_proc_t;        /* gdevpdff.h */
241
typedef struct pdf_char_proc_ownership_s pdf_char_proc_ownership_t;        /* gdevpdff.h */
242
typedef struct pdf_font_s pdf_font_t;  /* gdevpdff.h */
243
typedef struct pdf_text_data_s pdf_text_data_t;  /* gdevpdft.h */
244
245
/* ---------------- Other auxiliary structures ---------------- */
246
247
/* Outline nodes and levels */
248
typedef struct pdf_outline_node_s {
249
    int64_t id, parent_id, prev_id, first_id, last_id;
250
    int count;
251
    cos_dict_t *action;
252
} pdf_outline_node_t;
253
typedef struct pdf_outline_level_s {
254
    pdf_outline_node_t first;
255
    pdf_outline_node_t last;
256
    int left;
257
} pdf_outline_level_t;
258
/*
259
 * The GC descriptor is implicit, since outline levels occur only in an
260
 * embedded array in the gx_device_pdf structure.
261
 */
262
263
/* Articles */
264
typedef struct pdf_bead_s {
265
    int64_t id, article_id, prev_id, next_id, page_id;
266
    gs_rect rect;
267
} pdf_bead_t;
268
typedef struct pdf_article_s pdf_article_t;
269
struct pdf_article_s {
270
    pdf_article_t *next;
271
    cos_dict_t *contents;
272
    pdf_bead_t first;
273
    pdf_bead_t last;
274
};
275
276
#define private_st_pdf_article()\
277
  gs_private_st_ptrs2(st_pdf_article, pdf_article_t,\
278
    "pdf_article_t", pdf_article_enum_ptrs, pdf_article_reloc_ptrs,\
279
    next, contents)
280
281
/* ---------------- The device structure ---------------- */
282
283
/* Resource lists */
284
/* We seem to split the resources up into 'NUM_RESOURCE_CHAINS' linked
285
 * lists purely as a performance boost. Presumably it save searching very
286
 * long lists.
287
 */
288
323M
#define NUM_RESOURCE_CHAINS 16
289
typedef struct pdf_resource_list_s {
290
    pdf_resource_t *chains[NUM_RESOURCE_CHAINS];
291
} pdf_resource_list_t;
292
293
/* Define the hash function for gs_ids. */
294
2.73M
#define gs_id_hash(rid) ((rid) + ((rid) / NUM_RESOURCE_CHAINS))
295
/* Define the accessor for the proper hash chain. */
296
#define PDF_RESOURCE_CHAIN(pdev, type, rid)\
297
2.46M
  (&(pdev)->resources[type].chains[gs_id_hash(rid) % NUM_RESOURCE_CHAINS])
298
299
/* Define the bookkeeping for an open stream. */
300
typedef struct pdf_stream_position_s {
301
    int64_t length_id;
302
    gs_offset_t start_pos;
303
} pdf_stream_position_t;
304
305
/*
306
 * Define the structure for keeping track of text rotation.
307
 * There is one for the current page (for AutoRotate /PageByPage)
308
 * and one for the whole document (for AutoRotate /All).
309
 */
310
typedef struct pdf_text_rotation_s {
311
    int64_t counts[5];                /* 0, 90, 180, 270, other */
312
    int Rotate;                        /* computed rotation, -1 means none */
313
} pdf_text_rotation_t;
314
55.7k
#define pdf_text_rotation_angle_values 0, 90, 180, 270, -1
315
316
/*
317
 * Define document and page information derived from DSC comments.
318
 */
319
typedef struct pdf_page_dsc_info_s {
320
    int orientation;                /* -1 .. 3 */
321
    int viewing_orientation;        /* -1 .. 3 */
322
    gs_rect bounding_box;
323
} pdf_page_dsc_info_t;
324
325
/*
326
 * Define the stored information for a page.  Because pdfmarks may add
327
 * information to any page anywhere in the document, we have to wait
328
 * until the end to write the page dictionaries.
329
 */
330
typedef struct pdf_page_s {
331
    cos_dict_t *Page;
332
    gs_point MediaBox;
333
    pdf_procset_t procsets;
334
    int64_t contents_id;
335
    int64_t resource_ids[resourceFont + 1]; /* resources thru Font, see above */
336
    int64_t group_id;
337
    cos_array_t *Annots;
338
    pdf_text_rotation_t text_rotation;
339
    pdf_page_dsc_info_t dsc_info;
340
    bool NumCopies_set; /* ps2write only. */
341
    int NumCopies;      /* ps2write only. */
342
    float UserUnit;     /* pdfwrite only */
343
} pdf_page_t;
344
#define private_st_pdf_page()        /* in gdevpdf.c */\
345
  gs_private_st_ptrs2(st_pdf_page, pdf_page_t, "pdf_page_t",\
346
    pdf_page_enum_ptrs, pdf_page_reloc_ptrs, Page, Annots)
347
348
/*
349
 * Define the structure for the temporary files used while writing.
350
 * There are 4 of these, described below.
351
 */
352
typedef struct pdf_temp_file_s {
353
    char file_name[gp_file_name_sizeof];
354
    gp_file *file;
355
    stream *strm;
356
    byte *strm_buf;
357
    stream *save_strm;                /* save pdev->strm while writing here */
358
} pdf_temp_file_t;
359
360
typedef struct gx_device_pdf_s gx_device_pdf;
361
362
/* Structures and definitions for linearisation */
363
typedef struct linearisation_record_s {
364
    int PageUsage;
365
    int NumPagesUsing;
366
    int *PageList;
367
    uint NewObjectNumber;
368
    gs_offset_t OriginalOffset;
369
    gs_offset_t LinearisedOffset;
370
    gs_offset_t Length;
371
} pdf_linearisation_record_t;
372
373
#define private_st_pdf_linearisation_record()\
374
  gs_private_st_ptrs1(st_pdf_linearisation_record, pdf_linearisation_record_t,\
375
    "pdf_linearisation_record_t", pdf_linearisation_record_enum_ptrs, pdf_linearisation_record_reloc_ptrs,\
376
    PageList)
377
378
typedef struct page_hint_stream_header_s {
379
    unsigned int LeastObjectsPerPage;    /* Including the page object */
380
    /* Item 2 is already stored elsewhere */
381
    unsigned int MostObjectsPerPage;
382
    unsigned short ObjectNumBits;
383
    unsigned int LeastPageLength;        /* From beginning of page object to end of last object used by page */
384
    unsigned int MostPageLength;
385
    unsigned short PageLengthNumBits;
386
    unsigned int LeastPageOffset;
387
    unsigned int MostPageOffset;
388
    unsigned short PageOffsetNumBits;
389
    unsigned int LeastContentLength;
390
    unsigned int MostContentLength;
391
    unsigned short ContentLengthNumBits;
392
    unsigned int MostSharedObjects;
393
    unsigned int LargestSharedObject;
394
    unsigned short SharedObjectNumBits;
395
} page_hint_stream_header_t;
396
397
typedef struct page_hint_stream_s {
398
    unsigned int NumUniqueObjects; /* biased by the least number of objects on any page */
399
    unsigned int PageLength;       /* biased by the least page length*/
400
    unsigned int NumSharedObjects;
401
    unsigned int *SharedObjectRef; /* one for each shaed object on the page */
402
    /* Item 5 we invent */
403
    gs_offset_t ContentOffset; /* biased by the least offset to the conent stream for any page */
404
    gs_offset_t ContentLength;/* biased by the least content stream length */
405
} page_hint_stream_t;
406
407
typedef struct shared_hint_stream_header_s {
408
    unsigned int FirstSharedObject;
409
    gs_offset_t FirstObjectOffset;
410
    unsigned int FirstPageEntries;
411
    unsigned int NumSharedObjects;
412
    /* Item 5 is always 1 as far as we are concerned */
413
    unsigned int LeastObjectLength;
414
    unsigned int MostObjectLength;
415
    unsigned short LengthNumBits;
416
} shared_hint_stream_header_t;
417
418
typedef struct share_hint_stream_s {
419
    unsigned int ObjectNumber;
420
    gs_offset_t ObjectOffset;
421
    unsigned int ObjectLength;   /* biased by the LeastObjectLength */
422
    /* item 2 is always 0 */
423
    /* Which means that item 3 is never present */
424
    /* Finally item 4 is always 0 (1 less than the number of objects in the group, which is always 1) */
425
} shared_hint_stream_t;
426
427
typedef struct pdf_linearisation_s {
428
    gp_file *sfile;
429
    pdf_temp_file_t Lin_File;
430
    char HintBuffer[256];
431
    unsigned char HintBits;
432
    unsigned char HintByte;
433
    int64_t Catalog_id;
434
    int64_t Info_id;
435
    int64_t Pages_id;
436
    int64_t NumPage1Resources;
437
    int64_t NumPart1StructureResources;
438
    int64_t NumSharedResources;
439
    int64_t NumUniquePageResources;
440
    int64_t NumPart9Resources;
441
    int64_t NumNonPageResources;
442
    int64_t LastResource;
443
    int64_t MainFileEnd;
444
    gs_offset_t *Offsets;
445
    gs_offset_t xref;
446
    gs_offset_t FirstxrefOffset;
447
    gs_offset_t FirsttrailerOffset;
448
    gs_offset_t LDictOffset;
449
    gs_offset_t FileLength;
450
    gs_offset_t T;
451
    gs_offset_t E;
452
    page_hint_stream_header_t PageHintHeader;
453
    int NumPageHints;
454
    page_hint_stream_t *PageHints;
455
    shared_hint_stream_header_t SharedHintHeader;
456
    int NumSharedHints;
457
    shared_hint_stream_t *SharedHints;
458
} pdf_linearisation_t;
459
460
/* These are the values for 'PageUsage' above, values > 0 indicate the page number that uses the resource */
461
0
#define resource_usage_not_referenced 0
462
0
#define resource_usage_page_shared -1
463
/* Thses need to be lower than the shared value */
464
23.7k
#define resource_usage_part1_structure -2
465
71.5k
#define resource_usage_part9_structure -3
466
#define resource_usage_written -4
467
468
/*
469
 * Define the structure for PDF font cache element.
470
 */
471
typedef struct pdf_font_cache_elem_s pdf_font_cache_elem_t;
472
struct pdf_font_cache_elem_s {
473
    pdf_font_cache_elem_t *next;
474
    gs_id font_id;
475
    int num_chars;                /* safety purpose only */
476
    int num_widths;                /* safety purpose only */
477
    struct pdf_font_resource_s *pdfont;
478
    byte *glyph_usage;
479
    double *real_widths;        /* [count] (not used for Type 0) */
480
};
481
482
#define private_st_pdf_font_cache_elem()\
483
    gs_private_st_ptrs4(st_pdf_font_cache_elem, pdf_font_cache_elem_t,\
484
        "pdf_font_cache_elem_t", pdf_font_cache_elem_enum,\
485
        pdf_font_cache_elem_reloc, next, pdfont,\
486
        glyph_usage, real_widths)
487
488
/*
489
 * pdf_viewer_state tracks the graphic state of a viewer,
490
 * which would interpret the generated PDF file
491
 * immediately when it is generated.
492
 */
493
typedef struct pdf_viewer_state_s {
494
    int transfer_not_identity;        /* bitmask */
495
    gs_id transfer_ids[4];
496
    float strokeconstantalpha;
497
    float fillconstantalpha;
498
    bool alphaisshape;
499
    gs_blend_mode_t blend_mode; /* state.blend_mode */
500
    gs_id halftone_id;
501
    gs_id black_generation_id;
502
    gs_id undercolor_removal_id;
503
    int overprint_mode;
504
    float smoothness; /* state.smoothness */
505
    float flatness;
506
    bool text_knockout; /* state.text_knockout */
507
    bool fill_overprint;
508
    bool stroke_overprint;
509
    int stroke_adjust; /* state.stroke_adjust */
510
    bool fill_used_process_color;
511
    bool stroke_used_process_color;
512
    gx_hl_saved_color saved_fill_color;
513
    gx_hl_saved_color saved_stroke_color;
514
    gx_line_params line_params;
515
    float *dash_pattern;
516
    uint dash_pattern_size;
517
    gs_id soft_mask_id;
518
    gx_path *clip_path;
519
    gs_id clip_path_id;
520
} pdf_viewer_state;
521
522
/*
523
 * Define a structure for saving context when entering
524
 * a contents stream accumulation mode (charproc, Type 1 pattern).
525
 */
526
typedef struct pdf_substream_save_s {
527
    pdf_context_t        context;
528
    pdf_text_state_t        *text_state;
529
    gx_path                *clip_path;
530
    gs_id                clip_path_id;
531
    int                        vgstack_bottom;
532
    stream                *strm;
533
    cos_dict_t                *substream_Resources;
534
    pdf_procset_t        procsets;
535
    bool                skip_colors;
536
    pdf_resource_t      *font3;
537
    pdf_resource_t        *accumulating_substream_resource;
538
    bool                charproc_just_accumulated;
539
    bool                accumulating_a_global_object;
540
    pdf_resource_t      *pres_soft_mask_dict;
541
    gs_const_string                objname;
542
    int                        last_charpath_op;
543
} pdf_substream_save;
544
545
#define private_st_pdf_substream_save()\
546
    gs_private_st_strings1_ptrs7(st_pdf_substream_save, pdf_substream_save,\
547
        "pdf_substream_save", pdf_substream_save_enum,\
548
        pdf_substream_save_reloc, objname, text_state, clip_path, strm, \
549
        substream_Resources, font3, accumulating_substream_resource, pres_soft_mask_dict)
550
#define private_st_pdf_substream_save_element()\
551
  gs_private_st_element(st_pdf_substream_save_element, pdf_substream_save,\
552
    "pdf_substream_save[]", pdf_substream_save_elt_enum_ptrs,\
553
    pdf_substream_save_elt_reloc_ptrs, st_pdf_substream_save)
554
555
typedef enum {
556
    pdf_compress_none,
557
    pdf_compress_LZW,        /* not currently used, thanks to Unisys */
558
    pdf_compress_Flate,
559
    pdf_compress_Brotli
560
} pdf_compression_type;
561
562
typedef enum {
563
    OCR_UnInit,
564
    OCR_Rendering,
565
    OCR_Rendered,
566
    OCR_UnicodeAvailable,
567
    OCR_Failed
568
} pdf_OCR_stage;
569
570
typedef enum {
571
    UseOCRNever,
572
    UseOCRAsNeeded,
573
    UseOCRAlways
574
} pdf_OCR_usage;
575
576
typedef struct ocr_glyph_s{
577
    byte *data;
578
    int x;
579
    int y;
580
    int width;
581
    int height;
582
    int raster;
583
    void *next;
584
    gs_char char_code;
585
    gs_glyph glyph;
586
    bool is_space;
587
} ocr_glyph_t;
588
589
/* Define the device structure. */
590
struct gx_device_pdf_s {
591
    gx_device_psdf_common;
592
    gs_font_dir *pdf_font_dir;  /* Our own font cache, so that PCL can free its own ones, we set our font copeis to use this */
593
                                /* see 'pdf_free_pdf_font_cache' in gdevpdf.c for more details. */
594
    bool is_ps2write;          /* ps2write (true) versus pdfwrite (false); never changed */
595
    /* PDF-specific distiller parameters */
596
    double CompatibilityLevel;
597
    int EndPage;
598
    int StartPage;
599
    bool Optimize;
600
    bool ParseDSCCommentsForDocInfo;
601
    bool ParseDSCComments;
602
    bool EmitDSCWarnings;
603
    bool CreateJobTicket;
604
    bool PreserveEPSInfo;
605
    bool AutoPositionEPSFiles;
606
    bool PreserveCopyPage;
607
    bool UsePrologue;
608
    int OffOptimizations;
609
    /* End of distiller parameters */
610
    /* PDF/X parameters */
611
    gs_param_float_array PDFXTrimBoxToMediaBoxOffset;
612
    gs_param_float_array PDFXBleedBoxToTrimBoxOffset;
613
    bool PDFXSetBleedBoxToMediaBox;
614
    /* OCR Parameters */
615
    char ocr_language[1024];
616
    int ocr_engine;
617
    /* Other parameters */
618
    bool ReAssignCharacters;
619
    bool ReEncodeCharacters;
620
    int64_t FirstObjectNumber;
621
    bool CompressFonts;
622
    bool CompressStreams;
623
    bool PrintStatistics;
624
    gs_param_string DocumentUUID;
625
    gs_param_string InstanceUUID;
626
    int DocumentTimeSeq;
627
    bool ForOPDFRead;          /* PS2WRITE only. */
628
    bool Eps2Write;            /* EPS2WRITE only */
629
    bool CompressEntireFile;  /* PS2WRITE only. */
630
    bool ResourcesBeforeUsage; /* PS2WRITE only. */
631
    bool HavePDFWidths;        /* PS2WRITE only. */
632
    bool HaveStrokeColor;      /* PS2WRITE only. */
633
    bool ProduceDSC;               /* PS2WRITE only. */
634
    bool HaveTransparency;
635
    bool PatternImagemask; /* The target viewer|printer handles imagemask
636
                              with pattern color. */
637
    int PDFX;                   /* Generate PDF/X */
638
    int PDFA;                   /* Generate PDF/A 0 = don't produce, otherwise level of PDF/A */
639
    bool AbortPDFAX;            /* Abort generation of PDFA or X, produce regular PDF */
640
    int64_t MaxClipPathSize;  /* The maximal number of elements of a clipping path
641
                              that the target viewer|printer can handle. */
642
    int64_t MaxShadingBitmapSize; /* The maximal number of bytes in
643
                              a bitmap representation of a shading.
644
                              (Bigger shadings to be downsampled). */
645
    int64_t MaxInlineImageSize;
646
    /* Encryption parameters */
647
    gs_param_string OwnerPassword;
648
    gs_param_string UserPassword;
649
    uint KeyLength;
650
    int32_t Permissions;
651
    uint EncryptionR;
652
    gs_param_string NoEncrypt;
653
    bool EncryptMetadata;
654
    /* End of parameters */
655
    bool ComputeDocumentDigest; /* Developer needs only; Always true in production. */
656
    /* Encryption data */
657
    byte EncryptionO[32];
658
    byte EncryptionU[32];
659
    byte EncryptionKey[16];
660
    uint EncryptionV;
661
    /* Values derived from DSC comments */
662
    bool is_EPS;
663
    int AccumulatingBBox;
664
    gs_rect BBox;
665
    pdf_page_dsc_info_t doc_dsc_info; /* document default */
666
    pdf_page_dsc_info_t page_dsc_info; /* current page */
667
    /* Additional graphics state */
668
    bool fill_overprint, stroke_overprint;
669
    int rendering_intent;
670
    bool user_icc;
671
    bool remap_fill_color, remap_stroke_color;
672
    gs_id halftone_id;
673
    gs_id transfer_ids[4];
674
    int transfer_not_identity;        /* bitmask */
675
    gs_id black_generation_id, undercolor_removal_id;
676
    /* Following are set when device is opened. */
677
    pdf_compression_type compression;
678
    pdf_compression_type compression_at_page_start;
679
    /* pdf_memory is 'stable' memory, it is not subject to save and restore
680
     * and is the allocator which should be used for pretty much ewverything
681
     */
682
9.44M
#define pdf_memory v_memory
683
    /*
684
     * The xref temporary file is logically an array of longs.
685
     * xref[id - FirstObjectNumber] is the position in the output file
686
     * of the object with the given id.
687
     *
688
     * Note that xref, unlike the other temporary files, does not have
689
     * an associated stream or stream buffer.
690
     */
691
    pdf_temp_file_t xref;
692
    /*
693
     * asides holds resources and other "aside" objects.  It is
694
     * copied verbatim to the output file at the end of the document.
695
     */
696
    pdf_temp_file_t asides;
697
    /*
698
     * streams holds data for stream-type Cos objects.  The data is
699
     * copied to the output file at the end of the document.
700
     *
701
     * Note that streams.save_strm is not used, since we don't interrupt
702
     * normal output when saving stream data.
703
     */
704
    pdf_temp_file_t streams;
705
    /*
706
     * ObjStm holds a tream of objects being stored in an ObjStm, up to MAX_OBJSTM_OBJECTS,
707
     * We keep a record of the offset of each object within the stream in ObjStmOffsets, so
708
     * that later on we can write the offset within the stream for each object into the
709
     * beginning of the ObjStm. NumObjStmOffsets is used to keep track of how many we have,
710
     * and ObjStm_id is the id of the stream which we'll eventually store in the 'asides'
711
     * file, containing the ObjStm.
712
     */
713
    pdf_temp_file_t ObjStm;
714
    int64_t ObjStm_id;
715
    gs_offset_t *ObjStmOffsets;
716
    int NumObjStmObjects;
717
    bool doubleXref;
718
719
    /* ................ */
720
    int64_t next_id;
721
    /* The following 3 objects, and only these, are allocated */
722
    /* when the file is opened. */
723
    cos_dict_t *Catalog;
724
    cos_dict_t *Info;
725
    cos_dict_t *Pages;
726
0
#define pdf_num_initial_ids 3
727
    int64_t outlines_id;
728
    int next_page;
729
    int max_referred_page;
730
    int64_t contents_id;
731
    pdf_context_t context;
732
    int64_t contents_length_id;
733
    gs_offset_t contents_pos;
734
    pdf_procset_t procsets;        /* used on this page */
735
    pdf_text_data_t *text;
736
    pdf_text_rotation_t text_rotation;
737
78.9k
#define initial_num_pages 50
738
    pdf_page_t *pages;
739
    int num_pages;
740
    uint64_t used_mask;                /* for where_used: page level = 1 */
741
    pdf_resource_list_t resources[NUM_RESOURCE_TYPES];
742
    /* cs_Patterns[0] is colored; 1,3,4 are uncolored + Gray,RGB,CMYK */
743
    pdf_resource_t *cs_Patterns[5];
744
    pdf_resource_t *Identity_ToUnicode_CMaps[2]; /* WMode = 0,1 */
745
    pdf_resource_t *last_resource;
746
    pdf_resource_t *OneByteIdentityH;
747
    gs_id IdentityCIDSystemInfo_id;
748
    pdf_outline_level_t *outline_levels;
749
    int outline_depth;
750
    int max_outline_depth;
751
    int closed_outline_depth;
752
    int outlines_open;
753
    pdf_article_t *articles;
754
    cos_dict_t *Dests;
755
    cos_dict_t *EmbeddedFiles;
756
    cos_array_t *AF;
757
    byte fileID[16];
758
    /* Use a single time moment for all UUIDs to minimize an indeterminizm. */
759
    /* This needs to be a long because we call the platform code gp_get_realtime
760
     * to fill it in, and that takes a long *. Be aware that long is 32-bits on
761
     * 32-bit Windows and 64-bits on 64-bit Windows.
762
     */
763
    long uuid_time[2];
764
    /*
765
     * global_named_objects holds named objects that are independent of
766
     * the current namespace: {Catalog}, {DocInfo}, {Page#}, {ThisPage},
767
     * {PrevPage}, {NextPage}.
768
     */
769
    cos_dict_t *global_named_objects;
770
    /*
771
     * local_named_objects holds named objects in the current namespace.
772
     */
773
    cos_dict_t *local_named_objects;
774
    /*
775
     * NI_stack is a last-in, first-out stack in which each element is a
776
     * (named) cos_stream_t object that eventually becomes the object of an
777
     * image XObject resource.
778
     */
779
    cos_array_t *NI_stack;
780
    /*
781
     * Namespace_stack is a last-in, first-out stack in which each pair of
782
     * elements is, respectively, a saved value of local_named_objects and
783
     * a saved value of NI_stack.  (The latter is not documented by Adobe,
784
     * but it was confirmed by them.)
785
     */
786
    cos_array_t *Namespace_stack;
787
    pdf_font_cache_elem_t *font_cache;
788
    /*
789
     * char_width is used by pdf_text_set_cache to communicate
790
     * with assign_char_code around gdev_pdf_fill_mask.
791
     */
792
    gs_point char_width;
793
    /*
794
     * We need a stable copy of clipping path to prevent writing
795
     * redundant clipping paths when PS document generates such ones.
796
     */
797
    gx_path *clip_path;
798
799
    /* Used for preserving text rendering modes with clip. */
800
    bool clipped_text_pending;
801
    int saved_vgstack_bottom_for_textclip;
802
    int saved_vgstack_depth_for_textclip;
803
804
    /*
805
     * Page labels.
806
     */
807
    cos_array_t *PageLabels;
808
    int PageLabels_current_page;
809
    cos_dict_t *PageLabels_current_label;
810
    /*
811
     * The following is a dangerous pointer, which pdf_text_process
812
     * uses to communicate with assign_char_code.
813
     * It is a pointer from global memory to local memory.
814
     * The garbager must not proceess this pointer, and it must
815
     * not be listed in st_device_pdfwrite.
816
     * It's life time terminates on garbager invocation.
817
     */
818
    gs_text_enum_t *pte;
819
    /*
820
     * The viewer's graphic state stack.
821
     */
822
    pdf_viewer_state *vgstack;
823
    int vgstack_size;
824
    int vgstack_depth;
825
    int vgstack_bottom;                 /* Stack bottom for the current substream. */
826
    pdf_viewer_state vg_initial; /* Initial values for viewer's graphic state */
827
    bool vg_initial_set;
828
829
    /* The substream context stack. */
830
    int sbstack_size;
831
    int sbstack_depth;
832
    pdf_substream_save *sbstack;
833
834
    /* Temporary workaround. The only way to get forms out of pdfwrite at present
835
     * is via a transparency group or mask operation. Ordinarily we don't care
836
     * much about forms, but Patterns within forms need to be scaled to the
837
     * CTM of the Pattern, not the default page co-ordinate system. We use
838
     * this value to know if we are nested inside a form or not. If we are
839
     * we don't undo the resolution co-ordinate transform.
840
     */
841
    int FormDepth;
842
843
    /* Determine if we have a high level form. We want to do things differently
844
     * sometimes, if we are capturing a form
845
     */
846
    int HighLevelForm;
847
848
    /* Nasty hack. OPDFread.ps resets the graphics state to the identity before
849
     * replaying the Pattern PaintProc, but if the Pattern is nested inside a
850
     * previous pattern, this doesn't work. We use this to keep track of whether
851
     * we are nested, and if we are (and are ps2write, not pdfwrite) we track the
852
     * pattern CTM below.
853
     */
854
    int PatternDepth;
855
    gs_matrix AccumulatedPatternMatrix;
856
857
    /* Normally the PDF itnerpreter doesn't call execform to pass a From XObject to
858
     * pdfwrite, but it *does* create forms (and increment FormDepth) for transparent
859
     * Groups, which are handled as Form XObjects. Because of the way that Pattterns work,
860
     * the first pattern after a Form uses the Form co-ordinate system, but if the Pattern
861
     * itself uses Patterns, then the nested Pattern needs to use the co-ordinate system
862
     * of the parent Pattern. Unless, of course, we have another Form!
863
     * So essentially we need to know the Pattern depth, since the last form was executed.
864
     * If its 0, then we want to apply the current CTM, if its more than that then we want
865
     * to undo the pdfwrite scaling (see gdevpdfv.c, pdf_store_pattern1_params() at about line
866
     * 239.
867
     */
868
    int PatternsSinceForm;
869
870
    /* Accessories */
871
    cos_dict_t *substream_Resources;     /* Substream resources */
872
    gs_color_space_index pcm_color_info_index; /* Index of the ProcessColorModel space. */
873
    bool skip_colors; /* Skip colors while a pattern/charproc accumulation. */
874
    bool AR4_save_bug; /* See pdf_put_uncolored_pattern */
875
    pdf_resource_t *font3; /* The owner of the accumulated charstring. */
876
    pdf_resource_t *accumulating_substream_resource;
877
    gs_matrix_fixed charproc_ctm;
878
    bool accumulating_charproc;
879
    gs_rect charproc_BBox;
880
    bool charproc_just_accumulated; /* A flag for controlling
881
                        the glyph variation recognition.
882
                        Used only with uncached charprocs. */
883
    bool PS_accumulator; /* A flag to determine whether a given
884
                         accumulator is for a PostScript type 3 font or not. */
885
    int32_t Scaled_accumulator; /* We scale teh CTM when accumulating type 3 fonts */
886
    bool accumulating_a_global_object; /* ps2write only.
887
                        Accumulating a global object (such as a named Form,
888
                        so that resources used in it must also be global.
889
                        Important for viewers with small memory,
890
                        which drops resources per page. */
891
    const pdf_char_glyph_pairs_t *cgp; /* A temporary pointer
892
                        for pdf_is_same_charproc1.
893
                        Must be NULL when the garbager is invoked,
894
                        because it points from global to local memory. */
895
    int substituted_pattern_count;
896
    int substituted_pattern_drop_page;
897
    /* Temporary data for use_image_as_pattern,
898
       They pass an information about a mask of a masked image,
899
       (which is being converted into a pattern)
900
       between 2 consecutive calls to pdf_image_end_image_data. */
901
    gs_id     image_mask_id;
902
    bool      image_mask_is_SMask;
903
    bool      image_mask_skip; /* A flag for pdf_begin_transparency_mask */
904
    bool      smask_construction; /* True when pdfwrite is constructing a soft mask */
905
    uint      image_with_SMask; /* A flag for pdf_begin_transparency_group. In order to
906
                                 * deal with nested groups we set/test the bit according
907
                                 * to the FormDepth
908
                                 */
909
    gs_matrix converting_image_matrix;
910
    double    image_mask_scale;
911
    /* Temporary data for soft mask form. */
912
    pdf_resource_t *pres_soft_mask_dict;
913
    /* used to track duplicate stream definitions in pdfmarks */
914
    bool pdfmark_dup_stream;
915
    /* Temporary data for pdfmark_BP. */
916
    gs_const_string objname;
917
    int OPDFRead_procset_length;      /* PS2WRITE only. */
918
    void *find_resource_param; /* WARNING : not visible for garbager. */
919
    int last_charpath_op; /* true or false state of last charpath */
920
    bool type3charpath;
921
    bool SetPageSize;
922
    bool RotatePages;
923
    bool FitPages;
924
    bool CenterPages;
925
    bool DoNumCopies;
926
    bool PreserveSeparation;
927
    bool PreserveDeviceN;
928
    int PDFACompatibilityPolicy;
929
    bool DetectDuplicateImages;
930
    bool AllowIncrementalCFF;
931
    bool WantsToUnicode;
932
    bool PdfmarkCapable;
933
    bool WantsPageLabels;
934
    bool WantsOptionalContent;
935
    bool AllowPSRepeatFunctions;
936
    bool IsDistiller;
937
    bool PreserveSMask;
938
    bool PreserveTrMode;
939
    bool NoT3CCITT;                 /* A bug in Brother printers causes CCITTFaxDecode
940
                                     * to fail, especially with small amounts of data.
941
                                     * This parameter is present only to allow
942
                                     * ps2write output to work on those pritners.
943
                                     */
944
    bool Linearise;                 /* Whether to Linearizse the file, the next 2 parameter
945
                                     * are only used if this is true.
946
                                     */
947
    pdf_linearisation_record_t
948
        *ResourceUsage;             /* An array, one per resource defined to date, which
949
                                     * contains either -2 (shared on multiple pages), -1
950
                                     * (structure object, eg catalog), 0 (not used on a page
951
                                     * or the page number. This does limit us to a mere 2^31
952
                                     * pages
953
                                     */
954
    int ResourceUsageSize;          /* Size of the above array, currently */
955
    bool InOutputPage;              /* Used when closing the file, if this is true then we were
956
                                     * called from output_page and should emit a page even if there
957
                                     * are no marks. If false, then we probably were called from
958
                                     * closedevice and, if there are no marks, we should delete
959
                                     * the last file *if* we are emitting one file per page.
960
                                     */
961
    bool FlattenFonts;
962
    int LastFormID;
963
    char *ExtensionMetadata;        /* If present the non-standard pdfmark Extension_Metadata has been
964
                                     * used, the content of ths string is written out as part of the
965
                                     * metadata referenced by the Catalog.
966
                                     */
967
    char *PDFFormName;              /* If present, we are processing (or about to process) a Form XObject
968
                                     * which we wish to handle as a Form XObject, not flatten. Currently
969
                                     * this is only the case for Annotation Appearances. This must be NULL
970
                                     * after the form is processed. The name will be used to create a
971
                                     * local named object which pdfmark can reference.
972
                                     */
973
    stream *PassThroughWriter;      /* A copy of the stream that the image enumerator points to, if we are
974
                                     * doing JPEG pass through we write the JPEG data here, and don't write
975
                                     * anything in the image processing routines.
976
                                     */
977
    float UserUnit;
978
    pdf_OCR_usage UseOCR;           /* Never, AsNeeded or Always */
979
    gs_text_enum_t* OCRSaved;       /* Saved state of the text enumerator before rendering glyph bitmaps for later OCR */
980
    pdf_OCR_stage OCRStage;         /* Used to control a (sort of) state machine when using OCR to get a Unicode value for a glyph */
981
    int *OCRUnicode;                /* Used to pass back the Unicode value from the OCR engine to the text processing */
982
    gs_char OCR_char_code;          /* Passes the current character code from text processing to the image processing code when rendering glyph bitmaps for OCR */
983
    gs_glyph OCR_glyph;             /* Passes the current glyph code from text processing to the image processing code when rendering glyph bitmaps for OCR */
984
    gs_text_enum_t *OCR_enum;       /* We need this to update the OCR char_code and glyph in gdev_pdf_fill_mask() when rendering a glyph for OCR, when using PDF input */
985
    ocr_glyph_t *ocr_glyphs;        /* Records bitmaps and other data from text processing when doing OCR */
986
    gs_gstate **initial_pattern_states;
987
    bool OmitInfoDate;              /* If true, do not emit CreationDate and ModDate in the Info dictionary and XMP Metadata (must not be true for PDF/X support) */
988
    bool OmitXMP;                   /* If true, do not emit an XMP /Metadata block and do not reference it from the Catalog (must not be true for PDF/A output) */
989
    bool OmitID;                    /* If true, do not emit a /ID array in the trailer dicionary (must not be true for encrypted files or PDF 2.0) */
990
    bool ModifiesPageSize;          /* If true, the new PDF interpreter will not preserve *Box values (the media size has been modified, they will be incorrect) */
991
    bool ModifiesPageOrder;         /* If true, the new PDF interpreter will not preserve Outlines or Dests, because they will refer to the wrong page number */
992
    bool WriteXRefStm;              /* If true, (the default) use an XRef stream rather than an xref table */
993
    bool WriteObjStms;              /* If true, (the default) store candidate objects in ObjStms rather than plain text in the PDF file. */
994
    char *PendingOC;                /* An OptionalContent object is pending, the string is the name of the (already defined) object  */
995
    bool ToUnicodeForStdEnc;        /* Should we emit ToUnicode CMaps when a simple font has only standard glyph names. Defaults to true */
996
    bool EmbedSubstituteFonts;      /* When we use a substitute font to replace a missing font, should we embed it in the output */
997
    bool UseBrotli;                 /* Use Brotli compression in place of Flate */
998
};
999
1000
#define is_in_page(pdev)\
1001
23.3M
  ((pdev)->contents_id != 0)
1002
1003
/* Enumerate the individual pointers in a gx_device_pdf.
1004
   I disliked this macro and so its been removed, the pointers are
1005
   enumerated/relocated in gdevpdf.c now. We still need the gx_device_pdf_num_ptrs
1006
   though, so I'm maintaining this comment just to keep track of the number
1007
   of pointers.
1008
 */
1009
/*#define gx_device_pdf_do_ptrs(m)\
1010
 m(0,asides.strm) m(1,asides.strm_buf) m(2,asides.save_strm)\
1011
 m(3,streams.strm) m(4,streams.strm_buf)\
1012
 m(5,pictures.strm) m(6,pictures.strm_buf) m(7,pictures.save_strm)\
1013
 m(8,Catalog) m(9,Info) m(10,Pages)\
1014
 m(11,text) m(12,pages)\
1015
 m(13,cs_Patterns[0])\
1016
 m(14,cs_Patterns[1]) m(15,cs_Patterns[3]) m(16,cs_Patterns[4])\
1017
 m(17,last_resource)\
1018
 m(18,articles) m(19,Dests) m(20,global_named_objects)\
1019
 m(21, local_named_objects) m(22,NI_stack) m(23,Namespace_stack)\
1020
 m(24,font_cache) m(25,clip_path)\
1021
 m(26,PageLabels) m(27,PageLabels_current_label)\
1022
 m(28,sbstack) m(29,substream_Resources) m(30,font3)\
1023
 m(31,accumulating_substream_resource) \
1024
 m(32,pres_soft_mask_dict) m(33,PDFXTrimBoxToMediaBoxOffset.data)\
1025
 m(34,PDFXBleedBoxToTrimBoxOffset.data)
1026
 m(35,Identity_ToUnicode_CMaps[0]) m(36,Identity_ToUnicode_CMaps[1])\
1027
 m(37,vgstack)\
1028
 m(38, outline_levels)
1029
 m(39, gx_device_pdf, EmbeddedFiles);
1030
 m(40, gx_device_pdf, pdf_font_dir);
1031
 m(41, gx_device_pdf, Extension_Metadata);*/
1032
114M
#define gx_device_pdf_num_ptrs 44
1033
#define gx_device_pdf_do_param_strings(m)\
1034
825k
    m(0, OwnerPassword) m(1, UserPassword) m(2, NoEncrypt)\
1035
825k
    m(3, DocumentUUID) m(4, InstanceUUID)
1036
114M
#define gx_device_pdf_num_param_strings 5
1037
#define gx_device_pdf_do_const_strings(m)\
1038
825k
    m(0, objname)
1039
#define gx_device_pdf_num_const_strings 1
1040
1041
#define private_st_device_pdfwrite()        /* in gdevpdf.c */\
1042
  gs_private_st_composite_final(st_device_pdfwrite, gx_device_pdf,\
1043
    "gx_device_pdf", device_pdfwrite_enum_ptrs, device_pdfwrite_reloc_ptrs,\
1044
    device_pdfwrite_finalize)
1045
1046
/* ================ Driver procedures ================ */
1047
1048
    /* In gdevpdfb.c */
1049
dev_proc_copy_mono(gdev_pdf_copy_mono);
1050
dev_proc_copy_color(gdev_pdf_copy_color);
1051
dev_proc_fill_mask(gdev_pdf_fill_mask);
1052
dev_proc_strip_tile_rectangle(gdev_pdf_strip_tile_rectangle);
1053
    /* In gdevpdfd.c */
1054
extern const gx_device_vector_procs pdf_vector_procs;
1055
dev_proc_fill_rectangle(gdev_pdf_fill_rectangle);
1056
dev_proc_fill_path(gdev_pdf_fill_path);
1057
dev_proc_stroke_path(gdev_pdf_stroke_path);
1058
dev_proc_fill_stroke_path(gdev_pdf_fill_stroke_path);
1059
dev_proc_fillpage(gdev_pdf_fillpage);
1060
    /* In gdevpdfi.c */
1061
dev_proc_begin_typed_image(gdev_pdf_begin_typed_image);
1062
    /* In gdevpdfp.c */
1063
dev_proc_get_params(gdev_pdf_get_params);
1064
dev_proc_put_params(gdev_pdf_put_params);
1065
    /* In gdevpdft.c */
1066
dev_proc_text_begin(gdev_pdf_text_begin);
1067
dev_proc_fill_rectangle_hl_color(gdev_pdf_fill_rectangle_hl_color);
1068
    /* In gdevpdfv.c */
1069
dev_proc_include_color_space(gdev_pdf_include_color_space);
1070
    /* In gdevpdft.c */
1071
dev_proc_composite(gdev_pdf_composite);
1072
dev_proc_begin_transparency_group(gdev_pdf_begin_transparency_group);
1073
dev_proc_end_transparency_group(gdev_pdf_end_transparency_group);
1074
dev_proc_begin_transparency_mask(gdev_pdf_begin_transparency_mask);
1075
dev_proc_end_transparency_mask(gdev_pdf_end_transparency_mask);
1076
dev_proc_dev_spec_op(gdev_pdf_dev_spec_op);
1077
1078
/* ================ Utility procedures ================ */
1079
1080
/* ---------------- Exported by gdevpdf.c ---------------- */
1081
1082
/* Initialize the IDs allocated at startup. */
1083
void pdf_initialize_ids(gx_device_pdf * pdev);
1084
1085
/* Update the color mapping procedures after setting ProcessColorModel. */
1086
void pdf_set_process_color_model(gx_device_pdf * pdev, int index);
1087
1088
/* Reset the text state parameters to initial values. */
1089
void pdf_reset_text(gx_device_pdf *pdev);
1090
1091
/* ---------------- Exported by gdevpdfu.c ---------------- */
1092
1093
/* ------ Document ------ */
1094
1095
/* Utility functions to write args into the file as comments */
1096
int pdfwrite_fwrite_args_comment(gx_device_pdf *pdev, gp_file *f);
1097
int pdfwrite_write_args_comment(gx_device_pdf *pdev, stream *s);
1098
1099
/* Write a DSC compliant header to the file */
1100
int ps2write_dsc_header(gx_device_pdf * pdev, int pages);
1101
1102
/* Open the document if necessary. */
1103
int pdfwrite_pdf_open_document(gx_device_pdf * pdev);
1104
1105
/* ------ Objects ------ */
1106
1107
/* Allocate an ID for a future object, set its pos=0 so we can tell if it is used */
1108
int64_t pdf_obj_forward_ref(gx_device_pdf * pdev);
1109
1110
/* Allocate an ID for a future object. */
1111
int64_t pdf_obj_ref(gx_device_pdf * pdev);
1112
1113
/* Remove an object from the xref table (mark as unused) */
1114
int64_t pdf_obj_mark_unused(gx_device_pdf *pdev, int64_t id);
1115
1116
/* Read the current position in the output stream. */
1117
gs_offset_t pdf_stell(gx_device_pdf * pdev);
1118
1119
/* Begin an object, optionally allocating an ID. */
1120
int64_t pdf_open_obj(gx_device_pdf * pdev, int64_t id, pdf_resource_type_t type);
1121
int64_t pdf_begin_obj(gx_device_pdf * pdev, pdf_resource_type_t type);
1122
1123
/* End an object. */
1124
int pdf_end_obj(gx_device_pdf * pdev, pdf_resource_type_t type);
1125
1126
/* ------ Page contents ------ */
1127
1128
/* Open a page contents part. */
1129
/* Return an error if the page has too many contents parts. */
1130
int pdf_open_contents(gx_device_pdf * pdev, pdf_context_t context);
1131
1132
/* Close the current contents part if we are in one. */
1133
int pdf_close_contents(gx_device_pdf * pdev, bool last);
1134
1135
/* ------ Resources et al ------ */
1136
1137
extern const char *const pdf_resource_type_names[];
1138
extern const gs_memory_struct_type_t *const pdf_resource_type_structs[];
1139
1140
/* Record usage of resoruces by pages */
1141
int pdf_record_usage(gx_device_pdf *const pdev, int64_t resource_id, int page_num);
1142
int pdf_record_usage_by_parent(gx_device_pdf *const pdev, int64_t resource_id, int64_t parent);
1143
1144
/*
1145
 * Define the offset that indicates that a file position is in the
1146
 * asides file rather than the main (contents) file. We just use the top bit
1147
 * as a flag. Complexity is due to ubsan and the possibility we have 32-bit offset type.
1148
 */
1149
#ifdef ARCH_SIZEOF_GS_OFFSET_T
1150
# if ARCH_SIZEOF_GS_OFFSET_T == 8
1151
2.63M
#define ASIDES_BASE_POSITION ((uint64_t)1 << ((sizeof(gs_offset_t) * 8) - 1))
1152
# elif ARCH_SIZEOF_GS_OFFSET_T == 4
1153
#define ASIDES_BASE_POSITION ((uint32_t)1 << ((sizeof(gs_offset_t) * 8) - 1))
1154
# else
1155
UNSUPPORTED
1156
# endif
1157
# else
1158
UNSUPPORTED
1159
# endif
1160
1161
/* Begin an object logically separate from the contents. */
1162
/* (I.e., an object in the resource file.) */
1163
int64_t pdf_open_separate(gx_device_pdf * pdev, int64_t id, pdf_resource_type_t type);
1164
int64_t pdf_begin_separate(gx_device_pdf * pdev, pdf_resource_type_t type);
1165
1166
/* functions used for ObjStm writing */
1167
int FlushObjStm(gx_device_pdf *pdev);
1168
int NewObjStm(gx_device_pdf *pdev);
1169
int64_t pdf_open_separate_noObjStm(gx_device_pdf * pdev, int64_t id, pdf_resource_type_t type);
1170
int pdf_end_separate_noObjStm(gx_device_pdf * pdev, pdf_resource_type_t type);
1171
1172
/* Reserve object id. */
1173
void pdf_reserve_object_id(gx_device_pdf * pdev, pdf_resource_t *ppres, int64_t id);
1174
1175
/* Begin an aside (resource, annotation, ...). */
1176
int pdf_alloc_aside(gx_device_pdf * pdev, pdf_resource_t ** plist,
1177
                const gs_memory_struct_type_t * pst, pdf_resource_t **ppres,
1178
                int64_t id);
1179
/* Begin an aside (resource, annotation, ...). */
1180
int64_t pdf_begin_aside(gx_device_pdf * pdev, pdf_resource_t **plist,
1181
                    const gs_memory_struct_type_t * pst,
1182
                    pdf_resource_t **ppres, pdf_resource_type_t type);
1183
1184
/* Begin a resource of a given type. */
1185
int pdf_begin_resource(gx_device_pdf * pdev, pdf_resource_type_t rtype,
1186
                       gs_id rid, pdf_resource_t **ppres);
1187
1188
/* Begin a resource body of a given type. */
1189
int pdf_begin_resource_body(gx_device_pdf * pdev, pdf_resource_type_t rtype,
1190
                            gs_id rid, pdf_resource_t **ppres);
1191
1192
/* Allocate a resource, but don't open the stream. */
1193
int pdf_alloc_resource(gx_device_pdf * pdev, pdf_resource_type_t rtype,
1194
                       gs_id rid, pdf_resource_t **ppres, int64_t id);
1195
1196
/* Find same resource. */
1197
int pdf_find_same_resource(gx_device_pdf * pdev,
1198
        pdf_resource_type_t rtype, pdf_resource_t **ppres,
1199
        int (*eq)(gx_device_pdf * pdev, pdf_resource_t *pres0, pdf_resource_t *pres1));
1200
1201
/* Find resource by resource id. */
1202
pdf_resource_t *pdf_find_resource_by_resource_id(gx_device_pdf * pdev,
1203
                                                pdf_resource_type_t rtype, gs_id id);
1204
1205
/* Find a resource of a given type by gs_id. */
1206
pdf_resource_t *pdf_find_resource_by_gs_id(gx_device_pdf * pdev,
1207
                                           pdf_resource_type_t rtype,
1208
                                           gs_id rid);
1209
1210
/* Remove a resource from a chain of resources but do not free it. The resource will
1211
 * have to be listed elsewhere. This is primarily useful for moving existing resources
1212
 * to local named resources.
1213
 */
1214
void
1215
pdf_drop_resource_from_chain(gx_device_pdf * pdev, pdf_resource_t *pres1, pdf_resource_type_t rtype);
1216
1217
void pdf_drop_resources(gx_device_pdf * pdev, pdf_resource_type_t rtype,
1218
        int (*cond)(gx_device_pdf * pdev, pdf_resource_t *pres));
1219
1220
/* Print resource statistics. */
1221
void pdf_print_resource_statistics(gx_device_pdf * pdev);
1222
1223
/* Cancel a resource (do not write it into PDF). */
1224
int pdf_cancel_resource(gx_device_pdf * pdev, pdf_resource_t *pres,
1225
        pdf_resource_type_t rtype);
1226
1227
/* Remove a resource. */
1228
void pdf_forget_resource(gx_device_pdf * pdev, pdf_resource_t *pres1,
1229
        pdf_resource_type_t rtype);
1230
1231
/* Substitute a resource with a same one. */
1232
int pdf_substitute_resource(gx_device_pdf *pdev, pdf_resource_t **ppres,
1233
            pdf_resource_type_t rtype,
1234
            int (*eq)(gx_device_pdf *pdev, pdf_resource_t *pres0, pdf_resource_t *pres1),
1235
            bool write);
1236
1237
/* Get the object id of a resource. */
1238
int64_t pdf_resource_id(const pdf_resource_t *pres);
1239
1240
/* End a separate object. */
1241
int pdf_end_separate(gx_device_pdf * pdev, pdf_resource_type_t type);
1242
1243
/* End an aside. */
1244
int pdf_end_aside(gx_device_pdf * pdev, pdf_resource_type_t type);
1245
1246
/* End a resource. */
1247
int pdf_end_resource(gx_device_pdf * pdev, pdf_resource_type_t type);
1248
1249
/*
1250
 * Write the Cos objects for resources local to a content stream.
1251
 */
1252
int pdf_write_resource_objects(gx_device_pdf *pdev, pdf_resource_type_t rtype);
1253
1254
/*
1255
 * Reverse resource chains.
1256
 * ps2write uses it with page resources.
1257
 * Assuming only the 0th chain contauns something.
1258
 */
1259
void pdf_reverse_resource_chain(gx_device_pdf *pdev, pdf_resource_type_t rtype);
1260
1261
/*
1262
 * Free unnamed Cos objects for resources local to a content stream.
1263
 */
1264
int pdf_free_resource_objects(gx_device_pdf *pdev, pdf_resource_type_t rtype);
1265
1266
/*
1267
 * Store the resource sets for a content stream (page or XObject).
1268
 * Sets page->{procsets, resource_ids[], fonts_id}.
1269
 */
1270
int pdf_store_page_resources(gx_device_pdf *pdev, pdf_page_t *page, bool clear_usage);
1271
1272
/* Copy data from a temporary file to a stream. */
1273
int pdf_copy_data(stream *s, gp_file *file, gs_offset_t count, stream_arcfour_state *ss);
1274
int pdf_copy_data_safe(stream *s, gp_file *file, gs_offset_t position, int64_t count);
1275
1276
/* Add the encryption filter. */
1277
int pdf_begin_encrypt(gx_device_pdf * pdev, stream **s, gs_id object_id);
1278
/* Remove the encryption filter. */
1279
static int inline pdf_end_encrypt(gx_device_pdf *pdev)
1280
151k
{
1281
151k
    if (pdev->KeyLength)
1282
0
        return 1;
1283
151k
    return 0;
1284
151k
}
Unexecuted instantiation: gdevpdf.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfb.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfc.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfd.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfe.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfg.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfi.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfj.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfk.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfm.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfo.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfp.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdft.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdfr.c:pdf_end_encrypt
gdevpdfu.c:pdf_end_encrypt
Line
Count
Source
1280
32.0k
{
1281
32.0k
    if (pdev->KeyLength)
1282
0
        return 1;
1283
32.0k
    return 0;
1284
32.0k
}
Unexecuted instantiation: gdevpdfv.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdt.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdtd.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdtf.c:pdf_end_encrypt
gdevpdti.c:pdf_end_encrypt
Line
Count
Source
1280
119k
{
1281
119k
    if (pdev->KeyLength)
1282
0
        return 1;
1283
119k
    return 0;
1284
119k
}
Unexecuted instantiation: gdevpdts.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdtt.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdtw.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdtb.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdtc.c:pdf_end_encrypt
Unexecuted instantiation: gdevpdte.c:pdf_end_encrypt
1285
/* Initialize encryption. */
1286
int pdf_encrypt_init(const gx_device_pdf * pdev, gs_id object_id, stream_arcfour_state *psarc4);
1287
1288
/* ------ Pages ------ */
1289
1290
/* Get or assign the ID for a page. */
1291
/* Returns 0 if the page number is out of range. */
1292
int64_t pdf_page_id(gx_device_pdf * pdev, int page_num);
1293
1294
/* Get the page structure for the current page. */
1295
pdf_page_t *pdf_current_page(gx_device_pdf *pdev);
1296
1297
/* Get the dictionary object for the current page. */
1298
cos_dict_t *pdf_current_page_dict(gx_device_pdf *pdev);
1299
1300
/* Open a page for writing. */
1301
int pdf_open_page(gx_device_pdf * pdev, pdf_context_t context);
1302
1303
/*  Go to the unclipped stream context. */
1304
int pdf_unclip(gx_device_pdf * pdev);
1305
1306
/* Write saved page- or document-level information. */
1307
int pdf_write_saved_string(gx_device_pdf * pdev, gs_string * pstr);
1308
1309
/* ------ Path drawing ------ */
1310
1311
/* Store a copy of clipping path. */
1312
int pdf_remember_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath);
1313
1314
int pdf_check_soft_mask(gx_device_pdf * pdev, gs_gstate * pgs);
1315
1316
/* Test whether the clip path needs updating. */
1317
bool pdf_must_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath);
1318
1319
/* Write and update the clip path. */
1320
int pdf_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath);
1321
1322
/* ------ Masked image convertion ------ */
1323
1324
typedef struct pdf_lcvd_s {
1325
    gx_device_memory mdev;
1326
    gx_device_memory *mask;
1327
    gx_device_pdf *pdev;
1328
    dev_t_proc_copy_color((*std_copy_color), gx_device);
1329
    dev_t_proc_copy_mono((*std_copy_mono), gx_device);
1330
    dev_t_proc_fill_rectangle((*std_fill_rectangle), gx_device);
1331
    dev_t_proc_close_device((*std_close_device), gx_device);
1332
    dev_t_proc_get_clipping_box((*std_get_clipping_box), gx_device);
1333
    dev_t_proc_transform_pixel_region((*std_transform_pixel_region), gx_device);
1334
    bool mask_is_empty;
1335
    bool path_is_empty;
1336
    bool mask_is_clean;
1337
    bool filled_trap;
1338
    bool write_matrix;
1339
    bool has_background;
1340
    int pass;
1341
    gs_matrix m;
1342
    gs_point path_offset;
1343
} pdf_lcvd_t;
1344
1345
#define public_st_pdf_lcvd_t()\
1346
  gs_public_st_suffix_add2(st_pdf_lcvd_t, pdf_lcvd_t,\
1347
    "pdf_lcvd_t", pdf_lcvd_t_enum_ptrs,\
1348
    pdf_lcvd_t_reloc_ptrs, st_device_memory, mask, pdev)
1349
#define pdf_lcvd_t_max_ptrs (gx_device_memory_max_ptrs + 2)
1350
1351
int pdf_setup_masked_image_converter(gx_device_pdf *pdev, gs_memory_t *mem, const gs_matrix *m, pdf_lcvd_t **pcvd,
1352
                                 bool need_mask, int x, int y, int w, int h, bool write_on_close);
1353
int pdf_dump_converted_image(gx_device_pdf *pdev, pdf_lcvd_t *cvd, int for_pattern);
1354
void pdf_remove_masked_image_converter(gx_device_pdf *pdev, pdf_lcvd_t *cvd, bool need_mask);
1355
1356
/* ------ Miscellaneous output ------ */
1357
1358
39.4k
#define PDF_MAX_PRODUCER 200        /* adhoc */
1359
/* Generate the default Producer string. */
1360
void pdf_store_default_Producer(char buf[PDF_MAX_PRODUCER]);
1361
1362
/* Define the strings for filter names and parameters. */
1363
typedef struct pdf_filter_names_s {
1364
    const char *ASCII85Decode;
1365
    const char *ASCIIHexDecode;
1366
    const char *CCITTFaxDecode;
1367
    const char *DCTDecode;
1368
    const char *DecodeParms;
1369
    const char *Filter;
1370
    const char *FlateDecode;
1371
    const char *LZWDecode;
1372
    const char *RunLengthDecode;
1373
    const char *JBIG2Decode;
1374
    const char *JPXDecode;
1375
    const char *BrotliDecode;
1376
} pdf_filter_names_t;
1377
#define PDF_FILTER_NAMES\
1378
306k
  "/ASCII85Decode", "/ASCIIHexDecode", "/CCITTFaxDecode",\
1379
306k
  "/DCTDecode",  "/DecodeParms", "/Filter", "/FlateDecode",\
1380
306k
  "/LZWDecode", "/RunLengthDecode", "/JBIG2Decode", "/JPXDecode",\
1381
306k
  "/BrotliDecode"
1382
#define PDF_FILTER_NAMES_SHORT\
1383
  "/A85", "/AHx", "/CCF", "/DCT", "/DP", "/F", "/Fl", "/LZW", "/RL", "/???", "/???", "/Br"
1384
1385
/* Write matrix values. */
1386
void pdf_put_matrix(gx_device_pdf *pdev, const char *before,
1387
                    const gs_matrix *pmat, const char *after);
1388
1389
/* Write a name, with escapes for unusual characters. */
1390
typedef int (*pdf_put_name_chars_proc_t)(stream *, const byte *, uint);
1391
pdf_put_name_chars_proc_t
1392
    pdf_put_name_chars_proc(const gx_device_pdf *pdev);
1393
int pdf_put_name_chars(const gx_device_pdf *pdev, const byte *nstr,
1394
                        uint size);
1395
int pdf_put_name(const gx_device_pdf *pdev, const byte *nstr, uint size);
1396
1397
/* Write a string in its shortest form ( () or <> ). */
1398
int pdf_put_string(const gx_device_pdf *pdev, const byte *str, uint size);
1399
1400
/* Write a value, treating names specially. */
1401
int pdf_write_value(const gx_device_pdf *pdev, const byte *vstr, uint size, gs_id object_id);
1402
1403
/* Store filters for a stream. */
1404
int pdf_put_filters(cos_dict_t *pcd, gx_device_pdf *pdev, stream *s,
1405
                    const pdf_filter_names_t *pfn);
1406
1407
/* Define a possibly encoded and compressed data stream. */
1408
typedef struct pdf_data_writer_s {
1409
    psdf_binary_writer binary;
1410
    gs_offset_t start;
1411
    gs_offset_t length_pos;
1412
    pdf_resource_t *pres;
1413
    gx_device_pdf *pdev; /* temporary for backward compatibility of pdf_end_data prototype. */
1414
    int64_t length_id;
1415
    bool encrypted;
1416
} pdf_data_writer_t;
1417
/*
1418
 * Begin a data stream.  The client has opened the object and written
1419
 * the << and any desired dictionary keys.
1420
 */
1421
29.0k
#define DATA_STREAM_NOT_BINARY 0  /* data are text, not binary */
1422
841k
#define DATA_STREAM_BINARY 1        /* data are binary */
1423
508k
#define DATA_STREAM_COMPRESS 2        /* OK to compress data */
1424
551k
#define DATA_STREAM_NOLENGTH 4        /* Skip the length reference and filter names writing. */
1425
299k
#define DATA_STREAM_ENCRYPT  8        /* Encrypt data. */
1426
int pdf_begin_data_stream(gx_device_pdf *pdev, pdf_data_writer_t *pdw,
1427
                          int options, gs_id object_id);
1428
int pdf_append_data_stream_filters(gx_device_pdf *pdev, pdf_data_writer_t *pdw,
1429
                      int orig_options, gs_id object_id);
1430
/* begin_data = begin_data_binary with both options = true. */
1431
int pdf_begin_data(gx_device_pdf *pdev, pdf_data_writer_t *pdw);
1432
1433
/* End a data stream. */
1434
int pdf_end_data(pdf_data_writer_t *pdw);
1435
1436
/* ------ Functions ------ */
1437
1438
/* Define the maximum size of a Function reference. */
1439
#define MAX_REF_CHARS ((sizeof(int64_t) * 8 + 2) / 3)
1440
1441
/*
1442
 * Create a Function object with or without range scaling.  Scaling means
1443
 * that if x[i] is the i'th output value from the original Function,
1444
 * the i'th output value from the Function object will be (x[i] -
1445
 * ranges[i].rmin) / (ranges[i].rmax - ranges[i].rmin).  Note that this is
1446
 * the inverse of the scaling convention for Functions per se.
1447
 */
1448
int pdf_function(gx_device_pdf *pdev, const gs_function_t *pfn,
1449
                 cos_value_t *pvalue);
1450
int pdf_function_scaled(gx_device_pdf *pdev, const gs_function_t *pfn,
1451
                        const gs_range_t *pranges, cos_value_t *pvalue);
1452
1453
/* Write a Function object, returning its object ID. */
1454
int pdf_write_function(gx_device_pdf *pdev, const gs_function_t *pfn,
1455
                       int64_t *pid);
1456
1457
/* If a stitching function references an array of other functions, we need
1458
 * to 'unreference' those before freeing the function. otherwise we end up
1459
 * trying to free the referenced functions twice.
1460
 */
1461
int free_function_refs(gx_device_pdf *pdev, cos_object_t *pco);
1462
/* ------ Fonts ------ */
1463
1464
/* Write a FontBBox dictionary element. */
1465
int pdf_write_font_bbox(gx_device_pdf *pdev, const gs_int_rect *pbox);
1466
int pdf_write_font_bbox_float(gx_device_pdf *pdev, const gs_rect *pbox);
1467
1468
/* ---------------- Exported by gdevpdfm.c ---------------- */
1469
1470
/*
1471
 * Define the type for a pdfmark-processing procedure.
1472
 * If nameable is false, the objname argument is always NULL.
1473
 */
1474
#define pdfmark_proc(proc)\
1475
  int proc(gx_device_pdf *pdev, gs_param_string *pairs, uint count,\
1476
           const gs_matrix *pctm, const gs_param_string *objname)
1477
1478
/* Compare a C string and a gs_param_string. */
1479
bool pdf_key_eq(const gs_param_string * pcs, const char *str);
1480
1481
/* Scan an integer out of a parameter string. */
1482
int pdfmark_scan_int(const gs_param_string * pstr, int *pvalue);
1483
1484
/* Process a pdfmark (called from pdf_put_params). */
1485
int pdfmark_process(gx_device_pdf * pdev, const gs_param_string_array * pma);
1486
1487
/* Close the current level of the outline tree. */
1488
int pdfmark_close_outline(gx_device_pdf * pdev);
1489
1490
/* Close the pagelabel numtree. */
1491
int pdfmark_end_pagelabels(gx_device_pdf * pdev);
1492
1493
/* Finish writing an article. */
1494
int pdfmark_write_article(gx_device_pdf * pdev, const pdf_article_t * part);
1495
1496
/* ---------------- Exported by gdevpdfr.c ---------------- */
1497
1498
/* Test whether an object name has valid syntax, {name}. */
1499
bool pdf_objname_is_valid(const byte *data, uint size);
1500
1501
/*
1502
 * Look up a named object.  Return_error(gs_error_rangecheck if the syntax is invalid,
1503
 * gs_error_undefined if no object by that name exists.
1504
 */
1505
int pdf_find_named(gx_device_pdf * pdev, const gs_param_string * pname,
1506
                   cos_object_t **ppco);
1507
1508
/*
1509
 * Create a named object.  id = -1L means do not assign an id.  pname = 0
1510
 * means just create the object, do not name it.
1511
 */
1512
int pdf_create_named(gx_device_pdf *pdev, const gs_param_string *pname,
1513
                     cos_type_t cotype, cos_object_t **ppco, int64_t id);
1514
int pdf_create_named_dict(gx_device_pdf *pdev, const gs_param_string *pname,
1515
                          cos_dict_t **ppcd, int64_t id);
1516
1517
/*
1518
 * Look up a named object as for pdf_find_named.  If the object does not
1519
 * exist, create it (as a dictionary if it is one of the predefined names
1520
 * {ThisPage}, {NextPage}, {PrevPage}, or {Page<#>}, otherwise as a
1521
 * generic object) and return 1.
1522
 */
1523
int pdf_refer_named(gx_device_pdf *pdev, const gs_param_string *pname,
1524
                    cos_object_t **ppco);
1525
1526
/*
1527
 * Look up a named object as for pdf_refer_named.  If the object already
1528
 * exists and is not simply a forward reference, return_error(gs_error_rangecheck);
1529
 * if it exists as a forward reference, set its type and return 0;
1530
 * otherwise, create the object with the given type and return 1.
1531
 * pname = 0 is allowed: in this case, simply create the object.
1532
 */
1533
int pdf_make_named(gx_device_pdf * pdev, const gs_param_string * pname,
1534
                   cos_type_t cotype, cos_object_t **ppco, bool assign_id);
1535
int pdf_make_named_dict(gx_device_pdf * pdev, const gs_param_string * pname,
1536
                        cos_dict_t **ppcd, bool assign_id);
1537
1538
/*
1539
 * Look up a named object as for pdf_refer_named.  If the object does not
1540
 * exist, or is a forward reference, return gs_error_undefined; if the object
1541
 * exists has the wrong type, return gs_error_typecheck.
1542
 */
1543
int pdf_get_named(gx_device_pdf * pdev, const gs_param_string * pname,
1544
                  cos_type_t cotype, cos_object_t **ppco);
1545
1546
/*
1547
 * Push the current local namespace onto the namespace stack, and reset it
1548
 * to an empty namespace.
1549
 */
1550
int pdf_push_namespace(gx_device_pdf *pdev);
1551
1552
/*
1553
 * Pop the top local namespace from the namespace stack.  Return an error if
1554
 * the stack is empty.
1555
 */
1556
int pdf_pop_namespace(gx_device_pdf *pdev);
1557
1558
/*
1559
 * Scan a string for a token.  <<, >>, [, and ] are treated as tokens.
1560
 * Return 1 if a token was scanned, 0 if we reached the end of the string,
1561
 * or an error.  On a successful return, the token extends from *ptoken up
1562
 * to but not including *pscan.
1563
 */
1564
int pdf_scan_token(const byte **pscan, const byte * end, const byte **ptoken);
1565
1566
/*
1567
 * Scan a possibly composite token: arrays and dictionaries are treated as
1568
 * single tokens.
1569
 */
1570
int pdf_scan_token_composite(const byte **pscan, const byte * end,
1571
                             const byte **ptoken);
1572
1573
/* Replace object names with object references in a (parameter) string. */
1574
int pdf_replace_names(gx_device_pdf *pdev, const gs_param_string *from,
1575
                      gs_param_string *to);
1576
1577
/* ================ Text module procedures ================ */
1578
1579
/* ---------------- Exported by gdevpdfw.c ---------------- */
1580
1581
/* For gdevpdf.c */
1582
1583
int write_font_resources(gx_device_pdf *pdev, pdf_resource_list_t *prlist);
1584
1585
/* ---------------- Exported by gdevpdft.c ---------------- */
1586
1587
/* For gdevpdf.c */
1588
1589
pdf_text_data_t *pdf_text_data_alloc(gs_memory_t *mem);
1590
void pdf_set_text_state_default(pdf_text_state_t *pts);
1591
void pdf_text_state_copy(pdf_text_state_t *pts_to, pdf_text_state_t *pts_from);
1592
void pdf_reset_text_page(pdf_text_data_t *ptd);
1593
void pdf_reset_text_state(pdf_text_data_t *ptd);
1594
void pdf_close_text_page(gx_device_pdf *pdev);
1595
1596
/* For gdevpdfb.c */
1597
1598
int pdf_char_image_y_offset(const gx_device_pdf *pdev, int x, int y, int h);
1599
1600
/* Begin a CharProc for an embedded (bitmap) font. */
1601
int pdf_begin_char_proc(gx_device_pdf * pdev, int w, int h, int x_width,
1602
                        int y_offset, int x_offset, gs_id id, pdf_char_proc_t **ppcp,
1603
                        pdf_stream_position_t * ppos);
1604
1605
/* End a CharProc. */
1606
int pdf_end_char_proc(gx_device_pdf * pdev, pdf_stream_position_t * ppos);
1607
1608
/* Put out a reference to an image as a character in an embedded font. */
1609
int pdf_do_char_image(gx_device_pdf * pdev, const pdf_char_proc_t * pcp,
1610
                      const gs_matrix * pimat);
1611
1612
/* Start charproc accumulation for a Type 3 font. */
1613
int pdf_start_charproc_accum(gx_device_pdf *pdev);
1614
/* Install charproc accumulator for a Type 3 font. */
1615
int pdf_set_charproc_attrs(gx_device_pdf *pdev, gs_font *font, double *pw, int narg,
1616
                gs_text_cache_control_t control, gs_char ch, bool scale_100);
1617
/* Complete charproc accumulation for aType 3 font. */
1618
int pdf_end_charproc_accum(gx_device_pdf *pdev, gs_font *font, const pdf_char_glyph_pairs_t *cgp,
1619
                       gs_glyph glyph, gs_char output_char_code, const gs_const_string *gnstr);
1620
/* Open a stream object in the temporary file. */
1621
int pdf_open_aside(gx_device_pdf *pdev, pdf_resource_type_t rtype,
1622
        gs_id id, pdf_resource_t **ppres, bool reserve_object_id, int options);
1623
1624
/* Close a stream object in the temporary file. */
1625
int pdf_close_aside(gx_device_pdf *pdev);
1626
1627
/* Enter the substream accumulation mode. */
1628
int pdf_enter_substream(gx_device_pdf *pdev, pdf_resource_type_t rtype,
1629
                gs_id id, pdf_resource_t **ppres, bool reserve_object_id, bool compress);
1630
1631
/* Exit the substream accumulation mode. */
1632
int pdf_exit_substream(gx_device_pdf *pdev);
1633
/* Add procsets to substream Resources. */
1634
int pdf_add_procsets(cos_dict_t *pcd, pdf_procset_t procsets);
1635
/* Add a resource to substream Resources. */
1636
int pdf_add_resource(gx_device_pdf *pdev, cos_dict_t *pcd, const char *key, pdf_resource_t *pres);
1637
1638
/* For gdevpdfu.c */
1639
1640
int pdf_from_stream_to_text(gx_device_pdf *pdev);
1641
int pdf_from_string_to_text(gx_device_pdf *pdev);
1642
void pdf_close_text_contents(gx_device_pdf *pdev);
1643
1644
int gdev_pdf_get_param(gx_device *dev, char *Param, void *list);
1645
1646
int pdf_open_temp_file(gx_device_pdf *pdev, pdf_temp_file_t *ptf);
1647
int pdf_open_temp_stream(gx_device_pdf *pdev, pdf_temp_file_t *ptf);
1648
int pdf_close_temp_file(gx_device_pdf *pdev, pdf_temp_file_t *ptf, int code);
1649
1650
/* exported by gdevpdfe.c */
1651
1652
int pdf_xmp_write_translated(gx_device_pdf* pdev, stream* s, const byte* data, int data_length,
1653
    void(*write)(stream* s, const byte* data, int data_length));
1654
1655
#endif /* gdevpdfx_INCLUDED */