xref: /plan9/sys/src/cmd/gs/src/gdevpdfg.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gdevpdfg.h,v 1.42 2005/08/29 18:21:57 igor Exp $ */
18 /* Internal graphics interfaces for PDF-writing driver. */
19 
20 #ifndef gdevpdfg_INCLUDED
21 #  define gdevpdfg_INCLUDED
22 
23 #include "gscspace.h"		/* for gs_separation_name */
24 
25 /* ---------------- Exported by gdevpdfc.c ---------------- */
26 
27 /* Define standard and short color space names. */
28 /*
29  * Based on Adobe's published PDF documentation, it appears that the
30  * abbreviations for the calibrated color spaces were introduced in
31  * PDF 1.1 (added to Table 7.3) and removed in PDF 1.2 (Table 8.1)!
32  */
33 typedef struct pdf_color_space_names_s {
34     const char *DeviceCMYK;
35     const char *DeviceGray;
36     const char *DeviceRGB;
37     const char *Indexed;
38 } pdf_color_space_names_t;
39 #define PDF_COLOR_SPACE_NAMES\
40   "/DeviceCMYK", "/DeviceGray", "/DeviceRGB", "/Indexed"
41 #define PDF_COLOR_SPACE_NAMES_SHORT\
42   "/CMYK", "/G", "/RGB", "/I"
43 extern const pdf_color_space_names_t
44     pdf_color_space_names,
45     pdf_color_space_names_short;
46 
47 /* Define an abstract color space type. */
48 #ifndef gs_color_space_DEFINED
49 #  define gs_color_space_DEFINED
50 typedef struct gs_color_space_s gs_color_space;
51 #endif
52 
53 /*
54  * Define a ColorSpace resource.  We need to retain the range scaling
55  * information (if any).
56  */
57 typedef struct pdf_color_space_s pdf_color_space_t;
58 struct pdf_color_space_s {
59     pdf_resource_common(pdf_color_space_t);
60     const gs_range_t *ranges;
61     uint serialized_size;
62     byte *serialized;
63 };
64 /* The descriptor is public because it is for a resource type. */
65 #define public_st_pdf_color_space()  /* in gdevpdfc.c */\
66   gs_public_st_suffix_add2(st_pdf_color_space, pdf_color_space_t,\
67     "pdf_color_space_t", pdf_color_space_enum_ptrs,\
68     pdf_color_space_reloc_ptrs, st_pdf_resource, ranges, serialized)
69 
70 /*
71  * Create a local Device{Gray,RGB,CMYK} color space corresponding to the
72  * given number of components.
73  */
74 int pdf_cspace_init_Device(const gs_memory_t *mem, gs_color_space *pcs, int num_components);
75 
76 /*
77  * Create a PDF color space corresponding to a PostScript color space.
78  * For parameterless color spaces, set *pvalue to a (literal) string with
79  * the color space name; for other color spaces, create a cos_array_t if
80  * necessary and set *pvalue to refer to it.  In the latter case, if
81  * by_name is true, return a string /Rxxxx rather than a reference to
82  * the actual object.
83  *
84  * If ppranges is not NULL, then if the domain of the color space had
85  * to be scaled (to convert a CIEBased space to ICCBased), store a pointer
86  * to the ranges in *ppranges, otherwise set *ppranges to 0.
87  */
88 int pdf_color_space(gx_device_pdf *pdev, cos_value_t *pvalue,
89 		    const gs_range_t **ppranges,
90 		    const gs_color_space *pcs,
91 		    const pdf_color_space_names_t *pcsn,
92 		    bool by_name);
93 int pdf_color_space_named(gx_device_pdf *pdev, cos_value_t *pvalue,
94 		    const gs_range_t **ppranges,
95 		    const gs_color_space *pcs,
96 		    const pdf_color_space_names_t *pcsn,
97 		    bool by_name, const byte *res_name, int name_length);
98 
99 /* Create colored and uncolored Pattern color spaces. */
100 int pdf_cs_Pattern_colored(gx_device_pdf *pdev, cos_value_t *pvalue);
101 int pdf_cs_Pattern_uncolored(gx_device_pdf *pdev, cos_value_t *pvalue);
102 int pdf_cs_Pattern_uncolored_hl(gx_device_pdf *pdev,
103 	const gs_color_space *pcs, cos_value_t *pvalue);
104 
105 /* Set the ProcSets bits corresponding to an image color space. */
106 void pdf_color_space_procsets(gx_device_pdf *pdev,
107 			      const gs_color_space *pcs);
108 
109 /* ---------------- Exported by gdevpdfg.c ---------------- */
110 
111 /* Copy viewer state from images state. */
112 void pdf_viewer_state_from_imager_state(gx_device_pdf * pdev,
113 	const gs_imager_state *pis, const gx_device_color *pdevc);
114 
115 /* Prepare intitial values for viewer's graphics state parameters. */
116 void pdf_prepare_initial_viewer_state(gx_device_pdf * pdev, const gs_imager_state *pis);
117 
118 /* Reset the graphics state parameters to initial values. */
119 void pdf_reset_graphics(gx_device_pdf *pdev);
120 
121 /* Set initial color. */
122 void pdf_set_initial_color(gx_device_pdf * pdev, gx_hl_saved_color *saved_fill_color,
123 		    gx_hl_saved_color *saved_stroke_color,
124 		    bool *fill_used_process_color, bool *stroke_used_process_color);
125 
126 /* Set the fill or stroke color. */
127 /* pdecolor is &pdev->fill_color or &pdev->stroke_color. */
128 int pdf_set_pure_color(gx_device_pdf * pdev, gx_color_index color,
129 		   gx_hl_saved_color * psc,
130     		   bool *used_process_color,
131 		   const psdf_set_color_commands_t *ppscc);
132 int pdf_set_drawing_color(gx_device_pdf * pdev, const gs_imager_state * pis,
133 		      const gx_drawing_color *pdc,
134 		      gx_hl_saved_color * psc,
135 		      bool *used_process_color,
136 		      const psdf_set_color_commands_t *ppscc);
137 
138 /*
139  * Bring the graphics state up to date for a drawing operation.
140  * (Text uses either fill or stroke.)
141  */
142 int pdf_try_prepare_fill(gx_device_pdf *pdev, const gs_imager_state *pis);
143 int pdf_prepare_drawing(gx_device_pdf *pdev, const gs_imager_state *pis, pdf_resource_t **ppres);
144 int pdf_prepare_fill(gx_device_pdf *pdev, const gs_imager_state *pis);
145 int pdf_prepare_stroke(gx_device_pdf *pdev, const gs_imager_state *pis);
146 int pdf_prepare_image(gx_device_pdf *pdev, const gs_imager_state *pis);
147 int pdf_prepare_imagemask(gx_device_pdf *pdev, const gs_imager_state *pis,
148 			  const gx_drawing_color *pdcolor);
149 int pdf_save_viewer_state(gx_device_pdf *pdev, stream *s);
150 int pdf_restore_viewer_state(gx_device_pdf *pdev, stream *s);
151 int pdf_end_gstate(gx_device_pdf *pdev, pdf_resource_t *pres);
152 
153 /*
154  * Convert a string into cos name.
155  */
156 int pdf_string_to_cos_name(gx_device_pdf *pdev, const byte *str, uint len,
157 		       cos_value_t *pvalue);
158 
159 /* ---------------- Exported by gdevpdfi.c ---------------- */
160 
161 typedef struct pdf_pattern_s pdf_pattern_t;
162 struct pdf_pattern_s {
163     pdf_resource_common(pdf_pattern_t);
164     pdf_pattern_t *substitute;
165 };
166 /* The descriptor is public because it is for a resource type. */
167 #define private_st_pdf_pattern()  /* in gdevpdfc.c */\
168   gs_private_st_suffix_add1(st_pdf_pattern, pdf_pattern_t,\
169     "pdf_pattern_t", pdf_pattern_enum_ptrs,\
170     pdf_pattern_reloc_ptrs, st_pdf_resource, substitute)
171 
172 pdf_resource_t *pdf_substitute_pattern(pdf_resource_t *pres);
173 
174 /* ---------------- Exported by gdevpdfj.c ---------------- */
175 
176 /* ------ Image stream dictionaries ------ */
177 
178 /* Define the long and short versions of the keys in an image dictionary, */
179 /* and other strings for images. */
180 typedef struct pdf_image_names_s {
181     pdf_color_space_names_t color_spaces;
182     pdf_filter_names_t filter_names;
183     const char *BitsPerComponent;
184     const char *ColorSpace;
185     const char *Decode;
186     const char *Height;
187     const char *ImageMask;
188     const char *Interpolate;
189     const char *Width;
190 } pdf_image_names_t;
191 #define PDF_IMAGE_PARAM_NAMES\
192     "/BitsPerComponent", "/ColorSpace", "/Decode",\
193     "/Height", "/ImageMask", "/Interpolate", "/Width"
194 #define PDF_IMAGE_PARAM_NAMES_SHORT\
195     "/BPC", "/CS", "/D", "/H", "/IM", "/I", "/W"
196 extern const pdf_image_names_t pdf_image_names_full, pdf_image_names_short;
197 
198 /* Store the values of image parameters other than filters. */
199 /* pdev is used only for updating procsets. */
200 /* pcsvalue is not used for masks. */
201 int pdf_put_image_values(cos_dict_t *pcd, gx_device_pdf *pdev,
202 			 const gs_pixel_image_t *pic,
203 			 const pdf_image_names_t *pin,
204 			 const cos_value_t *pcsvalue);
205 
206 /* Store filters for an image. */
207 /* Currently this only saves parameters for CCITTFaxDecode. */
208 int pdf_put_image_filters(cos_dict_t *pcd, gx_device_pdf *pdev,
209 			  const psdf_binary_writer * pbw,
210 			  const pdf_image_names_t *pin);
211 
212 /* ------ Image writing ------ */
213 
214 /*
215  * Fill in the image parameters for a device space bitmap.
216  * PDF images are always specified top-to-bottom.
217  * data_h is the actual number of data rows, which may be less than h.
218  */
219 void pdf_make_bitmap_matrix(gs_matrix * pmat, int x, int y, int w, int h,
220 			    int h_actual);
221 
222 /* Put out the gsave and matrix for an image. */
223 void pdf_put_image_matrix(gx_device_pdf * pdev, const gs_matrix * pmat,
224 			  floatp y_scale);
225 
226 /* Put out a reference to an image resource. */
227 int pdf_do_image_by_id(gx_device_pdf * pdev, double scale,
228 	     const gs_matrix * pimat, bool in_contents, gs_id id);
229 int pdf_do_image(gx_device_pdf * pdev, const pdf_resource_t * pres,
230 		 const gs_matrix * pimat, bool in_contents);
231 
232 #define pdf_image_writer_num_alt_streams 4
233 
234 /* Define the structure for writing an image. */
235 typedef struct pdf_image_writer_s {
236     psdf_binary_writer binary[pdf_image_writer_num_alt_streams];
237     int alt_writer_count; /* no. of active elements in writer[] (1,2,3) */
238     const pdf_image_names_t *pin;
239     pdf_resource_t *pres;	/* XObject resource iff not in-line */
240     int height;			/* initially specified image height */
241     cos_stream_t *data;
242     const char *end_string;	/* string to write after EI if in-line */
243     cos_dict_t *named;		/* named dictionary from NI */
244     pdf_resource_t *pres_mask;	/* PS2WRITE only : XObject resource for mask */
245 } pdf_image_writer;
246 extern_st(st_pdf_image_writer);	/* public for gdevpdfi.c */
247 #define public_st_pdf_image_writer() /* in gdevpdfj.c */\
248   gs_public_st_composite(st_pdf_image_writer, pdf_image_writer,\
249     "pdf_image_writer", pdf_image_writer_enum_ptrs, pdf_image_writer_reloc_ptrs)
250 #define pdf_image_writer_max_ptrs (psdf_binary_writer_max_ptrs * pdf_image_writer_num_alt_streams + 4)
251 
252 /* Initialize image writer. */
253 void pdf_image_writer_init(pdf_image_writer * piw);
254 
255 /*
256  * Begin writing an image, creating the resource if not in-line, and setting
257  * up the binary writer.  If pnamed != 0, it is a dictionary object created
258  * by a NI pdfmark.
259  */
260 int pdf_begin_write_image(gx_device_pdf * pdev, pdf_image_writer * piw,
261 			  gx_bitmap_id id, int w, int h,
262 			  cos_dict_t *pnamed, bool in_line);
263 
264 /* Begin writing the image data, setting up the dictionary and filters. */
265 int pdf_begin_image_data(gx_device_pdf * pdev, pdf_image_writer * piw,
266 			 const gs_pixel_image_t * pim,
267 			 const cos_value_t *pcsvalue,
268 			 int alt_writer_index);
269 
270 /* Copy the data for a mask or monobit bitmap. */
271 int pdf_copy_mask_bits(stream *s, const byte *base, int sourcex,
272 		       int raster, int w, int h, byte invert);
273 
274 /* Copy the data for a colored image (device pixels). */
275 int pdf_copy_color_bits(stream *s, const byte *base, int sourcex,
276 			int raster, int w, int h, int bytes_per_pixel);
277 
278 /* Complete image data. */
279 int
280 pdf_complete_image_data(gx_device_pdf *pdev, pdf_image_writer *piw, int data_h,
281 			int width, int bits_per_pixel);
282 
283 /* Finish writing the binary image data. */
284 int pdf_end_image_binary(gx_device_pdf *pdev, pdf_image_writer *piw,
285 			 int data_h);
286 
287 /*
288  * Finish writing an image.  If in-line, write the BI/dict/ID/data/EI and
289  * return 1; if a resource, write the resource definition and return 0.
290  */
291 int pdf_end_write_image(gx_device_pdf * pdev, pdf_image_writer * piw);
292 
293 /*
294  *  Make alternative stream for image compression choice.
295  */
296 int pdf_make_alt_stream(gx_device_pdf * pdev, psdf_binary_writer * piw);
297 
298 /*
299  * End binary with choosing image compression.
300  */
301 int pdf_choose_compression(pdf_image_writer * piw, bool end_binary);
302 
303 /* If the current substream is a charproc, register a font used in it. */
304 int pdf_register_charproc_resource(gx_device_pdf *pdev, gs_id id, pdf_resource_type_t type);
305 
306 /* ---------------- Exported by gdevpdfv.c ---------------- */
307 
308 /* Store pattern 1 parameters to cos dictionary. */
309 int pdf_store_pattern1_params(gx_device_pdf *pdev, pdf_resource_t *pres,
310 			gs_pattern1_instance_t *pinst);
311 
312 /* Write a colored Pattern color. */
313 int pdf_put_colored_pattern(gx_device_pdf *pdev, const gx_drawing_color *pdc,
314 			const gs_color_space *pcs,
315 			const psdf_set_color_commands_t *ppscc,
316 			bool have_pattern_streams, pdf_resource_t **ppres);
317 
318 /* Write an uncolored Pattern color. */
319 int pdf_put_uncolored_pattern(gx_device_pdf *pdev, const gx_drawing_color *pdc,
320 			  const gs_color_space *pcs,
321 			  const psdf_set_color_commands_t *ppscc,
322 			  bool have_pattern_streams, pdf_resource_t **ppres);
323 
324 /* Write a PatternType 2 (shading pattern) color. */
325 int pdf_put_pattern2(gx_device_pdf *pdev, const gx_drawing_color *pdc,
326 		 const psdf_set_color_commands_t *ppscc,
327 		 pdf_resource_t **ppres);
328 
329 /* ---------------- Exported by gdevpdfb.c ---------------- */
330 
331 /* Copy a color bitmap.  for_pattern = -1 means put the image in-line, */
332 /* 1 means put the image in a resource. */
333 int pdf_copy_color_data(gx_device_pdf * pdev, const byte * base, int sourcex,
334 		    int raster, gx_bitmap_id id, int x, int y, int w, int h,
335 		    gs_image_t *pim, pdf_image_writer *piw,
336 		    int for_pattern);
337 
338 #endif /* gdevpdfg_INCLUDED */
339