Struct vizia::vg::Canvas

pub struct Canvas<T>
where T: Renderer,
{ /* private fields */ }
Expand description

Main 2D drawing context.

Implementations§

§

impl<T> Canvas<T>
where T: Renderer,

pub fn new(renderer: T) -> Result<Canvas<T>, ErrorKind>

Creates a new canvas.

pub fn new_with_text_context( renderer: T, text_context: TextContext ) -> Result<Canvas<T>, ErrorKind>

Creates a new canvas with the specified renderer and using the fonts registered with the provided TextContext. Note that the context is explicitly shared, so that any fonts registered with a clone of this context will also be visible to this canvas.

pub fn set_size(&mut self, width: u32, height: u32, dpi: f32)

Sets the size of the default framebuffer (screen size)

pub fn clear_rect( &mut self, x: u32, y: u32, width: u32, height: u32, color: Color )

Clears the rectangle area defined by left upper corner (x,y), width and height with the provided color.

pub fn width(&self) -> u32

Returns the width of the current render target.

pub fn height(&self) -> u32

Returns the height of the current render target.

pub fn flush(&mut self)

Tells the renderer to execute all drawing commands and clears the current internal state

Call this at the end of each frame.

pub fn screenshot(&mut self) -> Result<Img<Vec<Rgba<u8>>>, ErrorKind>

pub fn save(&mut self)

Pushes and saves the current render state into a state stack.

A matching restore() must be used to restore the state.

pub fn restore(&mut self)

Restores the previous render state

Restoring the initial/first state will just reset it to the defaults

pub fn reset(&mut self)

Resets current state to default values. Does not affect the state stack.

pub fn save_with(&mut self, callback: impl FnMut(&mut Canvas<T>))

Saves the current state before calling the callback and restores it afterwards

This is less error prone than remembering to match save() -> restore() calls

pub fn set_global_alpha(&mut self, alpha: f32)

Sets the transparency applied to all rendered shapes.

Already transparent paths will get proportionally more transparent as well.

pub fn global_composite_operation(&mut self, op: CompositeOperation)

Sets the composite operation.

pub fn global_composite_blend_func( &mut self, src_factor: BlendFactor, dst_factor: BlendFactor )

Sets the composite operation with custom pixel arithmetic.

pub fn global_composite_blend_func_separate( &mut self, src_rgb: BlendFactor, dst_rgb: BlendFactor, src_alpha: BlendFactor, dst_alpha: BlendFactor )

Sets the composite operation with custom pixel arithmetic for RGB and alpha components separately.

pub fn set_render_target(&mut self, target: RenderTarget)

Sets a new render target. All drawing operations after this call will happen on the provided render target

pub fn create_image_empty( &mut self, width: usize, height: usize, format: PixelFormat, flags: ImageFlags ) -> Result<ImageId, ErrorKind>

Allocates an empty image with the provided domensions and format.

pub fn create_image_from_native_texture( &mut self, texture: <T as Renderer>::NativeTexture, info: ImageInfo ) -> Result<ImageId, ErrorKind>

Allocates an image that wraps the given backend-specific texture. Use this function to import external textures into the rendering of a scene with femtovg.

It is necessary to call [Self::delete_image] to free femtovg specific book-keeping data structures, the underlying backend-specific texture memory will not be freed. It is the caller’s responsible to delete it.

pub fn create_image<'a, S>( &mut self, src: S, flags: ImageFlags ) -> Result<ImageId, ErrorKind>
where S: Into<ImageSource<'a>>,

Creates image from specified image data.

pub fn get_native_texture( &self, id: ImageId ) -> Result<<T as Renderer>::NativeTexture, ErrorKind>

Returns the native texture of an image given its ID.

pub fn get_image(&self, id: ImageId) -> Option<&<T as Renderer>::Image>

pub fn get_image_mut( &mut self, id: ImageId ) -> Option<&mut <T as Renderer>::Image>

pub fn realloc_image( &mut self, id: ImageId, width: usize, height: usize, format: PixelFormat, flags: ImageFlags ) -> Result<(), ErrorKind>

Resizes an image to the new provided dimensions.

pub fn load_image_file<P>( &mut self, filename: P, flags: ImageFlags ) -> Result<ImageId, ErrorKind>
where P: AsRef<Path>,

Decode an image from file

pub fn load_image_mem( &mut self, data: &[u8], flags: ImageFlags ) -> Result<ImageId, ErrorKind>

Decode an image from memory

pub fn update_image<'a, S>( &mut self, id: ImageId, src: S, x: usize, y: usize ) -> Result<(), ErrorKind>
where S: Into<ImageSource<'a>>,

Updates image data specified by image handle.

pub fn delete_image(&mut self, id: ImageId)

Deletes created image.

pub fn image_info(&self, id: ImageId) -> Result<ImageInfo, ErrorKind>

Returns image info

pub fn image_size(&self, id: ImageId) -> Result<(usize, usize), ErrorKind>

