home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GSDPARAM.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  9KB  |  288 lines

  1. /* Copyright (C) 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gsdparam.c */
  20. /* Default device parameters for Ghostscript library */
  21. #include "memory_.h"            /* for memcpy */
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsparam.h"
  25. #include "gxdevice.h"
  26. #include "gxfixed.h"
  27.  
  28. /* Get the device parameters. */
  29. int
  30. gs_getdeviceparams(gx_device *dev, gs_param_list *plist)
  31. {    gx_device_set_procs(dev);
  32.     fill_dev_proc(dev, get_params, gx_default_get_params);
  33.     return (*dev_proc(dev, get_params))(dev, plist);
  34. }
  35.  
  36. /* Get standard parameters. */
  37. int
  38. gx_default_get_params(gx_device *dev, gs_param_list *plist)
  39. {
  40.     int code;
  41.  
  42.     /* Standard page device parameters: */
  43.  
  44.     gs_param_string dns;
  45.     float HWResolution[2];
  46.     gs_param_float_array psa, ibba, hwra, ma;
  47. #define set_param_array(a, d, s)\
  48.   (a.data = d, a.size = s, a.persistent = false);
  49.  
  50.     /* Non-standard parameters: */
  51.  
  52.     int colors = dev->color_info.num_components;
  53.     int depth = dev->color_info.depth;
  54.     int GrayValues = dev->color_info.max_gray + 1;
  55.     int HWSize[2];
  56.       gs_param_int_array hwsa;
  57.     float InitialMatrix[6];
  58.       gs_param_float_array ima;
  59.  
  60.     /* Fill in page device parameters. */
  61.  
  62.     param_string_from_string(dns, dev->dname);
  63.     HWResolution[0] = dev->x_pixels_per_inch;
  64.     HWResolution[1] = dev->y_pixels_per_inch;
  65.     set_param_array(hwra, HWResolution, 2);
  66.     set_param_array(psa, dev->page.page_size, 2);
  67.     set_param_array(ibba, dev->page.imaging_bbox, 4);
  68.     set_param_array(ma, dev->page.margins, 2);
  69.  
  70.     /* Fill in non-standard parameters. */
  71.  
  72.     HWSize[0] = dev->width;
  73.     HWSize[1] = dev->height;
  74.     set_param_array(hwsa, HWSize, 2);
  75.     fill_dev_proc(dev, get_initial_matrix, gx_default_get_initial_matrix);
  76.     {    gs_matrix mat;
  77.         (*dev_proc(dev, get_initial_matrix))(dev, &mat);
  78.         InitialMatrix[0] = mat.xx;
  79.         InitialMatrix[1] = mat.xy;
  80.         InitialMatrix[2] = mat.yx;
  81.         InitialMatrix[3] = mat.yy;
  82.         InitialMatrix[4] = mat.tx;
  83.         InitialMatrix[5] = mat.ty;
  84.     }
  85.     set_param_array(ima, InitialMatrix, 6);
  86.  
  87.     /* Transmit the values. */
  88.  
  89.     if (
  90.             /* Standard parameters */
  91.  
  92.          (code = param_write_name(plist, "OutputDevice", &dns)) < 0 ||
  93.          (code = param_write_float_array(plist, "HWResolution", &hwra)) < 0 ||
  94.          (code = param_write_float_array(plist, "PageSize", &psa)) < 0 ||
  95.          (code = (dev->page.imaging_bbox_set ?
  96.               param_write_float_array(plist, "ImagingBBox", &ibba) :
  97.               param_write_null(plist, "ImagingBBox"))) < 0 ||
  98.          (code = param_write_float_array(plist, "Margins", &ma)) < 0 ||
  99.          (code = (dev->page.orientation_set ?
  100.               param_write_int(plist, "Orientation", &dev->page.orientation) :
  101.               param_write_null(plist, "Orientation"))) < 0 ||
  102.  
  103.             /* Non-standard parameters */
  104.  
  105.          (code = param_write_int_array(plist, "HWSize", &hwsa)) < 0 ||
  106.          (code = param_write_float_array(plist, "InitialMatrix", &ima)) < 0 ||
  107.          (code = param_write_string(plist, "Name", &dns)) < 0 ||
  108.          (code = param_write_int(plist, "Colors", &colors)) < 0 ||
  109.          (code = param_write_int(plist, "HWBitsPerPixel", &depth)) < 0 ||
  110.          (code = param_write_int(plist, "GrayValues", &GrayValues)) < 0 ||
  111.          (code = param_write_int(plist, "PageCount", &dev->page_count)) < 0
  112.  
  113.        )
  114.         return code;
  115.  
  116.     /* Fill in color information. */
  117.  
  118.     if ( colors > 1 )
  119.     {    int RGBValues = dev->color_info.max_rgb + 1;
  120.         long ColorValues = 1L << depth;
  121.         if ( (code = param_write_int(plist, "RedValues", &RGBValues)) < 0 ||
  122.              (code = param_write_int(plist, "GreenValues", &RGBValues)) < 0 ||
  123.              (code = param_write_int(plist, "BlueValues", &RGBValues)) < 0 ||
  124.              (code = param_write_long(plist, "ColorValues", &ColorValues)) < 0
  125.            )
  126.             return code;
  127.     }
  128.  
  129.     if ( depth <= 8 && colors <= 3 )
  130.     {    byte palette[3 << 8];
  131.         byte *p = palette;
  132.         gs_param_string hwcms;
  133.         gx_color_value rgb[3];
  134.         gx_color_index i;
  135.         if ( palette == 0 )
  136.             return_error(gs_error_VMerror);
  137.         for ( i = 0; (i >> depth) == 0; i++ )
  138.         {    int j;
  139.             (*dev_proc(dev, map_color_rgb))(dev, i, rgb);
  140.             for ( j = 0; j < colors; j++ )
  141.                 *p++ = gx_color_value_to_byte(rgb[j]);
  142.         }
  143.         hwcms.data = palette, hwcms.size = colors << depth, hwcms.persistent = false;
  144.         if ( (code = param_write_string(plist, "HWColorMap", &hwcms)) < 0 )
  145.             return code;
  146.     }
  147.  
  148.     return 0;
  149. }
  150.  
  151. /* Set the device parameters. */
  152. /* If the device was open and the put_params procedure closed it, */
  153. /* return 1; otherwise, return 0 or an error code as usual. */
  154. int
  155. gs_putdeviceparams(gx_device *dev, gs_param_list *plist)
  156. {    bool was_open = dev->is_open;
  157.     int code;
  158.     fill_dev_proc(dev, put_params, gx_default_put_params);
  159.     code = (*dev_proc(dev, put_params))(dev, plist);
  160.     return (code < 0 ? code : was_open && !dev->is_open ? 1 : code);
  161. }
  162.  
  163. /* Set standard parameters. */
  164. /* Note that setting the size or resolution closes the device. */
  165. /* We break out a procedure that only sets the parameters, */
  166. /* without closing or reopening the device, for the benefit of */
  167. /* window devices that need to do the latter themselves. */
  168. int
  169. gx_default_put_params_only(gx_device *dev, gs_param_list *plist)
  170. {    int ecode = 0, rcode = 0, code;
  171.     gs_param_int_array hwsa;
  172.     gs_param_float_array hwra, psa;
  173.     float page_width, page_height;
  174.  
  175.     /* We check the parameters in the order HWResolution, HWSize, */
  176.     /* PageSize so that PageSize supersedes HWSize and so that */
  177.     /* HWSize and PageSize each have the resolution available to */
  178.     /* compute the other one. */
  179.  
  180.     switch ( code = param_read_float_array(plist, "HWResolution", &hwra) )
  181.     {
  182.     default:
  183.         ecode = code;
  184.     case 1:
  185.         break;
  186.     case 0:
  187.         if ( hwra.size != 2 )
  188.             ecode = gs_error_typecheck;
  189. #define ap hwra.data
  190.         else if ( ap[0] <= 0 || ap[1] <= 0 )
  191.             ecode = gs_error_rangecheck;
  192.         else
  193.           {    dev->x_pixels_per_inch = ap[0];
  194.             dev->y_pixels_per_inch = ap[1];
  195. #undef ap
  196.             rcode = 1;
  197.             break;
  198.           }
  199.         param_signal_error(plist, "HWResolution", ecode);
  200.     }
  201.  
  202.     switch ( code = param_read_int_array(plist, "HWSize", &hwsa) )
  203.     {
  204.     default:
  205.         ecode = code;
  206.     case 1:
  207.         break;
  208.     case 0:
  209.         if ( hwsa.size != 2 )
  210.             ecode = gs_error_typecheck;
  211. #define ap hwsa.data
  212.         else if ( ap[0] <= 0 || ap[1] <= 0 )
  213.             ecode = gs_error_rangecheck;
  214. #define max_coord (max_fixed / fixed_1)
  215. #if max_coord < max_int
  216.         else if ( ap[0] > max_coord || ap[1] > max_coord )
  217.             ecode = gs_error_limitcheck;
  218. #endif
  219. #undef max_coord
  220.         else
  221.           {    dev->width = ap[0];
  222.             dev->height = ap[1];
  223.             dev->page.page_size[0] =
  224.               ap[0] * 72 / dev->x_pixels_per_inch;
  225.             dev->page.page_size[1] =
  226.               ap[1] * 72 / dev->y_pixels_per_inch;
  227. #undef ap
  228.             rcode = 1;
  229.             break;
  230.           }
  231.         param_signal_error(plist, "HWSize", ecode);
  232.     }
  233.  
  234.     switch ( code = param_read_float_array(plist, "PageSize", &psa) )
  235.     {
  236.     default:
  237.         ecode = code;
  238.     case 1:
  239.         break;
  240.     case 0:
  241.         if ( psa.size != 2 )
  242.             ecode = gs_error_typecheck;
  243. #define ap psa.data
  244.         else if ( (page_width = ap[0] * dev->x_pixels_per_inch / 72) <= 0 ||
  245.               (page_height = ap[1] * dev->y_pixels_per_inch / 72) <= 0
  246.             )
  247.             ecode = gs_error_rangecheck;
  248. #define max_coord (max_fixed / fixed_1)
  249. #if max_coord < max_int
  250.         else if ( page_width > max_coord || page_height > max_coord )
  251.             ecode = gs_error_limitcheck;
  252. #endif
  253. #undef max_coord
  254.         else
  255.           {    dev->width = page_width;
  256.             dev->height = page_height;
  257.             dev->page.page_size[0] = ap[0];
  258.             dev->page.page_size[1] = ap[1];
  259. #undef ap
  260.             rcode = 1;
  261.             break;
  262.           }
  263.         param_signal_error(plist, "HWSize", ecode);
  264.     }
  265.  
  266.     return (ecode != 0 ? ecode : rcode);
  267. }
  268. int
  269. gx_default_put_params(gx_device *dev, gs_param_list *plist)
  270. {    gx_device temp_dev;
  271.     int code;
  272.     temp_dev = *dev;
  273.     code = gx_default_put_params_only(&temp_dev, plist);
  274.     if ( code < 0 )
  275.         return code;
  276.     /* Close the device; gs_putdeviceparams will reopen it. */
  277.     if ( dev->is_open && code )
  278.     {    int ccode = gs_closedevice(dev);
  279.         if ( ccode < 0 ) return ccode;
  280.     }
  281.     dev->x_pixels_per_inch = temp_dev.x_pixels_per_inch;
  282.     dev->y_pixels_per_inch = temp_dev.y_pixels_per_inch;
  283.     dev->width = temp_dev.width;
  284.     dev->height = temp_dev.height;
  285.     dev->page = temp_dev.page;
  286.     return code;
  287. }
  288.