~kkubasik/mono/libgdiplus_1.2.5_gutsy_pkg

« back to all changes in this revision

Viewing changes to cairo/src/cairo-path-fixed.c

  • Committer: Kevin Kubasik
  • Date: 2007-09-06 07:35:29 UTC
  • Revision ID: kevin@kubasik.net-20070906073529-exfb0b1v3ripmhyq
inital import, Debian package

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* cairo - a vector graphics library with display and print output
 
2
 *
 
3
 * Copyright © 2002 University of Southern California
 
4
 * Copyright © 2005 Red Hat, Inc.
 
5
  *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it either under the terms of the GNU Lesser General Public
 
8
 * License version 2.1 as published by the Free Software Foundation
 
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 
11
 * notice, a recipient may use your version of this file under either
 
12
 * the MPL or the LGPL.
 
13
 *
 
14
 * You should have received a copy of the LGPL along with this library
 
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
17
 * You should have received a copy of the MPL along with this library
 
18
 * in the file COPYING-MPL-1.1
 
19
 *
 
20
 * The contents of this file are subject to the Mozilla Public License
 
21
 * Version 1.1 (the "License"); you may not use this file except in
 
22
 * compliance with the License. You may obtain a copy of the License at
 
23
 * http://www.mozilla.org/MPL/
 
24
 *
 
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 
27
 * the specific language governing rights and limitations.
 
28
 *
 
29
 * The Original Code is the cairo graphics library.
 
30
 *
 
31
 * The Initial Developer of the Original Code is University of Southern
 
32
 * California.
 
33
 *
 
34
 * Contributor(s):
 
35
 *      Carl D. Worth <cworth@cworth.org>
 
36
 */
 
37
 
 
38
#include "cairoint.h"
 
39
 
 
40
#include "cairo-path-fixed-private.h"
 
41
 
 
42
/* private functions */
 
43
static cairo_status_t
 
44
_cairo_path_fixed_add (cairo_path_fixed_t *path,
 
45
                       cairo_path_op_t     op,
 
46
                       cairo_point_t      *points,
 
47
                       int                 num_points);
 
48
 
 
49
static void
 
50
_cairo_path_fixed_add_buf (cairo_path_fixed_t *path,
 
51
                           cairo_path_buf_t   *buf);
 
52
 
 
53
static cairo_path_buf_t *
 
54
_cairo_path_buf_create (void);
 
55
 
 
56
static void
 
57
_cairo_path_buf_destroy (cairo_path_buf_t *buf);
 
58
 
 
59
static void
 
60
_cairo_path_buf_add_op (cairo_path_buf_t *buf,
 
61
                        cairo_path_op_t   op);
 
62
 
 
63
static void
 
64
_cairo_path_buf_add_points (cairo_path_buf_t *buf,
 
65
                            cairo_point_t    *points,
 
66
                            int               num_points);
 
67
 
 
68
void
 
69
_cairo_path_fixed_init (cairo_path_fixed_t *path)
 
70
{
 
71
    path->buf_head->next = NULL;
 
72
    path->buf_head->prev = NULL;
 
73
    path->buf_tail = path->buf_head;
 
74
 
 
75
    path->buf_head->num_ops = 0;
 
76
    path->buf_head->num_points = 0;
 
77
 
 
78
    path->current_point.x = 0;
 
79
    path->current_point.y = 0;
 
80
    path->has_current_point = FALSE;
 
81
    path->has_curve_to = FALSE;
 
82
    path->last_move_point = path->current_point;
 
83
}
 
84
 
 
85
cairo_status_t
 
86
_cairo_path_fixed_init_copy (cairo_path_fixed_t *path,
 
87
                             cairo_path_fixed_t *other)
 
