gsclipsr.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 1998 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gsclipsr.c,v 1.2 2000/09/19 19:00:26 lpd Exp $ */
  16. /* clipsave/cliprestore */
  17. #include "gx.h"
  18. #include "gserrors.h"
  19. #include "gsclipsr.h"
  20. #include "gsstruct.h"
  21. #include "gxclipsr.h"
  22. #include "gxfixed.h" /* for gxpath.h */
  23. #include "gxpath.h"
  24. #include "gzstate.h"
  25. /* Structure descriptors */
  26. private_st_clip_stack();
  27. /*
  28. * When we free a clip stack entry, free the associated clip path,
  29. * and iterate down the list. We do this iteratively so that we don't
  30. * take a level of recursion for each node on the list.
  31. */
  32. private void
  33. rc_free_clip_stack(gs_memory_t * mem, void *vstack, client_name_t cname)
  34. {
  35. gx_clip_stack_t *stack = (gx_clip_stack_t *)vstack;
  36. gx_clip_stack_t *next;
  37. do {
  38. gx_clip_path *pcpath = stack->clip_path;
  39. next = stack->next;
  40. gs_free_object(stack->rc.memory, stack, cname);
  41. gx_cpath_free(pcpath, "rc_free_clip_stack");
  42. } while ((stack = next) != 0 && !--(stack->rc.ref_count));
  43. }
  44. /* clipsave */
  45. int
  46. gs_clipsave(gs_state *pgs)
  47. {
  48. gs_memory_t *mem = pgs->memory;
  49. gx_clip_path *copy =
  50. gx_cpath_alloc_shared(pgs->clip_path, mem, "gs_clipsave(clip_path)");
  51. gx_clip_stack_t *stack =
  52. gs_alloc_struct(mem, gx_clip_stack_t, &st_clip_stack,
  53. "gs_clipsave(stack)");
  54. if (copy == 0 || stack == 0) {
  55. gs_free_object(mem, stack, "gs_clipsave(stack)");
  56. gs_free_object(mem, copy, "gs_clipsave(clip_path)");
  57. return_error(gs_error_VMerror);
  58. }
  59. rc_init_free(stack, mem, 1, rc_free_clip_stack);
  60. stack->clip_path = copy;
  61. stack->next = pgs->clip_stack;
  62. pgs->clip_stack = stack;
  63. return 0;
  64. }
  65. /* cliprestore */
  66. int
  67. gs_cliprestore(gs_state *pgs)
  68. {
  69. gx_clip_stack_t *stack = pgs->clip_stack;
  70. if (stack) {
  71. gx_clip_stack_t *next = stack->next;
  72. gx_clip_path *pcpath = stack->clip_path;
  73. int code;
  74. if (stack->rc.ref_count == 1) {
  75. /* Use assign_free rather than assign_preserve. */
  76. gs_free_object(stack->rc.memory, stack, "cliprestore");
  77. code = gx_cpath_assign_free(pgs->clip_path, pcpath);
  78. } else {
  79. code = gx_cpath_assign_preserve(pgs->clip_path, pcpath);
  80. if (code < 0)
  81. return code;
  82. --(stack->rc.ref_count);
  83. }
  84. pgs->clip_stack = next;
  85. return code;
  86. } else {
  87. return gx_cpath_assign_preserve(pgs->clip_path, pgs->saved->clip_path);
  88. }
  89. }