Trait piet::RenderContext
source · pub trait RenderContextwhere
Self::Brush: IntoBrush<Self>,{
type Brush: Clone;
type Text: Text<TextLayout = Self::TextLayout>;
type TextLayout: TextLayout;
type Image: Image;
Show 22 methods
fn status(&mut self) -> Result<(), Error>;
fn solid_brush(&mut self, color: Color) -> Self::Brush;
fn gradient(
&mut self,
gradient: impl Into<FixedGradient>
) -> Result<Self::Brush, Error>;
fn clear(&mut self, region: impl Into<Option<Rect>>, color: Color);
fn stroke(
&mut self,
shape: impl Shape,
brush: &impl IntoBrush<Self>,
width: f64
);
fn stroke_styled(
&mut self,
shape: impl Shape,
brush: &impl IntoBrush<Self>,
width: f64,
style: &StrokeStyle
);
fn fill(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>);
fn fill_even_odd(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>);
fn clip(&mut self, shape: impl Shape);
fn text(&mut self) -> &mut Self::Text;
fn draw_text(&mut self, layout: &Self::TextLayout, pos: impl Into<Point>);
fn save(&mut self) -> Result<(), Error>;
fn restore(&mut self) -> Result<(), Error>;
fn finish(&mut self) -> Result<(), Error>;
fn transform(&mut self, transform: Affine);
fn make_image(
&mut self,
width: usize,
height: usize,
buf: &[u8],
format: ImageFormat
) -> Result<Self::Image, Error>;
fn draw_image(
&mut self,
image: &Self::Image,
dst_rect: impl Into<Rect>,
interp: InterpolationMode
);
fn draw_image_area(
&mut self,
image: &Self::Image,
src_rect: impl Into<Rect>,
dst_rect: impl Into<Rect>,
interp: InterpolationMode
);
fn capture_image_area(
&mut self,
src_rect: impl Into<Rect>
) -> Result<Self::Image, Error>;
fn blurred_rect(
&mut self,
rect: Rect,
blur_radius: f64,
brush: &impl IntoBrush<Self>
);
fn current_transform(&self) -> Affine;
fn with_save(
&mut self,
f: impl FnOnce(&mut Self) -> Result<(), Error>
) -> Result<(), Error> { ... }
}
Expand description
The main trait for rendering graphics.
This trait provides an API for drawing 2D graphics. In basic usage, it wraps a surface of some kind, so that drawing commands paint onto the surface. It can also be a recording context, creating a display list for playback later.
The intent of the design is to be general so that any number of back-ends can implement this trait.
Code that draws graphics will in general take &mut impl RenderContext
.
Required Associated Types§
sourcetype Text: Text<TextLayout = Self::TextLayout>
type Text: Text<TextLayout = Self::TextLayout>
An associated factory for creating text layouts and related resources.
sourcetype TextLayout: TextLayout
type TextLayout: TextLayout
The type use to represent text layout objects.
Required Methods§
sourcefn status(&mut self) -> Result<(), Error>
fn status(&mut self) -> Result<(), Error>
Report an internal error.
Drawing operations may cause internal errors, which may also occur asynchronously after the drawing command was issued. This method reports any such error that has been detected.
sourcefn solid_brush(&mut self, color: Color) -> Self::Brush
fn solid_brush(&mut self, color: Color) -> Self::Brush
Create a new brush resource.
TODO: figure out how to document lifetime and rebuilding requirements. Should that be the responsibility of the client, or should the back-end take responsibility? We could have a cache that is flushed when the Direct2D render target is rebuilt. Solid brushes are super lightweight, but other potentially retained objects will be heavier.
sourcefn gradient(
&mut self,
gradient: impl Into<FixedGradient>
) -> Result<Self::Brush, Error>
fn gradient(
&mut self,
gradient: impl Into<FixedGradient>
) -> Result<Self::Brush, Error>
Create a new gradient brush.
sourcefn clear(&mut self, region: impl Into<Option<Rect>>, color: Color)
fn clear(&mut self, region: impl Into<Option<Rect>>, color: Color)
Replace a region of the canvas with the provided Color
.
The region can be omitted, in which case it will apply to the entire canvas.
This operation ignores any existing clipping and transforations.
Note:
You probably don’t want to call this. It is essentially a specialized
fill method that can be used in GUI contexts for things like clearing
damage regions. It does not have a good cross-platform implementation,
and eventually should be deprecated when support is added for blend
modes, at which point it will be easier to just use fill
for
everything.
sourcefn stroke(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>, width: f64)
fn stroke(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>, width: f64)
Stroke a Shape
, using the default StrokeStyle
.
sourcefn stroke_styled(
&mut self,
shape: impl Shape,
brush: &impl IntoBrush<Self>,
width: f64,
style: &StrokeStyle
)
fn stroke_styled(
&mut self,
shape: impl Shape,
brush: &impl IntoBrush<Self>,
width: f64,
style: &StrokeStyle
)
Stroke a Shape
, providing a custom StrokeStyle
.
sourcefn fill(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>)
fn fill(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>)
Fill a Shape
, using the non-zero fill rule.
sourcefn fill_even_odd(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>)
fn fill_even_odd(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>)
Fill a shape, using the even-odd fill rule.
sourcefn text(&mut self) -> &mut Self::Text
fn text(&mut self) -> &mut Self::Text
Returns a reference to a shared Text
object.
This provides access to the text API.
sourcefn draw_text(&mut self, layout: &Self::TextLayout, pos: impl Into<Point>)
fn draw_text(&mut self, layout: &Self::TextLayout, pos: impl Into<Point>)
Draw a TextLayout
.
The pos
parameter specifies the upper-left corner of the layout object
(even for right-to-left text). To draw on a baseline, you can use
TextLayout::line_metric
to get the baseline position of a specific line.
sourcefn save(&mut self) -> Result<(), Error>
fn save(&mut self) -> Result<(), Error>
Save the context state.
Pushes the current context state onto a stack, to be popped by
restore
.
Prefer with_save
if possible, as that statically
enforces balance of save/restore pairs.
The context state currently consists of a clip region and an affine transform, but is expected to grow in the near future.
sourcefn restore(&mut self) -> Result<(), Error>
fn restore(&mut self) -> Result<(), Error>
Restore the context state.
Pop a context state that was pushed by save
. See
that method for details.
sourcefn finish(&mut self) -> Result<(), Error>
fn finish(&mut self) -> Result<(), Error>
Finish any pending operations.
This will generally be called by a shell after all user drawing operations but before presenting. Not all back-ends will handle this the same way.
sourcefn transform(&mut self, transform: Affine)
fn transform(&mut self, transform: Affine)
Apply a transform.
Apply an affine transformation. The transformation remains in effect
until a restore
operation.
sourcefn make_image(
&mut self,
width: usize,
height: usize,
buf: &[u8],
format: ImageFormat
) -> Result<Self::Image, Error>
fn make_image(
&mut self,
width: usize,
height: usize,
buf: &[u8],
format: ImageFormat
) -> Result<Self::Image, Error>
Create a new Image
from a pixel buffer.
This takes raw pixel data and attempts create an object that the platform knows how to draw.
The generated image can be cached and reused. This is a good idea for images that are used frequently, because creating the image type may be expensive.
To draw the generated image, pass it to draw_image
. To draw a portion
of the image, use draw_image_area
.
If you are trying to create an image from the contents of this
RenderContext
, see capture_image_area
.
sourcefn draw_image(
&mut self,
image: &Self::Image,
dst_rect: impl Into<Rect>,
interp: InterpolationMode
)
fn draw_image(
&mut self,
image: &Self::Image,
dst_rect: impl Into<Rect>,
interp: InterpolationMode
)
sourcefn draw_image_area(
&mut self,
image: &Self::Image,
src_rect: impl Into<Rect>,
dst_rect: impl Into<Rect>,
interp: InterpolationMode
)
fn draw_image_area(
&mut self,
image: &Self::Image,
src_rect: impl Into<Rect>,
dst_rect: impl Into<Rect>,
interp: InterpolationMode
)
Draw a specified area of an Image
.
The src_rect
area of image
is scaled to the provided dst_rect
.
It will be squashed if the aspect ratios don’t match.
sourcefn capture_image_area(
&mut self,
src_rect: impl Into<Rect>
) -> Result<Self::Image, Error>
fn capture_image_area(
&mut self,
src_rect: impl Into<Rect>
) -> Result<Self::Image, Error>
Create an Image
of the specified region of the context.
The src_rect
area of the current render context will be captured
as a copy and returned.
This can be used for things like caching expensive drawing operations.
sourcefn blurred_rect(
&mut self,
rect: Rect,
blur_radius: f64,
brush: &impl IntoBrush<Self>
)
fn blurred_rect(
&mut self,
rect: Rect,
blur_radius: f64,
brush: &impl IntoBrush<Self>
)
Draw a rectangle with Gaussian blur.
The blur radius is sometimes referred to as the “standard deviation” of the blur.
sourcefn current_transform(&self) -> Affine
fn current_transform(&self) -> Affine
Returns the transformations currently applied to the context.