Layout

Layout helpers turn dimensions into inspectable rulers, grids, and repeated cells. They are regular Ruby objects, not another drawing DSL. Applications can inspect and test a layout before creating SVG elements. For PDF and PNG conversion, see Output.

Choose a layout model

Choose by the value the rest of the program needs, not only by the visible repetition:

NeedUseResult
Repeat one SVG template in the documentTile, TileX, or TileY<defs> plus positioned <use> elements
Copy independently editable SVG subtreesDuplicate, DuplicateX, or DuplicateYseparate element trees
Inspect repeated cells and their bounds in RubySumi::Sundries::Tilegeometry values without SVG output
Fit major and minor intervals into one spanRuler or RulerEveninspectable distances and margins
Combine two fitted rulersSVG.Grid or Sumi::Sundries::Gridlines, points, cells, and a fitted canvas

Rulers

Ruler fits whole major intervals inside a span. unit is the smallest step. multiple gives the number of units in one major interval. Requested margins are minimums. Any distance left after fitting whole major intervals is split between them while preserving an asymmetric start/end difference:

ruler = Sumi::Sundries::Ruler.new(
  brut: 103,
  unit: 1,
  multiple: 10,
  margins: [5]
)

ruler.n       # => 9 major intervals
ruler.ds      # major distances
ruler.hs      # halfway distances
ruler.ms      # minor distances
ruler.margins # => [6.5, 6.5]
ruler.d       # => 90.0 fitted distance, excluding margins
ruler.waste   # => 13.0 margins plus unfitted remainder

Use RulerEven when the number of major intervals must be even. The compact readers u, n, and d mean interval length, count, and fitted distance. su, sn, and sd describe the source subinterval.

Grid

Grid combines one horizontal and one vertical ruler. SVG.Grid builds both rulers from a canvas and preserves that canvas's size, unit, and name while replacing its margins with the fitted values:

canvas = SVG.Canvas width: 80, height: 50, margins: [5]
grid = SVG.Grid canvas, unit: 1, multiple: 10

drawing = SVG :inkscape, grid.canvas do
  Draw grid.x.major.lines, class: %w[guide horizontal], stroke: "silver"
  Draw grid.y.major.lines, class: %w[guide vertical], stroke: "silver"
end

drawing.Render

Axis names describe line direction: grid.x produces horizontal lines, and grid.y produces vertical lines. The positions of horizontal lines therefore come from the vertical ruler, and vice versa. Each major, minor, or halfway query has three representations:

ReaderValueTypical use
linesgeometry Line objectspass a complete set to Draw
pointspairs of geometry Point objectsplace marks at both endpoints
xyspairs of plain coordinate Arraysfeed data-only consumers
canvas = SVG.Canvas width: 80, height: 50, margins: [5]
grid = SVG.Grid canvas, unit: 1, multiple: 10

grid.x.major.lines  # geometry Line objects, suitable for Draw
grid.x.halve.points # endpoint Point pairs
grid.y.minor.xys    # plain coordinate pairs

lines keeps one exact geometry snapshot. points and xys apply the active Sumi::F precision when read. Use Sumi::F.with_precision when a data consumer needs a specific number of decimal places.

Grid also inherits tile accessors. Use grid.rowbox(i) or grid.colbox(i) to constrain an operation to one row or column. Guide-sheet consumers use this pattern:

canvas = SVG.Canvas width: 80, height: 50, margins: [5]
grid = SVG.Grid canvas, unit: 1, multiple: 10

SVG :inkscape, grid.canvas do
  Hatch grid.rowbox(0), angle: -30, step: grid.x.su, stroke: "silver"
end.Render

Guidesheets

A guidesheet combines the fitted values instead of recalculating coordinates while drawing. Put the reusable drawing in a callable module, then choose at the call site whether it belongs in an ordinary group or an Inkscape layer:

require "sumi"

canvas = SVG.Canvas width: 90, height: 60
grid = SVG.Grid canvas, unit: 6, multiple: 5

Guidesheet = SVG.Module do
  def call(grid)
    Draw grid.x.minor.lines, stroke: "magenta", "stroke-width": 0.12
    Draw grid.y.minor.lines, stroke: "magenta", "stroke-width": 0.12
    Draw grid.x.major.lines, stroke: "blue", "stroke-width": 0.4
    Draw grid.y.major.lines, stroke: "blue", "stroke-width": 0.4
  end
end

SVG :inkscape, grid.canvas do
  rect width: "100%", height: "100%", fill: "white"
  Layer! Guidesheet, grid, attributes: {id: "Guides"}
end.Render

The callable receives one inspectable layout object. Layer! supplies only placement and editor behavior. The Squared and Copperplate examples share this structure. Copperplate adds a clipped Hatch to every row, while both drawings retain the same fitted frame and major grid. The Ruler example shows the lower-level alternative: ordinary Ruby ranges create tick marks and labels directly when no fitted layout object is needed.

Tiles

Tile arranges copies of one geometry value into rows and columns. The source element's bounding box sets the cell pitch, and indexing is row-first:

cell = Sumi::Geometry::Rect[8, 4]
tile = Sumi::Sundries::Tile.new(cell, position: [10, 20], nx: 3, ny: 2)

tile.at(1, 2).position # direct cell lookup (row 1, column 2)
tile[1][2].position    # row 1, column 2
tile.rowbox(1)         # bounds of the second row
tile.colbox(2)         # bounds of the third column
tile.box               # bounds of the complete layout

Use this Ruby object when later calculations need the cells or their bounds. Use the DSL words Tile, TileX, or TileY when the output needs one SVG template and repeated <use> elements.

at, row, col, [], rowbox, and colbox accept Integer indices. Negative indices count from the end. Out-of-range indices return nil. Other index types raise Sumi::ArgumentError.