summaryrefslogtreecommitdiff
path: root/devices/vector/gdevpdfg.h
blob: e846b1a8f9cf9c64c0034e3fc6dee439450c1561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* Copyright (C) 2001-2019 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


/* Internal graphics interfaces for PDF-writing driver. */

#ifndef gdevpdfg_INCLUDED
#  define gdevpdfg_INCLUDED

#include "gscspace.h"		/* for gs_separation_name */
#include "gdevpdfx.h"

/* ---------------- Exported by gdevpdfc.c ---------------- */

/* Define standard and short color space names. */
/*
 * Based on Adobe's published PDF documentation, it appears that the
 * abbreviations for the calibrated color spaces were introduced in
 * PDF 1.1 (added to Table 7.3) and removed in PDF 1.2 (Table 8.1)!
 */
typedef struct pdf_color_space_names_s {
    const char *DeviceCMYK;
    const char *DeviceGray;
    const char *DeviceRGB;
    const char *Indexed;
} pdf_color_space_names_t;
#define PDF_COLOR_SPACE_NAMES\
  "/DeviceCMYK", "/DeviceGray", "/DeviceRGB", "/Indexed"
#define PDF_COLOR_SPACE_NAMES_SHORT\
  "/CMYK", "/G", "/RGB", "/I"
extern const pdf_color_space_names_t
    pdf_color_space_names,
    pdf_color_space_names_short;

/*
 * Define a ColorSpace resource.  We need to retain the range scaling
 * information (if any).
 */
typedef struct pdf_color_space_s pdf_color_space_t;
struct pdf_color_space_s {
    pdf_resource_common(pdf_color_space_t);
    const gs_range_t *ranges;
    uint serialized_size;
    byte *serialized;
};
/* The descriptor is public because it is for a resource type. */
#define public_st_pdf_color_space()  /* in gdevpdfc.c */\
  gs_public_st_suffix_add2(st_pdf_color_space, pdf_color_space_t,\
    "pdf_color_space_t", pdf_color_space_enum_ptrs,\
    pdf_color_space_reloc_ptrs, st_pdf_resource, ranges, serialized)

/*
 * Create a local Device{Gray,RGB,CMYK} color space corresponding to the
 * given number of components.
 */
int pdf_cspace_init_Device(gs_memory_t *mem, gs_color_space **ppcs, int num_components);

/* test an (image) colour space to see if its ICCBased. If so we will
 * convert to current device space
 */
int pdf_convert_ICC(gx_device_pdf *pdev,
                const gs_color_space *pcs, cos_value_t *pvalue,
                const pdf_color_space_names_t *pcsn);
/*
 * Create a PDF color space corresponding to a PostScript color space.
 * For parameterless color spaces, set *pvalue to a (literal) string with
 * the color space name; for other color spaces, create a cos_array_t if
 * necessary and set *pvalue to refer to it.  In the latter case, if
 * by_name is true, return a string /Rxxxx rather than a reference to
 * the actual object.
 *
 * If ppranges is not NULL, then if the domain of the color space had
 * to be scaled (to convert a CIEBased space to ICCBased), store a pointer
 * to the ranges in *ppranges, otherwise set *ppranges to 0.
 */
int pdf_color_space_named(gx_device_pdf *pdev, const gs_gstate * pgs,
                    cos_value_t *pvalue,
                    const gs_range_t **ppranges,
                    const gs_color_space *pcs,
                    const pdf_color_space_names_t *pcsn,
                    bool by_name, const byte *res_name, int name_length, bool keepICC);

int free_color_space(gx_device_pdf *pdev, pdf_resource_t *pres);
int pdf_indexed_color_space(gx_device_pdf *pdev, const gs_gstate * pgs, cos_value_t *pvalue,
                        const gs_color_space *pcs, cos_array_t *pca, cos_value_t *cos_base);

int convert_separation_alternate(gx_device_pdf * pdev, const gs_gstate * pgs, const gs_color_space *pcs,
                        const gx_drawing_color *pdc, bool *used_process_color,
                        const psdf_set_color_commands_t *ppscc, gs_client_color *pcc, cos_value_t *pvalue, bool by_name);
