Module: Sumi::Toplevel

Included in:
Sumi
Defined in:
toplevel/lib/sumi/toplevel.rb,
toplevel/lib/sumi/toplevel/derender.rb,
toplevel/lib/sumi/toplevel/executor.rb,
toplevel/lib/sumi/toplevel/function.rb,
toplevel/lib/sumi/toplevel/geometry.rb,
toplevel/lib/sumi/toplevel/graphics.rb,
toplevel/lib/sumi/toplevel/sundries.rb

Overview

Shared implementation for the full Sumi top-level DSL.

This module is installed by include Sumi or extend Sumi. Consumers do not normally be included directly.

See Also:

Instance Method Summary collapse

Instance Method Details

#Canvas(paper, **overrides) ⇒ Sumi::Graphics::Canvas #Canvas(width:, height:, unit: "mm", name: :custom, margins: []) ⇒ Sumi::Graphics::Canvas

Examples:

Build a paper-backed canvas

canvas = Sumi.Canvas :a4, margins: [12, 10]

Overloads:

  • #Canvas(paper, **overrides) ⇒ Sumi::Graphics::Canvas

    Builds a canvas from a paper profile with optional field overrides.

    Parameters:

    • paper (Sumi::Graphics::Paper, Symbol, String) —

      paper object or registered profile

    • overrides (Hash) —

      canvas field overrides

    Returns:

    Raises:

  • #Canvas(width:, height:, unit: "mm", name: :custom, margins: []) ⇒ Sumi::Graphics::Canvas

    Builds a canvas from an explicit size.

    Parameters:

    • width (Numeric) —

      canvas width

    • height (Numeric) —

      canvas height

    • unit (Symbol, String) (defaults to: "mm") —

      SVG unit

    • name (Symbol, String) (defaults to: :custom) —

      paper name

    • margins (Array<Numeric>) (defaults to: []) —

      margin shorthand values

    Returns:

    Raises:

See Also:



26
# File 'toplevel/lib/sumi/toplevel/graphics.rb', line 26

def Canvas(...) = Graphics.canvas(...)

#Decompile(content, id: nil, omit: nil) ⇒ Sumi::Derender::Node

Converts inline SVG/XML content into a derender node.

Parameters:

  • content (String) —

    SVG/XML source content

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

Raises:

See Also:



16
# File 'toplevel/lib/sumi/toplevel/derender.rb', line 16

def Decompile(content, id: nil, omit: nil) = Derender.decompile(content, id:, omit:)

#DecompileFile(file, id: nil, omit: nil) ⇒ Sumi::Derender::Node

Converts an SVG/XML file into a derender node.

Parameters:

  • file (String) —

    path to the source SVG/XML file

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when the file is absent, malformed, or rootless, or the id is absent

  • (SystemCallError) —

    when the file cannot be read

See Also:



28
# File 'toplevel/lib/sumi/toplevel/derender.rb', line 28

def DecompileFile(file, id: nil, omit: nil) = Derender.decompile_file(file, id:, omit:)

#Derender(content, id: nil, omit: nil) ⇒ String

Converts inline SVG/XML content into Sumi DSL Ruby source. The returned String is ordinary Ruby source. Review and integrate it statically rather than using Ruby's raw dynamic evaluation methods.

Parameters:

  • content (String) —

    SVG/XML source content

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

  • (String) —

    formatted Sumi DSL source

Raises:

See Also:



41
# File 'toplevel/lib/sumi/toplevel/derender.rb', line 41

def Derender(content, id: nil, omit: nil) = Derender.derender(content, id:, omit:)

#DerenderFile(file, id: nil, omit: nil) ⇒ String

Converts an SVG/XML file into Sumi DSL Ruby source. The returned String is ordinary Ruby source. Review and integrate it statically rather than using Ruby's raw dynamic evaluation methods.

Parameters:

  • file (String) —

    path to the source SVG/XML file

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

  • (String) —

    formatted Sumi DSL source

