Struct femtovg::Paint

source ·
pub struct Paint { /* private fields */ }
Expand description

Struct controlling how graphical shapes are rendered.

The Paint struct is a relatively lightweight object which contains all the information needed to display something on the canvas. Unlike the HTML canvas where the current drawing style is stored in an internal stack this paint struct is simply passed to the relevant drawing methods on the canvas.

Clients code can have as many paints as they desire for different use cases and styles. This makes the internal stack in the Canvas struct much lighter since it only needs to contain the transform stack and current scissor rectangle.

§Example

use femtovg::{Paint, Path, Color, Canvas, renderer::Void};

let mut canvas = Canvas::new(Void).expect("Cannot create canvas");

let fill_paint = Paint::color(Color::hex("454545"));
let mut stroke_paint = Paint::color(Color::hex("bababa"));
stroke_paint.set_line_width(4.0);

let mut path = Path::new();
path.rounded_rect(10.0, 10.0, 100.0, 100.0, 20.0);
canvas.fill_path(&path, &fill_paint);
canvas.stroke_path(&path, &stroke_paint);

Implementations§

source§

impl Paint

source

pub fn color(color: Color) -> Self

Creates a new solid color paint

source

pub fn image( id: ImageId, cx: f32, cy: f32, width: f32, height: f32, angle: f32, alpha: f32 ) -> Self

Creates a new image pattern paint.

  • id - is handle to the image to render
  • cx cy - Specify the top-left location of the image pattern
  • width height - The size of one image
  • angle - Rotation around the top-left corner
  • alpha - Transparency applied on the image
§Example
use femtovg::{Paint, Path, Color, Canvas, ImageFlags, renderer::Void};

let mut canvas = Canvas::new(Void).expect("Cannot create canvas");

let image_id = canvas.load_image_file("examples/assets/rust-logo.png", ImageFlags::GENERATE_MIPMAPS).expect("Cannot create image");
let fill_paint = Paint::image(image_id, 10.0, 10.0, 85.0, 85.0, 0.0, 1.0);

let mut path = Path::new();
path.rect(10.0, 10.0, 85.0, 85.0);
canvas.fill_path(&path, &fill_paint);
source

pub fn image_tint( id: ImageId, cx: f32, cy: f32, width: f32, height: f32, angle: f32, tint: Color ) -> Self

Like image, but allows for adding a tint, or a color which will transform each pixel’s color via channel-wise multiplication.

source

pub fn linear_gradient( start_x: f32, start_y: f32, end_x: f32, end_y: f32, start_color: Color, end_color: Color ) -> Self

Creates and returns a linear gradient paint.

The gradient is transformed by the current transform when it is passed to fill_path() or stroke_path().

§Example
use femtovg::{Paint, Path, Color, Canvas, ImageFlags, renderer::Void};

let mut canvas = Canvas::new(Void).expect("Cannot create canvas");

let bg = Paint::linear_gradient(0.0, 0.0, 0.0, 100.0, Color::rgba(255, 255, 255, 16), Color::rgba(0, 0, 0, 16));
let mut path = Path::new();
path.rounded_rect(0.0, 0.0, 100.0, 100.0, 5.0);
canvas.fill_path(&path, &bg);
source

pub fn linear_gradient_stops( start_x: f32, start_y: f32, end_x: f32, end_y: f32, stops: impl IntoIterator<Item = (f32, Color)> ) -> Self

Creates and returns a linear gradient paint with two or more stops.

The gradient is transformed by the current transform when it is passed to fill_path() or stroke_path().

§Example
use femtovg::{Paint, Path, Color, Canvas, ImageFlags, renderer::Void};

let mut canvas = Canvas::new(Void).expect("Cannot create canvas");

let bg = Paint::linear_gradient_stops(
   0.0, 0.0,
   0.0, 100.0,
   [
        (0.0, Color::rgba(255, 255, 255, 16)),
        (0.5, Color::rgba(0, 0, 0, 16)),
        (1.0, Color::rgba(255, 0, 0, 16))
   ]);
let mut path = Path::new();
path.rounded_rect(0.0, 0.0, 100.0, 100.0, 5.0);
canvas.fill_path(&path, &bg);
source

