home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GSSTATE.C < prev    next >
C/C++ Source or Header  |  1992-09-10  |  12KB  |  395 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsstate.c */
  21. /* Miscellaneous graphics state operators for Ghostscript library */
  22. #include "gx.h"
  23. #include "memory_.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"            /* for gzstate */
  27. #include "gzstate.h"
  28. #include "gzdevice.h"
  29. #include "gzcolor.h"            /* requires gxdevice.h */
  30. #include "gzht.h"
  31. #include "gzline.h"
  32. #include "gzpath.h"
  33.  
  34. /* Forward references */
  35. private gs_state *alloc_gstate(P4(proc_alloc_t, proc_free_t, const gs_state *, const char *));
  36. private int alloc_gstate_contents(P1(gs_state *));
  37. private void free_gstate_contents(P1(gs_state *));
  38. private void copy_gstate_contents(P2(gs_state *pto, const gs_state *pfrom));
  39.  
  40. /* Allocate and free a structure */
  41. #define alloc_struct(pgs,typ,cname)\
  42.   (typ *)(*(pgs)->memory_procs.alloc)(1, sizeof(typ), cname)
  43. #define free_struct(pgs,ptr,cname)\
  44.   (*(pgs)->memory_procs.free)((char *)(ptr), 1, sizeof(*(ptr)), cname)
  45.  
  46. /* ------ Operations on the entire graphics state ------ */
  47.  
  48. /* Allocate and initialize a graphics state. */
  49. private float
  50. null_transfer(gs_state *pgs, floatp gray)
  51. {    return gray;
  52. }
  53. gs_state *
  54. gs_state_alloc(proc_alloc_t palloc, proc_free_t pfree)
  55. {    register gs_state *pgs = alloc_gstate(palloc, pfree, (gs_state *)0, "gs_state_alloc");
  56.     if ( pgs == 0 ) return 0;
  57.     pgs->saved = 0;
  58.     /* Initialize things not covered by initgraphics */
  59.     gx_path_init(pgs->path, &pgs->memory_procs);
  60.     gx_path_init(&pgs->clip_path->path, &pgs->memory_procs);
  61.     gx_clip_list_init(&pgs->clip_path->list);
  62.     pgs->halftone->width = pgs->halftone->height =
  63.         pgs->halftone->order_size = 0;
  64.     gs_sethalftonephase(pgs, 0, 0);
  65.     /* Initialize the color so that gx_remap_color won't crash. */
  66.     gx_set_gray_only(pgs->color, (color_param)0);
  67.     gs_settransfer(pgs, null_transfer);
  68.     gs_nulldevice(pgs);
  69.     gs_setflat(pgs, 1.0);
  70.     gs_setstrokeadjust(pgs, 1);
  71.     /****** What about the font ? ******/
  72.     pgs->in_cachedevice = pgs->in_charpath = 0;
  73.     pgs->level = 0;
  74.     pgs->client_data = 0;
  75.     if ( gs_initgraphics(pgs) < 0 )
  76.        {    /* Something went very wrong */
  77.         return 0;
  78.        }
  79.     return pgs;
  80. }
  81.  
  82. /* Set the client data in a graphics state. */
  83. /* This should only be done to a newly created state. */
  84. void
  85. gs_state_set_client(gs_state *pgs, char/*void*/ *pdata, const gs_state_client_procs *pprocs)
  86. {    pgs->client_data = pdata;
  87.     pgs->client_procs = *pprocs;
  88. }
  89.  
  90. /* Get the client data from a graphics state. */
  91. char/*void*/ *
  92. gs_state_client_data(gs_state *pgs)
  93. {    return pgs->client_data;
  94. }
  95.  
  96. /* Free a graphics state */
  97. int
  98. gs_state_free(gs_state *pgs)
  99. {    free_gstate_contents(pgs);
  100.     free_struct(pgs, pgs, "gs_state_free");
  101.     return 0;
  102. }
  103.  
  104. /* Save the graphics state */
  105. int
  106. gs_gsave(gs_state *pgs)
  107. {    gs_state *pnew = alloc_struct(pgs, gs_state, "gs_gsave");
  108.     if ( pnew == 0 )
  109.         return_error(gs_error_VMerror);
  110.     *pnew = *pgs;
  111.     if ( alloc_gstate_contents(pgs) < 0 )
  112.        {    *pgs = *pnew;        /* undo partial alloc */
  113.         free_struct(pgs, pnew, "gs_gsave");
  114.         return_error(gs_error_VMerror);
  115.        }
  116.     copy_gstate_contents(pgs, pnew);
  117.     /* Make pnew, not pgs, have the newly-allocated client data. */
  118.     {    char/*void*/ *pdata = pgs->client_data;
  119.         pgs->client_data = pnew->client_data;
  120.         pnew->client_data = pdata;
  121.     }
  122.     pgs->saved = pnew;
  123.     pgs->level++;
  124.     if_debug2('g', "[g]save -> 0x%lx, level = %d\n",
  125.           (ulong)pnew, pgs->level);
  126.     return 0;
  127. }
  128.  
  129. /* Restore the graphics state. */
  130. int
  131. gs_grestore(gs_state *pgs)
  132. {    gs_state *saved = pgs->saved;
  133.     char/*void*/ *pdata = pgs->client_data;
  134.     if_debug2('g', "[g]restore 0x%lx, level was %d\n",
  135.           (ulong)saved, pgs->level);
  136.     if ( !saved ) return gs_gsave(pgs);    /* shouldn't happen */
  137.     pgs->client_data = 0;        /* don't free it */
  138.     free_gstate_contents(pgs);
  139.     *pgs = *saved;
  140.     if ( pgs->client_data != 0 )
  141.     {    if ( pdata != 0 )
  142.             (*pgs->client_procs.copy)(pdata, pgs->client_data);
  143.         (*pgs->client_procs.free)(pgs->client_data, &pgs->memory_procs);
  144.     }
  145.     pgs->client_data = pdata;
  146.     free_struct(pgs, saved, "gs_grestore");
  147.     return (pgs->saved == 0 ? gs_gsave(pgs) : 0);
  148. }
  149.  
  150. /* Restore to the bottommost graphics state. */
  151. int
  152. gs_grestoreall(gs_state *pgs)
  153. {    if ( !pgs->saved ) return gs_gsave(pgs);    /* shouldn't happen */
  154.     while ( pgs->saved->saved ) gs_grestore(pgs);
  155.     return gs_grestore(pgs);
  156. }
  157.  
  158. /* Allocate and return a new graphics state. */
  159. gs_state *
  160. gs_gstate(gs_state *pgs)
  161. {    gs_state *pnew = alloc_gstate(pgs->memory_procs.alloc, pgs->memory_procs.free, pgs, "gs_gstate");
  162.     if ( pnew == 0 ) return 0;
  163.     copy_gstate_contents(pnew, pgs);
  164.     pnew->saved = 0;
  165.     return pnew;
  166. }
  167.  
  168. /* Copy the current graphics state to a previously allocated one. */
  169. int
  170. gs_currentgstate(gs_state *pto, const gs_state *pgs)
  171. {    /* We have to copy both the scalar and composite parts */
  172.     /* of the state. */
  173.     gs_state sgs;
  174.     sgs = *pto;
  175.     *pto = *pgs;
  176.     /* Put back the composite part pointers. */
  177. #define gcopy(element)\
  178.     pto->element = sgs.element
  179.     gcopy(path);
  180.     gcopy(clip_path);
  181.     gcopy(line_params);
  182.     gcopy(halftone);
  183.     gcopy(color);
  184.     gcopy(dev_color);
  185.     gcopy(transfer);
  186.     gcopy(device);
  187.     gcopy(client_data);
  188. #undef gcopy
  189.     copy_gstate_contents(pto, pgs);
  190.     return 0;
  191. }
  192.  
  193. /* Restore the current graphics state from a previously allocated one. */
  194. int
  195. gs_setgstate(gs_state *pgs, const gs_state *pfrom)
  196. {    /* The implementation is the same as currentgstate, */
  197.     /* except we must preserve the saved pointer and the level. */
  198.     gs_state *saved = pgs->saved;
  199.     int level = pgs->level;
  200.     int code = gs_currentgstate(pgs, pfrom);
  201.     if ( code < 0 ) return code;
  202.     pgs->saved = saved;
  203.     pgs->level = level;
  204.     return 0;
  205. }
  206.  
  207. /* Swap the saved pointer of the graphics state. */
  208. /* This is provided only for save/restore. */
  209. gs_state *
  210. gs_state_swap_saved(gs_state *pgs, gs_state *new_saved)
  211. {    gs_state *saved = pgs->saved;
  212.     pgs->saved = new_saved;
  213.     return saved;
  214. }
  215.  
  216. /* Swap the contents of two graphics states, except for the saved pointer. */
  217. /* This is provided only for save/restore. */
  218. void
  219. gs_state_swap(gs_state *p1, gs_state *p2)
  220. {    gs_state temp;
  221.     temp = *p1, *p1 = *p2, *p2 = temp;
  222.     /* Restore the saved pointers. */
  223.     p2->saved = p1->saved;
  224.     p1->saved = temp.saved;
  225.     /* Restore the client data pointers. */
  226.     p2->client_data = p1->client_data;
  227.     p1->client_data = temp.client_data;
  228. }
  229.  
  230. /* ------ Operations on components ------ */
  231.  
  232. /* Reset most of the graphics state */
  233. int
  234. gs_initgraphics(register gs_state *pgs)
  235. {    int code;
  236.     gs_initmatrix(pgs);
  237.     if (    (code = gs_newpath(pgs)) < 0 ||
  238.         (code = gs_initclip(pgs)) < 0 ||
  239.         (code = gs_setlinewidth(pgs, 1.0)) < 0 ||
  240.         (code = gs_setlinecap(pgs, gs_cap_butt)) < 0 ||
  241.         (code = gs_setlinejoin(pgs, gs_join_miter)) < 0 ||
  242.         (code = gs_setdash(pgs, (float *)0, 0, 0.0)) < 0 ||
  243.         (code = gs_setgray(pgs, 0.0)) < 0 ||
  244.         (code = gs_setmiterlimit(pgs, 10.0)) < 0
  245.        ) return code;
  246.     return 0;
  247. }
  248.  
  249. /* setflat */
  250. int
  251. gs_setflat(gs_state *pgs, floatp flat)
  252. {    if ( flat <= 0.2 ) flat = 0.2;
  253.     else if ( flat > 100 ) flat = 100;
  254.     pgs->flatness = flat;
  255.     return 0;
  256. }
  257.  
  258. /* currentflat */
  259. float
  260. gs_currentflat(const gs_state *pgs)
  261. {    return pgs->flatness;
  262. }
  263.  
  264. /* setstrokeadjust */
  265. int
  266. gs_setstrokeadjust(gs_state *pgs, int stroke_adjust)
  267. {    pgs->stroke_adjust = stroke_adjust;
  268.     return 0;
  269. }
  270.  
  271. /* currentstrokeadjust */
  272. int
  273. gs_currentstrokeadjust(const gs_state *pgs)
  274. {    return pgs->stroke_adjust;
  275. }
  276.  
  277. /* ------ Internal routines ------ */
  278.  
  279. /* Allocate a graphics state object and its contents, */
  280. /* optionally initializing it from an existing object. */
  281. /* Return 0 if the allocation fails. */
  282. private gs_state *
  283. alloc_gstate(proc_alloc_t palloc, proc_free_t pfree, const gs_state *pold, const char *cname)
  284. {    gs_state *pgs = (gs_state *)(*palloc)(1, sizeof(gs_state), cname);
  285.     if ( pgs == 0 ) return 0;
  286.     if ( pold != 0 )
  287.         *pgs = *pold;
  288.     else
  289.         pgs->transfer = 0,
  290.         pgs->client_data = 0;
  291.     pgs->memory_procs.alloc = palloc;
  292.     pgs->memory_procs.free = pfree;
  293.     if ( alloc_gstate_contents(pgs) < 0 )
  294.        {    free_struct(pgs, pgs, cname);
  295.         return 0;
  296.        }
  297.     return pgs;
  298. }
  299.  
  300. /* Allocate the contents of a graphics state object. */
  301. /* Return -1 if the allocation fails. */
  302. /* Note that the contents have been smashed in this case. */
  303. private int
  304. alloc_gstate_contents(register gs_state *pgs)
  305. {    proc_alloc_t palloc = pgs->memory_procs.alloc;
  306.     static const char cname[] = "alloc_gstate_contents";
  307. #define galloc(element,type,fail)\
  308.     if ( (pgs->element = (type *)(*palloc)(1, sizeof(type), cname)) == 0 ) goto fail
  309.     galloc(path, gx_path, up);
  310.     galloc(clip_path, gx_clip_path, ucp);
  311.     galloc(line_params, line_params, ulp);
  312.     galloc(halftone, halftone_params, uht);
  313.     galloc(color, gs_color, uc);
  314.     galloc(dev_color, gx_device_color, udc);
  315.     if ( pgs->transfer != 0 )
  316.         pgs->transfer->ref_count++;
  317.     else
  318.        {    galloc(transfer, gx_transfer, ut);
  319.         pgs->transfer->ref_count = 1;
  320.        }
  321.     galloc(device, device, ud);
  322. #undef galloc
  323.     if ( pgs->client_data != 0 )
  324.     {    if ( (pgs->client_data = (*pgs->client_procs.alloc)(&pgs->memory_procs)) == 0 )
  325.             goto ucd;
  326.     }
  327.     pgs->device_is_shared = 0;
  328.     return 0;
  329.     /* Undo partial allocations if an allocation failed. */
  330. #define gunalloc(element) free_struct(pgs, pgs->element, cname)
  331. ucd:    gunalloc(device);
  332. ud:    gunalloc(transfer);
  333. ut:    gunalloc(dev_color);
  334. udc:    gunalloc(color);
  335. uc:    gunalloc(halftone);
  336. uht:    gunalloc(line_params);
  337. ulp:    gunalloc(clip_path);
  338. ucp:    gunalloc(path);
  339. up:    return -1;
  340. #undef gunalloc
  341. }
  342.  
  343. /* Free the contents of a graphics state, but not the state itself. */
  344. private void
  345. free_gstate_contents(gs_state *pgs)
  346. {    proc_free_t pfree = pgs->memory_procs.free;
  347.     static const char cname[] = "free_gstate_contents";
  348.     gx_cpath_release(pgs->clip_path);
  349.     gx_path_release(pgs->path);
  350.     if ( pgs->client_data != 0 )
  351.         (*pgs->client_procs.free)(pgs->client_data, &pgs->memory_procs);
  352. #define gfree(element)\
  353.     (*pfree)((char *)pgs->element, 1, sizeof(*pgs->element), cname)
  354.     if ( !pgs->device_is_shared )
  355.         gfree(device);
  356.     if ( !--(pgs->transfer->ref_count) )
  357.         gfree(transfer);
  358.     gfree(dev_color);
  359.     gfree(color);
  360.     gfree(halftone);
  361.     gfree(line_params);
  362.     gfree(clip_path);
  363.     gfree(path);
  364. #undef gfree
  365. }
  366.  
  367. /* Copy the composite parts of a graphics state. */
  368. private void
  369. copy_gstate_contents(gs_state *pto, const gs_state *pfrom)
  370. {
  371. #define gcopy(element)\
  372.     *pto->element = *pfrom->element
  373.     gcopy(path);
  374.     gcopy(clip_path);
  375.     gcopy(line_params);
  376.     gcopy(halftone);
  377.     gcopy(color);
  378.     gcopy(dev_color);
  379.     if ( pto->transfer != pfrom->transfer )
  380.        {    if ( !--(pto->transfer->ref_count) )
  381.            {    /* We could just copy the contents, but */
  382.             /* we'd rather free the storage and hope that */
  383.             /* we won't have to reallocate later. */
  384.             free_struct(pto, pto->transfer, "copy gstate");
  385.            }
  386.         (pto->transfer = pfrom->transfer)->ref_count++;
  387.        }
  388.     gcopy(device);
  389. #undef gcopy
  390.     if ( pfrom->client_data != 0 )
  391.         (*pfrom->client_procs.copy)(pto->client_data, pfrom->client_data);
  392.     gx_path_share(pto->path);
  393.     gx_cpath_share(pto->clip_path);
  394. }
  395.