#
source:
trunk/third/libart_lgpl/art_svp_vpath_stroke.c
@
18256

Revision 18256, 20.8 KB checked in by ghudson, 22 years ago (diff) |
---|

Line | |
---|---|

1 | /* Libart_LGPL - library of basic graphic primitives |

2 | * Copyright (C) 1998-2000 Raph Levien |

3 | * |

4 | * This library is free software; you can redistribute it and/or |

5 | * modify it under the terms of the GNU Library General Public |

6 | * License as published by the Free Software Foundation; either |

7 | * version 2 of the License, or (at your option) any later version. |

8 | * |

9 | * This library is distributed in the hope that it will be useful, |

10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |

11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |

12 | * Library General Public License for more details. |

13 | * |

14 | * You should have received a copy of the GNU Library General Public |

15 | * License along with this library; if not, write to the |

16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |

17 | * Boston, MA 02111-1307, USA. |

18 | */ |

19 | |

20 | |

21 | #include "config.h" |

22 | #include "art_svp_vpath_stroke.h" |

23 | |

24 | #include <stdlib.h> |

25 | #include <math.h> |

26 | |

27 | #include "art_misc.h" |

28 | |

29 | #include "art_vpath.h" |

30 | #include "art_svp.h" |

31 | #ifdef ART_USE_NEW_INTERSECTOR |

32 | #include "art_svp_intersect.h" |

33 | #else |

34 | #include "art_svp_wind.h" |

35 | #endif |

36 | #include "art_svp_vpath.h" |

37 | |

38 | #define EPSILON 1e-6 |

39 | #define EPSILON_2 1e-12 |

40 | |

41 | #define yes_OPTIMIZE_INNER |

42 | |

43 | /* Render an arc segment starting at (xc + x0, yc + y0) to (xc + x1, |

44 | yc + y1), centered at (xc, yc), and with given radius. Both x0^2 + |

45 | y0^2 and x1^2 + y1^2 should be equal to radius^2. |

46 | |

47 | A positive value of radius means curve to the left, negative means |

48 | curve to the right. |

49 | */ |

50 | static void |

51 | art_svp_vpath_stroke_arc (ArtVpath **p_vpath, int *pn, int *pn_max, |

52 | double xc, double yc, |

53 | double x0, double y0, |

54 | double x1, double y1, |

55 | double radius, |

56 | double flatness) |

57 | { |

58 | double theta; |

59 | double th_0, th_1; |

60 | int n_pts; |

61 | int i; |

62 | double aradius; |

63 | |

64 | aradius = fabs (radius); |

65 | theta = 2 * M_SQRT2 * sqrt (flatness / aradius); |

66 | th_0 = atan2 (y0, x0); |

67 | th_1 = atan2 (y1, x1); |

68 | if (radius > 0) |

69 | { |

70 | /* curve to the left */ |

71 | if (th_0 < th_1) th_0 += M_PI * 2; |

72 | n_pts = ceil ((th_0 - th_1) / theta); |

73 | } |

74 | else |

75 | { |

76 | /* curve to the right */ |

77 | if (th_1 < th_0) th_1 += M_PI * 2; |

78 | n_pts = ceil ((th_1 - th_0) / theta); |

79 | } |

80 | #ifdef VERBOSE |

81 | printf ("start %f %f; th_0 = %f, th_1 = %f, r = %f, theta = %f\n", x0, y0, th_0, th_1, radius, theta); |

82 | #endif |

83 | art_vpath_add_point (p_vpath, pn, pn_max, |

84 | ART_LINETO, xc + x0, yc + y0); |

85 | for (i = 1; i < n_pts; i++) |

86 | { |

87 | theta = th_0 + (th_1 - th_0) * i / n_pts; |

88 | art_vpath_add_point (p_vpath, pn, pn_max, |

89 | ART_LINETO, xc + cos (theta) * aradius, |

90 | yc + sin (theta) * aradius); |

91 | #ifdef VERBOSE |

92 | printf ("mid %f %f\n", cos (theta) * radius, sin (theta) * radius); |

93 | #endif |

94 | } |

95 | art_vpath_add_point (p_vpath, pn, pn_max, |

96 | ART_LINETO, xc + x1, yc + y1); |

97 | #ifdef VERBOSE |

98 | printf ("end %f %f\n", x1, y1); |

99 | #endif |

100 | } |

