Module: Sumi::Graphics

Extended by:
Graphics
Included in:
Graphics
Defined in:
graphics/lib/sumi/graphics.rb,
graphics/lib/sumi/graphics/element.rb,
graphics/lib/sumi/graphics/version.rb,
graphics/lib/sumi/graphics/document.rb,
graphics/lib/sumi/graphics/mixtures.rb,
graphics/lib/sumi/graphics/attribute.rb,
graphics/lib/sumi/graphics/mixtures/rdf.rb,
graphics/lib/sumi/graphics/auxiliary/xml.rb,
graphics/lib/sumi/graphics/document/base.rb,
graphics/lib/sumi/graphics/document/html.rb,
graphics/lib/sumi/graphics/mixtures/call.rb,
graphics/lib/sumi/graphics/mixtures/core.rb,
graphics/lib/sumi/graphics/mixtures/lint.rb,
graphics/lib/sumi/graphics/mixtures/save.rb,
graphics/lib/sumi/graphics/mixtures/tile.rb,
graphics/lib/sumi/graphics/mixtures/hatch.rb,
graphics/lib/sumi/graphics/auxiliary/paper.rb,
graphics/lib/sumi/graphics/mixtures/export.rb,
graphics/lib/sumi/graphics/mixtures/render.rb,
graphics/lib/sumi/graphics/auxiliary/canvas.rb,
graphics/lib/sumi/graphics/auxiliary/margin.rb,
graphics/lib/sumi/graphics/auxiliary/scalar.rb,
graphics/lib/sumi/graphics/document/default.rb,
graphics/lib/sumi/graphics/document/minimal.rb,
graphics/lib/sumi/graphics/mixtures/imprint.rb,
graphics/lib/sumi/graphics/mixtures/include.rb,
graphics/lib/sumi/graphics/mixtures/symbols.rb,
graphics/lib/sumi/graphics/auxiliary/content.rb,
graphics/lib/sumi/graphics/document/inkscape.rb,
graphics/lib/sumi/graphics/mixtures/identify.rb,
graphics/lib/sumi/graphics/mixtures/inkscape.rb,
graphics/lib/sumi/graphics/mixtures/validate.rb,
graphics/lib/sumi/graphics/mixtures/wrappers.rb,
graphics/lib/sumi/graphics/auxiliary/snapshot.rb,
graphics/lib/sumi/graphics/mixtures/duplicate.rb,
graphics/lib/sumi/graphics/mixtures/polyfills.rb,
graphics/lib/sumi/graphics/mixtures/transform.rb,
graphics/lib/sumi/graphics/mixtures/underscore.rb,
showcase/lib/sumi/showcase/passive.rb

Overview

SVG document builder and DSL namespace.

A document profile controls root metadata and preambles. A Canvas controls physical size, margins, viewport, and viewBox. #SVG combines them into an element tree. Library code keeps that tree as an object until it explicitly renders, validates, saves, or exports it.

Examples:

Build and render a minimal SVG document

drawing = Sumi::Graphics.SVG :minimal, width: 10, height: 10 do
  circle cx: 5, cy: 5, r: 4
end
drawing.Render #=> "<svg width=\"10\" height=\"10\">\n  <circle cx=\"5\" cy=\"5\" r=\"4\"/>\n</svg>"

See Also:

Defined Under Namespace

Modules: Document, Mixtures, Module, Modules Classes: Attributes, Canvas, Content, Element, Margin, Paper

Constant Summary collapse

VERSION =

Current graphics component version.

"1.0.0"
LintError =

Raised when graphics lint checks fail.

Class.new(Error)

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:

Use the component constructor in library code

page = Sumi::Graphics.canvas(:a4, margins: [12, 10])
icon = Sumi::Graphics.canvas(width: 24, height: 24, unit: :px)
[page.name, icon.viewport] # => [:a4, {width: "24.0px", height: "24.0px"}]

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:



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

def canvas(...)
  Graphics::Canvas.call(...)
end

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

Returns document class.

Examples:

Define, look up, and inspect a named document

Sumi::Graphics.document(:card, attributes: {viewBox: "0 0 100 60"})
Sumi::Graphics::Document.fetch(:card)              # => the registered document class
Sumi::Graphics::Document.profile(:card).attributes # => {viewBox: "0 0 100 60"}

