source: trunk/third/librsvg/rsvg-bpath-util.c @ 18275

Revision 18275, 4.5 KB checked in by ghudson, 21 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18274, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2   rsvg-bpath-util.c: Data structure and convenience functions for creating bezier paths.
3 
4   Copyright (C) 2000 Eazel, Inc.
5 
6   This program is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Library General Public License as
8   published by the Free Software Foundation; either version 2 of the
9   License, or (at your option) any later version.
10 
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Library General Public License for more details.
15 
16   You should have received a copy of the GNU Library General Public
17   License along with this program; if not, write to the
18   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   Boston, MA 02111-1307, USA.
20 
21   Author: Raph Levien <raph@artofcode.com>
22*/
23
24#include "config.h"
25#include "rsvg-bpath-util.h"
26
27#include <glib/gmem.h>
28#include <glib/gmessages.h>
29#include <math.h>
30#include <string.h>
31
32/* This is adapted from gnome-canvas-bpath-util in libgnomeprint
33   (originally developed as part of Gill). */
34
35RsvgBpathDef *
36rsvg_bpath_def_new (void)
37{
38  RsvgBpathDef *bpd;
39
40  bpd = g_new (RsvgBpathDef, 1);
41  bpd->n_bpath = 0;
42  bpd->n_bpath_max = 16;
43  bpd->moveto_idx = -1;
44  bpd->bpath = g_new (ArtBpath, bpd->n_bpath_max);
45  bpd->ref_count = 1;
46
47  return bpd;
48}
49
50RsvgBpathDef *
51rsvg_bpath_def_new_from (ArtBpath *path)
52{
53  RsvgBpathDef *bpd;
54  int i;
55
56  g_return_val_if_fail (path != NULL, NULL);
57
58  for (i = 0; path[i].code != ART_END; i++)
59    ;
60  if (i <= 0)
61    return rsvg_bpath_def_new ();
62
63  bpd = g_new (RsvgBpathDef, 1);
64
65  bpd->n_bpath = i;
66  bpd->n_bpath_max = i;
67  bpd->moveto_idx = -1;
68  bpd->ref_count = 1;
69  bpd->bpath = g_new (ArtBpath, i);
70
71  memcpy (bpd->bpath, path, i * sizeof (ArtBpath));
72  return bpd;
73}
74
75RsvgBpathDef *
76rsvg_bpath_def_ref (RsvgBpathDef *bpd)
77{
78  g_return_val_if_fail (bpd != NULL, NULL);
79
80  bpd->ref_count += 1;
81  return bpd;
82}
83
84void
85rsvg_bpath_def_free (RsvgBpathDef *bpd)
86{
87  g_return_if_fail (bpd != NULL);
88
89  bpd->ref_count -= 1;
90  if (bpd->ref_count == 0)
91    {
92      g_free (bpd->bpath);
93      g_free (bpd);
94    }
95}
96
97void
98rsvg_bpath_def_moveto (RsvgBpathDef *bpd, double x, double y)
99{
100  ArtBpath *bpath;
101  int n_bpath;
102
103  g_return_if_fail (bpd != NULL);
104
105  n_bpath = bpd->n_bpath++;
106
107  if (n_bpath == bpd->n_bpath_max)
108    bpd->bpath = g_realloc (bpd->bpath,
109                            (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath));
110  bpath = bpd->bpath;
111  bpath[n_bpath].code = ART_MOVETO_OPEN;
112  bpath[n_bpath].x3 = x;
113  bpath[n_bpath].y3 = y;
114  bpd->moveto_idx = n_bpath;
115}
116
117void
118rsvg_bpath_def_lineto (RsvgBpathDef *bpd, double x, double y)
119{
120  ArtBpath *bpath;
121  int n_bpath;
122
123  g_return_if_fail (bpd != NULL);
124  g_return_if_fail (bpd->moveto_idx >= 0);
125
126  n_bpath = bpd->n_bpath++;
127
128  if (n_bpath == bpd->n_bpath_max)
129    bpd->bpath = g_realloc (bpd->bpath,
130                            (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath));
131  bpath = bpd->bpath;
132  bpath[n_bpath].code = ART_LINETO;
133  bpath[n_bpath].x3 = x;
134  bpath[n_bpath].y3 = y;
135}
136
137void
138rsvg_bpath_def_curveto (RsvgBpathDef *bpd, double x1, double y1, double x2, double y2, double x3, double y3)
139{
140  ArtBpath *bpath;
141  int n_bpath;
142
143  g_return_if_fail (bpd != NULL);
144  g_return_if_fail (bpd->moveto_idx >= 0);
145
146  n_bpath = bpd->n_bpath++;
147
148  if (n_bpath == bpd->n_bpath_max)
149    bpd->bpath = g_realloc (bpd->bpath,
150                            (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath));
151  bpath = bpd->bpath;
152  bpath[n_bpath].code = ART_CURVETO;
153  bpath[n_bpath].x1 = x1;
154  bpath[n_bpath].y1 = y1;
155  bpath[n_bpath].x2 = x2;
156  bpath[n_bpath].y2 = y2;
157  bpath[n_bpath].x3 = x3;
158  bpath[n_bpath].y3 = y3;
159}
160
161void
162rsvg_bpath_def_closepath (RsvgBpathDef *bpd)
163{
164  ArtBpath *bpath;
165  int n_bpath;
166
167  g_return_if_fail (bpd != NULL);
168  g_return_if_fail (bpd->moveto_idx >= 0);
169  g_return_if_fail (bpd->n_bpath > 0);
170
171  bpath = bpd->bpath;
172  n_bpath = bpd->n_bpath;
173
174  /* Add closing vector if we need it. */
175  if (bpath[n_bpath - 1].x3 != bpath[bpd->moveto_idx].x3 ||
176      bpath[n_bpath - 1].y3 != bpath[bpd->moveto_idx].y3)
177    {
178      rsvg_bpath_def_lineto (bpd, bpath[bpd->moveto_idx].x3,
179                             bpath[bpd->moveto_idx].y3);
180      bpath = bpd->bpath;
181    }
182  bpath[bpd->moveto_idx].code = ART_MOVETO;
183  bpd->moveto_idx = -1;
184}
185
186void
187rsvg_bpath_def_art_finish (RsvgBpathDef *bpd)
188{
189  int n_bpath;
190
191  g_return_if_fail (bpd != NULL);
192
193  n_bpath = bpd->n_bpath++;
194
195  if (n_bpath == bpd->n_bpath_max)
196    bpd->bpath = g_realloc (bpd->bpath,
197                            (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath));
198  bpd->bpath[n_bpath].code = ART_END;
199}
Note: See TracBrowser for help on using the repository browser.