[][src]Trait piet::RenderContext

pub trait RenderContext {
    type Point: Into<Vec2> + RoundFrom<Vec2> + RoundFrom<(f32, f32)> + RoundFrom<(f64, f64)>;
    type Coord: Into<f64> + RoundFrom<f64>;
    type Brush;
    type Text: Text<TextLayout = Self::TextLayout>;
    type TextLayout: TextLayout;
    type Image;
    fn status(&mut self) -> Result<(), Error>;
fn solid_brush(&mut self, color: Color) -> Self::Brush;
fn gradient(&mut self, gradient: Gradient) -> Result<Self::Brush, Error>;
fn clear(&mut self, color: Color);
fn stroke(
        &mut self,
        shape: impl Shape,
        brush: &Self::Brush,
        width: impl RoundInto<Self::Coord>,
        style: Option<&StrokeStyle>
    );
fn fill(
        &mut self,
        shape: impl Shape,
        brush: &Self::Brush,
        fill_rule: FillRule
    );
fn clip(&mut self, shape: impl Shape, fill_rule: FillRule);
fn text(&mut self) -> &mut Self::Text;
fn draw_text(
        &mut self,
        layout: &Self::TextLayout,
        pos: impl RoundInto<Self::Point>,
        brush: &Self::Brush
    );
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,
        rect: impl Into<Rect>,
        interp: InterpolationMode
    ); fn with_save(
        &mut self,
        f: impl FnOnce(&mut Self) -> Result<(), Error>
    ) -> Result<(), Error> { ... } }

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.

Associated Types

type Point: Into<Vec2> + RoundFrom<Vec2> + RoundFrom<(f32, f32)> + RoundFrom<(f64, f64)>

The type of a 2D point, for this backend.

Generally this needs to be a newtype so that the RoundFrom traits can be implemented on it. Possibly this can be relaxed in the future, as we move towards a standard RoundFrom.

type Coord: Into<f64> + RoundFrom<f64>

The type of 1D measurements, for example stroke width.

Generally this will be either f32 or f64.

type Brush

The type of a "brush".

Initially just a solid RGBA color, but will probably expand to gradients.

type Text: Text<TextLayout = Self::TextLayout>

An associated factory for creating text layouts and related resources.

type TextLayout: TextLayout

type Image

The associated type of an image.

Loading content...

Required methods

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.

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 responsiblity? 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.

fn gradient(&mut self, gradient: Gradient) -> Result<Self::Brush, Error>

Create a new gradient brush.

fn clear(&mut self, color: Color)

Clear the canvas with the given color.

Note: only opaque colors are meaningful.

fn stroke(
    &mut self,
    shape: impl Shape,
    brush: &Self::Brush,
    width: impl RoundInto<Self::Coord>,
    style: Option<&StrokeStyle>
)

Stroke a shape.

fn fill(&mut self, shape: impl Shape, brush: &Self::Brush, fill_rule: FillRule)

Fill a shape.

fn clip(&mut self, shape: impl Shape, fill_rule: FillRule)

Clip to a shape.

All subsequent drawing operations up to the next restore are clipped by the shape.

fn text(&mut self) -> &mut Self::Text

fn draw_text(
    &mut self,
    layout: &Self::TextLayout,
    pos: impl RoundInto<Self::Point>,
    brush: &Self::Brush
)

Draw a text layout.

The pos parameter specifies the baseline of the left starting place of the text. Note: this is true even if the text is right-to-left.

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.

fn restore(&mut self) -> Result<(), Error>

Restore the context state.

Pop a context state that was pushed by save. See that method for details.

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.

fn transform(&mut self, transform: Affine)

Apply a transform.

Apply an affine transformation. The transformation remains in effect until a restore operation.

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.

fn draw_image(
    &mut self,
    image: &Self::Image,
    rect: impl Into<Rect>,
    interp: InterpolationMode
)

Draw an image.

The image is scaled to the provided rect. It will be squashed if aspect ratios don't match.

Loading content...

Provided methods

fn with_save(
    &mut self,
    f: impl FnOnce(&mut Self) -> Result<(), Error>
) -> Result<(), Error>

Do graphics operations with the context state saved and then restored.

Equivalent to save, calling f, then restore. See those methods for more details.

Loading content...

Implementors

Loading content...