Skip to main content

piet_test/
picture_0.rs

1//! A wide assortment of graphics meant to show off many different uses of piet
2
3use piet::kurbo::{Affine, BezPath, Line, Point, Rect, RoundedRect, Vec2};
4
5use piet::{
6    Color, Error, FontBuilder, ImageFormat, InterpolationMode, RenderContext, Text, TextLayout,
7    TextLayoutBuilder,
8};
9
10pub fn draw(rc: &mut impl RenderContext) -> Result<(), Error> {
11    rc.clear(Color::WHITE);
12    let brush = rc.solid_brush(Color::rgb8(0x00, 0x00, 0x80));
13    rc.stroke(Line::new((10.0, 10.0), (100.0, 50.0)), &brush, 1.0);
14
15    let mut path = BezPath::new();
16    path.move_to((50.0, 10.0));
17    path.quad_to((60.0, 50.0), (100.0, 90.0));
18    let brush = rc.solid_brush(Color::rgb8(0x00, 0x80, 0x00));
19    rc.stroke(path, &brush, 1.0);
20
21    let mut path = BezPath::new();
22    path.move_to((10.0, 20.0));
23    path.curve_to((10.0, 80.0), (100.0, 80.0), (100.0, 60.0));
24    let brush = rc.solid_brush(Color::rgba8(0x00, 0x00, 0x80, 0xC0));
25    rc.fill(path, &brush);
26
27    rc.stroke(RoundedRect::new(145.0, 45.0, 185.0, 85.0, 5.0), &brush, 1.0);
28
29    let font = rc.text().new_font_by_name("Segoe UI", 12.0).build()?;
30    let layout = rc
31        .text()
32        .new_text_layout(&font, "Hello piet!", std::f64::INFINITY)
33        .build()?;
34    let w: f64 = layout.width();
35    let brush = rc.solid_brush(Color::rgba8(0x80, 0x00, 0x00, 0xC0));
36    rc.draw_text(&layout, (80.0, 10.0), &brush);
37
38    rc.stroke(Line::new((80.0, 12.0), (80.0 + w, 12.0)), &brush, 1.0);
39
40    rc.with_save(|rc| {
41        rc.transform(Affine::rotate(0.1));
42        rc.draw_text(&layout, (80.0, 10.0), &brush);
43        Ok(())
44    })?;
45
46    rc.blurred_rect(Rect::new(155.0, 55.0, 185.0, 85.0), 5.0, &Color::BLACK);
47
48    let image_data = make_image_data(256, 256);
49    let image = rc.make_image(256, 256, &image_data, ImageFormat::RgbaSeparate)?;
50    rc.draw_image(
51        &image,
52        Rect::new(150.0, 50.0, 180.0, 80.0),
53        InterpolationMode::Bilinear,
54    );
55
56    // 3x3 px red image with a single blue pixel in the middle
57    #[rustfmt::skip]
58    let blue_dot_data = [
59        255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
60        255, 0, 0, 255, 0, 0, 255, 255, 255, 0, 0, 255,
61        255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
62    ];
63    let blue_dot_image = rc.make_image(3, 3, &blue_dot_data, ImageFormat::RgbaPremul)?;
64    // Draw using only the single blue pixel
65    rc.draw_image_area(
66        &blue_dot_image,
67        Rect::new(1.0, 1.0, 2.0, 2.0),
68        Rect::new(160.0, 20.0, 170.0, 30.0),
69        InterpolationMode::NearestNeighbor,
70    );
71
72    let clip_path = star(Point::new(90.0, 45.0), 10.0, 30.0, 24);
73    rc.clip(clip_path);
74    let layout = rc
75        .text()
76        .new_text_layout(&font, "Clipped text", std::f64::INFINITY)
77        .build()?;
78    rc.draw_text(&layout, (80.0, 50.0), &brush);
79
80    Ok(())
81}
82
83// Note: this could be a Shape.
84fn star(center: Point, inner: f64, outer: f64, n: usize) -> BezPath {
85    let mut result = BezPath::new();
86    let d_th = std::f64::consts::PI / (n as f64);
87    for i in 0..n {
88        let outer_pt = center + outer * Vec2::from_angle(d_th * ((i * 2) as f64));
89        if i == 0 {
90            result.move_to(outer_pt);
91        } else {
92            result.line_to(outer_pt);
93        }
94        result.line_to(center + inner * Vec2::from_angle(d_th * ((i * 2 + 1) as f64)));
95    }
96    result.close_path();
97    result
98}
99
100// allows for nice vertical formatting for `result[ix + 0]`
101#[allow(clippy::identity_op)]
102fn make_image_data(width: usize, height: usize) -> Vec<u8> {
103    let mut result = vec![0; width * height * 4];
104    for y in 0..height {
105        for x in 0..width {
106            let ix = (y * width + x) * 4;
107            result[ix + 0] = x as u8;
108            result[ix + 1] = y as u8;
109            result[ix + 2] = !(x as u8);
110            result[ix + 3] = 127;
111        }
112    }
113    result
114}