88
{
 
89
    cairo_path_buf_t *buf, *other_buf;
 
90
 
 
91
    _cairo_path_fixed_init (path);
 
92
    path->current_point = other->current_point;
 
93
    path->has_current_point = other->has_current_point;
 
94
    path->has_curve_to = other->has_curve_to;
 
95
    path->last_move_point = other->last_move_point;
 
96
 
 
97
    path->buf_head->num_ops = other->buf_head->num_ops;
 
98
    path->buf_head->num_points = other->buf_head->num_points;
 
99
    memcpy (path->buf_head->op, other->buf_head->op,
 
100
            other->buf_head->num_ops * sizeof (other->buf_head->op[0]));
 
101
    memcpy (path->buf_head->points, other->buf_head->points,
 
102
            other->buf_head->num_points * sizeof (other->buf_head->points[0]));
 
103
    for (other_buf = other->buf_head->next;
 
104
         other_buf;
 
105
         other_buf = other_buf->next)
 
106
    {
 
107
        buf = _cairo_path_buf_create ();
 
108
        if (buf == NULL) {
 
109
            _cairo_path_fixed_fini (path);
 
110
            return CAIRO_STATUS_NO_MEMORY;
 
111
        }
 
112
        memcpy (buf, other_buf, sizeof (cairo_path_buf_t));
 
113
        _cairo_path_fixed_add_buf (path, buf);
 
114
    }
 
115
 
 
116
    return CAIRO_STATUS_SUCCESS;
 
117
}
 
118
 
 
119
cairo_path_fixed_t *
 
120
_cairo_path_fixed_create (void)
 
121
{
 
122
    cairo_path_fixed_t  *path = malloc (sizeof (cairo_path_fixed_t));
 
123
 
 
124
    if (!path)
 
125
        return NULL;
 
126
    _cairo_path_fixed_init (path);
 
127
    return path;
 
128
}
 
129
 
 
130
void
 
131
_cairo_path_fixed_fini (cairo_path_fixed_t *path)
 
132
{
 
133
    cairo_path_buf_t *buf;
 
134
 
 
135
    buf = path->buf_head->next;
 
136
    while (buf) {
 
137
        cairo_path_buf_t *this = buf;
 
138
        buf = buf->next;
 
139
        _cairo_path_buf_destroy (this);
 
140
    }
 
141
    path->buf_head->next = NULL;
 
142
    path->buf_head->prev = NULL;
 
143
    path->buf_tail = path->buf_head;
 
144
    path->buf_head->num_ops = 0;
 
145
    path->buf_head->num_points = 0;
 
146
 
 
147
    path->has_current_point = FALSE;
 
148
    path->has_curve_to = FALSE;
 
149
}
 
150
 
 
151
void
 
152
_cairo_path_fixed_destroy (cairo_path_fixed_t *path)
 
153
{
 
154
    _cairo_path_fixed_fini (path);
 
155
    free (path);
 
156
}
 
157
 
 
158
cairo_status_t
 
159
_cairo_path_fixed_move_to (cairo_path_fixed_t  *path,
 
160
                           cairo_fixed_t        x,
 
161
                           cairo_fixed_t        y)
 
162
{
 
163
    cairo_status_t status;
 
164
    cairo_point_t point;
 
165
 
 
166
    point.x = x;
 
167
    point.y = y;
 
168
 
 
169
    /* If the previous op was also a MOVE_TO, then just change its
 
170
     * point rather than adding a new op. */
 
171
    if (path->buf_tail && path->buf_tail->num_ops &&
 
172
        path->buf_tail->op[path->buf_tail->num_ops - 1] == CAIRO_PATH_OP_MOVE_TO)
 
173
    {
 
174
        cairo_point_t *last_move_to_point;
 
175
        last_move_to_point = &path->buf_tail->points[path->buf_tail->num_points - 1];
 
176
        *last_move_to_point = point;
 
177
    } else {
 
178
        status = _cairo_path_fixed_add (path, CAIRO_PATH_OP_MOVE_TO, &point, 1);
 
179
        if (status)
 
180
            return status;
 
181
    }
 
182
 
 
183
    path->current_point = point;
 
184
    path->has_current_point = TRUE;
 
185
    path->last_move_point = path->current_point;
 
186
 
 
187
    return CAIRO_STATUS_SUCCESS;
 
188
}
 
