[18608] | 1 | /* vim: set sw=4: -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
---|
[17276] | 2 | /* |
---|
| 3 | rsvg-bpath-util.c: Data structure and convenience functions for creating bezier paths. |
---|
| 4 | |
---|
| 5 | Copyright (C) 2000 Eazel, Inc. |
---|
| 6 | |
---|
| 7 | This program is free software; you can redistribute it and/or |
---|
[18274] | 8 | modify it under the terms of the GNU Library General Public License as |
---|
[17276] | 9 | published by the Free Software Foundation; either version 2 of the |
---|
| 10 | License, or (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 GNU |
---|
[18274] | 15 | Library General Public License for more details. |
---|
[17276] | 16 | |
---|
[18274] | 17 | You should have received a copy of the GNU Library General Public |
---|
[17276] | 18 | License along with this program; if not, write to the |
---|
| 19 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
| 20 | Boston, MA 02111-1307, USA. |
---|
| 21 | |
---|
| 22 | Author: Raph Levien <raph@artofcode.com> |
---|
| 23 | */ |
---|
| 24 | |
---|
[18274] | 25 | #include "config.h" |
---|
| 26 | #include "rsvg-bpath-util.h" |
---|
| 27 | |
---|
| 28 | #include <glib/gmem.h> |
---|
| 29 | #include <glib/gmessages.h> |
---|
[17276] | 30 | #include <math.h> |
---|
| 31 | #include <string.h> |
---|
| 32 | |
---|
| 33 | /* This is adapted from gnome-canvas-bpath-util in libgnomeprint |
---|
| 34 | (originally developed as part of Gill). */ |
---|
| 35 | |
---|
| 36 | RsvgBpathDef * |
---|
| 37 | rsvg_bpath_def_new (void) |
---|
| 38 | { |
---|
[18608] | 39 | RsvgBpathDef *bpd; |
---|
| 40 | |
---|
| 41 | bpd = g_new (RsvgBpathDef, 1); |
---|
| 42 | bpd->n_bpath = 0; |
---|
| 43 | bpd->n_bpath_max = 16; |
---|
| 44 | bpd->moveto_idx = -1; |
---|
| 45 | bpd->bpath = g_new (ArtBpath, bpd->n_bpath_max); |
---|
| 46 | |
---|
| 47 | return bpd; |
---|
[17276] | 48 | } |
---|
| 49 | |
---|
| 50 | RsvgBpathDef * |
---|
| 51 | rsvg_bpath_def_new_from (ArtBpath *path) |
---|
| 52 | { |
---|
[18608] | 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->bpath = g_new (ArtBpath, i); |
---|
| 69 | |
---|
| 70 | memcpy (bpd->bpath, path, i * sizeof (ArtBpath)); |
---|
| 71 | return bpd; |
---|
[17276] | 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | void |
---|
| 76 | rsvg_bpath_def_free (RsvgBpathDef *bpd) |
---|
| 77 | { |
---|
[18608] | 78 | g_return_if_fail (bpd != NULL); |
---|
| 79 | |
---|
| 80 | g_free (bpd->bpath); |
---|
| 81 | g_free (bpd); |
---|
[17276] | 82 | } |
---|
| 83 | |
---|
| 84 | void |
---|
| 85 | rsvg_bpath_def_moveto (RsvgBpathDef *bpd, double x, double y) |
---|
| 86 | { |
---|
[18608] | 87 | ArtBpath *bpath; |
---|
| 88 | int n_bpath; |
---|
| 89 | |
---|
| 90 | g_return_if_fail (bpd != NULL); |
---|
| 91 | |
---|
| 92 | n_bpath = bpd->n_bpath++; |
---|
| 93 | |
---|
| 94 | if (n_bpath == bpd->n_bpath_max) |
---|
| 95 | bpd->bpath = g_realloc (bpd->bpath, |
---|
| 96 | (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath)); |
---|
| 97 | bpath = bpd->bpath; |
---|
| 98 | bpath[n_bpath].code = ART_MOVETO_OPEN; |
---|
| 99 | bpath[n_bpath].x3 = x; |
---|
| 100 | bpath[n_bpath].y3 = y; |
---|
| 101 | bpd->moveto_idx = n_bpath; |
---|
[17276] | 102 | } |
---|
| 103 | |
---|
| 104 | void |
---|
| 105 | rsvg_bpath_def_lineto (RsvgBpathDef *bpd, double x, double y) |
---|
| 106 | { |
---|
[18608] | 107 | ArtBpath *bpath; |
---|
| 108 | int n_bpath; |
---|
| 109 | |
---|
| 110 | g_return_if_fail (bpd != NULL); |
---|
| 111 | g_return_if_fail (bpd->moveto_idx >= 0); |
---|
| 112 | |
---|
| 113 | n_bpath = bpd->n_bpath++; |
---|
| 114 | |
---|
| 115 | if (n_bpath == bpd->n_bpath_max) |
---|
| 116 | bpd->bpath = g_realloc (bpd->bpath, |
---|
| 117 | (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath)); |
---|
| 118 | bpath = bpd->bpath; |
---|
| 119 | bpath[n_bpath].code = ART_LINETO; |
---|
| 120 | bpath[n_bpath].x3 = x; |
---|
| 121 | bpath[n_bpath].y3 = y; |
---|
[17276] | 122 | } |
---|
| 123 | |
---|
| 124 | void |
---|
| 125 | rsvg_bpath_def_curveto (RsvgBpathDef *bpd, double x1, double y1, double x2, double y2, double x3, double y3) |
---|
| 126 | { |
---|
[18608] | 127 | ArtBpath *bpath; |
---|
| 128 | int n_bpath; |
---|
| 129 | |
---|
| 130 | g_return_if_fail (bpd != NULL); |
---|
| 131 | g_return_if_fail (bpd->moveto_idx >= 0); |
---|
| 132 | |
---|
| 133 | n_bpath = bpd->n_bpath++; |
---|
| 134 | |
---|
| 135 | if (n_bpath == bpd->n_bpath_max) |
---|
| 136 | bpd->bpath = g_realloc (bpd->bpath, |
---|
| 137 | (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath)); |
---|
| 138 | bpath = bpd->bpath; |
---|
| 139 | bpath[n_bpath].code = ART_CURVETO; |
---|
| 140 | bpath[n_bpath].x1 = x1; |
---|
| 141 | bpath[n_bpath].y1 = y1; |
---|
| 142 | bpath[n_bpath].x2 = x2; |
---|
| 143 | bpath[n_bpath].y2 = y2; |
---|
| 144 | bpath[n_bpath].x3 = x3; |
---|
| 145 | bpath[n_bpath].y3 = y3; |
---|
[17276] | 146 | } |
---|
| 147 | |
---|
| 148 | void |
---|
| 149 | rsvg_bpath_def_closepath (RsvgBpathDef *bpd) |
---|
| 150 | { |
---|
[18608] | 151 | ArtBpath *bpath; |
---|
| 152 | int n_bpath; |
---|
| 153 | |
---|
| 154 | g_return_if_fail (bpd != NULL); |
---|
| 155 | g_return_if_fail (bpd->moveto_idx >= 0); |
---|
| 156 | g_return_if_fail (bpd->n_bpath > 0); |
---|
[17276] | 157 | |
---|
[18608] | 158 | bpath = bpd->bpath; |
---|
| 159 | n_bpath = bpd->n_bpath; |
---|
| 160 | |
---|
| 161 | /* Add closing vector if we need it. */ |
---|
| 162 | if (bpath[n_bpath - 1].x3 != bpath[bpd->moveto_idx].x3 || |
---|
| 163 | bpath[n_bpath - 1].y3 != bpath[bpd->moveto_idx].y3) |
---|
| 164 | { |
---|
| 165 | rsvg_bpath_def_lineto (bpd, bpath[bpd->moveto_idx].x3, |
---|
| 166 | bpath[bpd->moveto_idx].y3); |
---|
| 167 | bpath = bpd->bpath; |
---|
| 168 | } |
---|
| 169 | bpath[bpd->moveto_idx].code = ART_MOVETO; |
---|
| 170 | bpd->moveto_idx = -1; |
---|
[17276] | 171 | } |
---|
| 172 | |
---|
| 173 | void |
---|
| 174 | rsvg_bpath_def_art_finish (RsvgBpathDef *bpd) |
---|
| 175 | { |
---|
[18608] | 176 | int n_bpath; |
---|
| 177 | |
---|
| 178 | g_return_if_fail (bpd != NULL); |
---|
| 179 | |
---|
| 180 | n_bpath = bpd->n_bpath++; |
---|
| 181 | |
---|
| 182 | if (n_bpath == bpd->n_bpath_max) |
---|
| 183 | bpd->bpath = g_realloc (bpd->bpath, |
---|
| 184 | (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath)); |
---|
| 185 | bpd->bpath[n_bpath].code = ART_END; |
---|
[17276] | 186 | } |
---|