Define an anonymous document without changing the registry

klass = Sumi::Graphics.document(attributes: {viewBox: "0 0 10 10"})
klass.profile.name # => nil

Define and use a named profile from library code

Sumi::Graphics.document(:badge, attributes: {viewBox: "0 0 24 24"})
drawing = Sumi::Graphics.SVG(:badge) { circle cx: 12, cy: 12, r: 10 }
drawing[:viewBox] # => "0 0 24 24"

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

    Looks up a named profile when both definition keywords are omitted. Supplying preambles: or attributes: defines a named profile, or returns an existing profile when every explicitly supplied field matches. Omitted fields are ignored during existing-profile comparison. Profile containers and strings are copied into the process-global, thread-atomic registry. Attribute names and nested Hash keys are normalized. Nil attributes are omitted. Update suffixes remain available for document-class inheritance. Mutable non-container values are stringified once before registration. Concurrent identical definitions return the same registered class.

    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. Nil means an empty Hash

    Returns:

    • (Class) —

      document class

    Raises:

    • (Sumi::ArgumentError) —

      when a profile conflicts or metadata is invalid XML, cyclic, or cannot be stringified

  • #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. Nil means an empty Hash

    Returns:

    • (Class) —

      anonymous document class

    Raises:

Returns:

  • (Class) —

    document class



91
92
93
# File 'graphics/lib/sumi/graphics.rb', line 91

def document(name = Undefined, preambles: Undefined, attributes: Undefined)
  Graphics::Document.define(name, preambles:, attributes:)
end

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

Defines or replaces a document profile class. Validation and snapshot capture complete before an existing registration is atomically replaced. Use the non-bang #document for ordinary idempotent definitions. This bang form is an explicit process-global replacement.

Parameters:

  • name (Symbol, String) —

    profile name

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

    document preamble lines

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

    default root attributes. Nil means an empty Hash

Returns:

  • (Class) —

    document class

Raises:

  • (Sumi::ArgumentError) —

    when the name or metadata is invalid XML, cyclic, or cannot be stringified



104
105
106
# File 'graphics/lib/sumi/graphics.rb', line 104

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

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

Defines a paper profile unless the same profile already exists. Registration is process-global and thread-atomic. Identical concurrent definitions are idempotent. Conflicting definitions are rejected.

Parameters:

  • width (Numeric) —

    paper width

  • height (Numeric) —

    paper height

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

    profile name

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

    SVG unit

Returns:

  • (Symbol, String) —

    original profile name

Raises:

  • (Sumi::ArgumentError) —

    when the profile is invalid or an existing profile has different dimensions



117
118
119
120
121
# File 'graphics/lib/sumi/graphics.rb', line 117

def paper(width, height, name = :custom, unit: "mm")
  Graphics::Paper.define(name, width:, height:, unit:, overwrite: false)

  name
end

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

Defines or replaces a paper profile. Use this only when replacing the process-global meaning of a name is intentional.

Parameters:

  • width (Numeric) —

    paper width

  • height (Numeric) —

    paper height

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

    profile name

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

    SVG unit

Returns:

  • (Symbol, String) —

    original profile name

Raises:



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

def paper!(width, height, name = :custom, unit: "mm")
  name.tap { Graphics::Paper.define(name, width:, height:, unit:, overwrite: true) }
end

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

Builds an SVG root element tree without rendering it.

The first argument selects root metadata, or supplies the canvas while using the default profile. The second supplies canvas attributes when the first argument is a profile. Keyword attributes are applied after both.

Parameters:

Yields:

  • evaluates the drawing DSL in the root element

Yield Returns:

  • (Object) —

    ignored block result

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when the document/canvas profile or root XML attributes are invalid



146
147
148
149
150
151
152
# File 'graphics/lib/sumi/graphics.rb', line 146

def SVG(document = :default, canvas = Undefined, **, &block)
  if canvas.equal?(Undefined) && (document.is_a?(Canvas) || document.is_a?(Paper))
    document, canvas = :default, document
  end

  Graphics::Document.(document, canvas, **, &block)
end