~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to app/vectors/gimpvectors-compat.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * gimpvectors-compat.c
 
5
 * Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <glib-object.h>
 
25
 
 
26
#include "vectors-types.h"
 
27
 
 
28
#include "core/gimpimage.h"
 
29
#include "core/gimplist.h"
 
30
 
 
31
#include "gimpanchor.h"
 
32
#include "gimpbezierstroke.h"
 
33
#include "gimpvectors.h"
 
34
#include "gimpvectors-compat.h"
 
35
 
 
36
 
 
37
enum
 
38
{
 
39
  GIMP_VECTORS_COMPAT_ANCHOR     = 1,
 
40
  GIMP_VECTORS_COMPAT_CONTROL    = 2,
 
41
  GIMP_VECTORS_COMPAT_NEW_STROKE = 3
 
42
};
 
43
 
 
44
 
 
45
 
 
46
GimpVectors *
 
47
gimp_vectors_compat_new (GimpImage              *gimage,
 
48
                         const gchar            *name,
 
49
                         GimpVectorsCompatPoint *points,
 
50
                         gint                    n_points,
 
51
                         gboolean                closed)
 
52
{
 
53
  GimpVectors *vectors;
 
54
  GimpStroke  *stroke;
 
55
  GimpCoords  *coords;
 
56
  GimpCoords  *curr_stroke;
 
57
  GimpCoords  *curr_coord;
 
58
  gint         i;
 
59
 
 
60
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
61
  g_return_val_if_fail (name != NULL, NULL);
 
62
  g_return_val_if_fail (points != NULL || n_points == 0, NULL);
 
63
  g_return_val_if_fail (n_points >= 0, NULL);
 
64
 
 
65
  vectors = gimp_vectors_new (gimage, name);
 
66
 
 
67
  coords = g_new0 (GimpCoords, n_points + 1);
 
68
 
 
69
  curr_stroke = curr_coord = coords;
 
70
 
 
71
  /*  skip the first control point, will set it later  */
 
72
  curr_coord++;
 
73
 
 
74
  for (i = 0; i < n_points; i++)
 
75
    {
 
76
      curr_coord->x        = points[i].x;
 
77
      curr_coord->y        = points[i].y;
 
78
      curr_coord->pressure = GIMP_COORDS_DEFAULT_PRESSURE;
 
79
      curr_coord->xtilt    = GIMP_COORDS_DEFAULT_TILT;
 
80
      curr_coord->ytilt    = GIMP_COORDS_DEFAULT_TILT;
 
81
      curr_coord->wheel    = GIMP_COORDS_DEFAULT_WHEEL;
 
82
 
 
83
      /*  copy the first anchor to be the first control point  */
 
84
      if (curr_coord == curr_stroke + 1)
 
85
        *curr_stroke = *curr_coord;
 
86
 
 
87
      /*  found new stroke start  */
 
88
      if (points[i].type == GIMP_VECTORS_COMPAT_NEW_STROKE)
 
89
        {
 
90
          /*  copy the last control point to the beginning of the stroke  */
 
91
          *curr_stroke = *(curr_coord - 1);
 
92
 
 
93
          stroke =
 
94
            gimp_bezier_stroke_new_from_coords (curr_stroke,
 
95
                                                curr_coord - curr_stroke - 1,
 
96
                                                TRUE);
 
97
          gimp_vectors_stroke_add (vectors, stroke);
 
98
          g_object_unref (stroke);
 
99
 
 
100
          /*  start a new stroke  */
 
101
          curr_stroke = curr_coord - 1;
 
102
 
 
103
          /*  copy the first anchor to be the first control point  */
 
104
          *curr_stroke = *curr_coord;
 
105
        }
 
106
 
 
107
      curr_coord++;
 
108
    }
 
109
 
 
110
  if (closed)
 
111
    {
 
112
      /*  copy the last control point to the beginning of the stroke  */
 
113
      curr_coord--;
 
114
      *curr_stroke = *curr_coord;
 
115
    }
 
116
 
 
117
  stroke = gimp_bezier_stroke_new_from_coords (curr_stroke,
 
118
                                               curr_coord - curr_stroke,
 
119
                                               closed);
 
120
  gimp_vectors_stroke_add (vectors, stroke);
 
121
  g_object_unref (stroke);
 
122
 
 
123
  g_free (coords);
 
124
 
 
125
  return vectors;
 
126
}
 
127
 
 
128
gboolean
 
129
gimp_vectors_compat_is_compatible (GimpImage *gimage)
 
