1/***********************************************************************
2 *                                                                     *
3 * $Id: hpgsgstate.c 270 2006-01-29 21:12:23Z softadm $
4 *                                                                     *
5 * hpgs - HPGl Script, a hpgl/2 interpreter, which uses a Postscript   *
6 *        API for rendering a scene and thus renders to a variety of   *
7 *        devices and fileformats.                                     *
8 *                                                                     *
9 * (C) 2004-2006 ev-i Informationstechnologie GmbH  http://www.ev-i.at *
10 *                                                                     *
11 * Author: Wolfgang Glas                                               *
12 *                                                                     *
13 *  hpgs is free software; you can redistribute it and/or              *
14 * modify it under the terms of the GNU Lesser General Public          *
15 * License as published by the Free Software Foundation; either        *
16 * version 2.1 of the License, or (at your option) any later version.  *
17 *                                                                     *
18 * hpgs is distributed in the hope that it will be useful,             *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of      *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   *
21 * Lesser General Public License for more details.                     *
22 *                                                                     *
23 * You should have received a copy of the GNU Lesser General Public    *
24 * License along with this library; if not, write to the               *
25 * Free Software  Foundation, Inc., 59 Temple Place, Suite 330,        *
26 * Boston, MA  02111-1307  USA                                         *
27 *                                                                     *
28 ***********************************************************************
29 *                                                                     *
30 * The implementation of the public API for the gstate.                *
31 *                                                                     *
32 ***********************************************************************/
33
34#include<hpgspaint.h>
35#include<assert.h>
36#include<string.h>
37
38/*! Creates a new gstate on the heap.
39    Use \c hpgs_gstate_destroy in order to destroy the returned
40    gstate pointer.
41
42    A null pointer is returned, if the system is out of memory.
43*/
44hpgs_gstate *hpgs_new_gstate()
45{
46  hpgs_gstate *ret = (hpgs_gstate *)malloc(sizeof(hpgs_gstate));
47
48  if (ret)
49    {
50      ret->line_cap  = hpgs_cap_butt;
51      ret->line_join = hpgs_join_miter;
52      ret->color.r = 0.0;
53      ret->color.g = 0.0;
54      ret->color.b = 0.0;
55      ret->miterlimit = 5.0;
56      ret->linewidth   = 1.0;
57      ret->n_dashes    = 0;
58      ret->dash_lengths = 0;
59      ret->dash_offset  = 0.0;
60      // ROP3 function: destination = source or pattern.
61      ret->rop3 = 252;
62      ret->src_transparency = HPGS_TRUE;
63      ret->pattern_transparency = HPGS_TRUE;
64
65      ret->pattern_color.r = 0.0;
66      ret->pattern_color.g = 0.0;
67      ret->pattern_color.b = 0.0;
68    }
69  return ret;
70}
71
72/*! Destroys a gstate created using \c hpgs_new_gstate.
73*/
74void hpgs_gstate_destroy(hpgs_gstate *gstate)
75{
76  if (!gstate) return;
77  if (gstate->dash_lengths) free(gstate->dash_lengths);
78  free (gstate);
79}
80
81/*! Sets the dashes of \c gstate. The passed array and offset
82    folow th esemantics of PostScipt's \c setdash command.
83
84    Return value:
85    \li 0 Success.
86    \li -1 The system is out of memory.
87*/
88int hpgs_gstate_setdash(hpgs_gstate *gstate,
89			const float *dash_lengths,
90			unsigned n_dashes, double offset)
91{
92  if (gstate->dash_lengths) free(gstate->dash_lengths);
93
94  gstate->n_dashes = n_dashes;
95  gstate->dash_offset = offset;
96
97  if (gstate->n_dashes)
98    {
99      gstate->dash_lengths = (float *)malloc(sizeof(float)*gstate->n_dashes);
100      if (!gstate->dash_lengths)
101	{
102	  gstate->n_dashes = 0;
103	  return -1;
104	}
105
106      memmove(gstate->dash_lengths,dash_lengths,sizeof(float)*gstate->n_dashes);
107    }
108  else
109    gstate->dash_lengths = 0;
110
111  return 0;
112}
113