JFLib

pdf/src/shading.rs#

Source: lib/pdf/src/shading.rs

shading -- gradients (axial and radial shadings, ISO 32000-1 8.7.4.5) and hatches for
Canvas::shade, fill_shading and fill_hatch

A gradient becomes a shading dictionary of type 2 (axial) or 3 (radial) in DeviceRGB.  Its
colour function: one Type 2 function (exponential, N 1) from colour to colour, or for more stops
a Type 3 stitching function of such pieces, one per stretch between two stops; stops at the same
t (a hard edge) leave out the empty stretch between them, so Bounds always rise.  Before the
first stop and after the last the colour stays the first / last stop's.  The stops' alpha:
all the same -> the constant fill alpha (ExtGState ca); different -> a luminosity soft mask whose
group paints the same geometry as a DeviceGray shading of the alphas (Canvas builds it).

Hatches are stroked lines, not tiling patterns: pdfium draws the cells of a rotated tiling
pattern with hairline gaps and one-pixel steps where they meet, and both pdfium and Poppler round
the cell step to whole device pixels, so the lines drift from where they belong.  A hatch clips
to the path and strokes parallel lines over its bounding box in one path (one `S`, so a hatch's
alpha applies once).
  • enum ShadingKind the geometry of a gradient, in the user space of the canvas at the time it is painted

    • Axial { from: [f64; 2], to: [f64; 2] } -- colour runs from from (t = 0) to to (t = 1), constant across (PDF type 2)
    • Radial { from: [f64; 2], r0: f64, to: [f64; 2], r1: f64 } -- circles from (from, r0) at t = 0 to (to, r1) at t = 1 (PDF type 3); radii >= 0
  • struct Shading a gradient for Canvas::shade and Canvas::fill_shading

    • kind: ShadingKind
    • stops: Vec<(f64, Rgb, f64)> -- (t in 0..=1 ascending, colour, alpha 0..=1); at least one stop. Stops with a NaN t are dropped, t is clamped to 0..=1 and the stops sorted by it (stable: two stops at the same t make a hard edge); a NaN alpha is opaque
    • extend: (bool, bool) -- paint beyond t < 0 / t > 1 with the end colours (PDF /Extend), default (true, true)

impl Shading

  • fn axial(from: [f64; 2], to: [f64; 2], stops: &[(f64, Rgb, f64)]) -> Shading an axial gradient from from to to, extended at both ends

  • fn radial(from: [f64; 2], r0: f64, to: [f64; 2], r1: f64, stops: &[(f64, Rgb, f64)]) -> Shading a radial gradient from the circle (from, r0) to (to, r1), extended at both ends

  • struct Hatch parallel lines for Canvas::fill_hatch; two hatches over the same path cross

    • angle: f64 -- direction of the lines in the canvas's user space, radians from +x towards +y: counter- clockwise on the page where y points up (Origin::BottomLeft), clockwise where it points down (Origin::TopLeft, as Canvas::rotate)
    • spacing: f64 -- distance between neighbouring lines (user space units), > 0
    • width: f64 -- line width (user space units), > 0; at least spacing fills the path solid
    • color: Rgb
    • alpha: f64 -- 0 to 1, times the canvas's fill alpha
    • origin: [f64; 2] -- a point one of the lines runs through (user space)

impl Hatch

  • fn new(angle: f64, spacing: f64, width: f64, color: Rgb) -> Hatch opaque lines through (0, 0)

Source: docs/API.md in the JFLib repository.