130
{
 
131
  GList *list, *strokes;
 
132
  GimpVectors *vectors;
 
133
  GimpStroke *stroke;
 
134
  gint open_count;
 
135
 
 
136
  for (list = GIMP_LIST (gimage->vectors)->list;
 
137
       list;
 
138
       list = g_list_next (list))
 
139
    {
 
140
      open_count = 0;
 
141
 
 
142
      vectors = GIMP_VECTORS (list->data);
 
143
 
 
144
      if (GIMP_ITEM (vectors)->visible == TRUE)
 
145
        return FALSE;
 
146
 
 
147
      for (strokes = vectors->strokes; strokes; strokes = g_list_next (strokes))
 
148
        {
 
149
          stroke = GIMP_STROKE (strokes->data);
 
150
          if (! GIMP_IS_BEZIER_STROKE (stroke))
 
151
            return FALSE;
 
152
 
 
153
          if (!stroke->closed)
 
154
            open_count++;
 
155
        }
 
156
 
 
157
      if (open_count >= 2)
 
158
        return FALSE;
 
159
    }
 
160
  return TRUE;
 
161
}
 
162
 
 
163
GimpVectorsCompatPoint *
 
164
gimp_vectors_compat_get_points (GimpVectors *vectors,
 
165
                                guint32     *n_points,
 
166
                                guint32     *closed)
 
167
{
 
168
  GimpVectorsCompatPoint *points;
 
169
  GList                  *strokes;
 
170
  gint                    i;
 
171
  GList                  *postponed = NULL;  /* for the one open stroke... */
 
172
  gint                    open_count;
 
173
  gboolean                first_stroke = TRUE;
 
174
 
 
175
  g_return_val_if_fail (GIMP_IS_VECTORS (vectors), NULL);
 
176
  g_return_val_if_fail (n_points != NULL, NULL);
 
177
  g_return_val_if_fail (closed != NULL, NULL);
 
178
 
 
179
  *n_points = 0;
 
180
  *closed   = TRUE;
 
181
 
 
182
  open_count = 0;
 
183
 
 
184
  for (strokes = vectors->strokes; strokes; strokes = g_list_next (strokes))
 
185
    {
 
186
      GimpStroke *stroke = strokes->data;
 
187
      gint        n_anchors;
 
188
 
 
189
      if (! stroke->closed)
 
190
        {
 
191
          open_count++;
 
192
          postponed = strokes;
 
193
          *closed = FALSE;
 
194
 
 
195
          if (open_count >= 2)
 
196
            {
 
197
              g_warning ("gimp_vectors_compat_get_points(): convert failed");
 
198
              *n_points = 0;
 
199
              return NULL;
 
200
            }
 
201
        }
 
202
 
 
203
      n_anchors = g_list_length (stroke->anchors);
 
204
 
 
205
      if (! stroke->closed)
 
206
        n_anchors--;
 
207
 
 
208
      *n_points += n_anchors;
 
209
    }
 
210
 
 
211
  points = g_new0 (GimpVectorsCompatPoint, *n_points);
 
212
 
 
213
  i = 0;
 
214
 
 
215
  for (strokes = vectors->strokes;
 
216
       strokes || postponed;
 
217
       strokes = g_list_next (strokes))
 
218
    {
 
219
      GimpStroke *stroke;
 
220
      GList      *anchors;
 
221
 
 
222
      if (strokes)
 
223
        {
 
224
          if (postponed && strokes == postponed)
 
225
            /* we need to visit the open stroke last... */
 
226
            continue;
 
227
          else
 
228
            stroke = GIMP_STROKE (strokes->data);
 
229
        }
 
230
      else
 
231
        {
 
232
          stroke = GIMP_STROKE (postponed->data);
 
233
          postponed = NULL;
 
234
        }
 
235
 
 
236
      for (anchors = stroke->anchors;
 
237
           anchors;
 
238
           anchors = g_list_next (anchors))
 
239
        {
 
240
          GimpAnchor *anchor = anchors->data;
 
241
 
 
242
          /*  skip the first anchor, will add it at the end if needed  */
 
243
          if (! anchors->prev)
 
244
            continue;
 
245
 
 
246
          switch (anchor->type)
 
247
            {
 
248
            case GIMP_ANCHOR_ANCHOR:
 
249
              if (anchors->prev == stroke->anchors && ! first_stroke)
 
250
                points[i].type = GIMP_VECTORS_COMPAT_NEW_STROKE;
 
251
              else
 
252
                points[i].type = GIMP_VECTORS_COMPAT_ANCHOR;
 
253
              break;
 
254
 
 
255
            case GIMP_ANCHOR_CONTROL:
 
256
              points[i].type = GIMP_VECTORS_COMPAT_CONTROL;
 
257
              break;
 
258
            }
 
259
 
 
260
          points[i].x = anchor->position.x;
 
261
          points[i].y = anchor->position.y;
 
262
 
 
263
          i++;
 
264
 
 
265
          /*  write the skipped control point  */
 
266
          if (! anchors->next && stroke->closed)
 
267
            {
 
268
              anchor = GIMP_ANCHOR (stroke->anchors->data);
 
269
 
 
270
              points[i].type = GIMP_VECTORS_COMPAT_CONTROL;
 
271
              points[i].x    = anchor->position.x;
 
272
              points[i].y    = anchor->position.y;
 
273
 
 
274
              i++;
 
275
            }
 
276
        }
 
277
      first_stroke = FALSE;
 
278
    }
 
279
 
 
280
  return points;
 
281
}