101 | |

102 | /* Assume that forw and rev are at point i0. Bring them to i1, |

103 | joining with the vector i1 - i2. |

104 | |

105 | This used to be true, but isn't now that the stroke_raw code is |

106 | filtering out (near)zero length vectors: {It so happens that all |

107 | invocations of this function maintain the precondition i1 = i0 + 1, |

108 | so we could decrease the number of arguments by one. We haven't |

109 | done that here, though.} |

110 | |

111 | forw is to the line's right and rev is to its left. |

112 | |

113 | Precondition: no zero-length vectors, otherwise a divide by |

114 | zero will happen. */ |

115 | static void |

116 | render_seg (ArtVpath **p_forw, int *pn_forw, int *pn_forw_max, |

117 | ArtVpath **p_rev, int *pn_rev, int *pn_rev_max, |

118 | ArtVpath *vpath, int i0, int i1, int i2, |

119 | ArtPathStrokeJoinType join, |

120 | double line_width, double miter_limit, double flatness) |

121 | { |

122 | double dx0, dy0; |

123 | double dx1, dy1; |

124 | double dlx0, dly0; |

125 | double dlx1, dly1; |

126 | double dmx, dmy; |

127 | double dmr2; |

128 | double scale; |

129 | double cross; |

130 | |

131 | #ifdef VERBOSE |

132 | printf ("join style = %d\n", join); |

133 | #endif |

134 | |

135 | /* The vectors of the lines from i0 to i1 and i1 to i2. */ |

136 | dx0 = vpath[i1].x - vpath[i0].x; |

137 | dy0 = vpath[i1].y - vpath[i0].y; |

138 | |

139 | dx1 = vpath[i2].x - vpath[i1].x; |

140 | dy1 = vpath[i2].y - vpath[i1].y; |

141 | |

142 | /* Set dl[xy]0 to the vector from i0 to i1, rotated counterclockwise |

143 | 90 degrees, and scaled to the length of line_width. */ |

144 | scale = line_width / sqrt (dx0 * dx0 + dy0 * dy0); |

145 | dlx0 = dy0 * scale; |

146 | dly0 = -dx0 * scale; |

147 | |

148 | /* Set dl[xy]1 to the vector from i1 to i2, rotated counterclockwise |

149 | 90 degrees, and scaled to the length of line_width. */ |

150 | scale = line_width / sqrt (dx1 * dx1 + dy1 * dy1); |

151 | dlx1 = dy1 * scale; |

152 | dly1 = -dx1 * scale; |

153 | |

154 | #ifdef VERBOSE |

155 | printf ("%% render_seg: (%g, %g) - (%g, %g) - (%g, %g)\n", |

156 | vpath[i0].x, vpath[i0].y, |

157 | vpath[i1].x, vpath[i1].y, |

158 | vpath[i2].x, vpath[i2].y); |

159 | |

160 | printf ("%% render_seg: d[xy]0 = (%g, %g), dl[xy]0 = (%g, %g)\n", |

161 | dx0, dy0, dlx0, dly0); |

162 | |

163 | printf ("%% render_seg: d[xy]1 = (%g, %g), dl[xy]1 = (%g, %g)\n", |

164 | dx1, dy1, dlx1, dly1); |

165 | #endif |

166 | |

167 | /* now, forw's last point is expected to be colinear along d[xy]0 |

168 | to point i0 - dl[xy]0, and rev with i0 + dl[xy]0. */ |

169 | |

170 | /* positive for positive area (i.e. left turn) */ |

171 | cross = dx1 * dy0 - dx0 * dy1; |

172 | |

173 | dmx = (dlx0 + dlx1) * 0.5; |

174 | dmy = (dly0 + dly1) * 0.5; |

175 | dmr2 = dmx * dmx + dmy * dmy; |

176 | |

177 | if (join == ART_PATH_STROKE_JOIN_MITER && |

178 | dmr2 * miter_limit * miter_limit < line_width * line_width) |

179 | join = ART_PATH_STROKE_JOIN_BEVEL; |

180 | |

181 | /* the case when dmr2 is zero or very small bothers me |

182 | (i.e. near a 180 degree angle) */ |

183 | scale = line_width * line_width / dmr2; |

184 | dmx *= scale; |

185 | dmy *= scale; |

186 | |

187 | if (cross * cross < EPSILON_2 && dx0 * dx1 + dy0 * dy1 >= 0) |

188 | { |

189 | /* going straight */ |

190 | #ifdef VERBOSE |

191 | printf ("%% render_seg: straight\n"); |

192 | #endif |

193 | art_vpath_add_point (p_forw, pn_forw, pn_forw_max, |

194 | ART_LINETO, vpath[i1].x - dlx0, vpath[i1].y - dly0); |

195 | art_vpath_add_point (p_rev, pn_rev, pn_rev_max, |

196 | ART_LINETO, vpath[i1].x + dlx0, vpath[i1].y + dly0); |

197 | } |

198 | else if (cross > 0) |

199 | { |

200 | /* left turn, forw is outside and rev is inside */ |

201 | |

202 | #ifdef VERBOSE |

203 | printf ("%% render_seg: left\n"); |

204 | #endif |

205 | if ( |

206 | #ifdef NO_OPTIMIZE_INNER |

207 | 0 && |

208 | #endif |

209 | /* check that i1 + dm[xy] is inside i0-i1 rectangle */ |

210 | (dx0 + dmx) * dx0 + (dy0 + dmy) * dy0 > 0 && |

211 | /* and that i1 + dm[xy] is inside i1-i2 rectangle */ |

212 | ((dx1 - dmx) * dx1 + (dy1 - dmy) * dy1 > 0) |

213 | #ifdef PEDANTIC_INNER |

214 | && |

215 | /* check that i1 + dl[xy]1 is inside i0-i1 rectangle */ |

216 | (dx0 + dlx1) * dx0 + (dy0 + dly1) * dy0 > 0 && |

217 | /* and that i1 + dl[xy]0 is inside i1-i2 rectangle */ |

218 | ((dx1 - dlx0) * dx1 + (dy1 - dly0) * dy1 > 0) |

219 | #endif |

220 | ) |

221 | { |

222 | /* can safely add single intersection point */ |

223 | art_vpath_add_point (p_rev, pn_rev, pn_rev_max, |

224 | ART_LINETO, vpath[i1].x + dmx, vpath[i1].y + dmy); |

225 | } |

226 | else |

227 | { |

228 | /* need to loop-de-loop the inside */ |

229 | art_vpath_add_point (p_rev, pn_rev, pn_rev_max, |

230 | ART_LINETO, vpath[i1].x + dlx0, vpath[i1].y + dly0); |

231 | art_vpath_add_point (p_rev, pn_rev, pn_rev_max, |

232 | ART_LINETO, vpath[i1].x, vpath[i1].y); |

233 | art_vpath_add_point (p_rev, pn_rev, pn_rev_max, |

234 | ART_LINETO, vpath[i1].x + dlx1, vpath[i1].y + dly1); |

235 | } |

236 | |

237 | if (join == ART_PATH_STROKE_JOIN_BEVEL) |

238 | { |

239 | /* bevel */ |

240 | art_vpath_add_point (p_forw, pn_forw, pn_forw_max, |

241 | ART_LINETO, vpath[i1].x - dlx0, vpath[i1].y - dly0); |

242 | art_vpath_add_point (p_forw, pn_forw, pn_forw_max, |

243 | ART_LINETO, vpath[i1].x - dlx1, vpath[i1].y - dly1); |

244 | } |

245 | else if (join == ART_PATH_STROKE_JOIN_MITER) |

246 | { |

247 | art_vpath_add_point (p_forw, pn_forw, pn_forw_max, |

248 | ART_LINETO, vpath[i1].x - dmx, vpath[i1].y - dmy); |

249 | } |

250 | else if (join == ART_PATH_STROKE_JOIN_ROUND) |

251 | art_svp_vpath_stroke_arc (p_forw, pn_forw, pn_forw_max, |

252 | vpath[i1].x, vpath[i1].y, |

253 | -dlx0, -dly0, |

254 | -dlx1, -dly1, |

255 | line_width, |

256 | flatness); |

257 | } |

258 | else |

259 | { |

260 | /* right turn, rev is outside and forw is inside */ |

261 | #ifdef VERBOSE |

262 | printf ("%% render_seg: right\n"); |

263 | #endif |

264 | |

265 | if ( |

266 | #ifdef NO_OPTIMIZE_INNER |

267 | 0 && |

268 | #endif |

269 | /* check that i1 - dm[xy] is inside i0-i1 rectangle */ |

270 | (dx0 - dmx) * dx0 + (dy0 - dmy) * dy0 > 0 && |

271 | /* and that i1 - dm[xy] is inside i1-i2 rectangle */ |

272 | ((dx1 + dmx) * dx1 + (dy1 + dmy) * dy1 > 0) |

273 | #ifdef PEDANTIC_INNER |

274 | && |

275 | /* check that i1 - dl[xy]1 is inside i0-i1 rectangle */ |

276 | (dx0 - dlx1) * dx0 + (dy0 - dly1) * dy0 > 0 && |

277 | /* and that i1 - dl[xy]0 is inside i1-i2 rectangle */ |

278 | ((dx1 + dlx0) * dx1 + (dy1 + dly0) * dy1 > 0) |

279 | #endif |

280 | ) |

281 | { |

282 | /* can safely add single intersection point */ |

283 | art_vpath_add_point (p_forw, pn_forw, pn_forw_max, |

284 | ART_LINETO, vpath[i1].x - dmx, vpath[i1].y - dmy); |

285 | } |

286 | else |

287 | { |

288 | /* need to loop-de-loop the inside */ |

289 | art_vpath_add_point (p_forw, pn_forw, pn_forw_max, |

290 | ART_LINETO, vpath[i1].x - dlx0, vpath[i1].y - dly0); |

291 | art_vpath_add_point (p_forw, pn_forw, pn_forw_max, |

292 | ART_LINETO, vpath[i1].x, vpath[i1].y); |

293 | art_vpath_add_point (p_forw, pn_forw, pn_forw_max, |

294 | ART_LINETO, vpath[i1].x - dlx1, vpath[i1].y - dly1); |

295 | } |

296 | |

297 | if (join == ART_PATH_STROKE_JOIN_BEVEL) |

298 | { |

299 | /* bevel */ |

300 | art_vpath_add_point (p_rev, pn_rev, pn_rev_max, |

301 | ART_LINETO, vpath[i1].x + dlx0, vpath[i1].y + dly0); |

302 | art_vpath_add_point (p_rev, pn_rev, pn_rev_max, |

303 | ART_LINETO, vpath[i1].x + dlx1, vpath[i1].y + dly1); |

304 | } |

305 | else if (join == ART_PATH_STROKE_JOIN_MITER) |

306 | { |

307 | art_vpath_add_point (p_rev, pn_rev, pn_rev_max, |

308 | ART_LINETO, vpath[i1].x + dmx, vpath[i1].y + dmy); |

309 | } |

310 | else if (join == ART_PATH_STROKE_JOIN_ROUND) |

311 | art_svp_vpath_stroke_arc (p_rev, pn_rev, pn_rev_max, |

312 | vpath[i1].x, vpath[i1].y, |

313 | dlx0, dly0, |

314 | dlx1, dly1, |

315 | -line_width, |

316 | flatness); |

317 | |

318 | } |

319 | } |