int convert_DeviceN_alternate(gx_device_pdf * pdev, const gs_gstate * pgs, const gs_color_space *pcs,
                        const gx_drawing_color *pdc, bool *used_process_color,
                        const psdf_set_color_commands_t *ppscc, gs_client_color *pcc, cos_value_t *pvalue, bool by_name);
/* Create colored and uncolored Pattern color spaces. */
int pdf_cs_Pattern_colored(gx_device_pdf *pdev, cos_value_t *pvalue);
int pdf_cs_Pattern_uncolored(gx_device_pdf *pdev, cos_value_t *pvalue);
int pdf_cs_Pattern_uncolored_hl(gx_device_pdf *pdev,
        const gs_color_space *pcs, cos_value_t *pvalue, const gs_gstate * pgs);

/* Set the ProcSets bits corresponding to an image color space. */
void pdf_color_space_procsets(gx_device_pdf *pdev,
                              const gs_color_space *pcs);

/* ---------------- Exported by gdevpdfg.c ---------------- */

/* Copy viewer state from images state. */
void pdf_viewer_state_from_gs_gstate(gx_device_pdf * pdev,
        const gs_gstate *pgs, const gx_device_color *pdevc);

/* Prepare intitial values for viewer's graphics state parameters. */
void pdf_prepare_initial_viewer_state(gx_device_pdf * pdev, const gs_gstate *pgs);

/* Reset the graphics state parameters to initial values. */
void pdf_reset_graphics(gx_device_pdf *pdev);

/* Set initial color. */
void pdf_set_initial_color(gx_device_pdf * pdev, gx_hl_saved_color *saved_fill_color,
                    gx_hl_saved_color *saved_stroke_color,
                    bool *fill_used_process_color, bool *stroke_used_process_color);

void rescale_cie_color(gs_range_t *ranges, int num_colorants,
                    const gs_client_color *src, gs_client_color *des);
/* Set the fill or stroke color. */
int pdf_reset_color(gx_device_pdf * pdev, const gs_gstate * pgs,
                const gx_drawing_color *pdc, gx_hl_saved_color * psc,
                bool *used_process_color,
                const psdf_set_color_commands_t *ppscc);
/* pdecolor is &pdev->fill_color or &pdev->stroke_color. */
int pdf_set_pure_color(gx_device_pdf * pdev, gx_color_index color,
                   gx_hl_saved_color * psc,
                   bool *used_process_color,
                   const psdf_set_color_commands_t *ppscc);
int pdf_set_drawing_color(gx_device_pdf * pdev, const gs_gstate * pgs,
                      const gx_drawing_color *pdc,
                      gx_hl_saved_color * psc,
                      bool *used_process_color,
                      const psdf_set_color_commands_t *ppscc);
/*
 * Bring the graphics state up to date for a drawing operation.
 * (Text uses either fill or stroke.)
 */
int pdf_try_prepare_fill(gx_device_pdf *pdev, const gs_gstate *pgs, bool for_text);
int pdf_prepare_drawing(gx_device_pdf *pdev, const gs_gstate *pgs, pdf_resource_t **ppres, bool for_text);
int pdf_prepare_fill(gx_device_pdf *pdev, const gs_gstate *pgs, bool for_text);
int pdf_prepare_stroke(gx_device_pdf *pdev, const gs_gstate *pgs, bool for_text);
int pdf_prepare_fill_stroke(gx_device_pdf *pdev, const gs_gstate *pgs, bool for_text);
int pdf_prepare_image(gx_device_pdf *pdev, const gs_gstate *pgs);
int pdf_prepare_imagemask(gx_device_pdf *pdev, const gs_gstate *pgs,
                          const gx_drawing_color *pdcolor);
int pdf_save_viewer_state(gx_device_pdf *pdev, stream *s);
int pdf_restore_viewer_state(gx_device_pdf *pdev, stream *s);
int pdf_end_gstate(gx_device_pdf *pdev, pdf_resource_t *pres);

/*
 * Convert a string into cos name.
 */
int pdf_string_to_cos_name(gx_device_pdf *pdev, const byte *str, uint len,
                       cos_value_t *pvalue);

/* ---------------- Exported by gdevpdfi.c ---------------- */