Returns the size in pixels of the image for the specified id.

pub fn filter_image( &mut self, target_image: ImageId, filter: ImageFilter, source_image: ImageId )

Renders the given source_image into target_image while applying a filter effect.

The target image must have the same size as the source image. The filtering is recorded as a drawing command and run by the renderer when Self::flush() is called.

The filtering does not take any transformation set on the Canvas into account nor does it change the current rendering target.

pub fn reset_transform(&mut self)

Resets current transform to a identity matrix.

pub fn set_transform(&mut self, transform: &Transform2D)

Premultiplies current coordinate system by specified transform.

pub fn translate(&mut self, x: f32, y: f32)

Translates the current coordinate system.

pub fn rotate(&mut self, angle: f32)

Rotates the current coordinate system. Angle is specified in radians.

pub fn skew_x(&mut self, angle: f32)

Skews the current coordinate system along X axis. Angle is specified in radians.

pub fn skew_y(&mut self, angle: f32)

Skews the current coordinate system along Y axis. Angle is specified in radians.

pub fn scale(&mut self, x: f32, y: f32)

Scales the current coordinate system.

pub fn transform(&self) -> Transform2D

Returns the current transformation matrix

pub fn scissor(&mut self, x: f32, y: f32, w: f32, h: f32)

Sets the current scissor rectangle.

The scissor rectangle is transformed by the current transform.

pub fn intersect_scissor(&mut self, x: f32, y: f32, w: f32, h: f32)

Intersects current scissor rectangle with the specified rectangle.

The scissor rectangle is transformed by the current transform. Note: in case the rotation of previous scissor rect differs from the current one, the intersection will be done between the specified rectangle and the previous scissor rectangle transformed in the current transform space. The resulting shape is always rectangle.

pub fn reset_scissor(&mut self)

Reset and disables scissoring.

pub fn contains_point( &self, path: &Path, x: f32, y: f32, fill_rule: FillRule ) -> bool

Returns true if the specified point (x,y) is in the provided path, and false otherwise.

pub fn path_bbox(&self, path: &Path) -> Bounds

Return the bounding box for a Path

pub fn fill_path(&mut self, path: &Path, paint: &Paint)

Fills the provided Path with the specified Paint.

pub fn stroke_path(&mut self, path: &Path, paint: &Paint)

Strokes the provided Path with the specified Paint.

pub fn add_font<P>(&mut self, file_path: P) -> Result<FontId, ErrorKind>
where P: AsRef<Path>,

Adds a font file to the canvas

pub fn add_font_mem(&mut self, data: &[u8]) -> Result<FontId, ErrorKind>

Adds a font to the canvas by reading it from the specified chunk of memory.

pub fn add_font_dir<P>(&mut self, dir_path: P) -> Result<Vec<FontId>, ErrorKind>
where P: AsRef<Path>,

Adds all .ttf files from a directory

pub fn measure_text<S>( &self, x: f32, y: f32, text: S, paint: &Paint ) -> Result<TextMetrics, ErrorKind>
where S: AsRef<str>,

Returns information on how the provided text will be drawn with the specified paint.

pub fn measure_font(&self, paint: &Paint) -> Result<FontMetrics, ErrorKind>

Returns font metrics for a particular Paint.

pub fn break_text<S>( &self, max_width: f32, text: S, paint: &Paint ) -> Result<usize, ErrorKind>
where S: AsRef<str>,

Returns the maximum index-th byte of text that will fit inside max_width.

The retuned index will always lie at the start and/or end of a UTF-8 code point sequence or at the start or end of the text

pub fn break_text_vec<S>( &self, max_width: f32, text: S, paint: &Paint ) -> Result<Vec<Range<usize>>, ErrorKind>
where S: AsRef<str>,

Returnes a list of ranges representing each line of text that will fit inside max_width

pub fn fill_text<S>( &mut self, x: f32, y: f32, text: S, paint: &Paint ) -> Result<TextMetrics, ErrorKind>
where S: AsRef<str>,

Fills the provided string with the specified Paint.

pub fn stroke_text<S>( &mut self, x: f32, y: f32, text: S, paint: &Paint ) -> Result<TextMetrics, ErrorKind>
where S: AsRef<str>,

Strokes the provided string with the specified Paint.

pub fn draw_glyph_commands( &mut self, draw_commands: GlyphDrawCommands, paint: &Paint, scale: f32 )

Dispatch an explicit set of GlyphDrawCommands to the renderer. Use this only if you are using a custom font rasterizer/layout.

Trait Implementations§

§

impl<T> Drop for Canvas<T>
where T: Renderer,

§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Canvas<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for Canvas<T>

§

impl<T> !Send for Canvas<T>

§

impl<T> !Sync for Canvas<T>

§

impl<T> Unpin for Canvas<T>
where T: Unpin, <T as Renderer>::Image: Unpin,

§

impl<T> !UnwindSafe for Canvas<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more