320 | |

321 | /* caps i1, under the assumption of a vector from i0 */ |

322 | static void |

323 | render_cap (ArtVpath **p_result, int *pn_result, int *pn_result_max, |

324 | ArtVpath *vpath, int i0, int i1, |

325 | ArtPathStrokeCapType cap, double line_width, double flatness) |

326 | { |

327 | double dx0, dy0; |

328 | double dlx0, dly0; |

329 | double scale; |

330 | int n_pts; |

331 | int i; |

332 | |

333 | dx0 = vpath[i1].x - vpath[i0].x; |

334 | dy0 = vpath[i1].y - vpath[i0].y; |

335 | |

336 | /* Set dl[xy]0 to the vector from i0 to i1, rotated counterclockwise |

337 | 90 degrees, and scaled to the length of line_width. */ |

338 | scale = line_width / sqrt (dx0 * dx0 + dy0 * dy0); |

339 | dlx0 = dy0 * scale; |

340 | dly0 = -dx0 * scale; |

341 | |

342 | #ifdef VERBOSE |

343 | printf ("cap style = %d\n", cap); |

344 | #endif |

345 | |

346 | switch (cap) |

347 | { |

348 | case ART_PATH_STROKE_CAP_BUTT: |

349 | art_vpath_add_point (p_result, pn_result, pn_result_max, |

350 | ART_LINETO, vpath[i1].x - dlx0, vpath[i1].y - dly0); |

351 | art_vpath_add_point (p_result, pn_result, pn_result_max, |

352 | ART_LINETO, vpath[i1].x + dlx0, vpath[i1].y + dly0); |

353 | break; |

354 | case ART_PATH_STROKE_CAP_ROUND: |

355 | n_pts = ceil (M_PI / (2.0 * M_SQRT2 * sqrt (flatness / line_width))); |

356 | art_vpath_add_point (p_result, pn_result, pn_result_max, |

357 | ART_LINETO, vpath[i1].x - dlx0, vpath[i1].y - dly0); |

358 | for (i = 1; i < n_pts; i++) |

359 | { |

360 | double theta, c_th, s_th; |

361 | |

362 | theta = M_PI * i / n_pts; |

363 | c_th = cos (theta); |

364 | s_th = sin (theta); |

365 | art_vpath_add_point (p_result, pn_result, pn_result_max, |

366 | ART_LINETO, |

367 | vpath[i1].x - dlx0 * c_th - dly0 * s_th, |

368 | vpath[i1].y - dly0 * c_th + dlx0 * s_th); |

369 | } |

370 | art_vpath_add_point (p_result, pn_result, pn_result_max, |

371 | ART_LINETO, vpath[i1].x + dlx0, vpath[i1].y + dly0); |

372 | break; |

373 | case ART_PATH_STROKE_CAP_SQUARE: |

374 | art_vpath_add_point (p_result, pn_result, pn_result_max, |

375 | ART_LINETO, |

376 | vpath[i1].x - dlx0 - dly0, |

377 | vpath[i1].y - dly0 + dlx0); |

378 | art_vpath_add_point (p_result, pn_result, pn_result_max, |

379 | ART_LINETO, |

380 | vpath[i1].x + dlx0 - dly0, |

381 | vpath[i1].y + dly0 + dlx0); |

382 | break; |

383 | } |

384 | } |

