1 /* Copyright (C) 2001-2019 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.,  1305 Grant Avenue - Suite 200, Novato,
13    CA 94945, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* Common output syntax and parameters for PostScript and PDF writers */
18 
19 #ifndef gdevpsdf_INCLUDED
20 #  define gdevpsdf_INCLUDED
21 
22 /*
23  * This file should really be named gdevpsd.h or gdevpsdx.h, but we're
24  * keeping its current name for backward compatibility.
25  */
26 
27 #include "gdevvec.h"
28 #include "gsparam.h"
29 #include "strimpl.h"
30 #include "sa85x.h"
31 #include "scfx.h"
32 #include "spsdf.h"
33 
34 extern const stream_template s_DCTE_template; /* don't want all of sdct.h */
35 
36 /*
37  * NOTE: the default filter for color and gray images should be DCTEncode.
38  * We don't currently set this, because the management of the parameters
39  * for this filter simply doesn't work.
40  */
41 
42 /* ---------------- Distiller parameters ---------------- */
43 
44 /* Parameters for controlling distillation of images. */
45 typedef struct psdf_image_params_s {
46     gs_c_param_list *ACSDict;	/* JPEG */
47     bool AntiAlias;
48     bool AutoFilter;
49     int Depth;
50     gs_c_param_list *Dict;	/* JPEG or CCITTFax */
51     bool Downsample;
52     float DownsampleThreshold;
53     enum psdf_downsample_type {
54         ds_Average,
55         ds_Bicubic,
56         ds_Subsample
57     } DownsampleType;
58 #define psdf_ds_names\
59         "Average", "Bicubic", "Subsample"
60     bool Encode;
61     const char *Filter;
62     int Resolution;
63     enum psdf_autofilter_type {
64         af_Jpeg,
65         af_Jpeg2000
66     } AutoFilterStrategy;
67 #define psdf_afs_names\
68         "JPEG", "JPEG2000"
69     const stream_template *filter_template;
70 } psdf_image_params;
71 
72 /* Complete distiller parameters. */
73 typedef struct psdf_distiller_params_s {
74 
75     /* General parameters */
76 
77     bool ASCII85EncodePages;
78     enum psdf_auto_rotate_pages {
79         arp_None,
80         arp_All,
81         arp_PageByPage
82     } AutoRotatePages;
83 #define psdf_arp_names\
84         "None", "All", "PageByPage"
85     enum psdf_binding {
86         binding_Left,
87         binding_Right
88     } Binding;
89 #define psdf_binding_names\
90         "Left", "Right"
91     bool CompressPages;
92     enum psdf_default_rendering_intent {
93         ri_Default,
94         ri_Perceptual,
95         ri_Saturation,
96         ri_RelativeColorimetric,
97         ri_AbsoluteColorimetric
98     } DefaultRenderingIntent;
99 #define psdf_ri_names\
100         "Default", "Perceptual", "Saturation", "RelativeColorimetric",\
101         "AbsoluteColorimetric"
102     bool DetectBlends;
103     bool DoThumbnails;
104     long ImageMemory;
105     bool LockDistillerParams;
106     bool LZWEncodePages;
107     int OPM;
108     bool PreserveOPIComments;
109     bool UseFlateCompression;
110 
111     /* Color processing parameters */
112 
113     gs_const_string CalCMYKProfile;
114     gs_const_string CalGrayProfile;
115     gs_const_string CalRGBProfile;
116     gs_const_string sRGBProfile;
117     enum psdf_color_conversion_strategy {
118         ccs_LeaveColorUnchanged,
119         ccs_UseDeviceIndependentColor,
120         ccs_UseDeviceIndependentColorForImages,
121         ccs_sRGB,
122         ccs_CMYK,
123         ccs_Gray,
124         ccs_RGB,
125         ccs_ByObjectType
126     } ColorConversionStrategy;
127 #define psdf_ccs_names\
128         "LeaveColorUnchanged", \
129         "UseDeviceIndependentColor", "UseDeviceIndependentColorForImages",\
130         "sRGB", "CMYK", "Gray", "RGB", "ByObjectType"
131 
132     bool PreserveHalftoneInfo;
133     bool PreserveOverprintSettings;
134     enum psdf_transfer_function_info {
135         tfi_Preserve,
136         tfi_Apply,
137         tfi_Remove
138     } TransferFunctionInfo;
139 #define psdf_tfi_names\
140         "Preserve", "Apply", "Remove"
141     enum psdf_ucr_and_bg_info {
142         ucrbg_Preserve,
143         ucrbg_Remove
144     } UCRandBGInfo;
145 #define psdf_ucrbg_names\
146         "Preserve", "Remove"
147 
148     /* Color sampled image parameters */
149 
150     psdf_image_params ColorImage;
151     bool ConvertCMYKImagesToRGB;
152     bool ConvertImagesToIndexed;
153 
154     /* Grayscale sampled image parameters */
155 
156     psdf_image_params GrayImage;
157 
158     /* Monochrome sampled image parameters */
159 
160     psdf_image_params MonoImage;
161 
162     /* Font embedding parameters */
163 
164     gs_param_string_array AlwaysEmbed;
165     gs_param_string_array NeverEmbed;
166     enum psdf_cannot_embed_font_policy {
167         cefp_OK,
168         cefp_Warning,
169         cefp_Error
170     } CannotEmbedFontPolicy;
171 #define psdf_cefp_names\
172         "OK", "Warning", "Error"
173     bool EmbedAllFonts;
174     int MaxSubsetPct;
175     bool SubsetFonts;
176     bool PassThroughJPEGImages;
177     gs_param_string PSDocOptions;
178     gs_param_string_array PSPageOptions;
179 } psdf_distiller_params;
180 
181 /* Declare templates for default image compression filters. */
182 extern const stream_template s_CFE_template;
183 extern const stream_template s_zlibE_template;
184 
185 /* define some macros to use in setting up the default values of the pdfwrite device */
186 #define psdf_general_param_defaults(ascii)\
187     ascii,	    /* ASCIIEncodePages */ \
188     arp_None,	    /* Auto-RotatePages */ \
189     binding_Left,   /* Binding */\
190     1,		    /* CompressPages (true) */ \
191     ri_Default,	    /* rendering intent */ \
192     1,		    /* DetectBlends (true) */ \
193     0,		    /* DoThumbnails (false) */ \
194     500000,	    /* ImageMemory */ \
195     0,		    /* LockDistillerParams (false) */ \
196     0,		    /* LZWEncodePages (false) */ \
197     1,		    /* Overprintmode (OPM) */ \
198     0,		    /* PreserveOPIComments (false) */ \
199     1,		    /* UseFlateCompression (true) */ \
200         /* Color processing parameters */\
201     {0},	    /* calCMYKProfile */ \
202     {0},	    /* CalGrayProfile */ \
203     {0},	    /* calRGBProfile */ \
204     {0},	    /* sRGBProfile */ \
205     ccs_LeaveColorUnchanged, /* ColorConversionStrategy */ \
206     0,		    /* PreserveHalftoneInfo (false) */ \
207     0,		    /* PreserveOverprintSettings (false) */ \
208     tfi_Preserve,   /* TransferFunctionInfo */ \
209     ucrbg_Remove    /* UCRandBGInfo */
210 
211 #define psdf_color_image_param_defaults\
212   { NULL,	    /* ACSDict (JPEG) */ \
213     0,		    /* AntiAlias (false) */ \
214     0,		    /* AutoFilter (false) */ \
215     -1,		    /* Depth */ \
216     NULL,	    /* Dict (JPEG or CCITTFax) */ \
217     0,		    /* Downsample (false) */ \
218     1.5,	    /* Donwsample threshold */ \
219     ds_Bicubic,   /* Downsample type */ \
220     1,		    /* Encode (true) */ \
221     0,  /* compression biccufilter */ \
222     150,	    /* Downsample resolution */ \
223     af_Jpeg,	    /* AutoFilterStrategy */ \
224     &s_zlibE_template	/* Filter stream template */ \
225     },  0,	    /* ConvertCMYKImagesToRGB (false) */ \
226     1		    /* ConvertImagesToIndexed (true) */
227 
228 #define psdf_gray_image_param_defaults\
229   { NULL,	    /* ACSDict (JPEG) */ \
230     0,		    /* AntiAlias (false) */ \
231     0,		    /* AutoFilter (false) */ \
232     -1,		    /* Depth */ \
233     NULL,	    /* Dict (JPEG or CCITTFax) */ \
234     0,		    /* Downsample (false) */ \
235     1.5,	    /* Donwsample threshold */ \
236     ds_Subsample,   /* Downsample type */ \
237     1,		    /* Encode (true) */ \
238     0,  /* compression filter */ \
239     150,	    /* Downsample resolution */ \
240     af_Jpeg,	    /* AutoFilterStrategy */ \
241     &s_zlibE_template	/* Filter stream template */ \
242      }
243 
244 #define psdf_mono_image_param_defaults\
245   { NULL,	    /* ACSDict (JPEG) */ \
246     0,		    /* AntiAlias (false) */ \
247     0,		    /* AutoFilter (false) */ \
248     -1,		    /* Depth */ \
249     NULL,	    /* Dict (JPEG or CCITTFax) */ \
250     0,		    /* Downsample (false) */ \
251     2.0,	    /* Donwsample threshold */ \
252     ds_Subsample,   /* Downsample type */ \
253     1,		    /* Encode (true) */ \
254     "CCITTFaxEncode",  /* compression filter */ \
255     300,	    /* Downsample resolution */ \
256     af_Jpeg,	    /* AutoFilterStrategy */ \
257     &s_CFE_template	/* Filter stream template */ \
258     }
259 
260 #define psdf_font_param_defaults\
261     {0},	    /* AlwaysEmbed (array of names) */ \
262     {0},	    /* NeverEmbed (array of names) */ \
263     cefp_Warning,   /* CannotEmbedFontPolicy */ \
264     1,		    /* EmbedAllFonts (true) */ \
265     100,	    /* Max Subset Percent */ \
266     1		    /* Subset Fonts (true) */
267 
268 #define psdf_JPEGPassThrough_param_defaults\
269     1           /* PassThroughJPEGImages */
270 
271 #define psdf_PSOption_param_defaults\
272     {0},        /* PSDocOptions */\
273     {0}         /* PSPageOptions */
274 
275 /* Define PostScript/PDF versions, corresponding roughly to Adobe versions. */
276 typedef enum {
277     psdf_version_level1 = 1000,	/* Red Book Level 1 */
278     psdf_version_level1_color = 1100,	/* Level 1 + colorimage + CMYK color */
279     psdf_version_level2 = 2000,	/* Red Book Level 2 */
280     psdf_version_level2_with_TT = 2010,	/* Adobe release 2010 with Type 42 fonts */
281     psdf_version_level2_plus = 2017,	/* Adobe release 2017 */
282     psdf_version_ll3 = 3010	/* LanguageLevel 3, release 3010 */
283 } psdf_version;
284 
285 /* Define the extended device structure. */
286 #define gx_device_psdf_common\
287         gx_device_vector_common;\
288         psdf_version version;\
289         bool binary_ok;		/* derived from ASCII85EncodePages */\
290         bool HaveCFF;\
291         bool HaveTrueTypes;\
292         bool HaveCIDSystem;\
293         double ParamCompatibilityLevel;\
294         bool JPEG_PassThrough;\
295         psdf_distiller_params params
296 
297 typedef struct gx_device_psdf_s {
298     gx_device_psdf_common;
299 } gx_device_psdf;
300 
301 #define psdf_initial_values(version, ascii)\
302         vector_initial_values,\
303         version,\
304         !(ascii),\
305         true,\
306         true,\
307         false,\
308         1.3,\
309         0,\
310          { psdf_general_param_defaults(ascii),\
311            psdf_color_image_param_defaults,\
312            psdf_gray_image_param_defaults,\
313            psdf_mono_image_param_defaults,\
314            psdf_font_param_defaults,\
315            psdf_JPEGPassThrough_param_defaults,\
316            psdf_PSOption_param_defaults\
317          }
318 /* st_device_psdf is never instantiated per se, but we still need to */
319 /* extern its descriptor for the sake of subclasses. */
320 extern_st(st_device_psdf);
321 #define public_st_device_psdf()	/* in gdevpsdu.c */\
322   BASIC_PTRS(device_psdf_ptrs) {\
323     GC_OBJ_ELT2(gx_device_psdf, params.ColorImage.ACSDict,\
324                 params.ColorImage.Dict),\
325     GC_CONST_STRING_ELT(gx_device_psdf, params.CalCMYKProfile),\
326     GC_CONST_STRING_ELT(gx_device_psdf, params.CalGrayProfile),\
327     GC_CONST_STRING_ELT(gx_device_psdf, params.CalRGBProfile),\
328     GC_CONST_STRING_ELT(gx_device_psdf, params.sRGBProfile),\
329     GC_OBJ_ELT2(gx_device_psdf, params.GrayImage.ACSDict,\
330                 params.GrayImage.Dict),\
331     GC_OBJ_ELT2(gx_device_psdf, params.MonoImage.ACSDict,\
332                 params.MonoImage.Dict),\
333     GC_OBJ_ELT2(gx_device_psdf, params.AlwaysEmbed.data,\
334                 params.NeverEmbed.data),\
335     GC_CONST_STRING_ELT(gx_device_psdf, params.PSDocOptions),\
336     GC_OBJ_ELT(gx_device_psdf, params.PSPageOptions.data)\
337   };\
338   gs_public_st_basic_super_final(st_device_psdf, gx_device_psdf,\
339     "gx_device_psdf", device_psdf_ptrs, device_psdf_data,\
340     &st_device_vector, 0, gx_device_finalize)
341 #define st_device_psdf_max_ptrs (st_device_vector_max_ptrs + 14)
342 
343 /* Get/put parameters. */
344 int gdev_psdf_get_param(gx_device *dev, char *Param, void *list);
345 dev_proc_get_params(gdev_psdf_get_params);
346 dev_proc_put_params(gdev_psdf_put_params);
347 
348 dev_proc_dev_spec_op(gdev_psdf_dev_spec_op);
349 /* ---------------- Vector implementation procedures ---------------- */
350 
351         /* gs_gstate */
352 int psdf_setlinewidth(gx_device_vector * vdev, double width);
353 int psdf_setlinecap(gx_device_vector * vdev, gs_line_cap cap);
354 int psdf_setlinejoin(gx_device_vector * vdev, gs_line_join join);
355 int psdf_setmiterlimit(gx_device_vector * vdev, double limit);
356 int psdf_setdash(gx_device_vector * vdev, const float *pattern,
357                  uint count, double offset);
358 int psdf_setflat(gx_device_vector * vdev, double flatness);
359 int psdf_setlogop(gx_device_vector * vdev, gs_logical_operation_t lop,
360                   gs_logical_operation_t diff);
361 
362         /* Paths */
363 #define psdf_dopath gdev_vector_dopath
364 int psdf_dorect(gx_device_vector * vdev, fixed x0, fixed y0, fixed x1,
365                 fixed y1, gx_path_type_t type);
366 int psdf_beginpath(gx_device_vector * vdev, gx_path_type_t type);
367 int psdf_moveto(gx_device_vector * vdev, double x0, double y0,
368                 double x, double y, gx_path_type_t type);
369 int psdf_lineto(gx_device_vector * vdev, double x0, double y0,
370                 double x, double y, gx_path_type_t type);
371 int psdf_curveto(gx_device_vector * vdev, double x0, double y0,
372                  double x1, double y1, double x2,
373                  double y2, double x3, double y3, gx_path_type_t type);
374 int psdf_closepath(gx_device_vector * vdev, double x0, double y0,
375                    double x_start, double y_start, gx_path_type_t type);
376 
377 /* ---------------- Binary (image) data procedures ---------------- */
378 
379 /* Define the structure for writing binary data. */
380 typedef struct psdf_binary_writer_s {
381     gs_memory_t *memory;
382     stream *target;		/* underlying stream */
383     stream *strm;
384     gx_device_psdf *dev;	/* may be unused */
385     /* Due to recently introduced field cos_object_t::input_strm
386      * the binary writer may be simplified significantly.
387      * Keeping the old structure until we have time
388      * for this optimization.
389      */
390 } psdf_binary_writer;
391 extern_st(st_psdf_binary_writer);
392 #define public_st_psdf_binary_writer() /* in gdevpsdu.c */\
393   gs_public_st_ptrs3(st_psdf_binary_writer, psdf_binary_writer,\
394     "psdf_binary_writer", psdf_binary_writer_enum_ptrs,\
395     psdf_binary_writer_reloc_ptrs, target, strm, dev)
396 #define psdf_binary_writer_max_ptrs 3
397 
398 /* Begin writing binary data. */
399 int psdf_begin_binary(gx_device_psdf * pdev, psdf_binary_writer * pbw);
400 
401 /* Add an encoding filter.  The client must have allocated the stream state, */
402 /* if any, using pdev->v_memory. */
403 int psdf_encode_binary(psdf_binary_writer * pbw,
404                        const stream_template * template, stream_state * ss);
405 
406 /* Add a 2-D CCITTFax encoding filter. */
407 /* Set EndOfBlock iff the stream is not ASCII85 encoded. */
408 int psdf_CFE_binary(psdf_binary_writer * pbw, int w, int h, bool invert);
409 
410 /*
411  * Acquire parameters, and optionally set up the filter for, a DCTEncode
412  * filter.  This is a separate procedure so it can be used to validate
413  * filter parameters when they are set, rather than waiting until they are
414  * used.  pbw = NULL means just set up the stream state.
415  */
416 int psdf_DCT_filter(gs_param_list *plist /* may be NULL */,
417                     stream_state /*stream_DCTE_state*/ *st,
418                     int Columns, int Rows, int Colors,
419                     psdf_binary_writer *pbw /* may be NULL */);
420 
421 /* Decive whether to convert an image to RGB. */
422 bool psdf_is_converting_image_to_RGB(const gx_device_psdf * pdev,
423                 const gs_gstate * pgs, const gs_pixel_image_t * pim);
424 
425 /* Set up compression and downsampling filters for an image. */
426 /* Note that this may modify the image parameters. */
427 /* If pctm is NULL, downsampling is not used. */
428 /* pgs only provides UCR and BG information for CMYK => RGB conversion. */
429 int psdf_setup_image_filters(gx_device_psdf *pdev, psdf_binary_writer *pbw,
430                              gs_pixel_image_t *pim, const gs_matrix *pctm,
431                              const gs_gstate * pgs, bool lossless,
432                              bool in_line);
433 
434 int new_setup_image_filters(gx_device_psdf *pdev, psdf_binary_writer *pbw,
435                              gs_pixel_image_t *pim, const gs_matrix *pctm,
436                              const gs_gstate * pgs, bool lossless,
437                              bool in_line, bool colour_conversion);
438 
439 /* Set up compression filters for a lossless image, with no downsampling, */
440 /* no color space conversion, and only lossless filters. */
441 /* Note that this may modify the image parameters. */
442 int psdf_setup_lossless_filters(gx_device_psdf *pdev, psdf_binary_writer *pbw,
443                                 gs_pixel_image_t *pim, bool in_line);
444 
445 int new_setup_lossless_filters(gx_device_psdf *pdev, psdf_binary_writer *pbw,
446                                 gs_pixel_image_t *pim, bool in_line, bool colour_conversion, const gs_matrix *pctm, gs_gstate * pgs);
447 
448 
449 int new_resize_input(psdf_binary_writer *pbw, int width, int num_comps, int bpc_in, int bpc_out);
450 
451 /* Finish writing binary data. */
452 int psdf_end_binary(psdf_binary_writer * pbw);
453 
454 /* Set up image compression chooser. */
455 int psdf_setup_compression_chooser(psdf_binary_writer *pbw,
456                                    gx_device_psdf *pdev,
457                                    int width, int height, int depth,
458                                    int bits_per_sample);
459 
460 /* Set up an "image to mask" filter. */
461 int psdf_setup_image_to_mask_filter(psdf_binary_writer *pbw, gx_device_psdf *pdev,
462                                     int width, int height, int input_width,
463                                     int depth, int bits_per_sample, uint *MaskColor);
464 
465 /* Set up an image colors filter. */
466 int psdf_setup_image_colors_filter(psdf_binary_writer *pbw,
467                                    gx_device_psdf *pdev,
468                                    const gs_pixel_image_t *input_pim,
469                                    gs_pixel_image_t * pim,
470         const gs_gstate *pgs);
471 
472 /* ---------------- Symbolic data printing ---------------- */
473 
474 /* Backward compatibility definitions. */
475 #define psdf_write_string(s, str, size, print_ok)\
476   s_write_ps_string(s, str, size, print_ok)
477 #define psdf_alloc_position_stream(ps, mem)\
478   s_alloc_position_stream(ps, mem)
479 #define psdf_alloc_param_printer(pplist, ppp, s, mem)\
480   s_alloc_param_printer(pplist, ppp, s, mem)
481 #define psdf_free_param_printer(plist)\
482   s_free_param_printer(plist)
483 
484 /* ---------------- Other procedures ---------------- */
485 
486 /* Define the commands for setting the fill or stroke color. */
487 typedef struct psdf_set_color_commands_s {
488     const char *setgray;
489     const char *setrgbcolor;
490     const char *setcmykcolor;
491     const char *setcolorspace;
492     const char *setcolor;
493     const char *setcolorn;
494 } psdf_set_color_commands_t;
495 /* Define the standard color-setting commands (with PDF names). */
496 extern const psdf_set_color_commands_t
497     psdf_set_fill_color_commands, psdf_set_stroke_color_commands;
498 
499 /*
500  * Adjust a gx_color_index to compensate for the fact that the bit pattern
501  * of gx_color_index isn't representable.
502  */
503 gx_color_index psdf_adjust_color_index(gx_device_vector *vdev,
504                                        gx_color_index color);
505 
506 /* Set the fill or stroke color. */
507 int psdf_set_color(gx_device_vector *vdev, const gx_drawing_color *pdc,
508                    const psdf_set_color_commands_t *ppscc);
509 /* Round a double value to a specified precision. */
510 double psdf_round(double v, int precision, int radix);
511 
512 /* stubs to disable get_bits, get_bits_rectangle */
513 dev_proc_get_bits(psdf_get_bits);
514 dev_proc_get_bits_rectangle(psdf_get_bits_rectangle);
515 
516 /* intercept and ignore overprint compositor creation */
517 dev_proc_create_compositor(psdf_create_compositor);
518 
519 #endif /* gdevpsdf_INCLUDED */
520