/src/ghostpdl/devices/vector/gdevpdf.c
Line | Count | Source (jump to first uncovered line) |
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 | | /* PDF-writing driver */ |
18 | | #include "fcntl_.h" |
19 | | #include "memory_.h" |
20 | | #include "time_.h" |
21 | | #include "unistd_.h" |
22 | | #include "gx.h" |
23 | | #include "gp.h" /* for gp_get_realtime */ |
24 | | #include "gserrors.h" |
25 | | #include "gxdevice.h" |
26 | | #include "gdevpdfx.h" |
27 | | #include "gdevpdfg.h" /* only for pdf_reset_graphics */ |
28 | | #include "gdevpdfo.h" |
29 | | #include "smd5.h" |
30 | | #include "sarc4.h" |
31 | | #include "sbrotlix.h" |
32 | | #include "gscms.h" |
33 | | #include "gdevpdtf.h" |
34 | | #include "gdevpdtx.h" |
35 | | #include "gdevpdtd.h" |
36 | | #include "gdevpdti.h" |
37 | | #include "gsfcmap.h" /* For gs_cmap_ToUnicode_free */ |
38 | | |
39 | | #include "gxfcache.h" |
40 | | #include "gdevpdts.h" /* for sync_text_state */ |
41 | | |
42 | | #include "gdevpdfo.h" |
43 | | |
44 | | /* Define the default language level and PDF compatibility level. */ |
45 | | /* Acrobat 8 (PDF 1.7) is the default. (1.7 for ICC V4.2.0 profile support) */ |
46 | | #define PSDF_VERSION_INITIAL psdf_version_ll3 |
47 | | #define PDF_COMPATIBILITY_LEVEL_INITIAL 1.7 |
48 | | |
49 | | /* Define the size of internal stream buffers. */ |
50 | | /* (This is not a limitation, it only affects performance.) */ |
51 | 86.4k | #define sbuf_size 512 |
52 | | |
53 | | /* GC descriptors */ |
54 | | private_st_pdf_page(); |
55 | | gs_private_st_element(st_pdf_page_element, pdf_page_t, "pdf_page_t[]", |
56 | | pdf_page_elt_enum_ptrs, pdf_page_elt_reloc_ptrs, |
57 | | st_pdf_page); |
58 | | private_st_pdf_linearisation_record(); |
59 | | gs_private_st_element(st_pdf_linearisation_record_element, pdf_linearisation_record_t, "pdf_linearstion_record_t[]", |
60 | | pdf_linearisation_record_elt_enum_ptrs, pdf_linearisation_record_elt_reloc_ptrs, |
61 | | st_pdf_linearisation_record); |
62 | | private_st_device_pdfwrite(); |
63 | | private_st_pdf_substream_save(); |
64 | | private_st_pdf_substream_save_element(); |
65 | | |
66 | | /* GC procedures */ |
67 | | static |
68 | 127M | ENUM_PTRS_WITH(device_pdfwrite_enum_ptrs, gx_device_pdf *pdev) |
69 | 108M | { |
70 | 108M | index -= gx_device_pdf_num_ptrs + gx_device_pdf_num_param_strings; |
71 | 108M | if (index < NUM_RESOURCE_TYPES * NUM_RESOURCE_CHAINS) |
72 | 99.6M | ENUM_RETURN(pdev->resources[index / NUM_RESOURCE_CHAINS].chains[index % NUM_RESOURCE_CHAINS]); |
73 | 8.74M | index -= NUM_RESOURCE_TYPES * NUM_RESOURCE_CHAINS; |
74 | 8.74M | if (index <= pdev->outline_depth && pdev->outline_levels) |
75 | 71.3k | ENUM_RETURN(pdev->outline_levels[index].first.action); |
76 | 8.67M | index -= pdev->outline_depth + 1; |
77 | 8.67M | if (index <= pdev->outline_depth && pdev->outline_levels) |
78 | 71.3k | ENUM_RETURN(pdev->outline_levels[index].last.action); |
79 | 8.60M | index -= pdev->outline_depth + 1; |
80 | | |
81 | 8.60M | if (index < pdev->PatternDepth) { |
82 | 0 | ENUM_RETURN(pdev->initial_pattern_states[index]); |
83 | 0 | } |
84 | 8.60M | index -= pdev->PatternDepth; |
85 | 8.60M | ENUM_PREFIX(st_device_psdf, 0); |
86 | | |
87 | 8.60M | } |
88 | 8.60M | ENUM_PTR(0, gx_device_pdf, asides.strm); |
89 | 8.60M | ENUM_PTR(1, gx_device_pdf, asides.strm_buf); |
90 | 8.60M | ENUM_PTR(2, gx_device_pdf, asides.save_strm); |
91 | 8.60M | ENUM_PTR(3, gx_device_pdf, streams.strm); |
92 | 8.60M | ENUM_PTR(4, gx_device_pdf, streams.strm_buf); |
93 | 8.60M | ENUM_PTR(5, gx_device_pdf, ObjStm.strm); |
94 | 8.60M | ENUM_PTR(6, gx_device_pdf, ObjStm.strm_buf); |
95 | 8.60M | ENUM_PTR(7, gx_device_pdf, ObjStm.save_strm); |
96 | 8.60M | ENUM_PTR(8, gx_device_pdf, Catalog); |
97 | 8.60M | ENUM_PTR(9, gx_device_pdf, Info); |
98 | 8.60M | ENUM_PTR(10, gx_device_pdf, Pages); |
99 | 8.60M | ENUM_PTR(11, gx_device_pdf, text); |
100 | 8.60M | ENUM_PTR(12, gx_device_pdf, pages); |
101 | 8.60M | ENUM_PTR(13, gx_device_pdf, cs_Patterns[0]); |
102 | 8.60M | ENUM_PTR(14, gx_device_pdf, cs_Patterns[1]); |
103 | 8.60M | ENUM_PTR(15, gx_device_pdf, cs_Patterns[3]); |
104 | 8.60M | ENUM_PTR(16, gx_device_pdf, cs_Patterns[4]); |
105 | 8.60M | ENUM_PTR(17, gx_device_pdf, last_resource); |
106 | 8.60M | ENUM_PTR(18, gx_device_pdf, articles); |
107 | 8.60M | ENUM_PTR(19, gx_device_pdf, Dests); |
108 | 8.60M | ENUM_PTR(20, gx_device_pdf, global_named_objects); |
109 | 8.60M | ENUM_PTR(21, gx_device_pdf, local_named_objects); |
110 | 8.60M | ENUM_PTR(22, gx_device_pdf, NI_stack); |
111 | 8.60M | ENUM_PTR(23, gx_device_pdf, Namespace_stack); |
112 | 8.60M | ENUM_PTR(24, gx_device_pdf, font_cache); |
113 | 8.60M | ENUM_PTR(25, gx_device_pdf, clip_path); |
114 | 8.60M | ENUM_PTR(26, gx_device_pdf, PageLabels); |
115 | 8.60M | ENUM_PTR(27, gx_device_pdf, PageLabels_current_label); |
116 | 8.60M | ENUM_PTR(28, gx_device_pdf, sbstack); |
117 | 8.60M | ENUM_PTR(29, gx_device_pdf, substream_Resources); |
118 | 8.60M | ENUM_PTR(30, gx_device_pdf, font3); |
119 | 8.60M | ENUM_PTR(31, gx_device_pdf, accumulating_substream_resource); |
120 | 8.60M | ENUM_PTR(32, gx_device_pdf, pres_soft_mask_dict); |
121 | 8.60M | ENUM_PTR(33, gx_device_pdf, PDFXTrimBoxToMediaBoxOffset.data); |
122 | 8.60M | ENUM_PTR(34, gx_device_pdf, PDFXBleedBoxToTrimBoxOffset.data); |
123 | 8.60M | ENUM_PTR(35, gx_device_pdf, Identity_ToUnicode_CMaps[0]); |
124 | 8.60M | ENUM_PTR(36, gx_device_pdf, Identity_ToUnicode_CMaps[1]); |
125 | 8.60M | ENUM_PTR(37, gx_device_pdf, vgstack); |
126 | 8.60M | ENUM_PTR(38, gx_device_pdf, outline_levels); |
127 | 8.60M | ENUM_PTR(39, gx_device_pdf, EmbeddedFiles); |
128 | 8.60M | ENUM_PTR(40, gx_device_pdf, AF); |
129 | 8.60M | ENUM_PTR(41, gx_device_pdf, pdf_font_dir); |
130 | 8.60M | ENUM_PTR(42, gx_device_pdf, ExtensionMetadata); |
131 | 8.60M | ENUM_PTR(43, gx_device_pdf, PassThroughWriter); |
132 | 1.95M | #define e1(i,elt) ENUM_PARAM_STRING_PTR(i + gx_device_pdf_num_ptrs, gx_device_pdf, elt); |
133 | 8.60M | gx_device_pdf_do_param_strings(e1) |
134 | 0 | #undef e1 |
135 | 390k | #define e1(i,elt) ENUM_STRING_PTR(i + gx_device_pdf_num_ptrs + gx_device_pdf_num_param_strings,\ |
136 | 390k | gx_device_pdf, elt); |
137 | 127M | gx_device_pdf_do_const_strings(e1) |
138 | 127M | #undef e1 |
139 | 127M | ENUM_PTRS_END |
140 | 390k | static RELOC_PTRS_WITH(device_pdfwrite_reloc_ptrs, gx_device_pdf *pdev) |
141 | 390k | { |
142 | 390k | RELOC_PREFIX(st_device_psdf); |
143 | 390k | RELOC_PTR(gx_device_pdf, asides.strm); |
144 | 390k | RELOC_PTR(gx_device_pdf, asides.strm_buf); |
145 | 390k | RELOC_PTR(gx_device_pdf, asides.save_strm); |
146 | 390k | RELOC_PTR(gx_device_pdf, streams.strm); |
147 | 390k | RELOC_PTR(gx_device_pdf, streams.strm_buf); |
148 | 390k | RELOC_PTR(gx_device_pdf, ObjStm.strm); |
149 | 390k | RELOC_PTR(gx_device_pdf, ObjStm.strm_buf); |
150 | 390k | RELOC_PTR(gx_device_pdf, ObjStm.save_strm); |
151 | 390k | RELOC_PTR(gx_device_pdf, Catalog); |
152 | 390k | RELOC_PTR(gx_device_pdf, Info); |
153 | 390k | RELOC_PTR(gx_device_pdf, Pages); |
154 | 390k | RELOC_PTR(gx_device_pdf, text); |
155 | 390k | RELOC_PTR(gx_device_pdf, pages); |
156 | 390k | RELOC_PTR(gx_device_pdf, cs_Patterns[0]); |
157 | 390k | RELOC_PTR(gx_device_pdf, cs_Patterns[1]); |
158 | 390k | RELOC_PTR(gx_device_pdf, cs_Patterns[3]); |
159 | 390k | RELOC_PTR(gx_device_pdf, cs_Patterns[4]); |
160 | 390k | RELOC_PTR(gx_device_pdf, last_resource); |
161 | 390k | RELOC_PTR(gx_device_pdf, articles); |
162 | 390k | RELOC_PTR(gx_device_pdf, Dests); |
163 | 390k | RELOC_PTR(gx_device_pdf, global_named_objects); |
164 | 390k | RELOC_PTR(gx_device_pdf, local_named_objects); |
165 | 390k | RELOC_PTR(gx_device_pdf, NI_stack); |
166 | 390k | RELOC_PTR(gx_device_pdf, Namespace_stack); |
167 | 390k | RELOC_PTR(gx_device_pdf, font_cache); |
168 | 390k | RELOC_PTR(gx_device_pdf, clip_path); |
169 | 390k | RELOC_PTR(gx_device_pdf, PageLabels); |
170 | 390k | RELOC_PTR(gx_device_pdf, PageLabels_current_label); |
171 | 390k | RELOC_PTR(gx_device_pdf, sbstack); |
172 | 390k | RELOC_PTR(gx_device_pdf, substream_Resources); |
173 | 390k | RELOC_PTR(gx_device_pdf, font3); |
174 | 390k | RELOC_PTR(gx_device_pdf, accumulating_substream_resource); |
175 | 390k | RELOC_PTR(gx_device_pdf, pres_soft_mask_dict); |
176 | 390k | RELOC_PTR(gx_device_pdf, PDFXTrimBoxToMediaBoxOffset.data); |
177 | 390k | RELOC_PTR(gx_device_pdf, PDFXBleedBoxToTrimBoxOffset.data); |
178 | 390k | RELOC_PTR(gx_device_pdf, Identity_ToUnicode_CMaps[0]); |
179 | 390k | RELOC_PTR(gx_device_pdf, Identity_ToUnicode_CMaps[1]); |
180 | 390k | RELOC_PTR(gx_device_pdf, vgstack); |
181 | 390k | RELOC_PTR(gx_device_pdf, EmbeddedFiles); |
182 | 390k | RELOC_PTR(gx_device_pdf, AF); |
183 | 390k | RELOC_PTR(gx_device_pdf, pdf_font_dir); |
184 | 390k | RELOC_PTR(gx_device_pdf, ExtensionMetadata); |
185 | 390k | RELOC_PTR(gx_device_pdf, PassThroughWriter); |
186 | 1.95M | #define r1(i,elt) RELOC_PARAM_STRING_PTR(gx_device_pdf,elt); |
187 | 1.95M | gx_device_pdf_do_param_strings(r1) |
188 | 390k | #undef r1 |
189 | 390k | #define r1(i,elt) RELOC_CONST_STRING_PTR(gx_device_pdf,elt); |
190 | 390k | gx_device_pdf_do_const_strings(r1) |
191 | 390k | #undef r1 |
192 | 390k | { |
193 | 390k | int i, j; |
194 | | |
195 | 6.64M | for (i = 0; i < NUM_RESOURCE_TYPES; ++i) |
196 | 106M | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) |
197 | 100M | RELOC_PTR(gx_device_pdf, resources[i].chains[j]); |
198 | 390k | if (pdev->outline_levels) { |
199 | 142k | for (i = 0; i <= pdev->outline_depth; ++i) { |
200 | 71.3k | RELOC_PTR(gx_device_pdf, outline_levels[i].first.action); |
201 | 71.3k | RELOC_PTR(gx_device_pdf, outline_levels[i].last.action); |
202 | 71.3k | } |
203 | 71.3k | } |
204 | 390k | for (i = 0; i < pdev->PatternDepth; i++) { |
205 | 0 | RELOC_PTR(gx_device_pdf, initial_pattern_states[i]); |
206 | 0 | } |
207 | 390k | } |
208 | 390k | RELOC_PTR(gx_device_pdf, outline_levels); |
209 | 390k | } |
210 | 390k | RELOC_PTRS_END |
211 | | /* Even though device_pdfwrite_finalize is the same as gx_device_finalize, */ |
212 | | /* we need to implement it separately because st_composite_final */ |
213 | | /* declares all 3 procedures as private. */ |
214 | | static void |
215 | | device_pdfwrite_finalize(const gs_memory_t *cmem, void *vpdev) |
216 | 187k | { |
217 | 187k | gx_device_finalize(cmem, vpdev); |
218 | 187k | } |
219 | | |
220 | | /* Driver procedures */ |
221 | | static dev_proc_initialize_device(pdfwrite_initialize_device); |
222 | | static dev_proc_open_device(pdf_open); |
223 | | static dev_proc_output_page(pdf_output_page); |
224 | | static dev_proc_close_device(pdf_close); |
225 | | /* Driver procedures defined in other files are declared in gdevpdfx.h. */ |
226 | | |
227 | | #ifndef X_DPI |
228 | | # define X_DPI 720 |
229 | | #endif |
230 | | #ifndef Y_DPI |
231 | | # define Y_DPI 720 |
232 | | #endif |
233 | | |
234 | | /* ---------------- Device prototype ---------------- */ |
235 | | |
236 | | static void |
237 | | pdfwrite_initialize_device_procs(gx_device *dev) |
238 | 187k | { |
239 | 187k | set_dev_proc(dev, initialize_device, pdfwrite_initialize_device); |
240 | 187k | set_dev_proc(dev, open_device, pdf_open); |
241 | 187k | set_dev_proc(dev, get_initial_matrix, gx_upright_get_initial_matrix); |
242 | 187k | set_dev_proc(dev, output_page, pdf_output_page); |
243 | 187k | set_dev_proc(dev, close_device, pdf_close); |
244 | 187k | set_dev_proc(dev, map_rgb_color, gx_default_rgb_map_rgb_color); |
245 | 187k | set_dev_proc(dev, map_color_rgb, gx_default_rgb_map_color_rgb); |
246 | 187k | set_dev_proc(dev, fill_rectangle, gdev_pdf_fill_rectangle); |
247 | 187k | set_dev_proc(dev, copy_mono, gdev_pdf_copy_mono); |
248 | 187k | set_dev_proc(dev, copy_color, gdev_pdf_copy_color); |
249 | 187k | set_dev_proc(dev, get_params, gdev_pdf_get_params); |
250 | 187k | set_dev_proc(dev, put_params, gdev_pdf_put_params); |
251 | 187k | set_dev_proc(dev, get_page_device, gx_page_device_get_page_device); |
252 | 187k | set_dev_proc(dev, fill_path, gdev_pdf_fill_path); |
253 | 187k | set_dev_proc(dev, stroke_path, gdev_pdf_stroke_path); |
254 | 187k | set_dev_proc(dev, fill_mask, gdev_pdf_fill_mask); |
255 | 187k | set_dev_proc(dev, strip_tile_rectangle, gdev_pdf_strip_tile_rectangle); |
256 | 187k | set_dev_proc(dev, begin_typed_image, gdev_pdf_begin_typed_image); |
257 | 187k | set_dev_proc(dev, get_bits_rectangle, psdf_get_bits_rectangle); |
258 | 187k | set_dev_proc(dev, composite, gdev_pdf_composite); |
259 | 187k | set_dev_proc(dev, text_begin, gdev_pdf_text_begin); |
260 | 187k | set_dev_proc(dev, begin_transparency_group, gdev_pdf_begin_transparency_group); |
261 | 187k | set_dev_proc(dev, end_transparency_group, gdev_pdf_end_transparency_group); |
262 | 187k | set_dev_proc(dev, begin_transparency_mask ,gdev_pdf_begin_transparency_mask); |
263 | 187k | set_dev_proc(dev, end_transparency_mask, gdev_pdf_end_transparency_mask); |
264 | 187k | set_dev_proc(dev, fill_rectangle_hl_color, gdev_pdf_fill_rectangle_hl_color); |
265 | 187k | set_dev_proc(dev, include_color_space, gdev_pdf_include_color_space); |
266 | 187k | set_dev_proc(dev, fillpage, gdev_pdf_fillpage); |
267 | 187k | set_dev_proc(dev, dev_spec_op, gdev_pdf_dev_spec_op); |
268 | 187k | set_dev_proc(dev, fill_stroke_path, gdev_pdf_fill_stroke_path); |
269 | 187k | } |
270 | | |
271 | | #define PDF_DEVICE_NAME "pdfwrite" |
272 | | #define PDF_DEVICE_IDENT gs_pdfwrite_device |
273 | | #define PDF_DEVICE_MaxInlineImageSize 4000 |
274 | | #define PDF_FOR_OPDFREAD 0 |
275 | | #define PDF_FOR_EPS2WRITE 0 |
276 | | |
277 | | #include "gdevpdfb.h" |
278 | | |
279 | | #undef PDF_DEVICE_NAME |
280 | | #undef PDF_DEVICE_IDENT |
281 | | #undef PDF_DEVICE_MaxInlineImageSize |
282 | | #undef PDF_FOR_OPDFREAD |
283 | | |
284 | | #define PDF_DEVICE_NAME "ps2write" |
285 | | #define PDF_DEVICE_IDENT gs_ps2write_device |
286 | | #define PDF_DEVICE_MaxInlineImageSize max_long |
287 | | #define PDF_FOR_OPDFREAD 1 |
288 | | #define PDF_FOR_EPS2WRITE 0 |
289 | | |
290 | | #include "gdevpdfb.h" |
291 | | |
292 | | #undef PDF_DEVICE_NAME |
293 | | #undef PDF_DEVICE_IDENT |
294 | | #undef PDF_DEVICE_MaxInlineImageSize |
295 | | #undef PDF_FOR_OPDFREAD |
296 | | #undef PDF_FOR_EPS2WRITE |
297 | | |
298 | | #define PDF_DEVICE_NAME "eps2write" |
299 | | #define PDF_DEVICE_IDENT gs_eps2write_device |
300 | | #define PDF_DEVICE_MaxInlineImageSize max_long |
301 | | #define PDF_FOR_OPDFREAD 1 |
302 | | #define PDF_FOR_EPS2WRITE 1 |
303 | | |
304 | | #include "gdevpdfb.h" |
305 | | |
306 | | #undef PDF_DEVICE_NAME |
307 | | #undef PDF_DEVICE_IDENT |
308 | | #undef PDF_DEVICE_MaxInlineImageSize |
309 | | #undef PDF_FOR_OPDFREAD |
310 | | #undef PDF_FOR_EPS2WRITE |
311 | | /* ---------------- Device open/close ---------------- */ |
312 | | |
313 | | /* Close and remove temporary files. */ |
314 | | int |
315 | | pdf_close_temp_file(gx_device_pdf *pdev, pdf_temp_file_t *ptf, int code) |
316 | 154k | { |
317 | 154k | int err = 0; |
318 | 154k | gp_file *file = ptf->file; |
319 | | |
320 | | /* |
321 | | * ptf->strm == 0 or ptf->file == 0 is only possible if this procedure |
322 | | * is called to clean up during initialization failure, but ptf->strm |
323 | | * might not be open if it was finalized before the device was closed. |
324 | | */ |
325 | 154k | if (ptf->strm) { |
326 | 86.4k | if (s_is_valid(ptf->strm)) { |
327 | 86.4k | sflush(ptf->strm); |
328 | | /* Prevent freeing the stream from closing the file. */ |
329 | 86.4k | ptf->strm->file = 0; |
330 | 86.4k | } else |
331 | 0 | ptf->file = file = 0; /* file was closed by finalization */ |
332 | 86.4k | gs_free_object(pdev->pdf_memory, ptf->strm_buf, |
333 | 86.4k | "pdf_close_temp_file(strm_buf)"); |
334 | 86.4k | ptf->strm_buf = 0; |
335 | 86.4k | gs_free_object(pdev->pdf_memory, ptf->strm, |
336 | 86.4k | "pdf_close_temp_file(strm)"); |
337 | 86.4k | ptf->strm = 0; |
338 | 86.4k | } |
339 | 154k | if (file) { |
340 | 120k | err = gp_ferror(file) | gp_fclose(file); |
341 | 120k | gp_unlink(pdev->memory, ptf->file_name); |
342 | 120k | ptf->file = 0; |
343 | 120k | } |
344 | 154k | ptf->save_strm = 0; |
345 | 154k | return |
346 | 154k | (code < 0 ? code : err != 0 ? gs_note_error(gs_error_ioerror) : code); |
347 | 154k | } |
348 | | static int |
349 | | pdf_close_files(gx_device_pdf * pdev, int code) |
350 | 34.0k | { |
351 | 34.0k | code = pdf_close_temp_file(pdev, &pdev->ObjStm, code); |
352 | 34.0k | code = pdf_close_temp_file(pdev, &pdev->streams, code); |
353 | 34.0k | code = pdf_close_temp_file(pdev, &pdev->asides, code); |
354 | 34.0k | return pdf_close_temp_file(pdev, &pdev->xref, code); |
355 | 34.0k | } |
356 | | |
357 | | /* Reset the state of the current page. */ |
358 | | static void |
359 | | pdf_reset_page(gx_device_pdf * pdev) |
360 | 79.3k | { |
361 | 79.3k | pdev->page_dsc_info = gs_pdfwrite_device.page_dsc_info; |
362 | 79.3k | pdev->contents_id = 0; |
363 | 79.3k | pdf_reset_graphics(pdev); |
364 | 79.3k | pdev->procsets = NoMarks; |
365 | 79.3k | memset(pdev->cs_Patterns, 0, sizeof(pdev->cs_Patterns)); /* simplest to create for each page */ |
366 | 79.3k | pdf_reset_text_page(pdev->text); |
367 | 79.3k | pdf_remember_clip_path(pdev, 0); |
368 | 79.3k | pdev->clip_path_id = pdev->no_clip_path_id; |
369 | 79.3k | } |
370 | | |
371 | | /* Open a temporary file, with or without a stream. */ |
372 | | int |
373 | | pdf_open_temp_file(gx_device_pdf *pdev, pdf_temp_file_t *ptf) |
374 | 120k | { |
375 | 120k | char fmode[4]; |
376 | | |
377 | 120k | if (strlen(gp_fmode_binary_suffix) > 2) |
378 | 0 | return_error(gs_error_invalidfileaccess); |
379 | | |
380 | 120k | strcpy(fmode, "w+"); |
381 | 120k | strcat(fmode, gp_fmode_binary_suffix); |
382 | 120k | ptf->file = gp_open_scratch_file(pdev->memory, |
383 | 120k | gp_scratch_file_name_prefix, |
384 | 120k | ptf->file_name, |
385 | 120k | fmode); |
386 | 120k | if (ptf->file == 0) |
387 | 0 | return_error(gs_error_invalidfileaccess); |
388 | 120k | return 0; |
389 | 120k | } |
390 | | int |
391 | | pdf_open_temp_stream(gx_device_pdf *pdev, pdf_temp_file_t *ptf) |
392 | 86.4k | { |
393 | 86.4k | int code = pdf_open_temp_file(pdev, ptf); |
394 | | |
395 | 86.4k | if (code < 0) |
396 | 0 | return code; |
397 | 86.4k | ptf->strm = s_alloc(pdev->pdf_memory, "pdf_open_temp_stream(strm)"); |
398 | 86.4k | if (ptf->strm == 0) |
399 | 0 | return_error(gs_error_VMerror); |
400 | 86.4k | ptf->strm_buf = gs_alloc_bytes(pdev->pdf_memory, sbuf_size, |
401 | 86.4k | "pdf_open_temp_stream(strm_buf)"); |
402 | 86.4k | if (ptf->strm_buf == 0) { |
403 | 0 | gs_free_object(pdev->pdf_memory, ptf->strm, |
404 | 0 | "pdf_open_temp_stream(strm)"); |
405 | 0 | ptf->strm = 0; |
406 | 0 | return_error(gs_error_VMerror); |
407 | 0 | } |
408 | 86.4k | swrite_file(ptf->strm, ptf->file, ptf->strm_buf, sbuf_size); |
409 | 86.4k | return 0; |
410 | 86.4k | } |
411 | | |
412 | | /* Initialize the IDs allocated at startup. */ |
413 | | void |
414 | | pdf_initialize_ids(gx_device_pdf * pdev) |
415 | 34.0k | { |
416 | 34.0k | gs_param_string nstr; |
417 | | |
418 | 34.0k | pdev->next_id = pdev->FirstObjectNumber; |
419 | | |
420 | | /* Initialize the Catalog. */ |
421 | | |
422 | 34.0k | param_string_from_string(nstr, "{Catalog}"); |
423 | 34.0k | pdf_create_named_dict(pdev, &nstr, &pdev->Catalog, 0L); |
424 | | |
425 | | /* Initialize the Info dictionary. */ |
426 | | |
427 | 34.0k | param_string_from_string(nstr, "{DocInfo}"); |
428 | 34.0k | pdf_create_named_dict(pdev, &nstr, &pdev->Info, 0L); |
429 | 34.0k | { |
430 | 34.0k | char buf[PDF_MAX_PRODUCER]; |
431 | | |
432 | 34.0k | pdf_store_default_Producer(buf); |
433 | 34.0k | if (pdev->CompatibilityLevel <= 1.7) |
434 | 34.0k | cos_dict_put_c_key_string(pdev->Info, "/Producer", (byte *)buf, |
435 | 34.0k | strlen(buf)); |
436 | 34.0k | } |
437 | | /* |
438 | | * Acrobat Distiller sets CreationDate and ModDate to the current |
439 | | * date and time, rather than (for example) %%CreationDate from the |
440 | | * PostScript file. We think this is wrong, but we do the same. |
441 | | */ |
442 | 34.0k | if (!pdev->OmitInfoDate) |
443 | 34.0k | { |
444 | 34.0k | struct tm tms; |
445 | 34.0k | time_t t; |
446 | 34.0k | char buf[1+2+4+2+2+2+2+2+1+2+1+2+1+1+1]; /* (D:yyyymmddhhmmssZhh'mm')\0 */ |
447 | 34.0k | int timeoffset; |
448 | 34.0k | char timesign; |
449 | | |
450 | | #ifdef CLUSTER |
451 | | memset(&t, 0, sizeof(t)); |
452 | | memset(&tms, 0, sizeof(tms)); |
453 | | timesign = 'Z'; |
454 | | timeoffset = 0; |
455 | | #else |
456 | 34.0k | time(&t); |
457 | 34.0k | tms = *gmtime(&t); |
458 | 34.0k | tms.tm_isdst = -1; |
459 | 34.0k | timeoffset = (int)difftime(t, mktime(&tms)); /* tz+dst in seconds */ |
460 | 34.0k | timesign = (timeoffset == 0 ? 'Z' : timeoffset < 0 ? '-' : '+'); |
461 | 34.0k | timeoffset = any_abs(timeoffset) / 60; |
462 | 34.0k | tms = *localtime(&t); |
463 | 34.0k | #endif |
464 | | |
465 | 34.0k | gs_snprintf(buf, sizeof(buf), "(D:%04d%02d%02d%02d%02d%02d%c%02d\'%02d\')", |
466 | 34.0k | tms.tm_year + 1900, tms.tm_mon + 1, tms.tm_mday, |
467 | 34.0k | tms.tm_hour, tms.tm_min, tms.tm_sec, |
468 | 34.0k | timesign, timeoffset / 60, timeoffset % 60); |
469 | | |
470 | 34.0k | cos_dict_put_c_key_string(pdev->Info, "/CreationDate", (byte *)buf, |
471 | 34.0k | strlen(buf)); |
472 | 34.0k | cos_dict_put_c_key_string(pdev->Info, "/ModDate", (byte *)buf, |
473 | 34.0k | strlen(buf)); |
474 | 34.0k | } |
475 | | |
476 | | /* Allocate the root of the pages tree. */ |
477 | | |
478 | 34.0k | pdf_create_named_dict(pdev, NULL, &pdev->Pages, 0L); |
479 | 34.0k | } |
480 | | |
481 | | static int |
482 | | pdf_compute_fileID(gx_device_pdf * pdev) |
483 | 34.0k | { |
484 | | |
485 | | /* We compute a file identifier when beginning a document |
486 | | to allow its usage with PDF encryption. Due to that, |
487 | | in contradiction to the Adobe recommendation, our |
488 | | ID doesn't depend on the document size. |
489 | | */ |
490 | 34.0k | gs_memory_t *mem = pdev->pdf_memory; |
491 | 34.0k | stream *strm = pdev->strm; |
492 | 34.0k | uint ignore; |
493 | 34.0k | int code; |
494 | 34.0k | stream *s = s_MD5E_make_stream(mem, pdev->fileID, sizeof(pdev->fileID)); |
495 | 34.0k | long secs_ns[2]; |
496 | 34.0k | uint KeyLength = pdev->KeyLength; |
497 | | |
498 | 34.0k | if (s == NULL) |
499 | 0 | return_error(gs_error_VMerror); |
500 | 34.0k | pdev->KeyLength = 0; /* Disable encryption. Not so important though. */ |
501 | | #ifdef CLUSTER |
502 | | secs_ns[0] = 0; |
503 | | secs_ns[1] = 0; |
504 | | #else |
505 | 34.0k | gp_get_realtime(secs_ns); |
506 | 34.0k | #endif |
507 | 34.0k | sputs(s, (byte *)secs_ns, sizeof(secs_ns), &ignore); |
508 | | #ifdef CLUSTER |
509 | | /* Don't have the ID's vary by filename output in the cluster testing. |
510 | | * This prevents us comparing gs to gpdl results, and makes it harder |
511 | | * to manually reproduce results. */ |
512 | | sputs(s, (const byte *)"ClusterTest.pdf", strlen("ClusterTest.pdf"), &ignore); |
513 | | #else |
514 | 34.0k | sputs(s, (const byte *)pdev->fname, strlen(pdev->fname), &ignore); |
515 | 34.0k | #endif |
516 | 34.0k | pdev->strm = s; |
517 | 34.0k | code = cos_dict_elements_write(pdev->Info, pdev); |
518 | 34.0k | pdev->strm = strm; |
519 | 34.0k | pdev->KeyLength = KeyLength; |
520 | 34.0k | if (code < 0) |
521 | 0 | return code; |
522 | | /* Generally we would call s_close_filters() here in order to free the data buffer |
523 | | * associated with the MD5 filter, but the data buffer we passed in to s_MD5E_make_stream() |
524 | | * is part of the device structure, so we must *NOT* free that buffer. Therefore we must |
525 | | * instead call sclose(). This confusion over ownership of the stream buffers causes |
526 | | * a lot of problems...... |
527 | | */ |
528 | 34.0k | sclose(s); |
529 | 34.0k | gs_free_object(mem, s, "pdf_compute_fileID"); |
530 | 34.0k | return 0; |
531 | 34.0k | } |
532 | | |
533 | | static const byte pad[32] = { 0x28, 0xBF, 0x4E, 0x5E, 0x4E, 0x75, 0x8A, 0x41, |
534 | | 0x64, 0x00, 0x4E, 0x56, 0xFF, 0xFA, 0x01, 0x08, |
535 | | 0x2E, 0x2E, 0x00, 0xB6, 0xD0, 0x68, 0x3E, 0x80, |
536 | | 0x2F, 0x0C, 0xA9, 0xFE, 0x64, 0x53, 0x69, 0x7A}; |
537 | | |
538 | | static inline void |
539 | | copy_padded(byte buf[32], gs_param_string *str) |
540 | 0 | { |
541 | 0 | memcpy(buf, str->data, min(str->size, 32)); |
542 | 0 | if (32 > str->size) |
543 | 0 | memcpy(buf + str->size, pad, 32 - str->size); |
544 | 0 | } |
545 | | |
546 | | static void |
547 | | Adobe_magic_loop_50(byte digest[16], int key_length) |
548 | 0 | { |
549 | 0 | gs_md5_state_t md5; |
550 | 0 | int i; |
551 | |
|
552 | 0 | for (i = 0; i < 50; i++) { |
553 | 0 | gs_md5_init(&md5); |
554 | 0 | gs_md5_append(&md5, digest, key_length); |
555 | 0 | gs_md5_finish(&md5, digest); |
556 | 0 | } |
557 | 0 | } |
558 | | |
559 | | static void |
560 | | Adobe_magic_loop_19(byte *data, int data_size, const byte *key, int key_size) |
561 | 0 | { |
562 | 0 | stream_arcfour_state sarc4; |
563 | 0 | byte key_buf[16]; |
564 | 0 | int i, j; |
565 | |
|
566 | 0 | for (i = 1; i <= 19; i++) { |
567 | 0 | for (j = 0; j < key_size; j++) |
568 | 0 | key_buf[j] = key[j] ^ (byte)i; |
569 | 0 | s_arcfour_set_key(&sarc4, key_buf, key_size); |
570 | 0 | s_arcfour_process_buffer(&sarc4, data, data_size); |
571 | 0 | } |
572 | 0 | } |
573 | | |
574 | | static int |
575 | | pdf_compute_encryption_data(gx_device_pdf * pdev) |
576 | 0 | { |
577 | 0 | gs_md5_state_t md5; |
578 | 0 | byte digest[16], buf[32], t; |
579 | 0 | stream_arcfour_state sarc4; |
580 | |
|
581 | 0 | if (pdev->PDFX > 1 && pdev->KeyLength != 0) { |
582 | 0 | emprintf(pdev->memory, |
583 | 0 | "Encryption is not allowed in a PDF/X-3 doucment.\n"); |
584 | 0 | return_error(gs_error_rangecheck); |
585 | 0 | } |
586 | 0 | if (pdev->KeyLength == 0) |
587 | 0 | pdev->KeyLength = 40; |
588 | 0 | if (pdev->EncryptionV == 0 && pdev->KeyLength == 40) |
589 | 0 | pdev->EncryptionV = 1; |
590 | 0 | if (pdev->EncryptionV == 0 && pdev->KeyLength > 40) |
591 | 0 | pdev->EncryptionV = 2; |
592 | 0 | if (pdev->EncryptionV > 1 && pdev->CompatibilityLevel < 1.4) { |
593 | 0 | emprintf(pdev->memory, "PDF 1.3 only supports 40 bits keys.\n"); |
594 | 0 | return_error(gs_error_rangecheck); |
595 | 0 | } |
596 | 0 | if (pdev->EncryptionR == 0) |
597 | 0 | pdev->EncryptionR = 2; |
598 | 0 | if (pdev->EncryptionR < 2 || pdev->EncryptionR > 3) { |
599 | 0 | emprintf(pdev->memory, |
600 | 0 | "Encryption revisions 2 and 3 are only supported.\n"); |
601 | 0 | return_error(gs_error_rangecheck); |
602 | 0 | } |
603 | 0 | if (pdev->EncryptionR > 2 && pdev->CompatibilityLevel < 1.4) { |
604 | 0 | emprintf(pdev->memory, |
605 | 0 | "PDF 1.3 only supports the encryption revision 2.\n"); |
606 | 0 | return_error(gs_error_rangecheck); |
607 | 0 | } |
608 | 0 | if (pdev->KeyLength > 128) { |
609 | 0 | emprintf(pdev->memory, |
610 | 0 | "The maximal length of PDF encryption key is 128 bits.\n"); |
611 | 0 | return_error(gs_error_rangecheck); |
612 | 0 | } |
613 | 0 | if (pdev->KeyLength % 8) { |
614 | 0 | emprintf(pdev->memory, |
615 | 0 | "PDF encryption key length must be a multiple of 8.\n"); |
616 | 0 | return_error(gs_error_rangecheck); |
617 | 0 | } |
618 | 0 | if (pdev->EncryptionR == 2 && |
619 | 0 | ((pdev->Permissions & (0xFFFFFFC3)) != 0xFFFFFFC0)) { |
620 | 0 | emprintf(pdev->memory, |
621 | 0 | "Some of Permissions are not allowed with R=2.\n"); |
622 | 0 | return_error(gs_error_rangecheck); |
623 | 0 | } |
624 | 0 | if (pdev->EncryptionV == 2 && pdev->EncryptionR == 2 && pdev->KeyLength > 40) { |
625 | 0 | emprintf(pdev->memory, "Encryption version 2 revision 2 with " |
626 | 0 | "KeyLength > 40 appears incompatible to some viewers. With " |
627 | 0 | "long keys use revision 3.\n"); |
628 | 0 | return_error(gs_error_rangecheck); |
629 | 0 | } |
630 | | /* Compute O : */ |
631 | 0 | gs_md5_init(&md5); |
632 | 0 | copy_padded(buf, &pdev->OwnerPassword); |
633 | 0 | gs_md5_append(&md5, buf, sizeof(buf)); |
634 | 0 | gs_md5_finish(&md5, digest); |
635 | 0 | if (pdev->EncryptionR == 3) |
636 | 0 | Adobe_magic_loop_50(digest, pdev->KeyLength / 8); |
637 | 0 | copy_padded(buf, &pdev->UserPassword); |
638 | 0 | s_arcfour_set_key(&sarc4, digest, pdev->KeyLength / 8); |
639 | 0 | s_arcfour_process_buffer(&sarc4, buf, sizeof(buf)); |
640 | 0 | if (pdev->EncryptionR == 3) |
641 | 0 | Adobe_magic_loop_19(buf, sizeof(buf), digest, pdev->KeyLength / 8); |
642 | 0 | memcpy(pdev->EncryptionO, buf, sizeof(pdev->EncryptionO)); |
643 | | /* Compute Key : */ |
644 | 0 | gs_md5_init(&md5); |
645 | 0 | copy_padded(buf, &pdev->UserPassword); |
646 | 0 | gs_md5_append(&md5, buf, sizeof(buf)); |
647 | 0 | gs_md5_append(&md5, pdev->EncryptionO, sizeof(pdev->EncryptionO)); |
648 | 0 | t = (byte)(pdev->Permissions >> 0); gs_md5_append(&md5, &t, 1); |
649 | 0 | t = (byte)(pdev->Permissions >> 8); gs_md5_append(&md5, &t, 1); |
650 | 0 | t = (byte)(pdev->Permissions >> 16); gs_md5_append(&md5, &t, 1); |
651 | 0 | t = (byte)(pdev->Permissions >> 24); gs_md5_append(&md5, &t, 1); |
652 | 0 | gs_md5_append(&md5, pdev->fileID, sizeof(pdev->fileID)); |
653 | 0 | if (pdev->EncryptionR == 3) |
654 | 0 | if (!pdev->EncryptMetadata) { |
655 | 0 | const byte v[4] = {0xFF, 0xFF, 0xFF, 0xFF}; |
656 | |
|
657 | 0 | gs_md5_append(&md5, v, 4); |
658 | 0 | } |
659 | 0 | gs_md5_finish(&md5, digest); |
660 | 0 | if (pdev->EncryptionR == 3) |
661 | 0 | Adobe_magic_loop_50(digest, pdev->KeyLength / 8); |
662 | 0 | memcpy(pdev->EncryptionKey, digest, pdev->KeyLength / 8); |
663 | | /* Compute U : */ |
664 | 0 | if (pdev->EncryptionR == 3) { |
665 | 0 | gs_md5_init(&md5); |
666 | 0 | gs_md5_append(&md5, pad, sizeof(pad)); |
667 | 0 | gs_md5_append(&md5, pdev->fileID, sizeof(pdev->fileID)); |
668 | 0 | gs_md5_finish(&md5, digest); |
669 | 0 | s_arcfour_set_key(&sarc4, pdev->EncryptionKey, pdev->KeyLength / 8); |
670 | 0 | s_arcfour_process_buffer(&sarc4, digest, sizeof(digest)); |
671 | 0 | Adobe_magic_loop_19(digest, sizeof(digest), pdev->EncryptionKey, pdev->KeyLength / 8); |
672 | 0 | memcpy(pdev->EncryptionU, digest, sizeof(digest)); |
673 | 0 | memcpy(pdev->EncryptionU + sizeof(digest), pad, |
674 | 0 | sizeof(pdev->EncryptionU) - sizeof(digest)); |
675 | 0 | } else { |
676 | 0 | memcpy(pdev->EncryptionU, pad, sizeof(pdev->EncryptionU)); |
677 | 0 | s_arcfour_set_key(&sarc4, pdev->EncryptionKey, pdev->KeyLength / 8); |
678 | 0 | s_arcfour_process_buffer(&sarc4, pdev->EncryptionU, sizeof(pdev->EncryptionU)); |
679 | 0 | } |
680 | 0 | return 0; |
681 | 0 | } |
682 | | |
683 | | #ifdef __DECC |
684 | | /* The ansi alias rules are violated in this next routine. Tell the compiler |
685 | | to ignore this. |
686 | | */ |
687 | | #pragma optimize save |
688 | | #pragma optimize ansi_alias=off |
689 | | #endif |
690 | | /* |
691 | | * Update the color mapping procedures after setting ProcessColorModel. |
692 | | * |
693 | | * The 'index' value indicates the ProcessColorModel. |
694 | | * 0 = DeviceGray |
695 | | * 1 = DeviceRGB |
696 | | * 2 = DeviceCMYK |
697 | | * 3 = DeviceN (treat like CMYK except for color model name) |
698 | | */ |
699 | | void |
700 | | pdf_set_process_color_model(gx_device_pdf * pdev, int index) |
701 | 21.9k | { |
702 | 21.9k | const static gx_device_color_info pcm_color_info[] = { |
703 | 21.9k | dci_values(1, 8, 255, 0, 256, 0), /* Gray */ |
704 | 21.9k | dci_values(3, 24, 255, 255, 256, 256), /* RGB */ |
705 | 21.9k | dci_values(4, 32, 255, 255, 256, 256), /* CMYK */ |
706 | 21.9k | dci_values(4, 32, 255, 255, 256, 256) /* Treat DeviceN like CMYK */ |
707 | 21.9k | }; |
708 | | /* Don't blow away the ICC information */ |
709 | | |
710 | 21.9k | pdev->pcm_color_info_index = index; |
711 | 21.9k | pdev->color_info = pcm_color_info[index]; |
712 | | /* Set the separable and linear shift, masks, bits. */ |
713 | 21.9k | set_linear_color_bits_mask_shift((gx_device *)pdev); |
714 | 21.9k | pdev->color_info.separable_and_linear = GX_CINFO_SEP_LIN; |
715 | | /* |
716 | | * The conversion from PS to PDF should be transparent as possible. |
717 | | * Particularly it should not change representation of colors. |
718 | | * Perhaps due to historical reasons the source color information |
719 | | * sometimes isn't accessible from device methods, and |
720 | | * therefore they perform a mapping of colors to |
721 | | * an output color model. Here we handle some color models, |
722 | | * which were selected almost due to antique reasons. |
723 | | */ |
724 | 21.9k | switch (index) { |
725 | 0 | case 0: /* DeviceGray */ |
726 | 0 | set_dev_proc(pdev, map_rgb_color, gx_default_gray_map_rgb_color); |
727 | 0 | set_dev_proc(pdev, map_color_rgb, gx_default_gray_map_color_rgb); |
728 | 0 | set_dev_proc(pdev, map_cmyk_color, NULL); |
729 | 0 | set_dev_proc(pdev, get_color_mapping_procs, |
730 | 0 | gx_default_DevGray_get_color_mapping_procs); |
731 | 0 | set_dev_proc(pdev, get_color_comp_index, |
732 | 0 | gx_default_DevGray_get_color_comp_index); |
733 | 0 | set_dev_proc(pdev, encode_color, gx_default_gray_encode); |
734 | 0 | set_dev_proc(pdev, decode_color, gx_default_decode_color); |
735 | 0 | break; |
736 | 21.9k | case 1: /* DeviceRGB */ |
737 | 21.9k | set_dev_proc(pdev, map_rgb_color, gx_default_rgb_map_rgb_color); |
738 | 21.9k | set_dev_proc(pdev, map_color_rgb, gx_default_rgb_map_color_rgb); |
739 | 21.9k | set_dev_proc(pdev, map_cmyk_color, NULL); |
740 | 21.9k | set_dev_proc(pdev, get_color_mapping_procs, |
741 | 21.9k | gx_default_DevRGB_get_color_mapping_procs); |
742 | 21.9k | set_dev_proc(pdev, get_color_comp_index, |
743 | 21.9k | gx_default_DevRGB_get_color_comp_index); |
744 | 21.9k | set_dev_proc(pdev, encode_color, gx_default_rgb_map_rgb_color); |
745 | 21.9k | set_dev_proc(pdev, decode_color, gx_default_rgb_map_color_rgb); |
746 | 21.9k | break; |
747 | 0 | case 3: /* DeviceN - treat like DeviceCMYK except for cm_name */ |
748 | 0 | pdev->color_info.cm_name = "DeviceN"; |
749 | 0 | case 2: /* DeviceCMYK */ |
750 | 0 | set_dev_proc(pdev, map_rgb_color, NULL); |
751 | 0 | set_dev_proc(pdev, map_color_rgb, cmyk_8bit_map_color_rgb); |
752 | | /* possible problems with aliassing on next statement */ |
753 | 0 | set_dev_proc(pdev, map_cmyk_color, cmyk_8bit_map_cmyk_color); |
754 | 0 | set_dev_proc(pdev, get_color_mapping_procs, |
755 | 0 | gx_default_DevCMYK_get_color_mapping_procs); |
756 | 0 | set_dev_proc(pdev, get_color_comp_index, |
757 | 0 | gx_default_DevCMYK_get_color_comp_index); |
758 | 0 | set_dev_proc(pdev, encode_color, cmyk_8bit_map_cmyk_color); |
759 | 0 | set_dev_proc(pdev, decode_color, cmyk_8bit_map_color_cmyk); |
760 | 0 | break; |
761 | 0 | default: /* can't happen - see the call from gdev_pdf_put_params. */ |
762 | 0 | DO_NOTHING; |
763 | 21.9k | } |
764 | 21.9k | } |
765 | | #ifdef __DECC |
766 | | #pragma optimize restore |
767 | | #endif |
768 | | |
769 | | /* |
770 | | * Reset the text state parameters to initial values. |
771 | | */ |
772 | | void |
773 | | pdf_reset_text(gx_device_pdf * pdev) |
774 | 486k | { |
775 | | /* we need to flush the text buffer, in case we have (eg) Tr set, |
776 | | * but have reset it to 0 for the current (buffered) text. If we restore to a |
777 | | * graphics state which also has Tr 0 then we won't ever write out the change. |
778 | | * I suspect this can theoretically happen with other graphics state values too |
779 | | * See PCL file enter.test. |
780 | | */ |
781 | 486k | sync_text_state(pdev); |
782 | 486k | pdf_reset_text_state(pdev->text); |
783 | 486k | } |
784 | | |
785 | | static int |
786 | | pdfwrite_initialize_device(gx_device *dev) |
787 | 187k | { |
788 | | #if OCR_VERSION > 0 |
789 | | gx_device_pdf *pdev = (gx_device_pdf *) dev; |
790 | | const char *default_ocr_lang = "eng"; |
791 | | pdev->ocr_language[0] = '\0'; |
792 | | strcpy(pdev->ocr_language, default_ocr_lang); |
793 | | #endif |
794 | 187k | return 0; |
795 | 187k | } |
796 | | |
797 | | |
798 | | /* Open the device. */ |
799 | | static int |
800 | | pdf_open(gx_device * dev) |
801 | 34.0k | { |
802 | 34.0k | gx_device_pdf *pdev = (gx_device_pdf *) dev; |
803 | 34.0k | gs_memory_t *mem = pdev->pdf_memory = gs_memory_stable(pdev->memory); |
804 | 34.0k | int code; |
805 | | |
806 | 34.0k | pdev->InOutputPage = false; |
807 | | |
808 | 34.0k | if ((code = pdf_open_temp_file(pdev, &pdev->xref)) < 0 || |
809 | 34.0k | (code = pdf_open_temp_stream(pdev, &pdev->asides)) < 0 || |
810 | 34.0k | (code = pdf_open_temp_stream(pdev, &pdev->streams)) |
811 | 34.0k | ) |
812 | 0 | goto fail; |
813 | 34.0k | code = gdev_vector_open_file((gx_device_vector *) pdev, sbuf_size); |
814 | 34.0k | if (code < 0) |
815 | 0 | goto fail; |
816 | 34.0k | while (pdev->child) { |
817 | | /* we've been subclassed by gdev_vector_open_file. Ordinarily we would want to call |
818 | | * open_file last, in order to make sure that we don't care if we are subclessed |
819 | | * but we want to set up the stream, so we can't do that.... |
820 | | */ |
821 | 0 | pdev = (gx_device_pdf *)pdev->child; |
822 | 0 | } |
823 | 34.0k | if (pdev->ComputeDocumentDigest) { |
824 | 34.0k | stream *s = s_MD5C_make_stream(pdev->pdf_memory, pdev->strm); |
825 | | |
826 | 34.0k | if (s == NULL) |
827 | 0 | return_error(gs_error_VMerror); |
828 | 34.0k | pdev->strm = s; |
829 | 34.0k | } |
830 | 34.0k | gdev_vector_init((gx_device_vector *) pdev); |
831 | | #ifdef CLUSTER |
832 | | memset(&pdev->uuid_time, 0, sizeof(pdev->uuid_time)); |
833 | | #else |
834 | 34.0k | gp_get_realtime(pdev->uuid_time); |
835 | 34.0k | #endif |
836 | 34.0k | pdev->vec_procs = &pdf_vector_procs; |
837 | 34.0k | pdev->fill_options = pdev->stroke_options = gx_path_type_optimize; |
838 | | /* Set in_page so the vector routines won't try to call */ |
839 | | /* any vector implementation procedures. */ |
840 | 34.0k | pdev->in_page = true; |
841 | | /* |
842 | | * pdf_initialize_ids allocates some (global) named objects, so we must |
843 | | * initialize the named objects dictionary now. |
844 | | */ |
845 | 34.0k | pdev->local_named_objects = |
846 | 34.0k | pdev->global_named_objects = |
847 | 34.0k | cos_dict_alloc(pdev, "pdf_open(global_named_objects)"); |
848 | 34.0k | if (pdev->local_named_objects == NULL) { |
849 | 0 | code = gs_error_VMerror; |
850 | 0 | goto fail; |
851 | 0 | } |
852 | | /* Initialize internal structures that don't have IDs. */ |
853 | 34.0k | pdev->NI_stack = cos_array_alloc(pdev, "pdf_open(NI stack)"); |
854 | 34.0k | if (pdev->NI_stack == NULL) { |
855 | 0 | code = gs_error_VMerror; |
856 | 0 | goto fail; |
857 | 0 | } |
858 | 34.0k | pdev->vgstack = (pdf_viewer_state *)gs_alloc_bytes(pdev->pdf_memory, 11 * sizeof(pdf_viewer_state), "pdf_open(graphics state stack)"); |
859 | 34.0k | if (pdev->vgstack == 0) { |
860 | 0 | code = gs_error_VMerror; |
861 | 0 | goto fail; |
862 | 0 | } |
863 | 34.0k | memset(pdev->vgstack, 0x00, 11 * sizeof(pdf_viewer_state)); |
864 | 34.0k | pdev->vgstack_size = 11; |
865 | 34.0k | pdev->Namespace_stack = cos_array_alloc(pdev, "pdf_open(Namespace stack)"); |
866 | 34.0k | if (pdev->Namespace_stack == NULL) { |
867 | 0 | code = gs_error_VMerror; |
868 | 0 | goto fail; |
869 | 0 | } |
870 | 34.0k | pdf_initialize_ids(pdev); |
871 | 34.0k | code = pdf_compute_fileID(pdev); |
872 | 34.0k | if (code < 0) |
873 | 0 | goto fail; |
874 | 34.0k | if (pdev->OwnerPassword.size > 0) { |
875 | 0 | code = pdf_compute_encryption_data(pdev); |
876 | 0 | if (code < 0) |
877 | 0 | goto fail; |
878 | 34.0k | } else if(pdev->UserPassword.size > 0) { |
879 | 0 | emprintf(pdev->memory, |
880 | 0 | "User password is specified. Need an Owner password or both.\n"); |
881 | 0 | return_error(gs_error_rangecheck); |
882 | 34.0k | } else if (pdev->KeyLength) { |
883 | 0 | emprintf(pdev->memory, |
884 | 0 | "Can't accept encryption options without a password.\n"); |
885 | 0 | return_error(gs_error_rangecheck); |
886 | 0 | } |
887 | | /* Now create a new dictionary for the local named objects. */ |
888 | 34.0k | pdev->local_named_objects = |
889 | 34.0k | cos_dict_alloc(pdev, "pdf_open(local_named_objects)"); |
890 | 34.0k | if (pdev->local_named_objects == NULL) { |
891 | 0 | code = gs_error_VMerror; |
892 | 0 | goto fail; |
893 | 0 | } |
894 | 34.0k | pdev->outlines_id = 0; |
895 | 34.0k | pdev->next_page = 0; |
896 | 34.0k | pdev->text = pdf_text_data_alloc(mem); |
897 | 34.0k | pdev->sbstack_size = pdev->vgstack_size; /* Overestimated a few. */ |
898 | 34.0k | pdev->sbstack = gs_alloc_struct_array(mem, pdev->sbstack_size, pdf_substream_save, |
899 | 34.0k | &st_pdf_substream_save_element, "pdf_open"); |
900 | 34.0k | pdev->pages = |
901 | 34.0k | gs_alloc_struct_array(mem, initial_num_pages, pdf_page_t, |
902 | 34.0k | &st_pdf_page_element, "pdf_open(pages)"); |
903 | 34.0k | if (pdev->text == 0 || pdev->pages == 0 || pdev->sbstack == 0) { |
904 | 0 | code = gs_error_VMerror; |
905 | 0 | goto fail; |
906 | 0 | } |
907 | 34.0k | memset(pdev->sbstack, 0, pdev->sbstack_size * sizeof(pdf_substream_save)); |
908 | 34.0k | memset(pdev->pages, 0, initial_num_pages * sizeof(pdf_page_t)); |
909 | 34.0k | pdev->num_pages = initial_num_pages; |
910 | 34.0k | { |
911 | 34.0k | int i, j; |
912 | | |
913 | 578k | for (i = 0; i < NUM_RESOURCE_TYPES; ++i) |
914 | 9.25M | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) |
915 | 8.70M | pdev->resources[i].chains[j] = 0; |
916 | 34.0k | } |
917 | 34.0k | pdev->outline_levels = (pdf_outline_level_t *)gs_alloc_bytes(mem, INITIAL_MAX_OUTLINE_DEPTH * sizeof(pdf_outline_level_t), "outline_levels array"); |
918 | 34.0k | if (pdev->outline_levels == NULL) { |
919 | 0 | code = gs_error_VMerror; |
920 | 0 | goto fail; |
921 | 0 | } |
922 | | |
923 | 34.0k | memset(pdev->outline_levels, 0x00, INITIAL_MAX_OUTLINE_DEPTH * sizeof(pdf_outline_level_t)); |
924 | 34.0k | pdev->max_outline_depth = INITIAL_MAX_OUTLINE_DEPTH; |
925 | 34.0k | pdev->outline_levels[0].first.id = 0; |
926 | 34.0k | pdev->outline_levels[0].left = max_int; |
927 | 34.0k | pdev->outline_levels[0].first.action = 0; |
928 | 34.0k | pdev->outline_levels[0].last.action = 0; |
929 | 34.0k | pdev->outline_depth = 0; |
930 | 34.0k | pdev->closed_outline_depth = 0; |
931 | 34.0k | pdev->outlines_open = 0; |
932 | 34.0k | pdev->articles = 0; |
933 | 34.0k | pdev->Dests = 0; |
934 | 34.0k | pdev->EmbeddedFiles = 0; |
935 | 34.0k | pdev->AF = 0; |
936 | | /* {global,local}_named_objects was initialized above */ |
937 | 34.0k | pdev->PageLabels = 0; |
938 | 34.0k | pdev->PageLabels_current_page = 0; |
939 | 34.0k | pdev->PageLabels_current_label = 0; |
940 | 34.0k | pdev->pte = NULL; |
941 | 34.0k | pdf_reset_page(pdev); |
942 | 34.0k | pdev->BBox.p.x = pdev->width; |
943 | 34.0k | pdev->BBox.p.y = pdev->height; |
944 | 34.0k | pdev->BBox.q.x = 0; |
945 | 34.0k | pdev->BBox.q.y = 0; |
946 | | |
947 | 34.0k | if(pdev->UseCIEColor) { |
948 | 0 | emprintf(pdev->memory, "\n\nUse of -dUseCIEColor detected!\nSince the release of version 9.11 of Ghostscript we recommend you do not set\n-dUseCIEColor with the pdfwrite/ps2write device family.\n\n"); |
949 | 0 | } |
950 | | |
951 | | /* Build a font cache for pdfwrite, see 'pdf_free_pdf_font_cache' for why we need this. */ |
952 | 34.0k | pdev->pdf_font_dir = gs_font_dir_alloc2(pdev->memory->stable_memory, pdev->memory->non_gc_memory); |
953 | 34.0k | if (pdev->pdf_font_dir == 0) { |
954 | 0 | code = gs_error_VMerror; |
955 | 0 | goto fail; |
956 | 0 | } |
957 | | /* If we have a gs_lib_ctx, then we need to copy these function pointers from it (we are in PostScript). |
958 | | * We can't fill them in otherwise, as the functions are declared static in gsfont.c. |
959 | | * If we don't have one then we are in PCL/PXL/XPS, and cannot copy these function pointers. Fortunately |
960 | | * we don't need them for fonts in these languages. |
961 | | */ |
962 | 34.0k | if (pdev->memory->gs_lib_ctx->font_dir) { |
963 | 34.0k | pdev->pdf_font_dir->ccache.mark_glyph = pdev->memory->gs_lib_ctx->font_dir->ccache.mark_glyph; |
964 | 34.0k | pdev->pdf_font_dir->global_glyph_code = pdev->memory->gs_lib_ctx->font_dir->global_glyph_code; |
965 | 34.0k | } |
966 | | |
967 | | /* gs_opendevice() sets the device 'is_open' flag which is now of course the parent. We |
968 | | * still need to set the child's flag, we'll do it here to avoid setting it if we get any |
969 | | * failures, as those will also leave the parent not open. |
970 | | */ |
971 | 34.0k | if (pdev->parent) |
972 | 0 | pdev->is_open = true; |
973 | | |
974 | 34.0k | return 0; |
975 | 0 | fail: |
976 | 0 | gdev_vector_close_file((gx_device_vector *) pdev); |
977 | 0 | return pdf_close_files(pdev, code); |
978 | 34.0k | } |
979 | | |
980 | | /* Detect I/O errors. */ |
981 | | static int |
982 | | pdf_ferror(gx_device_pdf *pdev) |
983 | 118k | { |
984 | 118k | int code = 0; |
985 | | |
986 | 118k | gp_fflush(pdev->file); |
987 | 118k | gp_fflush(pdev->xref.file); |
988 | 118k | if (pdev->strm->file != NULL) |
989 | 0 | sflush(pdev->strm); |
990 | 118k | if (pdev->asides.strm->file != NULL) |
991 | 118k | sflush(pdev->asides.strm); |
992 | 118k | if (pdev->streams.strm->file != NULL) |
993 | 118k | sflush(pdev->streams.strm); |
994 | 118k | if (pdev->ObjStm.strm != NULL && pdev->ObjStm.strm->file != NULL) { |
995 | 30.8k | sflush(pdev->ObjStm.strm); |
996 | 30.8k | code = gp_ferror(pdev->ObjStm.file); |
997 | 30.8k | } |
998 | 118k | return gp_ferror(pdev->file) || gp_ferror(pdev->xref.file) || |
999 | 118k | gp_ferror(pdev->asides.file) || gp_ferror(pdev->streams.file) || |
1000 | 118k | code; |
1001 | 118k | } |
1002 | | |
1003 | | /* Compute the dominant text orientation of a page. */ |
1004 | | static int |
1005 | | pdf_dominant_rotation(const pdf_text_rotation_t *ptr) |
1006 | 21.3k | { |
1007 | 21.3k | int i, imax = -1; |
1008 | 21.3k | int64_t max_count = 0; |
1009 | 21.3k | static const int angles[] = { pdf_text_rotation_angle_values }; |
1010 | | |
1011 | 128k | for (i = 0; i < countof(ptr->counts); ++i) { |
1012 | 106k | int64_t count = ptr->counts[i]; |
1013 | | |
1014 | 106k | if (count > max_count) |
1015 | 6.08k | imax = i, max_count = count; |
1016 | 106k | } |
1017 | 21.3k | return (imax < 0 ? imax : angles[imax]); |
1018 | 21.3k | } |
1019 | | |
1020 | | /* Print a Rotate command, if requested and possible. */ |
1021 | | static void |
1022 | | pdf_print_orientation(gx_device_pdf * pdev, pdf_page_t *page) |
1023 | 54.5k | { |
1024 | 54.5k | stream *s = pdev->strm; |
1025 | 54.5k | int dsc_orientation = -1; |
1026 | 54.5k | const pdf_page_dsc_info_t *ppdi; |
1027 | | |
1028 | 54.5k | if (pdev->params.AutoRotatePages == arp_None) |
1029 | 33.1k | return; /* Not requested. */ |
1030 | | |
1031 | 21.3k | ppdi = (page != NULL ? &page->dsc_info : &pdev->doc_dsc_info); |
1032 | | |
1033 | | /* Determine DSC orientation : */ |
1034 | 21.3k | if (ppdi->viewing_orientation >= 0) |
1035 | 0 | dsc_orientation = ppdi->viewing_orientation; |
1036 | 21.3k | else if (ppdi->orientation >= 0) |
1037 | 0 | dsc_orientation = ppdi->orientation; |
1038 | 21.3k | if ((page == NULL && pdev->params.AutoRotatePages == arp_All) || /* document */ |
1039 | 21.3k | (page != NULL && page->text_rotation.Rotate >= 0) || /* page */ |
1040 | 21.3k | dsc_orientation >= 0 /* have DSC */) { |
1041 | 3.55k | const pdf_text_rotation_t *ptr = |
1042 | 3.55k | (page != NULL ? &page->text_rotation : &pdev->text_rotation); |
1043 | 3.55k | int angle = -1; |
1044 | | |
1045 | | /* Combine DSC rotation with text rotation : */ |
1046 | 3.55k | if (dsc_orientation == 0) { |
1047 | 0 | if (ptr->Rotate == 0 || ptr->Rotate == 180) |
1048 | 0 | angle = ptr->Rotate; |
1049 | 3.55k | } else if (dsc_orientation == 1) { |
1050 | 0 | if (ptr->Rotate == 90 || ptr->Rotate == 270) |
1051 | 0 | angle = ptr->Rotate; |
1052 | 0 | else |
1053 | 0 | angle = 90; |
1054 | 0 | } |
1055 | | |
1056 | 3.55k | if (angle < 0) { |
1057 | | /* If DSC and heuristic disagree, prefer dsc rotation to match the documented Adobe behaviour */ |
1058 | 3.55k | if (dsc_orientation >= 0) |
1059 | 0 | angle = dsc_orientation * 90; |
1060 | 3.55k | else |
1061 | 3.55k | angle = ptr->Rotate; |
1062 | 3.55k | } |
1063 | | |
1064 | | /* If got some, write it out : */ |
1065 | 3.55k | if (angle >= 0) |
1066 | 3.55k | pprintd1(s, "/Rotate %d", angle); |
1067 | 3.55k | } |
1068 | 21.3k | } |
1069 | | |
1070 | | /* Close the current page. */ |
1071 | | static int |
1072 | | pdf_close_page(gx_device_pdf * pdev, int num_copies) |
1073 | 45.3k | { |
1074 | 45.3k | int page_num; |
1075 | 45.3k | pdf_page_t *page; |
1076 | 45.3k | int code, i; |
1077 | | |
1078 | 45.3k | while (pdev->FormDepth > 0) { |
1079 | 0 | pdev->FormDepth--; |
1080 | 0 | code = pdf_exit_substream(pdev); |
1081 | 0 | if (code < 0) |
1082 | 0 | return code; |
1083 | 0 | } |
1084 | | |
1085 | | /* |
1086 | | * If the very first page is blank, we need to open the document |
1087 | | * before doing anything else. |
1088 | | */ |
1089 | | |
1090 | 45.3k | code = pdfwrite_pdf_open_document(pdev); |
1091 | 45.3k | if (code < 0) |
1092 | 0 | return code; |
1093 | 45.3k | if (pdev->ForOPDFRead && pdev->context == PDF_IN_NONE) { |
1094 | | /* Must create a context stream for empty pages. */ |
1095 | 12 | code = pdf_open_contents(pdev, PDF_IN_STREAM); |
1096 | 12 | if (code < 0) |
1097 | 5 | return code; |
1098 | 12 | } |
1099 | 45.3k | pdf_close_contents(pdev, true); |
1100 | | |
1101 | 45.3k | if (!pdev->DoNumCopies) |
1102 | 45.3k | num_copies = 1; |
1103 | | |
1104 | 90.7k | for(i=0;i<num_copies;i++) { |
1105 | 45.3k | bool clear_resource_use = i < num_copies - 1 ? 0 : 1; |
1106 | | |
1107 | 45.3k | page_num = ++(pdev->next_page); |
1108 | | /* |
1109 | | * We can't write the page object or the annotations array yet, because |
1110 | | * later pdfmarks might add elements to them. Write the other objects |
1111 | | * that the page references, and record what we'll need later. |
1112 | | * |
1113 | | * Start by making sure the pages array element exists. |
1114 | | */ |
1115 | | |
1116 | 45.3k | pdf_page_id(pdev, page_num); |
1117 | 45.3k | page = &pdev->pages[page_num - 1]; |
1118 | 45.3k | page->MediaBox.x = pdev->MediaSize[0]; |
1119 | 45.3k | page->MediaBox.y = pdev->MediaSize[1]; |
1120 | 45.3k | page->contents_id = pdev->contents_id; |
1121 | 45.3k | page->NumCopies_set = pdev->NumCopies_set; |
1122 | 45.3k | page->NumCopies = pdev->NumCopies; |
1123 | 45.3k | page->UserUnit = pdev->UserUnit; |
1124 | 45.3k | pdf_record_usage(pdev, pdev->contents_id, pdev->next_page); |
1125 | 45.3k | pdf_record_usage(pdev, pdev->contents_length_id, pdev->next_page); |
1126 | 45.3k | pdf_record_usage(pdev, page->Page->id, pdev->next_page); |
1127 | | |
1128 | | /* pdf_store_page_resources sets procsets, resource_ids[]. */ |
1129 | 45.3k | code = pdf_store_page_resources(pdev, page, clear_resource_use); |
1130 | 45.3k | if (code < 0) |
1131 | 0 | return code; |
1132 | | |
1133 | | /* Write the Functions. */ |
1134 | | |
1135 | 45.3k | code = pdf_write_resource_objects(pdev, resourceFunction); |
1136 | 45.3k | if (code < 0) |
1137 | 0 | return code; |
1138 | | |
1139 | | /* Close use of text on the page. */ |
1140 | | |
1141 | 45.3k | pdf_close_text_page(pdev); |
1142 | | |
1143 | | /* Accumulate text rotation. */ |
1144 | | |
1145 | 45.3k | page->text_rotation.Rotate = |
1146 | 45.3k | (pdev->params.AutoRotatePages == arp_PageByPage ? |
1147 | 33.1k | pdf_dominant_rotation(&page->text_rotation) : -1); |
1148 | 45.3k | { |
1149 | 45.3k | int i; |
1150 | | |
1151 | 272k | for (i = 0; i < countof(page->text_rotation.counts); ++i) |
1152 | 226k | pdev->text_rotation.counts[i] += page->text_rotation.counts[i]; |
1153 | 45.3k | } |
1154 | | |
1155 | | /* Record information from DSC comments. */ |
1156 | | |
1157 | 45.3k | page->dsc_info = pdev->page_dsc_info; |
1158 | 45.3k | if (page->dsc_info.orientation < 0) |
1159 | 45.3k | page->dsc_info.orientation = pdev->doc_dsc_info.orientation; |
1160 | | /* Bug 688793 */ |
1161 | 45.3k | if (page->dsc_info.viewing_orientation < 0) |
1162 | 45.3k | page->dsc_info.viewing_orientation = |
1163 | 45.3k | pdev->doc_dsc_info.viewing_orientation; |
1164 | 45.3k | if (page->dsc_info.bounding_box.p.x >= page->dsc_info.bounding_box.q.x || |
1165 | 45.3k | page->dsc_info.bounding_box.p.y >= page->dsc_info.bounding_box.q.y |
1166 | 45.3k | ) |
1167 | 45.3k | page->dsc_info.bounding_box = pdev->doc_dsc_info.bounding_box; |
1168 | | |
1169 | | /* Finish up. */ |
1170 | | |
1171 | 45.3k | if(pdf_ferror(pdev)) |
1172 | 0 | return(gs_note_error(gs_error_ioerror)); |
1173 | 45.3k | } |
1174 | 45.3k | pdf_reset_page(pdev); |
1175 | 45.3k | return (pdf_ferror(pdev) ? gs_note_error(gs_error_ioerror) : 0); |
1176 | 45.3k | } |
1177 | | |
1178 | | /* Write the page object. */ |
1179 | | static double |
1180 | | round_box_coord(double xy) |
1181 | 90.7k | { |
1182 | 90.7k | return (int)(xy * 100 + 0.5) / 100.0; |
1183 | 90.7k | } |
1184 | | static int check_annot_in_named(void *client_data, const byte *key_data, uint key_size, const cos_value_t *value) |
1185 | 2.97k | { |
1186 | 2.97k | cos_value_t *v = (cos_value_t *)client_data; |
1187 | | |
1188 | 2.97k | if (value->contents.object == v->contents.object) |
1189 | 0 | return 1; |
1190 | 2.97k | return 0; |
1191 | 2.97k | } |
1192 | | |
1193 | | static int |
1194 | | pdf_write_page(gx_device_pdf *pdev, int page_num) |
1195 | 45.3k | { |
1196 | 45.3k | int64_t page_id; |
1197 | 45.3k | pdf_page_t *page; |
1198 | 45.3k | double mediabox[4] = {0, 0}; |
1199 | 45.3k | stream *s; |
1200 | 45.3k | const cos_value_t *v_mediabox = NULL; |
1201 | | |
1202 | 45.3k | if (pdev->pages == NULL) |
1203 | 0 | return_error(gs_error_undefined); |
1204 | | |
1205 | 45.3k | page = &pdev->pages[page_num - 1]; |
1206 | 45.3k | if (page->Page != NULL) |
1207 | 45.3k | v_mediabox = cos_dict_find_c_key(page->Page, "/MediaBox"); |
1208 | 45.3k | page_id = pdf_page_id(pdev, page_num); |
1209 | | |
1210 | | /* If we have not been given a MediaBox overriding pdfmark, use the current media size. */ |
1211 | 45.3k | s = pdev->strm; |
1212 | 45.3k | pdf_open_obj(pdev, page_id, resourcePage); |
1213 | | |
1214 | 45.3k | if (v_mediabox == NULL ) { |
1215 | 45.3k | mediabox[2] = round_box_coord(page->MediaBox.x); |
1216 | 45.3k | mediabox[3] = round_box_coord(page->MediaBox.y); |
1217 | 45.3k | pprintg2(s, "<</Type/Page/MediaBox [0 0 %g %g]\n", |
1218 | 45.3k | mediabox[2], mediabox[3]); |
1219 | 45.3k | } else { |
1220 | 0 | const byte *p = v_mediabox->contents.chars.data; |
1221 | 0 | char buf[100]; |
1222 | 0 | int i, l = min (v_mediabox->contents.chars.size, sizeof(buf) - 1); |
1223 | 0 | float temp[4]; /* the type is float for sscanf. */ |
1224 | |
|
1225 | 0 | temp[0] = temp[1] = 0; |
1226 | 0 | temp[2] = round_box_coord(page->MediaBox.x); |
1227 | 0 | temp[3] = round_box_coord(page->MediaBox.y); |
1228 | 0 | memcpy(buf, p, l); |
1229 | 0 | buf[l] = 0; |
1230 | 0 | if (sscanf(buf, "[ %g %g %g %g ]", |
1231 | 0 | &temp[0], &temp[1], &temp[2], &temp[3]) == 4) { |
1232 | 0 | if (page->Page) |
1233 | 0 | cos_dict_delete_c_key(page->Page, "/MediaBox"); |
1234 | 0 | } |
1235 | 0 | pprintg4(s, "<</Type/Page/MediaBox [%g %g %g %g]\n", |
1236 | 0 | temp[0], temp[1], temp[2], temp[3]); |
1237 | 0 | for (i=0;i<4;i++) |
1238 | 0 | mediabox[i] = temp[i]; |
1239 | 0 | } |
1240 | 45.3k | if (pdev->PDFX != 0) { |
1241 | 0 | const cos_value_t *v_trimbox = NULL; |
1242 | 0 | const cos_value_t *v_artbox = NULL; |
1243 | 0 | const cos_value_t *v_cropbox = NULL; |
1244 | 0 | const cos_value_t *v_bleedbox = NULL; |
1245 | 0 | float trimbox[4] = {0, 0}, bleedbox[4] = {0, 0}, artbox[4] = {0, 0}, cropbox[4] = {0, 0}; |
1246 | 0 | bool print_bleedbox = false; |
1247 | |
|
1248 | 0 | if (page->Page != NULL) { |
1249 | 0 | v_trimbox = cos_dict_find_c_key(page->Page, "/TrimBox"); |
1250 | 0 | v_artbox = cos_dict_find_c_key(page->Page, "/ArtBox"); |
1251 | 0 | v_cropbox = cos_dict_find_c_key(page->Page, "/CropBox"); |
1252 | 0 | v_bleedbox = cos_dict_find_c_key(page->Page, "/BleedBox"); |
1253 | 0 | } |
1254 | |
|
1255 | 0 | cropbox[2] = trimbox[2] = bleedbox[2] = mediabox[2]; |
1256 | 0 | cropbox[3] = trimbox[3] = bleedbox[3] = mediabox[3]; |
1257 | | /* Offsets are [left right top bottom] according to the Acrobat 7.0 |
1258 | | distiller parameters manual, 12/7/2004, pp. 102-103. */ |
1259 | 0 | if (v_trimbox != NULL && v_trimbox->value_type == COS_VALUE_SCALAR) { |
1260 | 0 | const byte *p = v_trimbox->contents.chars.data; |
1261 | 0 | char buf[100]; |
1262 | 0 | int l = min (v_trimbox->contents.chars.size, sizeof(buf) - 1); |
1263 | 0 | float temp[4]; /* the type is float for sscanf. */ |
1264 | |
|
1265 | 0 | memcpy(buf, p, l); |
1266 | 0 | buf[l] = 0; |
1267 | 0 | if (sscanf(buf, "[ %g %g %g %g ]", |
1268 | 0 | &temp[0], &temp[1], &temp[2], &temp[3]) == 4) { |
1269 | 0 | trimbox[0] = temp[0]; |
1270 | 0 | trimbox[1] = temp[1]; |
1271 | 0 | trimbox[2] = temp[2]; |
1272 | 0 | trimbox[3] = temp[3]; |
1273 | 0 | if (page->Page != NULL) |
1274 | 0 | cos_dict_delete_c_key(page->Page, "/TrimBox"); |
1275 | 0 | } |
1276 | 0 | if (v_artbox != NULL && v_artbox->value_type == COS_VALUE_SCALAR && page->Page != NULL) |
1277 | 0 | cos_dict_delete_c_key(page->Page, "/ArtBox"); |
1278 | |
|
1279 | 0 | } else if (v_artbox != NULL && v_artbox->value_type == COS_VALUE_SCALAR) { |
1280 | | /* We have no TrimBox, but we ahve an ArtBox, set the TrimBox to be |
1281 | | * the supplied ArtBox (TrimBox is preferred for PDF/X) |
1282 | | */ |
1283 | 0 | const byte *p = v_artbox->contents.chars.data; |
1284 | 0 | char buf[100]; |
1285 | 0 | int l = min (v_artbox->contents.chars.size, sizeof(buf) - 1); |
1286 | 0 | float temp[4]; /* the type is float for sscanf. */ |
1287 | |
|
1288 | 0 | memcpy(buf, p, l); |
1289 | 0 | buf[l] = 0; |
1290 | 0 | if (sscanf(buf, "[ %g %g %g %g ]", |
1291 | 0 | &temp[0], &temp[1], &temp[2], &temp[3]) == 4) { |
1292 | 0 | if (pdev->PDFX == 3) { |
1293 | 0 | trimbox[0] = temp[0]; |
1294 | 0 | trimbox[1] = temp[1]; |
1295 | 0 | trimbox[2] = temp[2]; |
1296 | 0 | trimbox[3] = temp[3]; |
1297 | 0 | if (page->Page != NULL) |
1298 | 0 | cos_dict_delete_c_key(page->Page, "/ArtBox"); |
1299 | 0 | } else { |
1300 | 0 | artbox[0] = temp[0]; |
1301 | 0 | artbox[1] = temp[1]; |
1302 | 0 | artbox[2] = temp[2]; |
1303 | 0 | artbox[3] = temp[3]; |
1304 | 0 | } |
1305 | 0 | } |
1306 | 0 | } else { |
1307 | 0 | if (pdev->PDFXTrimBoxToMediaBoxOffset.size >= 4 && |
1308 | 0 | pdev->PDFXTrimBoxToMediaBoxOffset.data[0] >= 0 && |
1309 | 0 | pdev->PDFXTrimBoxToMediaBoxOffset.data[1] >= 0 && |
1310 | 0 | pdev->PDFXTrimBoxToMediaBoxOffset.data[2] >= 0 && |
1311 | 0 | pdev->PDFXTrimBoxToMediaBoxOffset.data[3] >= 0) { |
1312 | 0 | trimbox[0] = mediabox[0] + pdev->PDFXTrimBoxToMediaBoxOffset.data[0]; |
1313 | 0 | trimbox[1] = mediabox[1] + pdev->PDFXTrimBoxToMediaBoxOffset.data[3]; |
1314 | 0 | trimbox[2] = mediabox[2] - pdev->PDFXTrimBoxToMediaBoxOffset.data[1]; |
1315 | 0 | trimbox[3] = mediabox[3] - pdev->PDFXTrimBoxToMediaBoxOffset.data[2]; |
1316 | 0 | } |
1317 | 0 | } |
1318 | |
|
1319 | 0 | if (v_bleedbox != NULL && v_bleedbox->value_type == COS_VALUE_SCALAR) { |
1320 | 0 | const byte *p = v_bleedbox->contents.chars.data; |
1321 | 0 | char buf[100]; |
1322 | 0 | int l = min (v_bleedbox->contents.chars.size, sizeof(buf) - 1); |
1323 | 0 | float temp[4]; /* the type is float for sscanf. */ |
1324 | |
|
1325 | 0 | memcpy(buf, p, l); |
1326 | 0 | buf[l] = 0; |
1327 | 0 | if (sscanf(buf, "[ %g %g %g %g ]", |
1328 | 0 | &temp[0], &temp[1], &temp[2], &temp[3]) == 4) { |
1329 | 0 | if (temp[0] < mediabox[0]) |
1330 | 0 | bleedbox[0] = mediabox[0]; |
1331 | 0 | else |
1332 | 0 | bleedbox[0] = temp[0]; |
1333 | 0 | if (temp[1] < mediabox[1]) |
1334 | 0 | bleedbox[1] = mediabox[1]; |
1335 | 0 | else |
1336 | 0 | bleedbox[1] = temp[1]; |
1337 | 0 | if (temp[2] > mediabox[2]) |
1338 | 0 | bleedbox[2] = mediabox[2]; |
1339 | 0 | else |
1340 | 0 | bleedbox[2] = temp[2]; |
1341 | 0 | if (temp[3] > mediabox[3]) |
1342 | 0 | bleedbox[3] = mediabox[3]; |
1343 | 0 | else |
1344 | 0 | bleedbox[3] = temp[3]; |
1345 | 0 | print_bleedbox = true; |
1346 | 0 | if (page->Page != NULL) |
1347 | 0 | cos_dict_delete_c_key(page->Page, "/BleedBox"); |
1348 | 0 | } |
1349 | 0 | } else if (pdev->PDFXSetBleedBoxToMediaBox) |
1350 | 0 | print_bleedbox = true; |
1351 | 0 | else if (pdev->PDFXBleedBoxToTrimBoxOffset.size >= 4 && |
1352 | 0 | pdev->PDFXBleedBoxToTrimBoxOffset.data[0] >= 0 && |
1353 | 0 | pdev->PDFXBleedBoxToTrimBoxOffset.data[1] >= 0 && |
1354 | 0 | pdev->PDFXBleedBoxToTrimBoxOffset.data[2] >= 0 && |
1355 | 0 | pdev->PDFXBleedBoxToTrimBoxOffset.data[3] >= 0) { |
1356 | 0 | bleedbox[0] = trimbox[0] - pdev->PDFXBleedBoxToTrimBoxOffset.data[0]; |
1357 | 0 | bleedbox[1] = trimbox[1] - pdev->PDFXBleedBoxToTrimBoxOffset.data[3]; |
1358 | 0 | bleedbox[2] = trimbox[2] + pdev->PDFXBleedBoxToTrimBoxOffset.data[1]; |
1359 | 0 | bleedbox[3] = trimbox[3] + pdev->PDFXBleedBoxToTrimBoxOffset.data[2]; |
1360 | 0 | print_bleedbox = true; |
1361 | 0 | } |
1362 | |
|
1363 | 0 | if (print_bleedbox == true) { |
1364 | 0 | if (trimbox[0] < bleedbox[0] || trimbox[1] < bleedbox[1] || |
1365 | 0 | trimbox[2] > bleedbox[2] || trimbox[3] > bleedbox[3]) { |
1366 | 0 | switch (pdev->PDFACompatibilityPolicy) { |
1367 | 0 | case 0: |
1368 | 0 | emprintf(pdev->memory, |
1369 | 0 | "TrimBox does not fit inside BleedBox, not permitted in PDF/X-3, reverting to normal PDF output\n"); |
1370 | 0 | pdev->AbortPDFAX = true; |
1371 | 0 | pdev->PDFX = 0; |
1372 | 0 | break; |
1373 | 0 | case 1: |
1374 | 0 | emprintf(pdev->memory, |
1375 | 0 | "TrimBox does not fit inside BleedBox, not permitted in PDF/X-3, reducing TrimBox\n"); |
1376 | 0 | if (trimbox[0] < bleedbox[0]) |
1377 | 0 | trimbox[0] = bleedbox[0]; |
1378 | 0 | if (trimbox[1] < bleedbox[1]) |
1379 | 0 | trimbox[1] = bleedbox[1]; |
1380 | 0 | if (trimbox[2] > bleedbox[2]) |
1381 | 0 | trimbox[2] = bleedbox[2]; |
1382 | 0 | if (trimbox[3] > bleedbox[3]) |
1383 | 0 | trimbox[3] = bleedbox[3]; |
1384 | 0 | break; |
1385 | 0 | case 2: |
1386 | 0 | emprintf(pdev->memory, |
1387 | 0 | "TrimBox does not fit inside BleedBox, not permitted in PDF/X-3, aborting conversion\n"); |
1388 | 0 | return_error(gs_error_unknownerror); |
1389 | 0 | break; |
1390 | 0 | default: |
1391 | 0 | emprintf(pdev->memory, |
1392 | 0 | "TrimBox does not fit inside BleedBox, not permitted in PDF/X-3\nunrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n"); |
1393 | 0 | pdev->AbortPDFAX = true; |
1394 | 0 | pdev->PDFX = 0; |
1395 | 0 | break; |
1396 | 0 | } |
1397 | 0 | } |
1398 | 0 | } |
1399 | | |
1400 | 0 | if (v_cropbox != NULL && v_cropbox->value_type == COS_VALUE_SCALAR) { |
1401 | 0 | const byte *p = v_cropbox->contents.chars.data; |
1402 | 0 | char buf[100]; |
1403 | 0 | int l = min (v_cropbox->contents.chars.size, sizeof(buf) - 1); |
1404 | 0 | float temp[4]; /* the type is float for sscanf. */ |
1405 | |
|
1406 | 0 | memcpy(buf, p, l); |
1407 | 0 | buf[l] = 0; |
1408 | 0 | if (sscanf(buf, "[ %g %g %g %g ]", |
1409 | 0 | &temp[0], &temp[1], &temp[2], &temp[3]) == 4) { |
1410 | 0 | if (page->Page != NULL) |
1411 | 0 | cos_dict_delete_c_key(page->Page, "/CropBox"); |
1412 | | /* Ensure that CropBox is no larger than MediaBox. The spec says *nothing* about |
1413 | | * this, but Acrobat Preflight complains if it is larger. This can happen because |
1414 | | * we apply 'round_box_coord' to the mediabox at the start of this rouinte. |
1415 | | */ |
1416 | 0 | if (temp[0] < mediabox[0]) |
1417 | 0 | temp[0] = mediabox[0]; |
1418 | 0 | if (temp[1] < mediabox[1]) |
1419 | 0 | temp[1] = mediabox[1]; |
1420 | 0 | if (temp[2] > mediabox[2]) |
1421 | 0 | temp[2] = mediabox[2]; |
1422 | 0 | if (temp[3] > mediabox[3]) |
1423 | 0 | temp[3] = mediabox[3]; |
1424 | 0 | cropbox[0] = temp[0]; |
1425 | 0 | cropbox[1] = temp[1]; |
1426 | 0 | cropbox[2] = temp[2]; |
1427 | 0 | cropbox[3] = temp[3]; |
1428 | 0 | pprintg4(s, "/CropBox [%g %g %g %g]\n", |
1429 | 0 | temp[0], temp[1], temp[2], temp[3]); |
1430 | | /* Make sure TrimBox fits inside CropBox. Spec says 'must not extend |
1431 | | * beyond the boundaries' but Acrobat Preflight complains if they |
1432 | | * are the same value. |
1433 | | */ |
1434 | 0 | if (trimbox[0] < temp[0] || trimbox[1] < temp[1] || |
1435 | 0 | trimbox[2] > temp[2] || trimbox[3] > temp[3]) { |
1436 | 0 | switch (pdev->PDFACompatibilityPolicy) { |
1437 | 0 | case 0: |
1438 | 0 | emprintf(pdev->memory, |
1439 | 0 | "TrimBox does not fit inside CropBox, not permitted in PDF/X-3, reverting to normal PDF output\n"); |
1440 | 0 | pdev->AbortPDFAX = true; |
1441 | 0 | pdev->PDFX = 0; |
1442 | 0 | break; |
1443 | 0 | case 1: |
1444 | 0 | emprintf(pdev->memory, |
1445 | 0 | "TrimBox does not fit inside CropBox, not permitted in PDF/X-3, reducing TrimBox\n"); |
1446 | 0 | if (trimbox[0] < temp[0]) |
1447 | 0 | trimbox[0] = temp[0]; |
1448 | 0 | if (trimbox[1] < temp[1]) |
1449 | 0 | trimbox[1] = temp[1]; |
1450 | 0 | if (trimbox[2] > temp[2]) |
1451 | 0 | trimbox[2] = temp[2]; |
1452 | 0 | if (trimbox[3] > temp[3]) |
1453 | 0 | trimbox[3] = temp[3]; |
1454 | 0 | break; |
1455 | 0 | case 2: |
1456 | 0 | emprintf(pdev->memory, |
1457 | 0 | "TrimBox does not fit inside CropBox, not permitted in PDF/X-3, aborting conversion\n"); |
1458 | 0 | return_error(gs_error_unknownerror); |
1459 | 0 | break; |
1460 | 0 | default: |
1461 | 0 | emprintf(pdev->memory, |
1462 | 0 | "TrimBox does not fit inside CropBox, not permitted in PDF/X-3\nunrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n"); |
1463 | 0 | pdev->AbortPDFAX = true; |
1464 | 0 | pdev->PDFX = 0; |
1465 | 0 | break; |
1466 | 0 | } |
1467 | 0 | } |
1468 | 0 | } |
1469 | 0 | } |
1470 | | |
1471 | 0 | if (pdev->PDFX > 1 && page->Page != NULL) { |
1472 | 0 | if (cos_dict_find_c_key(page->Page, "/TrimBox") == NULL && |
1473 | 0 | cos_dict_find_c_key(page->Page, "/ArtBox") == NULL) |
1474 | 0 | pprintg4(s, "/TrimBox [%g %g %g %g]\n", |
1475 | 0 | trimbox[0], trimbox[1], trimbox[2], trimbox[3]); |
1476 | 0 | if (print_bleedbox && |
1477 | 0 | cos_dict_find_c_key(page->Page, "/BleedBox") == NULL) |
1478 | 0 | pprintg4(s, "/BleedBox [%g %g %g %g]\n", |
1479 | 0 | bleedbox[0], bleedbox[1], bleedbox[2], bleedbox[3]); |
1480 | 0 | } |
1481 | 0 | if (pdev->PDFX == 1 && page->Page != NULL) { |
1482 | 0 | if (cos_dict_find_c_key(page->Page, "/ArtBox") == NULL) { |
1483 | 0 | artbox[0] = mediabox[0]; |
1484 | 0 | artbox[1] = mediabox[1]; |
1485 | 0 | artbox[2] = mediabox[2]; |
1486 | 0 | artbox[3] = mediabox[3]; |
1487 | 0 | } |
1488 | 0 | if (cos_dict_find_c_key(page->Page, "/CropBox") != NULL) { |
1489 | 0 | if (artbox[0] < cropbox[0]) |
1490 | 0 | artbox[0] = cropbox[0]; |
1491 | 0 | if (artbox[1] < cropbox[1]) |
1492 | 0 | artbox[1] = cropbox[1]; |
1493 | 0 | if (artbox[2] < cropbox[2]) |
1494 | 0 | artbox[2] = cropbox[2]; |
1495 | 0 | if (artbox[3] < cropbox[3]) |
1496 | 0 | artbox[3] = cropbox[3]; |
1497 | 0 | } |
1498 | 0 | if (cos_dict_find_c_key(page->Page, "/BleedBox") != NULL) { |
1499 | 0 | if (artbox[0] < bleedbox[0]) |
1500 | 0 | artbox[0] = bleedbox[0]; |
1501 | 0 | if (artbox[1] < bleedbox[1]) |
1502 | 0 | artbox[1] = bleedbox[1]; |
1503 | 0 | if (artbox[2] < bleedbox[2]) |
1504 | 0 | artbox[2] = bleedbox[2]; |
1505 | 0 | if (artbox[3] < bleedbox[3]) |
1506 | 0 | artbox[3] = bleedbox[3]; |
1507 | 0 | } |
1508 | 0 | pprintg4(s, "/ArtBox [%g %g %g %g]\n", |
1509 | 0 | artbox[0], artbox[1], artbox[2], artbox[3]); |
1510 | 0 | } |
1511 | 0 | } |
1512 | 45.3k | pdf_print_orientation(pdev, page); |
1513 | 45.3k | if (page->UserUnit != 1) |
1514 | 6 | pprintg1(s, "/UserUnit %g\n", page->UserUnit); |
1515 | | |
1516 | 45.3k | pprinti64d1(s, "/Parent %"PRId64" 0 R\n", pdev->Pages->id); |
1517 | 45.3k | if (pdev->ForOPDFRead && pdev->DoNumCopies && !pdev->ProduceDSC) { |
1518 | 0 | if (page->NumCopies_set) |
1519 | 0 | pprinti64d1(s, "/NumCopies %"PRId64"\n", page->NumCopies); |
1520 | 0 | } |
1521 | 45.3k | if (page->group_id > 0) { |
1522 | 241 | pprinti64d1(s, "/Group %"PRId64" 0 R\n", page->group_id); |
1523 | 241 | } |
1524 | 45.3k | if (pdev->CompatibilityLevel <= 1.7) { |
1525 | 45.3k | stream_puts(s, "/Resources<</ProcSet[/PDF"); |
1526 | 45.3k | if (page->procsets & ImageB) |
1527 | 59 | stream_puts(s, " /ImageB"); |
1528 | 45.3k | if (page->procsets & ImageC) |
1529 | 1.30k | stream_puts(s, " /ImageC"); |
1530 | 45.3k | if (page->procsets & ImageI) |
1531 | 441 | stream_puts(s, " /ImageI"); |
1532 | 45.3k | if (page->procsets & Text) |
1533 | 3.50k | stream_puts(s, " /Text"); |
1534 | 45.3k | stream_puts(s, "]\n"); |
1535 | 45.3k | } else { |
1536 | 0 | stream_puts(s, "/Resources<<"); |
1537 | 0 | } |
1538 | 45.3k | { |
1539 | 45.3k | int i; |
1540 | | |
1541 | 408k | for (i = 0; i < countof(page->resource_ids); ++i) |
1542 | 362k | if (page->resource_ids[i] && pdf_resource_type_names[i]) { |
1543 | 23.0k | stream_puts(s, pdf_resource_type_names[i]); |
1544 | 23.0k | pprinti64d1(s, " %"PRId64" 0 R\n", page->resource_ids[i]); |
1545 | 23.0k | } |
1546 | 45.3k | } |
1547 | 45.3k | stream_puts(s, ">>\n"); |
1548 | | |
1549 | | /* Write the annotations array if any. */ |
1550 | | |
1551 | 45.3k | if (page->Annots) { |
1552 | 274 | const cos_value_t *value = NULL; |
1553 | 274 | const cos_array_element_t *e = NULL, *next = NULL; |
1554 | 274 | int64_t index = 0; |
1555 | | |
1556 | 274 | stream_puts(s, "/Annots"); |
1557 | 274 | COS_WRITE(page->Annots, pdev); |
1558 | | /* More complications caused by cos objects. Simply calling COS_FREE |
1559 | | * will free the array, but it won't free the elements of the array |
1560 | | * if they have non-zero IDs (see the comments regarding cleanup of |
1561 | | * resources in pdf_close at around line 1090 below). Because we've |
1562 | | * already written out the annotations, the dictionary contents have |
1563 | | * already been freed, but we need the IDs until this point when we |
1564 | | * write out the array in the page dictionary. So after using them, |
1565 | | * we must go through the array and set the ids to 0 so that COS_FREE |
1566 | | * will free the array elements as well as the array itself. |
1567 | | */ |
1568 | 274 | e = cos_array_element_first(page->Annots); |
1569 | 822 | while (e != NULL) { |
1570 | 548 | next = cos_array_element_next(e, &index, &value); |
1571 | 548 | if (value->contents.object != NULL) { |
1572 | | /* Check to see if this is a local named object, if it is do not |
1573 | | * zero the ID! This object has not yet been written, because it |
1574 | | * is a named object it can be modified after creation. We must |
1575 | | * allow the named object code to write out the object and free it. |
1576 | | */ |
1577 | 548 | if (cos_dict_forall(pdev->local_named_objects, (void *)value, |
1578 | 548 | check_annot_in_named) == 0) |
1579 | 548 | value->contents.object->id = 0; |
1580 | 548 | } |
1581 | 548 | e = next; |
1582 | 548 | } |
1583 | 274 | COS_FREE(page->Annots, "pdf_write_page(Annots)"); |
1584 | 274 | page->Annots = 0; |
1585 | 274 | } |
1586 | | /* |
1587 | | * The PDF documentation allows, and this code formerly emitted, |
1588 | | * a Contents entry whose value was an empty array. Acrobat Reader |
1589 | | * 3 and 4 accept this, but Acrobat Reader 5.0 rejects it. |
1590 | | * Fortunately, the Contents entry is optional. |
1591 | | */ |
1592 | 45.3k | if (page->contents_id != 0) |
1593 | 45.3k | pprinti64d1(s, "/Contents %"PRId64" 0 R\n", page->contents_id); |
1594 | | |
1595 | | /* Write any elements stored by pdfmarks. */ |
1596 | | |
1597 | 45.3k | if (page->Page != NULL) |
1598 | 45.3k | cos_dict_elements_write(page->Page, pdev); |
1599 | | |
1600 | 45.3k | stream_puts(s, ">>\n"); |
1601 | 45.3k | pdf_end_obj(pdev, resourcePage); |
1602 | 45.3k | return 0; |
1603 | 45.3k | } |
1604 | | |
1605 | | /* Wrap up ("output") a page. */ |
1606 | | /* if we are doing separate pages, call pdf_close to emit the file, then */ |
1607 | | /* pdf_open to open the next page as a new file */ |
1608 | | /* NB: converting an input PDF with offpage links will generate warnings */ |
1609 | | static int |
1610 | | pdf_output_page(gx_device * dev, int num_copies, int flush) |
1611 | 31.7k | { |
1612 | 31.7k | gx_device_pdf *const pdev = (gx_device_pdf *) dev; |
1613 | 31.7k | int code; |
1614 | | |
1615 | 31.7k | if (pdev->Eps2Write && pdev->next_page != 0 && !gx_outputfile_is_separate_pages(pdev->fname, dev->memory)) { |
1616 | 4.03k | emprintf(pdev->memory, "\n *** EPS files may not contain multiple pages.\n *** Use of the %%d filename format is required to output pages to multiple EPS files.\n"); |
1617 | 4.03k | return_error(gs_error_ioerror); |
1618 | 4.03k | } |
1619 | | |
1620 | 27.7k | if (pdev->ForOPDFRead) { |
1621 | 21.2k | code = pdf_close_page(pdev, num_copies); |
1622 | 21.2k | if (code < 0) |
1623 | 0 | return code; |
1624 | | |
1625 | 21.2k | while (pdev->sbstack_depth) { |
1626 | 16 | code = pdf_exit_substream(pdev); |
1627 | 16 | if (code < 0) |
1628 | 8 | return code; |
1629 | 16 | } |
1630 | 21.2k | } else { |
1631 | 6.46k | while (pdev->sbstack_depth) { |
1632 | 0 | code = pdf_exit_substream(pdev); |
1633 | 0 | if (code < 0) |
1634 | 0 | return code; |
1635 | 0 | } |
1636 | 6.46k | code = pdf_close_page(pdev, num_copies); |
1637 | 6.46k | if (code < 0) |
1638 | 0 | return code; |
1639 | 6.46k | } |
1640 | | |
1641 | 27.7k | if(pdev->UseCIEColor) { |
1642 | 0 | emprintf(pdev->memory, "\n\nUse of -dUseCIEColor detected!\nSince the release of version 9.11 of Ghostscript we recommend you do not set\n-dUseCIEColor with the pdfwrite/ps2write device family.\n\n"); |
1643 | 0 | } |
1644 | 27.7k | if (pdf_ferror(pdev)) |
1645 | 0 | return_error(gs_error_ioerror); |
1646 | | |
1647 | 27.7k | if ((code = gx_finish_output_page(dev, num_copies, flush)) < 0) |
1648 | 0 | return code; |
1649 | | |
1650 | 27.7k | if (gx_outputfile_is_separate_pages(((gx_device_vector *)dev)->fname, dev->memory)) { |
1651 | 0 | pdev->InOutputPage = true; |
1652 | 0 | if ((code = pdf_close(dev)) < 0) |
1653 | 0 | return code; |
1654 | 0 | code = pdf_open(dev); |
1655 | 0 | dev->is_open = true; |
1656 | 0 | } |
1657 | 27.7k | return code; |
1658 | 27.7k | } |
1659 | | |
1660 | | static int find_end_xref_section (gx_device_pdf *pdev, gp_file *tfile, int64_t start, gs_offset_t resource_pos) |
1661 | 27.6k | { |
1662 | 27.6k | int64_t start_offset; |
1663 | | |
1664 | 27.6k | if (pdev->doubleXref) |
1665 | 27.6k | start_offset = (start - pdev->FirstObjectNumber) * sizeof(gs_offset_t) * 2; |
1666 | 0 | else |
1667 | 0 | start_offset = (start - pdev->FirstObjectNumber) * sizeof(gs_offset_t); |
1668 | | |
1669 | 27.6k | if (gp_fseek(tfile, start_offset, SEEK_SET) == 0) |
1670 | 27.6k | { |
1671 | 27.6k | int64_t i, r; |
1672 | | |
1673 | 484k | for (i = start; i < pdev->next_id; ++i) { |
1674 | 457k | gs_offset_t pos, index = -1; |
1675 | | |
1676 | 457k | r = gp_fread(&pos, sizeof(pos), 1, tfile); |
1677 | 457k | if (r != 1) |
1678 | 0 | return(gs_note_error(gs_error_ioerror)); |
1679 | 457k | if (pdev->doubleXref) { |
1680 | 457k | index = pos; |
1681 | 457k | r = gp_fread(&pos, sizeof(pos), 1, tfile); |
1682 | 457k | if (r != 1) |
1683 | 0 | return(gs_note_error(gs_error_ioerror)); |
1684 | 457k | } else |
1685 | 0 | index = 0; |
1686 | 457k | if (pos & ASIDES_BASE_POSITION) { |
1687 | 142k | pos &= ~ASIDES_BASE_POSITION; |
1688 | 142k | pos += resource_pos; |
1689 | 142k | } |
1690 | 457k | pos -= pdev->OPDFRead_procset_length; |
1691 | 457k | if (pos == 0 && index == 0) { |
1692 | 83 | return i; |
1693 | 83 | } |
1694 | 457k | } |
1695 | 27.6k | } |
1696 | 27.5k | return pdev->next_id; |
1697 | 27.6k | } |
1698 | | |
1699 | | static int write_xref_section(gx_device_pdf *pdev, gp_file *tfile, int64_t start, int end, gs_offset_t resource_pos, gs_offset_t *Offsets) |
1700 | 0 | { |
1701 | 0 | int64_t start_offset; |
1702 | |
|
1703 | 0 | if (pdev->doubleXref) |
1704 | 0 | start_offset = (start - pdev->FirstObjectNumber) * sizeof(gs_offset_t) * 2; |
1705 | 0 | else |
1706 | 0 | start_offset = (start - pdev->FirstObjectNumber) * sizeof(gs_offset_t); |
1707 | |
|
1708 | 0 | if (gp_fseek(tfile, start_offset, SEEK_SET) == 0) |
1709 | 0 | { |
1710 | 0 | int64_t i, r; |
1711 | |
|
1712 | 0 | for (i = start; i < end; ++i) { |
1713 | 0 | gs_offset_t pos; |
1714 | 0 | char str[21]; |
1715 | |
|
1716 | 0 | r = gp_fread(&pos, sizeof(pos), 1, tfile); |
1717 | 0 | if (r != 1) |
1718 | 0 | return(gs_note_error(gs_error_ioerror)); |
1719 | 0 | if (pdev->doubleXref) { |
1720 | 0 | r = gp_fread(&pos, sizeof(pos), 1, tfile); |
1721 | 0 | if (r != 1) |
1722 | 0 | return(gs_note_error(gs_error_ioerror)); |
1723 | 0 | } |
1724 | | |
1725 | 0 | if (pos & ASIDES_BASE_POSITION) { |
1726 | 0 | pos &= ~ASIDES_BASE_POSITION; |
1727 | 0 | pos += resource_pos; |
1728 | 0 | } |
1729 | 0 | pos -= pdev->OPDFRead_procset_length; |
1730 | | |
1731 | | /* check to see we haven't got an offset which is too large to represent |
1732 | | * in an xref (10 digits). Throw an error if we do. |
1733 | | */ |
1734 | 0 | if (pos > 9999999999) { |
1735 | 0 | emprintf(pdev->pdf_memory, "ERROR - Attempt to create an xref entry with more than 10 digits which is illegal.\n"); |
1736 | 0 | emprintf(pdev->pdf_memory, "PDF file production has been aborted.\n"); |
1737 | 0 | return_error(gs_error_rangecheck); |
1738 | 0 | } |
1739 | | |
1740 | | /* If we are linearising there's no point in writing an xref we will |
1741 | | * later replace. Also makes the file slightly smaller reducing the |
1742 | | * chances of needing to write white space to pad the file out. |
1743 | | */ |
1744 | 0 | if (!pdev->Linearise) { |
1745 | 0 | gs_snprintf(str, sizeof(str), "%010"PRId64" 00000 n \n", pos); |
1746 | 0 | stream_puts(pdev->strm, str); |
1747 | 0 | } |
1748 | 0 | if (Offsets) |
1749 | 0 | Offsets[i] = pos; |
1750 | 0 | } |
1751 | 0 | } |
1752 | 0 | return 0; |
1753 | 0 | } |
1754 | | |
1755 | | static int write_xrefstm_section(gx_device_pdf *pdev, gp_file *tfile, int64_t start, int end, gs_offset_t resource_pos, int offset_size, stream *s) |
1756 | 9.21k | { |
1757 | 9.21k | int64_t start_offset; |
1758 | | |
1759 | 9.21k | if (pdev->doubleXref) |
1760 | 9.21k | start_offset = (start - pdev->FirstObjectNumber) * sizeof(gs_offset_t) * 2; |
1761 | 0 | else |
1762 | 0 | start_offset = (start - pdev->FirstObjectNumber) * sizeof(gs_offset_t); |
1763 | | |
1764 | 9.21k | if (gp_fseek(tfile, start_offset, SEEK_SET) == 0) |
1765 | 9.21k | { |
1766 | 9.21k | int64_t i, j, r; |
1767 | | |
1768 | 161k | for (i = start; i < end; ++i) { |
1769 | 152k | gs_offset_t pos, objstm = -1, index = 0; |
1770 | | |
1771 | 152k | r = gp_fread(&pos, sizeof(pos), 1, tfile); |
1772 | 152k | if (r != 1) |
1773 | 0 | return(gs_note_error(gs_error_ioerror)); |
1774 | | |
1775 | 152k | if (pdev->doubleXref) { |
1776 | 152k | objstm = pos; |
1777 | 152k | r = gp_fread(&index, sizeof(pos), 1, tfile); |
1778 | 152k | if (r != 1) |
1779 | 0 | return(gs_note_error(gs_error_ioerror)); |
1780 | 152k | pos = index; |
1781 | 152k | } |
1782 | | |
1783 | 152k | if (!pdev->doubleXref || objstm == 0) { |
1784 | 90.5k | if (pos & ASIDES_BASE_POSITION) { |
1785 | 47.5k | pos &= ~ASIDES_BASE_POSITION; |
1786 | 47.5k | pos += resource_pos; |
1787 | 47.5k | } |
1788 | 90.5k | pos -= pdev->OPDFRead_procset_length; |
1789 | | |
1790 | | /* check to see we haven't got an offset which is too large to represent |
1791 | | * in an xref (10 digits). Throw an error if we do. |
1792 | | */ |
1793 | 90.5k | if (pos > 9999999999) { |
1794 | 0 | emprintf(pdev->pdf_memory, "ERROR - Attempt to create an xref entry with more than 10 digits which is illegal.\n"); |
1795 | 0 | emprintf(pdev->pdf_memory, "PDF file production has been aborted.\n"); |
1796 | 0 | return_error(gs_error_rangecheck); |
1797 | 0 | } |
1798 | 90.5k | } |
1799 | | |
1800 | 152k | if (objstm > 0) { |
1801 | 61.8k | stream_putc(s, 0x02); |
1802 | 61.8k | pos = objstm; |
1803 | 61.8k | } |
1804 | 90.5k | else |
1805 | 90.5k | stream_putc(s, 0x01); |
1806 | 485k | for (j = 1; j <= offset_size; j++) |
1807 | 333k | stream_putc(s, (pos >> ((offset_size - j) * 8)) & 0xFF); |
1808 | 152k | stream_putc(s, 0x00); |
1809 | 152k | if (objstm > 0) |
1810 | 61.8k | stream_putc(s, index); |
1811 | 90.5k | else |
1812 | 90.5k | stream_putc(s, 0x00); |
1813 | 152k | } |
1814 | 9.21k | } |
1815 | 9.21k | return 0; |
1816 | 9.21k | } |
1817 | | |
1818 | | static int |
1819 | | rewrite_object(gx_device_pdf *const pdev, pdf_linearisation_t *linear_params, int object) |
1820 | 0 | { |
1821 | 0 | uint64_t read, Size; |
1822 | 0 | char c, *Scratch, *source, *target, Buf[280], *next; |
1823 | 0 | int code, ID, ScratchSize=16384; |
1824 | |
|
1825 | 0 | Size = pdev->ResourceUsage[object].Length; |
1826 | |
|
1827 | 0 | Scratch = (char *)gs_alloc_bytes(pdev->pdf_memory, ScratchSize, "Working memory for object rewriting"); |
1828 | 0 | if (Scratch == 0L) |
1829 | 0 | return (gs_note_error(gs_error_VMerror)); |
1830 | | |
1831 | 0 | pdev->ResourceUsage[object].LinearisedOffset = gp_ftell(linear_params->Lin_File.file); |
1832 | 0 | code = gp_fseek(linear_params->sfile, pdev->ResourceUsage[object].OriginalOffset, SEEK_SET); |
1833 | 0 | if (code < 0) |
1834 | 0 | return code; |
1835 | | |
1836 | 0 | read = 0; |
1837 | 0 | do { |
1838 | 0 | code = gp_fread(&c, 1, 1, linear_params->sfile); |
1839 | 0 | read++; |
1840 | 0 | } while (c != '\n' && code > 0); |
1841 | 0 | gs_snprintf(Scratch, ScratchSize, "%d 0 obj\n", pdev->ResourceUsage[object].NewObjectNumber); |
1842 | 0 | gp_fwrite(Scratch, strlen(Scratch), 1, linear_params->Lin_File.file); |
1843 | |
|
1844 | 0 | code = gp_fread(&c, 1, 1, linear_params->sfile); |
1845 | 0 | if (code != 1) |
1846 | 0 | return_error(gs_error_ioerror); |
1847 | | |
1848 | 0 | read++; |
1849 | 0 | if (c == '<' || c == '[') { |
1850 | 0 | int index = 0; |
1851 | 0 | Scratch[index++] = c; |
1852 | 0 | do { |
1853 | 0 | do { |
1854 | 0 | code = gp_fread(&c, 1, 1, linear_params->sfile); |
1855 | 0 | Scratch[index++] = c; |
1856 | 0 | read++; |
1857 | 0 | if (index == ScratchSize - 2) { |
1858 | 0 | char *Temp; |
1859 | |
|
1860 | 0 | Temp = (char *)gs_alloc_bytes(pdev->pdf_memory, ScratchSize * 2, "Working memory for object rewriting"); |
1861 | 0 | if (Temp == 0L) { |
1862 | 0 | gs_free_object(pdev->pdf_memory, Scratch, "Free working memory for object rewriting"); |
1863 | 0 | return (gs_note_error(gs_error_VMerror)); |
1864 | 0 | } |
1865 | 0 | memcpy(Temp, Scratch, ScratchSize); |
1866 | 0 | gs_free_object(pdev->pdf_memory, Scratch, "Increase working memory for object rewriting"); |
1867 | 0 | Scratch = Temp; |
1868 | 0 | ScratchSize *= 2; |
1869 | 0 | } |
1870 | 0 | }while (c != '\r' && c != '\n'); |
1871 | 0 | Scratch[index] = 0; |
1872 | 0 | if (strncmp(&Scratch[index - 7], "endobj", 6) == 0 || strncmp(&Scratch[index - 7], "stream", 6) == 0) |
1873 | 0 | break; |
1874 | 0 | } while (code); |
1875 | 0 | } else { |
1876 | 0 | Scratch[0] = 0; |
1877 | 0 | gp_fwrite(&c, 1, 1, linear_params->Lin_File.file); |
1878 | 0 | } |
1879 | | |
1880 | 0 | Size -= read; |
1881 | |
|
1882 | 0 | source = Scratch; |
1883 | 0 | do { |
1884 | 0 | target = strstr(source, " 0 R"); |
1885 | 0 | if (target) { |
1886 | 0 | next = target + 4; |
1887 | 0 | do { |
1888 | 0 | target--; |
1889 | 0 | }while (*target >= '0' && *target <= '9'); |
1890 | 0 | target++; |
1891 | 0 | (void)sscanf(target, "%d 0 R", &ID); |
1892 | 0 | gp_fwrite(source, target - source, 1, linear_params->Lin_File.file); |
1893 | 0 | gs_snprintf(Buf, sizeof(Buf), "%d 0 R", pdev->ResourceUsage[ID].NewObjectNumber); |
1894 | 0 | gp_fwrite(Buf, strlen(Buf), 1, linear_params->Lin_File.file); |
1895 | 0 | source = next; |
1896 | 0 | } else { |
1897 | 0 | gp_fwrite(source, strlen(source), 1, linear_params->Lin_File.file); |
1898 | 0 | } |
1899 | 0 | } while (target); |
1900 | |
|
1901 | 0 | while (Size) { |
1902 | 0 | if (Size > ScratchSize) { |
1903 | 0 | code = gp_fread(Scratch, ScratchSize, 1, linear_params->sfile); |
1904 | 0 | if (code != 1) |
1905 | 0 | return_error(gs_error_ioerror); |
1906 | 0 | gp_fwrite(Scratch, ScratchSize, 1, linear_params->Lin_File.file); |
1907 | 0 | Size -= ScratchSize; |
1908 | 0 | } else { |
1909 | 0 | code = gp_fread(Scratch, Size, 1, linear_params->sfile); |
1910 | 0 | if (code != 1) |
1911 | 0 | return_error(gs_error_ioerror); |
1912 | 0 | gp_fwrite(Scratch, Size, 1, linear_params->Lin_File.file); |
1913 | 0 | Size = 0; |
1914 | 0 | } |
1915 | 0 | }; |
1916 | |
|
1917 | 0 | gs_free_object(pdev->pdf_memory, Scratch, "Free working memory for object rewriting"); |
1918 | 0 | return 0; |
1919 | 0 | } |
1920 | | |
1921 | | static int flush_hint_stream(pdf_linearisation_t *linear_params) |
1922 | 0 | { |
1923 | 0 | int code; |
1924 | |
|
1925 | 0 | code = gp_fwrite(linear_params->HintBuffer, linear_params->HintByte, 1, linear_params->sfile); |
1926 | 0 | linear_params->HintBits = 0; |
1927 | 0 | linear_params->HintByte = 0; |
1928 | 0 | return code; |
1929 | 0 | } |
1930 | | |
1931 | | static int write_hint_stream(pdf_linearisation_t *linear_params, gs_offset_t val, char size_bits) |
1932 | 0 | { |
1933 | 0 | unsigned int input_mask, output_mask; |
1934 | |
|
1935 | 0 | if (size_bits == 0) |
1936 | 0 | return 0; |
1937 | | |
1938 | 0 | while(size_bits) { |
1939 | 0 | input_mask = 1 << (size_bits - 1); |
1940 | 0 | output_mask = 0x80 >> linear_params->HintBits; |
1941 | 0 | if (input_mask & val) |
1942 | 0 | linear_params->HintBuffer[linear_params->HintByte] |= output_mask; |
1943 | 0 | else |
1944 | 0 | linear_params->HintBuffer[linear_params->HintByte] &= ~output_mask; |
1945 | 0 | size_bits--; |
1946 | 0 | linear_params->HintBits++; |
1947 | 0 | if (linear_params->HintBits == 8) { |
1948 | 0 | linear_params->HintByte++; |
1949 | 0 | if (linear_params->HintByte > 254) { |
1950 | 0 | flush_hint_stream(linear_params); |
1951 | 0 | memset(linear_params->HintBuffer, 0x00, 256); |
1952 | 0 | } |
1953 | 0 | linear_params->HintBits = 0; |
1954 | 0 | } |
1955 | 0 | } |
1956 | 0 | return 0; |
1957 | 0 | } |
1958 | | |
1959 | | static int pdf_linearise(gx_device_pdf *pdev, pdf_linearisation_t *linear_params) |
1960 | 0 | { |
1961 | 0 | char Buffer[1024]; |
1962 | 0 | char Header[32], Binary[9] = "%\307\354\217\242\n", LDict[1024], fileID[35], Pad; |
1963 | 0 | int level = (int)(pdev->CompatibilityLevel * 10 + 0.5), i, j; |
1964 | 0 | int code=0, Part1To6 = 2; /* Include space for linearisation dict */ |
1965 | 0 | int Part7To8 = 1; |
1966 | 0 | int PartNone = 1; |
1967 | 0 | int Part9 = 1; |
1968 | 0 | int LDictObj, HintStreamObj, k; |
1969 | 0 | char T; |
1970 | 0 | int64_t mainxref, Length, HintStreamLen, HintStreamStart, HintLength, SharedHintOffset; |
1971 | |
|
1972 | 0 | fileID[0] = '<'; |
1973 | 0 | fileID[33] = '>'; |
1974 | 0 | fileID[34] = 0x00; |
1975 | 0 | for (i = 0;i< sizeof(pdev->fileID);i++) { |
1976 | 0 | T = pdev->fileID[i] >> 4; |
1977 | 0 | if (T > 9) { |
1978 | 0 | fileID[(i*2) + 1] = T - 10 + 'A'; |
1979 | 0 | } else { |
1980 | 0 | fileID[(i*2) + 1] = T + '0'; |
1981 | 0 | } |
1982 | 0 | T = pdev->fileID[i] & 0x0f; |
1983 | 0 | if (T > 9) { |
1984 | 0 | fileID[(i*2) + 2] = T - 10 + 'A'; |
1985 | 0 | } else { |
1986 | 0 | fileID[(i*2) + 2] = T + '0'; |
1987 | 0 | } |
1988 | 0 | } |
1989 | | |
1990 | | /* Make sure we've written everything to the main file */ |
1991 | 0 | sflush(pdev->strm); |
1992 | 0 | linear_params->sfile = pdev->file; |
1993 | 0 | linear_params->MainFileEnd = gp_ftell(pdev->file); |
1994 | |
|
1995 | 0 | linear_params->PageHints = NULL; |
1996 | 0 | linear_params->SharedHints = NULL; |
1997 | |
|
1998 | | #ifdef LINEAR_DEBUGGING |
1999 | | code = gx_device_open_output_file((gx_device *)pdev, "/temp/linear.pdf", |
2000 | | true, true, &linear_params->Lin_File.file); |
2001 | | #else |
2002 | 0 | code = pdf_open_temp_file(pdev, &linear_params->Lin_File); |
2003 | 0 | #endif |
2004 | 0 | if (code < 0) |
2005 | 0 | return code; |
2006 | | |
2007 | 0 | linear_params->Lin_File.strm = 0x0; |
2008 | | |
2009 | | /* Count resources used by page 1 */ |
2010 | 0 | linear_params->NumPage1Resources=0; |
2011 | 0 | for (i = 0;i < pdev->ResourceUsageSize; i++) { |
2012 | 0 | if (pdev->ResourceUsage[i].PageUsage == 1) |
2013 | 0 | linear_params->NumPage1Resources++; |
2014 | 0 | if (pdev->ResourceUsage[i].PageUsage == resource_usage_part1_structure) |
2015 | 0 | linear_params->NumPart1StructureResources++; |
2016 | 0 | } |
2017 | | |
2018 | | /* Count resources associated with pages */ |
2019 | 0 | linear_params->NumUniquePageResources=0; |
2020 | 0 | linear_params->NumSharedResources=0; |
2021 | 0 | for (i = 0;i < pdev->ResourceUsageSize; i++) { |
2022 | 0 | if (pdev->ResourceUsage[i].PageUsage > 1) |
2023 | 0 | linear_params->NumUniquePageResources++; |
2024 | 0 | if (pdev->ResourceUsage[i].PageUsage == resource_usage_page_shared) |
2025 | 0 | linear_params->NumSharedResources++; |
2026 | 0 | } |
2027 | | |
2028 | | /* Count resources not associated with pages */ |
2029 | 0 | linear_params->NumNonPageResources=0; |
2030 | 0 | for (i = 1;i < pdev->ResourceUsageSize; i++) { |
2031 | 0 | if (pdev->ResourceUsage[i].PageUsage == 0) |
2032 | 0 | linear_params->NumNonPageResources++; |
2033 | 0 | } |
2034 | | |
2035 | | /* Count resources not associated with pages */ |
2036 | 0 | linear_params->NumPart9Resources=0; |
2037 | 0 | for (i = 0;i < pdev->ResourceUsageSize; i++) { |
2038 | 0 | if (pdev->ResourceUsage[i].PageUsage == resource_usage_part9_structure) |
2039 | 0 | linear_params->NumPart9Resources++; |
2040 | 0 | } |
2041 | |
|
2042 | 0 | Part1To6 += linear_params->NumUniquePageResources + linear_params->NumSharedResources + linear_params->NumNonPageResources + linear_params->NumPart9Resources; |
2043 | 0 | PartNone += linear_params->NumUniquePageResources + linear_params->NumSharedResources; |
2044 | 0 | Part9 += linear_params->NumUniquePageResources + linear_params->NumSharedResources + linear_params->NumNonPageResources; |
2045 | 0 | LDictObj = linear_params->NumUniquePageResources + linear_params->NumSharedResources + linear_params->NumNonPageResources + linear_params->NumPart9Resources + 1; |
2046 | | |
2047 | | /* Record length and positions of all the objects and calculate the new object number */ |
2048 | 0 | for (i = 1;i < pdev->ResourceUsageSize; i++) { |
2049 | 0 | int j; |
2050 | 0 | gs_offset_t end; |
2051 | |
|
2052 | 0 | pdev->ResourceUsage[i].OriginalOffset = linear_params->Offsets[i]; |
2053 | |
|
2054 | 0 | end = linear_params->xref; |
2055 | 0 | for (j=0;j<=linear_params->LastResource;j++) { |
2056 | 0 | if (linear_params->Offsets[j] > linear_params->Offsets[i] && linear_params->Offsets[j] < end) |
2057 | 0 | end = linear_params->Offsets[j]; |
2058 | 0 | } |
2059 | 0 | pdev->ResourceUsage[i].Length = end - linear_params->Offsets[i]; |
2060 | |
|
2061 | 0 | if (pdev->ResourceUsage[i].PageUsage == 1 || pdev->ResourceUsage[i].PageUsage == resource_usage_part1_structure) |
2062 | 0 | pdev->ResourceUsage[i].NewObjectNumber = Part1To6++; |
2063 | 0 | else { |
2064 | 0 | if (pdev->ResourceUsage[i].PageUsage == resource_usage_not_referenced) |
2065 | 0 | pdev->ResourceUsage[i].NewObjectNumber = PartNone++; |
2066 | 0 | else { |
2067 | 0 | if (pdev->ResourceUsage[i].PageUsage == resource_usage_part9_structure) |
2068 | 0 | pdev->ResourceUsage[i].NewObjectNumber = Part9++; |
2069 | 0 | else |
2070 | 0 | pdev->ResourceUsage[i].NewObjectNumber = Part7To8++; |
2071 | 0 | } |
2072 | 0 | } |
2073 | 0 | } |
2074 | 0 | gs_free_object(pdev->pdf_memory, linear_params->Offsets, "free temp xref storage"); |
2075 | |
|
2076 | | #ifdef LINEAR_DEBUGGING |
2077 | | { |
2078 | | dmprintf6(pdev->pdf_memory, "NumPage1Resources %ld\tNumPart1StructureResources %ld\t\tNumUniquePageResources %ld\tNumSharedResources %ld\t\tNumNonPageResources %d\nNumPart9Resources %ld\n", linear_params->NumPage1Resources, linear_params->NumPart1StructureResources, linear_params->NumUniquePageResources, linear_params->NumSharedResources, linear_params->NumNonPageResources, linear_params->NumPart9Resources); |
2079 | | dmprintf(pdev->pdf_memory, "Old ID\tPage Usage\tNew ID\n"); |
2080 | | for (i = 1;i < pdev->ResourceUsageSize; i++) { |
2081 | | dmprintf3(pdev->pdf_memory, "%d\t%d\t%d\n", i, pdev->ResourceUsage[i].PageUsage, pdev->ResourceUsage[i].NewObjectNumber); |
2082 | | } |
2083 | | } |
2084 | | #endif |
2085 | | /* Linearisation. Part 1, file header */ |
2086 | 0 | gs_snprintf(Header, sizeof(Header), "%%PDF-%d.%d\n", level / 10, level % 10); |
2087 | 0 | gp_fwrite(Header, strlen(Header), 1, linear_params->Lin_File.file); |
2088 | 0 | if (pdev->binary_ok) |
2089 | 0 | gp_fwrite(Binary, strlen(Binary), 1, linear_params->Lin_File.file); |
2090 | 0 | pdfwrite_fwrite_args_comment(pdev, linear_params->Lin_File.file); |
2091 | |
|
2092 | 0 | pdf_record_usage(pdev, linear_params->LastResource + 1, resource_usage_part1_structure); |
2093 | 0 | pdev->ResourceUsage[linear_params->LastResource + 1].OriginalOffset = 0; |
2094 | 0 | pdev->ResourceUsage[linear_params->LastResource + 1].NewObjectNumber = LDictObj; |
2095 | 0 | pdev->ResourceUsage[linear_params->LastResource + 1].LinearisedOffset = gp_ftell(linear_params->Lin_File.file); |
2096 | | |
2097 | | /* Linearisation. Part 2, the Linearisation dictioanry */ |
2098 | 0 | linear_params->LDictOffset = gp_ftell(linear_params->Lin_File.file); |
2099 | 0 | gs_snprintf(LDict, sizeof(LDict), "%d 0 obj\n<< \n", |
2100 | 0 | LDictObj); |
2101 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->Lin_File.file); |
2102 | | |
2103 | | /* First page cross-reference table here (Part 3) */ |
2104 | 0 | linear_params->FirstxrefOffset = gp_ftell(linear_params->Lin_File.file); |
2105 | 0 | gs_snprintf(Header, sizeof(Header), "xref\n%d %d\n", LDictObj, Part1To6 - LDictObj + 1); /* +1 for the primary hint stream */ |
2106 | 0 | gp_fwrite(Header, strlen(Header), 1, linear_params->Lin_File.file); |
2107 | |
|
2108 | 0 | gs_snprintf(Header, sizeof(Header), "0000000000 00000 n \n"); |
2109 | |
|
2110 | 0 | for (i = LDictObj;i <= linear_params->LastResource + 2; i++) { |
2111 | 0 | gp_fwrite(Header, 20, 1, linear_params->Lin_File.file); |
2112 | 0 | } |
2113 | | |
2114 | | /* Size below is given as the Last Resource in the original file, +1 for object 0 (always free) |
2115 | | * +1 for the linearisation dict and +1 for the primary hint stream. |
2116 | | */ |
2117 | 0 | linear_params->FirsttrailerOffset = gp_ftell(linear_params->Lin_File.file); |
2118 | 0 | if (pdev->OmitID) |
2119 | 0 | gs_snprintf(LDict, sizeof(LDict), "\ntrailer\n<</Size %"PRId64"/Info %d 0 R/Root %d 0 R/Prev %d>>\nstartxref\r\n0\n%%%%EOF\n \n", |
2120 | 0 | linear_params->LastResource + 3, pdev->ResourceUsage[linear_params->Info_id].NewObjectNumber, pdev->ResourceUsage[linear_params->Catalog_id].NewObjectNumber, 0); |
2121 | 0 | else |
2122 | 0 | gs_snprintf(LDict, sizeof(LDict), "\ntrailer\n<</Size %"PRId64"/Info %d 0 R/Root %d 0 R/ID[%s%s]/Prev %d>>\nstartxref\r\n0\n%%%%EOF\n \n", |
2123 | 0 | linear_params->LastResource + 3, pdev->ResourceUsage[linear_params->Info_id].NewObjectNumber, pdev->ResourceUsage[linear_params->Catalog_id].NewObjectNumber, fileID, fileID, 0); |
2124 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->Lin_File.file); |
2125 | | |
2126 | | /* Write document catalog (Part 4) */ |
2127 | 0 | code = rewrite_object(pdev, linear_params, linear_params->Catalog_id); |
2128 | 0 | if (code < 0) |
2129 | 0 | goto error; |
2130 | | |
2131 | | /* In here we need the ViewerPreferences (don't think we support this), |
2132 | | * the PageMode entry (I think this would be direct in the catalog), The |
2133 | | * Threads entry (if any), The OpenAction (again, direct object ?) The |
2134 | | * AcroForm object (don't think we support this) and (TaDa) the Encrypt entry |
2135 | | * in the first page trailer dictionary. |
2136 | | */ |
2137 | | |
2138 | | /* First page section (Part 6) NB we do not currently support the OpenAction |
2139 | | * In the Catalogso this is always page 0, this needs to change if we ever |
2140 | | * support the OpenAction. The 1.7 PDF Reference says in implementation note 181 |
2141 | | * that Acrobat always treats page 0 as the first page for linearisation purposes |
2142 | | * EVEN IF there is an OpenAction, so we are Acrobat-compatible :-) |
2143 | | */ |
2144 | 0 | code = rewrite_object(pdev, linear_params, pdev->pages[0].Page->id); |
2145 | 0 | if (code < 0) |
2146 | 0 | goto error; |
2147 | 0 | for (i = 0;i < pdev->ResourceUsageSize; i++) { |
2148 | | /* we explicitly write the page objct above, make sure when writing the |
2149 | | * 'objects uniquely used on the page' that we don't write the page object again! |
2150 | | */ |
2151 | 0 | if (pdev->ResourceUsage[i].PageUsage == 1 && i != pdev->pages[0].Page->id) { |
2152 | 0 | code = rewrite_object(pdev, linear_params, i); |
2153 | 0 | if (code < 0) |
2154 | 0 | goto error; |
2155 | 0 | } |
2156 | 0 | } |
2157 | 0 | linear_params->E = gp_ftell(linear_params->Lin_File.file); |
2158 | | |
2159 | | /* Primary Hint Stream here (Part 5) |
2160 | | * this is a FIXME |
2161 | | */ |
2162 | 0 | HintStreamObj = linear_params->LastResource + 2; |
2163 | 0 | pdf_record_usage(pdev, HintStreamObj, resource_usage_part1_structure); |
2164 | 0 | pdev->ResourceUsage[HintStreamObj].OriginalOffset = 0; |
2165 | 0 | pdev->ResourceUsage[HintStreamObj].NewObjectNumber = HintStreamObj; |
2166 | 0 | pdev->ResourceUsage[HintStreamObj].LinearisedOffset = gp_ftell(linear_params->Lin_File.file); |
2167 | | |
2168 | | /* We don't actually embed the hint stream yet. We will do this when we copy the 'linearised' |
2169 | | * PDF file from the temp file back to the main file, and will insert the hint stream at this point. |
2170 | | * We will then know all the offsets of all the objects following the hint stream, so we can |
2171 | | * write these correctly into the hint stream. Since we will then know the size of the hint |
2172 | | * stream, we can add this to the stored offsets to generate the 'real' offsets for teh xref. |
2173 | | */ |
2174 | | |
2175 | | /* All remaining pages (part 7) */ |
2176 | 0 | for (i = 1;i < pdev->next_page;i++) { |
2177 | 0 | code = rewrite_object(pdev, linear_params, pdev->pages[i].Page->id); |
2178 | 0 | if (code < 0) |
2179 | 0 | goto error; |
2180 | 0 | for (j = 0;j < pdev->ResourceUsageSize; j++) { |
2181 | | /* we explicitly write the page objct above, make sure when writing the |
2182 | | * 'objects uniquely used on the page' that we don't write the page object again! |
2183 | | */ |
2184 | 0 | if (pdev->ResourceUsage[j].PageUsage - 1 == i && j != pdev->pages[i].Page->id) { |
2185 | 0 | code = rewrite_object(pdev, linear_params, j); |
2186 | 0 | if (code < 0) |
2187 | 0 | goto error; |
2188 | 0 | } |
2189 | 0 | } |
2190 | 0 | } |
2191 | | |
2192 | | /* Shared objects for all pages except the first (part 8) */ |
2193 | 0 | for (i = 0;i < pdev->ResourceUsageSize; i++) { |
2194 | 0 | if (pdev->ResourceUsage[i].PageUsage == resource_usage_page_shared) { |
2195 | 0 | code = rewrite_object(pdev, linear_params, i); |
2196 | 0 | if (code < 0) |
2197 | 0 | goto error; |
2198 | 0 | } |
2199 | 0 | } |
2200 | | |
2201 | | /* All objects not on any page (Part 9) */ |
2202 | 0 | for (i = 1;i < pdev->ResourceUsageSize; i++) { |
2203 | 0 | if (pdev->ResourceUsage[i].PageUsage == resource_usage_not_referenced || |
2204 | 0 | pdev->ResourceUsage[i].PageUsage == resource_usage_part9_structure) { |
2205 | 0 | code = rewrite_object(pdev, linear_params, i); |
2206 | 0 | if (code < 0) |
2207 | 0 | goto error; |
2208 | 0 | } |
2209 | 0 | } |
2210 | | |
2211 | | /* We won't bother with an overflow hint stream (I Hope) (part 10) |
2212 | | * Implementation Note 181 in the 1.7 PDF Reference Manual says Acrobat |
2213 | | * doesn't support overflow hint streams anyway.... |
2214 | | */ |
2215 | | |
2216 | | /* Now copy the linearised data back to the main file, as far as the offset of the |
2217 | | * primary hint stream |
2218 | | */ |
2219 | 0 | if (gp_fseek(linear_params->Lin_File.file, 0, SEEK_SET) != 0) { |
2220 | 0 | code = gs_error_ioerror; |
2221 | 0 | goto error; |
2222 | 0 | } |
2223 | 0 | if (gp_fseek(linear_params->sfile, 0, SEEK_SET) != 0){ |
2224 | 0 | code = gs_error_ioerror; |
2225 | 0 | goto error; |
2226 | 0 | } |
2227 | 0 | Length = pdev->ResourceUsage[HintStreamObj].LinearisedOffset; |
2228 | 0 | while (Length && code >= 0) { |
2229 | 0 | if (Length > 1024) { |
2230 | 0 | code = gp_fread(Buffer, 1024, 1, linear_params->Lin_File.file); |
2231 | 0 | gp_fwrite(Buffer, 1024, 1, linear_params->sfile); |
2232 | 0 | Length -= 1024; |
2233 | 0 | } else { |
2234 | 0 | code = gp_fread(Buffer, Length, 1, linear_params->Lin_File.file); |
2235 | 0 | gp_fwrite(Buffer, Length, 1, linear_params->sfile); |
2236 | 0 | Length = 0; |
2237 | 0 | } |
2238 | 0 | } |
2239 | | /* insert the primary hint stream */ |
2240 | 0 | gs_snprintf(LDict, sizeof(LDict), "%d 0 obj\n<</Length \n/S >>\nstream\n", HintStreamObj); |
2241 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->sfile); |
2242 | |
|
2243 | 0 | HintStreamStart = gp_ftell(linear_params->sfile); |
2244 | 0 | memset(linear_params->HintBuffer, 0x00, 256); |
2245 | 0 | linear_params->HintBits = linear_params->HintByte = 0; |
2246 | |
|
2247 | 0 | linear_params->PageHints = (page_hint_stream_t *)gs_alloc_bytes(pdev->pdf_memory, pdev->next_page * sizeof(page_hint_stream_t), "Hints for the pages"); |
2248 | 0 | if (linear_params->PageHints == NULL) { |
2249 | 0 | code = gs_error_VMerror; |
2250 | 0 | goto error; |
2251 | 0 | } |
2252 | 0 | memset(linear_params->PageHints, 0x00, pdev->next_page * sizeof(page_hint_stream_t)); |
2253 | 0 | linear_params->NumPageHints = pdev->next_page; |
2254 | |
|
2255 | 0 | linear_params->SharedHints = (shared_hint_stream_t *)gs_alloc_bytes(pdev->pdf_memory, (linear_params->NumPage1Resources + linear_params->NumSharedResources) * sizeof(shared_hint_stream_t), "Hints for the shared objects"); |
2256 | 0 | if (linear_params->SharedHints == NULL) { |
2257 | 0 | code = gs_error_VMerror; |
2258 | 0 | goto error; |
2259 | 0 | } |
2260 | 0 | memset(linear_params->SharedHints, 0x00, (linear_params->NumPage1Resources + linear_params->NumSharedResources) * sizeof(shared_hint_stream_t)); |
2261 | 0 | linear_params->NumSharedHints = linear_params->NumPage1Resources + linear_params->NumSharedResources; |
2262 | |
|
2263 | 0 | memset(&linear_params->PageHintHeader,0x00, sizeof(page_hint_stream_header_t)); |
2264 | 0 | memset(&linear_params->SharedHintHeader,0x00, sizeof(shared_hint_stream_header_t)); |
2265 | |
|
2266 | 0 | linear_params->PageHintHeader.LeastObjectsPerPage = 0x7fffffff; |
2267 | 0 | linear_params->PageHintHeader.LeastPageLength = 0x7fffffff; |
2268 | 0 | linear_params->PageHintHeader.LeastPageOffset = 0x7fffffff; |
2269 | 0 | linear_params->PageHintHeader.LeastContentLength = 0x7fffffff; |
2270 | | |
2271 | | /* Record the offset and length of the content stream for each page */ |
2272 | 0 | for (i=0;i<pdev->next_page;i++) { |
2273 | 0 | int PageContentID = pdev->pages[i].contents_id; |
2274 | 0 | pdf_linearisation_record_t *record = &pdev->ResourceUsage[PageContentID]; |
2275 | 0 | page_hint_stream_t *pagehint = &linear_params->PageHints[i]; |
2276 | |
|
2277 | 0 | pagehint->ContentOffset = record->LinearisedOffset; |
2278 | 0 | pagehint->ContentLength = record->Length; |
2279 | 0 | } |
2280 | | |
2281 | | /* For every object, record it in the page hints and shared hints. k is a counter |
2282 | | * of the current shared object, we increment it each time we find a new one. |
2283 | | */ |
2284 | 0 | k = 0; |
2285 | 0 | for (i = 1;i < pdev->ResourceUsageSize; i++) { |
2286 | 0 | pdf_linearisation_record_t *record = &pdev->ResourceUsage[i]; |
2287 | |
|
2288 | 0 | if (record->PageUsage < resource_usage_page_shared || record->PageUsage == resource_usage_not_referenced) |
2289 | 0 | continue; |
2290 | 0 | if (record->PageUsage == resource_usage_page_shared) { |
2291 | | /* shared objects are recorded in the shared object hints, and also the page hints */ |
2292 | 0 | for (j=0;j<record->NumPagesUsing;j++) { |
2293 | 0 | int page = record->PageList[j]; |
2294 | 0 | page_hint_stream_t *pagehint; |
2295 | |
|
2296 | 0 | if (page >= pdev->next_page) |
2297 | | /* This can happen if the job makes marks, but does not call showpage */ |
2298 | 0 | continue; |
2299 | | |
2300 | 0 | pagehint = &linear_params->PageHints[page - 1]; |
2301 | 0 | if (pagehint->SharedObjectRef){ |
2302 | 0 | int *Temp = (int *)gs_alloc_bytes(pdev->pdf_memory, (pagehint->NumSharedObjects + 1) * sizeof(int), "realloc shared object hints"); |
2303 | 0 | if (Temp == NULL) { |
2304 | 0 | code = gs_note_error(gs_error_VMerror); |
2305 | 0 | goto error; |
2306 | 0 | } |
2307 | 0 | memcpy(Temp, pagehint->SharedObjectRef, (pagehint->NumSharedObjects) * sizeof(int)); |
2308 | 0 | gs_free_object(pdev->pdf_memory, pagehint->SharedObjectRef, "realloc shared object hints"); |
2309 | 0 | pagehint->SharedObjectRef = (unsigned int *)Temp; |
2310 | 0 | } else { |
2311 | 0 | pagehint->SharedObjectRef = (unsigned int *)gs_alloc_bytes(pdev->pdf_memory, (pagehint->NumSharedObjects + 1) * sizeof(int), "shared object hints"); |
2312 | 0 | if (pagehint->SharedObjectRef == NULL) { |
2313 | 0 | code = gs_note_error(gs_error_VMerror); |
2314 | 0 | goto error; |
2315 | 0 | } |
2316 | 0 | } |
2317 | 0 | pagehint->SharedObjectRef[pagehint->NumSharedObjects] = i; |
2318 | 0 | pagehint->NumSharedObjects++; |
2319 | 0 | } |
2320 | 0 | linear_params->SharedHints[k].ObjectNumber = record->NewObjectNumber; |
2321 | 0 | linear_params->SharedHints[k].ObjectOffset = record->LinearisedOffset; |
2322 | 0 | linear_params->SharedHints[k++].ObjectLength = record->Length; |
2323 | 0 | } else { |
2324 | | /* Objects uniquely used on a page are stored in the page hints table, except |
2325 | | * page 1 whose objects are *also* stored in the shared objects hints. |
2326 | | */ |
2327 | 0 | int page = record->PageUsage, pageID = pdev->pages[page - 1].Page->id; |
2328 | 0 | int64_t LinearisedPageOffset = pdev->ResourceUsage[pageID].LinearisedOffset; |
2329 | 0 | page_hint_stream_t *pagehint; |
2330 | | |
2331 | | /* If the final page makes marks but does not call showpage we don't emit it |
2332 | | * which can lead to references to non-existent pages. |
2333 | | */ |
2334 | 0 | if (page <= pdev->next_page) { |
2335 | 0 | pagehint = &linear_params->PageHints[page - 1]; |
2336 | 0 | pagehint->NumUniqueObjects++; |
2337 | 0 | if (record->LinearisedOffset - LinearisedPageOffset > pagehint->PageLength) |
2338 | 0 | pagehint->PageLength = (record->LinearisedOffset + record->Length) - LinearisedPageOffset; |
2339 | 0 | if (page == 1) { |
2340 | 0 | linear_params->SharedHintHeader.FirstPageEntries++; |
2341 | 0 | } |
2342 | 0 | } |
2343 | 0 | } |
2344 | 0 | } |
2345 | | |
2346 | 0 | linear_params->PageHintHeader.LargestSharedObject = k; |
2347 | |
|
2348 | 0 | for (i = 0;i < linear_params->NumPageHints;i++) { |
2349 | 0 | page_hint_stream_t *hint = &linear_params->PageHints[i]; |
2350 | |
|
2351 | 0 | if (hint->NumUniqueObjects + hint->NumSharedObjects < linear_params->PageHintHeader.LeastObjectsPerPage) |
2352 | 0 | linear_params->PageHintHeader.LeastObjectsPerPage = hint->NumUniqueObjects + hint->NumSharedObjects; |
2353 | 0 | if (hint->NumUniqueObjects + hint->NumSharedObjects > linear_params->PageHintHeader.MostObjectsPerPage) |
2354 | 0 | linear_params->PageHintHeader.MostObjectsPerPage = hint->NumUniqueObjects + hint->NumSharedObjects; |
2355 | 0 | if (hint->PageLength < linear_params->PageHintHeader.LeastPageLength) |
2356 | 0 | linear_params->PageHintHeader.LeastPageLength = hint->PageLength; |
2357 | 0 | if (hint->PageLength > linear_params->PageHintHeader.MostPageLength) |
2358 | 0 | linear_params->PageHintHeader.MostPageLength = hint->PageLength; |
2359 | 0 | if (hint->ContentOffset < linear_params->PageHintHeader.LeastPageOffset) |
2360 | 0 | linear_params->PageHintHeader.LeastPageOffset = hint->ContentOffset; |
2361 | 0 | if (hint->ContentOffset > linear_params->PageHintHeader.MostPageOffset) |
2362 | 0 | linear_params->PageHintHeader.MostPageOffset = hint->ContentOffset; |
2363 | 0 | if (hint->ContentLength < linear_params->PageHintHeader.LeastContentLength) |
2364 | 0 | linear_params->PageHintHeader.LeastContentLength = hint->ContentLength; |
2365 | 0 | if (hint->ContentLength > linear_params->PageHintHeader.MostContentLength) |
2366 | 0 | linear_params->PageHintHeader.MostContentLength = hint->ContentLength; |
2367 | 0 | if (hint->NumSharedObjects > linear_params->PageHintHeader.MostSharedObjects) |
2368 | 0 | linear_params->PageHintHeader.MostSharedObjects = hint->NumSharedObjects; |
2369 | 0 | } |
2370 | |
|
2371 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.LeastObjectsPerPage, 32); |
2372 | 0 | write_hint_stream(linear_params, (gs_offset_t)pdev->ResourceUsage[pdev->pages[0].Page->id].LinearisedOffset, 32); |
2373 | 0 | i = (linear_params->PageHintHeader.MostObjectsPerPage - linear_params->PageHintHeader.MostObjectsPerPage + 1); |
2374 | 0 | j = 0; |
2375 | 0 | while (i) { |
2376 | 0 | i = i / 2; |
2377 | 0 | j++; |
2378 | 0 | } |
2379 | 0 | linear_params->PageHintHeader.ObjectNumBits = j; |
2380 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.ObjectNumBits, 16); |
2381 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.LeastPageLength, 32); |
2382 | 0 | i = (linear_params->PageHintHeader.MostPageLength - linear_params->PageHintHeader.LeastPageLength + 1); |
2383 | 0 | j = 0; |
2384 | 0 | while (i) { |
2385 | 0 | i = i / 2; |
2386 | 0 | j++; |
2387 | 0 | } |
2388 | 0 | linear_params->PageHintHeader.PageLengthNumBits = j; |
2389 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.PageLengthNumBits, 16); |
2390 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.LeastPageOffset, 32); |
2391 | 0 | i = (linear_params->PageHintHeader.MostPageOffset - linear_params->PageHintHeader.LeastPageOffset + 1); |
2392 | 0 | j = 0; |
2393 | 0 | while (i) { |
2394 | 0 | i = i / 2; |
2395 | 0 | j++; |
2396 | 0 | } |
2397 | 0 | linear_params->PageHintHeader.PageOffsetNumBits = j; |
2398 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.PageOffsetNumBits, 16); |
2399 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.LeastContentLength, 32); |
2400 | 0 | i = (linear_params->PageHintHeader.MostContentLength - linear_params->PageHintHeader.LeastContentLength + 1); |
2401 | 0 | j = 0; |
2402 | 0 | while (i) { |
2403 | 0 | i = i / 2; |
2404 | 0 | j++; |
2405 | 0 | } |
2406 | 0 | linear_params->PageHintHeader.ContentLengthNumBits = j; |
2407 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.ContentLengthNumBits, 16); |
2408 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.MostSharedObjects, 16); |
2409 | 0 | i = (linear_params->PageHintHeader.LargestSharedObject + 1); |
2410 | 0 | j = 0; |
2411 | 0 | while (i) { |
2412 | 0 | i = i / 2; |
2413 | 0 | j++; |
2414 | 0 | } |
2415 | 0 | linear_params->PageHintHeader.SharedObjectNumBits = j; |
2416 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.SharedObjectNumBits, 16); |
2417 | 0 | j = 1; |
2418 | 0 | write_hint_stream(linear_params, (gs_offset_t)j, 16); |
2419 | 0 | write_hint_stream(linear_params, (gs_offset_t)j, 16); |
2420 | |
|
2421 | | #ifdef LINEAR_DEBUGGING |
2422 | | dmprintf1(pdev->pdf_memory, "LeastObjectsPerPage %d\n", linear_params->PageHintHeader.LeastObjectsPerPage); |
2423 | | dmprintf1(pdev->pdf_memory, "Page 1 Offset %"PRId64"\n", pdev->ResourceUsage[pdev->pages[0].Page->id].LinearisedOffset); |
2424 | | dmprintf1(pdev->pdf_memory, "ObjectNumBits %d\n", linear_params->PageHintHeader.ObjectNumBits); |
2425 | | dmprintf1(pdev->pdf_memory, "LeastPageLength %d\n", linear_params->PageHintHeader.LeastPageLength); |
2426 | | dmprintf1(pdev->pdf_memory, "MostPagelength %d\n", linear_params->PageHintHeader.MostPageLength); |
2427 | | dmprintf1(pdev->pdf_memory, "PaegLengthNumBits %d\n", linear_params->PageHintHeader.PageLengthNumBits); |
2428 | | dmprintf1(pdev->pdf_memory, "LeastPageOffset %d\n", linear_params->PageHintHeader.LeastPageOffset); |
2429 | | dmprintf1(pdev->pdf_memory, "MostPageOffset %d\n", linear_params->PageHintHeader.MostPageOffset); |
2430 | | dmprintf1(pdev->pdf_memory, "PageOffsetNumBits %d\n", linear_params->PageHintHeader.PageOffsetNumBits); |
2431 | | dmprintf1(pdev->pdf_memory, "LeastContentLength %d\n", linear_params->PageHintHeader.LeastContentLength); |
2432 | | dmprintf1(pdev->pdf_memory, "MostContentLength %d\n", linear_params->PageHintHeader.MostContentLength); |
2433 | | dmprintf1(pdev->pdf_memory, "COntentLengthNumBits %d\n", linear_params->PageHintHeader.ContentLengthNumBits); |
2434 | | dmprintf1(pdev->pdf_memory, "MostSharedObjects %d\n", linear_params->PageHintHeader.MostSharedObjects); |
2435 | | dmprintf1(pdev->pdf_memory, "LargetsSharedObject %d\n", linear_params->PageHintHeader.LargestSharedObject); |
2436 | | dmprintf1(pdev->pdf_memory, "SharedObjectNumBits %d\n", linear_params->PageHintHeader.SharedObjectNumBits); |
2437 | | dmprintf(pdev->pdf_memory, "Position Numerator 1\n"); |
2438 | | dmprintf(pdev->pdf_memory, "Position Denominator 1\n\n"); |
2439 | | #endif |
2440 | |
|
2441 | 0 | for (i=0;i < pdev->next_page;i++) { |
2442 | 0 | page_hint_stream_t *hint = &linear_params->PageHints[i]; |
2443 | 0 | int Num; |
2444 | |
|
2445 | 0 | Num = hint->NumUniqueObjects - linear_params->PageHintHeader.LeastObjectsPerPage; |
2446 | 0 | write_hint_stream(linear_params, (gs_offset_t)Num, linear_params->PageHintHeader.ObjectNumBits); |
2447 | 0 | dmprintf2(pdev->pdf_memory, "Page %d NumUniqueObjects %d\n", i, Num); |
2448 | 0 | } |
2449 | 0 | for (i=0;i < pdev->next_page;i++) { |
2450 | 0 | page_hint_stream_t *hint = &linear_params->PageHints[i]; |
2451 | 0 | int Num; |
2452 | |
|
2453 | 0 | Num = hint->PageLength - linear_params->PageHintHeader.LeastPageLength; |
2454 | 0 | write_hint_stream(linear_params, (gs_offset_t)Num, linear_params->PageHintHeader.PageLengthNumBits); |
2455 | 0 | dmprintf2(pdev->pdf_memory, "Page %d PageLength %d\n", i, Num); |
2456 | 0 | } |
2457 | 0 | for (i=0;i < pdev->next_page;i++) { |
2458 | 0 | page_hint_stream_t *hint = &linear_params->PageHints[i]; |
2459 | |
|
2460 | 0 | if (i == 0) { |
2461 | 0 | write_hint_stream(linear_params, (gs_offset_t)i, linear_params->PageHintHeader.SharedObjectNumBits); |
2462 | 0 | dmprintf2(pdev->pdf_memory, "Page %d NumSharedObjects %d\n", i, 1); |
2463 | 0 | } |
2464 | 0 | else { |
2465 | 0 | write_hint_stream(linear_params, (gs_offset_t)hint->NumSharedObjects, linear_params->PageHintHeader.SharedObjectNumBits); |
2466 | 0 | dmprintf2(pdev->pdf_memory, "Page %d NumSharedObjects %d\n", i, hint->NumSharedObjects); |
2467 | 0 | } |
2468 | 0 | } |
2469 | 0 | for (i=1;i < pdev->next_page;i++) { |
2470 | 0 | page_hint_stream_t *hint = &linear_params->PageHints[i]; |
2471 | |
|
2472 | 0 | for (j=0;j < hint->NumSharedObjects;j++) { |
2473 | 0 | write_hint_stream(linear_params, (gs_offset_t)hint->SharedObjectRef[j], linear_params->PageHintHeader.SharedObjectNumBits); |
2474 | 0 | dmprintf3(pdev->pdf_memory, "Page %d SharedObject %d ObjectRef %d\n", i, j, hint->SharedObjectRef[j]); |
2475 | 0 | } |
2476 | 0 | } |
2477 | |
|
2478 | 0 | for (i=0;i < pdev->next_page;i++) { |
2479 | 0 | page_hint_stream_t *hint = &linear_params->PageHints[i]; |
2480 | |
|
2481 | 0 | for (j=0;j < hint->NumSharedObjects;j++) { |
2482 | 0 | write_hint_stream(linear_params, (gs_offset_t)j, 1); |
2483 | 0 | dmprintf2(pdev->pdf_memory, "Page %d SharedObject %d Position Numerator 1\n", i, j); |
2484 | 0 | } |
2485 | 0 | } |
2486 | 0 | for (i=1;i < pdev->next_page;i++) { |
2487 | 0 | page_hint_stream_t *hint = &linear_params->PageHints[i]; |
2488 | 0 | int Num; |
2489 | |
|
2490 | 0 | Num = hint->ContentOffset - linear_params->PageHintHeader.LeastPageOffset; |
2491 | 0 | write_hint_stream(linear_params, (gs_offset_t)Num, linear_params->PageHintHeader.PageOffsetNumBits); |
2492 | 0 | dmprintf2(pdev->pdf_memory, "Page %d ContentStreamOffset %d\n", i, Num); |
2493 | 0 | } |
2494 | 0 | for (i=1;i < pdev->next_page;i++) { |
2495 | 0 | page_hint_stream_t *hint = &linear_params->PageHints[i]; |
2496 | 0 | int Num; |
2497 | |
|
2498 | 0 | Num = hint->ContentLength - linear_params->PageHintHeader.LeastContentLength; |
2499 | 0 | write_hint_stream(linear_params, (gs_offset_t)Num, linear_params->PageHintHeader.ContentLengthNumBits); |
2500 | 0 | dmprintf2(pdev->pdf_memory, "Page %d ContentStreamLength %d\n", i, Num); |
2501 | 0 | } |
2502 | 0 | flush_hint_stream(linear_params); |
2503 | |
|
2504 | 0 | SharedHintOffset = gp_ftell(linear_params->sfile) - HintStreamStart; |
2505 | 0 | linear_params->SharedHintHeader.FirstSharedObject = linear_params->SharedHints[0].ObjectNumber; |
2506 | 0 | linear_params->SharedHintHeader.LeastObjectLength = linear_params->SharedHints[0].ObjectLength; |
2507 | 0 | linear_params->SharedHintHeader.MostObjectLength = linear_params->SharedHints[0].ObjectLength; |
2508 | |
|
2509 | 0 | for (i = 1; i< linear_params->NumSharedHints; i++) { |
2510 | 0 | if (linear_params->SharedHints[i].ObjectNumber < linear_params->SharedHintHeader.FirstSharedObject) { |
2511 | 0 | linear_params->SharedHintHeader.FirstSharedObject = linear_params->SharedHints[1].ObjectNumber; |
2512 | 0 | linear_params->SharedHintHeader.FirstObjectOffset = linear_params->SharedHints[1].ObjectOffset; |
2513 | 0 | } |
2514 | 0 | if (linear_params->SharedHints[i].ObjectLength < linear_params->SharedHintHeader.LeastObjectLength) { |
2515 | 0 | linear_params->SharedHintHeader.LeastObjectLength = linear_params->SharedHints[i].ObjectLength; |
2516 | 0 | } |
2517 | 0 | if (linear_params->SharedHints[i].ObjectLength > linear_params->SharedHintHeader.MostObjectLength) { |
2518 | 0 | linear_params->SharedHintHeader.MostObjectLength = linear_params->SharedHints[i].ObjectLength; |
2519 | 0 | } |
2520 | 0 | } |
2521 | |
|
2522 | 0 | linear_params->SharedHintHeader.FirstPageEntries = linear_params->NumPage1Resources; |
2523 | 0 | linear_params->SharedHintHeader.NumSharedObjects = linear_params->NumSharedResources + linear_params->SharedHintHeader.FirstPageEntries; |
2524 | |
|
2525 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.FirstSharedObject, 32); |
2526 | 0 | dmprintf1(pdev->pdf_memory, "\nFirstSharedObject %d\n", linear_params->SharedHintHeader.FirstSharedObject); |
2527 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.FirstObjectOffset, 32); |
2528 | 0 | dmprintf1(pdev->pdf_memory, "FirstObjectOffset %"PRId64"\n", linear_params->SharedHintHeader.FirstObjectOffset); |
2529 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.FirstPageEntries, 32); |
2530 | 0 | dmprintf1(pdev->pdf_memory, "FirstPageEntries %d\n", linear_params->SharedHintHeader.FirstPageEntries); |
2531 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.NumSharedObjects, 32); |
2532 | 0 | dmprintf1(pdev->pdf_memory, "NumSharedObjects %d\n", linear_params->SharedHintHeader.NumSharedObjects); |
2533 | 0 | j = 1; |
2534 | 0 | write_hint_stream(linear_params, (gs_offset_t)j, 32); |
2535 | 0 | dmprintf(pdev->pdf_memory, "GreatestObjectsNumBits 1\n"); |
2536 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.FirstObjectOffset, 16); |
2537 | 0 | dmprintf1(pdev->pdf_memory, "FirstObjectOffset %"PRId64"\n", linear_params->SharedHintHeader.FirstObjectOffset); |
2538 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.LeastObjectLength, 32); |
2539 | 0 | dmprintf1(pdev->pdf_memory, "LeastObjectLength %d\n", linear_params->SharedHintHeader.LeastObjectLength); |
2540 | |
|
2541 | 0 | i = (linear_params->SharedHintHeader.MostObjectLength - linear_params->SharedHintHeader.LeastObjectLength + 1) / 2; |
2542 | 0 | j = 0; |
2543 | 0 | while (i) { |
2544 | 0 | i = i / 2; |
2545 | 0 | j++; |
2546 | 0 | } |
2547 | 0 | linear_params->SharedHintHeader.LengthNumBits = j + 1; |
2548 | 0 | write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.LengthNumBits, 16); |
2549 | |
|
2550 | 0 | for (i = 0; i< linear_params->NumSharedHints; i++) { |
2551 | 0 | unsigned int Length = linear_params->SharedHints[i].ObjectLength - linear_params->SharedHintHeader.LeastObjectLength; |
2552 | |
|
2553 | 0 | write_hint_stream(linear_params, (gs_offset_t)Length, linear_params->SharedHintHeader.LengthNumBits); |
2554 | 0 | dmprintf2(pdev->pdf_memory, "Shared Object group %d, Length %d\n", i, Length); |
2555 | 0 | } |
2556 | |
|
2557 | 0 | j = 0; |
2558 | 0 | for (i = 0; i< linear_params->NumSharedHints; i++) { |
2559 | 0 | write_hint_stream(linear_params, (gs_offset_t)j, 1); |
2560 | 0 | dmprintf1(pdev->pdf_memory, "Shared Object group %d, SignatureFlag false\n", i); |
2561 | 0 | } |
2562 | 0 | for (i = 0; i< linear_params->NumSharedHints; i++) { |
2563 | 0 | write_hint_stream(linear_params, (gs_offset_t)j, 1); |
2564 | 0 | dmprintf1(pdev->pdf_memory, "Shared Object group %d, NumObjects 1\n", i); |
2565 | 0 | } |
2566 | |
|
2567 | 0 | flush_hint_stream(linear_params); |
2568 | 0 | HintLength = gp_ftell(linear_params->sfile) - HintStreamStart; |
2569 | |
|
2570 | 0 | gs_snprintf(LDict, sizeof(LDict), "\nendstream\nendobj\n"); |
2571 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->sfile); |
2572 | | /* Calculate the length of the primary hint stream */ |
2573 | 0 | HintStreamLen = gp_ftell(linear_params->sfile) - pdev->ResourceUsage[HintStreamObj].LinearisedOffset; |
2574 | | /* Read remainder of linearised file and write to the final file */ |
2575 | 0 | do { |
2576 | 0 | code = gp_fread(Buffer, 1, 1024, linear_params->Lin_File.file); |
2577 | 0 | if (code > 0) |
2578 | 0 | gp_fwrite(Buffer, 1, code, linear_params->sfile); |
2579 | 0 | } while (code > 0); |
2580 | | |
2581 | | /* Main xref (part 11) */ |
2582 | 0 | mainxref = gp_ftell(linear_params->sfile); |
2583 | | /* Acrobat 9 and X (possibly other versions) won't recognise a file as |
2584 | | * optimised unless the file is at least 4k bytes in length (!!!) |
2585 | | * Also, it is possible that the new file might be smaller than the old one, If a |
2586 | | * frequently used object changed to a lower number (eg form object 100 to object 10) |
2587 | | * We don't close and reopen the file, so we need to make sure that any difference |
2588 | | * is filled in with white space. |
2589 | | */ |
2590 | 0 | if (linear_params->MainFileEnd < 4096) |
2591 | 0 | Length = 4096 - (mainxref + strlen(LDict) + strlen(Header) + LDictObj * 20); |
2592 | 0 | else |
2593 | 0 | Length = linear_params->MainFileEnd - (mainxref + strlen(LDict) + strlen(Header) + LDictObj * 20); |
2594 | 0 | Pad = ' '; |
2595 | |
|
2596 | 0 | while(Length > 0) { |
2597 | 0 | gp_fwrite(&Pad, 1, 1, linear_params->sfile); |
2598 | 0 | Length--; |
2599 | 0 | } |
2600 | | |
2601 | | /* Now the file is long enough, write the xref */ |
2602 | 0 | mainxref = gp_ftell(linear_params->sfile); |
2603 | 0 | gs_snprintf(Header, sizeof(Header), "xref\n0 %d\n", LDictObj); |
2604 | 0 | gp_fwrite(Header, strlen(Header), 1, linear_params->sfile); |
2605 | |
|
2606 | 0 | linear_params->T = gp_ftell(linear_params->sfile) - 1; |
2607 | 0 | gs_snprintf(Header, sizeof(Header), "0000000000 65535 f \n"); |
2608 | 0 | gp_fwrite(Header, strlen(Header), 1, linear_params->sfile); |
2609 | |
|
2610 | 0 | for (i = 1;i < LDictObj; i++) { |
2611 | 0 | for (j = 0; j < pdev->ResourceUsageSize;j++) { |
2612 | 0 | if (pdev->ResourceUsage[j].NewObjectNumber == i) { |
2613 | 0 | gs_snprintf(Header, sizeof(Header), "%010"PRId64" 00000 n \n", pdev->ResourceUsage[j].LinearisedOffset + HintStreamLen); |
2614 | 0 | gp_fwrite(Header, 20, 1, linear_params->sfile); |
2615 | 0 | } |
2616 | 0 | } |
2617 | 0 | } |
2618 | |
|
2619 | 0 | gs_snprintf(LDict, sizeof(LDict), "trailer\n<</Size %d>>\nstartxref\n%"PRId64"\n%%%%EOF\n", |
2620 | 0 | LDictObj, linear_params->FirstxrefOffset); |
2621 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->sfile); |
2622 | |
|
2623 | 0 | linear_params->FileLength = gp_ftell(linear_params->sfile); |
2624 | | /* Return to the linearisation dictionary and write it again filling |
2625 | | * in the missing data. |
2626 | | */ |
2627 | | /* Implementation Note 178 in the PDF Reference 1.7 says that Acrobat requires |
2628 | | * a white space after the '[' for the hint stream offset. This appears not to be true |
2629 | | * for current versions, but we follow it anyway for the possible benefit of older |
2630 | | * versions. |
2631 | | */ |
2632 | 0 | gp_fseek(linear_params->sfile, linear_params->LDictOffset, SEEK_SET); |
2633 | 0 | gs_snprintf(LDict, sizeof(LDict), "%d 0 obj\n<</Linearized 1/L %"PRId64"/H[ ", LDictObj, linear_params->FileLength); |
2634 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->sfile); |
2635 | |
|
2636 | 0 | gs_snprintf(LDict, sizeof(LDict), "%"PRId64"", pdev->ResourceUsage[HintStreamObj].LinearisedOffset); |
2637 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->sfile); |
2638 | 0 | gs_snprintf(LDict, sizeof(LDict), " %"PRId64"]", HintStreamLen); |
2639 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->sfile); |
2640 | | /* Implementation Note 180 in hte PDF Reference 1.7 says that Acrobat |
2641 | | * gets the 'E' value wrong. So its probably not important.... |
2642 | | */ |
2643 | 0 | gs_snprintf(LDict, sizeof(LDict), "/O %d/E %"PRId64"",pdev->ResourceUsage[pdev->pages[0].Page->id].NewObjectNumber, linear_params->E); |
2644 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->sfile); |
2645 | 0 | gs_snprintf(LDict, sizeof(LDict), "/N %d/T %"PRId64">>\nendobj\n", pdev->next_page, linear_params->T); |
2646 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->sfile); |
2647 | | |
2648 | | /* Return to the secondary xref and write it again filling |
2649 | | * in the missing offsets. |
2650 | | */ |
2651 | 0 | if (gp_fseek(linear_params->sfile, linear_params->FirstxrefOffset, SEEK_SET) != 0) { |
2652 | 0 | code = gs_error_ioerror; |
2653 | 0 | goto error; |
2654 | 0 | } |
2655 | 0 | gs_snprintf(Header, sizeof(Header), "xref\n%d %d\n", LDictObj, Part1To6 - LDictObj + 1); /* +1 for the primary hint stream */ |
2656 | 0 | gp_fwrite(Header, strlen(Header), 1, linear_params->sfile); |
2657 | |
|
2658 | 0 | for (i = LDictObj;i <= linear_params->LastResource + 2; i++) { |
2659 | 0 | for (j = 0; j < pdev->ResourceUsageSize;j++) { |
2660 | 0 | if (pdev->ResourceUsage[j].NewObjectNumber == i) { |
2661 | 0 | gs_snprintf(Header, sizeof(Header), "%010"PRId64" 00000 n \n", pdev->ResourceUsage[j].LinearisedOffset); |
2662 | 0 | gp_fwrite(Header, 20, 1, linear_params->sfile); |
2663 | 0 | } |
2664 | 0 | } |
2665 | 0 | } |
2666 | | |
2667 | | /* Return to the secondary trailer dict and write it again filling |
2668 | | * in the missing values. |
2669 | | */ |
2670 | 0 | code = gp_fseek(linear_params->sfile, linear_params->FirsttrailerOffset, SEEK_SET); |
2671 | 0 | if (code != 0) |
2672 | 0 | return_error(gs_error_ioerror); |
2673 | | |
2674 | 0 | if (pdev->OmitID) |
2675 | 0 | gs_snprintf(LDict, sizeof(LDict), "\ntrailer\n<</Size %"PRId64"/Info %d 0 R/Root %d 0 R/Prev %"PRId64">>\nstartxref\r\n0\n%%%%EOF\n", |
2676 | 0 | linear_params->LastResource + 3, pdev->ResourceUsage[linear_params->Info_id].NewObjectNumber, pdev->ResourceUsage[linear_params->Catalog_id].NewObjectNumber, mainxref); |
2677 | 0 | else |
2678 | 0 | gs_snprintf(LDict, sizeof(LDict), "\ntrailer\n<</Size %"PRId64"/Info %d 0 R/Root %d 0 R/ID[%s%s]/Prev %"PRId64">>\nstartxref\r\n0\n%%%%EOF\n", |
2679 | 0 | linear_params->LastResource + 3, pdev->ResourceUsage[linear_params->Info_id].NewObjectNumber, pdev->ResourceUsage[linear_params->Catalog_id].NewObjectNumber, fileID, fileID, mainxref); |
2680 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->sfile); |
2681 | |
|
2682 | 0 | code = gp_fseek(linear_params->sfile, pdev->ResourceUsage[HintStreamObj].LinearisedOffset, SEEK_SET); |
2683 | 0 | if (code != 0) |
2684 | 0 | return_error(gs_error_ioerror); |
2685 | | |
2686 | 0 | gs_snprintf(LDict, sizeof(LDict), "%d 0 obj\n<</Length %10"PRId64"", HintStreamObj, HintLength); |
2687 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->sfile); |
2688 | 0 | gs_snprintf(LDict, sizeof(LDict), "\n/S %10"PRId64">>\nstream\n", SharedHintOffset); |
2689 | 0 | gp_fwrite(LDict, strlen(LDict), 1, linear_params->sfile); |
2690 | |
|
2691 | 0 | error: |
2692 | | #ifdef LINEAR_DEBUGGING |
2693 | | fclose(linear_params->Lin_File.file); |
2694 | | #else |
2695 | 0 | pdf_close_temp_file(pdev, &linear_params->Lin_File, code); |
2696 | 0 | #endif |
2697 | | /* FIXME free all the linearisation records */ |
2698 | |
|
2699 | 0 | if (linear_params->PageHints != NULL) { |
2700 | 0 | for (i=0;i<pdev->next_page;i++) { |
2701 | 0 | page_hint_stream_t *pagehint = &linear_params->PageHints[i]; |
2702 | |
|
2703 | 0 | if (pagehint && pagehint->SharedObjectRef) |
2704 | 0 | gs_free_object(pdev->pdf_memory, pagehint->SharedObjectRef, "Free Shared object references"); |
2705 | 0 | } |
2706 | 0 | } |
2707 | |
|
2708 | 0 | gs_free_object(pdev->pdf_memory, linear_params->PageHints, "Free Page Hint data"); |
2709 | 0 | gs_free_object(pdev->pdf_memory, linear_params->SharedHints, "Free Shared hint data"); |
2710 | |
|
2711 | 0 | return code; |
2712 | 0 | } |
2713 | | |
2714 | | int pdf_record_usage(gx_device_pdf *const pdev, int64_t resource_id, int page_num) |
2715 | 276k | { |
2716 | 276k | int i; |
2717 | 276k | void *Temp; |
2718 | 276k | pdf_linearisation_record_t *resize; |
2719 | | |
2720 | 276k | if (!pdev->Linearise) |
2721 | 276k | return 0; |
2722 | | |
2723 | 0 | if (resource_id < 0) |
2724 | 0 | return 0; |
2725 | | |
2726 | 0 | if (resource_id >= pdev->ResourceUsageSize) { |
2727 | 0 | if (pdev->ResourceUsageSize == 0) { |
2728 | 0 | pdev->ResourceUsageSize = resource_id + 1; |
2729 | 0 | pdev->ResourceUsage = gs_alloc_struct_array(pdev->pdf_memory->non_gc_memory, resource_id + 1, pdf_linearisation_record_t, |
2730 | 0 | &st_pdf_linearisation_record_element, "start resource usage array"); |
2731 | 0 | if (pdev->ResourceUsage == NULL) |
2732 | 0 | return_error(gs_error_VMerror); |
2733 | 0 | memset((char *)pdev->ResourceUsage, 0x00, (resource_id + 1) * sizeof(pdf_linearisation_record_t)); |
2734 | 0 | } else { |
2735 | 0 | resize = gs_resize_object(pdev->pdf_memory->non_gc_memory, pdev->ResourceUsage, resource_id + 1, "resize resource usage array"); |
2736 | 0 | if (resize == NULL) |
2737 | 0 | return_error(gs_error_VMerror); |
2738 | 0 | memset(&resize[pdev->ResourceUsageSize], 0x00, sizeof(pdf_linearisation_record_t) * (resource_id - pdev->ResourceUsageSize + 1)); |
2739 | 0 | pdev->ResourceUsageSize = resource_id + 1; |
2740 | 0 | pdev->ResourceUsage = resize; |
2741 | 0 | } |
2742 | 0 | } |
2743 | 0 | if (page_num > 0) { |
2744 | 0 | if (pdev->ResourceUsage[resource_id].PageUsage == 0) |
2745 | 0 | pdev->ResourceUsage[resource_id].PageUsage = page_num; |
2746 | 0 | else { |
2747 | 0 | if (pdev->ResourceUsage[resource_id].PageUsage > 1) |
2748 | 0 | pdev->ResourceUsage[resource_id].PageUsage = resource_usage_page_shared; |
2749 | 0 | else { |
2750 | | /* Should not happen, raise an error */ |
2751 | 0 | } |
2752 | 0 | } |
2753 | 0 | } else { |
2754 | 0 | if (pdev->ResourceUsage[resource_id].PageUsage != 0) |
2755 | | /* Should not happen, raise an error */ |
2756 | 0 | ; |
2757 | 0 | pdev->ResourceUsage[resource_id].PageUsage = page_num; |
2758 | 0 | } |
2759 | | |
2760 | | /* First check we haven't already recorded this Resource being used by this page */ |
2761 | 0 | if (pdev->ResourceUsage[resource_id].NumPagesUsing != 0) { |
2762 | 0 | for (i=0;i < pdev->ResourceUsage[resource_id].NumPagesUsing; i++) { |
2763 | 0 | if (pdev->ResourceUsage[resource_id].PageList[i] == page_num) |
2764 | 0 | return 0; |
2765 | 0 | } |
2766 | 0 | } |
2767 | 0 | Temp = gs_alloc_bytes(pdev->pdf_memory->non_gc_memory, (pdev->ResourceUsage[resource_id].NumPagesUsing + 1) * sizeof (int), "Page usage records"); |
2768 | 0 | if (Temp == NULL) |
2769 | 0 | return_error(gs_error_VMerror); |
2770 | 0 | memset((char *)Temp, 0x00, (pdev->ResourceUsage[resource_id].NumPagesUsing + 1) * sizeof (int)); |
2771 | 0 | memcpy((char *)Temp, pdev->ResourceUsage[resource_id].PageList, pdev->ResourceUsage[resource_id].NumPagesUsing * sizeof (int)); |
2772 | 0 | gs_free_object(pdev->pdf_memory->non_gc_memory, (byte *)pdev->ResourceUsage[resource_id].PageList, "Free old page usage records"); |
2773 | 0 | pdev->ResourceUsage[resource_id].PageList = (int *)Temp; |
2774 | 0 | pdev->ResourceUsage[resource_id].PageList[pdev->ResourceUsage[resource_id].NumPagesUsing] = page_num; |
2775 | 0 | pdev->ResourceUsage[resource_id].NumPagesUsing++; |
2776 | |
|
2777 | 0 | return 0; |
2778 | 0 | } |
2779 | | |
2780 | | int pdf_record_usage_by_parent(gx_device_pdf *const pdev, int64_t resource_id, int64_t parent_id) |
2781 | 159k | { |
2782 | 159k | int i; |
2783 | 159k | if (!pdev->Linearise) |
2784 | 159k | return 0; |
2785 | | |
2786 | 0 | if (pdev->ResourceUsage[parent_id].PageUsage >= 0) |
2787 | 0 | pdf_record_usage(pdev, resource_id, pdev->ResourceUsage[parent_id].PageUsage); |
2788 | 0 | else { |
2789 | 0 | for (i = 0; i < pdev->ResourceUsage[parent_id].NumPagesUsing; i++) { |
2790 | 0 | pdf_record_usage(pdev, resource_id, pdev->ResourceUsage[parent_id].PageList[i]); |
2791 | 0 | } |
2792 | 0 | } |
2793 | 0 | return 0; |
2794 | 159k | } |
2795 | | |
2796 | | /* This routine is related to the PCL interpreter. Because of the way that |
2797 | | * PCL pass through works, the PCL interpreter can shut down and free its font cache |
2798 | | * while still running. This leaves us with copies of fonts, which point to a now |
2799 | | * freed font cache. Large parts of the code which retrieve font information require |
2800 | | * that the font cache be present, and fail badly if it isn't. So we construct a |
2801 | | * font cache of our own, and when we copy fonts from the interpreter we point the |
2802 | | * copied font at this font cache, instead of the one the original font used. |
2803 | | * This allows the PCL interpreter to shut down and free its cache, thus eliminating |
2804 | | * a memory leak, while still allowing pdfwrite to retrieve the information it needs |
2805 | | * from the copied fonts. |
2806 | | * Here we need to shut down and free our font cache. |
2807 | | */ |
2808 | | static void pdf_free_pdf_font_cache(gx_device_pdf *pdev) |
2809 | 34.0k | { |
2810 | 34.0k | if (pdev->pdf_font_dir == NULL) |
2811 | 0 | return; |
2812 | 34.0k | gs_free_object(pdev->pdf_font_dir->memory, pdev->pdf_font_dir, "pdf_free_pdf_font_cache"); |
2813 | 34.0k | pdev->pdf_font_dir = NULL; |
2814 | 34.0k | } |
2815 | | |
2816 | | static int discard_dict_refs(void *client_data, const byte *key_data, uint key_size, const cos_value_t *v); |
2817 | | |
2818 | | static int discard_array_refs(gx_device_pdf *pdev, cos_object_t *pco) |
2819 | 0 | { |
2820 | 0 | int i; |
2821 | 0 | int64_t index; |
2822 | 0 | cos_array_t *pca = (cos_array_t *)pco; |
2823 | 0 | const cos_array_element_t *element = cos_array_element_first(pca); |
2824 | 0 | cos_value_t *v; |
2825 | |
|
2826 | 0 | while (element) { |
2827 | 0 | element = cos_array_element_next(element, &index, (const cos_value_t **)&v); |
2828 | 0 | if (v->value_type == COS_VALUE_OBJECT) { |
2829 | 0 | for (i=0;i<NUM_RESOURCE_TYPES;i++) { |
2830 | 0 | if (i == resourceOther) |
2831 | 0 | continue; |
2832 | 0 | if (pdf_find_resource_by_resource_id(pdev, i, v->contents.object->id)){ |
2833 | 0 | v->value_type = COS_VALUE_CONST; |
2834 | 0 | break; |
2835 | 0 | } |
2836 | 0 | if (cos_type(v->contents.object) == cos_type_array) { |
2837 | 0 | discard_array_refs(pdev, v->contents.object); |
2838 | 0 | } |
2839 | 0 | if (cos_type(v->contents.object) == cos_type_dict) { |
2840 | 0 | cos_dict_forall((const cos_dict_t *)v->contents.object, pdev, discard_dict_refs); |
2841 | 0 | } |
2842 | 0 | } |
2843 | 0 | } |
2844 | 0 | } |
2845 | 0 | return 0; |
2846 | 0 | } |
2847 | | |
2848 | | static int discard_dict_refs(void *client_data, const byte *key_data, uint key_size, const cos_value_t *v) |
2849 | 0 | { |
2850 | 0 | int i; |
2851 | 0 | gx_device_pdf *pdev = (gx_device_pdf *)client_data; |
2852 | 0 | cos_value_t *v1 = (cos_value_t *)v; |
2853 | |
|
2854 | 0 | if (v->value_type == COS_VALUE_OBJECT) { |
2855 | 0 | for (i=0;i<NUM_RESOURCE_TYPES;i++) { |
2856 | 0 | if (i == resourceOther) |
2857 | 0 | continue; |
2858 | 0 | if (pdf_find_resource_by_resource_id(pdev, i, v->contents.object->id)){ |
2859 | 0 | v1->value_type = COS_VALUE_CONST; |
2860 | 0 | break; |
2861 | 0 | } |
2862 | 0 | if (cos_type(v->contents.object) == cos_type_array) { |
2863 | 0 | discard_array_refs(pdev, v->contents.object); |
2864 | 0 | } |
2865 | 0 | if (cos_type(v->contents.object) == cos_type_dict) { |
2866 | 0 | cos_dict_forall((const cos_dict_t *)v->contents.object, pdev, discard_dict_refs); |
2867 | 0 | } |
2868 | 0 | } |
2869 | 0 | } |
2870 | 0 | return 0; |
2871 | 0 | } |
2872 | | |
2873 | | /* Close the device. */ |
2874 | | static int |
2875 | | pdf_close(gx_device * dev) |
2876 | 34.0k | { |
2877 | 34.0k | gx_device_pdf *const pdev = (gx_device_pdf *) dev; |
2878 | 34.0k | gs_memory_t *mem = pdev->pdf_memory; |
2879 | 34.0k | stream *s = NULL; |
2880 | 34.0k | gp_file *tfile = pdev->xref.file; |
2881 | 34.0k | gs_offset_t xref = 0; |
2882 | 34.0k | gs_offset_t resource_pos = 0; |
2883 | 34.0k | int64_t Catalog_id = 0, Info_id = 0, |
2884 | 34.0k | Pages_id = 0, Encrypt_id = 0; |
2885 | 34.0k | int64_t Threads_id = 0; |
2886 | 34.0k | bool partial_page = (pdev->contents_id != 0 && pdev->next_page != 0); |
2887 | 34.0k | int code = 0, code1, pagecount=0; |
2888 | 34.0k | int64_t start_section, end_section; |
2889 | 34.0k | char str[256]; |
2890 | 34.0k | pdf_linearisation_t linear_params; |
2891 | 34.0k | bool file_per_page = false; |
2892 | 34.0k | int bottom = (pdev->ResourcesBeforeUsage ? 1 : 0); |
2893 | | |
2894 | 34.0k | if (!dev->is_open) |
2895 | 0 | return_error(gs_error_undefined); |
2896 | 34.0k | dev->is_open = false; |
2897 | | |
2898 | 34.0k | if (pdev->sbstack_depth > bottom) { |
2899 | 17 | emprintf(pdev->pdf_memory, "Error closing device; open substreams detected!\n"); |
2900 | 17 | emprintf(pdev->pdf_memory, "Probably due to errors in the input. Output file is incorrect/invalid.\n"); |
2901 | 17 | } |
2902 | | |
2903 | 34.0k | while(pdev->sbstack_depth > bottom) |
2904 | | /* This is an error, and we are trying to recover, so ignore any further errors */ |
2905 | 17 | (void)pdf_exit_substream(pdev); |
2906 | | |
2907 | 34.0k | if (pdev->initial_pattern_states != NULL) { |
2908 | 0 | int pdepth = 0; |
2909 | |
|
2910 | 0 | while (pdev->initial_pattern_states[pdepth] != 0x00) { |
2911 | 0 | gs_free_object(pdev->pdf_memory, pdev->initial_pattern_states[pdepth], "Freeing dangling pattern state"); |
2912 | 0 | pdev->initial_pattern_states[pdepth] = NULL; |
2913 | 0 | pdepth++; |
2914 | 0 | } |
2915 | 0 | gs_free_object(pdev->pdf_memory->non_gc_memory, pdev->initial_pattern_states, "Freeing dangling pattern state stack"); |
2916 | 0 | pdev->initial_pattern_states = NULL; |
2917 | 0 | } |
2918 | | |
2919 | 34.0k | if (pdev->Catalog) |
2920 | 34.0k | Catalog_id = pdev->Catalog->id; |
2921 | 34.0k | if (pdev->Info) |
2922 | 34.0k | Info_id = pdev->Info->id; |
2923 | 34.0k | if (pdev->Pages) |
2924 | 34.0k | Pages_id = pdev->Pages->id; |
2925 | | |
2926 | 34.0k | memset(&linear_params, 0x00, sizeof(linear_params)); |
2927 | 34.0k | linear_params.Info_id = Info_id; |
2928 | 34.0k | linear_params.Pages_id = Pages_id; |
2929 | 34.0k | linear_params.Catalog_id = Catalog_id; |
2930 | | |
2931 | | /* |
2932 | | * If this is an EPS file, or if the file didn't end with a showpage for |
2933 | | * some other reason, or if the file has produced no marks at all, we |
2934 | | * need to tidy up a little so as not to produce illegal PDF. However, |
2935 | | * if there is at least one complete page, we discard any leftover |
2936 | | * marks. |
2937 | | */ |
2938 | 34.0k | if (pdev->next_page == 0) { |
2939 | 16.6k | file_per_page = !pdev->InOutputPage && |
2940 | 16.6k | gx_outputfile_is_separate_pages(pdev->fname, pdev->memory); |
2941 | 16.6k | if (!file_per_page) { |
2942 | 16.6k | code = pdf_open_page(pdev, PDF_IN_STREAM); |
2943 | | |
2944 | 16.6k | if (code < 0) |
2945 | 0 | return code; |
2946 | 16.6k | } |
2947 | 16.6k | } |
2948 | 34.0k | if (pdev->contents_id != 0) |
2949 | 17.6k | pdf_close_page(pdev, 1); |
2950 | | |
2951 | | /* Write the page objects. */ |
2952 | 34.0k | if (!(pdev->ForOPDFRead && pdev->ProduceDSC)) { |
2953 | 21.3k | for (pagecount = 1; pagecount <= pdev->next_page; ++pagecount) |
2954 | 12.1k | pdf_write_page(pdev, pagecount); |
2955 | 9.18k | } |
2956 | | |
2957 | 34.0k | if (pdev->PrintStatistics) |
2958 | 0 | pdf_print_resource_statistics(pdev); |
2959 | | |
2960 | | /* Write the font resources and related resources. */ |
2961 | 34.0k | code1 = pdf_write_resource_objects(pdev, resourceXObject); |
2962 | 34.0k | if (code >= 0) |
2963 | 34.0k | code = code1; |
2964 | 34.0k | code1 = pdf_free_resource_objects(pdev, resourceXObject); |
2965 | 34.0k | if (code >= 0) |
2966 | 34.0k | code = code1; |
2967 | 34.0k | code1 = pdf_free_resource_objects(pdev, resourceProperties); |
2968 | 34.0k | if (code >= 0) |
2969 | 34.0k | code = code1; |
2970 | 34.0k | code1 = pdf_write_resource_objects(pdev, resourceGroup); |
2971 | 34.0k | if (code >= 0) |
2972 | 34.0k | code = code1; |
2973 | 34.0k | code1 = pdf_free_resource_objects(pdev, resourceGroup); |
2974 | 34.0k | if (code >= 0) |
2975 | 34.0k | code = code1; |
2976 | 34.0k | code1 = pdf_write_resource_objects(pdev, resourceSoftMaskDict); |
2977 | 34.0k | if (code >= 0) |
2978 | 34.0k | code = code1; |
2979 | 34.0k | code1 = pdf_free_resource_objects(pdev, resourceSoftMaskDict); |
2980 | 34.0k | if (code >= 0) |
2981 | 34.0k | code = code1; |
2982 | | /* This was in pdf_close_document, but that made no sense, so moved here |
2983 | | * for more consistency (and ease of finding it). This code deals with |
2984 | | * emitting fonts and FontDescriptors |
2985 | | */ |
2986 | 34.0k | pdf_clean_standard_fonts(pdev); |
2987 | 34.0k | code1 = pdf_free_font_cache(pdev); |
2988 | 34.0k | if (code >= 0) |
2989 | 34.0k | code = code1; |
2990 | 34.0k | code1 = pdf_write_resource_objects(pdev, resourceCharProc); |
2991 | 34.0k | if (code >= 0) |
2992 | 34.0k | code = code1; |
2993 | 34.0k | code1 = pdf_finish_resources(pdev, resourceFont, pdf_convert_truetype_font); |
2994 | 34.0k | if (code >= 0) |
2995 | 34.0k | code = code1; |
2996 | 34.0k | code1 = pdf_finish_resources(pdev, resourceFontDescriptor, pdf_finish_FontDescriptor); |
2997 | 34.0k | if (code >= 0) |
2998 | 34.0k | code = code1; |
2999 | 34.0k | code1 = write_font_resources(pdev, &pdev->resources[resourceCIDFont]); |
3000 | 34.0k | if (code >= 0) |
3001 | 34.0k | code = code1; |
3002 | 34.0k | code1 = write_font_resources(pdev, &pdev->resources[resourceFont]); |
3003 | 34.0k | if (code >= 0) |
3004 | 34.0k | code = code1; |
3005 | 34.0k | code1 = pdf_finish_resources(pdev, resourceFontDescriptor, pdf_write_FontDescriptor); |
3006 | 34.0k | if (code >= 0) |
3007 | 34.0k | code = code1; |
3008 | | /* If required, write the Encoding for Type 3 bitmap fonts. */ |
3009 | 34.0k | code1 = pdf_write_bitmap_fonts_Encoding(pdev); |
3010 | 34.0k | if (code >= 0) |
3011 | 34.0k | code = code1; |
3012 | | |
3013 | 34.0k | code1 = pdf_write_resource_objects(pdev, resourceCMap); |
3014 | 34.0k | if (code >= 0) |
3015 | 34.0k | code = code1; |
3016 | 34.0k | code1 = pdf_free_resource_objects(pdev, resourceCMap); |
3017 | 34.0k | if (code >= 0) |
3018 | 34.0k | code = code1; |
3019 | 34.0k | if (!(pdev->ForOPDFRead && pdev->ProduceDSC)) { |
3020 | 9.18k | if (pdev->ResourcesBeforeUsage) |
3021 | 0 | pdf_reverse_resource_chain(pdev, resourcePage); |
3022 | 9.18k | code1 = pdf_write_resource_objects(pdev, resourcePage); |
3023 | 9.18k | if (code >= 0) |
3024 | 9.15k | code = code1; |
3025 | 9.18k | code1 = pdf_free_resource_objects(pdev, resourcePage); |
3026 | 9.18k | if (code >= 0) |
3027 | 9.15k | code = code1; |
3028 | 9.18k | } |
3029 | | |
3030 | | /* Create the Pages tree. */ |
3031 | 34.0k | if (!(pdev->ForOPDFRead && pdev->ProduceDSC) && pdev->strm != NULL) { |
3032 | 9.18k | pdf_open_obj(pdev, Pages_id, resourcePagesTree); |
3033 | 9.18k | pdf_record_usage(pdev, Pages_id, resource_usage_part9_structure); |
3034 | | |
3035 | 9.18k | s = pdev->strm; |
3036 | 9.18k | stream_puts(s, "<< /Type /Pages /Kids [\n"); |
3037 | | /* Omit the last page if it was incomplete. */ |
3038 | 9.18k | if (partial_page) |
3039 | 15 | --(pdev->next_page); |
3040 | 9.18k | { |
3041 | 9.18k | int i; |
3042 | | |
3043 | 21.3k | for (i = 0; i < pdev->next_page; ++i) |
3044 | 12.1k | pprinti64d1(s, "%"PRId64" 0 R\n", pdev->pages[i].Page->id); |
3045 | 9.18k | } |
3046 | 9.18k | pprintd1(s, "] /Count %d\n", pdev->next_page); |
3047 | | |
3048 | | /* If the last file was PostScript, its possible that DSC comments might be lying around |
3049 | | * and pdf_print_orientation will use that if its present. So make sure we get rid of those |
3050 | | * before considering the dominant page direction for the Pages tree. |
3051 | | */ |
3052 | 9.18k | pdev->doc_dsc_info.viewing_orientation = pdev->doc_dsc_info.orientation = -1; |
3053 | 9.18k | pdev->text_rotation.Rotate = pdf_dominant_rotation(&pdev->text_rotation); |
3054 | 9.18k | pdf_print_orientation(pdev, NULL); |
3055 | | |
3056 | 9.18k | cos_dict_elements_write(pdev->Pages, pdev); |
3057 | 9.18k | stream_puts(s, ">>\n"); |
3058 | 9.18k | pdf_end_obj(pdev, resourcePagesTree); |
3059 | | |
3060 | | /* Close outlines and articles. */ |
3061 | | |
3062 | 9.18k | if (pdev->outlines_id != 0) { |
3063 | 140 | pdf_record_usage(pdev, pdev->outlines_id, resource_usage_part9_structure); |
3064 | | /* depth > 0 is only possible for an incomplete outline tree. */ |
3065 | 145 | while (pdev->outline_depth > 0) { |
3066 | 5 | code1 = pdfmark_close_outline(pdev); |
3067 | 5 | if (code >= 0) |
3068 | 5 | code = code1; |
3069 | 5 | } |
3070 | 140 | code = pdfmark_close_outline(pdev); |
3071 | 140 | if (code >= 0) |
3072 | 140 | code = code1; |
3073 | 140 | pdf_open_obj(pdev, pdev->outlines_id, resourceOutline); |
3074 | 140 | pprintd1(s, "<< /Type /Outlines /Count %d", pdev->outlines_open); |
3075 | 140 | pprinti64d2(s, " /First %"PRId64" 0 R /Last %"PRId64" 0 R >>\n", |
3076 | 140 | pdev->outline_levels[0].first.id, |
3077 | 140 | pdev->outline_levels[0].last.id); |
3078 | 140 | pdf_end_obj(pdev, resourceOutline); |
3079 | 140 | } |
3080 | 9.18k | if (pdev->articles != 0) { |
3081 | 0 | pdf_article_t *part; |
3082 | | |
3083 | | /* Write the remaining information for each article. */ |
3084 | 0 | for (part = pdev->articles; part != 0; part = part->next) |
3085 | 0 | pdfmark_write_article(pdev, part); |
3086 | 0 | } |
3087 | | |
3088 | | /* Write named destinations. (We can't free them yet.) */ |
3089 | | |
3090 | 9.18k | if (pdev->CompatibilityLevel < 1.2) { |
3091 | 0 | if (pdev->Dests) { |
3092 | 0 | pdf_record_usage(pdev, pdev->Dests->id, resource_usage_part9_structure); |
3093 | 0 | COS_WRITE_OBJECT(pdev->Dests, pdev, resourceDests); |
3094 | 0 | } |
3095 | 9.18k | } else { |
3096 | 9.18k | if (pdev->Dests) { |
3097 | 0 | pdf_record_usage(pdev, pdev->Dests->id, resource_usage_part9_structure); |
3098 | 0 | cos_write_dict_as_ordered_array((cos_object_t *)pdev->Dests, pdev, resourceDests); |
3099 | 0 | } |
3100 | | /* Write Embedded files. (We can't free them yet.) */ |
3101 | | |
3102 | 9.18k | if (pdev->EmbeddedFiles) { |
3103 | 17 | pdf_record_usage(pdev, pdev->EmbeddedFiles->id, resource_usage_part9_structure); |
3104 | 17 | cos_write_dict_as_ordered_array((cos_object_t *)pdev->EmbeddedFiles, pdev, resourceEmbeddedFiles); |
3105 | 17 | } |
3106 | 9.18k | if (pdev->AF) { |
3107 | 0 | pdf_record_usage(pdev, pdev->AF->id, resource_usage_part9_structure); |
3108 | 0 | COS_WRITE_OBJECT(pdev->AF, pdev, resourceEmbeddedFiles); |
3109 | 0 | } |
3110 | 9.18k | } |
3111 | | |
3112 | | /* Write the PageLabel array */ |
3113 | 9.18k | pdfmark_end_pagelabels(pdev); |
3114 | 9.18k | if (pdev->PageLabels) { |
3115 | 0 | pdf_record_usage(pdev, pdev->PageLabels->id, resource_usage_part9_structure); |
3116 | 0 | COS_WRITE_OBJECT(pdev->PageLabels, pdev, resourceLabels); |
3117 | 0 | } |
3118 | | |
3119 | 9.18k | if (!pdev->OmitXMP) { |
3120 | 9.18k | bool saved = pdev->WriteObjStms; |
3121 | | |
3122 | 9.18k | if (pdev->WriteObjStms) |
3123 | 9.18k | pdev->WriteObjStms = false; |
3124 | | /* Write the document metadata. */ |
3125 | 9.18k | code1 = pdf_document_metadata(pdev); |
3126 | 9.18k | pdev->WriteObjStms = saved; |
3127 | 9.18k | if (code >= 0) |
3128 | 9.15k | code = code1; |
3129 | 9.18k | } |
3130 | | |
3131 | | /* Write the Catalog. */ |
3132 | | |
3133 | | /* |
3134 | | * The PDF specification requires Threads to be an indirect object. |
3135 | | * Write the threads now, if any. |
3136 | | */ |
3137 | 9.18k | if (pdev->articles != 0) { |
3138 | 0 | pdf_article_t *part; |
3139 | |
|
3140 | 0 | Threads_id = pdf_begin_obj(pdev, resourceThread); |
3141 | 0 | pdf_record_usage(pdev, Threads_id, resource_usage_part9_structure); |
3142 | 0 | s = pdev->strm; |
3143 | 0 | stream_puts(s, "[ "); |
3144 | 0 | while ((part = pdev->articles) != 0) { |
3145 | 0 | pdev->articles = part->next; |
3146 | 0 | pprinti64d1(s, "%"PRId64" 0 R\n", part->contents->id); |
3147 | 0 | COS_FREE(part->contents, "pdf_close(article contents)"); |
3148 | 0 | gs_free_object(mem, part, "pdf_close(article)"); |
3149 | 0 | } |
3150 | 0 | stream_puts(s, "]\n"); |
3151 | 0 | pdf_end_obj(pdev, resourceThread); |
3152 | 0 | } |
3153 | 9.18k | pdf_open_obj(pdev, Catalog_id, resourceCatalog); |
3154 | 9.18k | pdf_record_usage(pdev, Catalog_id, resource_usage_part1_structure); |
3155 | | |
3156 | 9.18k | s = pdev->strm; |
3157 | 9.18k | stream_puts(s, "<<"); |
3158 | 9.18k | pprinti64d1(s, "/Type /Catalog /Pages %"PRId64" 0 R\n", Pages_id); |
3159 | 9.18k | if (pdev->outlines_id != 0) |
3160 | 140 | pprinti64d1(s, "/Outlines %"PRId64" 0 R\n", pdev->outlines_id); |
3161 | 9.18k | if (Threads_id) { |
3162 | 0 | pprinti64d1(s, "/Threads %"PRId64" 0 R\n", Threads_id); |
3163 | 0 | pdf_record_usage(pdev, Threads_id, resource_usage_part1_structure); |
3164 | 0 | } |
3165 | 9.18k | if (pdev->CompatibilityLevel < 1.2) { |
3166 | 0 | if (pdev->Dests) |
3167 | 0 | pprinti64d1(s, "/Dests %"PRId64" 0 R\n", pdev->Dests->id); |
3168 | 9.18k | } else { |
3169 | 9.18k | if (pdev->Dests || pdev->EmbeddedFiles) { |
3170 | 17 | stream_puts(s, "/Names <<\n"); |
3171 | 17 | if (pdev->Dests) |
3172 | 0 | pprinti64d1(s, "/Dests <</Kids [%"PRId64" 0 R]>>\n", pdev->Dests->id); |
3173 | 17 | if (pdev->EmbeddedFiles) |
3174 | 17 | pprinti64d1(s, "/EmbeddedFiles << /Kids [%"PRId64" 0 R]>>\n", pdev->EmbeddedFiles->id); |
3175 | 17 | stream_puts(s, ">>\n"); |
3176 | 17 | } |
3177 | 9.18k | if (pdev->AF) |
3178 | 0 | if (!cos_dict_find_c_key(pdev->Catalog, "/AF")) |
3179 | 0 | pprinti64d1(s, "/AF %"PRId64" 0 R\n", pdev->AF->id); |
3180 | 9.18k | } |
3181 | 9.18k | if (pdev->PageLabels) |
3182 | 0 | pprinti64d1(s, "/PageLabels << /Nums %"PRId64" 0 R >>\n", |
3183 | 0 | pdev->PageLabels->id); |
3184 | 9.18k | cos_dict_elements_write(pdev->Catalog, pdev); |
3185 | 9.18k | stream_puts(s, ">>\n"); |
3186 | 9.18k | pdf_end_obj(pdev, resourceCatalog); |
3187 | 9.18k | if (pdev->Dests) { |
3188 | 0 | COS_FREE(pdev->Dests, "pdf_close(Dests)"); |
3189 | 0 | pdev->Dests = 0; |
3190 | 0 | } |
3191 | 9.18k | if (pdev->EmbeddedFiles) { |
3192 | 17 | COS_FREE(pdev->EmbeddedFiles, "pdf_close(EmbeddFiles)"); |
3193 | 17 | pdev->EmbeddedFiles = 0; |
3194 | 17 | } |
3195 | 9.18k | if (pdev->AF) { |
3196 | 0 | COS_FREE(pdev->AF, "pdf_close(AF)"); |
3197 | 0 | pdev->AF = 0; |
3198 | 0 | } |
3199 | 9.18k | if (pdev->PageLabels) { |
3200 | 0 | COS_FREE(pdev->PageLabels, "pdf_close(PageLabels)"); |
3201 | 0 | pdev->PageLabels = 0; |
3202 | 0 | pdev->PageLabels_current_label = 0; |
3203 | 0 | } |
3204 | | |
3205 | | /* Prevent writing special named objects twice. */ |
3206 | | |
3207 | 9.18k | pdev->Catalog->id = 0; |
3208 | | /*pdev->Info->id = 0;*/ /* Info should get written */ |
3209 | 9.18k | pdf_record_usage(pdev, pdev->Info->id, resource_usage_part9_structure); |
3210 | | |
3211 | 24.8k | } else { |
3212 | 24.8k | if (pdev->Info != NULL) |
3213 | 24.8k | pdev->Info->id = 0; /* Don't write Info dict for DSC PostScript */ |
3214 | 24.8k | } |
3215 | | /* |
3216 | | * Write the definitions of the named objects. |
3217 | | * Note that this includes Form XObjects created by BP/EP, named PS |
3218 | | * XObjects, and images named by NI. |
3219 | | */ |
3220 | | |
3221 | 34.0k | if(pdev->local_named_objects != NULL) { |
3222 | 34.0k | do { |
3223 | 34.0k | cos_dict_objects_write(pdev->local_named_objects, pdev); |
3224 | 34.0k | } while (pdf_pop_namespace(pdev) >= 0); |
3225 | 34.0k | } |
3226 | 34.0k | if (pdev->global_named_objects != NULL) |
3227 | 34.0k | cos_dict_objects_write(pdev->global_named_objects, pdev); |
3228 | | |
3229 | 34.0k | if (pdev->ForOPDFRead && pdev->ProduceDSC) { |
3230 | 24.8k | int pages; |
3231 | | |
3232 | 82.8k | for (pages = 0; pages <= pdev->next_page; ++pages) |
3233 | 58.0k | ; |
3234 | | |
3235 | 24.8k | code1 = ps2write_dsc_header(pdev, pages - 1); |
3236 | 24.8k | if (code >= 0) |
3237 | 24.8k | code = code1; |
3238 | 24.8k | } |
3239 | | |
3240 | | /* Copy the resources into the main file. */ |
3241 | | |
3242 | 34.0k | if (pdev->WriteObjStms) { |
3243 | 9.18k | FlushObjStm(pdev); |
3244 | 9.18k | pdev->WriteObjStms = false; |
3245 | 9.18k | } |
3246 | 34.0k | if (pdev->strm != NULL) { |
3247 | 34.0k | s = pdev->strm; |
3248 | 34.0k | resource_pos = stell(s); |
3249 | 34.0k | sflush(pdev->asides.strm); |
3250 | 34.0k | { |
3251 | 34.0k | gp_file *rfile = pdev->asides.file; |
3252 | 34.0k | int64_t res_end = gp_ftell(rfile); |
3253 | | |
3254 | 34.0k | gp_fseek(rfile, 0L, SEEK_SET); |
3255 | 34.0k | code1 = pdf_copy_data(s, rfile, res_end, NULL); |
3256 | 34.0k | if (code >= 0) |
3257 | 33.8k | code = code1; |
3258 | 34.0k | } |
3259 | 34.0k | } |
3260 | | |
3261 | 34.0k | if (pdev->ForOPDFRead && pdev->ProduceDSC && s != NULL) { |
3262 | 24.8k | int j; |
3263 | | |
3264 | 24.8k | pagecount = 1; |
3265 | | |
3266 | | /* All resources and procsets written, end the prolog */ |
3267 | 24.8k | stream_puts(pdev->strm, "%%EndProlog\n"); |
3268 | | |
3269 | 24.8k | if (pdev->params.PSDocOptions.data) { |
3270 | 0 | int i; |
3271 | 0 | char *p = (char *)pdev->params.PSDocOptions.data; |
3272 | |
|
3273 | 0 | stream_puts(pdev->strm, "%%BeginSetup\n"); |
3274 | 0 | for (i=0;i<pdev->params.PSDocOptions.size;i++) |
3275 | 0 | stream_putc(s, *p++); |
3276 | 0 | stream_puts(pdev->strm, "\n"); |
3277 | 0 | stream_puts(pdev->strm, "\n%%EndSetup\n"); |
3278 | 0 | } |
3279 | | |
3280 | 24.8k | if (pdev->ResourcesBeforeUsage) |
3281 | 24.8k | pdf_reverse_resource_chain(pdev, resourcePage); |
3282 | 422k | for (j = 0; j < NUM_RESOURCE_CHAINS && code >= 0; ++j) { |
3283 | 397k | pdf_resource_t *pres = pdev->resources[resourcePage].chains[j]; |
3284 | | |
3285 | 430k | for (; pres != 0; pres = pres->next) |
3286 | 33.1k | if ((!pres->named || pdev->ForOPDFRead) |
3287 | 33.1k | && !pres->object->written) { |
3288 | 33.1k | pdf_page_t *page = &pdev->pages[pagecount - 1]; |
3289 | | |
3290 | 33.1k | pprintd2(pdev->strm, "%%%%Page: %d %d\n", |
3291 | 33.1k | pagecount, pagecount); |
3292 | 33.1k | if (!pdev->Eps2Write) |
3293 | 20.1k | pprintd2(pdev->strm, "%%%%PageBoundingBox: 0 0 %d %d\n", (int)page->MediaBox.x, (int)page->MediaBox.y); |
3294 | 33.1k | stream_puts(pdev->strm, "%%BeginPageSetup\n"); |
3295 | | |
3296 | 33.1k | if (pdev->params.PSPageOptions.size) { |
3297 | 0 | if (pdev->params.PSPageOptionsWrap || (pagecount - 1) < pdev->params.PSPageOptions.size) { |
3298 | 0 | int i, index = (pagecount - 1) % pdev->params.PSPageOptions.size; |
3299 | 0 | char *p = (char *)pdev->params.PSPageOptions.data[index].data; |
3300 | |
|
3301 | 0 | for (i=0;i<pdev->params.PSPageOptions.data[index].size;i++) |
3302 | 0 | stream_putc(s, *p++); |
3303 | 0 | stream_puts(pdev->strm, "\n"); |
3304 | 0 | } |
3305 | 0 | } |
3306 | | |
3307 | 33.1k | pdf_write_page(pdev, pagecount++); |
3308 | | |
3309 | 33.1k | stream_puts(pdev->strm, "%%EndPageSetup\n"); |
3310 | 33.1k | pprinti64d1(pdev->strm, "%"PRId64" 0 obj\n", pres->object->id); |
3311 | 33.1k | code = cos_write(pres->object, pdev, pres->object->id); |
3312 | 33.1k | stream_puts(pdev->strm, "endobj\n"); |
3313 | 33.1k | pres->object->written = true; |
3314 | 33.1k | stream_puts(pdev->strm, "%%PageTrailer\n"); |
3315 | 33.1k | } |
3316 | 397k | } |
3317 | 24.8k | code1 = pdf_free_resource_objects(pdev, resourcePage); |
3318 | 24.8k | if (code >= 0) |
3319 | 24.8k | code = code1; |
3320 | 24.8k | stream_puts(pdev->strm, "%%Trailer\n"); |
3321 | 24.8k | stream_puts(pdev->strm, "end\n"); |
3322 | 24.8k | stream_puts(pdev->strm, "%%EOF\n"); |
3323 | 24.8k | } |
3324 | | |
3325 | 34.0k | if (pdev->params.PSPageOptions.size) { |
3326 | 0 | int ix; |
3327 | |
|
3328 | 0 | for (ix = 0; ix < pdev->params.PSPageOptions.size;ix++) |
3329 | 0 | gs_free_object(mem->non_gc_memory, (byte *)pdev->params.PSPageOptions.data[ix].data, "freeing old string array copy"); |
3330 | 0 | gs_free_object(mem->non_gc_memory, (byte *)pdev->params.PSPageOptions.data, "freeing old string array"); |
3331 | 0 | } |
3332 | | |
3333 | 34.0k | if (pdev->Linearise) { |
3334 | 0 | linear_params.LastResource = pdev->next_id - 1; |
3335 | 0 | linear_params.Offsets = (gs_offset_t *)gs_alloc_bytes(pdev->pdf_memory, pdev->next_id * sizeof(gs_offset_t), "temp xref storage"); |
3336 | 0 | if (linear_params.Offsets == NULL) { |
3337 | 0 | code = gs_error_VMerror; |
3338 | 0 | goto error_cleanup; |
3339 | 0 | } |
3340 | 0 | memset(linear_params.Offsets, 0x00, linear_params.LastResource * sizeof(gs_offset_t)); |
3341 | 0 | } |
3342 | | |
3343 | 34.0k | if (!(pdev->ForOPDFRead && pdev->ProduceDSC) && pdev->strm != NULL) { |
3344 | | /* Write Encrypt. */ |
3345 | 9.18k | if (pdev->OwnerPassword.size > 0) { |
3346 | 0 | Encrypt_id = pdf_obj_ref(pdev); |
3347 | |
|
3348 | 0 | pdf_open_obj(pdev, Encrypt_id, resourceEncrypt); |
3349 | 0 | s = pdev->strm; |
3350 | 0 | stream_puts(s, "<<"); |
3351 | 0 | stream_puts(s, "/Filter /Standard "); |
3352 | 0 | pprintld1(s, "/V %ld ", pdev->EncryptionV); |
3353 | 0 | pprintld1(s, "/Length %ld ", pdev->KeyLength); |
3354 | 0 | pprintld1(s, "/R %ld ", pdev->EncryptionR); |
3355 | 0 | pprintd1(s, "/P %d ", pdev->Permissions); |
3356 | 0 | stream_puts(s, "/O "); |
3357 | 0 | pdf_put_string(pdev, pdev->EncryptionO, sizeof(pdev->EncryptionO)); |
3358 | 0 | stream_puts(s, "\n/U "); |
3359 | 0 | pdf_put_string(pdev, pdev->EncryptionU, sizeof(pdev->EncryptionU)); |
3360 | 0 | stream_puts(s, ">>\n"); |
3361 | 0 | pdf_end_obj(pdev, resourceEncrypt); |
3362 | 0 | s = pdev->strm; |
3363 | 0 | } |
3364 | | |
3365 | | /* Write the cross-reference section. */ |
3366 | | |
3367 | 9.18k | start_section = pdev->FirstObjectNumber; |
3368 | 9.18k | end_section = find_end_xref_section(pdev, tfile, start_section, resource_pos); |
3369 | | |
3370 | 9.18k | xref = pdf_stell(pdev) - pdev->OPDFRead_procset_length; |
3371 | 9.18k | if (pdev->Linearise) |
3372 | 0 | linear_params.xref = xref; |
3373 | | |
3374 | 9.18k | if (!pdev->WriteXRefStm) { |
3375 | 0 | if (pdev->FirstObjectNumber == 1) { |
3376 | 0 | gs_snprintf(str, sizeof(str), "xref\n0 %"PRId64"\n0000000000 65535 f \n", |
3377 | 0 | end_section); |
3378 | 0 | stream_puts(s, str); |
3379 | 0 | } |
3380 | 0 | else { |
3381 | 0 | gs_snprintf(str, sizeof(str), "xref\n0 1\n0000000000 65535 f \n%"PRId64" %"PRId64"\n", |
3382 | 0 | start_section, |
3383 | 0 | end_section - start_section); |
3384 | 0 | stream_puts(s, str); |
3385 | 0 | } |
3386 | |
|
3387 | 0 | do { |
3388 | 0 | code = write_xref_section(pdev, tfile, start_section, end_section, resource_pos, linear_params.Offsets); |
3389 | 0 | if (code < 0) |
3390 | 0 | goto error_cleanup; |
3391 | | |
3392 | 0 | if (end_section >= pdev->next_id) |
3393 | 0 | break; |
3394 | 0 | do { |
3395 | 0 | start_section = end_section + 1; |
3396 | 0 | end_section = find_end_xref_section(pdev, tfile, start_section, resource_pos); |
3397 | 0 | if (end_section < 0) |
3398 | 0 | return end_section; |
3399 | 0 | } while (start_section == end_section); |
3400 | 0 | gs_snprintf(str, sizeof(str), "%"PRId64" %"PRId64"\n", start_section, end_section - start_section); |
3401 | 0 | stream_puts(s, str); |
3402 | 0 | } while (1); |
3403 | | |
3404 | | /* Write the trailer. */ |
3405 | 0 | if (!pdev->Linearise) { |
3406 | 0 | char xref_str[32]; |
3407 | 0 | stream_puts(s, "trailer\n"); |
3408 | 0 | pprinti64d3(s, "<< /Size %"PRId64" /Root %"PRId64" 0 R /Info %"PRId64" 0 R\n", |
3409 | 0 | pdev->next_id, Catalog_id, Info_id); |
3410 | 0 | if (!pdev->OmitID) { |
3411 | 0 | stream_puts(s, "/ID ["); |
3412 | 0 | psdf_write_string(pdev->strm, pdev->fileID, sizeof(pdev->fileID), 0); |
3413 | 0 | psdf_write_string(pdev->strm, pdev->fileID, sizeof(pdev->fileID), 0); |
3414 | 0 | stream_puts(s, "]\n"); |
3415 | 0 | } |
3416 | 0 | if (pdev->OwnerPassword.size > 0) { |
3417 | 0 | pprinti64d1(s, "/Encrypt %"PRId64" 0 R ", Encrypt_id); |
3418 | 0 | } |
3419 | 0 | stream_puts(s, ">>\n"); |
3420 | 0 | gs_snprintf(xref_str, sizeof(xref_str), "startxref\n%"PRId64"\n%%%%EOF\n", xref); |
3421 | 0 | stream_puts(s, xref_str); |
3422 | 0 | } |
3423 | 9.18k | } else { |
3424 | 9.18k | char xref_str[32]; |
3425 | 9.18k | gs_offset_t maxoffs = xref, offs_bytes = 0, length = 0; |
3426 | 9.18k | int i; |
3427 | | |
3428 | 27.9k | while(maxoffs > 0) { |
3429 | 18.7k | maxoffs = maxoffs >> 8; |
3430 | 18.7k | offs_bytes++; |
3431 | 18.7k | } |
3432 | | |
3433 | 9.18k | gs_snprintf(str, sizeof(str), "%"PRId64" 0 obj\n<<\n/Type /XRef\n", (int64_t)pdev->next_id); |
3434 | 9.18k | stream_puts(s, str); |
3435 | 9.18k | gs_snprintf(str, sizeof(str), "/Size %"PRId64"\n", (int64_t)(pdev->next_id + 1)); |
3436 | 9.18k | stream_puts(s, str); |
3437 | | |
3438 | 9.18k | pprinti64d2(s, "/Root %"PRId64" 0 R /Info %"PRId64" 0 R\n", Catalog_id, Info_id); |
3439 | 9.18k | if (!pdev->OmitID) { |
3440 | 9.18k | stream_puts(s, "/ID ["); |
3441 | 9.18k | psdf_write_string(pdev->strm, pdev->fileID, sizeof(pdev->fileID), 0); |
3442 | 9.18k | psdf_write_string(pdev->strm, pdev->fileID, sizeof(pdev->fileID), 0); |
3443 | 9.18k | stream_puts(s, "]\n"); |
3444 | 9.18k | } |
3445 | 9.18k | if (pdev->OwnerPassword.size > 0) { |
3446 | 0 | pprinti64d1(s, "/Encrypt %"PRId64" 0 R ", Encrypt_id); |
3447 | 0 | } |
3448 | | |
3449 | 9.18k | end_section = find_end_xref_section(pdev, tfile, start_section, resource_pos); |
3450 | 9.18k | if (end_section == pdev->next_id) |
3451 | 9.15k | end_section++; |
3452 | | |
3453 | 9.18k | length += (end_section - start_section) * (offs_bytes + 3); |
3454 | | |
3455 | 9.18k | if (pdev->FirstObjectNumber == 1) { |
3456 | 9.18k | gs_snprintf(str, sizeof(str), "/Index [0 %"PRId64" ", end_section); |
3457 | 9.18k | stream_puts(s, str); |
3458 | 9.18k | } else { |
3459 | 0 | gs_snprintf(str, sizeof(str), "/Index [0 1 %"PRId64" %"PRId64" ", pdev->FirstObjectNumber, end_section); |
3460 | 0 | stream_puts(s, str); |
3461 | 0 | } |
3462 | | |
3463 | 9.20k | do { |
3464 | 9.20k | if (end_section >= pdev->next_id) |
3465 | 9.18k | break; |
3466 | | |
3467 | 30 | do { |
3468 | 30 | start_section = end_section + 1; |
3469 | 30 | end_section = find_end_xref_section(pdev, tfile, start_section, resource_pos); |
3470 | 30 | if (end_section < 0) |
3471 | 0 | return end_section; |
3472 | 30 | } while (start_section == end_section); |
3473 | 28 | gs_snprintf(str, sizeof(str), "%"PRId64" %"PRId64" ", start_section, end_section - start_section); |
3474 | 28 | stream_puts(s, str); |
3475 | 28 | }while (1); |
3476 | | |
3477 | 9.18k | { |
3478 | 9.18k | pdf_temp_file_t xref_temp; |
3479 | 9.18k | stream *xs = NULL, *xs1; |
3480 | 9.18k | char xrefbuf[256]; |
3481 | 9.18k | int read = 0; |
3482 | 9.18k | stream_state *st = NULL; |
3483 | 9.18k | code = pdf_open_temp_stream(pdev, &xref_temp); |
3484 | 9.18k | if (code < 0) |
3485 | 0 | return code; |
3486 | | |
3487 | 9.18k | xs1 = xref_temp.strm; |
3488 | 9.18k | if (pdev->CompressStreams) { |
3489 | 9.18k | if (pdev->UseBrotli) { |
3490 | 0 | st = s_alloc_state(pdev->pdf_memory, s_brotliE_template.stype, "write_xref_strm"); |
3491 | 0 | if (st == NULL) |
3492 | 0 | return_error(gs_error_VMerror); |
3493 | 0 | s_brotliE_template.set_defaults (st); |
3494 | 0 | xs = s_add_filter(&xref_temp.strm, &s_brotliE_template, st, pdev->pdf_memory); |
3495 | 0 | if (xs == NULL) |
3496 | 0 | xs = xref_temp.strm; |
3497 | 9.18k | } else { |
3498 | 9.18k | st = s_alloc_state(pdev->pdf_memory, s_zlibE_template.stype, "write_xref_strm"); |
3499 | 9.18k | if (st == NULL) |
3500 | 0 | return_error(gs_error_VMerror); |
3501 | 9.18k | s_zlibE_template.set_defaults (st); |
3502 | 9.18k | xs = s_add_filter(&xref_temp.strm, &s_zlibE_template, st, pdev->pdf_memory); |
3503 | 9.18k | if (xs == NULL) |
3504 | 0 | xs = xref_temp.strm; |
3505 | 9.18k | } |
3506 | 9.18k | } else |
3507 | 0 | xs = xref_temp.strm; |
3508 | | |
3509 | | /* write the free entry */ |
3510 | 9.18k | stream_putc(xs, 0x00); |
3511 | 27.9k | for (i = 0; i < offs_bytes; i++) |
3512 | 18.7k | stream_putc(xs, 0x00); |
3513 | 9.18k | stream_putc(xs, 0xFF); |
3514 | 9.18k | stream_putc(xs, 0xFF); |
3515 | | |
3516 | 9.18k | start_section = pdev->FirstObjectNumber; |
3517 | 9.21k | do { |
3518 | 9.21k | end_section = find_end_xref_section(pdev, tfile, start_section, resource_pos); |
3519 | 9.21k | if (end_section < 0) |
3520 | 0 | { |
3521 | 0 | gs_free_object(pdev->memory, st, "write_xref_strm"); |
3522 | 0 | return end_section; |
3523 | 0 | } |
3524 | | |
3525 | 9.21k | code = write_xrefstm_section(pdev, tfile, start_section, end_section, resource_pos, offs_bytes, xs); |
3526 | 9.21k | if (code < 0) |
3527 | 0 | { |
3528 | 0 | gs_free_object(pdev->memory, st, "write_xref_strm"); |
3529 | 0 | goto error_cleanup; |
3530 | 0 | } |
3531 | | |
3532 | 9.21k | start_section = end_section + 1; |
3533 | 9.21k | } while (end_section < pdev->next_id); |
3534 | | |
3535 | | /* write the entry for this XRefStm */ |
3536 | 9.18k | stream_putc(xs, 0x01); |
3537 | 27.9k | for (i = 1; i <= offs_bytes; i++) |
3538 | 18.7k | stream_putc(xs, (xref >> ((offs_bytes - i) * 8)) & 0xFF); |
3539 | 9.18k | stream_putc(xs, 0x00); |
3540 | 9.18k | stream_putc(xs, 0x00); |
3541 | | |
3542 | 9.18k | sflush(xs); |
3543 | 9.18k | if (pdev->CompressStreams) |
3544 | 9.18k | s_close_filters(&xref_temp.strm, xs1); |
3545 | 9.18k | sflush(xref_temp.strm); |
3546 | 9.18k | length = stell(xref_temp.strm); |
3547 | | |
3548 | 9.18k | if (pdev->CompressStreams) { |
3549 | 9.18k | if (pdev->UseBrotli) |
3550 | 0 | gs_snprintf(str, sizeof(str), "]\n/W [1 %"PRId64" 2]\n/Filter /BrotliDecode/Length %"PRId64"\n>>\nstream\n", offs_bytes, length); |
3551 | 9.18k | else |
3552 | 9.18k | gs_snprintf(str, sizeof(str), "]\n/W [1 %"PRId64" 2]\n/Filter /FlateDecode/Length %"PRId64"\n>>\nstream\n", offs_bytes, length); |
3553 | 9.18k | } |
3554 | 0 | else |
3555 | 0 | gs_snprintf(str, sizeof(str), "]\n/W [1 %"PRId64" 2]\n/Length %"PRId64"\n>>\nstream\n", offs_bytes, length); |
3556 | 9.18k | stream_puts(s, str); |
3557 | | |
3558 | 9.18k | code = gp_fseek(xref_temp.file , 0, SEEK_SET); |
3559 | 9.18k | if (code < 0) |
3560 | 0 | return code; |
3561 | 9.51k | do { |
3562 | 9.51k | read = gp_fread(&xrefbuf, 1, 256, xref_temp.file); |
3563 | 9.51k | if (read < 0) |
3564 | 0 | { |
3565 | 0 | gs_free_object(pdev->memory, st, "write_xref_strm"); |
3566 | 0 | return_error(gs_error_VMerror); |
3567 | 0 | } |
3568 | 9.51k | stream_write(s, xrefbuf, read); |
3569 | 9.51k | } while (read == 256); |
3570 | | |
3571 | 9.18k | pdf_close_temp_file(pdev, &xref_temp, 0); |
3572 | 9.18k | } |
3573 | | |
3574 | 0 | gs_snprintf(str, sizeof(str), "\nendstream\nendobj\n"); |
3575 | 9.18k | stream_puts(s, str); |
3576 | 9.18k | pdev->next_id++; |
3577 | | |
3578 | 9.18k | gs_snprintf(str, sizeof(xref_str), "startxref\n%"PRId64"\n%%%%EOF\n", xref); |
3579 | 9.18k | stream_puts(s, str); |
3580 | 9.18k | } |
3581 | 9.18k | } |
3582 | | |
3583 | 34.0k | if (pdev->Linearise && pdev->strm != NULL) { |
3584 | 0 | int i; |
3585 | |
|
3586 | 0 | code = pdf_linearise(pdev, &linear_params); |
3587 | 0 | for (i=0; i<pdev->ResourceUsageSize; i++) { |
3588 | 0 | if (pdev->ResourceUsage[i].PageList) |
3589 | 0 | gs_free_object(pdev->pdf_memory->non_gc_memory, pdev->ResourceUsage[i].PageList, "Free linearisation Page Usage list records"); |
3590 | 0 | } |
3591 | 0 | gs_free_object(pdev->pdf_memory->non_gc_memory, pdev->ResourceUsage, "Free linearisation resource usage records"); |
3592 | 0 | } |
3593 | | |
3594 | 34.0k | error_cleanup: |
3595 | | /* Require special handling for Fonts, ColorSpace and Pattern resources |
3596 | | * These are tracked in pdev->last_resource, and are complex structures which may |
3597 | | * contain other memory allocations. All other resource types can be simply dicarded |
3598 | | * as above. |
3599 | | */ |
3600 | | |
3601 | | /* Memory management of resources in pdfwrite is bizarre and complex. Originally there was no means |
3602 | | * to free any resources on completion, pdfwrite simply relied on the garbage collector to clean up |
3603 | | * and all the resource objects are GC-visible. However, this doesn't work well when the interpreter |
3604 | | * does not use GC, ie PCL or XPS, and even when GC is available, the time taken to clean up the |
3605 | | * (sometimes enormous numbers) of objects can be surprisingly significant. So code was added above |
3606 | | * to handle the simple cases using pdf_free_resource_object(), and below to handle the more complex |
3607 | | * situations. |
3608 | | * The way this works is that for each resource type we free the 'object', if the object is itself |
3609 | | * a 'cos' object (array, dictionary) then we free each of its members. However, if any of the objects |
3610 | | * have an ID which is not zero, then we don't free them (this is true only for contents, all the |
3611 | | * objects of a given type are freed regardless of whether their ID is 0). These are taken to be |
3612 | | * references to resource objects of a type which has not yet been freed, and will be freed when |
3613 | | * that resource type is freed. For the simple resources, which is most of them, this works well. |
3614 | | * |
3615 | | * However, there are complications; colour spaces and functions can contain cos objects |
3616 | | * whose members pointers to other objects of the same resource type (eg a type 3 stitching function |
3617 | | * can point to an array of type 0 functions). We can't afford to have these free the object, because |
3618 | | * the resource chain is still pointing at it, and will try to free it again. The same is also true if |
3619 | | * we should encounter the object which is referenced before we find the reference. So for these cases |
3620 | | * we first scan the members of each resource (if present) to see if they are references to another |
3621 | | * resource of the same type. If they are we simply 'corrupt' the reference by changing the object type. |
3622 | | * The resource has already been written, and the reference will free the memory, so this is safe to do. |
3623 | | * |
3624 | | * The final case is 'resourceOther' which can contain pretty much anything. In particular it can contain |
3625 | | * Colorant dictionaries for colour spaces, but technically it could contain cos objects pointing at any |
3626 | | * resource type. So far I have only found this to be possible with colorspace and function resources, but |
3627 | | * the routines 'discard_array_refs' and 'discard_dict_refs' actually check for *all* the resource types except |
3628 | | * resourceOther. We can't free the resourceOther types because they cvan contain resources that are being used |
3629 | | * by resources, eg the spot colour names for /Separation and DeviceN spaces. |
3630 | | * |
3631 | | * There is a 'gotcha' here; previously we used to free the 'resourceOther' resources *before* calling |
3632 | | * pdf_document_metadata(), now we call it after. The problem is that the metadata is stored as a resourceOther |
3633 | | * resource *and* referenced from the Catalog dictionary, which is a 'global named resource'. We free global |
3634 | | * named resoruces as the absolute last action. Previously because we had free resourceOther resources before |
3635 | | * creating the new reference to the Metadata, the fact that it was freed by the action of releasing the |
3636 | | * global named resources wasn't a problem, now it is. If we free the metadata as a 'resourceOther' then when |
3637 | | * we try to free it as a global named resource we will run into trouble again. So pdf_document_metadata() has been |
3638 | | * specifically altered to remove the reference to the metadata from the resourceOther resource chain immediately |
3639 | | * after it has been created. Ick..... |
3640 | | */ |
3641 | 34.0k | { |
3642 | 34.0k | int j; |
3643 | | |
3644 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3645 | 544k | pdf_resource_t *pres = pdev->resources[resourceOther].chains[j]; |
3646 | | |
3647 | 588k | for (; pres != 0;) { |
3648 | 44.0k | if (pres->object) { |
3649 | 44.0k | if (cos_type(pres->object) == cos_type_array) { |
3650 | 0 | discard_array_refs(pdev, pres->object); |
3651 | 0 | } |
3652 | 44.0k | if (cos_type(pres->object) == cos_type_dict) { |
3653 | 0 | cos_dict_forall((const cos_dict_t *)pres->object, pdev, discard_dict_refs); |
3654 | 0 | } |
3655 | 44.0k | } |
3656 | 44.0k | pres = pres->next; |
3657 | 44.0k | } |
3658 | 544k | } |
3659 | 34.0k | } |
3660 | | |
3661 | 34.0k | { |
3662 | | /* PDF font records and all their associated contents need to be |
3663 | | * freed by means other than the garbage collector, or the memory |
3664 | | * usage will increase with languages other than PostScript. In addition |
3665 | | * debug builds of non-PS languages take a long time to close down |
3666 | | * due to reporting the dangling memory allocations. |
3667 | | */ |
3668 | 34.0k | int j; |
3669 | | |
3670 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3671 | 544k | pdf_resource_t *pres = pdev->resources[resourceFont].chains[j]; |
3672 | | |
3673 | 576k | for (; pres != 0;) { |
3674 | 32.5k | pdf_font_resource_t *pdfont = (pdf_font_resource_t *)pres; |
3675 | | |
3676 | 32.5k | font_resource_free(pdev, pdfont); |
3677 | 32.5k | pres = pres->next; |
3678 | 32.5k | } |
3679 | 544k | } |
3680 | 34.0k | } |
3681 | | |
3682 | 34.0k | { |
3683 | 34.0k | int j; |
3684 | | |
3685 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3686 | 544k | pdf_resource_t *pres = pdev->resources[resourceCIDFont].chains[j]; |
3687 | | |
3688 | 544k | for (; pres != 0;) { |
3689 | 324 | pdf_font_resource_t *pdfont = (pdf_font_resource_t *)pres; |
3690 | | |
3691 | 324 | font_resource_free(pdev, pdfont); |
3692 | 324 | pres = pres->next; |
3693 | 324 | } |
3694 | 544k | } |
3695 | 34.0k | } |
3696 | | |
3697 | 34.0k | { |
3698 | 34.0k | int j; |
3699 | | |
3700 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3701 | 544k | pdf_resource_t *pres = pdev->resources[resourceFontDescriptor].chains[j]; |
3702 | 570k | for (; pres != 0;) { |
3703 | 25.9k | pdf_font_descriptor_free(pdev, pres); |
3704 | 25.9k | pres = pres->next; |
3705 | 25.9k | } |
3706 | 544k | } |
3707 | 34.0k | } |
3708 | | |
3709 | 34.0k | { |
3710 | 34.0k | int j; |
3711 | | |
3712 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3713 | 544k | pdf_resource_t *pres = pdev->resources[resourceCharProc].chains[j]; |
3714 | | |
3715 | 687k | for (; pres != 0;) { |
3716 | 143k | if (pres->object) { |
3717 | 143k | cos_free(pres->object, "free CharProc resource"); |
3718 | 143k | pres->object = 0; |
3719 | 143k | } |
3720 | 143k | pres = pres->next; |
3721 | 143k | } |
3722 | 544k | } |
3723 | 34.0k | } |
3724 | | |
3725 | 34.0k | { |
3726 | 34.0k | int j; |
3727 | | |
3728 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3729 | 544k | pdf_resource_t *pres = pdev->resources[resourceExtGState].chains[j]; |
3730 | | |
3731 | 558k | for (; pres != 0;) { |
3732 | 13.9k | if (pres->object) { |
3733 | 13.9k | cos_release(pres->object, "release ExtGState object"); |
3734 | 13.9k | gs_free_object(pdev->pdf_memory, pres->object, "free ExtGState"); |
3735 | 13.9k | pres->object = 0; |
3736 | 13.9k | } |
3737 | 13.9k | pres = pres->next; |
3738 | 13.9k | } |
3739 | 544k | } |
3740 | 34.0k | } |
3741 | | |
3742 | 34.0k | { |
3743 | 34.0k | int j; |
3744 | | |
3745 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3746 | 544k | pdf_resource_t *pres = pdev->resources[resourcePattern].chains[j]; |
3747 | | |
3748 | 555k | for (; pres != 0;) { |
3749 | 11.5k | if (pres->object) { |
3750 | 10.6k | cos_release(pres->object, "free pattern dict"); |
3751 | 10.6k | gs_free_object(pdev->pdf_memory, pres->object, "free pattern resources"); |
3752 | 10.6k | pres->object = 0; |
3753 | 10.6k | } |
3754 | 11.5k | pres = pres->next; |
3755 | 11.5k | } |
3756 | 544k | } |
3757 | 34.0k | } |
3758 | | |
3759 | 34.0k | { |
3760 | 34.0k | int j; |
3761 | | |
3762 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3763 | 544k | pdf_resource_t *pres = pdev->resources[resourceShading].chains[j]; |
3764 | | |
3765 | 545k | for (; pres != 0;) { |
3766 | 683 | if (pres->object) { |
3767 | 683 | cos_release(pres->object, "free Shading dict"); |
3768 | 683 | gs_free_object(pdev->pdf_memory, pres->object, "free Shading resources"); |
3769 | 683 | pres->object = 0; |
3770 | 683 | } |
3771 | 683 | pres = pres->next; |
3772 | 683 | } |
3773 | 544k | } |
3774 | 34.0k | } |
3775 | | |
3776 | 34.0k | { |
3777 | 34.0k | int j; |
3778 | | |
3779 | | /* prescan the colour space arrays to see if any of them reference other colour |
3780 | | * spaces. If they do, then remove the reference by changing it from a COS_VALUE_OBJECT |
3781 | | * into a COS_VALUE_CONST, see comment at the start of this section as to why this is safe. |
3782 | | */ |
3783 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3784 | 544k | pdf_resource_t *pres = pdev->resources[resourceColorSpace].chains[j]; |
3785 | 547k | for (; pres != 0;) { |
3786 | 3.48k | if (cos_type(pres->object) == cos_type_array) { |
3787 | 3.16k | int64_t index; |
3788 | 3.16k | cos_array_t *pca = (cos_array_t *)pres->object; |
3789 | 3.16k | const cos_array_element_t *element = cos_array_element_first(pca); |
3790 | 3.16k | cos_value_t *v; |
3791 | | |
3792 | 12.8k | while (element) { |
3793 | 9.68k | element = cos_array_element_next(element, &index, (const cos_value_t **)&v); |
3794 | 9.68k | if (v->value_type == COS_VALUE_OBJECT) { |
3795 | 2.57k | if (pdf_find_resource_by_resource_id(pdev, resourceColorSpace, v->contents.object->id)){ |
3796 | 144 | v->value_type = COS_VALUE_CONST; |
3797 | 144 | } |
3798 | 2.57k | } |
3799 | 9.68k | } |
3800 | 3.16k | } |
3801 | 3.48k | pres = pres->next; |
3802 | 3.48k | } |
3803 | 544k | } |
3804 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3805 | 544k | pdf_resource_t *pres = pdev->resources[resourceColorSpace].chains[j]; |
3806 | 547k | for (; pres != 0;) { |
3807 | 3.48k | free_color_space(pdev, pres); |
3808 | 3.48k | pres = pres->next; |
3809 | 3.48k | } |
3810 | 544k | } |
3811 | 34.0k | } |
3812 | | |
3813 | 34.0k | { |
3814 | 34.0k | int j; |
3815 | | |
3816 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3817 | 544k | pdf_resource_t *pres = pdev->resources[resourceGroup].chains[j]; |
3818 | | |
3819 | 544k | for (; pres != 0;) { |
3820 | 0 | if (pres->object) { |
3821 | 0 | cos_release(pres->object, "free Group dict"); |
3822 | 0 | gs_free_object(pdev->pdf_memory, pres->object, "free Group resources"); |
3823 | 0 | pres->object = 0; |
3824 | 0 | } |
3825 | 0 | pres = pres->next; |
3826 | 0 | } |
3827 | 544k | } |
3828 | 34.0k | } |
3829 | | |
3830 | 34.0k | { |
3831 | 34.0k | int j; |
3832 | | |
3833 | | /* prescan the function arrays and dictionariesto see if any of them reference other |
3834 | | * functions. If they do, then remove the reference by changing it from a COS_VALUE_OBJECT |
3835 | | * into a COS_VALUE_CONST, see comment at the start of this section as to why this is safe. |
3836 | | */ |
3837 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3838 | 544k | pdf_resource_t *pnext = 0, *pres = pdev->resources[resourceFunction].chains[j]; |
3839 | | |
3840 | 547k | for (; pres != 0;) { |
3841 | 2.96k | if (pres->object) { |
3842 | 2.96k | free_function_refs(pdev, pres->object); |
3843 | 2.96k | } |
3844 | 2.96k | pnext = pres->next; |
3845 | 2.96k | pres = pnext; |
3846 | 2.96k | } |
3847 | 544k | } |
3848 | 578k | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3849 | 544k | pdf_resource_t *pnext = 0, *pres = pdev->resources[resourceFunction].chains[j]; |
3850 | | |
3851 | 547k | for (; pres != 0;) { |
3852 | 2.96k | if (pres->object) { |
3853 | 2.96k | cos_release(pres->object, "free function dict"); |
3854 | 2.96k | gs_free_object(pdev->pdf_memory, pres->object, "free function resources"); |
3855 | 2.96k | pres->object = 0; |
3856 | 2.96k | } |
3857 | 2.96k | pnext = pres->next; |
3858 | 2.96k | pres = pnext; |
3859 | 2.96k | } |
3860 | 544k | } |
3861 | 34.0k | } |
3862 | | |
3863 | | /* Free named objects. */ |
3864 | | |
3865 | 34.0k | if (pdev->NI_stack != NULL) { |
3866 | 34.0k | cos_release((cos_object_t *)pdev->NI_stack, "Release Name Index stack"); |
3867 | 34.0k | gs_free_object(mem, pdev->NI_stack, "Free Name Index stack"); |
3868 | 34.0k | pdev->NI_stack = 0; |
3869 | 34.0k | } |
3870 | | |
3871 | 34.0k | if (pdev->local_named_objects != NULL) { |
3872 | 34.0k | cos_dict_objects_delete(pdev->local_named_objects); |
3873 | 34.0k | COS_FREE(pdev->local_named_objects, "pdf_close(local_named_objects)"); |
3874 | 34.0k | pdev->local_named_objects = 0; |
3875 | 34.0k | } |
3876 | | |
3877 | 34.0k | if (pdev->global_named_objects != NULL) { |
3878 | | /* global resources include the Catalog object and apparently the Info dict */ |
3879 | 34.0k | cos_dict_objects_delete(pdev->global_named_objects); |
3880 | 34.0k | COS_FREE(pdev->global_named_objects, "pdf_close(global_named_objects)"); |
3881 | 34.0k | pdev->global_named_objects = 0; |
3882 | 34.0k | } |
3883 | | |
3884 | 34.0k | code1 = pdf_free_resource_objects(pdev, resourceOther); |
3885 | 34.0k | if (code >= 0) |
3886 | 34.0k | code = code1; |
3887 | | |
3888 | 34.0k | if (code >= 0) { |
3889 | 34.0k | int i, j; |
3890 | | |
3891 | 578k | for (i = 0; i < NUM_RESOURCE_TYPES; i++) { |
3892 | 9.25M | for (j = 0; j < NUM_RESOURCE_CHAINS; ++j) { |
3893 | 8.70M | pdev->resources[i].chains[j] = 0; |
3894 | 8.70M | } |
3895 | 544k | } |
3896 | 34.0k | } |
3897 | | |
3898 | | /* Release the resource records. */ |
3899 | | /* So what exactly is stored in this list ? I believe the following types of resource: |
3900 | | * |
3901 | | * resourceCharProc |
3902 | | * resourceFont |
3903 | | * resourceCIDFont |
3904 | | * resourceFontDescriptor |
3905 | | * resourceColorSpace |
3906 | | * resourceExtGState |
3907 | | * resourceXObject |
3908 | | * resourceSoftMaskDict |
3909 | | * resourceGroup |
3910 | | * resourcePattern |
3911 | | * resourceShading |
3912 | | * |
3913 | | * resourceCMap resourcePage and resourceFunction don't appear to be tracked. I note |
3914 | | * that resourcePage and resourceCMap are freed above, as is resourceXObject and resourceSoftmaskDict. |
3915 | | * So presumably reourceFunction just leaks. |
3916 | | * |
3917 | | * It seems that all these types of resources are added when they are created |
3918 | | * in addition there are 'pdf_forget_resource' and pdf_cancel_resource which |
3919 | | * remove entries from this list. |
3920 | | */ |
3921 | 34.0k | { |
3922 | 34.0k | pdf_resource_t *pres; |
3923 | 34.0k | pdf_resource_t *prev; |
3924 | | |
3925 | 356k | for (prev = pdev->last_resource; (pres = prev) != 0;) { |
3926 | 322k | prev = pres->prev; |
3927 | 322k | gs_free_object(mem, pres, "pdf_resource_t"); |
3928 | 322k | } |
3929 | 34.0k | pdev->last_resource = 0; |
3930 | 34.0k | } |
3931 | | |
3932 | | /* Wrap up. */ |
3933 | | |
3934 | 34.0k | pdev->font_cache = 0; |
3935 | | |
3936 | 34.0k | if (pdev->pages != NULL) |
3937 | 34.0k | { |
3938 | 34.0k | int i; |
3939 | 79.3k | for (i=0;i < pdev->next_page;i++) { |
3940 | 45.3k | cos_release((cos_object_t *)pdev->pages[i].Page, "Free page dict"); |
3941 | 45.3k | if (pdev->pages[i].Annots) { |
3942 | 0 | cos_release((cos_object_t *)pdev->pages[i].Annots, "Release Annots dict"); |
3943 | 0 | gs_free_object(mem, pdev->pages[i].Annots, "Free Annots dict"); |
3944 | 0 | } |
3945 | 45.3k | gs_free_object(mem, pdev->pages[i].Page, "Free Page object"); |
3946 | 45.3k | pdev->pages[i].Page = NULL; |
3947 | 45.3k | } |
3948 | 1.73M | for (i=0;i < pdev->num_pages;i++) { |
3949 | 1.70M | if (pdev->pages[i].Page != NULL) { |
3950 | 20 | emprintf(pdev->memory, |
3951 | 20 | "Page object was reserved for an Annotation destination, but no such page was drawn, annotation in output will be invalid.\n"); |
3952 | 20 | gs_free_object(mem, pdev->pages[i].Page, "Free Page object"); |
3953 | 20 | pdev->pages[i].Page = NULL; |
3954 | 20 | } |
3955 | 1.70M | } |
3956 | 34.0k | } |
3957 | 34.0k | gs_free_object(mem, pdev->pages, "pages"); |
3958 | 34.0k | pdev->pages = 0; |
3959 | | |
3960 | 34.0k | pdev->num_pages = 0; |
3961 | | |
3962 | 34.0k | gs_free_object(mem, pdev->sbstack, "Free sbstack"); |
3963 | 34.0k | pdev->sbstack = 0; |
3964 | | |
3965 | 34.0k | if (pdev->text != NULL) |
3966 | 34.0k | text_data_free(mem, pdev->text); |
3967 | 34.0k | pdev->text = 0; |
3968 | | |
3969 | 34.0k | if (pdev->Pages != NULL) { |
3970 | 34.0k | cos_release((cos_object_t *)pdev->Pages, "release Pages dict"); |
3971 | 34.0k | gs_free_object(mem, pdev->Pages, "Free Pages dict"); |
3972 | 34.0k | pdev->Pages = 0; |
3973 | 34.0k | } |
3974 | | |
3975 | 34.0k | if (pdev->vgstack != NULL) |
3976 | 34.0k | { |
3977 | 34.0k | int i; |
3978 | 408k | for (i=0;i < pdev->vgstack_size;i++) { |
3979 | 374k | if (pdev->vgstack[i].dash_pattern != NULL) |
3980 | 303 | gs_free_object(pdev->memory->non_gc_memory, pdev->vgstack[i].dash_pattern, "pdfwrite final free stored dash in gstate"); |
3981 | 374k | } |
3982 | 34.0k | gs_free_object(pdev->pdf_memory, pdev->vgstack, "pdf_close(graphics state stack)"); |
3983 | 34.0k | pdev->vgstack = 0; |
3984 | 34.0k | } |
3985 | | |
3986 | 34.0k | if (pdev->Namespace_stack != NULL) { |
3987 | 34.0k | cos_release((cos_object_t *)pdev->Namespace_stack, "release Name space stack"); |
3988 | 34.0k | gs_free_object(mem, pdev->Namespace_stack, "Free Name space stack"); |
3989 | 34.0k | pdev->Namespace_stack = 0; |
3990 | 34.0k | } |
3991 | | |
3992 | 34.0k | pdev->Catalog = 0; |
3993 | 34.0k | pdev->Info = 0; |
3994 | | |
3995 | 34.0k | gs_free_object(mem, pdev->outline_levels, "outline_levels array"); |
3996 | 34.0k | pdev->outline_levels = 0; |
3997 | 34.0k | pdev->outline_depth = -1; |
3998 | 34.0k | pdev->max_outline_depth = 0; |
3999 | | |
4000 | 34.0k | if (s != NULL) |
4001 | 34.0k | { |
4002 | | /* pdf_open_dcument could set up filters for entire document. |
4003 | | Removing them now. */ |
4004 | 34.0k | int status; |
4005 | | |
4006 | 34.0k | if (!pdev->ProduceDSC) |
4007 | 0 | stream_putc(s, 0x04); |
4008 | 68.0k | while (s->strm) { |
4009 | 34.0k | s = s->strm; |
4010 | 34.0k | } |
4011 | 34.0k | status = s_close_filters(&pdev->strm, s); |
4012 | 34.0k | if (status < 0 && code == 0) |
4013 | 0 | code = gs_error_ioerror; |
4014 | 34.0k | } |
4015 | | |
4016 | 34.0k | pdf_free_pdf_font_cache(pdev); |
4017 | | |
4018 | 34.0k | code1 = gdev_vector_close_file((gx_device_vector *) pdev); |
4019 | 34.0k | if (code >= 0) |
4020 | 34.0k | code = code1; |
4021 | 34.0k | if (pdev->max_referred_page >= pdev->next_page + 1 && pdev->next_page != 0 && !file_per_page) { |
4022 | | /* Note : pdev->max_referred_page counts from 1, |
4023 | | and pdev->next_page counts from 0. */ |
4024 | 0 | emprintf2(pdev->memory, "ERROR: A pdfmark destination page %d " |
4025 | 0 | "points beyond the last page %d.\n", |
4026 | 0 | pdev->max_referred_page, pdev->next_page); |
4027 | 0 | } |
4028 | 34.0k | code = pdf_close_files(pdev, code); |
4029 | 34.0k | if (code < 0) |
4030 | 0 | return code; |
4031 | | |
4032 | | /* If we didn't get called from pdf_output_page, and we are doign file-per-page |
4033 | | * output, then the call from close_device will leave an empty file which we don't |
4034 | | * want. So here we delete the file. |
4035 | | * NOTE: We needed to let it process the whole page in order to make sure everything |
4036 | | * got properly freed. |
4037 | | */ |
4038 | 34.0k | if (file_per_page) { |
4039 | 0 | code = gx_device_delete_output_file((const gx_device *)pdev, pdev->fname); |
4040 | 0 | if (code != 0) |
4041 | 0 | code = gs_note_error(gs_error_ioerror); |
4042 | 0 | } |
4043 | | |
4044 | 34.0k | return code; |
4045 | 34.0k | } |