iced_graphics/geometry/frame.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
//! Draw and generate geometry.
use crate::core::{Point, Radians, Rectangle, Size, Vector};
use crate::geometry::{self, Fill, Image, Path, Stroke, Svg, Text};
/// The region of a surface that can be used to draw geometry.
#[allow(missing_debug_implementations)]
pub struct Frame<Renderer>
where
Renderer: geometry::Renderer,
{
raw: Renderer::Frame,
}
impl<Renderer> Frame<Renderer>
where
Renderer: geometry::Renderer,
{
/// Creates a new [`Frame`] with the given dimensions.
pub fn new(renderer: &Renderer, size: Size) -> Self {
Self {
raw: renderer.new_frame(size),
}
}
/// Returns the width of the [`Frame`].
pub fn width(&self) -> f32 {
self.raw.width()
}
/// Returns the height of the [`Frame`].
pub fn height(&self) -> f32 {
self.raw.height()
}
/// Returns the dimensions of the [`Frame`].
pub fn size(&self) -> Size {
self.raw.size()
}
/// Returns the coordinate of the center of the [`Frame`].
pub fn center(&self) -> Point {
self.raw.center()
}
/// Draws the given [`Path`] on the [`Frame`] by filling it with the
/// provided style.
pub fn fill(&mut self, path: &Path, fill: impl Into<Fill>) {
self.raw.fill(path, fill);
}
/// Draws an axis-aligned rectangle given its top-left corner coordinate and
/// its `Size` on the [`Frame`] by filling it with the provided style.
pub fn fill_rectangle(
&mut self,
top_left: Point,
size: Size,
fill: impl Into<Fill>,
) {
self.raw.fill_rectangle(top_left, size, fill);
}
/// Draws the stroke of the given [`Path`] on the [`Frame`] with the
/// provided style.
pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>) {
self.raw.stroke(path, stroke);
}
/// Draws the stroke of an axis-aligned rectangle with the provided style
/// given its top-left corner coordinate and its `Size` on the [`Frame`] .
pub fn stroke_rectangle<'a>(
&mut self,
top_left: Point,
size: Size,
stroke: impl Into<Stroke<'a>>,
) {
self.raw.stroke_rectangle(top_left, size, stroke);
}
/// Draws the characters of the given [`Text`] on the [`Frame`], filling
/// them with the given color.
///
/// __Warning:__ All text will be rendered on top of all the layers of
/// a `Canvas`. Therefore, it is currently only meant to be used for
/// overlays, which is the most common use case.
pub fn fill_text(&mut self, text: impl Into<Text>) {
self.raw.fill_text(text);
}
/// Draws the given [`Image`] on the [`Frame`] inside the given bounds.
#[cfg(feature = "image")]
pub fn draw_image(&mut self, bounds: Rectangle, image: impl Into<Image>) {
self.raw.draw_image(bounds, image);
}
/// Draws the given [`Svg`] on the [`Frame`] inside the given bounds.
#[cfg(feature = "svg")]
pub fn draw_svg(&mut self, bounds: Rectangle, svg: impl Into<Svg>) {
self.raw.draw_svg(bounds, svg);
}
/// Stores the current transform of the [`Frame`] and executes the given
/// drawing operations, restoring the transform afterwards.
///
/// This method is useful to compose transforms and perform drawing
/// operations in different coordinate systems.
#[inline]
pub fn with_save<R>(&mut self, f: impl FnOnce(&mut Self) -> R) -> R {
self.push_transform();
let result = f(self);
self.pop_transform();
result
}
/// Pushes the current transform in the transform stack.
pub fn push_transform(&mut self) {
self.raw.push_transform();
}
/// Pops a transform from the transform stack and sets it as the current transform.
pub fn pop_transform(&mut self) {
self.raw.pop_transform();
}
/// Executes the given drawing operations within a [`Rectangle`] region,
/// clipping any geometry that overflows its bounds. Any transformations
/// performed are local to the provided closure.
///
/// This method is useful to perform drawing operations that need to be
/// clipped.
#[inline]
pub fn with_clip<R>(
&mut self,
region: Rectangle,
f: impl FnOnce(&mut Self) -> R,
) -> R {
let mut frame = self.draft(region);
let result = f(&mut frame);
self.paste(frame);
result
}
/// Creates a new [`Frame`] with the given [`Size`].
///
/// Draw its contents back to this [`Frame`] with [`paste`].
///
/// [`paste`]: Self::paste
fn draft(&mut self, clip_bounds: Rectangle) -> Self {
Self {
raw: self.raw.draft(clip_bounds),
}
}
/// Draws the contents of the given [`Frame`] with origin at the given [`Point`].
fn paste(&mut self, frame: Self) {
self.raw.paste(frame.raw);
}
/// Applies a translation to the current transform of the [`Frame`].
pub fn translate(&mut self, translation: Vector) {
self.raw.translate(translation);
}
/// Applies a rotation in radians to the current transform of the [`Frame`].
pub fn rotate(&mut self, angle: impl Into<Radians>) {
self.raw.rotate(angle);
}
/// Applies a uniform scaling to the current transform of the [`Frame`].
pub fn scale(&mut self, scale: impl Into<f32>) {
self.raw.scale(scale);
}
/// Applies a non-uniform scaling to the current transform of the [`Frame`].
pub fn scale_nonuniform(&mut self, scale: impl Into<Vector>) {
self.raw.scale_nonuniform(scale);
}
/// Turns the [`Frame`] into its underlying geometry.
pub fn into_geometry(self) -> Renderer::Geometry {
self.raw.into_geometry()
}
}
/// The internal implementation of a [`Frame`].
///
/// Analogous to [`Frame`]. See [`Frame`] for the documentation
/// of each method.
#[allow(missing_docs)]
pub trait Backend: Sized {
type Geometry;
fn width(&self) -> f32;
fn height(&self) -> f32;
fn size(&self) -> Size;
fn center(&self) -> Point;
fn push_transform(&mut self);
fn pop_transform(&mut self);
fn translate(&mut self, translation: Vector);
fn rotate(&mut self, angle: impl Into<Radians>);
fn scale(&mut self, scale: impl Into<f32>);
fn scale_nonuniform(&mut self, scale: impl Into<Vector>);
fn draft(&mut self, clip_bounds: Rectangle) -> Self;
fn paste(&mut self, frame: Self);
fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>);
fn stroke_rectangle<'a>(
&mut self,
top_left: Point,
size: Size,
stroke: impl Into<Stroke<'a>>,
);
fn fill(&mut self, path: &Path, fill: impl Into<Fill>);
fn fill_text(&mut self, text: impl Into<Text>);
fn fill_rectangle(
&mut self,
top_left: Point,
size: Size,
fill: impl Into<Fill>,
);
fn draw_image(&mut self, bounds: Rectangle, image: impl Into<Image>);
fn draw_svg(&mut self, bounds: Rectangle, svg: impl Into<Svg>);
fn into_geometry(self) -> Self::Geometry;
}
#[cfg(debug_assertions)]
impl Backend for () {
type Geometry = ();
fn width(&self) -> f32 {
0.0
}
fn height(&self) -> f32 {
0.0
}
fn size(&self) -> Size {
Size::ZERO
}
fn center(&self) -> Point {
Point::ORIGIN
}
fn push_transform(&mut self) {}
fn pop_transform(&mut self) {}
fn translate(&mut self, _translation: Vector) {}
fn rotate(&mut self, _angle: impl Into<Radians>) {}
fn scale(&mut self, _scale: impl Into<f32>) {}
fn scale_nonuniform(&mut self, _scale: impl Into<Vector>) {}
fn draft(&mut self, _clip_bounds: Rectangle) -> Self {}
fn paste(&mut self, _frame: Self) {}
fn stroke<'a>(&mut self, _path: &Path, _stroke: impl Into<Stroke<'a>>) {}
fn stroke_rectangle<'a>(
&mut self,
_top_left: Point,
_size: Size,
_stroke: impl Into<Stroke<'a>>,
) {
}
fn fill(&mut self, _path: &Path, _fill: impl Into<Fill>) {}
fn fill_text(&mut self, _text: impl Into<Text>) {}
fn fill_rectangle(
&mut self,
_top_left: Point,
_size: Size,
_fill: impl Into<Fill>,
) {
}
fn draw_image(&mut self, _bounds: Rectangle, _image: impl Into<Image>) {}
fn draw_svg(&mut self, _bounds: Rectangle, _svg: impl Into<Svg>) {}
fn into_geometry(self) -> Self::Geometry {}
}