gxclip.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /* Copyright (C) 1998, 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gxclip.c,v 1.15 2004/06/24 05:03:36 dan Exp $ */
  14. /* Implementation of (path-based) clipping */
  15. #include "gx.h"
  16. #include "gxdevice.h"
  17. #include "gxclip.h"
  18. #include "gxpath.h"
  19. #include "gxcpath.h"
  20. /* Define whether to look for vertical clipping regions. */
  21. #define CHECK_VERTICAL_CLIPPING
  22. /* ------ Rectangle list clipper ------ */
  23. /* Device for clipping with a region. */
  24. /* We forward non-drawing operations, but we must be sure to intercept */
  25. /* all drawing operations. */
  26. private dev_proc_open_device(clip_open);
  27. private dev_proc_fill_rectangle(clip_fill_rectangle);
  28. private dev_proc_copy_mono(clip_copy_mono);
  29. private dev_proc_copy_color(clip_copy_color);
  30. private dev_proc_copy_alpha(clip_copy_alpha);
  31. private dev_proc_fill_mask(clip_fill_mask);
  32. private dev_proc_strip_tile_rectangle(clip_strip_tile_rectangle);
  33. private dev_proc_strip_copy_rop(clip_strip_copy_rop);
  34. private dev_proc_get_clipping_box(clip_get_clipping_box);
  35. private dev_proc_get_bits_rectangle(clip_get_bits_rectangle);
  36. /* The device descriptor. */
  37. private const gx_device_clip gs_clip_device =
  38. {std_device_std_body(gx_device_clip, 0, "clipper",
  39. 0, 0, 1, 1),
  40. {clip_open,
  41. gx_forward_get_initial_matrix,
  42. gx_default_sync_output,
  43. gx_default_output_page,
  44. gx_default_close_device,
  45. gx_forward_map_rgb_color,
  46. gx_forward_map_color_rgb,
  47. clip_fill_rectangle,
  48. gx_default_tile_rectangle,
  49. clip_copy_mono,
  50. clip_copy_color,
  51. gx_default_draw_line,
  52. gx_default_get_bits,
  53. gx_forward_get_params,
  54. gx_forward_put_params,
  55. gx_forward_map_cmyk_color,
  56. gx_forward_get_xfont_procs,
  57. gx_forward_get_xfont_device,
  58. gx_forward_map_rgb_alpha_color,
  59. gx_forward_get_page_device,
  60. gx_forward_get_alpha_bits,
  61. clip_copy_alpha,
  62. gx_forward_get_band,
  63. gx_default_copy_rop,
  64. gx_default_fill_path,
  65. gx_default_stroke_path,
  66. clip_fill_mask,
  67. gx_default_fill_trapezoid,
  68. gx_default_fill_parallelogram,
  69. gx_default_fill_triangle,
  70. gx_default_draw_thin_line,
  71. gx_default_begin_image,
  72. gx_default_image_data,
  73. gx_default_end_image,
  74. clip_strip_tile_rectangle,
  75. clip_strip_copy_rop,
  76. clip_get_clipping_box,
  77. gx_default_begin_typed_image,
  78. clip_get_bits_rectangle,
  79. gx_forward_map_color_rgb_alpha,
  80. gx_no_create_compositor,
  81. gx_forward_get_hardware_params,
  82. gx_default_text_begin,
  83. gx_default_finish_copydevice,
  84. NULL, /* begin_transparency_group */
  85. NULL, /* end_transparency_group */
  86. NULL, /* begin_transparency_mask */
  87. NULL, /* end_transparency_mask */
  88. NULL, /* discard_transparency_layer */
  89. gx_forward_get_color_mapping_procs,
  90. gx_forward_get_color_comp_index,
  91. gx_forward_encode_color,
  92. gx_forward_decode_color,
  93. gx_forward_pattern_manage,
  94. gx_forward_fill_rectangle_hl_color,
  95. gx_forward_include_color_space,
  96. gx_default_fill_linear_color_scanline,
  97. gx_default_fill_linear_color_trapezoid,
  98. gx_default_fill_linear_color_triangle,
  99. gx_forward_update_spot_equivalent_colors
  100. }
  101. };
  102. /* Make a clipping device. */
  103. void
  104. gx_make_clip_translate_device(gx_device_clip * dev, const gx_clip_list * list,
  105. int tx, int ty, gs_memory_t *mem)
  106. {
  107. gx_device_init((gx_device *)dev, (const gx_device *)&gs_clip_device,
  108. mem, true);
  109. dev->list = *list;
  110. dev->translation.x = tx;
  111. dev->translation.y = ty;
  112. }
  113. void
  114. gx_make_clip_path_device(gx_device_clip * dev, const gx_clip_path * pcpath)
  115. {
  116. gx_make_clip_device(dev, gx_cpath_list(pcpath));
  117. }
  118. /* Define debugging statistics for the clipping loops. */
  119. #ifdef DEBUG
  120. struct stats_clip_s {
  121. long
  122. loops, out, in_y, in, in1, down, up, x, no_x;
  123. } stats_clip;
  124. private const uint clip_interval = 10000;
  125. # define INCR(v) (++(stats_clip.v))
  126. # define INCR_THEN(v, e) (INCR(v), (e))
  127. #else
  128. # define INCR(v) DO_NOTHING
  129. # define INCR_THEN(v, e) (e)
  130. #endif
  131. /*
  132. * Enumerate the rectangles of the x,w,y,h argument that fall within
  133. * the clipping region.
  134. */
  135. private int
  136. clip_enumerate_rest(gx_device_clip * rdev,
  137. int x, int y, int xe, int ye,
  138. int (*process)(clip_callback_data_t * pccd,
  139. int xc, int yc, int xec, int yec),
  140. clip_callback_data_t * pccd)
  141. {
  142. gx_clip_rect *rptr = rdev->current; /* const within algorithm */
  143. int yc;
  144. int code;
  145. #ifdef DEBUG
  146. if (INCR(loops) % clip_interval == 0 && gs_debug_c('q')) {
  147. dprintf5("[q]loops=%ld out=%ld in_y=%ld in=%ld in1=%ld\n",
  148. stats_clip.loops, stats_clip.out, stats_clip.in,
  149. stats_clip.in_y, stats_clip.in1);
  150. dprintf4("[q] down=%ld up=%ld x=%ld no_x=%ld\n",
  151. stats_clip.down, stats_clip.up, stats_clip.x,
  152. stats_clip.no_x);
  153. }
  154. #endif
  155. pccd->x = x, pccd->y = y;
  156. pccd->w = xe - x, pccd->h = ye - y;
  157. /*
  158. * Warp the cursor forward or backward to the first rectangle row
  159. * that could include a given y value. Assumes rptr is set, and
  160. * updates it. Specifically, after this loop, either rptr == 0 (if
  161. * the y value is greater than all y values in the list), or y <
  162. * rptr->ymax and either rptr->prev == 0 or y >= rptr->prev->ymax.
  163. * Note that y <= rptr->ymin is possible.
  164. *
  165. * In the first case below, the while loop is safe because if there
  166. * is more than one rectangle, there is a 'stopper' at the end of
  167. * the list.
  168. */
  169. if (y >= rptr->ymax) {
  170. if ((rptr = rptr->next) != 0)
  171. while (INCR_THEN(up, y >= rptr->ymax))
  172. rptr = rptr->next;
  173. } else
  174. while (rptr->prev != 0 && y < rptr->prev->ymax)
  175. INCR_THEN(down, rptr = rptr->prev);
  176. if (rptr == 0 || (yc = rptr->ymin) >= ye) {
  177. INCR(out);
  178. if (rdev->list.count > 1)
  179. rdev->current =
  180. (rptr != 0 ? rptr :
  181. y >= rdev->current->ymax ? rdev->list.tail :
  182. rdev->list.head);
  183. return 0;
  184. }
  185. rdev->current = rptr;
  186. if (yc < y)
  187. yc = y;
  188. do {
  189. const int ymax = rptr->ymax;
  190. int yec = min(ymax, ye);
  191. if_debug2('Q', "[Q]yc=%d yec=%d\n", yc, yec);
  192. do {
  193. int xc = rptr->xmin;
  194. int xec = rptr->xmax;
  195. if (xc < x)
  196. xc = x;
  197. if (xec > xe)
  198. xec = xe;
  199. if (xec > xc) {
  200. clip_rect_print('Q', "match", rptr);
  201. if_debug2('Q', "[Q]xc=%d xec=%d\n", xc, xec);
  202. INCR(x);
  203. /*
  204. * Conditionally look ahead to detect unclipped vertical strips. This is
  205. * really only valuable for 90 degree rotated images or (nearly-)vertical
  206. * lines with convex clipping regions; if we ever change images to use
  207. * source buffering and destination-oriented enumeration, we could probably
  208. * take out the code here with no adverse effects.
  209. */
  210. #ifdef CHECK_VERTICAL_CLIPPING
  211. if (xec - xc == pccd->w) { /* full width */
  212. /* Look ahead for a vertical swath. */
  213. while ((rptr = rptr->next) != 0 &&
  214. rptr->ymin == yec &&
  215. rptr->ymax <= ye &&
  216. rptr->xmin <= x &&
  217. rptr->xmax >= xe
  218. )
  219. yec = rptr->ymax;
  220. } else
  221. rptr = rptr->next;
  222. #else
  223. rptr = rptr->next;
  224. #endif
  225. code = process(pccd, xc, yc, xec, yec);
  226. if (code < 0)
  227. return code;
  228. } else {
  229. INCR_THEN(no_x, rptr = rptr->next);
  230. }
  231. if (rptr == 0)
  232. return 0;
  233. }
  234. while (rptr->ymax == ymax);
  235. } while ((yc = rptr->ymin) < ye);
  236. return 0;
  237. }
  238. private int
  239. clip_enumerate(gx_device_clip * rdev, int x, int y, int w, int h,
  240. int (*process)(clip_callback_data_t * pccd,
  241. int xc, int yc, int xec, int yec),
  242. clip_callback_data_t * pccd)
  243. {
  244. int xe, ye;
  245. const gx_clip_rect *rptr = rdev->current;
  246. if (w <= 0 || h <= 0)
  247. return 0;
  248. pccd->tdev = rdev->target;
  249. x += rdev->translation.x;
  250. xe = x + w;
  251. y += rdev->translation.y;
  252. ye = y + h;
  253. /* Check for the region being entirely within the current rectangle. */
  254. if (y >= rptr->ymin && ye <= rptr->ymax &&
  255. x >= rptr->xmin && xe <= rptr->xmax
  256. ) {
  257. pccd->x = x, pccd->y = y, pccd->w = w, pccd->h = h;
  258. return INCR_THEN(in, process(pccd, x, y, xe, ye));
  259. }
  260. return clip_enumerate_rest(rdev, x, y, xe, ye, process, pccd);
  261. }
  262. /* Open a clipping device */
  263. private int
  264. clip_open(gx_device * dev)
  265. {
  266. gx_device_clip *const rdev = (gx_device_clip *) dev;
  267. gx_device *tdev = rdev->target;
  268. /* Initialize the cursor. */
  269. rdev->current =
  270. (rdev->list.head == 0 ? &rdev->list.single : rdev->list.head);
  271. rdev->color_info = tdev->color_info;
  272. rdev->cached_colors = tdev->cached_colors;
  273. rdev->width = tdev->width;
  274. rdev->height = tdev->height;
  275. gx_device_copy_color_procs(dev, tdev);
  276. rdev->clipping_box_set = false;
  277. rdev->memory = tdev->memory;
  278. return 0;
  279. }
  280. /* Fill a rectangle */
  281. int
  282. clip_call_fill_rectangle(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  283. {
  284. return (*dev_proc(pccd->tdev, fill_rectangle))
  285. (pccd->tdev, xc, yc, xec - xc, yec - yc, pccd->color[0]);
  286. }
  287. private int
  288. clip_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  289. gx_color_index color)
  290. {
  291. gx_device_clip *rdev = (gx_device_clip *) dev;
  292. clip_callback_data_t ccdata;
  293. /* We handle the fastest cases in-line here. */
  294. gx_device *tdev = rdev->target;
  295. /*const*/ gx_clip_rect *rptr = rdev->current;
  296. int xe, ye;
  297. if (w <= 0 || h <= 0)
  298. return 0;
  299. x += rdev->translation.x;
  300. xe = x + w;
  301. y += rdev->translation.y;
  302. ye = y + h;
  303. /* We open-code the most common cases here. */
  304. if ((y >= rptr->ymin && ye <= rptr->ymax) ||
  305. ((rptr = rptr->next) != 0 &&
  306. y >= rptr->ymin && ye <= rptr->ymax)
  307. ) {
  308. rdev->current = rptr; /* may be redundant, but awkward to avoid */
  309. INCR(in_y);
  310. if (x >= rptr->xmin && xe <= rptr->xmax) {
  311. INCR(in);
  312. return dev_proc(tdev, fill_rectangle)(tdev, x, y, w, h, color);
  313. }
  314. else if ((rptr->prev == 0 || rptr->prev->ymax != rptr->ymax) &&
  315. (rptr->next == 0 || rptr->next->ymax != rptr->ymax)
  316. ) {
  317. INCR(in1);
  318. if (x < rptr->xmin)
  319. x = rptr->xmin;
  320. if (xe > rptr->xmax)
  321. xe = rptr->xmax;
  322. return
  323. (x >= xe ? 0 :
  324. dev_proc(tdev, fill_rectangle)(tdev, x, y, xe - x, h, color));
  325. }
  326. }
  327. ccdata.tdev = tdev;
  328. ccdata.color[0] = color;
  329. return clip_enumerate_rest(rdev, x, y, xe, ye,
  330. clip_call_fill_rectangle, &ccdata);
  331. }
  332. /* Copy a monochrome rectangle */
  333. int
  334. clip_call_copy_mono(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  335. {
  336. return (*dev_proc(pccd->tdev, copy_mono))
  337. (pccd->tdev, pccd->data + (yc - pccd->y) * pccd->raster,
  338. pccd->sourcex + xc - pccd->x, pccd->raster, gx_no_bitmap_id,
  339. xc, yc, xec - xc, yec - yc, pccd->color[0], pccd->color[1]);
  340. }
  341. private int
  342. clip_copy_mono(gx_device * dev,
  343. const byte * data, int sourcex, int raster, gx_bitmap_id id,
  344. int x, int y, int w, int h,
  345. gx_color_index color0, gx_color_index color1)
  346. {
  347. gx_device_clip *rdev = (gx_device_clip *) dev;
  348. clip_callback_data_t ccdata;
  349. /* We handle the fastest case in-line here. */
  350. gx_device *tdev = rdev->target;
  351. const gx_clip_rect *rptr = rdev->current;
  352. int xe, ye;
  353. if (w <= 0 || h <= 0)
  354. return 0;
  355. x += rdev->translation.x;
  356. xe = x + w;
  357. y += rdev->translation.y;
  358. ye = y + h;
  359. if (y >= rptr->ymin && ye <= rptr->ymax) {
  360. INCR(in_y);
  361. if (x >= rptr->xmin && xe <= rptr->xmax) {
  362. INCR(in);
  363. return dev_proc(tdev, copy_mono)
  364. (tdev, data, sourcex, raster, id, x, y, w, h, color0, color1);
  365. }
  366. }
  367. ccdata.tdev = tdev;
  368. ccdata.data = data, ccdata.sourcex = sourcex, ccdata.raster = raster;
  369. ccdata.color[0] = color0, ccdata.color[1] = color1;
  370. return clip_enumerate_rest(rdev, x, y, xe, ye,
  371. clip_call_copy_mono, &ccdata);
  372. }
  373. /* Copy a color rectangle */
  374. int
  375. clip_call_copy_color(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  376. {
  377. return (*dev_proc(pccd->tdev, copy_color))
  378. (pccd->tdev, pccd->data + (yc - pccd->y) * pccd->raster,
  379. pccd->sourcex + xc - pccd->x, pccd->raster, gx_no_bitmap_id,
  380. xc, yc, xec - xc, yec - yc);
  381. }
  382. private int
  383. clip_copy_color(gx_device * dev,
  384. const byte * data, int sourcex, int raster, gx_bitmap_id id,
  385. int x, int y, int w, int h)
  386. {
  387. gx_device_clip *rdev = (gx_device_clip *) dev;
  388. clip_callback_data_t ccdata;
  389. ccdata.data = data, ccdata.sourcex = sourcex, ccdata.raster = raster;
  390. return clip_enumerate(rdev, x, y, w, h, clip_call_copy_color, &ccdata);
  391. }
  392. /* Copy a rectangle with alpha */
  393. int
  394. clip_call_copy_alpha(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  395. {
  396. return (*dev_proc(pccd->tdev, copy_alpha))
  397. (pccd->tdev, pccd->data + (yc - pccd->y) * pccd->raster,
  398. pccd->sourcex + xc - pccd->x, pccd->raster, gx_no_bitmap_id,
  399. xc, yc, xec - xc, yec - yc, pccd->color[0], pccd->depth);
  400. }
  401. private int
  402. clip_copy_alpha(gx_device * dev,
  403. const byte * data, int sourcex, int raster, gx_bitmap_id id,
  404. int x, int y, int w, int h,
  405. gx_color_index color, int depth)
  406. {
  407. gx_device_clip *rdev = (gx_device_clip *) dev;
  408. clip_callback_data_t ccdata;
  409. ccdata.data = data, ccdata.sourcex = sourcex, ccdata.raster = raster;
  410. ccdata.color[0] = color, ccdata.depth = depth;
  411. return clip_enumerate(rdev, x, y, w, h, clip_call_copy_alpha, &ccdata);
  412. }
  413. /* Fill a region defined by a mask. */
  414. int
  415. clip_call_fill_mask(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  416. {
  417. return (*dev_proc(pccd->tdev, fill_mask))
  418. (pccd->tdev, pccd->data + (yc - pccd->y) * pccd->raster,
  419. pccd->sourcex + xc - pccd->x, pccd->raster, gx_no_bitmap_id,
  420. xc, yc, xec - xc, yec - yc, pccd->pdcolor, pccd->depth,
  421. pccd->lop, NULL);
  422. }
  423. private int
  424. clip_fill_mask(gx_device * dev,
  425. const byte * data, int sourcex, int raster, gx_bitmap_id id,
  426. int x, int y, int w, int h,
  427. const gx_drawing_color * pdcolor, int depth,
  428. gs_logical_operation_t lop, const gx_clip_path * pcpath)
  429. {
  430. gx_device_clip *rdev = (gx_device_clip *) dev;
  431. clip_callback_data_t ccdata;
  432. if (pcpath != 0)
  433. return gx_default_fill_mask(dev, data, sourcex, raster, id,
  434. x, y, w, h, pdcolor, depth, lop,
  435. pcpath);
  436. ccdata.data = data, ccdata.sourcex = sourcex, ccdata.raster = raster;
  437. ccdata.pdcolor = pdcolor, ccdata.depth = depth, ccdata.lop = lop;
  438. return clip_enumerate(rdev, x, y, w, h, clip_call_fill_mask, &ccdata);
  439. }
  440. /* Strip-tile a rectangle. */
  441. int
  442. clip_call_strip_tile_rectangle(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  443. {
  444. return (*dev_proc(pccd->tdev, strip_tile_rectangle))
  445. (pccd->tdev, pccd->tiles, xc, yc, xec - xc, yec - yc,
  446. pccd->color[0], pccd->color[1], pccd->phase.x, pccd->phase.y);
  447. }
  448. private int
  449. clip_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
  450. int x, int y, int w, int h,
  451. gx_color_index color0, gx_color_index color1, int phase_x, int phase_y)
  452. {
  453. gx_device_clip *rdev = (gx_device_clip *) dev;
  454. clip_callback_data_t ccdata;
  455. ccdata.tiles = tiles;
  456. ccdata.color[0] = color0, ccdata.color[1] = color1;
  457. ccdata.phase.x = phase_x, ccdata.phase.y = phase_y;
  458. return clip_enumerate(rdev, x, y, w, h, clip_call_strip_tile_rectangle, &ccdata);
  459. }
  460. /* Copy a rectangle with RasterOp and strip texture. */
  461. int
  462. clip_call_strip_copy_rop(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  463. {
  464. return (*dev_proc(pccd->tdev, strip_copy_rop))
  465. (pccd->tdev, pccd->data + (yc - pccd->y) * pccd->raster,
  466. pccd->sourcex + xc - pccd->x, pccd->raster, gx_no_bitmap_id,
  467. pccd->scolors, pccd->textures, pccd->tcolors,
  468. xc, yc, xec - xc, yec - yc, pccd->phase.x, pccd->phase.y,
  469. pccd->lop);
  470. }
  471. private int
  472. clip_strip_copy_rop(gx_device * dev,
  473. const byte * sdata, int sourcex, uint raster, gx_bitmap_id id,
  474. const gx_color_index * scolors,
  475. const gx_strip_bitmap * textures, const gx_color_index * tcolors,
  476. int x, int y, int w, int h,
  477. int phase_x, int phase_y, gs_logical_operation_t lop)
  478. {
  479. gx_device_clip *rdev = (gx_device_clip *) dev;
  480. clip_callback_data_t ccdata;
  481. ccdata.data = sdata, ccdata.sourcex = sourcex, ccdata.raster = raster;
  482. ccdata.scolors = scolors, ccdata.textures = textures,
  483. ccdata.tcolors = tcolors;
  484. ccdata.phase.x = phase_x, ccdata.phase.y = phase_y, ccdata.lop = lop;
  485. return clip_enumerate(rdev, x, y, w, h, clip_call_strip_copy_rop, &ccdata);
  486. }
  487. /* Get the (outer) clipping box, in client coordinates. */
  488. private void
  489. clip_get_clipping_box(gx_device * dev, gs_fixed_rect * pbox)
  490. {
  491. gx_device_clip *const rdev = (gx_device_clip *) dev;
  492. if (!rdev->clipping_box_set) {
  493. gx_device *tdev = rdev->target;
  494. gs_fixed_rect tbox;
  495. (*dev_proc(tdev, get_clipping_box)) (tdev, &tbox);
  496. if (rdev->list.count != 0) {
  497. gs_fixed_rect cbox;
  498. if (rdev->list.count == 1) {
  499. cbox.p.x = int2fixed(rdev->list.single.xmin);
  500. cbox.p.y = int2fixed(rdev->list.single.ymin);
  501. cbox.q.x = int2fixed(rdev->list.single.xmax);
  502. cbox.q.y = int2fixed(rdev->list.single.ymax);
  503. } else {
  504. /* The head and tail elements are dummies.... */
  505. cbox.p.x = int2fixed(rdev->list.xmin);
  506. cbox.p.y = int2fixed(rdev->list.head->next->ymin);
  507. cbox.q.x = int2fixed(rdev->list.xmax);
  508. cbox.q.y = int2fixed(rdev->list.tail->prev->ymax);
  509. }
  510. rect_intersect(tbox, cbox);
  511. }
  512. if (rdev->translation.x | rdev->translation.y) {
  513. fixed tx = int2fixed(rdev->translation.x),
  514. ty = int2fixed(rdev->translation.y);
  515. if (tbox.p.x != min_fixed)
  516. tbox.p.x -= tx;
  517. if (tbox.p.y != min_fixed)
  518. tbox.p.y -= ty;
  519. if (tbox.q.x != max_fixed)
  520. tbox.q.x -= tx;
  521. if (tbox.q.y != max_fixed)
  522. tbox.q.y -= ty;
  523. }
  524. rdev->clipping_box = tbox;
  525. rdev->clipping_box_set = true;
  526. }
  527. *pbox = rdev->clipping_box;
  528. }
  529. /* Get bits back from the device. */
  530. private int
  531. clip_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
  532. gs_get_bits_params_t * params, gs_int_rect ** unread)
  533. {
  534. gx_device_clip *rdev = (gx_device_clip *) dev;
  535. gx_device *tdev = rdev->target;
  536. int tx = rdev->translation.x, ty = rdev->translation.y;
  537. gs_int_rect rect;
  538. int code;
  539. rect.p.x = prect->p.x - tx, rect.p.y = prect->p.y - ty;
  540. rect.q.x = prect->q.x - tx, rect.q.y = prect->q.y - ty;
  541. code = (*dev_proc(tdev, get_bits_rectangle))
  542. (tdev, &rect, params, unread);
  543. if (code > 0) {
  544. /* Adjust unread rectangle coordinates */
  545. gs_int_rect *list = *unread;
  546. int i;
  547. for (i = 0; i < code; ++list, ++i) {
  548. list->p.x += tx, list->p.y += ty;
  549. list->q.x += tx, list->q.y += ty;
  550. }
  551. }
  552. return code;
  553. }