Skip to content

Commit 6fe1182

Browse files
committed
Add documentation for Path functions, example for curve_to
1 parent a3a0040 commit 6fe1182

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

cairo/src/context.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,8 @@ impl Context {
10761076
/// If there is no current point before the call to [`Context::close_path`], this function will
10771077
/// have no effect.
10781078
///
1079+
/// This function maps to [`PathSegment::ClosePath`].
1080+
///
10791081
/// **Note**: Calls to [`Context::close_path`] now (as of `cairo 1.2.4`) place an explicit
10801082
/// [`MoveTo`] element into the path immediately after the [`ClosePath`] element. This can
10811083
/// simplify path processing in some cases. See below for an example of this case.
@@ -1126,6 +1128,7 @@ impl Context {
11261128
/// # }
11271129
/// ```
11281130
///
1131+
/// [`PathSegment::ClosePath`]: crate::PathSegment::ClosePath
11291132
/// [`MoveTo`]: crate::PathSegment::MoveTo
11301133
/// [`ClosePath`]: crate::PathSegment::ClosePath
11311134
#[doc(alias = "cairo_close_path")]
@@ -1143,16 +1146,104 @@ impl Context {
11431146
unsafe { ffi::cairo_arc_negative(self.0.as_ptr(), xc, yc, radius, angle1, angle2) }
11441147
}
11451148

1149+
/// Adds a cubic Bézier spline from the current point to position `(x3, y3)` in user-space
1150+
/// coordinates to the path, using `(x1, y1)` and `(x2, y2)` as the control points. After this
1151+
/// call, the current point will be `(x3, y3)`.
1152+
///
1153+
/// If there is no current point before the call to [`Context::curve_to`], this function will
1154+
/// behave as if preceded by a call to [`Context::move_to(&ctx, x1, y1)`].
1155+
///
1156+
/// This function maps to [`PathSegment::CurveTo`].
1157+
///
1158+
/// # Examples
1159+
///
1160+
/// ```
1161+
/// # fn main() -> Result<(), cairo::Error> {
1162+
/// use cairo::{Context, Format, ImageSurface};
1163+
///
1164+
/// let surface = ImageSurface::create(Format::ARgb32, 100, 100).unwrap();
1165+
/// let ctx = Context::new(&surface).unwrap();
1166+
///
1167+
/// // paint the background black
1168+
/// ctx.paint()?;
1169+
///
1170+
/// // draw an infinity symbol with a white stroke like how a human would
1171+
/// ctx.set_source_rgb(1.0, 1.0, 1.0);
1172+
/// ctx.move_to(20.0, 50.0);
1173+
/// ctx.curve_to( // left loop, going up
1174+
/// 20.0, 33.0,
1175+
/// 40.0, 33.0,
1176+
/// 50.0, 50.0,
1177+
/// );
1178+
/// ctx.curve_to( // right loop, going down
1179+
/// 60.0, 67.0,
1180+
/// 80.0, 67.0,
1181+
/// 80.0, 50.0,
1182+
/// );
1183+
/// ctx.curve_to( // right loop, going up
1184+
/// 80.0, 33.0,
1185+
/// 60.0, 33.0,
1186+
/// 50.0, 50.0,
1187+
/// );
1188+
/// ctx.curve_to( // left loop, going down
1189+
/// 40.0, 67.0,
1190+
/// 20.0, 67.0,
1191+
/// 20.0, 50.0,
1192+
/// );
1193+
///
1194+
/// // print the path details before stroking (will clear the path)
1195+
/// let path = ctx.copy_path()?;
1196+
/// for element in path.iter() {
1197+
/// println!("{:?}", element);
1198+
/// }
1199+
///
1200+
/// ctx.stroke()?;
1201+
///
1202+
/// // output:
1203+
/// // MoveTo((20.0, 50.0))
1204+
/// // CurveTo((20.0, 33.0), (40.0, 33.0), (50.0, 50.0))
1205+
/// // CurveTo((60.0, 67.0), (80.0, 67.0), (80.0, 50.0))
1206+
/// // CurveTo((80.0, 33.0), (60.0, 33.0), (50.0, 50.0))
1207+
/// // CurveTo((40.0, 67.0), (20.0, 67.0), (20.0, 50.0))
1208+
///
1209+
/// # Ok(())
1210+
/// # }
1211+
/// ```
1212+
///
1213+
/// [`Context::move_to(&ctx, x1, y1)`]: crate::Context::move_to
1214+
/// [`PathSegment::CurveTo`]: crate::PathSegment::CurveTo
11461215
#[doc(alias = "cairo_curve_to")]
11471216
pub fn curve_to(&self, x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64) {
11481217
unsafe { ffi::cairo_curve_to(self.0.as_ptr(), x1, y1, x2, y2, x3, y3) }
11491218
}
11501219

1220+
/// Adds a line segment from the current point to the given point `(x, y)`, to the path. After
1221+
/// this call, the current point will be `(x, y)`.
1222+
///
1223+
/// If there is no current point before the call to [`Context::line_to`], this function will
1224+
/// behave as [`Context::move_to`].
1225+
///
1226+
/// This function maps to [`PathSegment::LineTo`].
1227+
///
1228+
/// # Examples
1229+
///
1230+
/// See [`Context::close_path`] for an example of using [`Context::line_to`].
1231+
///
1232+
/// [`PathSegment::LineTo`]: crate::PathSegment::LineTo
11511233
#[doc(alias = "cairo_line_to")]
11521234
pub fn line_to(&self, x: f64, y: f64) {
11531235
unsafe { ffi::cairo_line_to(self.0.as_ptr(), x, y) }
11541236
}
11551237

1238+
/// Begins a new sub-path. After this call, the current point will be `(x, y)`.
1239+
///
1240+
/// This function maps to [`PathSegment::MoveTo`].
1241+
///
1242+
/// # Examples
1243+
///
1244+
/// See [`Context::close_path`] for an example of using [`Context::move_to`].
1245+
///
1246+
/// [`PathSegment::MoveTo`]: crate::PathSegment::MoveTo
11561247
#[doc(alias = "cairo_move_to")]
11571248
pub fn move_to(&self, x: f64, y: f64) {
11581249
unsafe { ffi::cairo_move_to(self.0.as_ptr(), x, y) }

cairo/src/paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub enum PathSegment {
122122
/// Draw a straight line from the current point to the given point.
123123
LineTo((f64, f64)),
124124

125-
/// Draw a cubic Bezier curve from the current point to the given point, using the two control
125+
/// Draw a cubic Bézier curve from the current point to the given point, using the two control
126126
/// points to define the curve.
127127
///
128128
/// The first and second points are the control points, and the third point is the end point.

0 commit comments

Comments
 (0)