Raises:

  • (Sumi::ArgumentError) —

    when the file is absent, malformed, or rootless, or the id is absent

  • (Sumi::PanicError) —

    when generated Ruby source cannot be formatted

  • (SystemCallError) —

    when the file cannot be read

See Also:



55
# File 'toplevel/lib/sumi/toplevel/derender.rb', line 55

def DerenderFile(file, id: nil, omit: nil) = Derender.derender_file(file, id:, omit:)

#Document(name) ⇒ Class #Document(name, preambles: Undefined, attributes: Undefined) ⇒ Class #Document(preambles: Undefined, attributes: Undefined) ⇒ Class

Returns document class.

Examples:

Define and use a document profile

Sumi.Document :icon, attributes: {viewBox: "0 0 24 24"}
drawing = Sumi.SVG(:icon) { circle cx: 12, cy: 12, r: 10 }

Overloads:

  • #Document(name) ⇒ Class

    Looks up an existing document profile by name.

    Parameters:

    • name (Symbol, String) —

      profile name

    Returns:

    • (Class) —

      document class

    Raises:

  • #Document(name, preambles: Undefined, attributes: Undefined) ⇒ Class

    Defines or validates a named document profile.

    Parameters:

    • name (Symbol, String) —

      profile name

    • preambles (Array<String>, nil, Sumi::Undefined) (defaults to: Undefined) —

      document preamble lines

    • attributes (Hash, nil, Sumi::Undefined) (defaults to: Undefined) —

      default root attributes

    Returns:

    • (Class) —

      document class

    Raises:

  • #Document(preambles: Undefined, attributes: Undefined) ⇒ Class

    Defines an anonymous document profile without registering it globally.

    Parameters:

    • preambles (Array<String>, nil, Sumi::Undefined) (defaults to: Undefined) —

      document preamble lines

    • attributes (Hash, nil, Sumi::Undefined) (defaults to: Undefined) —

      default root attributes

    Returns:

    • (Class) —

      anonymous document class

    Raises:

Returns:

  • (Class) —

    document class

See Also:



52
# File 'toplevel/lib/sumi/toplevel/graphics.rb', line 52

def Document(...) = Graphics.document(...)

#Document!(name, preambles: [], attributes: {}) ⇒ Class

Defines or replaces a document profile.

Parameters:

  • name (Symbol, String) —

    profile name

  • preambles (Array<String>, nil) (defaults to: []) —

    document preamble lines

  • attributes (Hash, nil) (defaults to: {}) —

    default root attributes

Returns:

  • (Class) —

    document class

Raises:

See Also:



62
63
64
# File 'toplevel/lib/sumi/toplevel/graphics.rb', line 62

def Document!(name, preambles: [], attributes: {})
  Graphics.document!(name, preambles:, attributes:)
end

#Evaluate(content, element, id: nil, omit: nil) ⇒ Sumi::Graphics::Element?

Evaluates inline SVG/XML content under a graphics element, including the selected node.

Parameters:

  • content (String) —

    SVG/XML source content

  • element (Sumi::Graphics::Element) —

    target graphics element

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

Raises:

See Also:



67
# File 'toplevel/lib/sumi/toplevel/derender.rb', line 67

def Evaluate(content, element, id: nil, omit: nil) = Derender.evaluate(content, element, id:, omit:)

#EvaluateChildren(content, element, id: nil, omit: nil) ⇒ Array<Sumi::Graphics::Element>

Evaluates only the selected node's children from inline SVG/XML content under a graphics element.

Parameters:

  • content (String) —

    SVG/XML source content

  • element (Sumi::Graphics::Element) —

    target graphics element

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

Raises:

See Also:



79
80
81
# File 'toplevel/lib/sumi/toplevel/derender.rb', line 79

def EvaluateChildren(content, element, id: nil, omit: nil)
  Derender.evaluate_children(content, element, id:, omit:)
end

#EvaluateChildrenFile(file, element, id: nil, omit: nil) ⇒ Array<Sumi::Graphics::Element>