189
 
 
190
void
 
191
_cairo_path_fixed_new_sub_path (cairo_path_fixed_t *path)
 
192
{
 
193
    path->has_current_point = FALSE;
 
194
}
 
195
 
 
196
cairo_status_t
 
197
_cairo_path_fixed_rel_move_to (cairo_path_fixed_t *path,
 
198
                               cairo_fixed_t       dx,
 
199
                               cairo_fixed_t       dy)
 
200
{
 
201
    cairo_fixed_t x, y;
 
202
 
 
203
    if (! path->has_current_point)
 
204
        return CAIRO_STATUS_NO_CURRENT_POINT;
 
205
 
 
206
    x = path->current_point.x + dx;
 
207
    y = path->current_point.y + dy;
 
208
 
 
209
    return _cairo_path_fixed_move_to (path, x, y);
 
210
}
 
211
 
 
212
cairo_status_t
 
213
_cairo_path_fixed_line_to (cairo_path_fixed_t *path,
 
214
                           cairo_fixed_t        x,
 
215
                           cairo_fixed_t        y)
 
216
{
 
217
    cairo_status_t status;
 
218
    cairo_point_t point;
 
219
 
 
220
    point.x = x;
 
221
    point.y = y;
 
222
 
 
223
    /* When there is not yet a current point, the line_to operation
 
224
     * becomes a move_to instead. Note: We have to do this by
 
225
     * explicitly calling into _cairo_path_fixed_line_to to ensure
 
226
     * that the last_move_point state is updated properly.
 
227
     */
 
228
    if (! path->has_current_point)
 
229
        status = _cairo_path_fixed_move_to (path, point.x, point.y);
 
230
    else
 
231
        status = _cairo_path_fixed_add (path, CAIRO_PATH_OP_LINE_TO, &point, 1);
 
232
 
 
233
    if (status)
 
234
        return status;
 
235
 
 
236
    path->current_point = point;
 
237
    path->has_current_point = TRUE;
 
238
 
 
239
    return CAIRO_STATUS_SUCCESS;
 
240
}
 
241
 
 
242
cairo_status_t
 
243
_cairo_path_fixed_rel_line_to (cairo_path_fixed_t *path,
 
244
                               cairo_fixed_t       dx,
 
245
                               cairo_fixed_t       dy)
 
246
{
 
247
    cairo_fixed_t x, y;
 
248
 
 
249
    if (! path->has_current_point)
 
250
        return CAIRO_STATUS_NO_CURRENT_POINT;
 
251
 
 
252
    x = path->current_point.x + dx;
 
253
    y = path->current_point.y + dy;
 
254
 
 
255
    return _cairo_path_fixed_line_to (path, x, y);
 
256
}
 
257
 
 
258
cairo_status_t
 
259
_cairo_path_fixed_curve_to (cairo_path_fixed_t  *path,
 
260
                            cairo_fixed_t x0, cairo_fixed_t y0,
 
261
                            cairo_fixed_t x1, cairo_fixed_t y1,
 
262
                            cairo_fixed_t x2, cairo_fixed_t y2)
 
263
{
 
264
    cairo_status_t status;
 
265
    cairo_point_t point[3];
 
266
 
 
267
    point[0].x = x0; point[0].y = y0;
 
268
    point[1].x = x1; point[1].y = y1;
 
269
    point[2].x = x2; point[2].y = y2;
 
270
 
 
271
    if (! path->has_current_point) {
 
272
        status = _cairo_path_fixed_add (path, CAIRO_PATH_OP_MOVE_TO,
 
273
                                        &point[0], 1);
 
274
        if (status)
 
275
            return status;
 
276
    }
 
277
 
 
278
    status = _cairo_path_fixed_add (path, CAIRO_PATH_OP_CURVE_TO, point, 3);
 
279
    if (status)
 
280
        return status;
 
281
 
 
282
    path->current_point = point[2];
 
283
    path->has_current_point = TRUE;
 
284
    path->has_curve_to = TRUE;
 
285
 
 
286
    return CAIRO_STATUS_SUCCESS;
 
287
}
 