typedef struct pdf_pattern_s pdf_pattern_t;
struct pdf_pattern_s {
    pdf_resource_common(pdf_pattern_t);
    pdf_pattern_t *substitute;
};
/* The descriptor is public because it is for a resource type. */
#define private_st_pdf_pattern()  /* in gdevpdfc.c */\
  gs_private_st_suffix_add1(st_pdf_pattern, pdf_pattern_t,\
    "pdf_pattern_t", pdf_pattern_enum_ptrs,\
    pdf_pattern_reloc_ptrs, st_pdf_resource, substitute)

pdf_resource_t *pdf_substitute_pattern(pdf_resource_t *pres);

/* ---------------- Exported by gdevpdfj.c ---------------- */

/* ------ Image stream dictionaries ------ */

/* Define the long and short versions of the keys in an image dictionary, */
/* and other strings for images. */
typedef struct pdf_image_names_s {
    pdf_color_space_names_t color_spaces;
    pdf_filter_names_t filter_names;
    const char *BitsPerComponent;
    const char *ColorSpace;
    const char *Decode;
    const char *Height;
    const char *ImageMask;
    const char *Interpolate;
    const char *Width;
} pdf_image_names_t;
#define PDF_IMAGE_PARAM_NAMES\
    "/BitsPerComponent", "/ColorSpace", "/Decode",\
    "/Height", "/ImageMask", "/Interpolate", "/Width"
#define PDF_IMAGE_PARAM_NAMES_SHORT\
    "/BPC", "/CS", "/D", "/H", "/IM", "/I", "/W"
extern const pdf_image_names_t pdf_image_names_full, pdf_image_names_short;

/* Store the values of image parameters other than filters. */
/* pdev is used only for updating procsets. */
/* pcsvalue is not used for masks. */
int pdf_put_image_values(cos_dict_t *pcd, gx_device_pdf *pdev,
                         const gs_pixel_image_t *pic,
                         const pdf_image_names_t *pin,
                         const cos_value_t *pcsvalue);

/* Store filters for an image. */
/* Currently this only saves parameters for CCITTFaxDecode. */
int pdf_put_image_filters(cos_dict_t *pcd, gx_device_pdf *pdev,
                          const psdf_binary_writer * pbw,
                          const pdf_image_names_t *pin);

/* ------ Image writing ------ */

/*
 * Fill in the image parameters for a device space bitmap.
 * PDF images are always specified top-to-bottom.
 * data_h is the actual number of data rows, which may be less than h.
 */
void pdf_make_bitmap_matrix(gs_matrix * pmat, int x, int y, int w, int h,
                            int h_actual);

/* Put out the gsave and matrix for an image. */
void pdf_put_image_matrix(gx_device_pdf * pdev, const gs_matrix * pmat,
                          double y_scale);

/* Put out a reference to an image resource. */
int pdf_do_image_by_id(gx_device_pdf * pdev, double scale,
             const gs_matrix * pimat, bool in_contents, gs_id id);
int pdf_do_image(gx_device_pdf * pdev, const pdf_resource_t * pres,
                 const gs_matrix * pimat, bool in_contents);

#define pdf_image_writer_num_alt_streams 4

/* Define the structure for writing an image. */
typedef struct pdf_image_writer_s {
    psdf_binary_writer binary[pdf_image_writer_num_alt_streams];
    int alt_writer_count; /* no. of active elements in writer[] (1,2,3) */
    const pdf_image_names_t *pin;
    pdf_resource_t *pres;	/* XObject resource iff not in-line */
    int height;			/* initially specified image height */
    cos_stream_t *data;
    const char *end_string;	/* string to write after EI if in-line */
    cos_dict_t *named;		/* named dictionary from NI */
    pdf_resource_t *pres_mask;	/* PS2WRITE only : XObject resource for mask */
} pdf_image_writer;
extern_st(st_pdf_image_writer);	/* public for gdevpdfi.c */
#define public_st_pdf_image_writer() /* in gdevpdfj.c */\
  gs_public_st_composite(st_pdf_image_writer, pdf_image_writer,\
    "pdf_image_writer", pdf_image_writer_enum_ptrs, pdf_image_writer_reloc_ptrs)
