1
/* cairo - a vector graphics library with display and print output
3
* Copyright © 2002 University of Southern California
5
* This library is free software; you can redistribute it and/or
6
* modify it either under the terms of the GNU Lesser General Public
7
* License version 2.1 as published by the Free Software Foundation
8
* (the "LGPL") or, at your option, under the terms of the Mozilla
9
* Public License Version 1.1 (the "MPL"). If you do not alter this
10
* notice, a recipient may use your version of this file under either
11
* the MPL or the LGPL.
13
* You should have received a copy of the LGPL along with this library
14
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
* You should have received a copy of the MPL along with this library
17
* in the file COPYING-MPL-1.1
19
* The contents of this file are subject to the Mozilla Public License
20
* Version 1.1 (the "License"); you may not use this file except in
21
* compliance with the License. You may obtain a copy of the License at
22
* http://www.mozilla.org/MPL/
24
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
26
* the specific language governing rights and limitations.
28
* The Original Code is the cairo graphics library.
30
* The Initial Developer of the Original Code is University of Southern
34
* Carl D. Worth <cworth@cworth.org>
40
_cairo_pen_vertices_needed (double tolerance, double radius, cairo_matrix_t *matrix);
43
_cairo_pen_compute_slopes (cairo_pen_t *pen);
46
_cairo_pen_stroke_spline_half (cairo_pen_t *pen, cairo_spline_t *spline, cairo_direction_t dir, cairo_polygon_t *polygon);
49
_cairo_pen_init_empty (cairo_pen_t *pen)
54
pen->num_vertices = 0;
58
_cairo_pen_init (cairo_pen_t *pen,
68
pen->tolerance = tolerance;
70
_cairo_matrix_compute_determinant (ctm, &det);
77
pen->num_vertices = _cairo_pen_vertices_needed (tolerance,
81
pen->vertices = _cairo_malloc_ab (pen->num_vertices,
82
sizeof (cairo_pen_vertex_t));
83
if (pen->vertices == NULL)
84
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
87
* Compute pen coordinates. To generate the right ellipse, compute points around
88
* a circle in user space and transform them to device space. To get a consistent
89
* orientation in device space, flip the pen if the transformation matrix
92
for (i=0; i < pen->num_vertices; i++) {
93
double theta = 2 * M_PI * i / (double) pen->num_vertices;
94
double dx = radius * cos (reflect ? -theta : theta);
95
double dy = radius * sin (reflect ? -theta : theta);
96
cairo_pen_vertex_t *v = &pen->vertices[i];
97
cairo_matrix_transform_distance (ctm, &dx, &dy);
98
v->point.x = _cairo_fixed_from_double (dx);
99
v->point.y = _cairo_fixed_from_double (dy);
102
_cairo_pen_compute_slopes (pen);
104
return CAIRO_STATUS_SUCCESS;
108
_cairo_pen_fini (cairo_pen_t *pen)
110
free (pen->vertices);
111
pen->vertices = NULL;
113
_cairo_pen_init_empty (pen);
117
_cairo_pen_init_copy (cairo_pen_t *pen, cairo_pen_t *other)
121
if (pen->num_vertices) {
122
pen->vertices = _cairo_malloc_ab (pen->num_vertices,
123
sizeof (cairo_pen_vertex_t));
124
if (pen->vertices == NULL)
125
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
127
memcpy (pen->vertices, other->vertices, pen->num_vertices * sizeof (cairo_pen_vertex_t));
130
return CAIRO_STATUS_SUCCESS;
134
_cairo_pen_add_points (cairo_pen_t *pen, cairo_point_t *point, int num_points)
136
cairo_pen_vertex_t *vertices;
137
cairo_status_t status;
141
num_vertices = pen->num_vertices + num_points;
142
vertices = _cairo_realloc_ab (pen->vertices,
143
num_vertices, sizeof (cairo_pen_vertex_t));
144
if (vertices == NULL)
145
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
147
pen->vertices = vertices;
148
pen->num_vertices = num_vertices;
150
/* initialize new vertices */
151
for (i=0; i < num_points; i++)
152
pen->vertices[pen->num_vertices-num_points+i].point = point[i];
154
status = _cairo_hull_compute (pen->vertices, &pen->num_vertices);
158
_cairo_pen_compute_slopes (pen);
160
return CAIRO_STATUS_SUCCESS;
164
The circular pen in user space is transformed into an ellipse in
167
We construct the pen by computing points along the circumference
168
using equally spaced angles.
170
We show that this approximation to the ellipse has maximum error at the
171
major axis of the ellipse.
175
M = major axis length
176
m = minor axis length
178
Align 'M' along the X axis and 'm' along the Y axis and draw
179
an ellipse parameterized by angle 't':
181
x = M cos t y = m sin t
183
Perturb t by ± d and compute two new points (x+,y+), (x-,y-).
184
The distance from the average of these two points to (x,y) represents
185
the maximum error in approximating the ellipse with a polygon formed
186
from vertices 2∆ radians apart.
188
x+ = M cos (t+∆) y+ = m sin (t+∆)
189
x- = M cos (t-∆) y- = m sin (t-∆)
191
Now compute the approximation error, E:
193
Ex = (x - (x+ + x-) / 2)
194
Ex = (M cos(t) - (Mcos(t+∆) + Mcos(t-∆))/2)
195
= M (cos(t) - (cos(t)cos(∆) + sin(t)sin(∆) +
196
cos(t)cos(∆) - sin(t)sin(∆))/2)
197
= M(cos(t) - cos(t)cos(∆))
198
= M cos(t) (1 - cos(∆))
200
Ey = y - (y+ - y-) / 2
201
= m sin (t) - (m sin(t+∆) + m sin(t-∆)) / 2
202
= m (sin(t) - (sin(t)cos(∆) + cos(t)sin(∆) +
203
sin(t)cos(∆) - cos(t)sin(∆))/2)
204
= m (sin(t) - sin(t)cos(∆))
205
= m sin(t) (1 - cos(∆))
208
= (M cos(t) (1 - cos (∆)))² + (m sin(t) (1-cos(∆)))²
209
= (1 - cos(∆))² (M² cos²(t) + m² sin²(t))
210
= (1 - cos(∆))² ((m² + M² - m²) cos² (t) + m² sin²(t))
211
= (1 - cos(∆))² (M² - m²) cos² (t) + (1 - cos(∆))² m²
213
Find the extremum by differentiation wrt t and setting that to zero
215
∂(E²)/∂(t) = (1-cos(∆))² (M² - m²) (-2 cos(t) sin(t))
217
0 = 2 cos (t) sin (t)
221
Which is to say that the maximum and minimum errors occur on the
222
axes of the ellipse at 0 and π radians:
224
E²(0) = (1-cos(∆))² (M² - m²) + (1-cos(∆))² m²
226
E²(π) = (1-cos(∆))² m²
228
maximum error = M (1-cos(∆))
229
minimum error = m (1-cos(∆))
231
We must make maximum error ≤ tolerance, so compute the ∆ needed:
233
tolerance = M (1-cos(∆))
234
tolerance / M = 1 - cos (∆)
235
cos(∆) = 1 - tolerance/M
236
∆ = acos (1 - tolerance / M);
238
Remembering that ∆ is half of our angle between vertices,
239
the number of vertices is then
241
vertices = ceil(2π/2∆).
244
Note that this also equation works for M == m (a circle) as it
245
doesn't matter where on the circle the error is computed.
249
_cairo_pen_vertices_needed (double tolerance,
251
cairo_matrix_t *matrix)
254
* the pen is a circle that gets transformed to an ellipse by matrix.
255
* compute major axis length for a pen with the specified radius.
256
* we don't need the minor axis length.
259
double major_axis = _cairo_matrix_transformed_circle_major_axis(matrix, radius);
262
* compute number of vertices needed
266
/* Where tolerance / M is > 1, we use 4 points */
267
if (tolerance >= major_axis) {
270
double delta = acos (1 - tolerance / major_axis);
271
num_vertices = ceil (M_PI / delta);
273
/* number of vertices must be even */
274
if (num_vertices % 2)
277
/* And we must always have at least 4 vertices. */
278
if (num_vertices < 4)
286
_cairo_pen_compute_slopes (cairo_pen_t *pen)
289
cairo_pen_vertex_t *prev, *v, *next;
291
for (i=0, i_prev = pen->num_vertices - 1;
292
i < pen->num_vertices;
294
prev = &pen->vertices[i_prev];
295
v = &pen->vertices[i];
296
next = &pen->vertices[(i + 1) % pen->num_vertices];
298
_cairo_slope_init (&v->slope_cw, &prev->point, &v->point);
299
_cairo_slope_init (&v->slope_ccw, &v->point, &next->point);
303
* Find active pen vertex for clockwise edge of stroke at the given slope.
305
* NOTE: The behavior of this function is sensitive to the sense of
306
* the inequality within _cairo_slope_clockwise/_cairo_slope_counter_clockwise.
308
* The issue is that the slope_ccw member of one pen vertex will be
309
* equivalent to the slope_cw member of the next pen vertex in a
310
* counterclockwise order. However, for this function, we care
311
* strongly about which vertex is returned.
314
_cairo_pen_find_active_cw_vertex_index (cairo_pen_t *pen,
315
cairo_slope_t *slope,
320
for (i=0; i < pen->num_vertices; i++) {
321
if (_cairo_slope_clockwise (slope, &pen->vertices[i].slope_ccw)
322
&& _cairo_slope_counter_clockwise (slope, &pen->vertices[i].slope_cw))
326
/* If the desired slope cannot be found between any of the pen
327
* vertices, then we must have a degenerate pen, (such as a pen
328
* that's been transformed to a line). In that case, we consider
329
* the first pen vertex as the appropriate clockwise vertex.
331
if (i == pen->num_vertices)
337
/* Find active pen vertex for counterclockwise edge of stroke at the given slope.
339
* NOTE: The behavior of this function is sensitive to the sense of
340
* the inequality within _cairo_slope_clockwise/_cairo_slope_counter_clockwise.
343
_cairo_pen_find_active_ccw_vertex_index (cairo_pen_t *pen,
344
cairo_slope_t *slope,
348
cairo_slope_t slope_reverse;
350
slope_reverse = *slope;
351
slope_reverse.dx = -slope_reverse.dx;
352
slope_reverse.dy = -slope_reverse.dy;
354
for (i=pen->num_vertices-1; i >= 0; i--) {
355
if (_cairo_slope_counter_clockwise (&pen->vertices[i].slope_ccw, &slope_reverse)
356
&& _cairo_slope_clockwise (&pen->vertices[i].slope_cw, &slope_reverse))
360
/* If the desired slope cannot be found between any of the pen
361
* vertices, then we must have a degenerate pen, (such as a pen
362
* that's been transformed to a line). In that case, we consider
363
* the last pen vertex as the appropriate counterclockwise vertex.
366
i = pen->num_vertices - 1;
372
_cairo_pen_stroke_spline_half (cairo_pen_t *pen,
373
cairo_spline_t *spline,
374
cairo_direction_t dir,
375
cairo_polygon_t *polygon)
378
int start, stop, step;
380
cairo_point_t hull_point;
381
cairo_slope_t slope, initial_slope, final_slope;
382
cairo_point_t *point = spline->points;
383
int num_points = spline->num_points;
385
if (dir == CAIRO_DIRECTION_FORWARD) {
389
initial_slope = spline->initial_slope;
390
final_slope = spline->final_slope;
392
start = num_points - 1;
395
initial_slope = spline->final_slope;
396
initial_slope.dx = -initial_slope.dx;
397
initial_slope.dy = -initial_slope.dy;
398
final_slope = spline->initial_slope;
399
final_slope.dx = -final_slope.dx;
400
final_slope.dy = -final_slope.dy;
403
_cairo_pen_find_active_cw_vertex_index (pen,
409
hull_point.x = point[i].x + pen->vertices[active].point.x;
410
hull_point.y = point[i].y + pen->vertices[active].point.y;
412
_cairo_polygon_line_to (polygon, &hull_point);
414
if (i + step == stop)
417
_cairo_slope_init (&slope, &point[i], &point[i+step]);
418
if (_cairo_slope_counter_clockwise (&slope, &pen->vertices[active].slope_ccw)) {
419
if (++active == pen->num_vertices)
421
} else if (_cairo_slope_clockwise (&slope, &pen->vertices[active].slope_cw)) {
423
active = pen->num_vertices - 1;
430
/* Compute outline of a given spline using the pen.
431
The trapezoids needed to fill that outline will be added to traps
434
_cairo_pen_stroke_spline (cairo_pen_t *pen,
435
cairo_spline_t *spline,
437
cairo_traps_t *traps)
439
cairo_status_t status;
440
cairo_polygon_t polygon;
442
/* If the line width is so small that the pen is reduced to a
443
single point, then we have nothing to do. */
444
if (pen->num_vertices <= 1)
445
return CAIRO_STATUS_SUCCESS;
447
_cairo_polygon_init (&polygon);
449
status = _cairo_spline_decompose (spline, tolerance);
453
_cairo_pen_stroke_spline_half (pen, spline, CAIRO_DIRECTION_FORWARD, &polygon);
455
_cairo_pen_stroke_spline_half (pen, spline, CAIRO_DIRECTION_REVERSE, &polygon);
457
_cairo_polygon_close (&polygon);
458
status = _cairo_polygon_status (&polygon);
462
status = _cairo_bentley_ottmann_tessellate_polygon (traps, &polygon, CAIRO_FILL_RULE_WINDING);
464
_cairo_polygon_fini (&polygon);