288
 
 
289
cairo_status_t
 
290
_cairo_path_fixed_rel_curve_to (cairo_path_fixed_t *path,
 
291
                                cairo_fixed_t dx0, cairo_fixed_t dy0,
 
292
                                cairo_fixed_t dx1, cairo_fixed_t dy1,
 
293
                                cairo_fixed_t dx2, cairo_fixed_t dy2)
 
294
{
 
295
    cairo_fixed_t x0, y0;
 
296
    cairo_fixed_t x1, y1;
 
297
    cairo_fixed_t x2, y2;
 
298
 
 
299
    if (! path->has_current_point)
 
300
        return CAIRO_STATUS_NO_CURRENT_POINT;
 
301
 
 
302
    x0 = path->current_point.x + dx0;
 
303
    y0 = path->current_point.y + dy0;
 
304
 
 
305
    x1 = path->current_point.x + dx1;
 
306
    y1 = path->current_point.y + dy1;
 
307
 
 
308
    x2 = path->current_point.x + dx2;
 
309
    y2 = path->current_point.y + dy2;
 
310
 
 
311
    return _cairo_path_fixed_curve_to (path,
 
312
                                       x0, y0,
 
313
                                       x1, y1,
 
314
                                       x2, y2);
 
315
}
 
316
 
 
317
cairo_status_t
 
318
_cairo_path_fixed_close_path (cairo_path_fixed_t *path)
 
319
{
 
320
    cairo_status_t status;
 
321
 
 
322
    if (! path->has_current_point)
 
323
        return CAIRO_STATUS_SUCCESS;
 
324
 
 
325
    status = _cairo_path_fixed_add (path, CAIRO_PATH_OP_CLOSE_PATH, NULL, 0);
 
326
    if (status)
 
327
        return status;
 
328
 
 
329
    status = _cairo_path_fixed_move_to (path,
 
330
                                        path->last_move_point.x,
 
331
                                        path->last_move_point.y);
 
332
    if (status)
 
333
        return status;
 
334
 
 
335
    return CAIRO_STATUS_SUCCESS;
 
336
}
 
337
 
 
338
cairo_status_t
 
339
_cairo_path_fixed_get_current_point (cairo_path_fixed_t *path,
 
340
                                     cairo_fixed_t      *x,
 
341
                                     cairo_fixed_t      *y)
 
342
{
 
343
    if (! path->has_current_point)
 
344
        return CAIRO_STATUS_NO_CURRENT_POINT;
 
345
 
 
346
    *x = path->current_point.x;
 
347
    *y = path->current_point.y;
 
348
 
 
349
    return CAIRO_STATUS_SUCCESS;
 
350
}
 
351
 
 
352
static cairo_status_t
 
353
_cairo_path_fixed_add (cairo_path_fixed_t *path,
 
354
                       cairo_path_op_t     op,
 
355
                       cairo_point_t      *points,
 
356
                       int                 num_points)
 
357
{
 
358
    if ((unsigned int) path->buf_tail->num_ops + 1 > CAIRO_PATH_BUF_SIZE ||
 
359
        (unsigned int) path->buf_tail->num_points + num_points > CAIRO_PATH_BUF_SIZE)
 
360
    {
 
361
        cairo_path_buf_t *buf;
 
362
 
 
363
        buf = _cairo_path_buf_create ();
 
364
        if (buf == NULL)
 
365
            return CAIRO_STATUS_NO_MEMORY;
 
366
 
 
367
        _cairo_path_fixed_add_buf (path, buf);
 
368
    }
 
369
 
 
370
    _cairo_path_buf_add_op (path->buf_tail, op);
 
371
    _cairo_path_buf_add_points (path->buf_tail, points, num_points);
 
372
 
 
373
    return CAIRO_STATUS_SUCCESS;
 
374
}
 
