Skip to content

Commit 743277d

Browse files
committed
Remove duplicated setup code from Context examples
1 parent 6fe1182 commit 743277d

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

cairo/src/context.rs

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,33 @@ impl fmt::Debug for RectangleList {
6060
/// ```
6161
/// use cairo::{Context, Format, ImageSurface};
6262
///
63-
/// let surface = ImageSurface::create(Format::ARgb32, 100, 100).unwrap();
64-
/// let ctx = Context::new(&surface).unwrap();
63+
/// fn main() -> Result<(), cairo::Error> {
64+
/// let surface = ImageSurface::create(Format::ARgb32, 100, 100)?;
65+
/// let ctx = Context::new(&surface)?;
6566
///
66-
/// // paint the background black
67-
/// ctx.set_source_rgb(0.0, 0.0, 0.0);
68-
/// ctx.paint();
67+
/// // paint the background black
68+
/// ctx.set_source_rgb(0.0, 0.0, 0.0);
69+
/// ctx.paint();
6970
///
70-
/// // etc.
71+
/// // etc., other drawing operations
72+
/// Ok(())
73+
/// }
74+
/// ```
75+
///
76+
/// Each example in the documentation below can be used with the above setup. To view the images
77+
/// generated by the examples, you can render the surface to a file. For example, to render the
78+
/// surface to a PNG file (requires the `png` feature to be enabled), you can use the following
79+
/// code after drawing on the surface:
80+
///
81+
/// ```
82+
/// # fn main() -> Result<(), cairo::Error> {
83+
/// # use cairo::{Context, Format, ImageSurface};
84+
/// # let surface = ImageSurface::create(Format::ARgb32, 100, 100)?;
85+
/// # let ctx = Context::new(&surface)?;
86+
/// let mut file = std::fs::File::create("example.png")?;
87+
/// surface.write_to_png(&mut file)?;
88+
/// # Ok(())
89+
/// # }
7190
/// ```
7291
///
7392
/// [`ImageSurface`]: crate::ImageSurface
@@ -205,11 +224,9 @@ impl Context {
205224
///
206225
/// ```
207226
/// # fn main() -> Result<(), cairo::Error> {
208-
/// use cairo::{Context, Format, ImageSurface};
209-
///
210-
/// let surface = ImageSurface::create(Format::ARgb32, 100, 100).unwrap();
211-
/// let ctx = Context::new(&surface).unwrap();
212-
///
227+
/// # use cairo::{Context, Format, ImageSurface};
228+
/// # let surface = ImageSurface::create(Format::ARgb32, 100, 100)?;
229+
/// # let ctx = Context::new(&surface)?;
213230
/// // set source to red
214231
/// ctx.set_source_rgb(1.0, 0.0, 0.0);
215232
///
@@ -349,11 +366,9 @@ impl Context {
349366
///
350367
/// ```
351368
/// # fn main() -> Result<(), cairo::Error> {
352-
/// use cairo::{Context, Format, ImageSurface};
353-
///
354-
/// let surface = ImageSurface::create(Format::ARgb32, 100, 100).unwrap();
355-
/// let ctx = Context::new(&surface).unwrap();
356-
///
369+
/// # use cairo::{Context, Format, ImageSurface};
370+
/// # let surface = ImageSurface::create(Format::ARgb32, 100, 100)?;
371+
/// # let ctx = Context::new(&surface)?;
357372
/// // paint the background black
358373
/// ctx.paint()?;
359374
///
@@ -381,11 +396,9 @@ impl Context {
381396
///
382397
/// ```
383398
/// # fn main() -> Result<(), cairo::Error> {
384-
/// use cairo::{Context, Format, ImageSurface};
385-
///
386-
/// let surface = ImageSurface::create(Format::ARgb32, 100, 100).unwrap();
387-
/// let ctx = Context::new(&surface).unwrap();
388-
///
399+
/// # use cairo::{Context, Format, ImageSurface};
400+
/// # let surface = ImageSurface::create(Format::ARgb32, 100, 100)?;
401+
/// # let ctx = Context::new(&surface)?;
389402
/// // paint the background black
390403
/// ctx.paint()?;
391404
///
@@ -419,12 +432,10 @@ impl Context {
419432
///
420433
/// ```
421434
/// # fn main() -> Result<(), cairo::Error> {
422-
/// use cairo::{Context, Format, ImageSurface, RadialGradient};
423-
///
424-
/// let surface = ImageSurface::create(Format::ARgb32, 100, 100).unwrap();
425-
/// let ctx = Context::new(&surface).unwrap();
426-
///
427-
/// // creates a radial gradient from red to blue
435+
/// # use cairo::{Context, Format, ImageSurface};
436+
/// # let surface = ImageSurface::create(Format::ARgb32, 100, 100)?;
437+
/// # let ctx = Context::new(&surface)?;
438+
/// // create a radial gradient from red to blue
428439
///
429440
/// // gradient will begin at (50, 50) with radius 0
430441
/// // ends at (50, 50) with radius 50
@@ -436,7 +447,7 @@ impl Context {
436447
/// // second color stop: at offset=1, draw with blue=(0, 0, 1)
437448
/// pattern.add_color_stop_rgb(1.0, 0.0, 0.0, 1.0);
438449
///
439-
/// // paints the canvas with the gradient
450+
/// // paint the canvas with the gradient
440451
/// ctx.set_source(&pattern)?;
441452
/// ctx.paint()?;
442453
/// # Ok(())
@@ -1086,11 +1097,9 @@ impl Context {
10861097
///
10871098
/// ```
10881099
/// # fn main() -> Result<(), cairo::Error> {
1089-
/// use cairo::{Context, Format, ImageSurface};
1090-
///
1091-
/// let surface = ImageSurface::create(Format::ARgb32, 100, 100).unwrap();
1092-
/// let ctx = Context::new(&surface).unwrap();
1093-
///
1100+
/// # use cairo::{Context, Format, ImageSurface};
1101+
/// # let surface = ImageSurface::create(Format::ARgb32, 100, 100)?;
1102+
/// # let ctx = Context::new(&surface)?;
10941103
/// // paint the background black
10951104
/// ctx.paint()?;
10961105
///
@@ -1159,11 +1168,9 @@ impl Context {
11591168
///
11601169
/// ```
11611170
/// # 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-
///
1171+
/// # use cairo::{Context, Format, ImageSurface};
1172+
/// # let surface = ImageSurface::create(Format::ARgb32, 100, 100)?;
1173+
/// # let ctx = Context::new(&surface)?;
11671174
/// // paint the background black
11681175
/// ctx.paint()?;
11691176
///

cairo/src/paths.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ use crate::{ffi, PathDataType};
1919
///
2020
/// ```
2121
/// # fn main() -> Result<(), cairo::Error> {
22-
/// use cairo::{Context, Format, ImageSurface};
23-
///
24-
/// let surface = ImageSurface::create(Format::ARgb32, 100, 100).unwrap();
25-
/// let ctx = Context::new(&surface).unwrap();
26-
///
22+
/// # use cairo::{Context, Format, ImageSurface};
23+
/// # let surface = ImageSurface::create(Format::ARgb32, 100, 100)?;
24+
/// # let ctx = Context::new(&surface)?;
2725
/// // paint the background black
2826
/// ctx.paint()?;
2927
///

0 commit comments

Comments
 (0)