385 | |

386 | /** |

387 | * art_svp_from_vpath_raw: Stroke a vector path, raw version |

388 | * @vpath: #ArtVPath to stroke. |

389 | * @join: Join style. |

390 | * @cap: Cap style. |

391 | * @line_width: Width of stroke. |

392 | * @miter_limit: Miter limit. |

393 | * @flatness: Flatness. |

394 | * |

395 | * Exactly the same as art_svp_vpath_stroke(), except that the resulting |

396 | * stroke outline may self-intersect and have regions of winding number |

397 | * greater than 1. |

398 | * |

399 | * Return value: Resulting raw stroked outline in svp format. |

400 | **/ |

401 | ArtVpath * |

402 | art_svp_vpath_stroke_raw (ArtVpath *vpath, |

403 | ArtPathStrokeJoinType join, |

404 | ArtPathStrokeCapType cap, |

405 | double line_width, |

406 | double miter_limit, |

407 | double flatness) |

408 | { |

409 | int begin_idx, end_idx; |

410 | int i; |

411 | ArtVpath *forw, *rev; |

412 | int n_forw, n_rev; |

413 | int n_forw_max, n_rev_max; |

414 | ArtVpath *result; |

415 | int n_result, n_result_max; |

416 | double half_lw = 0.5 * line_width; |

417 | int closed; |

418 | int last, this, next, second; |

419 | double dx, dy; |

420 | |

421 | n_forw_max = 16; |

422 | forw = art_new (ArtVpath, n_forw_max); |

423 | |

424 | n_rev_max = 16; |

425 | rev = art_new (ArtVpath, n_rev_max); |

426 | |

427 | n_result = 0; |

428 | n_result_max = 16; |

429 | result = art_new (ArtVpath, n_result_max); |

430 | |

431 | for (begin_idx = 0; vpath[begin_idx].code != ART_END; begin_idx = end_idx) |

432 | { |

433 | n_forw = 0; |

434 | n_rev = 0; |

435 | |

436 | closed = (vpath[begin_idx].code == ART_MOVETO); |

437 | |

438 | /* we don't know what the first point joins with until we get to the |

439 | last point and see if it's closed. So we start with the second |

440 | line in the path. |

441 | |

442 | Note: this is not strictly true (we now know it's closed from |

443 | the opening pathcode), but why fix code that isn't broken? |

444 | */ |

445 | |

446 | this = begin_idx; |

447 | /* skip over identical points at the beginning of the subpath */ |

448 | for (i = this + 1; vpath[i].code == ART_LINETO; i++) |

449 | { |

450 | dx = vpath[i].x - vpath[this].x; |

451 | dy = vpath[i].y - vpath[this].y; |

452 | if (dx * dx + dy * dy > EPSILON_2) |

453 | break; |

454 | } |

455 | next = i; |

456 | second = next; |

457 | |

458 | /* invariant: this doesn't coincide with next */ |

459 | while (vpath[next].code == ART_LINETO) |

460 | { |

461 | last = this; |

462 | this = next; |

463 | /* skip over identical points after the beginning of the subpath */ |

464 | for (i = this + 1; vpath[i].code == ART_LINETO; i++) |

465 | { |

466 | dx = vpath[i].x - vpath[this].x; |

467 | dy = vpath[i].y - vpath[this].y; |

468 | if (dx * dx + dy * dy > EPSILON_2) |

469 | break; |

470 | } |

471 | next = i; |

472 | if (vpath[next].code != ART_LINETO) |

473 | { |

474 | /* reached end of path */ |

475 | /* make "closed" detection conform to PostScript |

476 | semantics (i.e. explicit closepath code rather than |

477 | just the fact that end of the path is the beginning) */ |

478 | if (closed && |

479 | vpath[this].x == vpath[begin_idx].x && |

480 | vpath[this].y == vpath[begin_idx].y) |

481 | { |

482 | int j; |

483 | |

484 | /* path is closed, render join to beginning */ |

485 | render_seg (&forw, &n_forw, &n_forw_max, |

486 | &rev, &n_rev, &n_rev_max, |

487 | vpath, last, this, second, |

488 | join, half_lw, miter_limit, flatness); |

489 | |

490 | #ifdef VERBOSE |

491 | printf ("%% forw %d, rev %d\n", n_forw, n_rev); |

492 | #endif |

493 | /* do forward path */ |

494 | art_vpath_add_point (&result, &n_result, &n_result_max, |

495 | ART_MOVETO, forw[n_forw - 1].x, |

496 | forw[n_forw - 1].y); |

497 | for (j = 0; j < n_forw; j++) |

498 | art_vpath_add_point (&result, &n_result, &n_result_max, |

499 | ART_LINETO, forw[j].x, |

500 | forw[j].y); |

501 | |

502 | /* do reverse path, reversed */ |

503 | art_vpath_add_point (&result, &n_result, &n_result_max, |

504 | ART_MOVETO, rev[0].x, |

505 | rev[0].y); |

506 | for (j = n_rev - 1; j >= 0; j--) |

507 | art_vpath_add_point (&result, &n_result, &n_result_max, |

508 | ART_LINETO, rev[j].x, |

509 | rev[j].y); |

510 | } |

511 | else |

512 | { |

513 | /* path is open */ |

514 | int j; |

515 | |

516 | /* add to forw rather than result to ensure that |

517 | forw has at least one point. */ |

518 | render_cap (&forw, &n_forw, &n_forw_max, |

519 | vpath, last, this, |

520 | cap, half_lw, flatness); |

521 | art_vpath_add_point (&result, &n_result, &n_result_max, |

522 | ART_MOVETO, forw[0].x, |

523 | forw[0].y); |

524 | for (j = 1; j < n_forw; j++) |

525 | art_vpath_add_point (&result, &n_result, &n_result_max, |

526 | ART_LINETO, forw[j].x, |

527 | forw[j].y); |

528 | for (j = n_rev - 1; j >= 0; j--) |

529 | art_vpath_add_point (&result, &n_result, &n_result_max, |

530 | ART_LINETO, rev[j].x, |

531 | rev[j].y); |

532 | render_cap (&result, &n_result, &n_result_max, |

533 | vpath, second, begin_idx, |

534 | cap, half_lw, flatness); |

535 | art_vpath_add_point (&result, &n_result, &n_result_max, |

536 | ART_LINETO, forw[0].x, |

537 | forw[0].y); |

538 | } |

539 | } |

540 | else |

541 | render_seg (&forw, &n_forw, &n_forw_max, |

542 | &rev, &n_rev, &n_rev_max, |

543 | vpath, last, this, next, |

544 | join, half_lw, miter_limit, flatness); |

545 | } |

546 | end_idx = next; |

547 | } |

548 | |

549 | art_free (forw); |

550 | art_free (rev); |

551 | #ifdef VERBOSE |

552 | printf ("%% n_result = %d\n", n_result); |

553 | #endif |

554 | art_vpath_add_point (&result, &n_result, &n_result_max, ART_END, 0, 0); |

555 | return result; |

556 | } |