375
 
 
376
static void
 
377
_cairo_path_fixed_add_buf (cairo_path_fixed_t *path,
 
378
                           cairo_path_buf_t   *buf)
 
379
{
 
380
    buf->next = NULL;
 
381
    buf->prev = path->buf_tail;
 
382
 
 
383
    path->buf_tail->next = buf;
 
384
    path->buf_tail = buf;
 
385
}
 
386
 
 
387
static cairo_path_buf_t *
 
388
_cairo_path_buf_create (void)
 
389
{
 
390
    cairo_path_buf_t *buf;
 
391
 
 
392
    buf = malloc (sizeof (cairo_path_buf_t));
 
393
 
 
394
    if (buf) {
 
395
        buf->next = NULL;
 
396
        buf->prev = NULL;
 
397
        buf->num_ops = 0;
 
398
        buf->num_points = 0;
 
399
    }
 
400
 
 
401
    return buf;
 
402
}
 
403
 
 
404
static void
 
405
_cairo_path_buf_destroy (cairo_path_buf_t *buf)
 
406
{
 
407
    free (buf);
 
408
}
 
409
 
 
410
static void
 
411
_cairo_path_buf_add_op (cairo_path_buf_t *buf,
 
412
                        cairo_path_op_t   op)
 
413
{
 
414
    buf->op[buf->num_ops++] = op;
 
415
}
 
416
 
 
417
static void
 
418
_cairo_path_buf_add_points (cairo_path_buf_t *buf,
 
419
                            cairo_point_t    *points,
 
420
                            int               num_points)
 
421
{
 
422
    int i;
 
423
 
 
424
    for (i=0; i < num_points; i++) {
 
425
        buf->points[buf->num_points++] = points[i];
 
426
    }
 
427
}
 
428
 
 
429
static int const num_args[] =
 
430
{
 
431
    1, /* cairo_path_move_to */
 
432
    1, /* cairo_path_op_line_to */
 
433
    3, /* cairo_path_op_curve_to */
 
434
    0, /* cairo_path_op_close_path */
 
435
};
 
436
 
 
437
cairo_status_t
 
438
_cairo_path_fixed_interpret (cairo_path_fixed_t                 *path,
 
439
                             cairo_direction_t                   dir,
 
440
                             cairo_path_fixed_move_to_func_t    *move_to,
 
441
                             cairo_path_fixed_line_to_func_t    *line_to,
 
442
                             cairo_path_fixed_curve_to_func_t   *curve_to,
 
443
                             cairo_path_fixed_close_path_func_t *close_path,
 
444
                             void                               *closure)
 
445
{
 
446
    cairo_status_t status;
 
447
    cairo_path_buf_t *buf;
 
448
    cairo_path_op_t op;
 
449
    cairo_bool_t forward = (dir == CAIRO_DIRECTION_FORWARD);
 
450
    int step = forward ? 1 : -1;
 
451
 
 
452
    for (buf = forward ? path->buf_head : path->buf_tail;
 
453
         buf;
 
454
         buf = forward ? buf->next : buf->prev)
 
455
    {
 
456
        cairo_point_t *points;
 
457
        int start, stop, i;
 
458
        if (forward) {
 
459
            start = 0;
 
460
            stop = buf->num_ops;
 
461
            points = buf->points;
 
462
        } else {
 
463
            start = buf->num_ops - 1;
 
464
            stop = -1;
 
465
            points = buf->points + buf->num_points;
 
466
        }
 
467
 
 
468
        for (i=start; i != stop; i += step) {
 
469
            op = buf->op[i];
 
470
 
 
471
            if (! forward) {
 
472
                points -= num_args[op];
 
473
            }
 
474
 
 
475
            switch (op) {
 
476
            case CAIRO_PATH_OP_MOVE_TO:
 
477
                status = (*move_to) (closure, &points[0]);
 
478
                break;
 
479
            case CAIRO_PATH_OP_LINE_TO:
 
480
                status = (*line_to) (closure, &points[0]);
 
481
                break;
 
482
            case CAIRO_PATH_OP_CURVE_TO:
 
483
                status = (*curve_to) (closure, &points[0], &points[1], &points[2]);
 
484
                break;
 
485
            case CAIRO_PATH_OP_CLOSE_PATH:
 
486
            default:
 
487
                status = (*close_path) (closure);
 
488
                break;
 
489
            }
 
490
            if (status)
 
491
                return status;
 
492
 
 
493
            if (forward) {
 
494
                points += num_args[op];
 
495
            }
 
496
 
 
497
        }
 
498
    }
 
499
 
 
500
    return CAIRO_STATUS_SUCCESS;
 
501
}
 