Evaluates only the selected node's children from an SVG/XML file under a graphics element.

Parameters:

  • file (String) —

    path to the source SVG/XML file

  • element (Sumi::Graphics::Element) —

    target graphics element

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when the file is absent, malformed, or rootless, or the id is absent

  • (SystemCallError) —

    when the file cannot be read

See Also:



94
95
96
# File 'toplevel/lib/sumi/toplevel/derender.rb', line 94

def EvaluateChildrenFile(file, element, id: nil, omit: nil)
  Derender.evaluate_children_file(file, element, id:, omit:)
end

#EvaluateFile(file, element, id: nil, omit: nil) ⇒ Sumi::Graphics::Element?

Evaluates an SVG/XML file under a graphics element, including the selected node.

Parameters:

  • file (String) —

    path to the source SVG/XML file

  • element (Sumi::Graphics::Element) —

    target graphics element

  • id (String, Symbol, nil) (defaults to: nil) —

    optional SVG id selecting a node inside the source

  • omit (String, Symbol, Array<String, Symbol>, nil) (defaults to: nil) —

    exact attribute name or names omitted from the selected subtree after id selection

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when the file is absent, malformed, or rootless, or the id is absent

  • (SystemCallError) —

    when the file cannot be read

See Also:



109
# File 'toplevel/lib/sumi/toplevel/derender.rb', line 109

def EvaluateFile(file, element, id: nil, omit: nil) = Derender.evaluate_file(file, element, id:, omit:)

#Grid(canvas, unit:, multiple:) ⇒ Sumi::Sundries::Grid

Builds a drawable grid fitted inside a graphics canvas.

The canvas margins are minimum clearances. Any span left after fitting whole major intervals is shared equally between the opposite margins, so their requested difference is preserved. The returned grid starts at (0, 0). Sundries::Grid#canvas exposes the fitted page margins.

Parameters:

  • canvas (Sumi::Graphics::Canvas) —

    canvas defining page size and margins

  • unit (Numeric) —

    minor grid unit

  • multiple (Integer) —

    number of minor units in each major interval

Returns:

Raises:

See Also:



34
35
36
37
38
39
40
41
42
# File 'toplevel/lib/sumi/toplevel/sundries.rb', line 34

def Grid(canvas, unit:, multiple:)
  ArgumentError.("Must be a Canvas: #{canvas}") unless canvas.is_a?(Graphics::Canvas)

  Sundries::Grid.new(
    x: Sundries::Ruler.new(brut: canvas.width, unit:, multiple:, margins: [canvas.left, canvas.right]),
    y: Sundries::Ruler.new(brut: canvas.height, unit:, multiple:, margins: [canvas.top, canvas.bottom]),
    canvas:
  )
end

#Load(*files) ⇒ Array<String>

Note:

Load resolves against the active executor scope in the current fiber.

Note:

Ordinary library code uses Ruby require. Load is available only during Sumi script execution.

Loads one or more Sumi files relative to the caller's source file.

Parameters:

  • files (Array<String>) —

    Sumi script files to locate and execute

Returns:

  • (Array<String>) —

    the input file list

Raises:

See Also:



93
94
95
96
97
98
99
100
101
# File 'toplevel/lib/sumi/toplevel/executor.rb', line 93

def Load(*files)
  start = ::Sumi.__send__(:load_directory, caller_locations(1))

  files.each do |file|
    location = F.locate(file, start)

    ::Sumi::Executor.__send__(:load, location.file)
  end
end

#Mixin(mod, document = Sumi::Graphics::Document::Base) ⇒ nil #Mixin(mod, document = Sumi::Graphics::Document::Base) { ... } ⇒ Module #Mixin(document = Sumi::Graphics::Document::Base) { ... } ⇒ Module