557 | |

558 | #define noVERBOSE |

559 | |

560 | #ifdef VERBOSE |

561 | |

562 | #define XOFF 50 |

563 | #define YOFF 700 |

564 | |

565 | static void |

566 | print_ps_vpath (ArtVpath *vpath) |

567 | { |

568 | int i; |

569 | |

570 | for (i = 0; vpath[i].code != ART_END; i++) |

571 | { |

572 | switch (vpath[i].code) |

573 | { |

574 | case ART_MOVETO: |

575 | printf ("%g %g moveto\n", XOFF + vpath[i].x, YOFF - vpath[i].y); |

576 | break; |

577 | case ART_LINETO: |

578 | printf ("%g %g lineto\n", XOFF + vpath[i].x, YOFF - vpath[i].y); |

579 | break; |

580 | default: |

581 | break; |

582 | } |

583 | } |

584 | printf ("stroke showpage\n"); |

585 | } |

586 | |

587 | static void |

588 | print_ps_svp (ArtSVP *vpath) |

589 | { |

590 | int i, j; |

591 | |

592 | printf ("%% begin\n"); |

593 | for (i = 0; i < vpath->n_segs; i++) |

594 | { |

595 | printf ("%g setgray\n", vpath->segs[i].dir ? 0.7 : 0); |

596 | for (j = 0; j < vpath->segs[i].n_points; j++) |

597 | { |

598 | printf ("%g %g %s\n", |

599 | XOFF + vpath->segs[i].points[j].x, |

600 | YOFF - vpath->segs[i].points[j].y, |

601 | j ? "lineto" : "moveto"); |

602 | } |

603 | printf ("stroke\n"); |

604 | } |

605 | |

606 | printf ("showpage\n"); |

607 | } |

