JFLib

diagram_core/src/ops.rs#

Source: lib/diagram_core/src/ops.rs

ops -- the editing gestures on shapes: move, size, turn, flip, pin, control points, vertices,
groups, z-order, new shapes, text, copy and paste (JD7: SP-03..07, TX-01, TX-03, DM-02)

  frame(doc, shape) -> Frame          where a shape is: local -> page, Width, Height
  Frame::corners / bounds / centre / grip(gx, gy) / drag(gx, gy, to, keep_aspect)
  bounds(doc, shapes)                 the box of a selection on the page
  locks(eng, doc, shape) -> Locks     the protection cells that are set (Locks::MOVE_X ...)
  move_shapes(tx, eng, shapes, d)     2D: the pin; 1D: BeginX/Y, EndX/Y of the free ends
  shift_all(tx, eng, owner, d)        every top-level shape, locks or not (the page's origin
                                      moves: pagesetup::grow, fit_to_drawing)
  set_frame(tx, eng, shape, &frame)   size and place, same turn (the eight grips)
  scale_shapes(tx, eng, shapes, from, to)   a selection from one box into another
  rotate_shapes(tx, eng, shapes, centre, angle) / flip_shapes(tx, eng, shapes, horizontal)
  set_pin(tx, eng, shape, at)         the pin to another place, the shape stays
  controls / set_control              the yellow control points (SP-04)
  vertices / set_vertex / insert_vertex / delete_vertex     Geometry rows (SP-05)
  move_vertices / delete_vertices, arc_handles / set_arc_point / set_eccentricity   (JD21,
                                      SP-05) several vertices together, the handles of arcs
                                      (ops/arcs.rs)
  group(tx, eng, owner, shapes) / ungroup(tx, eng, group)   (SP-06; JD21: glue between a
                                      guide and shapes breaks unless both are grouped, SP-17;
                                      an instance is made plain first: instance::detach)
  reorder(tx, shapes, Z::Front | Back | Forward | Backward) (SP-07)
  new_shape(tx, eng, owner, Basic::Rectangle | Ellipse | TextBox, a, b)   rectangle and
                                      ellipse with the document's default line, fill and text
                                      styles (DocumentSettings, else "Normal"), a text box with
                                      "Text Only" (DM-04)
  set_text(tx, eng, shape, text) / text_frame / set_text_frame   (TX-01, TX-03)
  copy(doc, shapes) -> Clip / paste(tx, eng, owner, &clip, offset)
  new_freeform / new_arc / simplify / Spline / quarter_arc   (JD8 rest) the freeform and arc
                                      tools: 1-D shapes like a line (ops/draw.rs, RS-16)
  new_picture / probe_picture / PictureInfo   (JD8 rest) a picture file as a Foreign shape
                                      with its bytes as a package part (ops/picture.rs, RE-02)

Every position is on the page in drawing units (PinX of a top-level shape as the file has
it), y up; local coordinates have their origin at the bottom left of the shape's box.  A
gesture runs inside the caller's transaction and becomes one undo step (DM-02):

  let mut tx = doc.transaction("Verschieben");
  ops::move_shapes(&mut tx, &mut eng, &sel, [dx, dy])?;
  let conns = glue::dependents(tx.doc(), owner, &sel);
  glue::update(&mut tx, &mut eng, &conns)?;
  let change = tx.commit();            after undo/redo: eng.refresh(&doc, &change.sheets())

How cells are written (as Visio writes them):
* A value the user gives goes through the rules of a user's edit (Engine::user_set): a GUARD
  formula refuses it and the cell is left alone (the gesture goes on without it), SETATREF
  sends it on; the formula engine recomputes what depends on it once per step.
* A formula of the proportional form keeps its form: a subshape's PinX "Sheet.7!Width*0.25"
  becomes "Sheet.7!Width*<new k>", a LocPinX "Width*0.5" becomes "Width*<k>", a vertex
  "Width*1" "Width*<k>".  Anything else becomes a constant.
* Sizes first, then the pin: the local pin follows its own formula (mostly Width*0.5), and
  the pin goes where the frame puts the local pin.
* The protection cells are asked per shape (a formula is evaluated): LockMoveX/Y keep that
  part of a move away, LockWidth/LockHeight keep a side, LockAspect the proportions,
  LockRotate leaves a shape out of a turn, LockBegin/LockEnd keep an end of a 1D shape on a
  resize; what only makes sense unlocked is refused with EditError::Invalid (text with
  LockTextEdit, vertices with LockVtxEdit, ungrouping with LockGroup).
* Glued ends of a 1D shape (a Connect entry with FromCell BeginX or EndX) are not moved here:
  glue::update routes them.

