Struct aoer_plotty_rs::context::Context
source · [−]pub struct Context { /* private fields */ }
Expand description
Context
A Context is a drawing context, used to perform operations against a pseudo-canvas. Those operations are later collected up and turned into an SVG, including line strokes, fills, and all the other useful tools that we need to drive a plotter robot.
Example
use aoer_plotty_rs::context::Context;
let mut ctx = Context::new();
ctx.stroke("black")
.fill("red")
.pen(0.5)
.outline(Some(5.0))
.poly(vec![(0.0,0.0),
(25.0,0.0),
(25.0,25.0),
(0.0,25.0)],
vec![])
.outline(None)
.hatch(135.0)
.stroke("blue")
.fill("yellow")
.circle(12.5,12.5, 5.0)
.push()
.hatch(180.0)
.stroke("red")
.fill("green")
.circle(17.5,12.5,2.5)
.pop().unwrap()
.hatch(0.0)
.clip(true)
.circle(7.5,12.5,2.5)
.clip(false)
.stroke("brown")
.pen(1.0)
.line(0.0, 0.0, 3.0, 3.0)
.pen(0.1)
.outline(Some(1.0))
.stroke("pink")
.line(3.0, 0.0, 0.0, 3.0)
.stroke("purple")
.spline(&vec![(0.0, 25.0), (0.0, 25.0), (10.0, 20.0), (20.0,25.0), (25.0, 25.0)],
8, 0.5)
.push() // Prepare for this transformation stuff...
.transform(Some(
&(Context::translate_matrix(25.0, 25.0)
* Context::rotate_matrix(45.0)
* Context::scale_matrix(1.0, 0.5)
))) // Holy crap we can multiply these?! ;)
.stroke("cyan")
.circle(0.0, 0.0, 8.0)
.pop().unwrap() // We're back to purple and regular coords
.outline(None)
.stroke("green")
.regular_poly(8, 80.0, 80.0, 20.0, 0.0)
.star_poly(5, 30.0, 80.0, 10.0, 20.0, 0.0)
;
Implementations
sourceimpl Context
impl Context
sourcepub fn default_font() -> Font
pub fn default_font() -> Font
Default font
sourcepub fn finalize_arrangement(
&self,
arrangement: &Arrangement<f64>
) -> Arrangement<f64>
pub fn finalize_arrangement(
&self,
arrangement: &Arrangement<f64>
) -> Arrangement<f64>
Finalize Arrangement
sourcepub fn viewbox(x0: f64, y0: f64, x1: f64, y1: f64) -> Rect<f64>
pub fn viewbox(x0: f64, y0: f64, x1: f64, y1: f64) -> Rect<f64>
Viewbox helper. Useful to create an arbitrary viewbox for your SVGs.
sourcepub fn unit_matrix() -> Affine2<f64>
pub fn unit_matrix() -> Affine2<f64>
Unit matrix. Basically a no-op
sourcepub fn translate_matrix(tx: f64, ty: f64) -> Affine2<f64>
pub fn translate_matrix(tx: f64, ty: f64) -> Affine2<f64>
Helper to create a translation matrix
sourcepub fn rotate_matrix(degrees: f64) -> Affine2<f64>
pub fn rotate_matrix(degrees: f64) -> Affine2<f64>
Angle is in degrees because I am a terrible person. Also, compass degrees. For an SVG anyhow. I am a bastard.
sourcepub fn bounds(&self) -> Result<Rect<f64>, Box<dyn Error>>
pub fn bounds(&self) -> Result<Rect<f64>, Box<dyn Error>>
Bounds returns a Rect defining the bounds of all operations drawn on the context. Note: Since this has to iterate over ALL geometry in the drawing, it’s kind of expensive. I’ll probably cache this per operation at some point, but for now it’s pricy.
sourcepub fn mask_poly(
&mut self,
exterior: Vec<(f64, f64)>,
interiors: Vec<Vec<(f64, f64)>>
) -> &mut Self
pub fn mask_poly(
&mut self,
exterior: Vec<(f64, f64)>,
interiors: Vec<Vec<(f64, f64)>>
) -> &mut Self
Masks any further operations with a clipping polygon. Only items inside the clipping poly will be used.
pub fn mask_box(&mut self, x0: f64, y0: f64, x1: f64, y1: f64) -> &mut Self
sourcepub fn set_mask(&mut self, mask: &Option<Geometry<f64>>) -> &mut Self
pub fn set_mask(&mut self, mask: &Option<Geometry<f64>>) -> &mut Self
Sets the mask to Geometry, or None.
sourcepub fn pop(&mut self) -> Result<&mut Self, ContextError>
pub fn pop(&mut self) -> Result<&mut Self, ContextError>
Pops the previous context off the stack
sourcepub fn transform(&mut self, transformation: Option<&Affine2<f64>>) -> &mut Self
pub fn transform(&mut self, transformation: Option<&Affine2<f64>>) -> &mut Self
Set the transformation matrix for subsequent ops. Take a look at the transformation
helpers (crate::context::Context::scale_matrix
,
crate::context::Context::translate_matrix
, and
crate::context::Context::rotate_matrix
, which are great
if you don’t want to generate your own unsafe Affine2 transformations. Also, usefully,
these transformations can be COMPOSED via multiplication. Note that the order of the
compositions is right-to-left, so the last in the chain of multiplications is the
first one to be performed. See the example in context_basic.rs for more info.
sourcepub fn mul_transform(&mut self, transformation: &Affine2<f64>) -> &mut Self
pub fn mul_transform(&mut self, transformation: &Affine2<f64>) -> &mut Self
Similar to transform, but multiplies the CURRENT transformation matrix by the new one. If the current matrix is None, then multiplies by the UNIT matrix. This is really useful for stepping through relative positions, or rotations. Couples well with push/pop to make an addition relative to current matrix, then resetting to origin.
sourcepub fn geometry(&mut self, geometry: &Geometry<f64>) -> &mut Self
pub fn geometry(&mut self, geometry: &Geometry<f64>) -> &mut Self
Adds a geometry to the operations list. Has some checking to make it safe for general users.
sourcepub fn line(&mut self, x0: f64, y0: f64, x1: f64, y1: f64) -> &mut Self
pub fn line(&mut self, x0: f64, y0: f64, x1: f64, y1: f64) -> &mut Self
Draws a simple line from x0,y0 to x1,y1
sourcepub fn typography(
&mut self,
text: &String,
x0: f64,
y0: f64,
typography: &Typography
) -> &mut Self
pub fn typography(
&mut self,
text: &String,
x0: f64,
y0: f64,
typography: &Typography
) -> &mut Self
Draws a line of text
sourcepub fn glyph(&mut self, glyph: char, close: bool) -> &mut Self
pub fn glyph(&mut self, glyph: char, close: bool) -> &mut Self
Glyph Draws a single glyph on the Context, at 0,0
sourcepub fn path(&mut self, bezier: &BezPath) -> &mut Self
pub fn path(&mut self, bezier: &BezPath) -> &mut Self
Way more useful path interface. Uses Kurbo’s BezierPath module. After creation, uses GEOS polygonize_full to generate polygons and line segments from the drawing, ensuring that we can have filled geometry as an output.
sourcepub fn spline(
&mut self,
points: &Vec<(f64, f64)>,
num_interpolated_segments: u32,
tension: f64
) -> &mut Self
pub fn spline(
&mut self,
points: &Vec<(f64, f64)>,
num_interpolated_segments: u32,
tension: f64
) -> &mut Self
Generates a spline from a set of points and renders as a multi line string. Doesn’t do errors very well, just silently fails to draw. First and last point in points are NOT drawn, and set the ‘tension’ points which the line pulls from.
sourcepub fn arc_center(
&mut self,
x0: f64,
y0: f64,
radius: f64,
deg0: f64,
deg1: f64
) -> &mut Self
pub fn arc_center(
&mut self,
x0: f64,
y0: f64,
radius: f64,
deg0: f64,
deg1: f64
) -> &mut Self
centerpoint arc Draw an arc around x0,y0 with the given radius, from deg0 to deg1. Arcs will always be coords oriented clockwise from “north” on an SVG. ie: 45 to 135 will be NE to SE.
sourcepub fn rect(&mut self, x0: f64, y0: f64, x1: f64, y1: f64) -> &mut Self
pub fn rect(&mut self, x0: f64, y0: f64, x1: f64, y1: f64) -> &mut Self
What it says on the box. Draws a simple rectangle on the context.
sourcepub fn poly(
&mut self,
exterior: Vec<(f64, f64)>,
interiors: Vec<Vec<(f64, f64)>>
) -> &mut Self
pub fn poly(
&mut self,
exterior: Vec<(f64, f64)>,
interiors: Vec<Vec<(f64, f64)>>
) -> &mut Self
Draws a polygon
sourcepub fn circle(&mut self, x0: f64, y0: f64, radius: f64) -> &mut Self
pub fn circle(&mut self, x0: f64, y0: f64, radius: f64) -> &mut Self
Draws a circle. Actually just buffers a point, and returns a polygon which it draws on the context.
sourcepub fn regular_poly(
&mut self,
sides: usize,
x: f64,
y: f64,
radius: f64,
rotation: f64
) -> &mut Self
pub fn regular_poly(
&mut self,
sides: usize,
x: f64,
y: f64,
radius: f64,
rotation: f64
) -> &mut Self
Circumscribed regular polygon. The vertices of the polygon will be situated on a circle defined by the given radius. Polygon will be centered at x,y.
sourcepub fn star_poly(
&mut self,
sides: usize,
x: f64,
y: f64,
inner_radius: f64,
outer_radius: f64,
rotation: f64
) -> &mut Self
pub fn star_poly(
&mut self,
sides: usize,
x: f64,
y: f64,
inner_radius: f64,
outer_radius: f64,
rotation: f64
) -> &mut Self
Regular star polygon. This is effectively a star shape with the number of points indicated, and with inner and outer radiuses which correspond to the valleys and tips of the star respectively. Note: this is not a star polygon in the strict mathematical sense. This is just a polygon that is in the shape of a star. I may or may not get to regular star polygons (in the canonical mathematical sense) at some point.
sourcepub fn clip(&mut self, clip: bool) -> &mut Self
pub fn clip(&mut self, clip: bool) -> &mut Self
Sets the clipping state. Any subsequent objects will clip their predecessors. Note that this is an EXPENSIVE operation, so you might want to leave it off if you’re sure you won’t have intersections.
sourcepub fn hatch(&mut self, angle: f64) -> &mut Self
pub fn hatch(&mut self, angle: f64) -> &mut Self
Sets the hatch state, either None for no hatching or Some(angle) to set a hatching angle. Will use the current pen width as the spacing between hatch lines.
sourcepub fn outline(&mut self, stroke: Option<f64>) -> &mut Self
pub fn outline(&mut self, stroke: Option<f64>) -> &mut Self
Neat option. Instead of drawing a complex poly, why not just set outline, and have the lines/polys/whatever you subsequently draw get buffered and hatched to imitate a really thick pen? I knew you’d like this one :D
sourcepub fn flatten(&self) -> Self
pub fn flatten(&self) -> Self
Flatten will take a context and “flatten” together all polygons of a given color and “depth”. What that means is that we watch for changes to fill/color/etc, and set those as boundaries. Then every geometry within a set of boundaries is flattened as “unions” into a single geometry. This is nice because overlapping polygons get turned into a single unified polygon, and their fills are no longer disjoint (and they don’t have unexpected overlapping boundary lines). See the 10_hello example for more details. Unlike the other methods, this one generates an entirely new context including a NEW HISTORY, so you can’t use push/pop to go back, and the individual operations are (obviously) lost.
pub fn to_geo(&self) -> Result<Geometry<f64>, Box<dyn Error>>
sourcepub fn to_layers(&self) -> Vec<OPLayer>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn to_layers(&self) -> Vec<OPLayer>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
Generate layers of perimeters and fills
sourcepub fn to_svg(
&self,
arrangement: &Arrangement<f64>
) -> Result<Document, ContextError>
pub fn to_svg(
&self,
arrangement: &Arrangement<f64>
) -> Result<Document, ContextError>
Take this giant complex thing and generate and SVG Document, or an error. Whatever.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Context
impl !Send for Context
impl !Sync for Context
impl Unpin for Context
impl UnwindSafe for Context
Blanket Implementations
sourceimpl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Component + Float,
Swp: WhitePoint,
Dwp: WhitePoint,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Component + Float,
Swp: WhitePoint,
Dwp: WhitePoint,
D: AdaptFrom<S, Swp, Dwp, T>,
sourcefn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<Swp, Dwp, T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<Swp, Dwp, T>,
sourcefn adapt_into(self) -> D
fn adapt_into(self) -> D
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T, U> ConvertInto<U> for Twhere
U: ConvertFrom<T>,
impl<T, U> ConvertInto<U> for Twhere
U: ConvertFrom<T>,
sourcefn convert_into(self) -> U
fn convert_into(self) -> U
sourcefn convert_unclamped_into(self) -> U
fn convert_unclamped_into(self) -> U
sourcefn try_convert_into(self) -> Result<U, OutOfBounds<U>>
fn try_convert_into(self) -> Result<U, OutOfBounds<U>>
OutOfBounds
error is returned which contains the unclamped color. Read moreimpl<T> Pointable for T
impl<T> Pointable for T
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read morefn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.