608 | #endif |

609 | |

610 | /* Render a vector path into a stroked outline. |

611 | |

612 | Status of this routine: |

613 | |

614 | Basic correctness: Only miter and bevel line joins are implemented, |

615 | and only butt line caps. Otherwise, seems to be fine. |

616 | |

617 | Numerical stability: We cheat (adding random perturbation). Thus, |

618 | it seems very likely that no numerical stability problems will be |

619 | seen in practice. |

620 | |

621 | Speed: Should be pretty good. |

622 | |

623 | Precision: The perturbation fuzzes the coordinates slightly, |

624 | but not enough to be visible. */ |

625 | /** |

626 | * art_svp_vpath_stroke: Stroke a vector path. |

627 | * @vpath: #ArtVPath to stroke. |

628 | * @join: Join style. |

629 | * @cap: Cap style. |

630 | * @line_width: Width of stroke. |

631 | * @miter_limit: Miter limit. |

632 | * @flatness: Flatness. |

633 | * |

634 | * Computes an svp representing the stroked outline of @vpath. The |

635 | * width of the stroked line is @line_width. |

636 | * |

637 | * Lines are joined according to the @join rule. Possible values are |

638 | * ART_PATH_STROKE_JOIN_MITER (for mitered joins), |

639 | * ART_PATH_STROKE_JOIN_ROUND (for round joins), and |

640 | * ART_PATH_STROKE_JOIN_BEVEL (for bevelled joins). The mitered join |

641 | * is converted to a bevelled join if the miter would extend to a |

642 | * distance of more than @miter_limit * @line_width from the actual |

643 | * join point. |

644 | * |

645 | * If there are open subpaths, the ends of these subpaths are capped |

646 | * according to the @cap rule. Possible values are |

647 | * ART_PATH_STROKE_CAP_BUTT (squared cap, extends exactly to end |

648 | * point), ART_PATH_STROKE_CAP_ROUND (rounded half-circle centered at |

649 | * the end point), and ART_PATH_STROKE_CAP_SQUARE (squared cap, |

650 | * extending half @line_width past the end point). |

651 | * |

652 | * The @flatness parameter controls the accuracy of the rendering. It |

653 | * is most important for determining the number of points to use to |

654 | * approximate circular arcs for round lines and joins. In general, the |

655 | * resulting vector path will be within @flatness pixels of the "ideal" |

656 | * path containing actual circular arcs. I reserve the right to use |

657 | * the @flatness parameter to convert bevelled joins to miters for very |

658 | * small turn angles, as this would reduce the number of points in the |

659 | * resulting outline path. |

660 | * |

661 | * The resulting path is "clean" with respect to self-intersections, i.e. |

662 | * the winding number is 0 or 1 at each point. |

663 | * |

664 | * Return value: Resulting stroked outline in svp format. |

665 | **/ |