The transform of a shape (MS-VSDX "Shape Transform"; geometry.rs and formula/geom.rs use the
same): parent = Pin + R(Angle) Flip (local - LocPin), and a subshape continues through its
groups up to the page.  Flipping a turned shape changes its angle: a mirror M times R(a)
equals R(-a) times M, so a horizontal flip toggles FlipX and makes the angle 2φ - a + 180°
(φ the direction of the mirror line in the parent's coordinates, 90° on the page: -a), a
vertical flip toggles FlipY and makes it 2φ - a.
  • struct Frame where a shape is: its local coordinates to the page and its size (a box from (0, 0) to (w, h) in local coordinates; the transform is rigid, flips included)
    • xf: Xform -- local -> page
    • w: f64 -- Width
    • h: f64 -- Height

impl Frame

  • fn from_box(b: [f64; 4]) -> Frame an upright box on the page (x0, y0, x1, y1)

  • fn at(&self, p: [f64; 2]) -> [f64; 2] a local point on the page

  • fn local(&self, p: [f64; 2]) -> Option<[f64; 2]> a page point in local coordinates (None: a degenerate transform)

  • fn corners(&self) -> [[f64; 2]; 4] the corners on the page: (0, 0), (w, 0), (w, h), (0, h)

  • fn bounds(&self) -> [f64; 4] the upright box around the corners (x0, y0, x1, y1)

  • fn centre(&self) -> [f64; 2] the middle of the box on the page

  • fn grip(&self, gx: i8, gy: i8) -> [f64; 2] a grip on the page: gx -1, 0, 1 is the left edge, the middle, the right edge of the local box, gy -1, 0, 1 the bottom, the middle, the top (a flipped shape has its local left edge on the right)

  • fn drag(&self, gx: i8, gy: i8, to: [f64; 2], keep_aspect: bool) -> Frame the frame after grip (gx, gy) is dragged to the page point to: the opposite edges stay, sizes stop at 0; keep_aspect (a corner grip, SP-03) scales both sides by the larger factor and keeps the opposite corner

  • fn frame(doc: &Document, shape: ShapeRef) -> Frame where a shape is on its page (cached values, nothing recomputed)

  • fn bounds(doc: &Document, shapes: &[ShapeRef]) -> Option<[f64; 4]> the upright box of shapes on the page: the corners of 2D shapes, the ends of 1D shapes

  • struct Locks(pub u32) the protection cells of a shape that are set (SS-15), one bit per name of formula::LOCKS

impl Locks

  • const WIDTH: u32 = 1 << 0

  • const HEIGHT: u32 = 1 << 1

  • const ASPECT: u32 = 1 << 2

  • const MOVE_X: u32 = 1 << 3

  • const MOVE_Y: u32 = 1 << 4

  • const ROTATE: u32 = 1 << 5

  • const BEGIN: u32 = 1 << 6

  • const END: u32 = 1 << 7

  • const DELETE: u32 = 1 << 8

  • const SELECT: u32 = 1 << 9

  • const FORMAT: u32 = 1 << 10

  • const TEXT_EDIT: u32 = 1 << 11

  • const VTX_EDIT: u32 = 1 << 12

  • const CROP: u32 = 1 << 13

  • const GROUP: u32 = 1 << 14

  • const CALC_WH: u32 = 1 << 15

  • fn has(self, lock: u32) -> bool whether a lock (one of the constants) is set

  • fn bit(name: &str) -> u32 the bit of a lock cell's name ("LockWidth" ...; 0: not a lock cell)

  • fn locks(eng: &mut Engine, doc: &Document, shape: ShapeRef) -> Locks the protection cells of a shape that are set, with inheritance and formulas

  • fn move_shapes(tx: &mut Transaction, eng: &mut Engine, shapes: &[ShapeRef], d: [f64; 2]) -> Result<(), EditError> move shapes by d (page): a 2D shape's pin, a 1D shape's free ends (glued ones stay for glue::update); a shape inside another one of the list moves with it; LockMoveX/LockMoveY keep that part of the move away

  • fn shift_all(tx: &mut Transaction, eng: &mut Engine, owner: Owner, d: [f64; 2]) -> Result<(), EditError> move every top-level shape of a page or master by d, whatever their locks say: the page's origin moves under the drawing (a page that grows to the left or down, a page fitted to its drawing; pagesetup.rs). Free 1D ends move, glued ones follow their formulas

  • fn set_frame(tx: &mut Transaction, eng: &mut Engine, shape: ShapeRef, to: &Frame) -> Result<(), EditError> give a shape another size and place with the same turn and flips (to from Frame::drag of frame(doc, shape)): Width and Height first, then the pin where the frame puts the local pin; a 1D shape gets its free ends (not glued, no LockBegin/LockEnd) on the frame's line through the local pin. LockWidth, LockHeight keep a side, LockAspect the proportions

  • fn scale_shapes(tx: &mut Transaction, eng: &mut Engine, shapes: &[ShapeRef], from: [f64; 4], to: [f64; 4]) -> Result<(), EditError> scale shapes from the page box from into to (x0, y0, x1, y1; a selection's grips): each shape's middle goes where the boxes map it, its sides grow by how much the boxes stretch them along its own axes (upright shapes exactly, turned ones by their axes); 1D shapes map their ends

  • fn rotate_shapes(tx: &mut Transaction, eng: &mut Engine, shapes: &[ShapeRef], centre: [f64; 2], angle: f64) -> Result<(), EditError> turn shapes by angle (radians, counter-clockwise) about the page point centre: Angle grows (falls for a subshape in a mirrored group), the pin turns along; 1D shapes turn their free ends; LockRotate leaves a shape out

  • fn flip_shapes(tx: &mut Transaction, eng: &mut Engine, shapes: &[ShapeRef], horizontal: bool) -> Result<(), EditError> flip shapes about the middle of their box (horizontal: left and right change places): a horizontal flip toggles FlipX, a vertical one FlipY, a turned shape's angle changes with it (see the module comment), the pin is mirrored; a 1D shape mirrors its free ends and toggles FlipY (its local y axis turns over)

  • fn set_pin(tx: &mut Transaction, eng: &mut Engine, shape: ShapeRef, at: [f64; 2]) -> Result<(), EditError> move the pin of a shape (its centre of rotation) to the page point at without moving the shape: LocPinX/LocPinY get the local point (proportional "Width*k" when their formula has that form), the pin follows

  • fn group(tx: &mut Transaction, eng: &mut Engine, owner: Owner, shapes: &[ShapeRef]) -> Result<ShapeRef, EditError> group shapes of one parent (SP-06): a new group shape (Type Group, the style "Normal", ResizeMode 0) around the shapes, at the place of the topmost of them in the z-order; the box is the upright hull of the shapes in the parent's coordinates (turned shapes with their turned corners, 1D shapes with their ends). The shapes move into it in their order and get Visio's formulas for a group that scales its members: PinX "Sheet.<group>!Width*k", PinY "Sheet.<group>!Height*k", and for upright members (turned by a multiple of 90°) whose size is a constant Width/Height "Sheet.<group>!Width*k" (ResizeMode 1 of the member: only the pin); free ends of 1D members alike, glued ends keep their formulas -> the group

  • fn ungroup(tx: &mut Transaction, eng: &mut Engine, group: ShapeRef) -> Result<Vec<ShapeRef>, EditError> take a group apart (SP-06): its members go to the group's parent at the group's place in the z-order, with the group's pin, angle and flips worked into their own; formulas that name the group become their values, glue to the group is let go (Formula -> value, Connect entry gone), the group shape goes -> the former members. Instances of masters and groups with LockGroup are refused (Invalid)

  • enum Z where shapes go in the z-order among their siblings (SP-07)

    • Front -- on top
    • Back -- below all
    • Forward -- one step up (past the next shape that is not moved too)
    • Backward -- one step down
  • fn reorder(tx: &mut Transaction, shapes: &[ShapeRef], z: Z) -> Result<(), EditError> change the z-order of shapes among their siblings; shapes of several groups move within their own group, moved shapes keep their order among themselves

  • enum Basic the shapes the drawing tools make

    • Rectangle -- the rectangle tool: Geometry RelMoveTo/RelLineTo around the box, style "Normal"
    • Ellipse -- the ellipse tool: one Ellipse row (X Width*0.5, Y Height*0.5, A Width*1, B Height*0.5, C Width*0.5, D Height*1), style "Normal"
    • TextBox -- the text tool: a box without line and fill (style "Text Only", as Visio's text tool gives it; JD11: so that its text keeps the style's dark font style in every design) whose Height follows its text: TEXTHEIGHT(TheText,Width)
  • fn new_shape(tx: &mut Transaction, eng: &mut Engine, owner: Owner, kind: Basic, a: [f64; 2], b: [f64; 2]) -> Result<ShapeRef, EditError> a new shape of a drawing tool between the page points a and b (the dragged box), on top of the owner's shapes; a box without area is refused (Invalid), except for the text tool, which makes a box 1 inch wide around a -> the shape

  • fn set_text(tx: &mut Transaction, eng: &mut Engine, shape: ShapeRef, text: Text) -> Result<(), EditError> give a shape its own text (TX-01) and recompute what depends on it (TEXTWIDTH, TEXTHEIGHT: a text box's Height, TxtHeight ...); LockTextEdit refuses (Invalid)

  • fn text_frame(doc: &Document, shape: ShapeRef) -> Frame where a shape's text block is (TX-03): the text transform (TxtPinX/Y, TxtLocPinX/Y, TxtAngle inside the shape) to the page and TxtWidth/TxtHeight; cells the shape lacks have Visio's defaults (the whole box, turned 0)

  • fn set_text_frame(tx: &mut Transaction, eng: &mut Engine, shape: ShapeRef, to: &Frame) -> Result<(), EditError> move and size a shape's text block (TX-03; to from Frame::drag of text_frame, same turn): TxtWidth and TxtHeight, then TxtPinX/Y where the frame puts the text's local pin (proportional formulas keep their form); LockTextEdit refuses (Invalid)

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