/src/ghostpdl/devices/vector/gdevpdfd.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 | | /* Path drawing procedures for pdfwrite driver */ |
18 | | #include "math_.h" |
19 | | #include "memory_.h" |
20 | | #include "gx.h" |
21 | | #include "gxdevice.h" |
22 | | #include "gxfixed.h" |
23 | | #include "gxgstate.h" |
24 | | #include "gxpaint.h" |
25 | | #include "gxcoord.h" |
26 | | #include "gxdevmem.h" |
27 | | #include "gxcolor2.h" |
28 | | #include "gxhldevc.h" |
29 | | #include "gsstate.h" |
30 | | #include "gxstate.h" |
31 | | #include "gserrors.h" |
32 | | #include "gsptype2.h" |
33 | | #include "gsshade.h" |
34 | | #include "gzpath.h" |
35 | | #include "gzcpath.h" |
36 | | #include "gdevpdfx.h" |
37 | | #include "gdevpdfg.h" |
38 | | #include "gdevpdfo.h" |
39 | | #include "gsutil.h" |
40 | | #include "gdevpdtf.h" |
41 | | #include "gdevpdts.h" |
42 | | #include "gxdevsop.h" |
43 | | |
44 | | /* ---------------- Drawing ---------------- */ |
45 | | |
46 | | /* Fill a rectangle. */ |
47 | | int |
48 | | gdev_pdf_fill_rectangle(gx_device * dev, int x, int y, int w, int h, |
49 | | gx_color_index color) |
50 | 135M | { |
51 | 135M | gx_device_pdf *pdev = (gx_device_pdf *) dev; |
52 | 135M | int code; |
53 | | |
54 | 135M | if (pdev->Eps2Write) { |
55 | 134M | float x0, y0, x1, y1; |
56 | 134M | gs_rect *Box; |
57 | | |
58 | 134M | if (!pdev->accumulating_charproc) { |
59 | 132M | Box = &pdev->BBox; |
60 | 132M | x0 = x / (pdev->HWResolution[0] / 72.0); |
61 | 132M | y0 = y / (pdev->HWResolution[1] / 72.0); |
62 | 132M | x1 = x0 + (w / (pdev->HWResolution[0] / 72.0)); |
63 | 132M | y1 = y0 + (h / (pdev->HWResolution[1] / 72.0)); |
64 | 132M | } |
65 | 2.20M | else { |
66 | 2.20M | Box = &pdev->charproc_BBox; |
67 | 2.20M | x0 = (float)x / 100; |
68 | 2.20M | y0 = (float)y / 100; |
69 | 2.20M | x1 = x0 + (w / 100); |
70 | 2.20M | y1 = y0 + (h / 100); |
71 | 2.20M | } |
72 | | |
73 | 134M | if (Box->p.x > x0) |
74 | 31.3k | Box->p.x = x0; |
75 | 134M | if (Box->p.y > y0) |
76 | 19.6k | Box->p.y = y0; |
77 | 134M | if (Box->q.x < x1) |
78 | 2.23M | Box->q.x = x1; |
79 | 134M | if (Box->q.y < y1) |
80 | 1.39M | Box->q.y = y1; |
81 | 134M | if (pdev->AccumulatingBBox) |
82 | 134M | return 0; |
83 | 134M | } |
84 | 960k | code = pdf_open_page(pdev, PDF_IN_STREAM); |
85 | 960k | if (code < 0) |
86 | 0 | return code; |
87 | | /* Make sure we aren't being clipped. */ |
88 | 960k | code = pdf_put_clip_path(pdev, NULL); |
89 | 960k | if (code < 0) |
90 | 0 | return code; |
91 | 960k | pdf_set_pure_color(pdev, color, &pdev->saved_fill_color, |
92 | 960k | &pdev->fill_used_process_color, |
93 | 960k | &psdf_set_fill_color_commands); |
94 | 960k | if (!pdev->HaveStrokeColor) |
95 | 897k | pdev->saved_stroke_color = pdev->saved_fill_color; |
96 | 960k | pprintd4(pdev->strm, "%d %d %d %d re f\n", x, y, w, h); |
97 | 960k | return 0; |
98 | 960k | } |
99 | | |
100 | | /* ---------------- Path drawing ---------------- */ |
101 | | |
102 | | /* ------ Vector device implementation ------ */ |
103 | | |
104 | | static int |
105 | | pdf_setlinewidth(gx_device_vector * vdev, double width) |
106 | 70.5k | { |
107 | | /* Acrobat Reader doesn't accept negative line widths. */ |
108 | 70.5k | return psdf_setlinewidth(vdev, fabs(width)); |
109 | 70.5k | } |
110 | | |
111 | | static bool |
112 | | pdf_can_handle_hl_color(gx_device_vector * vdev, const gs_gstate * pgs, |
113 | | const gx_drawing_color * pdc) |
114 | 1.73M | { |
115 | 1.73M | return pgs != NULL; |
116 | 1.73M | } |
117 | | |
118 | | static int |
119 | | pdf_setfillcolor(gx_device_vector * vdev, const gs_gstate * pgs, |
120 | | const gx_drawing_color * pdc) |
121 | 1.01M | { |
122 | 1.01M | gx_device_pdf *const pdev = (gx_device_pdf *)vdev; |
123 | 1.01M | bool hl_color = (*vdev_proc(vdev, can_handle_hl_color)) (vdev, pgs, pdc); |
124 | 1.01M | const gs_gstate *pgs_for_hl_color = (hl_color ? pgs : NULL); |
125 | | |
126 | 1.01M | if (!pdev->HaveStrokeColor) { |
127 | | /* opdfread.ps assumes same color for stroking and non-stroking operations. */ |
128 | 553k | int code = pdf_set_drawing_color(pdev, pgs_for_hl_color, pdc, &pdev->saved_stroke_color, |
129 | 553k | &pdev->stroke_used_process_color, |
130 | 553k | &psdf_set_stroke_color_commands); |
131 | 553k | if (code < 0) |
132 | 11.5k | return code; |
133 | 553k | } |
134 | 999k | return pdf_set_drawing_color(pdev, pgs_for_hl_color, pdc, &pdev->saved_fill_color, |
135 | 999k | &pdev->fill_used_process_color, |
136 | 999k | &psdf_set_fill_color_commands); |
137 | 1.01M | } |
138 | | |
139 | | static int |
140 | | pdf_setstrokecolor(gx_device_vector * vdev, const gs_gstate * pgs, |
141 | | const gx_drawing_color * pdc) |
142 | 91.1k | { |
143 | 91.1k | gx_device_pdf *const pdev = (gx_device_pdf *)vdev; |
144 | 91.1k | bool hl_color = (*vdev_proc(vdev, can_handle_hl_color)) (vdev, pgs, pdc); |
145 | 91.1k | const gs_gstate *pgs_for_hl_color = (hl_color ? pgs : NULL); |
146 | | |
147 | 91.1k | if (!pdev->HaveStrokeColor) { |
148 | | /* opdfread.ps assumes same color for stroking and non-stroking operations. */ |
149 | 69.6k | int code = pdf_set_drawing_color(pdev, pgs_for_hl_color, pdc, &pdev->saved_fill_color, |
150 | 69.6k | &pdev->fill_used_process_color, |
151 | 69.6k | &psdf_set_fill_color_commands); |
152 | 69.6k | if (code < 0) |
153 | 168 | return code; |
154 | 69.6k | } |
155 | 90.9k | return pdf_set_drawing_color(pdev, pgs_for_hl_color, pdc, &pdev->saved_stroke_color, |
156 | 90.9k | &pdev->stroke_used_process_color, |
157 | 90.9k | &psdf_set_stroke_color_commands); |
158 | 91.1k | } |
159 | | |
160 | | static int |
161 | | pdf_dorect(gx_device_vector * vdev, fixed x0, fixed y0, fixed x1, fixed y1, |
162 | | gx_path_type_t type) |
163 | 500k | { |
164 | 500k | gx_device_pdf *pdev = (gx_device_pdf *)vdev; |
165 | 500k | fixed xmax = int2fixed(32766), ymax = int2fixed(32766); |
166 | 500k | int bottom = (pdev->ResourcesBeforeUsage ? 1 : 0); |
167 | 500k | fixed xmin = (pdev->sbstack_depth > bottom ? -xmax : 0); |
168 | 500k | fixed ymin = (pdev->sbstack_depth > bottom ? -ymax : 0); |
169 | | |
170 | | /* |
171 | | * If we're doing a stroke operation, expand the checking box by the |
172 | | * stroke width. |
173 | | */ |
174 | 500k | if (type & gx_path_type_stroke) { |
175 | 176k | double w = vdev->state.line_params.half_width; |
176 | 176k | double xw = w * (fabs(vdev->state.ctm.xx) + fabs(vdev->state.ctm.yx)); |
177 | 176k | int d = float2fixed(xw) + fixed_1; |
178 | | |
179 | 176k | xmin -= d; |
180 | 176k | xmax += d; |
181 | 176k | ymin -= d; |
182 | 176k | ymax += d; |
183 | 176k | } |
184 | 500k | if (pdev->PDFA == 1) { |
185 | | /* Check values, and decide how to proceed based on PDFACompatibilitylevel */ |
186 | 0 | if (x0 < xmin || y0 < ymin || x1 - x0 > xmax || y1 - y0 >ymax) { |
187 | 0 | switch(pdev->PDFACompatibilityPolicy) { |
188 | 0 | case 0: |
189 | 0 | emprintf(pdev->memory, |
190 | 0 | "Required co-ordinate outside valid range for PDF/A-1, reverting to normal PDF output.\n"); |
191 | 0 | pdev->AbortPDFAX = true; |
192 | 0 | pdev->PDFA = 0; |
193 | 0 | break; |
194 | 0 | case 1: |
195 | 0 | emprintf(pdev->memory, |
196 | 0 | "Required co-ordinate outside valid range for PDF/A-1, clamping to valid range, output may be incorrect.\n"); |
197 | | /* |
198 | | * Clamp coordinates to avoid tripping over Acrobat Reader's limit |
199 | | * of 32K on user coordinate values, which was adopted by the PDF/A-1 spec. |
200 | | */ |
201 | 0 | if (x0 < xmin) |
202 | 0 | x0 = xmin; |
203 | |
|
204 | 0 | if (y0 < ymin) |
205 | 0 | y0 = ymin; |
206 | | |
207 | | /* We used to clamp x1 and y1 here, but actually, because this is a rectangle |
208 | | * we don't need to do that, we need to clamp the *difference* between x0,x1 |
209 | | * and y0,y1 to keep it inside the Acrobat 4 or 5 limits. |
210 | | */ |
211 | 0 | if (x1 - x0 > xmax) |
212 | 0 | x1 = x0 + xmax; |
213 | 0 | if (y1 - y0 > ymax) |
214 | 0 | y1 = y0 + ymax; |
215 | 0 | break; |
216 | 0 | default: |
217 | 0 | case 2: |
218 | 0 | emprintf(pdev->memory, |
219 | 0 | "Required co-ordinate outside valid range for PDF/A-1, aborting.\n"); |
220 | 0 | return_error(gs_error_limitcheck); |
221 | 0 | break; |
222 | 0 | } |
223 | 0 | } |
224 | 0 | } |
225 | 500k | return psdf_dorect(vdev, x0, y0, x1, y1, type); |
226 | 500k | } |
227 | | |
228 | | static int |
229 | | pdf_endpath(gx_device_vector * vdev, gx_path_type_t type) |
230 | 1.42M | { |
231 | 1.42M | return 0; /* always handled by caller */ |
232 | 1.42M | } |
233 | | |
234 | | const gx_device_vector_procs pdf_vector_procs = { |
235 | | /* Page management */ |
236 | | NULL, |
237 | | /* Imager state */ |
238 | | pdf_setlinewidth, |
239 | | psdf_setlinecap, |
240 | | psdf_setlinejoin, |
241 | | psdf_setmiterlimit, |
242 | | psdf_setdash, |
243 | | psdf_setflat, |
244 | | psdf_setlogop, |
245 | | /* Other state */ |
246 | | pdf_can_handle_hl_color, |
247 | | pdf_setfillcolor, |
248 | | pdf_setstrokecolor, |
249 | | /* Paths */ |
250 | | psdf_dopath, |
251 | | pdf_dorect, |
252 | | psdf_beginpath, |
253 | | psdf_moveto, |
254 | | psdf_lineto, |
255 | | psdf_curveto, |
256 | | psdf_closepath, |
257 | | pdf_endpath |
258 | | }; |
259 | | |
260 | | /* ------ Utilities ------ */ |
261 | | |
262 | | /* Store a copy of clipping path. */ |
263 | | int |
264 | | pdf_remember_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath) |
265 | 219k | { |
266 | 219k | int code = 0; |
267 | | /* Used for skipping redundant clip paths. SF bug #624168. */ |
268 | 219k | if (pdev->clip_path != 0) { |
269 | 69.4k | gx_path_free(pdev->clip_path, "pdf clip path"); |
270 | 69.4k | } |
271 | 219k | if (pcpath == 0) { |
272 | 148k | pdev->clip_path = 0; |
273 | 148k | return 0; |
274 | 148k | } |
275 | 71.2k | pdev->clip_path = gx_path_alloc(pdev->pdf_memory, "pdf clip path"); |
276 | 71.2k | if (pdev->clip_path == 0) |
277 | 0 | return_error(gs_error_VMerror); |
278 | | |
279 | 71.2k | code = gx_cpath_to_path((gx_clip_path *)pcpath, pdev->clip_path); |
280 | 71.2k | if (code < 0) |
281 | 0 | return code; |
282 | | |
283 | | /* gx_cpath_to_path above ends up going through gx_path_assign_preserve |
284 | | * which specifically states that the segments of the paths (in this case pcpath |
285 | | * and pdev->clip_path) must have been allocated with the same allocator. |
286 | | * If that's not true (eg pdfi running inside GS) then we need to 'unshare' |
287 | | * the path. Otherwise we mauy end up with pcpath being freed and discarded |
288 | | * while the pdfwrite devcie still thinks it has a pointer to it. |
289 | | */ |
290 | 71.2k | if (pcpath->path.memory != pdev->pdf_memory) |
291 | 71.2k | code = gx_path_unshare(pdev->clip_path); |
292 | | |
293 | 71.2k | return code; |
294 | 71.2k | } |
295 | | |
296 | | /* Check if same clipping path. */ |
297 | | static int |
298 | | pdf_is_same_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath) |
299 | 200k | { |
300 | | /* Used for skipping redundant clip paths. SF bug #624168. */ |
301 | 200k | gs_cpath_enum cenum; |
302 | 200k | gs_path_enum penum; |
303 | 200k | gs_fixed_point vs0[3], vs1[3]; |
304 | 200k | int code, pe_op; |
305 | | |
306 | 200k | if ((pdev->clip_path != 0) != (pcpath != 0)) |
307 | 118k | return 0; |
308 | | /* Both clip paths are empty, so the same */ |
309 | 81.7k | if (pdev->clip_path == 0) |
310 | 0 | return 1; |
311 | 81.7k | code = gx_path_enum_init(&penum, pdev->clip_path); |
312 | 81.7k | if (code < 0) |
313 | 0 | return code; |
314 | 81.7k | code = gx_cpath_enum_init(&cenum, (gx_clip_path *)pcpath); |
315 | 81.7k | if (code < 0) |
316 | 0 | return code; |
317 | | /* This flags a warning in Coverity, uninitialised variable cenum.first_visit */ |
318 | | /* This is because gx_cpath_enum_init doesn't initialise first_visit, but the */ |
319 | | /* variable can be used in enum_next. However, this is not truly used this */ |
320 | | /* way. The enum_init sets the 'state' to 'scan', and the first thing that happens */ |
321 | | /* in enum_next when state is 'scan' is to set first_visit. */ |
322 | 205k | while ((code = gx_cpath_enum_next(&cenum, vs0)) > 0) { |
323 | 191k | pe_op = gx_path_enum_next(&penum, vs1); |
324 | 191k | if (pe_op < 0) |
325 | 0 | return pe_op; |
326 | 191k | if (pe_op != code) |
327 | 484 | return 0; |
328 | 191k | switch (pe_op) { |
329 | 0 | case gs_pe_curveto: |
330 | 0 | if (vs0[1].x != vs1[1].x || vs0[1].y != vs1[1].y || |
331 | 0 | vs0[2].x != vs1[2].x || vs0[2].y != vs1[2].y) |
332 | 0 | return 0; |
333 | | /* fall through */ |
334 | 85.9k | case gs_pe_moveto: |
335 | 182k | case gs_pe_lineto: |
336 | 182k | case gs_pe_gapto: |
337 | 182k | if (vs0[0].x != vs1[0].x || vs0[0].y != vs1[0].y) |
338 | 68.0k | return 0; |
339 | 191k | } |
340 | 191k | } |
341 | 13.1k | if (code < 0) |
342 | 0 | return code; |
343 | 13.1k | code = gx_path_enum_next(&penum, vs1); |
344 | 13.1k | if (code < 0) |
345 | 0 | return code; |
346 | 13.1k | return (code == 0); |
347 | 13.1k | } |
348 | | |
349 | | int |
350 | | pdf_check_soft_mask(gx_device_pdf * pdev, gs_gstate * pgs) |
351 | 7.20M | { |
352 | 7.20M | int code = 0; |
353 | | |
354 | 7.20M | if (pgs && pdev->state.soft_mask_id != pgs->soft_mask_id) { |
355 | | /* |
356 | | * The contents must be open already, so the following will only exit |
357 | | * text or string context. |
358 | | */ |
359 | 157 | code = pdf_open_contents(pdev, PDF_IN_STREAM); |
360 | 157 | if (code < 0) |
361 | 0 | return code; |
362 | 157 | if (pdev->vgstack_depth > pdev->vgstack_bottom) { |
363 | 120 | code = pdf_restore_viewer_state(pdev, pdev->strm); |
364 | 120 | if (code < 0) |
365 | 0 | return code; |
366 | 120 | } |
367 | 157 | } |
368 | 7.20M | return code; |
369 | 7.20M | } |
370 | | |
371 | | /* Test whether we will need to put the clipping path. */ |
372 | | bool |
373 | | pdf_must_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath) |
374 | 9.02M | { |
375 | 9.02M | if (pcpath == NULL) { |
376 | 17.2k | if (pdev->clip_path_id == pdev->no_clip_path_id) |
377 | 16.2k | return false; |
378 | 9.00M | } else { |
379 | 9.00M | if (pdev->clip_path_id == pcpath->id) |
380 | 796k | return false; |
381 | 8.20M | if (gx_cpath_includes_rectangle(pcpath, fixed_0, fixed_0, |
382 | 8.20M | int2fixed(pdev->width), |
383 | 8.20M | int2fixed(pdev->height))) |
384 | 8.08M | if (pdev->clip_path_id == pdev->no_clip_path_id) |
385 | 8.07M | return false; |
386 | 129k | if (pdf_is_same_clip_path(pdev, pcpath) > 0) { |
387 | 13.1k | pdev->clip_path_id = pcpath->id; |
388 | 13.1k | return false; |
389 | 13.1k | } |
390 | 129k | } |
391 | 117k | return true; |
392 | 9.02M | } |
393 | | |
394 | | static int pdf_write_path(gx_device_pdf * pdev, gs_path_enum *cenum, gdev_vector_dopath_state_t *state, gx_path *path, int is_clip_enum, |
395 | | gx_path_type_t type, const gs_matrix *pmat) |
396 | 1.37M | { |
397 | 1.37M | int pe_op; |
398 | 1.37M | gdev_vector_path_seg_record segments[5] = {0}; |
399 | 1.37M | int i, seg_index = 0, is_rect = 1, buffering = 0, initial_m = 0, segs = 0, code, matrix_optimisable = 0; |
400 | 1.37M | gx_path_rectangular_type rtype = prt_none; |
401 | 1.37M | gs_fixed_rect rbox; |
402 | 1.37M | gx_device_vector *vdev = (gx_device_vector *)pdev; |
403 | 1.37M | gs_fixed_point line_start = {0,0}; |
404 | 1.37M | gs_point p, q; |
405 | 1.37M | bool do_close = (type & (gx_path_type_clip | gx_path_type_stroke | gx_path_type_always_close)) != 0; |
406 | 1.37M | bool stored_moveto = false; |
407 | | |
408 | 1.37M | gdev_vector_dopath_init(state, (gx_device_vector *)pdev, |
409 | 1.37M | type, pmat); |
410 | 1.37M | if (is_clip_enum) |
411 | 25.2k | code = gx_cpath_enum_init((gs_cpath_enum *)cenum, (gx_clip_path *)path); |
412 | 1.35M | else { |
413 | 1.35M | code = gx_path_enum_init(cenum, path); |
414 | 1.35M | rtype = gx_path_is_rectangular(path, &rbox); |
415 | 1.35M | } |
416 | 1.37M | if (code < 0) |
417 | 0 | return code; |
418 | | |
419 | 1.37M | if((pmat == 0 || is_xxyy(pmat) || is_xyyx(pmat)) && |
420 | 1.37M | (state->scale_mat.xx == 1.0 && state->scale_mat.yy == 1.0 && |
421 | 1.37M | is_xxyy(&state->scale_mat) && is_fzero2(state->scale_mat.tx, state->scale_mat.ty))) { |
422 | 1.20M | matrix_optimisable = 1; |
423 | 1.20M | buffering = 1; |
424 | 1.20M | } |
425 | | /* |
426 | | * if the path type is stroke, we only recognize closed |
427 | | * rectangles; otherwise, we recognize all rectangles. |
428 | | * Note that for stroking with a transformation, we can't use dorect, |
429 | | * which requires (untransformed) device coordinates. |
430 | | */ |
431 | 1.37M | if (rtype != prt_none && |
432 | 1.37M | (!(type & gx_path_type_stroke) || rtype == prt_closed) && |
433 | 1.37M | matrix_optimisable == 1) |
434 | 455k | { |
435 | 455k | gs_point p, q; |
436 | | |
437 | 455k | gs_point_transform_inverse((double)rbox.p.x, (double)rbox.p.y, |
438 | 455k | &state->scale_mat, &p); |
439 | 455k | gs_point_transform_inverse((double)rbox.q.x, (double)rbox.q.y, |
440 | 455k | &state->scale_mat, &q); |
441 | 455k | code = vdev_proc(vdev, dorect)(vdev, (fixed)p.x, (fixed)p.y, |
442 | 455k | (fixed)q.x, (fixed)q.y, type); |
443 | 455k | if (code >= 0) { |
444 | 455k | if (code == 0) |
445 | 455k | return 1; |
446 | 0 | return code; |
447 | 455k | } |
448 | | /* If the dorect proc failed, use a general path. */ |
449 | 455k | } |
450 | | |
451 | | /* The following is an optimisation for space. If we see a closed subpath |
452 | | * which is a rectangle, emit it as a 're' instead of writing the individual |
453 | | * segments of the path. Note that 're' always applies the width before the |
454 | | * height when constructing the path, so we must take care to ensure that |
455 | | * we get the path direction correct. We do ths by detecting whether the path |
456 | | * moves first in the x or y directoin, if it moves in the y direction first, |
457 | | * we simply move one vertex round the rectangle, ie we start at point 2. |
458 | | */ |
459 | 7.98M | do { |
460 | 7.98M | if (is_clip_enum) |
461 | 206k | segments[seg_index].op = pe_op = gx_cpath_enum_next((gs_cpath_enum *)cenum, segments[seg_index].vs); |
462 | 7.77M | else |
463 | 7.77M | segments[seg_index].op = pe_op = gx_path_enum_next(cenum, segments[seg_index].vs); |
464 | | |
465 | 7.98M | if (segs == 0 && pe_op > 0) |
466 | 919k | segs = 1; |
467 | | |
468 | 7.98M | switch(pe_op) { |
469 | 1.19M | case gs_pe_moveto: |
470 | 1.19M | case gs_pe_gapto: |
471 | 1.19M | if (!buffering) { |
472 | 284k | stored_moveto = true; |
473 | 284k | line_start = segments[0].vs[0]; |
474 | 284k | seg_index = -1; |
475 | 907k | } else { |
476 | 1.13M | for (i=0;i<seg_index;i++) { |
477 | 224k | gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs); |
478 | 224k | } |
479 | 907k | line_start = segments[seg_index].vs[0]; |
480 | 907k | segments[0] = segments[seg_index]; |
481 | 907k | seg_index = 0; |
482 | 907k | initial_m = 1; |
483 | 907k | } |
484 | 1.19M | break; |
485 | 2.97M | case gs_pe_lineto: |
486 | 2.97M | if (!buffering) { |
487 | 1.40M | if (stored_moveto) { |
488 | 210k | gdev_vector_dopath_segment(state, gs_pe_moveto, &line_start); |
489 | 210k | stored_moveto = false; |
490 | 210k | } |
491 | 2.81M | for (i=0;i<=seg_index;i++) |
492 | 1.40M | gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs); |
493 | 1.40M | seg_index = -1; |
494 | 1.56M | } else { |
495 | 1.56M | if (!initial_m) { |
496 | 0 | for (i=0;i<=seg_index;i++) { |
497 | 0 | gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs); |
498 | 0 | } |
499 | 0 | buffering = 0; |
500 | 0 | seg_index = -1; |
501 | 0 | } |
502 | 1.56M | if (type & gx_path_type_optimize && seg_index > 0) { |
503 | 1.56M | if (segments[seg_index - 1].op == gs_pe_lineto) { |
504 | 790k | if (segments[seg_index].vs[0].x == segments[seg_index - 1].vs[0].x && segments[seg_index].vs[0].x == line_start.x) { |
505 | 80.9k | if (segments[seg_index - 1].vs[0].y > line_start.y && segments[seg_index].vs[0].y >= segments[seg_index - 1].vs[0].y) { |
506 | 25.7k | segments[seg_index - 1].vs[0].y = segments[seg_index].vs[0].y; |
507 | 25.7k | seg_index--; |
508 | 55.1k | } else { |
509 | 55.1k | if (segments[seg_index - 1].vs[0].y < line_start.y && segments[seg_index].vs[0].y <= segments[seg_index - 1].vs[0].y) { |
510 | 25.6k | segments[seg_index - 1].vs[0].y = segments[seg_index].vs[0].y; |
511 | 25.6k | seg_index--; |
512 | 25.6k | } else |
513 | 29.5k | line_start = segments[seg_index - 1].vs[0]; |
514 | 55.1k | } |
515 | 709k | } else { |
516 | 709k | if (segments[seg_index].vs[0].y == segments[seg_index - 1].vs[0].y && segments[seg_index].vs[0].y == line_start.y) { |
517 | 7.12k | if (segments[seg_index - 1].vs[0].x > line_start.x && segments[seg_index].vs[0].x > segments[seg_index - 1].vs[0].x) { |
518 | 734 | segments[seg_index - 1].vs[0].x = segments[seg_index].vs[0].x; |
519 | 734 | seg_index--; |
520 | 6.39k | } else { |
521 | 6.39k | if (segments[seg_index - 1].vs[0].x < line_start.x && segments[seg_index].vs[0].x < segments[seg_index - 1].vs[0].x) { |
522 | 726 | segments[seg_index - 1].vs[0].x = segments[seg_index].vs[0].x; |
523 | 726 | seg_index--; |
524 | 726 | } else |
525 | 5.66k | line_start = segments[seg_index - 1].vs[0]; |
526 | 6.39k | } |
527 | 7.12k | } else |
528 | 702k | line_start = segments[seg_index - 1].vs[0]; |
529 | 709k | } |
530 | 790k | } |
531 | 1.56M | } |
532 | 1.56M | } |
533 | 2.97M | break; |
534 | 2.56M | case gs_pe_curveto: |
535 | 2.56M | if (stored_moveto) { |
536 | 69.3k | gdev_vector_dopath_segment(state, gs_pe_moveto, &line_start); |
537 | 69.3k | stored_moveto = false; |
538 | 69.3k | } |
539 | 5.38M | for (i=0;i<=seg_index;i++) { |
540 | 2.82M | gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs); |
541 | 2.82M | } |
542 | 2.56M | line_start = segments[seg_index].vs[2]; |
543 | 2.56M | seg_index = -1; |
544 | 2.56M | buffering = 0; |
545 | 2.56M | break; |
546 | 407k | case gs_pe_closepath: |
547 | 407k | if (!buffering || seg_index < 4) { |
548 | 143k | if (stored_moveto && ((type & gx_path_type_stroke) && !(type & gx_path_type_fill))) |
549 | 44 | gdev_vector_dopath_segment(state, gs_pe_moveto, &line_start); |
550 | 143k | stored_moveto = false; |
551 | 143k | if (!do_close) { |
552 | 102k | i = seg_index; |
553 | 103k | while (i > 0 && segments[i - 1].op == gs_pe_moveto) { |
554 | 1.14k | segments[i - 1] = segments[i]; |
555 | 1.14k | i--; |
556 | 1.14k | } |
557 | 102k | seg_index = i; |
558 | 250k | for (i=0;i<seg_index;i++) |
559 | 148k | gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs); |
560 | | |
561 | 102k | seg_index = 0; |
562 | 102k | if (is_clip_enum) |
563 | 0 | segments[seg_index].op = pe_op = gx_cpath_enum_next((gs_cpath_enum *)cenum, segments[seg_index].vs); |
564 | 102k | else |
565 | 102k | segments[seg_index].op = pe_op = gx_path_enum_next(cenum, segments[seg_index].vs); |
566 | | |
567 | 102k | if (pe_op > 0) { |
568 | 29.8k | gdev_vector_dopath_segment(state, gs_pe_closepath, segments[0].vs); |
569 | 29.8k | if (pe_op == gs_pe_moveto) { |
570 | 29.8k | if (matrix_optimisable) |
571 | 29.8k | buffering = 1; |
572 | 0 | else |
573 | 0 | buffering = 0; |
574 | 29.8k | seg_index = 0; |
575 | 29.8k | initial_m = 1; |
576 | 29.8k | line_start = segments[0].vs[0]; |
577 | 29.8k | } else { |
578 | 0 | gdev_vector_dopath_segment(state, segments[0].op, segments[0].vs); |
579 | 0 | buffering = 0; |
580 | 0 | seg_index = -1; |
581 | 0 | } |
582 | 29.8k | } |
583 | 102k | } else { |
584 | 108k | for (i=0;i<=seg_index;i++) |
585 | 68.1k | gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs); |
586 | 40.5k | if (matrix_optimisable) |
587 | 34.2k | buffering = 1; |
588 | 6.29k | else |
589 | 6.29k | buffering = 0; |
590 | 40.5k | seg_index = -1; |
591 | 40.5k | } |
592 | 264k | } else { |
593 | 264k | is_rect = 1; |
594 | 414k | for (i=1;i<seg_index;i++) { |
595 | 368k | if (segments[i - 1].vs[0].x != segments[i].vs[0].x) { |
596 | 282k | if (segments[i - 1].vs[0].y != segments[i].vs[0].y) { |
597 | 198k | is_rect = 0; |
598 | 198k | break; |
599 | 198k | } else { |
600 | 83.9k | if (segments[i].vs[0].x != segments[i + 1].vs[0].x || segments[i].vs[0].y == segments[i + 1].vs[0].x){ |
601 | 6.61k | is_rect = 0; |
602 | 6.61k | break; |
603 | 6.61k | } |
604 | 83.9k | } |
605 | 282k | } else { |
606 | 85.7k | if (segments[i - 1].vs[0].y == segments[i].vs[0].y) { |
607 | 4.48k | is_rect = 0; |
608 | 4.48k | break; |
609 | 81.2k | } else { |
610 | 81.2k | if (segments[i].vs[0].y != segments[i + 1].vs[0].y || segments[i].vs[0].x == segments[i + 1].vs[0].x){ |
611 | 9.06k | is_rect = 0; |
612 | 9.06k | break; |
613 | 9.06k | } |
614 | 81.2k | } |
615 | 85.7k | } |
616 | 368k | } |
617 | 264k | if (segments[0].vs[0].x != segments[seg_index].vs[0].x || segments[0].vs[0].y != segments[seg_index].vs[0].y) |
618 | 2.29k | is_rect = 0; |
619 | | |
620 | | /* If we would have to alter the starting point, and we are dashing a stroke, then don't treat |
621 | | * this as a rectangle. Changing the start vertex will alter the dash pattern. |
622 | | */ |
623 | 264k | if (segments[0].vs[0].x == segments[1].vs[0].x && (type & gx_path_type_dashed_stroke)) |
624 | 0 | is_rect = 0; |
625 | | |
626 | 264k | if (is_rect == 1) { |
627 | 45.6k | gs_fixed_point *pt = &segments[0].vs[0]; |
628 | 45.6k | fixed width, height; |
629 | | |
630 | 45.6k | if (segments[0].vs[0].x == segments[1].vs[0].x) { |
631 | 20.8k | pt = &segments[1].vs[0]; |
632 | 20.8k | width = segments[2].vs[0].x - segments[1].vs[0].x; |
633 | 20.8k | height = segments[0].vs[0].y - segments[1].vs[0].y; |
634 | 24.8k | } else { |
635 | 24.8k | width = segments[1].vs[0].x - segments[0].vs[0].x; |
636 | 24.8k | height = segments[2].vs[0].y - segments[1].vs[0].y; |
637 | 24.8k | } |
638 | | |
639 | 45.6k | gs_point_transform_inverse((double)pt->x, (double)pt->y, |
640 | 45.6k | &state->scale_mat, &p); |
641 | 45.6k | gs_point_transform_inverse((double)width, (double)height, |
642 | 45.6k | &state->scale_mat, &q); |
643 | 45.6k | code = vdev_proc(vdev, dorect)(vdev, (fixed)p.x, (fixed)p.y, |
644 | 45.6k | (fixed)p.x + (fixed)q.x, (fixed)p.y + (fixed)q.y, type); |
645 | 45.6k | if (code < 0) |
646 | 0 | return code; |
647 | 45.6k | seg_index = -1; |
648 | 219k | } else { |
649 | 1.31M | for (i=0;i<=seg_index;i++) { |
650 | 1.09M | gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs); |
651 | 1.09M | } |
652 | 219k | buffering = 0; |
653 | 219k | seg_index = -1; |
654 | 219k | } |
655 | 264k | } |
656 | 407k | break; |
657 | 848k | default: |
658 | 1.39M | for (i=0;i<seg_index;i++) |
659 | 549k | gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs); |
660 | 848k | if (stored_moveto && ((type & gx_path_type_stroke) && !(type & gx_path_type_fill))) |
661 | 536 | gdev_vector_dopath_segment(state, gs_pe_moveto, &line_start); |
662 | 848k | seg_index = -1; |
663 | 848k | buffering = 0; |
664 | 848k | break; |
665 | 7.98M | } |
666 | 7.98M | seg_index++; |
667 | 7.98M | if (seg_index > 4) { |
668 | 220k | for (i=0;i<seg_index;i++) { |
669 | 183k | gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs); |
670 | 183k | } |
671 | 36.7k | seg_index = 0; |
672 | 36.7k | buffering = 0; |
673 | 36.7k | } |
674 | 7.98M | } while (pe_op > 0); |
675 | | |
676 | 921k | if (pe_op < 0) |
677 | 0 | return pe_op; |
678 | | |
679 | 921k | code = vdev_proc(vdev, endpath)(vdev, type); |
680 | 921k | return (code < 0 ? code : segs); |
681 | 921k | } |
682 | | |
683 | | /* Put a single element of a clipping path list. */ |
684 | | static int |
685 | | pdf_put_clip_path_list_elem(gx_device_pdf * pdev, gx_cpath_path_list *e, |
686 | | gs_path_enum *cenum, gdev_vector_dopath_state_t *state, |
687 | | gs_fixed_point vs[3]) |
688 | 8.25k | { |
689 | 8.25k | int segments = 0; |
690 | | |
691 | | /* This function was previously recursive and reversed the order of subpaths. This |
692 | | * could lead to a C exec stack overflow on sufficiently complex clipping paths |
693 | | * (such as those produced by pdfwrite as a fallback for certain kinds of images). |
694 | | * Writing the subpaths in the forward order avoids the problem, is probably |
695 | | * slightly faster and uses less memory. Bug #706523. |
696 | | */ |
697 | 53.6k | while (e) { |
698 | 45.3k | segments = pdf_write_path(pdev, cenum, state, &e->path, 0, gx_path_type_clip | gx_path_type_optimize, NULL); |
699 | 45.3k | if (segments < 0) |
700 | 0 | return segments; |
701 | 45.3k | if (segments) |
702 | 45.3k | pprints1(pdev->strm, "%s n\n", (e->rule <= 0 ? "W" : "W*")); |
703 | 45.3k | e = e->next; |
704 | 45.3k | } |
705 | 8.25k | return 0; |
706 | 8.25k | } |
707 | | |
708 | | /* Put a clipping path on the output file. */ |
709 | | int |
710 | | pdf_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath) |
711 | 2.92M | { |
712 | 2.92M | int code; |
713 | 2.92M | stream *s = pdev->strm; |
714 | 2.92M | gs_id new_id; |
715 | | |
716 | | /* Check for no update needed. */ |
717 | 2.92M | if (pcpath == NULL) { |
718 | 977k | if (pdev->clip_path_id == pdev->no_clip_path_id) |
719 | 977k | return 0; |
720 | 11 | new_id = pdev->no_clip_path_id; |
721 | 1.95M | } else { |
722 | 1.95M | if (pdev->clip_path_id == pcpath->id) |
723 | 378k | return 0; |
724 | 1.57M | new_id = pcpath->id; |
725 | 1.57M | if (gx_cpath_includes_rectangle(pcpath, fixed_0, fixed_0, |
726 | 1.57M | int2fixed(pdev->width), |
727 | 1.57M | int2fixed(pdev->height)) |
728 | 1.57M | ) { |
729 | 1.50M | if (pdev->clip_path_id == pdev->no_clip_path_id) |
730 | 1.50M | return 0; |
731 | 13 | new_id = pdev->no_clip_path_id; |
732 | 13 | } |
733 | 71.2k | code = pdf_is_same_clip_path(pdev, pcpath); |
734 | 71.2k | if (code < 0) |
735 | 0 | return code; |
736 | 71.2k | if (code) { |
737 | 0 | pdev->clip_path_id = new_id; |
738 | 0 | return 0; |
739 | 0 | } |
740 | 71.2k | } |
741 | | /* |
742 | | * The contents must be open already, so the following will only exit |
743 | | * text or string context. |
744 | | */ |
745 | 71.2k | code = pdf_open_contents(pdev, PDF_IN_STREAM); |
746 | 71.2k | if (code < 0) |
747 | 0 | return code; |
748 | | /* Use Q to unwind the old clipping path. */ |
749 | 71.2k | if (pdev->vgstack_depth > pdev->vgstack_bottom) { |
750 | 159 | code = pdf_restore_viewer_state(pdev, s); |
751 | 159 | if (code < 0) |
752 | 0 | return code; |
753 | 159 | } |
754 | 71.2k | if (new_id != pdev->no_clip_path_id) { |
755 | 71.2k | gs_fixed_rect rect; |
756 | | |
757 | | /* Use q to allow the new clipping path to unwind. */ |
758 | 71.2k | code = pdf_save_viewer_state(pdev, s); |
759 | 71.2k | if (code < 0) |
760 | 0 | return code; |
761 | | /* path_valid states that the clip path is a simple path. If the clip is an intersection of |
762 | | * two paths, then path_valid is false. The problem is that the rectangle list is the |
763 | | * scan-converted result of the clip, and ths is at the device resolution. Its possible |
764 | | * that the intersection of the clips, at device resolution, is rectangular but the |
765 | | * two paths are not, and that at a different resolution, nor is the intersection. |
766 | | * So we *only* want to write a rectangle, if the clip is rectangular, and its the |
767 | | * result of a simple rectangle. Otherwise we want to write the paths that create |
768 | | * the clip. However, see below about the path_list. |
769 | | */ |
770 | 71.2k | if (pcpath->path_valid && cpath_is_rectangle(pcpath, &rect)) { |
771 | | /* Use unrounded coordinates. */ |
772 | 37.7k | pprintg4(s, "%g %g %g %g re", |
773 | 37.7k | fixed2float(rect.p.x), fixed2float(rect.p.y), |
774 | 37.7k | fixed2float(rect.q.x - rect.p.x), |
775 | 37.7k | fixed2float(rect.q.y - rect.p.y)); |
776 | 37.7k | pprints1(s, " %s n\n", (pcpath->rule <= 0 ? "W" : "W*")); |
777 | 37.7k | } else { |
778 | 33.5k | gdev_vector_dopath_state_t state; |
779 | 33.5k | gs_fixed_point vs[3]; |
780 | | |
781 | | /* the first comment below is (now) incorrect. Previously in gx_clip_to_rectangle() |
782 | | * we would create a rectangular clip, without using a path to do so. This results |
783 | | * in a rectangular clip, where path_valid is false. However, we did *not* clear |
784 | | * the path_list! So if there had previously been a clip path set, by setting paths, |
785 | | * we did not clear it. This is not sensible, and caused massive confusion for this code |
786 | | * so it has been altered to clear path_list, indicating that there is a clip, |
787 | | * the path is not valid, and that it was not created using arbitrary paths. |
788 | | * In this case we just emit the rectangle as well (there should be only one). |
789 | | */ |
790 | 33.5k | if (pcpath->path_list == NULL) { |
791 | | /* |
792 | | * We think this should be never executed. |
793 | | * This obsolete branch writes a clip path intersection |
794 | | * as a set of rectangles computed by |
795 | | * gx_cpath_intersect_path_slow. |
796 | | * Those rectangles use coordinates rounded to pixels, |
797 | | * therefore the precision may be unsatisfactory - |
798 | | * see Bug 688407. |
799 | | */ |
800 | 25.2k | gs_cpath_enum cenum; |
801 | | |
802 | | /* |
803 | | * We have to break 'const' here because the clip path |
804 | | * enumeration logic uses some internal mark bits. |
805 | | * This is very unfortunate, but until we can come up with |
806 | | * a better algorithm, it's necessary. |
807 | | */ |
808 | 25.2k | code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)pcpath, 1, gx_path_type_clip | gx_path_type_optimize, NULL); |
809 | 25.2k | if (code < 0) |
810 | 0 | return code; |
811 | 25.2k | pprints1(s, "%s n\n", (pcpath->rule <= 0 ? "W" : "W*")); |
812 | 25.2k | } else { |
813 | 8.25k | gs_path_enum cenum; |
814 | | |
815 | 8.25k | code = pdf_put_clip_path_list_elem(pdev, pcpath->path_list, &cenum, &state, vs); |
816 | 8.25k | if (code < 0) |
817 | 0 | return code; |
818 | 8.25k | } |
819 | 33.5k | } |
820 | 71.2k | } |
821 | 71.2k | pdev->clip_path_id = new_id; |
822 | 71.2k | return pdf_remember_clip_path(pdev, |
823 | 71.2k | (pdev->clip_path_id == pdev->no_clip_path_id ? NULL : pcpath)); |
824 | 71.2k | } |
825 | | |
826 | | /* |
827 | | * Compute the scaling to ensure that user coordinates for a path are within |
828 | | * PDF/A-1 valid range. Return true if scaling was needed. In this case, the |
829 | | * CTM will be multiplied by *pscale, and all coordinates will be divided by |
830 | | * *pscale. |
831 | | */ |
832 | | static bool |
833 | | make_rect_scaling(const gx_device_pdf *pdev, const gs_fixed_rect *bbox, |
834 | | double prescale, double *pscale) |
835 | 1.31M | { |
836 | 1.31M | double bmin, bmax; |
837 | | |
838 | 1.31M | if (pdev->PDFA != 1) { |
839 | 1.31M | *pscale = 1; |
840 | 1.31M | return false; |
841 | 1.31M | } |
842 | | |
843 | 0 | bmin = min(fixed2float(bbox->p.x) / pdev->scale.x, fixed2float(bbox->p.y) / pdev->scale.y) * prescale; |
844 | 0 | bmax = max(fixed2float(bbox->q.x) / pdev->scale.x, fixed2float(bbox->q.y) / pdev->scale.y) * prescale; |
845 | 0 | if (bmin <= int2fixed(-MAX_USER_COORD) || |
846 | 0 | bmax > int2fixed(MAX_USER_COORD) |
847 | 0 | ) { |
848 | | /* Rescale the path. */ |
849 | 0 | *pscale = max(bmin / int2fixed(-MAX_USER_COORD), |
850 | 0 | bmax / int2fixed(MAX_USER_COORD)); |
851 | 0 | return true; |
852 | 0 | } else { |
853 | 0 | *pscale = 1; |
854 | 0 | return false; |
855 | 0 | } |
856 | 0 | } |
857 | | |
858 | | /* |
859 | | * Prepare a fill with a color anc a clipping path. |
860 | | * Return 1 if there is nothing to paint. |
861 | | * Changes *box to the clipping box. |
862 | | */ |
863 | | static int |
864 | | prepare_fill_with_clip(gx_device_pdf *pdev, const gs_gstate * pgs, |
865 | | gs_fixed_rect *box, bool have_path, |
866 | | const gx_drawing_color * pdcolor, const gx_clip_path * pcpath) |
867 | 1.24M | { |
868 | 1.24M | bool new_clip; |
869 | 1.24M | int code; |
870 | | |
871 | | /* |
872 | | * Check for an empty clipping path. |
873 | | */ |
874 | 1.24M | if (pcpath) { |
875 | 1.23M | gs_fixed_rect cbox; |
876 | | |
877 | 1.23M | gx_cpath_outer_box(pcpath, &cbox); |
878 | 1.23M | if (cbox.p.x >= cbox.q.x || cbox.p.y >= cbox.q.y) |
879 | 23.3k | return 1; /* empty clipping path */ |
880 | 1.21M | *box = cbox; |
881 | 1.21M | } |
882 | 1.22M | code = pdf_check_soft_mask(pdev, (gs_gstate *)pgs); |
883 | 1.22M | if (code < 0) |
884 | 0 | return code; |
885 | | |
886 | 1.22M | new_clip = pdf_must_put_clip_path(pdev, pcpath); |
887 | 1.22M | if (have_path || pdev->context == PDF_IN_NONE || new_clip) { |
888 | 1.02M | if (new_clip) |
889 | 34.2k | code = pdf_unclip(pdev); |
890 | 991k | else |
891 | 991k | code = pdf_open_page(pdev, PDF_IN_STREAM); |
892 | 1.02M | if (code < 0) |
893 | 0 | return code; |
894 | 1.02M | } |
895 | 1.22M | code = pdf_prepare_fill(pdev, pgs, false); |
896 | 1.22M | if (code < 0) |
897 | 3.71k | return code; |
898 | 1.21M | return pdf_put_clip_path(pdev, pcpath); |
899 | 1.22M | } |
900 | | |
901 | | /* -------------A local image converter device. -----------------------------*/ |
902 | | |
903 | | public_st_pdf_lcvd_t(); |
904 | | |
905 | | static int |
906 | | lcvd_copy_color_shifted(gx_device * dev, |
907 | | const byte * base, int sourcex, int sraster, gx_bitmap_id id, |
908 | | int x, int y, int w, int h) |
909 | 0 | { |
910 | 0 | pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev; |
911 | 0 | int code; |
912 | 0 | int dw = cvd->mdev.width; |
913 | 0 | int dh = cvd->mdev.height; |
914 | |
|
915 | 0 | cvd->mdev.width -= cvd->mdev.mapped_x; |
916 | 0 | cvd->mdev.height -= cvd->mdev.mapped_y; |
917 | |
|
918 | 0 | code = cvd->std_copy_color((gx_device *)&cvd->mdev, base, sourcex, sraster, id, |
919 | 0 | x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, w, h); |
920 | |
|
921 | 0 | cvd->mdev.width = dw; |
922 | 0 | cvd->mdev.height = dh; |
923 | |
|
924 | 0 | return code; |
925 | 0 | } |
926 | | |
927 | | static int |
928 | | lcvd_copy_mono_shifted(gx_device * dev, |
929 | | const byte * base, int sourcex, int sraster, gx_bitmap_id id, |
930 | | int x, int y, int w, int h, gx_color_index zero, gx_color_index one) |
931 | 9.72k | { |
932 | 9.72k | pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev; |
933 | 9.72k | int code; |
934 | 9.72k | int dw = cvd->mdev.width; |
935 | 9.72k | int dh = cvd->mdev.height; |
936 | | |
937 | 9.72k | cvd->mdev.width -= cvd->mdev.mapped_x; |
938 | 9.72k | cvd->mdev.height -= cvd->mdev.mapped_y; |
939 | | |
940 | 9.72k | code = cvd->std_copy_mono((gx_device *)&cvd->mdev, base, sourcex, sraster, id, |
941 | 9.72k | x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, w, h, |
942 | 9.72k | zero, one); |
943 | | |
944 | 9.72k | cvd->mdev.width = dw; |
945 | 9.72k | cvd->mdev.height = dh; |
946 | | |
947 | 9.72k | return code; |
948 | 9.72k | } |
949 | | |
950 | | static int |
951 | | lcvd_fill_rectangle_shifted(gx_device *dev, int x, int y, int width, int height, gx_color_index color) |
952 | 27.4M | { |
953 | 27.4M | pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev; |
954 | 27.4M | int code; |
955 | 27.4M | int w = cvd->mdev.width; |
956 | 27.4M | int h = cvd->mdev.height; |
957 | | |
958 | 27.4M | if (cvd->pass == 2) |
959 | 5.84M | return 0; |
960 | | |
961 | 21.6M | cvd->mdev.width -= cvd->mdev.mapped_x; |
962 | 21.6M | cvd->mdev.height -= cvd->mdev.mapped_y; |
963 | | |
964 | 21.6M | code = cvd->std_fill_rectangle((gx_device *)&cvd->mdev, |
965 | 21.6M | x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, width, height, color); |
966 | | |
967 | 21.6M | cvd->mdev.width = w; |
968 | 21.6M | cvd->mdev.height = h; |
969 | | |
970 | 21.6M | return code; |
971 | 27.4M | } |
972 | | static int |
973 | | lcvd_fill_rectangle_shifted2(gx_device *dev, int x, int y, int width, int height, gx_color_index color) |
974 | 6.97M | { |
975 | 6.97M | pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev; |
976 | 6.97M | int code = 0; |
977 | | |
978 | 6.97M | if (cvd->pass != 1) |
979 | 888k | { |
980 | 888k | if (cvd->mask) { |
981 | 888k | code = (*dev_proc(cvd->mask, fill_rectangle))((gx_device *)cvd->mask, |
982 | 888k | x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, width, height, (gx_color_index)1); |
983 | 888k | if (code < 0) |
984 | 0 | goto fail; |
985 | 888k | } |
986 | 888k | } |
987 | 6.97M | if (cvd->pass != 2) |
988 | 6.27M | { |
989 | 6.27M | int w = cvd->mdev.width; |
990 | 6.27M | int h = cvd->mdev.height; |
991 | 6.27M | cvd->mdev.width -= cvd->mdev.mapped_x; |
992 | 6.27M | cvd->mdev.height -= cvd->mdev.mapped_y; |
993 | | |
994 | 6.27M | code = cvd->std_fill_rectangle((gx_device *)&cvd->mdev, |
995 | 6.27M | x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, width, height, color); |
996 | 6.27M | cvd->mdev.width = w; |
997 | 6.27M | cvd->mdev.height = h; |
998 | 6.27M | } |
999 | | |
1000 | 6.97M | fail: |
1001 | | |
1002 | 6.97M | return code; |
1003 | 6.97M | } |
1004 | | static void |
1005 | | lcvd_get_clipping_box_shifted_from_mdev(gx_device *dev, gs_fixed_rect *pbox) |
1006 | 6.17k | { |
1007 | 6.17k | fixed ofs; |
1008 | 6.17k | pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev; |
1009 | 6.17k | int w = cvd->mdev.width; |
1010 | 6.17k | int h = cvd->mdev.height; |
1011 | | |
1012 | 6.17k | cvd->mdev.width -= cvd->mdev.mapped_x; |
1013 | 6.17k | cvd->mdev.height -= cvd->mdev.mapped_y; |
1014 | | |
1015 | 6.17k | cvd->std_get_clipping_box((gx_device *)&cvd->mdev, pbox); |
1016 | 6.17k | cvd->mdev.width = w; |
1017 | 6.17k | cvd->mdev.height = h; |
1018 | 6.17k | ofs = int2fixed(cvd->mdev.mapped_x); |
1019 | 6.17k | pbox->p.x += ofs; |
1020 | 6.17k | pbox->q.x += ofs; |
1021 | 6.17k | ofs = int2fixed(cvd->mdev.mapped_y); |
1022 | 6.17k | pbox->p.y += ofs; |
1023 | 6.17k | pbox->q.y += ofs; |
1024 | 6.17k | } |
1025 | | static int |
1026 | | lcvd_dev_spec_op(gx_device *pdev1, int dev_spec_op, |
1027 | | void *data, int size) |
1028 | 29.9M | { |
1029 | 29.9M | pdf_lcvd_t *cvd = (pdf_lcvd_t *)pdev1; |
1030 | 29.9M | int code, w, h; |
1031 | | |
1032 | 29.9M | switch (dev_spec_op) { |
1033 | 1.82M | case gxdso_pattern_shading_area: |
1034 | 1.82M | return 1; /* Request shading area. */ |
1035 | 0 | case gxdso_pattern_can_accum: |
1036 | 0 | case gxdso_pattern_start_accum: |
1037 | 0 | case gxdso_pattern_finish_accum: |
1038 | 0 | case gxdso_pattern_load: |
1039 | 26 | case gxdso_pattern_is_cpath_accum: |
1040 | 26 | case gxdso_pattern_shfill_doesnt_need_path: |
1041 | 26 | case gxdso_pattern_handles_clip_path: |
1042 | 26 | case gxdso_copy_color_is_fast: |
1043 | 26 | return 0; |
1044 | 29.9M | } |
1045 | | |
1046 | 28.1M | w = cvd->mdev.width; |
1047 | 28.1M | h = cvd->mdev.height; |
1048 | 28.1M | cvd->mdev.width -= cvd->mdev.mapped_x; |
1049 | 28.1M | cvd->mdev.height -= cvd->mdev.mapped_y; |
1050 | | |
1051 | 28.1M | code = gx_default_dev_spec_op(pdev1, dev_spec_op, data, size); |
1052 | | |
1053 | 28.1M | cvd->mdev.width = w; |
1054 | 28.1M | cvd->mdev.height = h; |
1055 | | |
1056 | 28.1M | return code; |
1057 | 29.9M | } |
1058 | | static int |
1059 | | lcvd_close_device_with_writing(gx_device *pdev) |
1060 | 105 | { |
1061 | | /* Assuming 'mdev' is being closed before 'mask' - see gx_image3_end_image. */ |
1062 | 105 | pdf_lcvd_t *cvd = (pdf_lcvd_t *)pdev; |
1063 | 105 | int code, code1; |
1064 | | |
1065 | 105 | code = pdf_dump_converted_image(cvd->pdev, cvd, 0); |
1066 | 105 | code1 = cvd->std_close_device((gx_device *)&cvd->mdev); |
1067 | 105 | return code < 0 ? code : code1; |
1068 | 105 | } |
1069 | | |
1070 | | static int |
1071 | | write_image(gx_device_pdf *pdev, gx_device_memory *mdev, gs_matrix *m, int for_pattern) |
1072 | 2.37k | { |
1073 | 2.37k | gs_image_t image; |
1074 | 2.37k | pdf_image_writer writer; |
1075 | 2.37k | const int sourcex = 0; |
1076 | 2.37k | int code; |
1077 | | |
1078 | 2.37k | if (m != NULL) |
1079 | 0 | pdf_put_matrix(pdev, NULL, m, " cm\n"); |
1080 | 2.37k | code = pdf_copy_color_data(pdev, mdev->base, sourcex, |
1081 | 2.37k | mdev->raster, gx_no_bitmap_id, 0, 0, mdev->width, mdev->height, |
1082 | 2.37k | &image, &writer, for_pattern); |
1083 | 2.37k | if (code == 1) |
1084 | 2.37k | code = 0; /* Empty image. */ |
1085 | 0 | else if (code == 0) |
1086 | 0 | code = pdf_do_image(pdev, writer.pres, NULL, true); |
1087 | 2.37k | return code; |
1088 | 2.37k | } |
1089 | | static int |
1090 | | write_mask(gx_device_pdf *pdev, gx_device_memory *mdev, gs_matrix *m) |
1091 | 0 | { |
1092 | 0 | const int sourcex = 0; |
1093 | 0 | gs_id save_clip_id = pdev->clip_path_id; |
1094 | 0 | bool save_skip_color = pdev->skip_colors; |
1095 | 0 | int code; |
1096 | |
|
1097 | 0 | if (m != NULL) |
1098 | 0 | pdf_put_matrix(pdev, NULL, m, " cm\n"); |
1099 | 0 | pdev->clip_path_id = pdev->no_clip_path_id; |
1100 | 0 | pdev->skip_colors = true; |
1101 | 0 | code = gdev_pdf_copy_mono((gx_device *)pdev, mdev->base, sourcex, |
1102 | 0 | mdev->raster, gx_no_bitmap_id, 0, 0, mdev->width, mdev->height, |
1103 | 0 | gx_no_color_index, (gx_color_index)0); |
1104 | 0 | pdev->clip_path_id = save_clip_id; |
1105 | 0 | pdev->skip_colors = save_skip_color; |
1106 | 0 | return code; |
1107 | 0 | } |
1108 | | |
1109 | | static void |
1110 | | max_subimage_width(int width, byte *base, int x0, int64_t count1, int *x1, int64_t *count) |
1111 | 13.0k | { |
1112 | 13.0k | int64_t c = 0, c1 = count1 - 1; |
1113 | 13.0k | int x = x0; |
1114 | 13.0k | byte p = 1; /* The inverse of the previous bit. */ |
1115 | 13.0k | byte r; /* The inverse of the current bit. */ |
1116 | 13.0k | byte *q = base + (x / 8), m = 0x80 >> (x % 8); |
1117 | | |
1118 | 10.8M | for (; x < width; x++) { |
1119 | 10.8M | r = !(*q & m); |
1120 | 10.8M | if (p != r) { |
1121 | 135k | if (c >= c1) { |
1122 | 27 | if (!r) |
1123 | 16 | goto ex; /* stop before the upgrade. */ |
1124 | 27 | } |
1125 | 135k | c++; |
1126 | 135k | } |
1127 | 10.8M | p = r; |
1128 | 10.8M | m >>= 1; |
1129 | 10.8M | if (!m) { |
1130 | 1.35M | m = 0x80; |
1131 | 1.35M | q++; |
1132 | 1.35M | } |
1133 | 10.8M | } |
1134 | 13.0k | if (p) |
1135 | 10.2k | c++; /* Account the last downgrade. */ |
1136 | 13.0k | ex: |
1137 | 13.0k | *count = c; |
1138 | 13.0k | *x1 = x; |
1139 | 13.0k | } |
1140 | | |
1141 | | static int |
1142 | | cmpbits(const byte *base, const byte *base2, int w) |
1143 | 148k | { |
1144 | 148k | int code; |
1145 | | |
1146 | 148k | code = memcmp(base, base2, w>>3); |
1147 | 148k | if (code) |
1148 | 25.6k | return code; |
1149 | 122k | base += w>>3; |
1150 | 122k | base2 += w>>3; |
1151 | 122k | w &= 7; |
1152 | 122k | if (w == 0) |
1153 | 99.7k | return 0; |
1154 | 22.6k | return ((*base ^ *base2) & (0xff00>>w)); |
1155 | 122k | } |
1156 | | |
1157 | | static void |
1158 | | compute_subimage(int width, int height, int raster, byte *base, |
1159 | | int x0, int y0, int64_t MaxClipPathSize, int *x1, int *y1) |
1160 | 184 | { |
1161 | | /* Returns a semiopen range : [x0:x1)*[y0:y1). */ |
1162 | 184 | if (x0 != 0) { |
1163 | 0 | int64_t count; |
1164 | | |
1165 | | /* A partial single scanline. */ |
1166 | 0 | max_subimage_width(width, base + y0 * raster, x0, MaxClipPathSize / 4, x1, &count); |
1167 | 0 | *y1 = y0; |
1168 | 184 | } else { |
1169 | 184 | int xx, y = y0, yy; |
1170 | 184 | int64_t count, count1 = MaxClipPathSize / 4; |
1171 | | |
1172 | 13.2k | for(; y < height && count1 > 0; ) { |
1173 | 13.0k | max_subimage_width(width, base + y * raster, 0, count1, &xx, &count); |
1174 | 13.0k | if (xx < width) { |
1175 | 16 | if (y == y0) { |
1176 | | /* Partial single scanline. */ |
1177 | 0 | *y1 = y + 1; |
1178 | 0 | *x1 = xx; |
1179 | 0 | return; |
1180 | 16 | } else { |
1181 | | /* Full lines before this scanline. */ |
1182 | 16 | break; |
1183 | 16 | } |
1184 | 16 | } |
1185 | 13.0k | count1 -= count; |
1186 | 13.0k | yy = y + 1; |
1187 | 74.2k | for (; yy < height; yy++) |
1188 | 74.0k | if (cmpbits(base + raster * y, base + raster * yy, width)) |
1189 | 12.8k | break; |
1190 | 13.0k | y = yy; |
1191 | | |
1192 | 13.0k | } |
1193 | 184 | *y1 = y; |
1194 | 184 | *x1 = width; |
1195 | 184 | } |
1196 | 184 | } |
1197 | | |
1198 | | static int |
1199 | | image_line_to_clip(gx_device_pdf *pdev, byte *base, int x0, int x1, int y0, int y1, bool started) |
1200 | 13.0k | { /* returns the number of segments or error code. */ |
1201 | 13.0k | int x = x0, xx; |
1202 | 13.0k | byte *q = base + (x / 8), m = 0x80 >> (x % 8); |
1203 | 13.0k | int64_t c = 0; |
1204 | | |
1205 | 82.1k | for (;;) { |
1206 | | /* Look for upgrade : */ |
1207 | 7.92M | for (; x < x1; x++) { |
1208 | 7.91M | if (*q & m) |
1209 | 69.0k | break; |
1210 | 7.84M | m >>= 1; |
1211 | 7.84M | if (!m) { |
1212 | 980k | m = 0x80; |
1213 | 980k | q++; |
1214 | 980k | } |
1215 | 7.84M | } |
1216 | 82.1k | if (x == x1) |
1217 | 13.0k | return c; |
1218 | 69.0k | xx = x; |
1219 | | /* Look for downgrade : */ |
1220 | 3.06M | for (; x < x1; x++) { |
1221 | 3.06M | if (!(*q & m)) |
1222 | 66.2k | break; |
1223 | 2.99M | m >>= 1; |
1224 | 2.99M | if (!m) { |
1225 | 372k | m = 0x80; |
1226 | 372k | q++; |
1227 | 372k | } |
1228 | 2.99M | } |
1229 | | /* Found the interval [xx:x). */ |
1230 | 69.0k | if (!started) { |
1231 | 169 | stream_puts(pdev->strm, "n\n"); |
1232 | 169 | started = true; |
1233 | 169 | } |
1234 | 69.0k | pprintld2(pdev->strm, "%ld %ld m ", xx, y0); |
1235 | 69.0k | pprintld2(pdev->strm, "%ld %ld l ", x, y0); |
1236 | 69.0k | pprintld2(pdev->strm, "%ld %ld l ", x, y1); |
1237 | 69.0k | pprintld2(pdev->strm, "%ld %ld l h\n", xx, y1); |
1238 | 69.0k | c += 4; |
1239 | 69.0k | } |
1240 | 0 | return c; |
1241 | 13.0k | } |
1242 | | |
1243 | | static int |
1244 | | mask_to_clip(gx_device_pdf *pdev, int width, int height, |
1245 | | int raster, byte *base, int x0, int y0, int x1, int y1) |
1246 | 184 | { |
1247 | 184 | int y, yy, code = 0; |
1248 | 184 | bool has_segments = false; |
1249 | | |
1250 | 13.2k | for (y = y0; y < y1 && code >= 0;) { |
1251 | 13.0k | yy = y + 1; |
1252 | 13.0k | if (x0 == 0) { |
1253 | 74.2k | for (; yy < y1; yy++) |
1254 | 74.0k | if (cmpbits(base + raster * y, base + raster * yy, width)) |
1255 | 12.8k | break; |
1256 | 13.0k | } |
1257 | 13.0k | code = image_line_to_clip(pdev, base + raster * y, x0, x1, y, yy, has_segments); |
1258 | 13.0k | if (code > 0) |
1259 | 12.7k | has_segments = true; |
1260 | 13.0k | y = yy; |
1261 | 13.0k | } |
1262 | 184 | if (has_segments) |
1263 | 169 | stream_puts(pdev->strm, "W n\n"); |
1264 | 184 | return code < 0 ? code : has_segments ? 1 : 0; |
1265 | 184 | } |
1266 | | |
1267 | | static int |
1268 | | write_subimage(gx_device_pdf *pdev, gx_device_memory *mdev, int x, int y, int x1, int y1, int for_pattern) |
1269 | 169 | { |
1270 | 169 | gs_image_t image; |
1271 | 169 | pdf_image_writer writer; |
1272 | | /* expand in 1 pixel to provide a proper color interpolation */ |
1273 | 169 | int X = max(0, x - 1); |
1274 | 169 | int Y = max(0, y - 1); |
1275 | 169 | int X1 = min(mdev->width, x1 + 1); |
1276 | 169 | int Y1 = min(mdev->height, y1 + 1); |
1277 | 169 | int code; |
1278 | | |
1279 | 169 | code = pdf_copy_color_data(pdev, mdev->base + mdev->raster * Y, X, |
1280 | 169 | mdev->raster, gx_no_bitmap_id, |
1281 | 169 | X, Y, X1 - X, Y1 - Y, |
1282 | 169 | &image, &writer, for_pattern); |
1283 | 169 | if (code < 0) |
1284 | 0 | return code; |
1285 | 169 | if (!writer.pres) |
1286 | 169 | return 0; /* inline image. */ |
1287 | 0 | return pdf_do_image(pdev, writer.pres, NULL, true); |
1288 | 169 | } |
1289 | | |
1290 | | static int |
1291 | | write_image_with_clip(gx_device_pdf *pdev, pdf_lcvd_t *cvd, int for_pattern) |
1292 | 155 | { |
1293 | 155 | int x = 0, y = 0; |
1294 | 155 | int code, code1; |
1295 | | |
1296 | 155 | if (cvd->write_matrix) |
1297 | 131 | pdf_put_matrix(pdev, NULL, &cvd->m, " cm q\n"); |
1298 | 184 | for(;;) { |
1299 | 184 | int x1, y1; |
1300 | | |
1301 | 184 | compute_subimage(cvd->mask->width, cvd->mask->height, |
1302 | 184 | cvd->mask->raster, cvd->mask->base, |
1303 | 184 | x, y, max(pdev->MaxClipPathSize, 100), &x1, &y1); |
1304 | 184 | code = mask_to_clip(pdev, |
1305 | 184 | cvd->mask->width, cvd->mask->height, |
1306 | 184 | cvd->mask->raster, cvd->mask->base, |
1307 | 184 | x, y, x1, y1); |
1308 | 184 | if (code < 0) |
1309 | 0 | return code; |
1310 | 184 | if (code > 0) { |
1311 | 169 | code1 = write_subimage(pdev, &cvd->mdev, x, y, x1, y1, for_pattern); |
1312 | 169 | if (code1 < 0) |
1313 | 0 | return code1; |
1314 | 169 | } |
1315 | 184 | if (x1 >= cvd->mdev.width && y1 >= cvd->mdev.height) |
1316 | 155 | break; |
1317 | 29 | if (code > 0) |
1318 | 29 | stream_puts(pdev->strm, "Q q\n"); |
1319 | 29 | if (x1 == cvd->mask->width) { |
1320 | 29 | x = 0; |
1321 | 29 | y = y1; |
1322 | 29 | } else { |
1323 | 0 | x = x1; |
1324 | 0 | y = y1; |
1325 | 0 | } |
1326 | 29 | } |
1327 | 155 | if (cvd->write_matrix) |
1328 | 131 | stream_puts(pdev->strm, "Q\n"); |
1329 | 155 | return 0; |
1330 | 155 | } |
1331 | | |
1332 | | int |
1333 | | pdf_dump_converted_image(gx_device_pdf *pdev, pdf_lcvd_t *cvd, int for_pattern) |
1334 | 3.03k | { |
1335 | 3.03k | int code = 0; |
1336 | | |
1337 | 3.03k | if (cvd->pass == 1) |
1338 | 0 | return 0; |
1339 | | |
1340 | 3.03k | cvd->mdev.width -= cvd->mdev.mapped_x; |
1341 | 3.03k | cvd->mdev.height -= cvd->mdev.mapped_y; |
1342 | | |
1343 | 3.03k | if (!cvd->path_is_empty || cvd->has_background) { |
1344 | 2.37k | if (!cvd->has_background) |
1345 | 2.36k | stream_puts(pdev->strm, "W n\n"); |
1346 | 2.37k | code = write_image(pdev, &cvd->mdev, (cvd->write_matrix ? &cvd->m : NULL), for_pattern); |
1347 | 2.37k | cvd->path_is_empty = true; |
1348 | 2.37k | } else if (!cvd->mask_is_empty && pdev->PatternImagemask) { |
1349 | | /* Convert to imagemask with a pattern color. */ |
1350 | | /* See also use_image_as_pattern in gdevpdfi.c . */ |
1351 | 0 | gs_gstate s; |
1352 | 0 | gs_pattern1_instance_t inst; |
1353 | 0 | gs_id id = gs_next_ids(cvd->mdev.memory, 1); |
1354 | 0 | cos_value_t v; |
1355 | 0 | const pdf_resource_t *pres; |
1356 | |
|
1357 | 0 | memset(&s, 0, sizeof(s)); |
1358 | 0 | s.ctm.xx = cvd->m.xx; |
1359 | 0 | s.ctm.xy = cvd->m.xy; |
1360 | 0 | s.ctm.yx = cvd->m.yx; |
1361 | 0 | s.ctm.yy = cvd->m.yy; |
1362 | 0 | s.ctm.tx = cvd->m.tx; |
1363 | 0 | s.ctm.ty = cvd->m.ty; |
1364 | 0 | memset(&inst, 0, sizeof(inst)); |
1365 | 0 | inst.saved = (gs_gstate *)&s; /* HACK : will use s.ctm only. */ |
1366 | 0 | inst.templat.PaintType = 1; |
1367 | 0 | inst.templat.TilingType = 1; |
1368 | 0 | inst.templat.BBox.p.x = inst.templat.BBox.p.y = 0; |
1369 | 0 | inst.templat.BBox.q.x = cvd->mdev.width; |
1370 | 0 | inst.templat.BBox.q.y = cvd->mdev.height; |
1371 | 0 | inst.templat.XStep = (float)cvd->mdev.width; |
1372 | 0 | inst.templat.YStep = (float)cvd->mdev.height; |
1373 | |
|
1374 | 0 | { |
1375 | 0 | pattern_accum_param_s param; |
1376 | 0 | param.pinst = (void *)&inst; |
1377 | 0 | param.graphics_state = (void *)&s; |
1378 | 0 | param.pinst_id = inst.id; |
1379 | |
|
1380 | 0 | code = (*dev_proc(pdev, dev_spec_op))((gx_device *)pdev, |
1381 | 0 | gxdso_pattern_start_accum, ¶m, sizeof(pattern_accum_param_s)); |
1382 | 0 | } |
1383 | |
|
1384 | 0 | if (code >= 0) { |
1385 | 0 | stream_puts(pdev->strm, "W n\n"); |
1386 | 0 | code = write_image(pdev, &cvd->mdev, NULL, for_pattern); |
1387 | 0 | } |
1388 | 0 | pres = pdev->accumulating_substream_resource; |
1389 | 0 | if (pres == NULL) |
1390 | 0 | code = gs_note_error(gs_error_unregistered); |
1391 | 0 | if (code >= 0) { |
1392 | 0 | pattern_accum_param_s param; |
1393 | 0 | param.pinst = (void *)&inst; |
1394 | 0 | param.graphics_state = (void *)&s; |
1395 | 0 | param.pinst_id = inst.id; |
1396 | |
|
1397 | 0 | code = (*dev_proc(pdev, dev_spec_op))((gx_device *)pdev, |
1398 | 0 | gxdso_pattern_finish_accum, ¶m, id); |
1399 | 0 | } |
1400 | 0 | if (code >= 0) |
1401 | 0 | code = (*dev_proc(pdev, dev_spec_op))((gx_device *)pdev, |
1402 | 0 | gxdso_pattern_load, &id, sizeof(gs_id)); |
1403 | 0 | if (code >= 0) |
1404 | 0 | code = pdf_cs_Pattern_colored(pdev, &v); |
1405 | 0 | if (code >= 0) { |
1406 | 0 | cos_value_write(&v, pdev); |
1407 | 0 | pprinti64d1(pdev->strm, " cs /R%"PRId64" scn ", pdf_resource_id(pres)); |
1408 | 0 | } |
1409 | 0 | if (code >= 0) |
1410 | 0 | code = write_mask(pdev, cvd->mask, (cvd->write_matrix ? &cvd->m : NULL)); |
1411 | 0 | cvd->mask_is_empty = true; |
1412 | 662 | } else if (!cvd->mask_is_empty && !pdev->PatternImagemask) { |
1413 | | /* Convert to image with a clipping path. */ |
1414 | 155 | stream_puts(pdev->strm, "q\n"); |
1415 | 155 | code = write_image_with_clip(pdev, cvd, for_pattern); |
1416 | 155 | stream_puts(pdev->strm, "Q\n"); |
1417 | 507 | } else if (cvd->filled_trap){ |
1418 | 0 | stream_puts(pdev->strm, "q\n"); |
1419 | 0 | code = write_image_with_clip(pdev, cvd, for_pattern); |
1420 | 0 | stream_puts(pdev->strm, "Q\n"); |
1421 | 0 | } |
1422 | 3.03k | cvd->filled_trap = false; |
1423 | 3.03k | cvd->mdev.width += cvd->mdev.mapped_x; |
1424 | 3.03k | cvd->mdev.height += cvd->mdev.mapped_y; |
1425 | 3.03k | if (code > 0) |
1426 | 0 | code = (*dev_proc(&cvd->mdev, fill_rectangle))((gx_device *)&cvd->mdev, |
1427 | 0 | 0, 0, cvd->mdev.width, cvd->mdev.height, (gx_color_index)0); |
1428 | | |
1429 | 3.03k | return code; |
1430 | 3.03k | } |
1431 | | static int |
1432 | | lcvd_fill_trapezoid(gx_device * dev, const gs_fixed_edge * left, |
1433 | | const gs_fixed_edge * right, fixed ybot, fixed ytop, bool swap_axes, |
1434 | | const gx_device_color * pdevc, gs_logical_operation_t lop) |
1435 | 372k | { |
1436 | 372k | pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev; |
1437 | | |
1438 | 372k | if (cvd->mask != NULL) |
1439 | 118k | cvd->filled_trap = true; |
1440 | 372k | return gx_default_fill_trapezoid(dev, left, right, ybot, ytop, swap_axes, pdevc, lop); |
1441 | 372k | } |
1442 | | |
1443 | | static int |
1444 | | lcvd_handle_fill_path_as_shading_coverage(gx_device *dev, |
1445 | | const gs_gstate *pgs, gx_path *ppath, |
1446 | | const gx_fill_params *params, |
1447 | | const gx_drawing_color *pdcolor, const gx_clip_path *pcpath) |
1448 | 1.82M | { |
1449 | 1.82M | pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev; |
1450 | 1.82M | gx_device_pdf *pdev = (gx_device_pdf *)cvd->mdev.target; |
1451 | 1.82M | int code; |
1452 | | |
1453 | 1.82M | if (cvd->pass == 1) |
1454 | 1.82M | return 0; |
1455 | 6.96k | if (cvd->has_background) |
1456 | 72 | return 0; |
1457 | 6.89k | if (gx_path_is_null(ppath)) { |
1458 | | /* use the mask. */ |
1459 | 2.14k | if (!cvd->path_is_empty) { |
1460 | 20 | code = pdf_dump_converted_image(pdev, cvd, 2); |
1461 | 20 | if (code < 0) |
1462 | 0 | return code; |
1463 | 20 | stream_puts(pdev->strm, "Q q\n"); |
1464 | 20 | dev_proc(&cvd->mdev, fill_rectangle) = lcvd_fill_rectangle_shifted2; |
1465 | 20 | } |
1466 | 2.14k | if (cvd->mask && (!cvd->mask_is_clean || !cvd->path_is_empty)) { |
1467 | 11 | code = (*dev_proc(cvd->mask, fill_rectangle))((gx_device *)cvd->mask, |
1468 | 11 | 0, 0, cvd->mask->width, cvd->mask->height, (gx_color_index)0); |
1469 | 11 | if (code < 0) |
1470 | 0 | return code; |
1471 | 11 | cvd->mask_is_clean = true; |
1472 | 11 | } |
1473 | 2.14k | cvd->path_is_empty = true; |
1474 | 2.14k | if (cvd->mask) |
1475 | 2.14k | cvd->mask_is_empty = false; |
1476 | 4.75k | } else { |
1477 | 4.75k | gs_matrix m; |
1478 | 4.75k | gs_path_enum cenum; |
1479 | 4.75k | gdev_vector_dopath_state_t state; |
1480 | | |
1481 | 4.75k | gs_make_translation(cvd->path_offset.x, cvd->path_offset.y, &m); |
1482 | | /* use the clipping. */ |
1483 | 4.75k | if (!cvd->mask_is_empty) { |
1484 | 19 | code = pdf_dump_converted_image(pdev, cvd, 2); |
1485 | 19 | if (code < 0) |
1486 | 0 | return code; |
1487 | 19 | stream_puts(pdev->strm, "Q q\n"); |
1488 | 19 | dev_proc(&cvd->mdev, fill_rectangle) = lcvd_fill_rectangle_shifted; |
1489 | 19 | cvd->mask_is_empty = true; |
1490 | 19 | } |
1491 | 4.75k | code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_fill | gx_path_type_optimize, &m); |
1492 | 4.75k | if (code < 0) |
1493 | 0 | return code; |
1494 | 4.75k | stream_puts(pdev->strm, "h\n"); |
1495 | 4.75k | cvd->path_is_empty = false; |
1496 | 4.75k | } |
1497 | 6.89k | return 0; |
1498 | 6.89k | } |
1499 | | |
1500 | | static int |
1501 | | lcvd_transform_pixel_region(gx_device *dev, transform_pixel_region_reason reason, transform_pixel_region_data *data) |
1502 | 16.6k | { |
1503 | 16.6k | transform_pixel_region_data local_data; |
1504 | 16.6k | gx_dda_fixed_point local_pixels, local_rows; |
1505 | 16.6k | gs_int_rect local_clip; |
1506 | 16.6k | pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev; |
1507 | 16.6k | int ret; |
1508 | 16.6k | dev_t_proc_fill_rectangle((*fill_rectangle), gx_device); |
1509 | 16.6k | dev_t_proc_copy_color((*copy_color), gx_device); |
1510 | 16.6k | int w = cvd->mdev.width; |
1511 | 16.6k | int h = cvd->mdev.height; |
1512 | | |
1513 | 16.6k | cvd->mdev.width -= cvd->mdev.mapped_x; |
1514 | 16.6k | cvd->mdev.height -= cvd->mdev.mapped_y; |
1515 | | |
1516 | 16.6k | if (reason == transform_pixel_region_begin) { |
1517 | 40 | local_data = *data; |
1518 | 40 | local_pixels = *local_data.u.init.pixels; |
1519 | 40 | local_rows = *local_data.u.init.rows; |
1520 | 40 | local_clip = *local_data.u.init.clip; |
1521 | 40 | local_data.u.init.pixels = &local_pixels; |
1522 | 40 | local_data.u.init.rows = &local_rows; |
1523 | 40 | local_data.u.init.clip = &local_clip; |
1524 | 40 | local_pixels.x.state.Q -= int2fixed(cvd->mdev.mapped_x); |
1525 | 40 | local_pixels.y.state.Q -= int2fixed(cvd->mdev.mapped_y); |
1526 | 40 | local_rows.x.state.Q -= int2fixed(cvd->mdev.mapped_x); |
1527 | 40 | local_rows.y.state.Q -= int2fixed(cvd->mdev.mapped_y); |
1528 | 40 | local_clip.p.x -= cvd->mdev.mapped_x; |
1529 | 40 | local_clip.p.y -= cvd->mdev.mapped_y; |
1530 | 40 | local_clip.q.x -= cvd->mdev.mapped_x; |
1531 | 40 | local_clip.q.y -= cvd->mdev.mapped_y; |
1532 | 40 | ret = cvd->std_transform_pixel_region(dev, reason, &local_data); |
1533 | 40 | data->state = local_data.state; |
1534 | 40 | cvd->mdev.width = w; |
1535 | 40 | cvd->mdev.height = h; |
1536 | 40 | return ret; |
1537 | 40 | } |
1538 | 16.6k | copy_color = dev_proc(&cvd->mdev, copy_color); |
1539 | 16.6k | fill_rectangle = dev_proc(&cvd->mdev, fill_rectangle); |
1540 | 16.6k | dev_proc(&cvd->mdev, copy_color) = cvd->std_copy_color; |
1541 | 16.6k | dev_proc(&cvd->mdev, fill_rectangle) = cvd->std_fill_rectangle; |
1542 | 16.6k | ret = cvd->std_transform_pixel_region(dev, reason, data); |
1543 | 16.6k | dev_proc(&cvd->mdev, copy_color) = copy_color; |
1544 | 16.6k | dev_proc(&cvd->mdev, fill_rectangle) = fill_rectangle; |
1545 | 16.6k | cvd->mdev.width = w; |
1546 | 16.6k | cvd->mdev.height = h; |
1547 | 16.6k | return ret; |
1548 | 16.6k | } |
1549 | | |
1550 | | int |
1551 | | pdf_setup_masked_image_converter(gx_device_pdf *pdev, gs_memory_t *mem, const gs_matrix *m, pdf_lcvd_t **pcvd, |
1552 | | bool need_mask, int x, int y, int w, int h, bool write_on_close) |
1553 | 3.31k | { |
1554 | 3.31k | int code; |
1555 | 3.31k | gx_device_memory *mask = 0; |
1556 | 3.31k | pdf_lcvd_t *cvd = *pcvd; |
1557 | | |
1558 | 3.31k | if (cvd == NULL) { |
1559 | 131 | cvd = gs_alloc_struct(mem, pdf_lcvd_t, &st_pdf_lcvd_t, "pdf_setup_masked_image_converter"); |
1560 | 131 | if (cvd == NULL) |
1561 | 0 | return_error(gs_error_VMerror); |
1562 | 131 | *pcvd = cvd; |
1563 | 131 | } |
1564 | 3.31k | cvd->pdev = pdev; |
1565 | 3.31k | gs_make_mem_device(&cvd->mdev, gdev_mem_device_for_bits(pdev->color_info.depth), |
1566 | 3.31k | mem, 0, (gx_device *)pdev); |
1567 | | /* x and y are always non-negative here. */ |
1568 | 3.31k | if (x < 0 || y < 0) |
1569 | 0 | return_error(gs_error_Fatal); |
1570 | 3.31k | cvd->mdev.width = w; /* The size we want to allocate for */ |
1571 | 3.31k | cvd->mdev.height = h; |
1572 | 3.31k | cvd->mdev.mapped_x = x; |
1573 | 3.31k | cvd->mdev.mapped_y = y; |
1574 | 3.31k | cvd->mdev.bitmap_memory = mem; |
1575 | 3.31k | cvd->mdev.color_info = pdev->color_info; |
1576 | 3.31k | cvd->path_is_empty = true; |
1577 | 3.31k | cvd->mask_is_empty = true; |
1578 | 3.31k | cvd->mask_is_clean = false; |
1579 | 3.31k | cvd->filled_trap = false; |
1580 | 3.31k | cvd->has_background = false; |
1581 | 3.31k | cvd->pass = 0; |
1582 | 3.31k | cvd->mask = 0; |
1583 | 3.31k | cvd->write_matrix = true; |
1584 | 3.31k | code = (*dev_proc(&cvd->mdev, open_device))((gx_device *)&cvd->mdev); |
1585 | 3.31k | if (code < 0) |
1586 | 0 | return code; /* FIXME: free cvd? */ |
1587 | 3.31k | code = (*dev_proc(&cvd->mdev, fill_rectangle))((gx_device *)&cvd->mdev, |
1588 | 3.31k | 0, 0, cvd->mdev.width, cvd->mdev.height, (gx_color_index)0); |
1589 | 3.31k | if (code < 0) |
1590 | 0 | return code; /* FIXME: free cvd? */ |
1591 | 3.31k | if (need_mask) { |
1592 | 304 | mask = gs_alloc_struct_immovable(mem, gx_device_memory, &st_device_memory, "pdf_setup_masked_image_converter"); |
1593 | 304 | if (mask == NULL) |
1594 | 0 | return_error(gs_error_VMerror); /* FIXME: free cvd? */ |
1595 | 304 | cvd->mask = mask; |
1596 | 304 | gs_make_mem_mono_device(mask, mem, (gx_device *)pdev); |
1597 | 304 | mask->width = cvd->mdev.width; |
1598 | 304 | mask->height = cvd->mdev.height; |
1599 | 304 | mask->raster = gx_device_raster((gx_device *)mask, 1); |
1600 | 304 | mask->bitmap_memory = mem; |
1601 | 304 | code = (*dev_proc(mask, open_device))((gx_device *)mask); |
1602 | 304 | if (code < 0) |
1603 | 0 | return code; /* FIXME: free cvd? */ |
1604 | 304 | if (write_on_close) { |
1605 | 105 | code = (*dev_proc(mask, fill_rectangle))((gx_device *)mask, |
1606 | 105 | 0, 0, mask->width, mask->height, (gx_color_index)0); |
1607 | 105 | if (code < 0) |
1608 | 0 | return code; /* FIXME: free cvd? */ |
1609 | 105 | } |
1610 | 304 | } |
1611 | 3.31k | cvd->std_copy_color = dev_proc(&cvd->mdev, copy_color); |
1612 | 3.31k | cvd->std_copy_mono = dev_proc(&cvd->mdev, copy_mono); |
1613 | 3.31k | cvd->std_fill_rectangle = dev_proc(&cvd->mdev, fill_rectangle); |
1614 | 3.31k | cvd->std_close_device = dev_proc(&cvd->mdev, close_device); |
1615 | 3.31k | cvd->std_get_clipping_box = dev_proc(&cvd->mdev, get_clipping_box); |
1616 | 3.31k | cvd->std_transform_pixel_region = dev_proc(&cvd->mdev, transform_pixel_region); |
1617 | 3.31k | if (!write_on_close) { |
1618 | | /* Type 3 images will write to the mask directly. */ |
1619 | 3.21k | dev_proc(&cvd->mdev, fill_rectangle) = (need_mask ? lcvd_fill_rectangle_shifted2 |
1620 | 3.21k | : lcvd_fill_rectangle_shifted); |
1621 | 3.21k | } else { |
1622 | 105 | dev_proc(&cvd->mdev, fill_rectangle) = lcvd_fill_rectangle_shifted; |
1623 | 105 | } |
1624 | 3.31k | dev_proc(&cvd->mdev, get_clipping_box) = lcvd_get_clipping_box_shifted_from_mdev; |
1625 | 3.31k | dev_proc(&cvd->mdev, copy_color) = lcvd_copy_color_shifted; |
1626 | 3.31k | dev_proc(&cvd->mdev, copy_mono) = lcvd_copy_mono_shifted; |
1627 | 3.31k | dev_proc(&cvd->mdev, dev_spec_op) = lcvd_dev_spec_op; |
1628 | 3.31k | dev_proc(&cvd->mdev, fill_path) = lcvd_handle_fill_path_as_shading_coverage; |
1629 | 3.31k | dev_proc(&cvd->mdev, transform_pixel_region) = lcvd_transform_pixel_region; |
1630 | 3.31k | dev_proc(&cvd->mdev, fill_trapezoid) = lcvd_fill_trapezoid; |
1631 | 3.31k | cvd->m = *m; |
1632 | 3.31k | if (write_on_close) { |
1633 | 105 | cvd->mdev.is_open = true; |
1634 | 105 | if (mask) |
1635 | 105 | mask->is_open = true; |
1636 | 105 | dev_proc(&cvd->mdev, close_device) = lcvd_close_device_with_writing; |
1637 | 105 | } |
1638 | 3.31k | cvd->mdev.width = w + x; /* The size we appear to the world as */ |
1639 | 3.31k | cvd->mdev.height = h + y; |
1640 | 3.31k | return 0; |
1641 | 3.31k | } |
1642 | | |
1643 | | void |
1644 | | pdf_remove_masked_image_converter(gx_device_pdf *pdev, pdf_lcvd_t *cvd, bool need_mask) |
1645 | 3.18k | { |
1646 | 3.18k | cvd->mdev.width -= cvd->mdev.mapped_x; |
1647 | 3.18k | cvd->mdev.height -= cvd->mdev.mapped_y; |
1648 | 3.18k | (*dev_proc(&cvd->mdev, close_device))((gx_device *)&cvd->mdev); |
1649 | 3.18k | if (cvd->mask) { |
1650 | 173 | (*dev_proc(cvd->mask, close_device))((gx_device *)cvd->mask); |
1651 | 173 | gs_free_object(cvd->mask->memory, cvd->mask, "pdf_remove_masked_image_converter"); |
1652 | 173 | } |
1653 | 3.18k | } |
1654 | | |
1655 | | /* ------ Driver procedures ------ */ |
1656 | | |
1657 | | /* Fill a path. */ |
1658 | | int |
1659 | | gdev_pdf_fill_path(gx_device * dev, const gs_gstate * pgs, gx_path * ppath, |
1660 | | const gx_fill_params * params, |
1661 | | const gx_drawing_color * pdcolor, const gx_clip_path * pcpath) |
1662 | 3.38M | { |
1663 | 3.38M | gx_device_pdf *pdev = (gx_device_pdf *) dev; |
1664 | 3.38M | int code; |
1665 | | /* |
1666 | | * HACK: we fill an empty path in order to set the clipping path |
1667 | | * and the color for writing text. If it weren't for this, we |
1668 | | * could detect and skip empty paths before putting out the clip |
1669 | | * path or the color. We also clip with an empty path in order |
1670 | | * to advance currentpoint for show operations without actually |
1671 | | * drawing anything. |
1672 | | */ |
1673 | 3.38M | bool have_path; |
1674 | 3.38M | gs_fixed_rect box = {{0, 0}, {0, 0}}, box1; |
1675 | 3.38M | gs_rect box2; |
1676 | | |
1677 | 3.38M | if (pdev->Eps2Write) { |
1678 | 2.38M | gx_path_bbox(ppath, &box1); |
1679 | 2.38M | if (box1.p.x != 0 || box1.p.y != 0 || box1.q.x != 0 || box1.q.y != 0){ |
1680 | 2.34M | if (pcpath != 0) |
1681 | 2.34M | rect_intersect(box1, pcpath->outer_box); |
1682 | | /* convert fixed point co-ordinates to floating point and account for resolution */ |
1683 | 2.34M | box2.p.x = fixed2int(box1.p.x) / (pdev->HWResolution[0] / 72.0); |
1684 | 2.34M | box2.p.y = fixed2int(box1.p.y) / (pdev->HWResolution[1] / 72.0); |
1685 | 2.34M | box2.q.x = fixed2int(box1.q.x) / (pdev->HWResolution[0] / 72.0); |
1686 | 2.34M | box2.q.y = fixed2int(box1.q.y) / (pdev->HWResolution[1] / 72.0); |
1687 | | /* Finally compare the new BBox of the path with the existing EPS BBox */ |
1688 | 2.34M | if (box2.p.x < pdev->BBox.p.x) |
1689 | 3.88k | pdev->BBox.p.x = box2.p.x; |
1690 | 2.34M | if (box2.p.y < pdev->BBox.p.y) |
1691 | 9.39k | pdev->BBox.p.y = box2.p.y; |
1692 | 2.34M | if (box2.q.x > pdev->BBox.q.x) |
1693 | 6.65k | pdev->BBox.q.x = box2.q.x; |
1694 | 2.34M | if (box2.q.y > pdev->BBox.q.y) |
1695 | 4.63k | pdev->BBox.q.y = box2.q.y; |
1696 | 2.34M | } |
1697 | 2.38M | if (pdev->AccumulatingBBox) |
1698 | 2.12M | return 0; |
1699 | 2.38M | } |
1700 | 1.26M | have_path = !gx_path_is_void(ppath); |
1701 | 1.26M | if (!have_path && !pdev->vg_initial_set) { |
1702 | | /* See lib/gs_pdfwr.ps about "initial graphic state". */ |
1703 | 34.0k | pdf_prepare_initial_viewer_state(pdev, pgs); |
1704 | 34.0k | pdf_reset_graphics(pdev); |
1705 | 34.0k | return 0; |
1706 | 34.0k | } |
1707 | 1.22M | if (have_path) { |
1708 | 1.01M | code = gx_path_bbox(ppath, &box); |
1709 | 1.01M | if (code < 0) |
1710 | 0 | return code; |
1711 | 1.01M | } |
1712 | 1.22M | box1 = box; |
1713 | | |
1714 | 1.22M | code = prepare_fill_with_clip(pdev, pgs, &box, have_path, pdcolor, pcpath); |
1715 | 1.22M | if (code == gs_error_rangecheck) { |
1716 | | /* Fallback to the default implermentation for handling |
1717 | | a transparency with CompatibilityLevel<=1.3 . */ |
1718 | 3.71k | return gx_default_fill_path((gx_device *)pdev, pgs, ppath, params, pdcolor, pcpath); |
1719 | 3.71k | } |
1720 | 1.22M | if (code < 0) |
1721 | 0 | return code; |
1722 | 1.22M | if (code == 1) |
1723 | 23.3k | return 0; /* Nothing to paint. */ |
1724 | 1.20M | if (!have_path) |
1725 | 207k | return 0; |
1726 | 992k | code = pdf_setfillcolor((gx_device_vector *)pdev, pgs, pdcolor); |
1727 | 992k | if (code == gs_error_rangecheck) { |
1728 | 13.5k | const bool convert_to_image = ((pdev->CompatibilityLevel <= 1.2 || |
1729 | 13.5k | pdev->params.ColorConversionStrategy != ccs_LeaveColorUnchanged) && |
1730 | 13.5k | gx_dc_is_pattern2_color(pdcolor)); |
1731 | | |
1732 | 13.5k | if (!convert_to_image) { |
1733 | | /* Fallback to the default implermentation for handling |
1734 | | a shading with CompatibilityLevel<=1.2 . */ |
1735 | 9.73k | return gx_default_fill_path(dev, pgs, ppath, params, pdcolor, pcpath); |
1736 | 9.73k | } else { |
1737 | | /* Convert a shading into a bitmap |
1738 | | with CompatibilityLevel<=1.2 . */ |
1739 | 3.82k | pdf_lcvd_t cvd, *pcvd = &cvd; |
1740 | 3.82k | int sx, sy; |
1741 | 3.82k | gs_fixed_rect bbox, bbox1; |
1742 | 3.82k | bool need_mask = gx_dc_pattern2_can_overlap(pdcolor); |
1743 | 3.82k | gs_matrix m, save_ctm = ctm_only(pgs), ms, msi, mm; |
1744 | 3.82k | gs_int_point rect_size; |
1745 | | /* double scalex = 1.9, scaley = 1.4; debug purpose only. */ |
1746 | 3.82k | double scale, scalex, scaley; |
1747 | 3.82k | int log2_scale_x = 0, log2_scale_y = 0; |
1748 | 3.82k | gx_drawing_color dc = *pdcolor; |
1749 | 3.82k | gs_pattern2_instance_t pi = *(gs_pattern2_instance_t *)dc.ccolor.pattern; |
1750 | 3.82k | gs_gstate *pgs2 = gs_gstate_copy(pi.saved, gs_gstate_memory(pi.saved)); |
1751 | | |
1752 | 3.82k | if (pgs2 == NULL) |
1753 | 0 | return_error(gs_error_VMerror); |
1754 | 3.82k | dc.ccolor.pattern = (gs_pattern_instance_t *)π |
1755 | 3.82k | pi.saved = pgs2; |
1756 | 3.82k | code = gx_path_bbox(ppath, &bbox); |
1757 | 3.82k | if (code < 0) |
1758 | 0 | goto image_exit; |
1759 | 3.82k | rect_intersect(bbox, box); |
1760 | 3.82k | code = gx_dc_pattern2_get_bbox(pdcolor, &bbox1); |
1761 | 3.82k | if (code < 0) |
1762 | 0 | goto image_exit; |
1763 | 3.82k | if (code) |
1764 | 3.82k | rect_intersect(bbox, bbox1); |
1765 | 3.82k | if (bbox.p.x >= bbox.q.x || bbox.p.y >= bbox.q.y) { |
1766 | 635 | code = 0; |
1767 | 635 | goto image_exit; |
1768 | 635 | } |
1769 | 3.18k | sx = fixed2int(bbox.p.x); |
1770 | 3.18k | sy = fixed2int(bbox.p.y); |
1771 | 3.18k | gs_make_identity(&m); |
1772 | 3.18k | rect_size.x = fixed2int(bbox.q.x + fixed_half) - sx; |
1773 | 3.18k | rect_size.y = fixed2int(bbox.q.y + fixed_half) - sy; |
1774 | 3.18k | if (rect_size.x == 0 || rect_size.y == 0) |
1775 | 0 | goto image_exit; |
1776 | 3.18k | m.tx = (float)sx; |
1777 | 3.18k | m.ty = (float)sy; |
1778 | 3.18k | cvd.path_offset.x = sx; |
1779 | 3.18k | cvd.path_offset.y = sy; |
1780 | 3.18k | scale = (double)rect_size.x * rect_size.y * pdev->color_info.num_components / |
1781 | 3.18k | pdev->MaxShadingBitmapSize; |
1782 | 3.18k | if (scale > 1) { |
1783 | | /* This section (together with the call to 'path_scale' below) |
1784 | | sets up a downscaling when converting the shading into bitmap. |
1785 | | We used floating point numbers to debug it, but in production |
1786 | | we prefer to deal only with integers being powers of 2 |
1787 | | in order to avoid possible distorsions when scaling paths. |
1788 | | */ |
1789 | 411 | log2_scale_x = log2_scale_y = ilog2((int)ceil(sqrt(scale))); |
1790 | 411 | if ((double)(1 << log2_scale_x) * (1 << log2_scale_y) < scale) |
1791 | 356 | log2_scale_y++; |
1792 | 411 | if ((double)(1 << log2_scale_x) * (1 << log2_scale_y) < scale) |
1793 | 231 | log2_scale_x++; |
1794 | 411 | scalex = (double)(1 << log2_scale_x); |
1795 | 411 | scaley = (double)(1 << log2_scale_y); |
1796 | 411 | rect_size.x = (int)floor(rect_size.x / scalex + 0.5); |
1797 | 411 | rect_size.y = (int)floor(rect_size.y / scaley + 0.5); |
1798 | 411 | gs_make_scaling(1.0 / scalex, 1.0 / scaley, &ms); |
1799 | 411 | gs_make_scaling(scalex, scaley, &msi); |
1800 | 411 | gs_matrix_multiply(&msi, &m, &m); |
1801 | 411 | gs_matrix_multiply(&ctm_only(pgs), &ms, &mm); |
1802 | 411 | gs_setmatrix((gs_gstate *)pgs, &mm); |
1803 | 411 | gs_matrix_multiply(&ctm_only(pgs2), &ms, &mm); |
1804 | 411 | gs_setmatrix((gs_gstate *)pgs2, &mm); |
1805 | 411 | sx = fixed2int(bbox.p.x / (int)scalex); |
1806 | 411 | sy = fixed2int(bbox.p.y / (int)scaley); |
1807 | 411 | cvd.path_offset.x = sx; /* m.tx / scalex */ |
1808 | 411 | cvd.path_offset.y = sy; |
1809 | 411 | } |
1810 | 3.18k | if (sx < 0 || sy < 0) |
1811 | 0 | return 0; |
1812 | | |
1813 | 3.18k | code = pdf_setup_masked_image_converter(pdev, pdev->memory, &m, &pcvd, need_mask, sx, sy, |
1814 | 3.18k | rect_size.x, rect_size.y, false); |
1815 | 3.18k | if (code >= 0) { |
1816 | 3.18k | gs_path_enum cenum; |
1817 | 3.18k | gdev_vector_dopath_state_t state; |
1818 | | |
1819 | 3.18k | pcvd->has_background = gx_dc_pattern2_has_background(pdcolor); |
1820 | 3.18k | stream_puts(pdev->strm, "q\n"); |
1821 | 3.18k | code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_clip | gx_path_type_optimize, NULL); |
1822 | 3.18k | if (code >= 0) { |
1823 | 3.18k | stream_puts(pdev->strm, (params->rule < 0 ? "W n\n" : "W* n\n")); |
1824 | 3.18k | pdf_put_matrix(pdev, NULL, &cvd.m, " cm q\n"); |
1825 | 3.18k | cvd.write_matrix = false; |
1826 | 3.18k | if (!pcvd->has_background) { |
1827 | 3.18k | pcvd->pass = 1; |
1828 | 3.18k | code = gs_shading_do_fill_rectangle(pi.templat.Shading, |
1829 | 3.18k | NULL, (gx_device *)&cvd.mdev, pgs2, !pi.shfill); |
1830 | 3.18k | pcvd->pass = 2; |
1831 | 3.18k | pcvd->mask_is_empty = true; |
1832 | 3.18k | pcvd->path_is_empty = true; |
1833 | 3.18k | pcvd->filled_trap = 0; |
1834 | 3.18k | } |
1835 | 3.18k | if (code >= 0) { |
1836 | 2.86k | code = gs_shading_do_fill_rectangle(pi.templat.Shading, |
1837 | 2.86k | NULL, (gx_device *)&cvd.mdev, pgs2, !pi.shfill); |
1838 | 2.86k | } |
1839 | 3.18k | if (code >= 0) |
1840 | 2.86k | code = pdf_dump_converted_image(pdev, &cvd, 2); |
1841 | 3.18k | } |
1842 | 3.18k | stream_puts(pdev->strm, "Q Q\n"); |
1843 | 3.18k | pdf_remove_masked_image_converter(pdev, &cvd, need_mask); |
1844 | 3.18k | } |
1845 | 3.18k | gs_setmatrix((gs_gstate *)pgs, &save_ctm); |
1846 | 3.82k | image_exit: |
1847 | 3.82k | gs_gstate_free(pgs2); |
1848 | 3.82k | return code; |
1849 | 3.18k | } |
1850 | 13.5k | } |
1851 | 978k | if (code < 0) |
1852 | 0 | return code; |
1853 | 978k | { |
1854 | 978k | stream *s = pdev->strm; |
1855 | 978k | double scale; |
1856 | 978k | gs_matrix smat, *psmat = NULL; |
1857 | 978k | gs_path_enum cenum; |
1858 | 978k | gdev_vector_dopath_state_t state; |
1859 | | |
1860 | 978k | if (pcpath) { |
1861 | 978k | rect_intersect(box1, box); |
1862 | 978k | if (box1.p.x > box1.q.x || box1.p.y > box1.q.y) |
1863 | 313k | return 0; /* outside the clipping path */ |
1864 | 978k | } |
1865 | 664k | if (params->flatness != pdev->state.flatness) { |
1866 | 14.0k | pprintg1(s, "%g i\n", params->flatness); |
1867 | 14.0k | pdev->state.flatness = params->flatness; |
1868 | 14.0k | } |
1869 | 664k | if (make_rect_scaling(pdev, &box1, 1.0, &scale)) { |
1870 | 0 | gs_make_scaling(pdev->scale.x * scale, pdev->scale.y * scale, |
1871 | 0 | &smat); |
1872 | 0 | pdf_put_matrix(pdev, "q ", &smat, "cm\n"); |
1873 | 0 | psmat = &smat; |
1874 | 0 | } |
1875 | 664k | code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_fill | gx_path_type_optimize, NULL); |
1876 | 664k | if (code < 0) |
1877 | 0 | return code; |
1878 | | |
1879 | 664k | stream_puts(s, (params->rule < 0 ? "f\n" : "f*\n")); |
1880 | 664k | if (psmat != NULL) |
1881 | 0 | stream_puts(pdev->strm, "Q\n"); |
1882 | 664k | } |
1883 | 0 | return 0; |
1884 | 664k | } |
1885 | | |
1886 | | /* Stroke a path. */ |
1887 | | int |
1888 | | gdev_pdf_stroke_path(gx_device * dev, const gs_gstate * pgs, |
1889 | | gx_path * ppath, const gx_stroke_params * params, |
1890 | | const gx_drawing_color * pdcolor, const gx_clip_path * pcpath) |
1891 | 712k | { |
1892 | 712k | gx_device_pdf *pdev = (gx_device_pdf *) dev; |
1893 | 712k | stream *s; |
1894 | 712k | int code; |
1895 | 712k | double scale, path_scale; |
1896 | 712k | bool set_ctm; |
1897 | 712k | gs_matrix mat; |
1898 | 712k | double prescale = 1; |
1899 | 712k | gs_fixed_rect bbox; |
1900 | 712k | gs_path_enum cenum; |
1901 | 712k | gdev_vector_dopath_state_t state; |
1902 | | |
1903 | 712k | if (gx_path_is_void(ppath)) |
1904 | 48.2k | return 0; /* won't mark the page */ |
1905 | 664k | code = pdf_check_soft_mask(pdev, (gs_gstate *)pgs); |
1906 | 664k | if (code < 0) |
1907 | 0 | return code; |
1908 | 664k | if (pdf_must_put_clip_path(pdev, pcpath)) |
1909 | 34.5k | code = pdf_unclip(pdev); |
1910 | 630k | else if ((pdev->last_charpath_op & TEXT_DO_FALSE_CHARPATH) && ppath->current_subpath && |
1911 | 630k | (ppath->last_charpath_segment == ppath->current_subpath->last) && !pdev->ForOPDFRead) { |
1912 | 0 | bool hl_color = pdf_can_handle_hl_color((gx_device_vector *)pdev, pgs, pdcolor); |
1913 | 0 | const gs_gstate *pgs_for_hl_color = (hl_color ? pgs : NULL); |
1914 | |
|
1915 | 0 | if (pdf_modify_text_render_mode(pdev->text->text_state, 1)) { |
1916 | | /* Set the colour for the stroke */ |
1917 | 0 | code = pdf_reset_color(pdev, pgs_for_hl_color, pdcolor, &pdev->saved_stroke_color, |
1918 | 0 | &pdev->stroke_used_process_color, &psdf_set_stroke_color_commands); |
1919 | 0 | if (code == 0) { |
1920 | 0 | s = pdev->strm; |
1921 | | /* Text is emitted scaled so that the CTM is an identity matrix, the line width |
1922 | | * needs to be scaled to match otherwise we will get the default, or the current |
1923 | | * width scaled by the CTM before the text, either of which would be wrong. |
1924 | | */ |
1925 | 0 | scale = 72 / pdev->HWResolution[0]; |
1926 | 0 | scale *= fabs(pgs->ctm.xx); |
1927 | 0 | pprintg1(s, "%g w\n", (pgs->line_params.half_width * 2) * (float)scale); |
1928 | | /* Some trickery here. We have altered the colour, text render mode and linewidth, |
1929 | | * we don't want those to persist. By switching to a stream context we will flush the |
1930 | | * pending text. This has the beneficial side effect of executing a grestore. So |
1931 | | * everything works out neatly. |
1932 | | */ |
1933 | 0 | code = pdf_open_page(pdev, PDF_IN_STREAM); |
1934 | 0 | return(code); |
1935 | 0 | } |
1936 | 0 | } |
1937 | | /* Can only get here if any of the above steps fail, in which case we proceed to |
1938 | | * emit the charpath as a normal path, and stroke it. |
1939 | | */ |
1940 | 0 | code = pdf_open_page(pdev, PDF_IN_STREAM); |
1941 | 0 | } else |
1942 | 630k | code = pdf_open_page(pdev, PDF_IN_STREAM); |
1943 | 664k | if (code < 0) |
1944 | 0 | return code; |
1945 | 664k | code = pdf_prepare_stroke(pdev, pgs, false); |
1946 | 664k | if (code == gs_error_rangecheck) { |
1947 | | /* Fallback to the default implermentation for handling |
1948 | | a transparency with CompatibilityLevel<=1.3 . */ |
1949 | 1.13k | return gx_default_stroke_path((gx_device *)dev, pgs, ppath, params, pdcolor, pcpath); |
1950 | 1.13k | } |
1951 | 663k | if (code < 0) |
1952 | 0 | return code; |
1953 | 663k | code = pdf_put_clip_path(pdev, pcpath); |
1954 | 663k | if (code < 0) |
1955 | 0 | return code; |
1956 | | /* |
1957 | | * If the CTM is not uniform, stroke width depends on angle. |
1958 | | * We'd like to avoid resetting the CTM, so we check for uniform |
1959 | | * CTMs explicitly. Note that in PDF, unlike PostScript, it is |
1960 | | * the CTM at the time of the stroke operation, not the CTM at |
1961 | | * the time the path was constructed, that is used for transforming |
1962 | | * the points of the path; so if we have to reset the CTM, we must |
1963 | | * do it before constructing the path, and inverse-transform all |
1964 | | * the coordinates. |
1965 | | */ |
1966 | 663k | set_ctm = (bool)gdev_vector_stroke_scaling((gx_device_vector *)pdev, |
1967 | 663k | pgs, &scale, &mat); |
1968 | 663k | if (set_ctm && ((pgs->ctm.xx == 0 && pgs->ctm.xy == 0) || |
1969 | 170k | (pgs->ctm.yx == 0 && pgs->ctm.yy == 0))) { |
1970 | | /* Acrobat Reader 5 and Adobe Reader 6 issues |
1971 | | the "Wrong operand type" error with matrices, which have 3 zero coefs. |
1972 | | Besides that, we found that Acrobat Reader 4, Acrobat Reader 5 |
1973 | | and Adobe Reader 6 all store the current path in user space |
1974 | | and apply CTM in the time of stroking - See the bug 687901. |
1975 | | Therefore a precise conversion of Postscript to PDF isn't possible in this case. |
1976 | | Adobe viewers render a line with a constant width instead. |
1977 | | At last, with set_ctm == true we need the inverse matrix in |
1978 | | gdev_vector_dopath. Therefore we exclude projection matrices |
1979 | | (see bug 688363). */ |
1980 | 1.21k | set_ctm = false; |
1981 | 1.21k | scale = fabs(pgs->ctm.xx + pgs->ctm.xy + pgs->ctm.yx + pgs->ctm.yy) /* Using the non-zero coeff. */ |
1982 | 1.21k | / sqrt(2); /* Empirically from Adobe. */ |
1983 | 1.21k | } |
1984 | 663k | if (set_ctm && pdev->PDFA == 1) { |
1985 | | /* |
1986 | | * We want a scaling factor that will bring the largest reasonable |
1987 | | * user coordinate within bounds. We choose a factor based on the |
1988 | | * minor axis of the transformation. Thanks to Raph Levien for |
1989 | | * the following formula. |
1990 | | */ |
1991 | 0 | double a = mat.xx, b = mat.xy, c = mat.yx, d = mat.yy; |
1992 | 0 | double u = fabs(a * d - b * c); |
1993 | 0 | double v = a * a + b * b + c * c + d * d; |
1994 | 0 | double minor = (sqrt(v + 2 * u) - sqrt(v - 2 * u)) * 0.5; |
1995 | |
|
1996 | 0 | prescale = (minor == 0 || minor > 1 ? 1 : 1 / minor); |
1997 | 0 | } |
1998 | 663k | gx_path_bbox(ppath, &bbox); |
1999 | 663k | { |
2000 | | /* Check whether a painting appears inside the clipping box. |
2001 | | Doing so after writing the clipping path due to /SP pdfmark |
2002 | | uses a special hack with painting outside the clipping box |
2003 | | for synchronizing the clipping path (see lib/gs_pdfwr.ps). |
2004 | | That hack appeared because there is no way to pass |
2005 | | the gs_gstate through gdev_pdf_put_params, |
2006 | | which pdfmark is implemented with. |
2007 | | */ |
2008 | 663k | gs_fixed_rect clip_box, stroke_bbox = bbox; |
2009 | 663k | gs_point d0, d1; |
2010 | 663k | gs_fixed_point p0, p1; |
2011 | 663k | fixed bbox_expansion_x, bbox_expansion_y; |
2012 | | |
2013 | 663k | gs_distance_transform(pgs->line_params.half_width, 0, &ctm_only(pgs), &d0); |
2014 | 663k | gs_distance_transform(0, pgs->line_params.half_width, &ctm_only(pgs), &d1); |
2015 | 663k | p0.x = float2fixed(any_abs(d0.x)); |
2016 | 663k | p0.y = float2fixed(any_abs(d0.y)); |
2017 | 663k | p1.x = float2fixed(any_abs(d1.x)); |
2018 | 663k | p1.y = float2fixed(any_abs(d1.y)); |
2019 | 663k | bbox_expansion_x = max(p0.x, p1.x) + fixed_1 * 2; |
2020 | 663k | bbox_expansion_y = max(p0.y, p1.y) + fixed_1 * 2; |
2021 | 663k | stroke_bbox.p.x -= bbox_expansion_x; |
2022 | 663k | stroke_bbox.p.y -= bbox_expansion_y; |
2023 | 663k | stroke_bbox.q.x += bbox_expansion_x; |
2024 | 663k | stroke_bbox.q.y += bbox_expansion_y; |
2025 | 663k | gx_cpath_outer_box(pcpath, &clip_box); |
2026 | 663k | rect_intersect(stroke_bbox, clip_box); |
2027 | 663k | if (stroke_bbox.q.x < stroke_bbox.p.x || stroke_bbox.q.y < stroke_bbox.p.y) |
2028 | 32.0k | return 0; |
2029 | 663k | } |
2030 | 631k | if (make_rect_scaling(pdev, &bbox, prescale, &path_scale)) { |
2031 | 0 | scale /= path_scale; |
2032 | 0 | if (set_ctm) |
2033 | 0 | gs_matrix_scale(&mat, path_scale, path_scale, &mat); |
2034 | 0 | else { |
2035 | 0 | gs_make_scaling(path_scale, path_scale, &mat); |
2036 | 0 | set_ctm = true; |
2037 | 0 | } |
2038 | 0 | } |
2039 | 631k | code = gdev_vector_prepare_stroke((gx_device_vector *)pdev, pgs, params, |
2040 | 631k | pdcolor, scale); |
2041 | 631k | if (code < 0) |
2042 | 184 | return gx_default_stroke_path(dev, pgs, ppath, params, pdcolor, |
2043 | 184 | pcpath); |
2044 | 631k | if (!pdev->HaveStrokeColor) |
2045 | 588k | pdev->saved_fill_color = pdev->saved_stroke_color; |
2046 | 631k | if (set_ctm) |
2047 | 161k | pdf_put_matrix(pdev, "q ", &mat, "cm\n"); |
2048 | 631k | if (pgs->line_params.dash.offset != 0 || pgs->line_params.dash.pattern_size != 0) |
2049 | 5.86k | code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_stroke | gx_path_type_optimize | gx_path_type_dashed_stroke, (set_ctm ? &mat : (const gs_matrix *)0)); |
2050 | 625k | else |
2051 | 625k | code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_stroke | gx_path_type_optimize, (set_ctm ? &mat : (const gs_matrix *)0)); |
2052 | 631k | if (code < 0) |
2053 | 0 | return code; |
2054 | 631k | s = pdev->strm; |
2055 | 631k | stream_puts(s, "S"); |
2056 | 631k | stream_puts(s, (set_ctm ? " Q\n" : "\n")); |
2057 | 631k | if (pdev->Eps2Write) { |
2058 | 280k | pdev->AccumulatingBBox++; |
2059 | 280k | code = gx_default_stroke_path(dev, pgs, ppath, params, pdcolor, |
2060 | 280k | pcpath); |
2061 | 280k | pdev->AccumulatingBBox--; |
2062 | 280k | if (code < 0) |
2063 | 1 | return code; |
2064 | 280k | } |
2065 | 631k | return 0; |
2066 | 631k | } |
2067 | | |
2068 | | int |
2069 | | gdev_pdf_fill_stroke_path(gx_device *dev, const gs_gstate *pgs, gx_path *ppath, |
2070 | | const gx_fill_params *fill_params, const gx_drawing_color *pdcolor_fill, |
2071 | | const gx_stroke_params *stroke_params, const gx_drawing_color *pdcolor_stroke, |
2072 | | const gx_clip_path *pcpath) |
2073 | 12.4k | { |
2074 | 12.4k | gx_device_pdf *pdev = (gx_device_pdf *) dev; |
2075 | 12.4k | int code; |
2076 | 12.4k | bool new_clip; |
2077 | 12.4k | bool have_path; |
2078 | | |
2079 | 12.4k | have_path = !gx_path_is_void(ppath); |
2080 | 12.4k | if (!have_path) { |
2081 | 9.62k | if (!pdev->vg_initial_set) { |
2082 | | /* See lib/gs_pdfwr.ps about "initial graphic state". */ |
2083 | 0 | pdf_prepare_initial_viewer_state(pdev, pgs); |
2084 | 0 | pdf_reset_graphics(pdev); |
2085 | 0 | return 0; |
2086 | 0 | } |
2087 | 9.62k | } |
2088 | | |
2089 | | /* PostScript doesn't have a fill+stroke primitive, so break it into two operations |
2090 | | * PDF 1.2 only has a single overprint setting, we can't be certainto match that |
2091 | | * Because our inpu tcould be from a higher level. So be sure and break it into |
2092 | | * 2 operations. |
2093 | | */ |
2094 | 12.4k | if (pdev->ForOPDFRead || pdev->CompatibilityLevel < 1.3) { |
2095 | 10.8k | code = gdev_pdf_fill_path(dev, pgs, ppath, fill_params, pdcolor_fill, pcpath); |
2096 | 10.8k | if (code < 0) |
2097 | 0 | return code; |
2098 | 10.8k | gs_swapcolors_quick(pgs); |
2099 | 10.8k | code = gdev_pdf_stroke_path(dev, pgs, ppath, stroke_params, pdcolor_stroke, pcpath); |
2100 | 10.8k | gs_swapcolors_quick(pgs); |
2101 | 10.8k | return code; |
2102 | 10.8k | } else { |
2103 | 1.64k | bool set_ctm; |
2104 | 1.64k | gs_matrix mat; |
2105 | 1.64k | double scale, path_scale; |
2106 | 1.64k | double prescale = 1; |
2107 | 1.64k | gs_fixed_rect bbox; |
2108 | 1.64k | gs_path_enum cenum; |
2109 | 1.64k | gdev_vector_dopath_state_t state; |
2110 | 1.64k | stream *s = pdev->strm; |
2111 | | /* |
2112 | | * Check for an empty clipping path. |
2113 | | */ |
2114 | 1.64k | if (pcpath) { |
2115 | 1.64k | gs_fixed_rect cbox; |
2116 | | |
2117 | 1.64k | gx_cpath_outer_box(pcpath, &cbox); |
2118 | 1.64k | if (cbox.p.x >= cbox.q.x || cbox.p.y >= cbox.q.y) |
2119 | 25 | return 1; /* empty clipping path */ |
2120 | 1.64k | } |
2121 | 1.61k | code = pdf_check_soft_mask(pdev, (gs_gstate *)pgs); |
2122 | 1.61k | if (code < 0) |
2123 | 0 | return code; |
2124 | | |
2125 | 1.61k | new_clip = pdf_must_put_clip_path(pdev, pcpath); |
2126 | 1.61k | if (have_path || pdev->context == PDF_IN_NONE || new_clip) { |
2127 | 95 | if (new_clip) |
2128 | 39 | code = pdf_unclip(pdev); |
2129 | 56 | else |
2130 | 56 | code = pdf_open_page(pdev, PDF_IN_STREAM); |
2131 | 95 | if (code < 0) |
2132 | 0 | return code; |
2133 | 95 | } |
2134 | 1.61k | code = pdf_prepare_fill_stroke(pdev, pgs, false); |
2135 | 1.61k | if (code < 0) |
2136 | 0 | return code; |
2137 | | |
2138 | 1.61k | code = pdf_put_clip_path(pdev, pcpath); |
2139 | 1.61k | if (code < 0) |
2140 | 0 | return code; |
2141 | | /* |
2142 | | * If the CTM is not uniform, stroke width depends on angle. |
2143 | | * We'd like to avoid resetting the CTM, so we check for uniform |
2144 | | * CTMs explicitly. Note that in PDF, unlike PostScript, it is |
2145 | | * the CTM at the time of the stroke operation, not the CTM at |
2146 | | * the time the path was constructed, that is used for transforming |
2147 | | * the points of the path; so if we have to reset the CTM, we must |
2148 | | * do it before constructing the path, and inverse-transform all |
2149 | | * the coordinates. |
2150 | | */ |
2151 | 1.61k | set_ctm = (bool)gdev_vector_stroke_scaling((gx_device_vector *)pdev, |
2152 | 1.61k | pgs, &scale, &mat); |
2153 | 1.61k | if (set_ctm && ((pgs->ctm.xx == 0 && pgs->ctm.xy == 0) || |
2154 | 96 | (pgs->ctm.yx == 0 && pgs->ctm.yy == 0))) { |
2155 | | /* Acrobat Reader 5 and Adobe Reader 6 issues |
2156 | | the "Wrong operand type" error with matrices, which have 3 zero coefs. |
2157 | | Besides that, we found that Acrobat Reader 4, Acrobat Reader 5 |
2158 | | and Adobe Reader 6 all store the current path in user space |
2159 | | and apply CTM in the time of stroking - See the bug 687901. |
2160 | | Therefore a precise conversion of Postscript to PDF isn't possible in this case. |
2161 | | Adobe viewers render a line with a constant width instead. |
2162 | | At last, with set_ctm == true we need the inverse matrix in |
2163 | | gdev_vector_dopath. Therefore we exclude projection matrices |
2164 | | (see bug 688363). */ |
2165 | 12 | set_ctm = false; |
2166 | 12 | scale = fabs(pgs->ctm.xx + pgs->ctm.xy + pgs->ctm.yx + pgs->ctm.yy) /* Using the non-zero coeff. */ |
2167 | 12 | / sqrt(2); /* Empirically from Adobe. */ |
2168 | 12 | } |
2169 | 1.61k | if (pdev->PDFA == 1 && set_ctm) { |
2170 | | /* |
2171 | | * We want a scaling factor that will bring the largest reasonable |
2172 | | * user coordinate within bounds. We choose a factor based on the |
2173 | | * minor axis of the transformation. Thanks to Raph Levien for |
2174 | | * the following formula. |
2175 | | */ |
2176 | 0 | double a = mat.xx, b = mat.xy, c = mat.yx, d = mat.yy; |
2177 | 0 | double u = fabs(a * d - b * c); |
2178 | 0 | double v = a * a + b * b + c * c + d * d; |
2179 | 0 | double minor = (sqrt(v + 2 * u) - sqrt(v - 2 * u)) * 0.5; |
2180 | |
|
2181 | 0 | prescale = (minor == 0 || minor > 1 ? 1 : 1 / minor); |
2182 | 0 | } |
2183 | 1.61k | gx_path_bbox(ppath, &bbox); |
2184 | 1.61k | { |
2185 | | /* Check whether a painting appears inside the clipping box. |
2186 | | Doing so after writing the clipping path due to /SP pdfmark |
2187 | | uses a special hack with painting outside the clipping box |
2188 | | for synchronizing the clipping path (see lib/gs_pdfwr.ps). |
2189 | | That hack appeared because there is no way to pass |
2190 | | the gs_gstate through gdev_pdf_put_params, |
2191 | | which pdfmark is implemented with. |
2192 | | */ |
2193 | 1.61k | gs_fixed_rect clip_box, stroke_bbox = bbox; |
2194 | 1.61k | gs_point d0, d1; |
2195 | 1.61k | gs_fixed_point p0, p1; |
2196 | 1.61k | fixed bbox_expansion_x, bbox_expansion_y; |
2197 | | |
2198 | 1.61k | gs_distance_transform(pgs->line_params.half_width, 0, &ctm_only(pgs), &d0); |
2199 | 1.61k | gs_distance_transform(0, pgs->line_params.half_width, &ctm_only(pgs), &d1); |
2200 | 1.61k | p0.x = float2fixed(any_abs(d0.x)); |
2201 | 1.61k | p0.y = float2fixed(any_abs(d0.y)); |
2202 | 1.61k | p1.x = float2fixed(any_abs(d1.x)); |
2203 | 1.61k | p1.y = float2fixed(any_abs(d1.y)); |
2204 | 1.61k | bbox_expansion_x = max(p0.x, p1.x) + fixed_1 * 2; |
2205 | 1.61k | bbox_expansion_y = max(p0.y, p1.y) + fixed_1 * 2; |
2206 | 1.61k | stroke_bbox.p.x -= bbox_expansion_x; |
2207 | 1.61k | stroke_bbox.p.y -= bbox_expansion_y; |
2208 | 1.61k | stroke_bbox.q.x += bbox_expansion_x; |
2209 | 1.61k | stroke_bbox.q.y += bbox_expansion_y; |
2210 | 1.61k | gx_cpath_outer_box(pcpath, &clip_box); |
2211 | 1.61k | rect_intersect(stroke_bbox, clip_box); |
2212 | 1.61k | if (stroke_bbox.q.x < stroke_bbox.p.x || stroke_bbox.q.y < stroke_bbox.p.y) |
2213 | 275 | return 0; |
2214 | 1.61k | } |
2215 | 1.34k | if (make_rect_scaling(pdev, &bbox, prescale, &path_scale)) { |
2216 | 0 | scale /= path_scale; |
2217 | 0 | if (set_ctm) |
2218 | 0 | gs_matrix_scale(&mat, path_scale, path_scale, &mat); |
2219 | 0 | else { |
2220 | 0 | gs_make_scaling(path_scale, path_scale, &mat); |
2221 | 0 | set_ctm = true; |
2222 | 0 | } |
2223 | 0 | } |
2224 | | |
2225 | 1.34k | code = pdf_setfillcolor((gx_device_vector *)pdev, pgs, pdcolor_fill); |
2226 | 1.34k | if (code == gs_error_rangecheck) { |
2227 | | /* rangecheck means we revert to the equivalent to the default implementation */ |
2228 | 16 | code = gdev_pdf_fill_path(dev, pgs, ppath, fill_params, pdcolor_fill, pcpath); |
2229 | 16 | if (code < 0) |
2230 | 0 | return code; |
2231 | | /* Swap colors to make sure the pgs colorspace is correct for stroke */ |
2232 | 16 | gs_swapcolors_quick(pgs); |
2233 | 16 | code = gdev_pdf_stroke_path(dev, pgs, ppath, stroke_params, pdcolor_stroke, pcpath); |
2234 | 16 | gs_swapcolors_quick(pgs); |
2235 | 16 | return code; |
2236 | 16 | } |
2237 | | |
2238 | | /* Swap colors to make sure the pgs colorspace is correct for stroke */ |
2239 | 1.32k | gs_swapcolors_quick(pgs); |
2240 | 1.32k | code = gdev_vector_prepare_stroke((gx_device_vector *)pdev, pgs, stroke_params, |
2241 | 1.32k | pdcolor_stroke, scale); |
2242 | 1.32k | gs_swapcolors_quick(pgs); |
2243 | 1.32k | if (code < 0) { |
2244 | 0 | code = gdev_pdf_fill_path(dev, pgs, ppath, fill_params, pdcolor_fill, pcpath); |
2245 | 0 | if (code < 0) |
2246 | 0 | return code; |
2247 | 0 | return gdev_pdf_stroke_path(dev, pgs, ppath, stroke_params, pdcolor_stroke, pcpath); |
2248 | 0 | } |
2249 | 1.32k | if (!pdev->HaveStrokeColor) |
2250 | 0 | pdev->saved_fill_color = pdev->saved_stroke_color; |
2251 | 1.32k | if (set_ctm) |
2252 | 40 | pdf_put_matrix(pdev, "q ", &mat, "cm\n"); |
2253 | 1.32k | if (pgs->line_params.dash.offset != 0 || pgs->line_params.dash.pattern_size != 0) |
2254 | 1 | code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_stroke | gx_path_type_optimize | gx_path_type_dashed_stroke, (set_ctm ? &mat : (const gs_matrix *)0)); |
2255 | 1.32k | else |
2256 | 1.32k | code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_stroke | gx_path_type_optimize, (set_ctm ? &mat : (const gs_matrix *)0)); |
2257 | 1.32k | if (code < 0) |
2258 | 0 | return code; |
2259 | 1.32k | s = pdev->strm; |
2260 | 1.32k | stream_puts(s, (fill_params->rule < 0 ? "B\n" : "B*\n")); |
2261 | 1.32k | stream_puts(s, (set_ctm ? " Q\n" : "\n")); |
2262 | 1.32k | } |
2263 | 1.32k | return 0; |
2264 | 12.4k | } |
2265 | | |
2266 | | /* |
2267 | | The fill_rectangle_hl_color device method. |
2268 | | See gxdevcli.h about return codes. |
2269 | | */ |
2270 | | int |
2271 | | gdev_pdf_fill_rectangle_hl_color(gx_device *dev, const gs_fixed_rect *rect, |
2272 | | const gs_gstate *pgs, const gx_drawing_color *pdcolor, |
2273 | | const gx_clip_path *pcpath) |
2274 | 17.9k | { |
2275 | 17.9k | int code; |
2276 | 17.9k | gs_fixed_rect box1 = *rect, box = box1; |
2277 | 17.9k | gx_device_pdf *pdev = (gx_device_pdf *) dev; |
2278 | 17.9k | double scale; |
2279 | 17.9k | gs_matrix smat, *psmat = NULL; |
2280 | 17.9k | const bool convert_to_image = (pdev->CompatibilityLevel <= 1.2 && |
2281 | 17.9k | gx_dc_is_pattern2_color(pdcolor)); |
2282 | | |
2283 | 17.9k | if (rect->p.x == rect->q.x) |
2284 | 893 | return 0; |
2285 | 17.0k | if (!convert_to_image) { |
2286 | 17.0k | code = prepare_fill_with_clip(pdev, pgs, &box, true, pdcolor, pcpath); |
2287 | 17.0k | if (code < 0) |
2288 | 0 | return code; |
2289 | 17.0k | if (code == 1) |
2290 | 0 | return 0; /* Nothing to paint. */ |
2291 | 17.0k | code = pdf_setfillcolor((gx_device_vector *)pdev, pgs, pdcolor); |
2292 | 17.0k | if (code < 0) |
2293 | 0 | return code; |
2294 | 17.0k | if (pcpath) |
2295 | 17.0k | rect_intersect(box1, box); |
2296 | 17.0k | if (box1.p.x > box1.q.x || box1.p.y > box1.q.y) |
2297 | 0 | return 0; /* outside the clipping path */ |
2298 | 17.0k | if (make_rect_scaling(pdev, &box1, 1.0, &scale)) { |
2299 | 0 | gs_make_scaling(pdev->scale.x * scale, pdev->scale.y * scale, &smat); |
2300 | 0 | pdf_put_matrix(pdev, "q ", &smat, "cm\n"); |
2301 | 0 | psmat = &smat; |
2302 | 0 | } |
2303 | 17.0k | pprintg4(pdev->strm, "%g %g %g %g re f\n", |
2304 | 17.0k | fixed2float(box1.p.x) / scale, fixed2float(box1.p.y) / scale, |
2305 | 17.0k | fixed2float(box1.q.x - box1.p.x) / scale, fixed2float(box1.q.y - box1.p.y) / scale); |
2306 | 17.0k | if (psmat != NULL) |
2307 | 0 | stream_puts(pdev->strm, "Q\n"); |
2308 | 17.0k | if (pdev->Eps2Write) { |
2309 | 9.58k | gs_rect *Box; |
2310 | | |
2311 | 9.58k | if (!pdev->accumulating_charproc) |
2312 | 9.58k | Box = &pdev->BBox; |
2313 | 0 | else |
2314 | 0 | Box = &pdev->charproc_BBox; |
2315 | | |
2316 | 9.58k | if (fixed2float(box1.p.x) / (pdev->HWResolution[0] / 72.0) < Box->p.x) |
2317 | 711 | Box->p.x = fixed2float(box1.p.x) / (pdev->HWResolution[0] / 72.0); |
2318 | 9.58k | if (fixed2float(box1.p.y) / (pdev->HWResolution[1] / 72.0) < Box->p.y) |
2319 | 700 | Box->p.y = fixed2float(box1.p.y) / (pdev->HWResolution[1] / 72.0); |
2320 | 9.58k | if (fixed2float(box1.q.x) / (pdev->HWResolution[0] / 72.0) > Box->q.x) |
2321 | 1.12k | Box->q.x = fixed2float(box1.q.x) / (pdev->HWResolution[0] / 72.0); |
2322 | 9.58k | if (fixed2float(box1.q.y) / (pdev->HWResolution[1] / 72.0) > Box->q.y) |
2323 | 871 | Box->q.y = fixed2float(box1.q.y) / (pdev->HWResolution[1] / 72.0); |
2324 | 9.58k | } |
2325 | 17.0k | return 0; |
2326 | 17.0k | } else { |
2327 | 0 | gx_fill_params params; |
2328 | 0 | gx_path path; |
2329 | |
|
2330 | 0 | params.rule = 1; /* Not important because the path is a rectange. */ |
2331 | 0 | params.adjust.x = params.adjust.y = 0; |
2332 | 0 | params.flatness = pgs->flatness; |
2333 | 0 | gx_path_init_local(&path, pgs->memory); |
2334 | 0 | code = gx_path_add_rectangle(&path, rect->p.x, rect->p.y, rect->q.x, rect->q.y); |
2335 | 0 | if (code < 0) |
2336 | 0 | return code; |
2337 | 0 | code = gdev_pdf_fill_path(dev, pgs, &path, ¶ms, pdcolor, pcpath); |
2338 | 0 | if (code < 0) |
2339 | 0 | return code; |
2340 | 0 | gx_path_free(&path, "gdev_pdf_fill_rectangle_hl_color"); |
2341 | 0 | return code; |
2342 | |
|
2343 | 0 | } |
2344 | 17.0k | } |
2345 | | |
2346 | | int |
2347 | | gdev_pdf_fillpage(gx_device *dev, gs_gstate * pgs, gx_device_color *pdevc) |
2348 | 250k | { |
2349 | 250k | gx_device_pdf *pdev = (gx_device_pdf *) dev; |
2350 | 250k | int bottom = (pdev->ResourcesBeforeUsage ? 1 : 0); |
2351 | | |
2352 | 250k | if (gx_dc_pure_color(pdevc) == pdev->white && !is_in_page(pdev) && pdev->sbstack_depth <= bottom) { |
2353 | | /* PDF doesn't need to erase the page if its plain white */ |
2354 | 239k | return 0; |
2355 | 239k | } |
2356 | 11.0k | else |
2357 | 11.0k | return gx_default_fillpage(dev, pgs, pdevc); |
2358 | 250k | } |