502
 
 
503
static void
 
504
_cairo_path_fixed_offset_and_scale (cairo_path_fixed_t *path,
 
505
                                    cairo_fixed_t offx,
 
506
                                    cairo_fixed_t offy,
 
507
                                    cairo_fixed_t scalex,
 
508
                                    cairo_fixed_t scaley)
 
509
{
 
510
    cairo_path_buf_t *buf = path->buf_head;
 
511
    int i;
 
512
    cairo_int64_t i64temp;
 
513
    cairo_fixed_t fixedtemp;
 
514
 
 
515
    while (buf) {
 
516
         for (i = 0; i < buf->num_points; i++) {
 
517
             if (scalex == CAIRO_FIXED_ONE) {
 
518
                 buf->points[i].x += offx;
 
519
             } else {
 
520
                 fixedtemp = buf->points[i].x + offx;
 
521
                 i64temp = _cairo_int32x32_64_mul (fixedtemp, scalex);
 
522
                 buf->points[i].x = _cairo_int64_to_int32(_cairo_int64_rsl (i64temp, 16));
 
523
             }
 
524
 
 
525
             if (scaley == CAIRO_FIXED_ONE) {
 
526
                 buf->points[i].y += offy;
 
527
             } else {
 
528
                 fixedtemp = buf->points[i].y + offy;
 
529
                 i64temp = _cairo_int32x32_64_mul (fixedtemp, scaley);
 
530
                 buf->points[i].y = _cairo_int64_to_int32(_cairo_int64_rsl (i64temp, 16));
 
531
             }
 
532
         }
 
533
 
 
534
         buf = buf->next;
 
535
    }
 
536
}
 
537
 
 
538
 
 
539
/**
 
540
 * _cairo_path_fixed_device_transform:
 
541
 * @path: a #cairo_path_fixed_t to be transformed
 
542
 * @device_transform: a matrix with only scaling/translation (no rotation or shear)
 
543
 *
 
544
 * Transform the fixed-point path according to the scaling and
 
545
 * translation of the given matrix. This function assert()s that the
 
546
 * given matrix has no rotation or shear elements, (that is, xy and yx
 
547
 * are 0.0).
 
548
 **/
 
549
void
 
550
_cairo_path_fixed_device_transform (cairo_path_fixed_t  *path,
 
551
                                    cairo_matrix_t      *device_transform)
 
552
{
 
553
    assert (device_transform->yx == 0.0 && device_transform->xy == 0.0);
 
554
    /* XXX: FRAGILE: I'm not really sure whether we're doing the
 
555
     * "right" thing here if there is both scaling and translation in
 
556
     * the matrix. But for now, the internals guarantee that we won't
 
557
     * really ever have both going on. */
 
558
    _cairo_path_fixed_offset_and_scale (path,
 
559
                                        _cairo_fixed_from_double (device_transform->x0),
 
560
                                        _cairo_fixed_from_double (device_transform->y0),
 
561
                                        _cairo_fixed_from_double (device_transform->xx),
 
562
                                        _cairo_fixed_from_double (device_transform->yy));
 
563
}