pdf/src/canvas.rs#
Source: lib/pdf/src/canvas.rs
canvas -- drawing on one page: paths, paint, transforms, clipping, images, text, links
The canvas writes the page's content stream. Graphics state that only matters when
something is painted (colours, alpha, line width, caps, joins, miter limit, dash, text mode)
is kept as wanted and written just before the paint operator that needs it, so a run of
paths in the same style writes it once. Transforms (`cm`) are written at once; a path
still open when the transform changes keeps where it was drawn. Path construction is
buffered until it is painted, so no state operator ever lands inside a path object.
enum Originwhere a canvas puts (0, 0) and which way y growsTopLeft-- the top left corner of the page, y downwards (as gfx, the screen and page layout)BottomLeft-- the bottom left corner, y upwards (PDF's own page space)
struct Canvas<'a>drawing on one page (Document::canvas). Coordinates are points (1/72 inch) from the origin the canvas was made with; what it draws goes into the page when it is dropped, with every save it left open restored. Several canvases on one page draw one after the other.
impl<'a> Canvas<'a>
fn page(&self) -> PageIdthe page this canvas draws onfn page_size(&self) -> (f64, f64)width and height of the page in pointsfn ctm(&self) -> Matrixthe current transform from this canvas's coordinates to PDF page space (points from the bottom left, y up)fn document(&self) -> &Documentthe document (fonts, metrics, shaping) while drawingfn save(&mut self)saves the whole state (transform, clip, colours, alpha, stroke and text settings)fn restore(&mut self)back to the state of the matching save (none open: nothing happens)fn depth(&self) -> usizehow many saves are openfn transform(&mut self, m: Matrix)m first, then the current transform (singular or not finite: ignored)fn translate(&mut self, tx: f64, ty: f64)fn scale(&mut self, sx: f64, sy: f64)fn rotate(&mut self, angle: f64)radians: counter-clockwise where y points up, clockwise where it points downfn set_fill_color(&mut self, c: Rgb)fn set_stroke_color(&mut self, c: Rgb)fn set_fill_alpha(&mut self, a: f64)opacity of fills, text fills and images, 0 to 1 (kept in thousandths)fn set_stroke_alpha(&mut self, a: f64)opacity of strokes, 0 to 1 (kept in thousandths)fn set_line_width(&mut self, w: f64)in the coordinates at the time of the stroke (0: the thinnest line the device draws)fn set_line_cap(&mut self, cap: LineCap)fn set_line_join(&mut self, join: LineJoin)fn set_miter_limit(&mut self, limit: f64)the longest miter, as a multiple of the line width (at least 1)fn set_dash(&mut self, pattern: &[f64], phase: f64)dashes: lengths on, off, on, ... fromphaseinto the pattern; an empty list (or one with a negative length or only zeros) draws solid linesfn set_text_mode(&mut self, mode: TextMode)fn move_to(&mut self, x: f64, y: f64)fn line_to(&mut self, x: f64, y: f64)fn curve_to(&mut self, x1: f64, y1: f64, x2: f64, y2: f64, x: f64, y: f64)fn quad_to(&mut self, x1: f64, y1: f64, x: f64, y: f64)fn close(&mut self)fn rect(&mut self, x: f64, y: f64, w: f64, h: f64)fn rounded_rect(&mut self, x: f64, y: f64, w: f64, h: f64, r: f64)fn ellipse(&mut self, cx: f64, cy: f64, rx: f64, ry: f64)fn circle(&mut self, cx: f64, cy: f64, r: f64)fn arc(&mut self, cx: f64, cy: f64, rx: f64, ry: f64, start: f64, sweep: f64)Path::arc on the canvas's pathfn append_path(&mut self, p: &Path)a prepared path's subpaths, in the current coordinatesfn fill(&mut self, rule: FillRule)fills the path and starts a new onefn stroke(&mut self)strokes the path and starts a new onefn fill_stroke(&mut self, rule: FillRule)fills, then strokes the path, and starts a new onefn clip(&mut self, rule: FillRule)intersects the clip with the path (until the matching restore) and starts a new pathfn end_path(&mut self)drops the path without painting itfn fill_path(&mut self, p: &Path, rule: FillRule)fn stroke_path(&mut self, p: &Path)fn shade(&mut self, s: &Shading)paints the gradient over the whole current clip (sh), its coordinates in the current user space, times the canvas's fill alpha. Degenerate geometry paints the last stop's colour (axial from = to, radial with both radii 0: everywhere; two equal circles: that disk); no stops, coordinates that are not finite or a negative radius: nothing. The path being built is kept.fn fill_shading(&mut self, rule: FillRule, s: &Shading)fills the path with the gradient (q, clip withrule, sh, Q) and starts a new path, as fill() does; what shade() does with degenerate and invalid gradients, within the path (an invalid one drops the path)fn fill_hatch(&mut self, rule: FillRule, h: &Hatch)fills the path with the hatch's lines (q, clip withrule, the lines stroked, Q) and starts a new path, as fill() does. Lines at least as wide as their spacing fill the path solid; an angle, spacing, width or origin that is not finite (or a spacing or width <= 0), a path with coordinates that are not finite or more than 100 000 lines: nothing, the path droppedfn image(&mut self, img: ImageId, x: f64, y: f64, w: f64, h: f64)the image upright (as it looks on the page) in the rectangle x, y, w, h of the current coordinatesfn image_transform(&mut self, img: ImageId, m: Matrix)the image's unit square (row 0 along y = 1, column 0 along x = 0) mapped by m into the current coordinatesfn text(&mut self, font: FontId, size: f64, x: f64, y: f64, s: &str) -> f64one line of text shaped with the font (kerning, ligatures, marks, bidi), its baseline starting at (x, y),sizethe em in points; upright as it looks on the page. Returns the advance along the baseline (0 when nothing was drawn).fn text_fallback(&mut self, fonts: &[FontId], size: f64, x: f64, y: f64, s: &str) -> f64the same with fallback fonts: a run of one script the first font cannot show comes from the first of the others that canfn glyphs(&mut self, fonts: &[FontId], size: f64, x: f64, y: f64, glyphs: &[Glyph], text: &str) -> f64shaped glyphs (in visual order;Glyph::fontindexesfonts) from the pen at (x, y);textis the shaped string (each glyph's cluster is a byte range of it) for search and copy. Returns the advance.fn link_uri(&mut self, x: f64, y: f64, w: f64, h: f64, uri: &str)a link on the rectangle x, y, w, h (its box on the page when turned) to a URIfn link_dest(&mut self, x: f64, y: f64, w: f64, h: f64, dest: Dest)a link on the rectangle x, y, w, h to a place in the documentfn dest(&self, x: f64, y: f64) -> Desta destination showing the point (x, y) of this canvas at the top left of the window, the zoom unchanged