JFLib

diagram_core/src/hit.rs#

Source: lib/diagram_core/src/hit.rs

hit -- hit tests on a page's display list: the shape under a point, shapes in a rectangle (JD7)

  hit::at(&drawing, p, tol) -> Option<Hit>   the topmost shape drawn under p
  hit::all_at(&drawing, p, tol) -> Vec<Hit>  every shape drawn under p, topmost first, each
                                   once (the subselection in groups goes down this list)
  hit::in_rect(&drawing, r, whole) -> Vec<ShapeRef>   the top-level shapes wholly inside r
                                   (whole) or touching it, in z-order (FEATURES ZF-06)
  hit::top_boxes(&drawing) -> Vec<(ShapeRef, [f64; 4])>   each top-level shape's box, in
                                   z-order (alignment guides, selection frames)
  hit::item_at(&item, p, tol) / item_box(&item)   one item: under p?  its exact box
  Hit { shape, top, item }         the shape drawn (a subshape too), its top-level shape
                                   (what a click selects), the item's index

Everything works on render::Drawing: paper inches, y up, the origin at the page's bottom
left corner (the app maps its pixels there: 96 DIP per inch, y down); `tol` is in paper
inches.  Only the page's own shapes count: the items of its background pages (their shapes
belong to another page) and shadows are never hit.  Under a point means: inside a fill
(even-odd; curves cut into straight pieces within a hundredth of the tolerance, at least
1e-4 in), within half its width plus tol of a line (a hairline: tol), inside a text's runs
(each from the descent to the ascent, grown by tol), inside a picture's rectangle where the
shape's box keeps it.  Line ends count with their line, a text background as text.
A box (item_box) is the geometry's without the line width, a text's runs, a picture's
visible part; a top-level shape's box is that of its geometry and line ends, of its texts
where it has no geometry.  The drawing's grid (Drawing::query) finds the candidates: a test
looks at the items near the point or rectangle, not at the whole page.
  • struct Hit a shape under a point

    • shape: ShapeRef -- the shape the item draws (a subshape of a group too)
    • top: ShapeRef -- its top-level shape: what a click selects
    • item: u32 -- the item's index in the drawing
  • fn at(d: &Drawing, p: [f64; 2], tol: f64) -> Option<Hit> the topmost shape drawn under p

  • fn all_at(d: &Drawing, p: [f64; 2], tol: f64) -> Vec<Hit> every shape drawn under p, the topmost first, each shape once (its topmost item)

  • fn in_rect(d: &Drawing, r: [f64; 4], whole: bool) -> Vec<ShapeRef> the top-level shapes of the page wholly inside r (x0, y0, x1, y1; whole) or touching it (not whole), in z-order; a shape that draws nothing is never inside

  • fn top_boxes(d: &Drawing) -> Vec<(ShapeRef, [f64; 4])> the box of each top-level shape of the page that draws something, in z-order: its geometry's and line ends' (without the line width), its texts' where it has no geometry

  • fn item_at(it: &Item, p: [f64; 2], tol: f64) -> bool whether one item is drawn under p (see the module comment)

  • fn item_box(it: &Item) -> [f64; 4] an item's exact box: the geometry's without the line width, a text's runs, a picture's visible part

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