#define pdf_image_writer_max_ptrs (psdf_binary_writer_max_ptrs * pdf_image_writer_num_alt_streams + 4)

/* Initialize image writer. */
void pdf_image_writer_init(pdf_image_writer * piw);

/*
 * Begin writing an image, creating the resource if not in-line, and setting
 * up the binary writer.  If pnamed != 0, it is a dictionary object created
 * by a NI pdfmark.
 */
int pdf_begin_write_image(gx_device_pdf * pdev, pdf_image_writer * piw,
                          gx_bitmap_id id, int w, int h,
                          cos_dict_t *pnamed, bool in_line);

/* Begin writing the image data, setting up the dictionary and filters. */
int pdf_begin_image_data(gx_device_pdf * pdev, pdf_image_writer * piw,
                         const gs_pixel_image_t * pim,
                         const cos_value_t *pcsvalue,
                         int alt_writer_index);

/* Copy the data for a mask or monobit bitmap. */
int pdf_copy_mask_bits(stream *s, const byte *base, int sourcex,
                       int raster, int w, int h, byte invert);

/* Copy the data for a colored image (device pixels). */
int pdf_copy_color_bits(stream *s, const byte *base, int sourcex,
                        int raster, int w, int h, int bytes_per_pixel);

/* Complete image data. */
int
pdf_complete_image_data(gx_device_pdf *pdev, pdf_image_writer *piw, int data_h,
                        int width, int bits_per_pixel);

/* Finish writing the binary image data. */
int pdf_end_image_binary(gx_device_pdf *pdev, pdf_image_writer *piw,
                         int data_h);

/*
 * Finish writing an image.  If in-line, write the BI/dict/ID/data/EI and
 * return 1; if a resource, write the resource definition and return 0.
 */
int pdf_end_write_image(gx_device_pdf * pdev, pdf_image_writer * piw);

/*
 *  Make alternative stream for image compression choice.
 */
int pdf_make_alt_stream(gx_device_pdf * pdev, psdf_binary_writer * piw);

/*
 * End binary with choosing image compression.
 */
int pdf_choose_compression(pdf_image_writer * piw, bool end_binary);

/* If the current substream is a charproc, register a font used in it. */
int pdf_register_charproc_resource(gx_device_pdf *pdev, gs_id id, pdf_resource_type_t type);

/* ---------------- Exported by gdevpdfv.c ---------------- */

/* Store pattern 1 parameters to cos dictionary. */
int pdf_store_pattern1_params(gx_device_pdf *pdev, pdf_resource_t *pres,
                        gs_pattern1_instance_t *pinst);

/* Write a colored Pattern color. */
int pdf_put_colored_pattern(gx_device_pdf *pdev, const gx_drawing_color *pdc,
                        const gs_color_space *pcs,
                        const psdf_set_color_commands_t *ppscc,
                        const gs_gstate * pgs, pdf_resource_t **ppres);

/* Write an uncolored Pattern color. */
int pdf_put_uncolored_pattern(gx_device_pdf *pdev, const gx_drawing_color *pdc,
                          const gs_color_space *pcs,
                          const psdf_set_color_commands_t *ppscc,
                          const gs_gstate *pgs, pdf_resource_t **ppres);

/* Write a PatternType 2 (shading pattern) color. */
int pdf_put_pattern2(gx_device_pdf *pdev, const gs_gstate * pgs, const gx_drawing_color *pdc,
                 const psdf_set_color_commands_t *ppscc,
                 pdf_resource_t **ppres);

/* ---------------- Exported by gdevpdfb.c ---------------- */

/* Copy a color bitmap.  for_pattern = -1 means put the image in-line, */
/* 1 means put the image in a resource. */
int pdf_copy_color_data(gx_device_pdf * pdev, const byte * base, int sourcex,
                    int raster, gx_bitmap_id id, int x, int y, int w, int h,
                    gs_image_t *pim, pdf_image_writer *piw,
                    int for_pattern);

#endif /* gdevpdfg_INCLUDED */

/* ---------------- Exported by gdevpdfe.c ---------------- */

/* Write metadata */
int pdf_document_metadata(gx_device_pdf *pdev);
int pdf_get_docinfo_item(gx_device_pdf *pdev, const char *key, char *buf, int buf_length);