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)
;

context_basic

Implementations

Set accuracy (allowed tolerance) in mm

Default font

Finalize Arrangement

Viewbox helper. Useful to create an arbitrary viewbox for your SVGs.

Helper to create a scaling matrix

Unit matrix. Basically a no-op

Helper to create a translation matrix

Angle is in degrees because I am a terrible person. Also, compass degrees. For an SVG anyhow. I am a bastard.

I can haz a new default drawing context?

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.

Masks any further operations with a clipping polygon. Only items inside the clipping poly will be used.

Sets the mask to Geometry, or None.

Pushes the current context onto the stack.

Pops the previous context off the stack

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.

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.

Adds a geometry to the operations list. Has some checking to make it safe for general users.

Draws a simple line from x0,y0 to x1,y1

Draws a line of text

Glyph Draws a single glyph on the Context, at 0,0

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.

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.

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.

What it says on the box. Draws a simple rectangle on the context.

Draws a polygon

Draws a circle. Actually just buffers a point, and returns a polygon which it draws on the context.

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.

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.

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.

Sets the stroke color

Sets the fill color

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.

Sets the pen width

Set the hatch pattern

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

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.

Generate layers of perimeters and fills

Take this giant complex thing and generate and SVG Document, or an error. Whatever.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Convert the source color to the destination color using the specified method Read more
Convert the source color to the destination color using the bradford method by default Read more
Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Convert into T with values clamped to the color defined bounds Read more
Convert into T. The resulting color might be invalid in its color space Read more
Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Checks if self is actually part of its subset T (and can be converted to it).
Use with care! Same as self.to_subset but without any property checks. Always succeeds.
The inclusion map: converts self to the equivalent element of its superset.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.