Struct femtovg::Canvas

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

Main 2D drawing context.

Implementations§

source§

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

source

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

Creates a new canvas.

source

pub fn new_with_text_context( renderer: T, text_context: TextContext ) -> Result<Self, 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.

source

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

Sets the size of the default framebuffer (screen size)

source

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.

source

pub fn width(&self) -> u32

Returns the width of the current render target.

source

pub fn height(&self) -> u32

Returns the height of the current render target.

source

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.

source

pub fn screenshot(&mut self) -> Result<ImgVec<RGBA8>, ErrorKind>

source

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.

source

pub fn restore(&mut self)

Restores the previous render state

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

source

pub fn reset(&mut self)

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

source

pub fn save_with(&mut self, callback: impl FnMut(&mut Self))

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

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

source

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.

source

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

Sets the composite operation.

source

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

Sets the composite operation with custom pixel arithmetic.

source

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.

source

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

source

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.

source

pub fn create_image_from_native_texture( &mut self, texture: T::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.

source

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

Creates image from specified image data.

source

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

Returns the native texture of an image given its ID.

source

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

source

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

source

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.

source

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

Decode an image from file

source

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

Decode an image from memory

source

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

Updates image data specified by image handle.

source

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

Deletes created image.

source

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

Returns image info

source

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

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

source

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.

source

pub fn reset_transform(&mut self)

Resets current transform to a identity matrix.

source

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

Premultiplies current coordinate system by specified transform.

source

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

Translates the current coordinate system.

source

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

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

source

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

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

source

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

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

source

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

Scales the current coordinate system.

source

pub fn transform(&self) -> Transform2D

Returns the current transformation matrix

source

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.

source

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.

source

pub fn reset_scissor(&mut self)

Reset and disables scissoring.

source

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.

source

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

Return the bounding box for a Path

source

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

Fills the provided Path with the specified Paint.

source

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

Strokes the provided Path with the specified Paint.

source

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

Adds a font file to the canvas

source

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.

source

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

Adds all .ttf files from a directory

source

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

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

source

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

Returns font metrics for a particular Paint.

source

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

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

source

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

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

source

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

Fills the provided string with the specified Paint.

source

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

Strokes the provided string with the specified Paint.

source

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§

source§

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

source§

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
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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.