Overloads:

  • #Mixin(mod, document = Sumi::Graphics::Document::Base) ⇒ nil

    Adds a named graphics mixture to a document class.

    Parameters:

    • mod (Symbol, String) —

      named mixture to mix into the document

    • document (Class) (defaults to: Sumi::Graphics::Document::Base) —

      document class receiving the mixture

    Returns:

    • (nil)

    Raises:

    • (NameError) —

      when a named mixture cannot be resolved

    See Also:

  • #Mixin(mod, document = Sumi::Graphics::Document::Base) { ... } ⇒ Module

    Adds a named graphics mixture and an anonymous extension to a document class.

    Parameters:

    • mod (Symbol, String) —

      named mixture to mix into the document

    • document (Class) (defaults to: Sumi::Graphics::Document::Base) —

      document class receiving the mixture

    Yields:

    • optional anonymous mixture module body

    Yield Returns:

    • (void)

    Returns:

    • (Module) —

      anonymous mixture

    Raises:

    • (NameError) —

      when a named mixture cannot be resolved

    See Also:

  • #Mixin(document = Sumi::Graphics::Document::Base) { ... } ⇒ Module

    Adds an anonymous graphics mixture to a document class.

    Parameters:

    • document (Class) (defaults to: Sumi::Graphics::Document::Base) —

      document class receiving the mixture

    Yields:

    • anonymous mixture module body

    Yield Returns:

    • (void)

    Returns:

    • (Module) —

      anonymous mixture

    Raises:

    See Also:



109
# File 'toplevel/lib/sumi/toplevel/graphics.rb', line 109

def Mixin(...) = Graphics::Mixtures.mixin(...)

#Paper(width, height, name = :custom, unit: "mm") ⇒ Symbol, String

Defines or validates a named paper profile for DSL use.

Parameters:

  • width (Numeric) —

    paper width

  • height (Numeric) —

    paper height

  • name (Symbol, String) (defaults to: :custom) —

    paper profile name

  • unit (String, Symbol) (defaults to: "mm") —

    size unit

Returns:

  • (Symbol, String) —

    the original paper profile name

Raises:

See Also:



121
# File 'toplevel/lib/sumi/toplevel/graphics.rb', line 121

def Paper(...) = Graphics.paper(...)

#Paper!(width, height, name = :custom, unit: "mm") ⇒ Symbol, String

Defines or overwrites a named paper profile for DSL use.

Parameters:

  • width (Numeric) —

    paper width

  • height (Numeric) —

    paper height

  • name (Symbol, String) (defaults to: :custom) —

    paper profile name

  • unit (String, Symbol) (defaults to: "mm") —

    size unit

Returns:

  • (Symbol, String) —

    the original paper profile name

Raises:

See Also:



133
# File 'toplevel/lib/sumi/toplevel/graphics.rb', line 133

def Paper!(...) = Graphics.paper!(...)

#Ruler(brut:, unit:, multiple:, margins: [0.0]) ⇒ Sumi::Sundries::Ruler

Fits major and minor intervals into a span without drawing SVG elements.

Returns fitted ruler.

Parameters:

  • brut (Numeric) —

    full available span

  • unit (Numeric) —

    minor interval length

  • multiple (Integer) —

    minor intervals per major interval

  • margins (Array<Numeric>) (defaults to: [0.0]) —

    one symmetric or two start/end minimum margins

Returns:

Raises:

See Also:



16
# File 'toplevel/lib/sumi/toplevel/sundries.rb', line 16

def Ruler(...) = Sundries::Ruler.new(...)

#SVG(document = :default, canvas = Undefined, **attributes) { ... } ⇒ Sumi::Graphics::Document::Proto

Builds an SVG document through the full Sumi top-level DSL.

Parameters:

Yields:

  • the document block evaluated in the SVG document context

Yield Returns:

  • (void)

Returns:

Raises:

See Also:

  • SVG
  • Graphics.SVG


78
79
80
# File 'toplevel/lib/sumi/toplevel/graphics.rb', line 78

def SVG(document = :default, canvas = Undefined, **attributes, &block)
  Graphics.SVG(document, canvas, **attributes, &block)
end