pub fn box_gradient( x: f32, y: f32, width: f32, height: f32, radius: f32, feather: f32, inner_color: Color, outer_color: Color ) -> Self

Creates and returns a box gradient.

Box gradient is a feathered rounded rectangle, it is useful for rendering drop shadows or highlights for boxes. Parameters (x,y) define the top-left corner of the rectangle, (w,h) define the size of the rectangle, r defines the corner radius, and f feather. Feather defines how blurry the border of the rectangle is. Parameter inner_color specifies the inner color and outer_color the outer color of the gradient. The gradient is transformed by the current transform when it is passed to fill_path() or stroke_path().

§Example
use femtovg::{Paint, Path, Color, Canvas, ImageFlags, renderer::Void};

let mut canvas = Canvas::new(Void).expect("Cannot create canvas");

let bg = Paint::box_gradient(
   0.0,
   0.0,
   100.0,
   100.0,
   10.0,
   10.0,
   Color::rgba(0, 0, 0, 128),
   Color::rgba(0, 0, 0, 0),
);

let mut path = Path::new();
path.rounded_rect(0.0, 0.0, 100.0, 100.0, 5.0);
canvas.fill_path(&path, &bg);
source

pub fn radial_gradient( cx: f32, cy: f32, in_radius: f32, out_radius: f32, inner_color: Color, outer_color: Color ) -> Self

Creates and returns a radial gradient.

Parameters (cx,cy) specify the center, in_radius and out_radius specify the inner and outer radius of the gradient, inner_color specifies the start color and outer_color the end color. The gradient is transformed by the current transform when it is passed to fill_paint() or stroke_paint().

§Example
use femtovg::{Paint, Path, Color, Canvas, ImageFlags, renderer::Void};

let mut canvas = Canvas::new(Void).expect("Cannot create canvas");

let bg = Paint::radial_gradient(
   50.0,
   50.0,
   18.0,
   24.0,
   Color::rgba(0, 0, 0, 128),
   Color::rgba(0, 0, 0, 0),
);

let mut path = Path::new();
path.circle(50.0, 50.0, 20.0);
canvas.fill_path(&path, &bg);
source

pub fn radial_gradient_stops( cx: f32, cy: f32, in_radius: f32, out_radius: f32, stops: impl IntoIterator<Item = (f32, Color)> ) -> Self

Creates and returns a multi-stop radial gradient.

Parameters (cx,cy) specify the center, in_radius and out_radius specify the inner and outer radius of the gradient, colors specifies a list of color stops with offsets. The first offset should be 0.0 and the last offset should be 1.0.

The gradient is transformed by the current transform when it is passed to fill_paint() or stroke_paint().

§Example
use femtovg::{Paint, Path, Color, Canvas, ImageFlags, renderer::Void};

let mut canvas = Canvas::new(Void).expect("Cannot create canvas");

let bg = Paint::radial_gradient_stops(
   50.0,
   50.0,
   18.0,
   24.0,
   [
        (0.0, Color::rgba(0, 0, 0, 128)),
        (0.5, Color::rgba(0, 0, 128, 128)),
        (1.0, Color::rgba(0, 128, 0, 128))
   ]
);

let mut path = Path::new();
path.circle(50.0, 50.0, 20.0);
canvas.fill_path(&path, &bg);
source

pub fn set_color(&mut self, color: Color)

Creates a new solid color paint

source

pub fn with_color(self, color: Color) -> Self

Returns the paint with a new solid color set to the specified value.

source

pub fn anti_alias(&self) -> bool

Returns boolean if the shapes drawn with this paint will be antialiased.

source

pub fn set_anti_alias(&mut self, value: bool)

Sets whether shapes drawn with this paint will be anti aliased. Enabled by default.

source

pub fn with_anti_alias(self, value: bool) -> Self

Returns the paint with anti alias set to the specified value.

source

pub fn stencil_strokes(&self) -> bool

True if this paint uses higher quality stencil strokes.

source

pub fn set_stencil_strokes(&mut self, value: bool)

Sets whether to use higher quality stencil strokes.

source

pub fn with_stencil_strokes(self, value: bool) -> Self

Returns the paint with stencil strokes set to the specified value.

source

pub fn line_width(&self) -> f32

Returns the current line width.

