plotters::backend

Trait DrawingBackend

Source
pub trait DrawingBackend: Sized {
    type ErrorType: Error + Send + Sync;

    // Required methods
    fn get_size(&self) -> (u32, u32);
    fn ensure_prepared(
        &mut self,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>>;
    fn present(&mut self) -> Result<(), DrawingErrorKind<Self::ErrorType>>;
    fn draw_pixel(
        &mut self,
        point: (i32, i32),
        color: BackendColor,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>>;

    // Provided methods
    fn draw_line<S>(
        &mut self,
        from: (i32, i32),
        to: (i32, i32),
        style: &S,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
       where S: BackendStyle { ... }
    fn draw_rect<S>(
        &mut self,
        upper_left: (i32, i32),
        bottom_right: (i32, i32),
        style: &S,
        fill: bool,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
       where S: BackendStyle { ... }
    fn draw_path<S, I>(
        &mut self,
        path: I,
        style: &S,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
       where S: BackendStyle,
             I: IntoIterator<Item = (i32, i32)> { ... }
    fn draw_circle<S>(
        &mut self,
        center: (i32, i32),
        radius: u32,
        style: &S,
        fill: bool,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
       where S: BackendStyle { ... }
    fn fill_polygon<S, I>(
        &mut self,
        vert: I,
        style: &S,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
       where S: BackendStyle,
             I: IntoIterator<Item = (i32, i32)> { ... }
    fn draw_text<TStyle>(
        &mut self,
        text: &str,
        style: &TStyle,
        pos: (i32, i32),
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
       where TStyle: BackendTextStyle { ... }
    fn estimate_text_size<TStyle>(
        &self,
        text: &str,
        style: &TStyle,
    ) -> Result<(u32, u32), DrawingErrorKind<Self::ErrorType>>
       where TStyle: BackendTextStyle { ... }
    fn blit_bitmap(
        &mut self,
        pos: (i32, i32),
        _: (u32, u32),
        src: &[u8],
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>> { ... }
}
Expand description

The drawing backend trait, which implements the low-level drawing APIs. This trait has a set of default implementation. And the minimal requirement of implementing a drawing backend is implementing the draw_pixel function.

If the drawing backend supports vector graphics, the other drawing APIs should be override by the backend specific implementation. Otherwise, the default implementation will use the pixel-based approach to draw other types of low-level shapes.

Required Associated Types§

Source

type ErrorType: Error + Send + Sync

The error type reported by the backend

Required Methods§

Source

fn get_size(&self) -> (u32, u32)

Get the dimension of the drawing backend in pixels

Source

fn ensure_prepared(&mut self) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Ensure the backend is ready to draw

Source

fn present(&mut self) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Finalize the drawing step and present all the changes. This is used as the real-time rendering support. The backend may implement in the following way, when ensure_prepared is called it checks if it needs a fresh buffer and present is called rendering all the pending changes on the screen.

Source

fn draw_pixel( &mut self, point: (i32, i32), color: BackendColor, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Draw a pixel on the drawing backend

  • point: The backend pixel-based coordinate to draw
  • color: The color of the pixel

Provided Methods§

Source

fn draw_line<S>( &mut self, from: (i32, i32), to: (i32, i32), style: &S, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
where S: BackendStyle,

Draw a line on the drawing backend

  • from: The start point of the line
  • to: The end point of the line
  • style: The style of the line
Source

fn draw_rect<S>( &mut self, upper_left: (i32, i32), bottom_right: (i32, i32), style: &S, fill: bool, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
where S: BackendStyle,

Draw a rectangle on the drawing backend

  • upper_left: The coordinate of the upper-left corner of the rect
  • bottom_right: The coordinate of the bottom-right corner of the rect
  • style: The style
  • fill: If the rectangle should be filled
Source

fn draw_path<S, I>( &mut self, path: I, style: &S, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
where S: BackendStyle, I: IntoIterator<Item = (i32, i32)>,

Draw a path on the drawing backend

  • path: The iterator of key points of the path
  • style: The style of the path
Source

fn draw_circle<S>( &mut self, center: (i32, i32), radius: u32, style: &S, fill: bool, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
where S: BackendStyle,

Draw a circle on the drawing backend

  • center: The center coordinate of the circle
  • radius: The radius of the circle
  • style: The style of the shape
  • fill: If the circle should be filled
Source

fn fill_polygon<S, I>( &mut self, vert: I, style: &S, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
where S: BackendStyle, I: IntoIterator<Item = (i32, i32)>,

Source

fn draw_text<TStyle>( &mut self, text: &str, style: &TStyle, pos: (i32, i32), ) -> Result<(), DrawingErrorKind<Self::ErrorType>>
where TStyle: BackendTextStyle,

Draw a text on the drawing backend

  • text: The text to draw
  • style: The text style
  • pos : The text anchor point
Source

fn estimate_text_size<TStyle>( &self, text: &str, style: &TStyle, ) -> Result<(u32, u32), DrawingErrorKind<Self::ErrorType>>
where TStyle: BackendTextStyle,

Estimate the size of the horizontal text if rendered on this backend. This is important because some of the backend may not have font ability. Thus this allows those backend reports proper value rather than ask the font rasterizer for that.

  • text: The text to estimate
  • font: The font to estimate
  • Returns The estimated text size
Source

fn blit_bitmap( &mut self, pos: (i32, i32), _: (u32, u32), src: &[u8], ) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Blit a bitmap on to the backend.

  • text: pos the left upper conner of the bitmap to blit
  • src: The source of the image

TODO: The default implementation of bitmap blitting assumes that the bitmap is RGB, but this may not be the case. But for bitmap backend it’s actually ok if we use the bitmap element that matches the pixel format, but we need to fix this.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§