666 | ArtSVP * |

667 | art_svp_vpath_stroke (ArtVpath *vpath, |

668 | ArtPathStrokeJoinType join, |

669 | ArtPathStrokeCapType cap, |

670 | double line_width, |

671 | double miter_limit, |

672 | double flatness) |

673 | { |

674 | #ifdef ART_USE_NEW_INTERSECTOR |

675 | ArtVpath *vpath_stroke; |

676 | ArtSVP *svp, *svp2; |

677 | ArtSvpWriter *swr; |

678 | |

679 | vpath_stroke = art_svp_vpath_stroke_raw (vpath, join, cap, |

680 | line_width, miter_limit, flatness); |

681 | #ifdef VERBOSE |

682 | print_ps_vpath (vpath_stroke); |

683 | #endif |

684 | svp = art_svp_from_vpath (vpath_stroke); |

685 | #ifdef VERBOSE |

686 | print_ps_svp (svp); |

687 | #endif |

688 | art_free (vpath_stroke); |

689 | |

690 | swr = art_svp_writer_rewind_new (ART_WIND_RULE_NONZERO); |

691 | art_svp_intersector (svp, swr); |

692 | |

693 | svp2 = art_svp_writer_rewind_reap (swr); |

694 | #ifdef VERBOSE |

695 | print_ps_svp (svp2); |

696 | #endif |

697 | art_svp_free (svp); |

698 | return svp2; |

699 | #else |

700 | ArtVpath *vpath_stroke, *vpath2; |

701 | ArtSVP *svp, *svp2, *svp3; |

702 | |

703 | vpath_stroke = art_svp_vpath_stroke_raw (vpath, join, cap, |

704 | line_width, miter_limit, flatness); |

705 | #ifdef VERBOSE |

706 | print_ps_vpath (vpath_stroke); |

707 | #endif |

708 | vpath2 = art_vpath_perturb (vpath_stroke); |

709 | #ifdef VERBOSE |

710 | print_ps_vpath (vpath2); |

711 | #endif |

712 | art_free (vpath_stroke); |

713 | svp = art_svp_from_vpath (vpath2); |

714 | #ifdef VERBOSE |

715 | print_ps_svp (svp); |

716 | #endif |

717 | art_free (vpath2); |

718 | svp2 = art_svp_uncross (svp); |

719 | #ifdef VERBOSE |

720 | print_ps_svp (svp2); |

721 | #endif |

722 | art_svp_free (svp); |

723 | svp3 = art_svp_rewind_uncrossed (svp2, ART_WIND_RULE_NONZERO); |

724 | #ifdef VERBOSE |

725 | print_ps_svp (svp3); |

726 | #endif |

727 | art_svp_free (svp2); |

728 | |

729 | return svp3; |

730 | #endif |

731 | } |

**Note:**See TracBrowser for help on using the repository browser.