source

pub fn set_line_width(&mut self, width: f32)

Sets the line width for shapes stroked with this paint.

source

pub fn with_line_width(self, width: f32) -> Self

Returns the paint with line width set to the specified value.

source

pub fn miter_limit(&self) -> f32

Getter for the miter limit

source

pub fn set_miter_limit(&mut self, limit: f32)

Sets the limit at which a sharp corner is drawn beveled.

If the miter at a corner exceeds this limit, LineJoin is replaced with LineJoin::Bevel.

source

pub fn with_miter_limit(self, limit: f32) -> Self

Returns the paint with the miter limit set to the specified value.

source

pub fn line_cap_start(&self) -> LineCap

Returns the current start line cap for this paint.

source

pub fn line_cap_end(&self) -> LineCap

Returns the current start line cap for this paint.

source

pub fn set_line_cap(&mut self, cap: LineCap)

Sets how the start and end of the line (cap) is drawn

By default it’s set to LineCap::Butt

source

pub fn with_line_cap(self, cap: LineCap) -> Self

Returns the paint with line cap set to the specified value.

source

pub fn set_line_cap_start(&mut self, cap: LineCap)

Sets how the beggining cap of the line is drawn

By default it’s set to LineCap::Butt

source

pub fn with_line_cap_start(self, cap: LineCap) -> Self

Returns the paint with the beginning cap of the line set to the specified value.

source

pub fn set_line_cap_end(&mut self, cap: LineCap)

Sets how the end cap of the line is drawn

By default it’s set to LineCap::Butt

source

pub fn with_line_cap_end(self, cap: LineCap) -> Self

Returns the paint with the beginning cap of the line set to the specified value.

source

pub fn line_join(&self) -> LineJoin

Returns the current line join for this paint.

source

pub fn set_line_join(&mut self, join: LineJoin)

Sets how sharp path corners are drawn.

By default it’s set to LineJoin::Miter

source

pub fn with_line_join(self, join: LineJoin) -> Self

Returns the paint with the line join set to the specified value.

source

pub fn set_font(&mut self, font_ids: &[FontId])

source

pub fn with_font(self, font_ids: &[FontId]) -> Self

Returns the paint with the font set to the specified value.

source

pub fn font_size(&self) -> f32

Returns the current font size

Only has effect on canvas text operations

source

pub fn set_font_size(&mut self, size: f32)

Sets the font size.

Only has effect on canvas text operations

source

pub fn with_font_size(self, size: f32) -> Self

Returns the paint with the font size set to the specified value.

source

pub fn letter_spacing(&self) -> f32

Returns the current letter spacing

source

pub fn set_letter_spacing(&mut self, spacing: f32)

Sets the letter spacing for this paint

Only has effect on canvas text operations

source

pub fn with_letter_spacing(self, spacing: f32) -> Self

Returns the paint with the letter spacing set to the specified value.

source

pub fn text_baseline(&self) -> Baseline

Returns the current vertical align

source

pub fn set_text_baseline(&mut self, align: Baseline)

Sets the text vertical alignment for this paint

Only has effect on canvas text operations

source

pub fn with_text_baseline(self, align: Baseline) -> Self

Returns the paint with the text vertical alignment set to the specified value.

source

pub fn text_align(&self) -> Align

Returns the current horizontal align

source

pub fn set_text_align(&mut self, align: Align)

Sets the text horizontal alignment for this paint

Only has effect on canvas text operations

source

pub fn with_text_align(self, align: Align) -> Self

Returns the paint with the text horizontal alignment set to the specified value.

source

pub fn fill_rule(&self) -> FillRule

Retrieves the current fill rule setting for this paint

source

pub fn set_fill_rule(&mut self, rule: FillRule)

Sets the current rule to be used when filling a path

https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-rule

source

pub fn with_fill_rule(self, rule: FillRule) -> Self

Returns the paint with the rule for filling a path set to the specified value.

Trait Implementations§

source§

impl Clone for Paint

source§

fn clone(&self) -> Paint

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Paint

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Paint

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Paint

§

impl RefUnwindSafe for Paint

§

impl !Send for Paint

§

impl !Sync for Paint

§

impl Unpin for Paint

§

impl UnwindSafe for Paint

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.