home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevbbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  38.9 KB  |  1,321 lines

  1. /* Copyright (C) 1996, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevbbox.c,v 1.3 2000/09/19 19:00:11 lpd Exp $ */
  20. /* Device for tracking bounding box */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsparam.h"
  26. #include "gxdevice.h"
  27. #include "gsdevice.h"        /* requires gsmatrix.h */
  28. #include "gdevbbox.h"
  29. #include "gxdcolor.h"        /* for gx_device_black/white */
  30. #include "gxiparam.h"        /* for image source size */
  31. #include "gxistate.h"
  32. #include "gxpaint.h"
  33. #include "gxpath.h"
  34. #include "gxcpath.h"
  35.  
  36. /* GC descriptor */
  37. public_st_device_bbox();
  38.  
  39. /* Device procedures */
  40. private dev_proc_open_device(bbox_open_device);
  41. private dev_proc_close_device(bbox_close_device);
  42. private dev_proc_output_page(bbox_output_page);
  43. private dev_proc_fill_rectangle(bbox_fill_rectangle);
  44. private dev_proc_copy_mono(bbox_copy_mono);
  45. private dev_proc_copy_color(bbox_copy_color);
  46. private dev_proc_get_params(bbox_get_params);
  47. private dev_proc_put_params(bbox_put_params);
  48. private dev_proc_copy_alpha(bbox_copy_alpha);
  49. private dev_proc_fill_path(bbox_fill_path);
  50. private dev_proc_stroke_path(bbox_stroke_path);
  51. private dev_proc_fill_mask(bbox_fill_mask);
  52. private dev_proc_fill_trapezoid(bbox_fill_trapezoid);
  53. private dev_proc_fill_parallelogram(bbox_fill_parallelogram);
  54. private dev_proc_fill_triangle(bbox_fill_triangle);
  55. private dev_proc_draw_thin_line(bbox_draw_thin_line);
  56. private dev_proc_strip_tile_rectangle(bbox_strip_tile_rectangle);
  57. private dev_proc_strip_copy_rop(bbox_strip_copy_rop);
  58. private dev_proc_begin_typed_image(bbox_begin_typed_image);
  59. private dev_proc_create_compositor(bbox_create_compositor);
  60. private dev_proc_text_begin(bbox_text_begin);
  61.  
  62. /* The device prototype */
  63. /*
  64.  * Normally this would be private, but if the device is going to be used
  65.  * stand-alone, it has to be public.
  66.  */
  67. /*private */ const
  68. /*
  69.  * The bbox device sets the resolution to some value R (currently 4000), and
  70.  * the page size in device pixels to slightly smaller than the largest
  71.  * representable values (around 500K), leaving a little room for stroke
  72.  * widths, rounding, etc.  If an input file (or the command line) resets the
  73.  * resolution to a value R' > R, the page size in pixels will get multiplied
  74.  * by R'/R, and will thereby exceed the representable range, causing a
  75.  * limitcheck.  That is why the bbox device must set the resolution to a
  76.  * value larger than that of any real device.  A consequence of this is that
  77.  * the page size in inches is limited to the maximum representable pixel
  78.  * size divided by R, which gives a limit of about 120" in each dimension.
  79.  */
  80. #define MAX_COORD (max_int_in_fixed - 1000)
  81. #define MAX_RESOLUTION 4000
  82. gx_device_bbox gs_bbox_device =
  83. {
  84.     /*
  85.      * Define the device as 8-bit gray scale to avoid computing halftones.
  86.      */
  87.     std_device_dci_body(gx_device_bbox, 0, "bbox",
  88.             MAX_COORD, MAX_COORD,
  89.             MAX_RESOLUTION, MAX_RESOLUTION,
  90.             1, 8, 255, 0, 256, 1),
  91.     {bbox_open_device,
  92.      gx_upright_get_initial_matrix,
  93.      NULL,            /* sync_output */
  94.      bbox_output_page,
  95.      bbox_close_device,
  96.      gx_default_gray_map_rgb_color,
  97.      gx_default_gray_map_color_rgb,
  98.      bbox_fill_rectangle,
  99.      NULL,            /* tile_rectangle */
  100.      bbox_copy_mono,
  101.      bbox_copy_color,
  102.      NULL,            /* draw_line */
  103.      NULL,            /* get_bits */
  104.      bbox_get_params,
  105.      bbox_put_params,
  106.      gx_default_map_cmyk_color,
  107.      NULL,            /* get_xfont_procs */
  108.      NULL,            /* get_xfont_device */
  109.      gx_default_map_rgb_alpha_color,
  110.      gx_page_device_get_page_device,
  111.      NULL,            /* get_alpha_bits */
  112.      bbox_copy_alpha,
  113.      NULL,            /* get_band */
  114.      NULL,            /* copy_rop */
  115.      bbox_fill_path,
  116.      bbox_stroke_path,
  117.      bbox_fill_mask,
  118.      bbox_fill_trapezoid,
  119.      bbox_fill_parallelogram,
  120.      bbox_fill_triangle,
  121.      bbox_draw_thin_line,
  122.      gx_default_begin_image,
  123.      NULL,            /* image_data */
  124.      NULL,            /* end_image */
  125.      bbox_strip_tile_rectangle,
  126.      bbox_strip_copy_rop,
  127.      NULL,            /* get_clipping_box */
  128.      bbox_begin_typed_image,
  129.      NULL,            /* get_bits_rectangle */
  130.      gx_default_map_color_rgb_alpha,
  131.      bbox_create_compositor,
  132.      NULL,            /* get_hardware_params */
  133.      bbox_text_begin,
  134.      NULL            /* finish_copydevice */
  135.     },
  136.     0,                /* target */
  137.     1,                /*true *//* free_standing */
  138.     1                /*true *//* forward_open_close */
  139. };
  140.  
  141. #undef MAX_COORD
  142. #undef MAX_RESOLUTION
  143.  
  144. /* Default box procedures */
  145.  
  146. bool
  147. bbox_default_init_box(void *pdata)
  148. {
  149.     gx_device_bbox *const bdev = (gx_device_bbox *)pdata;
  150.     gs_fixed_rect *const pr = &bdev->bbox;
  151.  
  152.     pr->p.x = pr->p.y = max_fixed;
  153.     pr->q.x = pr->q.y = min_fixed;
  154.     return bdev->white != bdev->transparent;
  155. }
  156. #define BBOX_INIT_BOX(bdev)\
  157.   bdev->box_procs.init_box(bdev->box_proc_data)
  158.  
  159. void
  160. bbox_default_get_box(const void *pdata, gs_fixed_rect *pbox)
  161. {
  162.     const gx_device_bbox *const bdev = (const gx_device_bbox *)pdata;
  163.  
  164.     *pbox = bdev->bbox;
  165. }
  166. #define BBOX_GET_BOX(bdev, pbox)\
  167.     bdev->box_procs.get_box(bdev->box_proc_data, pbox);
  168.  
  169. void
  170. bbox_default_add_rect(void *pdata, fixed x0, fixed y0, fixed x1, fixed y1)
  171. {
  172.     gx_device_bbox *const bdev = (gx_device_bbox *)pdata;
  173.     gs_fixed_rect *const pr = &bdev->bbox;
  174.  
  175.     if (x0 < pr->p.x)
  176.     pr->p.x = x0;
  177.     if (y0 < pr->p.y)
  178.     pr->p.y = y0;
  179.     if (x1 > pr->q.x)
  180.     pr->q.x = x1;
  181.     if (y1 > pr->q.y)
  182.     pr->q.y = y1;
  183. }
  184. #define BBOX_ADD_RECT(bdev, x0, y0, x1, y1)\
  185.     bdev->box_procs.add_rect(bdev->box_proc_data, x0, y0, x1, y1)
  186. #define BBOX_ADD_INT_RECT(bdev, x0, y0, x1, y1)\
  187.     BBOX_ADD_RECT(bdev, int2fixed(x0), int2fixed(y0), int2fixed(x1),\
  188.           int2fixed(y1))
  189.  
  190. bool
  191. bbox_default_in_rect(const void *pdata, const gs_fixed_rect *pbox)
  192. {
  193.     const gx_device_bbox *const bdev = (const gx_device_bbox *)pdata;
  194.  
  195.     return rect_within(*pbox, bdev->bbox);
  196. }
  197. #define BBOX_IN_RECT(bdev, pbox)\
  198.     bdev->box_procs.in_rect(bdev->box_proc_data, pbox)
  199.  
  200. private const gx_device_bbox_procs_t box_procs_default = {
  201.     bbox_default_init_box, bbox_default_get_box, bbox_default_add_rect,
  202.     bbox_default_in_rect
  203. };
  204.  
  205. #define RECT_IS_PAGE(dev, x, y, w, h)\
  206.   (x <= 0 && y <= 0 && x + w >= dev->width && y + h >= dev->height)
  207.  
  208.      /* ---------------- Open/close/page ---------------- */
  209.  
  210. /* Copy device parameters back from the target. */
  211. private void
  212. bbox_copy_params(gx_device_bbox * bdev, bool remap_colors)
  213. {
  214.     gx_device *tdev = bdev->target;
  215.  
  216.     if (tdev != 0)
  217.     gx_device_copy_params((gx_device *)bdev, tdev);
  218.     if (remap_colors) {
  219.     bdev->black = gx_device_black((gx_device *)bdev);
  220.     bdev->white = gx_device_white((gx_device *)bdev);
  221.     bdev->transparent =
  222.         (bdev->white_is_opaque ? gx_no_color_index : bdev->white);
  223.     }
  224. }
  225.  
  226. #define GX_DC_IS_TRANSPARENT(pdevc, bdev)\
  227.   (gx_dc_pure_color(pdevc) == (bdev)->transparent && gx_dc_is_pure(pdevc))
  228.  
  229. private int
  230. bbox_close_device(gx_device * dev)
  231. {
  232.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  233.     gx_device *tdev = bdev->target;
  234.  
  235.     if (bdev->box_procs.init_box != box_procs_default.init_box) {
  236.     /*
  237.      * This device was created as a wrapper for a compositor.
  238.      * Just free the devices.
  239.      */
  240.     int code = (bdev->forward_open_close ? gs_closedevice(tdev) : 0);
  241.  
  242.     gs_free_object(dev->memory, dev, "bbox_close_device(composite)");
  243.     return code;
  244.     } else {
  245.     return (tdev && bdev->forward_open_close ? gs_closedevice(tdev) : 0);
  246.     }
  247. }
  248.  
  249. /* Initialize a bounding box device. */
  250. void
  251. gx_device_bbox_init(gx_device_bbox * dev, gx_device * target)
  252. {
  253.     gx_device_init((gx_device *) dev, (const gx_device *)&gs_bbox_device,
  254.            (target ? target->memory : NULL), true);
  255.     gx_device_forward_fill_in_procs((gx_device_forward *) dev);
  256.     if (target) {
  257.     set_dev_proc(dev, get_initial_matrix, gx_forward_get_initial_matrix);
  258.     set_dev_proc(dev, map_rgb_color, gx_forward_map_rgb_color);
  259.     set_dev_proc(dev, map_color_rgb, gx_forward_map_color_rgb);
  260.     set_dev_proc(dev, map_cmyk_color, gx_forward_map_cmyk_color);
  261.     set_dev_proc(dev, map_rgb_alpha_color, gx_forward_map_rgb_alpha_color);
  262.     set_dev_proc(dev, map_color_rgb_alpha, gx_forward_map_color_rgb_alpha);
  263.     gx_device_set_target((gx_device_forward *)dev, target);
  264.     }
  265.     dev->box_procs = box_procs_default;
  266.     dev->box_proc_data = dev;
  267.     bbox_copy_params(dev, false);
  268.     dev->free_standing = false;    /* being used as a component */
  269. }
  270.  
  271. /* Set whether a bounding box device propagates open/close to its target. */
  272. void
  273. gx_device_bbox_fwd_open_close(gx_device_bbox * dev, bool forward_open_close)
  274. {
  275.     dev->forward_open_close = forward_open_close;
  276. }
  277.  
  278. /* Set whether a bounding box device considers white to be opaque. */
  279. void
  280. gx_device_bbox_set_white_opaque(gx_device_bbox *bdev, bool white_is_opaque)
  281. {
  282.     bdev->white_is_opaque = white_is_opaque;
  283.     bdev->transparent =
  284.     (bdev->white_is_opaque ? gx_no_color_index : bdev->white);
  285. }
  286.  
  287. /* Release a bounding box device. */
  288. void
  289. gx_device_bbox_release(gx_device_bbox *dev)
  290. {
  291.     /* Just release the reference to the target. */
  292.     gx_device_set_target((gx_device_forward *)dev, NULL);
  293. }
  294.  
  295. /* Read back the bounding box in 1/72" units. */
  296. void
  297. gx_device_bbox_bbox(gx_device_bbox * dev, gs_rect * pbbox)
  298. {
  299.     gs_fixed_rect bbox;
  300.  
  301.     BBOX_GET_BOX(dev, &bbox);
  302.     if (bbox.p.x > bbox.q.x || bbox.p.y > bbox.q.y) {
  303.     /* Nothing has been written on this page. */
  304.     pbbox->p.x = pbbox->p.y = pbbox->q.x = pbbox->q.y = 0;
  305.     } else {
  306.     gs_rect dbox;
  307.     gs_matrix mat;
  308.  
  309.     dbox.p.x = fixed2float(bbox.p.x);
  310.     dbox.p.y = fixed2float(bbox.p.y);
  311.     dbox.q.x = fixed2float(bbox.q.x);
  312.     dbox.q.y = fixed2float(bbox.q.y);
  313.     gs_deviceinitialmatrix((gx_device *)dev, &mat);
  314.     gs_bbox_transform_inverse(&dbox, &mat, pbbox);
  315.     }
  316. }
  317.  
  318. private int
  319. bbox_open_device(gx_device * dev)
  320. {
  321.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  322.  
  323.     if (bdev->free_standing) {
  324.     gx_device_forward_fill_in_procs((gx_device_forward *) dev);
  325.     bdev->box_procs = box_procs_default;
  326.     bdev->box_proc_data = bdev;
  327.     }
  328.     if (bdev->box_procs.init_box == box_procs_default.init_box)
  329.     BBOX_INIT_BOX(bdev);
  330.     /* gx_forward_open_device doesn't exist */
  331.     {
  332.     gx_device *tdev = bdev->target;
  333.     int code =
  334.         (tdev && bdev->forward_open_close ? gs_opendevice(tdev) : 0);
  335.  
  336.     bbox_copy_params(bdev, true);
  337.     return code;
  338.     }
  339. }
  340.  
  341. private int
  342. bbox_output_page(gx_device * dev, int num_copies, int flush)
  343. {
  344.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  345.  
  346.     if (bdev->free_standing) {
  347.     /*
  348.      * This is a free-standing device.  Print the page bounding box.
  349.      */
  350.     gs_rect bbox;
  351.  
  352.     gx_device_bbox_bbox(bdev, &bbox);
  353.     dlprintf4("%%%%BoundingBox: %d %d %d %d\n",
  354.           (int)floor(bbox.p.x), (int)floor(bbox.p.y),
  355.           (int)ceil(bbox.q.x), (int)ceil(bbox.q.y));
  356.     dlprintf4("%%%%HiResBoundingBox: %f %f %f %f\n",
  357.           bbox.p.x, bbox.p.y, bbox.q.x, bbox.q.y);
  358.     }
  359.     return gx_forward_output_page(dev, num_copies, flush);
  360. }
  361.  
  362. /* ---------------- Low-level drawing ---------------- */
  363.  
  364. private int
  365. bbox_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  366.             gx_color_index color)
  367. {
  368.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  369.     gx_device *tdev = bdev->target;
  370.     /* gx_forward_fill_rectangle doesn't exist */
  371.     int code =
  372.     (tdev == 0 ? 0 :
  373.      dev_proc(tdev, fill_rectangle)(tdev, x, y, w, h, color));
  374.  
  375.     /* Check for erasing the entire page. */
  376.     if (RECT_IS_PAGE(dev, x, y, w, h)) {
  377.     if (!BBOX_INIT_BOX(bdev))
  378.         return code;
  379.     }
  380.     if (color != bdev->transparent)
  381.     BBOX_ADD_INT_RECT(bdev, x, y, x + w, y + h);
  382.     return code;
  383. }
  384.  
  385. private int
  386. bbox_copy_mono(gx_device * dev, const byte * data,
  387.         int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  388.            gx_color_index zero, gx_color_index one)
  389. {
  390.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  391.     /* gx_forward_copy_mono doesn't exist */
  392.     gx_device *tdev = bdev->target;
  393.     int code =
  394.     (tdev == 0 ? 0 :
  395.      dev_proc(tdev, copy_mono)
  396.      (tdev, data, dx, raster, id, x, y, w, h, zero, one));
  397.  
  398.     if ((one != gx_no_color_index && one != bdev->transparent) ||
  399.     (zero != gx_no_color_index && zero != bdev->transparent)
  400.     )
  401.     BBOX_ADD_INT_RECT(bdev, x, y, x + w, y + h);
  402.     return code;
  403. }
  404.  
  405. private int
  406. bbox_copy_color(gx_device * dev, const byte * data,
  407.         int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h)
  408. {
  409.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  410.     /* gx_forward_copy_color doesn't exist */
  411.     gx_device *tdev = bdev->target;
  412.     int code =
  413.     (tdev == 0 ? 0 :
  414.      dev_proc(tdev, copy_color)
  415.      (tdev, data, dx, raster, id, x, y, w, h));
  416.  
  417.     BBOX_ADD_INT_RECT(bdev, x, y, x + w, y + h);
  418.     return code;
  419. }
  420.  
  421. private int
  422. bbox_copy_alpha(gx_device * dev, const byte * data, int data_x,
  423.         int raster, gx_bitmap_id id, int x, int y, int w, int h,
  424.         gx_color_index color, int depth)
  425. {
  426.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  427.     /* gx_forward_copy_alpha doesn't exist */
  428.     gx_device *tdev = bdev->target;
  429.     int code =
  430.     (tdev == 0 ? 0 :
  431.      dev_proc(tdev, copy_alpha)
  432.      (tdev, data, data_x, raster, id, x, y, w, h, color, depth));
  433.  
  434.     BBOX_ADD_INT_RECT(bdev, x, y, x + w, y + h);
  435.     return code;
  436. }
  437.  
  438. private int
  439. bbox_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
  440.    int x, int y, int w, int h, gx_color_index color0, gx_color_index color1,
  441.               int px, int py)
  442. {
  443.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  444.     /* Skip the call if there is no target. */
  445.     gx_device *tdev = bdev->target;
  446.     int code =
  447.     (tdev == 0 ? 0 :
  448.      dev_proc(tdev, strip_tile_rectangle)
  449.      (tdev, tiles, x, y, w, h, color0, color1, px, py));
  450.  
  451.     if (RECT_IS_PAGE(dev, x, y, w, h)) {
  452.     if (!BBOX_INIT_BOX(bdev))
  453.         return code;
  454.     }
  455.     BBOX_ADD_INT_RECT(bdev, x, y, x + w, y + h);
  456.     return code;
  457. }
  458.  
  459. private int
  460. bbox_strip_copy_rop(gx_device * dev,
  461.             const byte * sdata, int sourcex, uint sraster,
  462.             gx_bitmap_id id,
  463.             const gx_color_index * scolors,
  464.             const gx_strip_bitmap * textures,
  465.             const gx_color_index * tcolors,
  466.             int x, int y, int w, int h,
  467.             int phase_x, int phase_y, gs_logical_operation_t lop)
  468. {
  469.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  470.     /* gx_forward_strip_copy_rop doesn't exist */
  471.     gx_device *tdev = bdev->target;
  472.     int code =
  473.     (tdev == 0 ? 0 :
  474.      dev_proc(tdev, strip_copy_rop)
  475.      (tdev, sdata, sourcex, sraster, id, scolors,
  476.       textures, tcolors, x, y, w, h, phase_x, phase_y, lop));
  477.  
  478.     BBOX_ADD_INT_RECT(bdev, x, y, x + w, y + h);
  479.     return code;
  480. }
  481.  
  482. /* ---------------- Parameters ---------------- */
  483.  
  484. /* We implement get_params to provide a way to read out the bounding box. */
  485. private int
  486. bbox_get_params(gx_device * dev, gs_param_list * plist)
  487. {
  488.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  489.     gs_fixed_rect fbox;
  490.     int code = gx_forward_get_params(dev, plist);
  491.     gs_param_float_array bba;
  492.     float bbox[4];
  493.  
  494.     if (code < 0)
  495.     return code;
  496.     /*
  497.      * We might be calling get_params before the device has been
  498.      * initialized: in this case, box_proc_data = 0.
  499.      */
  500.     if (bdev->box_proc_data == 0)
  501.     fbox = bdev->bbox;
  502.     else
  503.     BBOX_GET_BOX(bdev, &fbox);
  504.     bbox[0] = fixed2float(fbox.p.x);
  505.     bbox[1] = fixed2float(fbox.p.y);
  506.     bbox[2] = fixed2float(fbox.q.x);
  507.     bbox[3] = fixed2float(fbox.q.y);
  508.     bba.data = bbox, bba.size = 4, bba.persistent = false;
  509.     return param_write_float_array(plist, "PageBoundingBox", &bba);
  510. }
  511.  
  512. /* We implement put_params to ensure that we keep the important */
  513. /* device parameters up to date, and to prevent an /undefined error */
  514. /* from PageBoundingBox. */
  515. private int
  516. bbox_put_params(gx_device * dev, gs_param_list * plist)
  517. {
  518.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  519.     int code;
  520.     int ecode = 0;
  521.     gs_param_name param_name;
  522.     gs_param_float_array bba;
  523.  
  524.     code = param_read_float_array(plist, (param_name = "PageBoundingBox"),
  525.                   &bba);
  526.     switch (code) {
  527.     case 0:
  528.         if (bba.size != 4) {
  529.         ecode = gs_note_error(gs_error_rangecheck);
  530.         goto e;
  531.         }
  532.         break;
  533.     default:
  534.         ecode = code;
  535.       e:param_signal_error(plist, param_name, ecode);
  536.     case 1:
  537.         bba.data = 0;
  538.     }
  539.  
  540.     code = gx_forward_put_params(dev, plist);
  541.     if (ecode < 0)
  542.     code = ecode;
  543.     if (code >= 0 && bba.data != 0) {
  544.     BBOX_INIT_BOX(bdev);
  545.     BBOX_ADD_RECT(bdev, float2fixed(bba.data[0]), float2fixed(bba.data[1]),
  546.               float2fixed(bba.data[2]), float2fixed(bba.data[3]));
  547.     }
  548.     bbox_copy_params(bdev, true);
  549.     return code;
  550. }
  551.  
  552. /* ---------------- Polygon drawing ---------------- */
  553.  
  554. private fixed
  555. edge_x_at_y(const gs_fixed_edge * edge, fixed y)
  556. {
  557.     return fixed_mult_quo(edge->end.x - edge->start.x,
  558.               y - edge->start.y,
  559.               edge->end.y - edge->start.y) + edge->start.x;
  560. }
  561. private int
  562. bbox_fill_trapezoid(gx_device * dev,
  563.             const gs_fixed_edge * left, const gs_fixed_edge * right,
  564.             fixed ybot, fixed ytop, bool swap_axes,
  565.             const gx_device_color * pdevc, gs_logical_operation_t lop)
  566. {
  567.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  568.     /* Skip the call if there is no target. */
  569.     gx_device *tdev = bdev->target;
  570.     int code =
  571.     (tdev == 0 ? 0 :
  572.      dev_proc(tdev, fill_trapezoid)
  573.      (tdev, left, right, ybot, ytop, swap_axes, pdevc, lop));
  574.  
  575.     if (!GX_DC_IS_TRANSPARENT(pdevc, bdev)) {
  576.     fixed x0l =
  577.         (left->start.y == ybot ? left->start.x :
  578.          edge_x_at_y(left, ybot));
  579.     fixed x1l =
  580.         (left->end.y == ytop ? left->end.x :
  581.          edge_x_at_y(left, ytop));
  582.     fixed x0r =
  583.         (right->start.y == ybot ? right->start.x :
  584.          edge_x_at_y(right, ybot));
  585.     fixed x1r =
  586.         (right->end.y == ytop ? right->end.x :
  587.          edge_x_at_y(right, ytop));
  588.     fixed xminl = min(x0l, x1l), xmaxl = max(x0l, x1l);
  589.     fixed xminr = min(x0r, x1r), xmaxr = max(x0r, x1r);
  590.     fixed x0 = min(xminl, xminr), x1 = max(xmaxl, xmaxr);
  591.  
  592.     if (swap_axes)
  593.         BBOX_ADD_RECT(bdev, ybot, x0, ytop, x1);
  594.     else
  595.         BBOX_ADD_RECT(bdev, x0, ybot, x1, ytop);
  596.     }
  597.     return code;
  598. }
  599.  
  600. private int
  601. bbox_fill_parallelogram(gx_device * dev,
  602.             fixed px, fixed py, fixed ax, fixed ay,
  603.             fixed bx, fixed by, const gx_device_color * pdevc,
  604.             gs_logical_operation_t lop)
  605. {
  606.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  607.     /* Skip the call if there is no target. */
  608.     gx_device *tdev = bdev->target;
  609.     int code =
  610.     (tdev == 0 ? 0 :
  611.      dev_proc(tdev, fill_parallelogram)
  612.      (tdev, px, py, ax, ay, bx, by, pdevc, lop));
  613.  
  614.     if (!GX_DC_IS_TRANSPARENT(pdevc, bdev)) {
  615.     fixed xmin, ymin, xmax, ymax;
  616.  
  617.     /* bbox_add_rect requires points in correct order. */
  618. #define SET_MIN_MAX(vmin, vmax, av, bv)\
  619.   BEGIN\
  620.     if (av <= 0) {\
  621.     if (bv <= 0)\
  622.         vmin = av + bv, vmax = 0;\
  623.     else\
  624.         vmin = av, vmax = bv;\
  625.     } else if (bv <= 0)\
  626.     vmin = bv, vmax = av;\
  627.     else\
  628.     vmin = 0, vmax = av + bv;\
  629.   END
  630.     SET_MIN_MAX(xmin, xmax, ax, bx);
  631.     SET_MIN_MAX(ymin, ymax, ay, by);
  632. #undef SET_MIN_MAX
  633.     BBOX_ADD_RECT(bdev, px + xmin, py + ymin, px + xmax, py + ymax);
  634.     }
  635.     return code;
  636. }
  637.  
  638. private int
  639. bbox_fill_triangle(gx_device * dev,
  640.            fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
  641.            const gx_device_color * pdevc, gs_logical_operation_t lop)
  642. {
  643.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  644.     /* Skip the call if there is no target. */
  645.     gx_device *tdev = bdev->target;
  646.     int code =
  647.     (tdev == 0 ? 0 :
  648.      dev_proc(tdev, fill_triangle)
  649.      (tdev, px, py, ax, ay, bx, by, pdevc, lop));
  650.  
  651.     if (!GX_DC_IS_TRANSPARENT(pdevc, bdev)) {
  652.     fixed xmin, ymin, xmax, ymax;
  653.  
  654.     /* bbox_add_rect requires points in correct order. */
  655. #define SET_MIN_MAX(vmin, vmax, av, bv)\
  656.   BEGIN\
  657.     if (av <= 0) {\
  658.     if (bv <= 0)\
  659.         vmin = min(av, bv), vmax = 0;\
  660.     else\
  661.         vmin = av, vmax = bv;\
  662.     } else if (bv <= 0)\
  663.     vmin = bv, vmax = av;\
  664.     else\
  665.     vmin = 0, vmax = max(av, bv);\
  666.   END
  667.     SET_MIN_MAX(xmin, xmax, ax, bx);
  668.     SET_MIN_MAX(ymin, ymax, ay, by);
  669. #undef SET_MIN_MAX
  670.     BBOX_ADD_RECT(bdev, px + xmin, py + ymin, px + xmax, py + ymax);
  671.     }
  672.     return code;
  673. }
  674.  
  675. private int
  676. bbox_draw_thin_line(gx_device * dev,
  677.             fixed fx0, fixed fy0, fixed fx1, fixed fy1,
  678.             const gx_device_color * pdevc, gs_logical_operation_t lop)
  679. {
  680.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  681.     /* Skip the call if there is no target. */
  682.     gx_device *tdev = bdev->target;
  683.     int code =
  684.     (tdev == 0 ? 0 :
  685.      dev_proc(tdev, draw_thin_line)
  686.      (tdev, fx0, fy0, fx1, fy0, pdevc, lop));
  687.  
  688.     if (!GX_DC_IS_TRANSPARENT(pdevc, bdev)) {
  689.     fixed xmin, ymin, xmax, ymax;
  690.  
  691.     /* bbox_add_rect requires points in correct order. */
  692. #define SET_MIN_MAX(vmin, vmax, av, bv)\
  693.   BEGIN\
  694.     if (av < bv)\
  695.     vmin = av, vmax = bv;\
  696.     else\
  697.     vmin = bv, vmax = av;\
  698.   END
  699.     SET_MIN_MAX(xmin, xmax, fx0, fx1);
  700.     SET_MIN_MAX(ymin, ymax, fy0, fy1);
  701. #undef SET_MIN_MAX
  702.     BBOX_ADD_RECT(bdev, xmin, ymin, xmax, ymax);
  703.     }
  704.     return code;
  705. }
  706.  
  707. /* ---------------- High-level drawing ---------------- */
  708.  
  709. #define adjust_box(pbox, adj)\
  710. ((pbox)->p.x -= (adj).x, (pbox)->p.y -= (adj).y,\
  711.  (pbox)->q.x += (adj).x, (pbox)->q.y += (adj).y)
  712.  
  713. private int
  714. bbox_fill_path(gx_device * dev, const gs_imager_state * pis, gx_path * ppath,
  715.            const gx_fill_params * params, const gx_device_color * pdevc,
  716.            const gx_clip_path * pcpath)
  717. {
  718.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  719.     gx_device *tdev = bdev->target;
  720.     dev_proc_fill_path((*fill_path)) =
  721.     (tdev == 0 ? dev_proc(&gs_null_device, fill_path) :
  722.      dev_proc(tdev, fill_path));
  723.     int code;
  724.  
  725.     if (!GX_DC_IS_TRANSPARENT(pdevc, bdev) && !gx_path_is_void(ppath)) {
  726.     gs_fixed_rect ibox;
  727.     gs_fixed_point adjust;
  728.  
  729.     if (gx_path_bbox(ppath, &ibox) < 0)
  730.         return 0;
  731.     adjust = params->adjust;
  732.     if (params->fill_zero_width)
  733.         gx_adjust_if_empty(&ibox, &adjust);
  734.     adjust_box(&ibox, adjust);
  735.     /*
  736.      * If the path lies within the already accumulated box, just draw
  737.      * on the target.
  738.      */
  739.     if (BBOX_IN_RECT(bdev, &ibox))
  740.         return fill_path(tdev, pis, ppath, params, pdevc, pcpath);
  741.     /*
  742.      * If the target uses the default algorithm, just draw on the
  743.      * bbox device.
  744.      */
  745.     if (tdev != 0 && fill_path == gx_default_fill_path)
  746.         return fill_path(dev, pis, ppath, params, pdevc, pcpath);
  747.     /* Draw on the target now. */
  748.     code = fill_path(tdev, pis, ppath, params, pdevc, pcpath);
  749.     if (code < 0)
  750.         return code;
  751.     if (pcpath != NULL &&
  752.         !gx_cpath_includes_rectangle(pcpath, ibox.p.x, ibox.p.y,
  753.                      ibox.q.x, ibox.q.y)
  754.         ) {
  755.         /*
  756.          * Let the target do the drawing, but break down the
  757.          * fill path into pieces for computing the bounding box.
  758.          */
  759.         gx_drawing_color devc;
  760.  
  761.         color_set_pure(&devc, bdev->black);  /* any non-white color will do */
  762.         bdev->target = NULL;
  763.         code = gx_default_fill_path(dev, pis, ppath, params, &devc, pcpath);
  764.         bdev->target = tdev;
  765.     } else {        /* Just use the path bounding box. */
  766.         BBOX_ADD_RECT(bdev, ibox.p.x, ibox.p.y, ibox.q.x, ibox.q.y);
  767.     }
  768.     return code;
  769.     } else
  770.     return fill_path(tdev, pis, ppath, params, pdevc, pcpath);
  771. }
  772.  
  773. private int
  774. bbox_stroke_path(gx_device * dev, const gs_imager_state * pis, gx_path * ppath,
  775.          const gx_stroke_params * params,
  776.          const gx_drawing_color * pdevc, const gx_clip_path * pcpath)
  777. {
  778.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  779.     gx_device *tdev = bdev->target;
  780.     /* Skip the call if there is no target. */
  781.     int code =
  782.     (tdev == 0 ? 0 :
  783.      dev_proc(tdev, stroke_path)(tdev, pis, ppath, params, pdevc, pcpath));
  784.  
  785.     if (!GX_DC_IS_TRANSPARENT(pdevc, bdev)) {
  786.     gs_fixed_rect ibox;
  787.     gs_fixed_point expand;
  788.  
  789.     if (gx_stroke_path_expansion(pis, ppath, &expand) == 0 &&
  790.         gx_path_bbox(ppath, &ibox) >= 0
  791.         ) {
  792.         /* The fast result is exact. */
  793.         adjust_box(&ibox, expand);
  794.     } else {
  795.         /*
  796.          * The result is not exact.  Compute an exact result using
  797.          * strokepath.
  798.          */
  799.         gx_path *spath = gx_path_alloc(pis->memory, "bbox_stroke_path");
  800.         int code = 0;
  801.  
  802.         if (spath)
  803.         code = gx_imager_stroke_add(ppath, spath, dev, pis);
  804.         else
  805.         code = -1;
  806.         if (code >= 0)
  807.         code = gx_path_bbox(spath, &ibox);
  808.         if (code < 0) {
  809.         ibox.p.x = ibox.p.y = min_fixed;
  810.         ibox.q.x = ibox.q.y = max_fixed;
  811.         }
  812.         if (spath)
  813.         gx_path_free(spath, "bbox_stroke_path");
  814.     }
  815.     if (pcpath != NULL &&
  816.         !gx_cpath_includes_rectangle(pcpath, ibox.p.x, ibox.p.y,
  817.                      ibox.q.x, ibox.q.y)
  818.         ) {
  819.         /* Let the target do the drawing, but break down the */
  820.         /* fill path into pieces for computing the bounding box. */
  821.         gx_drawing_color devc;
  822.  
  823.         color_set_pure(&devc, bdev->black);  /* any non-white color will do */
  824.         bdev->target = NULL;
  825.         gx_default_stroke_path(dev, pis, ppath, params, &devc, pcpath);
  826.         bdev->target = tdev;
  827.     } else {
  828.         /* Just use the path bounding box. */
  829.         BBOX_ADD_RECT(bdev, ibox.p.x, ibox.p.y, ibox.q.x, ibox.q.y);
  830.     }
  831.     }
  832.     return code;
  833. }
  834.  
  835. private int
  836. bbox_fill_mask(gx_device * dev,
  837.            const byte * data, int dx, int raster, gx_bitmap_id id,
  838.            int x, int y, int w, int h,
  839.            const gx_drawing_color * pdcolor, int depth,
  840.            gs_logical_operation_t lop, const gx_clip_path * pcpath)
  841. {
  842.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  843.     gx_device *tdev = bdev->target;
  844.     /* Skip the call if there is no target. */
  845.     int code =
  846.     (tdev == 0 ? 0 :
  847.      dev_proc(tdev, fill_mask)
  848.      (tdev, data, dx, raster, id, x, y, w, h,
  849.       pdcolor, depth, lop, pcpath));
  850.  
  851.     if (pcpath != NULL &&
  852.     !gx_cpath_includes_rectangle(pcpath, int2fixed(x), int2fixed(y),
  853.                      int2fixed(x + w),
  854.                      int2fixed(y + h))
  855.     ) {
  856.     /* Let the target do the drawing, but break down the */
  857.     /* image into pieces for computing the bounding box. */
  858.     bdev->target = NULL;
  859.     gx_default_fill_mask(dev, data, dx, raster, id, x, y, w, h,
  860.                  pdcolor, depth, lop, pcpath);
  861.     bdev->target = tdev;
  862.     } else {
  863.     /* Just use the mask bounding box. */
  864.     BBOX_ADD_INT_RECT(bdev, x, y, x + w, y + h);
  865.     }
  866.     return code;
  867. }
  868.  
  869. /* ------ Bitmap imaging ------ */
  870.  
  871. typedef struct bbox_image_enum_s {
  872.     gx_image_enum_common;
  873.     gs_memory_t *memory;
  874.     gs_matrix matrix;        /* map from image space to device space */
  875.     const gx_clip_path *pcpath;
  876.     gx_image_enum_common_t *target_info;
  877.     bool params_are_const;
  878.     int x0, x1;
  879.     int y, height;
  880. } bbox_image_enum;
  881.  
  882. gs_private_st_suffix_add2(st_bbox_image_enum, bbox_image_enum,
  883.   "bbox_image_enum", bbox_image_enum_enum_ptrs, bbox_image_enum_reloc_ptrs,
  884.   st_gx_image_enum_common, pcpath, target_info);
  885.  
  886. private image_enum_proc_plane_data(bbox_image_plane_data);
  887. private image_enum_proc_end_image(bbox_image_end_image);
  888. private image_enum_proc_flush(bbox_image_flush);
  889. private image_enum_proc_planes_wanted(bbox_image_planes_wanted);
  890. private const gx_image_enum_procs_t bbox_image_enum_procs = {
  891.     bbox_image_plane_data, bbox_image_end_image,
  892.     bbox_image_flush, bbox_image_planes_wanted
  893. };
  894.  
  895. private int
  896. bbox_image_begin(const gs_imager_state * pis, const gs_matrix * pmat,
  897.          const gs_image_common_t * pic, const gs_int_rect * prect,
  898.          const gx_clip_path * pcpath, gs_memory_t * memory,
  899.          bbox_image_enum ** ppbe)
  900. {
  901.     int code;
  902.     gs_matrix mat;
  903.     bbox_image_enum *pbe;
  904.  
  905.     if (pmat == 0)
  906.     pmat = &ctm_only(pis);
  907.     if ((code = gs_matrix_invert(&pic->ImageMatrix, &mat)) < 0 ||
  908.     (code = gs_matrix_multiply(&mat, pmat, &mat)) < 0
  909.     )
  910.     return code;
  911.     pbe = gs_alloc_struct(memory, bbox_image_enum, &st_bbox_image_enum,
  912.               "bbox_image_begin");
  913.     if (pbe == 0)
  914.     return_error(gs_error_VMerror);
  915.     pbe->memory = memory;
  916.     pbe->matrix = mat;
  917.     pbe->pcpath = pcpath;
  918.     pbe->target_info = 0;    /* in case no target */
  919.     pbe->params_are_const = false;    /* check the first time */
  920.     if (prect) {
  921.     pbe->x0 = prect->p.x, pbe->x1 = prect->q.x;
  922.     pbe->y = prect->p.y, pbe->height = prect->q.y - prect->p.y;
  923.     } else {
  924.     gs_int_point size;
  925.     int code = (*pic->type->source_size) (pis, pic, &size);
  926.  
  927.     if (code < 0) {
  928.         gs_free_object(memory, pbe, "bbox_image_begin");
  929.         return code;
  930.     }
  931.     pbe->x0 = 0, pbe->x1 = size.x;
  932.     pbe->y = 0, pbe->height = size.y;
  933.     }
  934.     *ppbe = pbe;
  935.     return 0;
  936. }
  937.  
  938. private void
  939. bbox_image_copy_target_info(bbox_image_enum * pbe)
  940. {
  941.     const gx_image_enum_common_t *target_info = pbe->target_info;
  942.  
  943.     pbe->num_planes = target_info->num_planes;
  944.     memcpy(pbe->plane_depths, target_info->plane_depths,
  945.        pbe->num_planes * sizeof(pbe->plane_depths[0]));
  946.     memcpy(pbe->plane_widths, target_info->plane_widths,
  947.        pbe->num_planes * sizeof(pbe->plane_widths[0]));
  948. }
  949.  
  950. private int
  951. bbox_begin_typed_image(gx_device * dev,
  952.                const gs_imager_state * pis, const gs_matrix * pmat,
  953.            const gs_image_common_t * pic, const gs_int_rect * prect,
  954.                const gx_drawing_color * pdcolor,
  955.                const gx_clip_path * pcpath,
  956.                gs_memory_t * memory, gx_image_enum_common_t ** pinfo)
  957. {
  958.     bbox_image_enum *pbe;
  959.     int code =
  960.     bbox_image_begin(pis, pmat, pic, prect, pcpath, memory, &pbe);
  961.  
  962.     if (code < 0)
  963.     return code;
  964.     /*
  965.      * If there is no target, we still have to call default_begin_image
  966.      * to get the correct num_planes and plane_depths.
  967.      */
  968.     {
  969.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  970.     gx_device *tdev = bdev->target;
  971.     dev_proc_begin_typed_image((*begin_typed_image));
  972.     byte wanted[GS_IMAGE_MAX_PLANES];
  973.  
  974.     if (tdev == 0) {
  975.         tdev = dev;
  976.         begin_typed_image = gx_default_begin_typed_image;
  977.     } else {
  978.         begin_typed_image = dev_proc(tdev, begin_typed_image);
  979.     }
  980.     code = (*begin_typed_image)
  981.         (tdev, pis, pmat, pic, prect, pdcolor, pcpath, memory,
  982.          &pbe->target_info);
  983.     if (code) {
  984.         bbox_image_end_image((gx_image_enum_common_t *)pbe, false);
  985.         return code;
  986.     }
  987.     /*
  988.      * We fill in num_planes and plane_depths later.  format is
  989.      * irrelevant.  NOTE: we assume that if begin_typed_image returned
  990.      * 0, the image is a data image.
  991.      */
  992.     code = gx_image_enum_common_init((gx_image_enum_common_t *) pbe,
  993.                      (const gs_data_image_t *)pic,
  994.                      &bbox_image_enum_procs, dev,
  995.                      0, gs_image_format_chunky);
  996.     if (code < 0)
  997.         return code;
  998.     bbox_image_copy_target_info(pbe);
  999.     pbe->params_are_const =
  1000.         gx_image_planes_wanted(pbe->target_info, wanted);
  1001.     }
  1002.     *pinfo = (gx_image_enum_common_t *) pbe;
  1003.     return 0;
  1004. }
  1005.  
  1006. private int
  1007. bbox_image_plane_data(gx_image_enum_common_t * info,
  1008.               const gx_image_plane_t * planes, int height,
  1009.               int *rows_used)
  1010. {
  1011.     gx_device *dev = info->dev;
  1012.     gx_device_bbox *const bdev = (gx_device_bbox *)dev;
  1013.     gx_device *tdev = bdev->target;
  1014.     bbox_image_enum *pbe = (bbox_image_enum *) info;
  1015.     const gx_clip_path *pcpath = pbe->pcpath;
  1016.     gs_rect sbox, dbox;
  1017.     gs_point corners[4];
  1018.     gs_fixed_rect ibox;
  1019.     int code;
  1020.  
  1021.     code = gx_image_plane_data_rows(pbe->target_info, planes, height,
  1022.                     rows_used);
  1023.     if (code != 1 && !pbe->params_are_const)
  1024.     bbox_image_copy_target_info(pbe);
  1025.     sbox.p.x = pbe->x0;
  1026.     sbox.p.y = pbe->y;
  1027.     sbox.q.x = pbe->x1;
  1028.     sbox.q.y = pbe->y = min(pbe->y + height, pbe->height);
  1029.     gs_bbox_transform_only(&sbox, &pbe->matrix, corners);
  1030.     gs_points_bbox(corners, &dbox);
  1031.     ibox.p.x = float2fixed(dbox.p.x);
  1032.     ibox.p.y = float2fixed(dbox.p.y);
  1033.     ibox.q.x = float2fixed(dbox.q.x);
  1034.     ibox.q.y = float2fixed(dbox.q.y);
  1035.     if (pcpath != NULL &&
  1036.     !gx_cpath_includes_rectangle(pcpath, ibox.p.x, ibox.p.y,
  1037.                      ibox.q.x, ibox.q.y)
  1038.     ) {
  1039.     /* Let the target do the drawing, but drive two triangles */
  1040.     /* through the clipping path to get an accurate bounding box. */
  1041.     gx_device_clip cdev;
  1042.     gx_drawing_color devc;
  1043.     fixed x0 = float2fixed(corners[0].x), y0 = float2fixed(corners[0].y);
  1044.     fixed bx2 = float2fixed(corners[2].x) - x0, by2 = float2fixed(corners[2].y) - y0;
  1045.  
  1046.     gx_make_clip_path_device(&cdev, pcpath);
  1047.     cdev.target = dev;
  1048.     (*dev_proc(&cdev, open_device)) ((gx_device *) & cdev);
  1049.     color_set_pure(&devc, bdev->black);  /* any non-white color will do */
  1050.     bdev->target = NULL;
  1051.     gx_default_fill_triangle((gx_device *) & cdev, x0, y0,
  1052.                  float2fixed(corners[1].x) - x0,
  1053.                  float2fixed(corners[1].y) - y0,
  1054.                  bx2, by2, &devc, lop_default);
  1055.     gx_default_fill_triangle((gx_device *) & cdev, x0, y0,
  1056.                  float2fixed(corners[3].x) - x0,
  1057.                  float2fixed(corners[3].y) - y0,
  1058.                  bx2, by2, &devc, lop_default);
  1059.     bdev->target = tdev;
  1060.     } else {
  1061.     /* Just use the bounding box. */
  1062.     BBOX_ADD_RECT(bdev, ibox.p.x, ibox.p.y, ibox.q.x, ibox.q.y);
  1063.     }
  1064.     return code;
  1065. }
  1066.  
  1067. private int
  1068. bbox_image_end_image(gx_image_enum_common_t * info, bool draw_last)
  1069. {
  1070.     bbox_image_enum *pbe = (bbox_image_enum *) info;
  1071.     int code = gx_image_end(pbe->target_info, draw_last);
  1072.  
  1073.     gs_free_object(pbe->memory, pbe, "bbox_end_image");
  1074.     return code;
  1075. }
  1076.  
  1077. private int
  1078. bbox_image_flush(gx_image_enum_common_t * info)
  1079. {
  1080.     bbox_image_enum *pbe = (bbox_image_enum *) info;
  1081.     gx_image_enum_common_t *target_info = pbe->target_info;
  1082.  
  1083.     return (target_info ? gx_image_flush(target_info) : 0);
  1084. }
  1085.  
  1086. private bool
  1087. bbox_image_planes_wanted(const gx_image_enum_common_t * info, byte *wanted)
  1088. {
  1089.     /* This is only used if target_info != 0. */
  1090.     const bbox_image_enum *pbe = (const bbox_image_enum *)info;
  1091.     
  1092.     return gx_image_planes_wanted(pbe->target_info, wanted);
  1093. }
  1094.  
  1095. /* Compositing */
  1096.  
  1097. private bool
  1098. bbox_forward_init_box(void *pdata)
  1099. {
  1100.     gx_device_bbox *const bdev = (gx_device_bbox *)pdata;
  1101.  
  1102.     return BBOX_INIT_BOX(bdev);
  1103. }
  1104. private void
  1105. bbox_forward_get_box(const void *pdata, gs_fixed_rect *pbox)
  1106. {
  1107.     const gx_device_bbox *const bdev = (const gx_device_bbox *)pdata;
  1108.  
  1109.     BBOX_GET_BOX(bdev, pbox);
  1110. }
  1111. private void
  1112. bbox_forward_add_rect(void *pdata, fixed x0, fixed y0, fixed x1, fixed y1)
  1113. {
  1114.     gx_device_bbox *const bdev = (gx_device_bbox *)pdata;
  1115.  
  1116.     BBOX_ADD_RECT(bdev, x0, y0, x1, y1);
  1117. }
  1118. private bool
  1119. bbox_forward_in_rect(const void *pdata, const gs_fixed_rect *pbox)
  1120. {
  1121.     const gx_device_bbox *const bdev = (const gx_device_bbox *)pdata;
  1122.  
  1123.     return BBOX_IN_RECT(bdev, pbox);
  1124. }
  1125. private const gx_device_bbox_procs_t box_procs_forward = {
  1126.     bbox_forward_init_box, bbox_forward_get_box, bbox_forward_add_rect,
  1127.     bbox_forward_in_rect
  1128. };
  1129.  
  1130. private int
  1131. bbox_create_compositor(gx_device * dev,
  1132.                gx_device ** pcdev, const gs_composite_t * pcte,
  1133.                const gs_imager_state * pis, gs_memory_t * memory)
  1134. {
  1135.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  1136.     gx_device *target = bdev->target;
  1137.  
  1138.     /*
  1139.      * If there isn't a target, all we care about is the bounding box,
  1140.      * so don't bother with actually compositing.
  1141.      */
  1142.     if (target == 0) {
  1143.     *pcdev = dev;
  1144.     return 0;
  1145.     }
  1146.     /*
  1147.      * Create a compositor for the target, and then wrap another
  1148.      * bbox device around it, but still accumulating the bounding
  1149.      * box in the same place.
  1150.      */
  1151.     {
  1152.     gx_device *cdev;
  1153.     gx_device_bbox *bbcdev;
  1154.     int code = (*dev_proc(target, create_compositor))
  1155.         (target, &cdev, pcte, pis, memory);
  1156.  
  1157.     if (code < 0)
  1158.         return code;
  1159.     bbcdev = gs_alloc_struct_immovable(memory, gx_device_bbox,
  1160.                        &st_device_bbox,
  1161.                        "bbox_create_compositor");
  1162.     if (bbcdev == 0) {
  1163.         (*dev_proc(cdev, close_device)) (cdev);
  1164.         return_error(gs_error_VMerror);
  1165.     }
  1166.     gx_device_bbox_init(bbcdev, target);
  1167.     gx_device_set_target((gx_device_forward *)bbcdev, cdev);
  1168.     bbcdev->box_procs = box_procs_forward;
  1169.     bbcdev->box_proc_data = bdev;
  1170.     *pcdev = (gx_device *) bbcdev;
  1171.     return 0;
  1172.     }
  1173. }
  1174.  
  1175. /* ------ Text imaging ------ */
  1176.  
  1177. extern_st(st_gs_text_enum);
  1178.  
  1179. typedef struct bbox_text_enum_s {
  1180.     gs_text_enum_common;
  1181.     gs_text_enum_t *target_info;
  1182. } bbox_text_enum;
  1183.  
  1184. gs_private_st_suffix_add1(st_bbox_text_enum, bbox_text_enum, "bbox_text_enum",
  1185.             bbox_text_enum_enum_ptrs, bbox_text_enum_reloc_ptrs,
  1186.               st_gs_text_enum, target_info);
  1187.  
  1188. private text_enum_proc_resync(bbox_text_resync);
  1189. private text_enum_proc_process(bbox_text_process);
  1190. private text_enum_proc_is_width_only(bbox_text_is_width_only);
  1191. private text_enum_proc_current_width(bbox_text_current_width);
  1192. private text_enum_proc_set_cache(bbox_text_set_cache);
  1193. private text_enum_proc_retry(bbox_text_retry);
  1194. private text_enum_proc_release(bbox_text_release);
  1195. private rc_free_proc(bbox_text_free);
  1196.  
  1197. private const gs_text_enum_procs_t bbox_text_procs =
  1198. {
  1199.     bbox_text_resync, bbox_text_process, bbox_text_is_width_only,
  1200.     bbox_text_current_width, bbox_text_set_cache, bbox_text_retry,
  1201.     bbox_text_release
  1202. };
  1203.  
  1204. private void
  1205. bbox_text_enum_copy(bbox_text_enum *pbte)
  1206. {
  1207.     rc_header rc_save;
  1208.  
  1209.     rc_save = pbte->rc;
  1210.     *(gs_text_enum_t *)pbte = *pbte->target_info;    /* copy common info */
  1211.     pbte->rc = rc_save;
  1212.     pbte->procs = &bbox_text_procs;
  1213. }
  1214.  
  1215. private int
  1216. bbox_text_begin(gx_device * dev, gs_imager_state * pis,
  1217.         const gs_text_params_t * text, gs_font * font,
  1218.         gx_path * path, const gx_device_color * pdcolor,
  1219.         const gx_clip_path * pcpath,
  1220.         gs_memory_t * memory, gs_text_enum_t ** ppenum)
  1221. {
  1222.     gx_device_bbox *const bdev = (gx_device_bbox *) dev;
  1223.     gx_device *tdev = bdev->target;
  1224.     bbox_text_enum *pbte;
  1225.     int code;
  1226.  
  1227.     if (tdev == 0)
  1228.     return gx_default_text_begin(dev, pis, text, font, path, pdcolor,
  1229.                      pcpath, memory, ppenum);
  1230.     rc_alloc_struct_1(pbte, bbox_text_enum, &st_bbox_text_enum, memory,
  1231.               return_error(gs_error_VMerror),
  1232.               "bbox_text_begin");
  1233.     pbte->rc.free = bbox_text_free;
  1234.     code =
  1235.     (*dev_proc(tdev, text_begin))
  1236.     (tdev, pis, text, font, path, pdcolor, pcpath, memory,
  1237.      &pbte->target_info);
  1238.     if (code < 0) {
  1239.     gs_free_object(memory, pbte, "bbox_text_begin");
  1240.     return code;
  1241.     }
  1242.     bbox_text_enum_copy(pbte);
  1243.     *ppenum = (gs_text_enum_t *) pbte;
  1244.     return code;
  1245. }
  1246.  
  1247. private int
  1248. bbox_text_resync(gs_text_enum_t *pte, const gs_text_enum_t *pfrom)
  1249. {
  1250.     bbox_text_enum *const pbte = (bbox_text_enum *) pte;
  1251.     int code = gs_text_resync(pbte->target_info, pfrom);
  1252.  
  1253.     if (code >= 0)
  1254.     bbox_text_enum_copy(pbte);
  1255.     return code;
  1256. }
  1257.  
  1258. private int
  1259. bbox_text_process(gs_text_enum_t * pte)
  1260. {
  1261.     bbox_text_enum *const pbte = (bbox_text_enum *) pte;
  1262.     int code = gs_text_process(pbte->target_info);
  1263.  
  1264.     gs_text_enum_copy_dynamic(pte, pbte->target_info, true);
  1265.     return code;
  1266. }
  1267.  
  1268. private bool
  1269. bbox_text_is_width_only(const gs_text_enum_t *pte)
  1270. {
  1271.     const bbox_text_enum *const pbte = (const bbox_text_enum *)pte;
  1272.  
  1273.     return gs_text_is_width_only(pbte->target_info);
  1274. }
  1275.  
  1276. private int
  1277. bbox_text_current_width(const gs_text_enum_t *pte, gs_point *pwidth)
  1278. {
  1279.     const bbox_text_enum *const pbte = (const bbox_text_enum *)pte;
  1280.  
  1281.     return gs_text_current_width(pbte->target_info, pwidth);
  1282. }
  1283.  
  1284. private int
  1285. bbox_text_set_cache(gs_text_enum_t * pte, const double *values,
  1286.             gs_text_cache_control_t control)
  1287. {
  1288.     bbox_text_enum *const pbte = (bbox_text_enum *) pte;
  1289.     gs_text_enum_t *tpte = pbte->target_info;
  1290.     int code = gs_text_set_cache(tpte, values, control);
  1291.  
  1292.     if (code < 0)
  1293.     return code;
  1294.     /* Copy back the dynamic information for the client. */
  1295.     pte->index = tpte->index;
  1296.     return code;
  1297. }
  1298.  
  1299. private int
  1300. bbox_text_retry(gs_text_enum_t *pte)
  1301. {
  1302.     bbox_text_enum *const pbte = (bbox_text_enum *) pte;
  1303.  
  1304.     return gs_text_retry(pbte->target_info);
  1305. }
  1306.  
  1307. private void
  1308. bbox_text_release(gs_text_enum_t *pte, client_name_t cname)
  1309. {
  1310.     bbox_text_enum *const pbte = (bbox_text_enum *)pte;
  1311.  
  1312.     gs_text_release(pbte->target_info, cname);
  1313. }
  1314.  
  1315. private void
  1316. bbox_text_free(gs_memory_t * memory, void *vpte, client_name_t cname)
  1317. {
  1318.     bbox_text_release((gs_text_enum_t *)vpte, cname);
  1319.     rc_free_struct_only(memory, vpte, cname);
  1320. }
  1321.