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

  1. /* Copyright (C) 1993 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. /* gsprops.c */
  20. /* Default device properties 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 properties. */
  29. int
  30. gs_getdeviceprops(gx_device *dev, gs_prop_list *plist)
  31. {    gx_device_set_procs(dev);
  32.     fill_dev_proc(dev, get_props, gx_default_get_props);
  33.     return (*dev_proc(dev, get_props))(dev, plist);
  34. }
  35.  
  36. /* Get standard properties. */
  37. int
  38. gx_default_get_props(gx_device *dev, gs_prop_list *plist)
  39. {    int colors = dev->color_info.num_components;
  40.     int depth = dev->color_info.depth;
  41.     int GrayValues = dev->color_info.max_gray + 1;
  42.     int code;
  43.     float HWResolution[2];
  44.       gs_prop_float_array hwra;
  45.     int HWSize[2];
  46.       gs_prop_int_array hwsa;
  47.     gs_matrix mat;
  48.     float InitialMatrix[6];
  49.       gs_prop_float_array ima;
  50.     gs_prop_string dns;
  51.  
  52.     /* Fill in values common to all devices. */
  53.  
  54.     HWResolution[0] = dev->x_pixels_per_inch;
  55.     HWResolution[1] = dev->y_pixels_per_inch;
  56.     hwra.data = HWResolution, hwra.size = 2, hwra.persistent = false;
  57.     HWSize[0] = dev->width;
  58.     HWSize[1] = dev->height;
  59.     hwsa.data = HWSize, hwsa.size = 2, hwsa.persistent = false;
  60.     fill_dev_proc(dev, get_initial_matrix, gx_default_get_initial_matrix);
  61.     (*dev_proc(dev, get_initial_matrix))(dev, &mat);
  62.     InitialMatrix[0] = mat.xx;
  63.     InitialMatrix[1] = mat.xy;
  64.     InitialMatrix[2] = mat.yx;
  65.     InitialMatrix[3] = mat.yy;
  66.     InitialMatrix[4] = mat.tx;
  67.     InitialMatrix[5] = mat.ty;
  68.     ima.data = InitialMatrix, ima.size = 6, ima.persistent = false;
  69.     prop_string_from_string(dns, dev->dname);
  70.  
  71.     if ( (code = prop_float_array_param(plist, "HWResolution", &hwra)) < 0 ||
  72.          (code = prop_int_array_param(plist, "HWSize", &hwsa)) < 0 ||
  73.          (code = prop_float_array_param(plist, "InitialMatrix", &ima)) < 0 ||
  74.          (code = prop_string_param(plist, "Name", &dns)) < 0 ||
  75.          (code = prop_int_param(plist, "Colors", &colors)) < 0 ||
  76.          (code = prop_int_param(plist, "HWBitsPerPixel", &depth)) < 0 ||
  77.          (code = prop_int_param(plist, "GrayValues", &GrayValues)) < 0 ||
  78.          (code = prop_int_param(plist, "PageCount", &dev->page_count)) < 0
  79.        )
  80.         return code;
  81.  
  82.     /* Fill in color information. */
  83.  
  84.     if ( colors > 1 )
  85.     {    int RGBValues = dev->color_info.max_rgb + 1;
  86.         long ColorValues = 1L << depth;
  87.         if ( (code = prop_int_param(plist, "RedValues", &RGBValues)) < 0 ||
  88.              (code = prop_int_param(plist, "GreenValues", &RGBValues)) < 0 ||
  89.              (code = prop_int_param(plist, "BlueValues", &RGBValues)) < 0 ||
  90.              (code = prop_long_param(plist, "ColorValues", &ColorValues)) < 0
  91.            )
  92.             return code;
  93.     }
  94.  
  95.     if ( depth <= 8 && colors <= 3 )
  96.     {    byte palette[3 << 8];
  97.         byte *p = palette;
  98.         gs_prop_string hwcms;
  99.         gx_color_value rgb[3];
  100.         gx_color_index i;
  101.         if ( palette == 0 )
  102.             return_error(gs_error_VMerror);
  103.         for ( i = 0; (i >> depth) == 0; i++ )
  104.         {    int j;
  105.             (*dev_proc(dev, map_color_rgb))(dev, i, rgb);
  106.             for ( j = 0; j < colors; j++ )
  107.                 *p++ = gx_color_value_to_byte(rgb[j]);
  108.         }
  109.         hwcms.data = palette, hwcms.size = colors << depth, hwcms.persistent = false;
  110.         if ( (code = prop_string_param(plist, "HWColorMap", &hwcms)) < 0 )
  111.             return code;
  112.     }
  113.  
  114.     return 0;
  115. }
  116.  
  117. /* Set the device properties. */
  118. /* If the device was open and the put_props procedure closed it, */
  119. /* return 1; otherwise, return 0 or an error code as usual. */
  120. int
  121. gs_putdeviceprops(gx_device *dev, gs_prop_list *plist)
  122. {    bool was_open = dev->is_open;
  123.     int code;
  124.     fill_dev_proc(dev, put_props, gx_default_put_props);
  125.     code = (*dev_proc(dev, put_props))(dev, plist);
  126.     return (code < 0 ? code : was_open && !dev->is_open ? 1 : code);
  127. }
  128.  
  129. /* Set standard properties. */
  130. /* Note that setting the size or resolution closes the device. */
  131. /* We break out a procedure that only sets the properties, */
  132. /* without closing or reopening the device, for the benefit of */
  133. /* window devices that need to do the latter themselves. */
  134. int
  135. gx_default_put_props_only(gx_device *dev, gs_prop_list *plist)
  136. {    int code = 0;
  137.     gs_prop_int_array hwsa;
  138.     gs_prop_float_array hwra;
  139.  
  140.     switch ( code = prop_int_array_param(plist, "HWSize", &hwsa) )
  141.     {
  142.     default:
  143.         return code;
  144.     case 1:
  145.         break;
  146.     case 0:
  147.         if ( hwsa.size != 2 )
  148.             return_error(gs_error_typecheck);
  149. #define ap hwsa.data
  150.         if ( ap[0] <= 0 || ap[1] <= 0 )
  151.             return_error(gs_error_rangecheck);
  152. #define max_coord (max_fixed / fixed_1)
  153. #if max_coord < max_int
  154.         if ( ap[0] > max_coord || ap[1] > max_coord )
  155.             return_error(gs_error_limitcheck);
  156. #endif
  157. #undef max_coord
  158.         dev->width = ap[0];
  159.         dev->height = ap[1];
  160. #undef ap
  161.         code = 1;
  162.     }
  163.  
  164.     switch ( code = prop_float_array_param(plist, "HWResolution", &hwra) )
  165.     {
  166.     default:
  167.         return code;
  168.     case 1:
  169.         break;
  170.     case 0:
  171.         if ( hwra.size != 2 )
  172.             return_error(gs_error_typecheck);
  173. #define ap hwra.data
  174.         if ( ap[0] <= 0 || ap[1] <= 0 )
  175.             return_error(gs_error_rangecheck);
  176.         dev->x_pixels_per_inch = ap[0];
  177.         dev->y_pixels_per_inch = ap[1];
  178. #undef ap
  179.         code = 1;
  180.     }
  181.  
  182.     return code;
  183. }
  184. int
  185. gx_default_put_props(gx_device *dev, gs_prop_list *plist)
  186. {    gx_device temp_dev;
  187.     int code;
  188.     temp_dev = *dev;
  189.     code = gx_default_put_props_only(&temp_dev, plist);
  190.     if ( code < 0 )
  191.         return code;
  192.     /* Close the device; gs_putdeviceprops will reopen it. */
  193.     if ( dev->is_open && code )
  194.     {    int ccode = gs_closedevice(dev);
  195.         if ( ccode < 0 ) return ccode;
  196.     }
  197.     dev->x_pixels_per_inch = temp_dev.x_pixels_per_inch;
  198.     dev->y_pixels_per_inch = temp_dev.y_pixels_per_inch;
  199.     dev->width = temp_dev.width;
  200.     dev->height = temp_dev.height;
  201.     return code;
  202. }
  203.