Documents

An SVG document is an element tree built with a canvas and a document profile. The canvas defines its dimensions. The profile defines its metadata and available methods. Compose covers reusable drawing code that does not belong to one document type.

Construct a document

require "sumi"

drawing = SVG width: 32, height: 20 do
  g id: "badge" do
    rect width: 32, height: 20, rx: 4, fill: "gold"
    text "S", x: 16, y: 14, "text-anchor": "middle"
  end
end

drawing.Render

Lowercase calls add SVG elements to the tree. Capitalized words create supporting values or operate on elements. In library code, use SVG for the document constructor and SVG. for surrounding operations. Types and namespaces use SVG::.

Canvas

A canvas keeps dimensions, units, margins, and the resulting viewBox together. Its size is the outer paper. inner is the size left after margins. The default viewBox shifts by the negative left and top margins. Drawing coordinate (0, 0) starts at the inner area's top-left while the viewport still includes the margins:

require "sumi"

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

drawing = SVG canvas do
  rect width: canvas.inner.width, height: canvas.inner.height
end

The canvas describes physical size and drawing coordinates. A profile describes root metadata, preambles, and available document methods. Keep them separate when several profiles share one page size or one profile uses several sizes.

Paper and Canvas provide orientation helpers: landscape, portrait, and rotate return rotated copies without mutating the original. Paper also provides aspect_ratio, landscape?, portrait?, and square?, and Canvas#to_h returns constructor fields. Margin accepts keyword arguments such as SVG::Margin.new(left: 10) and reports all-zero margins with zero?.

Profiles

A profile controls document metadata and extra DSL capabilities. It does not control canvas size or checking policy. All four profiles use the same validation and lint lifecycle.

ProfilePreambleRoot metadataAdditional DSL
:minimalnonenonecommon document DSL
:defaultXML declarationSVG namespacecommon document DSL
:htmlnoneSVG namespacecommon document DSL
:inkscapeXML declarationSVG and editor namespaces with crisp edgesDraw, Hatch, and editor/RDF helpers

Omit the profile for a standalone SVG file. This selects :default. Use :minimal for syntax examples or XML fragments, :html for SVG embedded in HTML, and :inkscape when editor metadata or its additional helpers belong to the drawing. The Inkscape root adds Sumi, Inkscape, and Sodipodi namespaces plus shape-rendering="crispEdges". The presence of Draw and Hatch on :inkscape is a convenience default, not an Inkscape format requirement.

Minimal and Default are sibling concrete profiles. Minimal adds no metadata and is not the base of the other profiles. SVG::Document::Base supplies the drawing methods shared by all profiles, but is not itself a selectable named profile.

Define a profile

SVG.Document creates a profile class derived from SVG::Document::Base. Omit the name for a private, one-off class. Give it a name when other code must select it by symbol:

require "sumi"

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

SVG.Document :badge, attributes: {viewBox: "0 0 40 16"}
SVG(:badge) { text "OK", x: 20, y: 12, "text-anchor": "middle" }.Render

Anonymous profiles stay local to the code that holds their class. Named profiles are registered for the current Ruby process. Use them for shared document vocabulary, not request-specific values. Repeating SVG.Document with the same name and metadata is safe. Conflicting metadata raises an error. SVG.Document! explicitly replaces an existing definition.

Use this factory when root attributes and preambles are the only differences. When a document also owns DSL methods, define a document type or add a mixture to the returned profile class.

Document types

A subclass of SVG::Document::Base is an unnamed document type with the common SVG DSL. Pass the class itself to SVG. Methods defined on it are available as bare drawing words and are inherited by its subclasses. SVG.Mixin adds methods after a class has been created, which is useful when a separate library owns the extension.

Sumi builds its own profiles from the same pieces: profile classes inherit from Base or another profile, and profile-specific drawing methods arrive through mixtures.

require "sumi"

Flowchart = Class.new(SVG::Document::Base) do
  def Node(label, x:, y:)
    g transform: "translate(#{x} #{y})" do
      rect x: -32, y: -12, width: 64, height: 24, rx: 4, fill: "white", stroke: "black"
      text label, y: 4, "text-anchor": "middle"
    end
  end
end

SVG.Mixin Flowchart do
  def Link(from:, to:, **attributes)
    x1, y1 = from
    x2, y2 = to
    line x1:, y1:, x2:, y2:, stroke: "black", **attributes
  end
end

drawing = SVG Flowchart, width: 200, height: 80 do
  Link from: [77, 40], to: [113, 40]
  Node "Parse", x: 45, y: 40
  Node "Render", x: 145, y: 40
end

drawing.Render

Flowchart owns Node. The mixture adds Link. Both run as document methods, so they can use the document's DSL and private helpers directly. Their names become methods of this document type.

The choice depends on what the application is defining:

NeedUseEffect
Different root metadata or preamblesSVG.DocumentCreates an anonymous or registered profile class
A new document type that always owns certain methodssubclass SVG::Document::BaseMethods belong to the type and its subclasses
Add named or application-defined methods to an existing typeSVG.MixinMethods join the target type and its subclasses

SVG.Mixin also accepts Sumi's named mixtures. For example, a private type can use Hatch without adopting the Inkscape profile's metadata:

profile = Class.new(SVG::Document::Base)
SVG.Mixin :Hatch, profile
region = Sumi::Geometry::Rect[24, 12]

SVG(profile) do
  Draw region.lines, stroke: "silver"
  Hatch region, angle: 30, step: 3, stroke: "black"
end.Render

Targeting SVG::Document::Base changes every descendant profile for the whole process. Subclass it first to keep the extension local. Base subclasses and SVG.Mixin change what a document type can do. Use SVG.Module for drawing code that works across document types without adding methods.

Element dispatch

The DSL recognizes SVG element names dynamically, so it does not need a Ruby method for every element in each SVG release. Sumi validates the resulting standard SVG before checked output. Names are case-sensitive. linearGradient is an SVG element, while LinearGradient is a different Ruby call.

Use Element when producing foreign XML or when a qualified name cannot be expressed as a bare Ruby call:

SVG :minimal do
  Element "catalog:item", "featured", "catalog:rank": 1
end.Render

Content safety

Ordinary String arguments are XML text-encoded automatically. Use a Content constructor only when content needs a different serialization channel.

InputUseBehavior
Ordinary text argumenttext "A & B"encoded automatically
Explicit reusable text contentContent.encodedXML text-encoded
Literal text body in a CDATA sectionContent.cdataCDATA terminators split safely
CSS rules expressed as a HashContent.cssrendered as CSS inside CDATA
Already serialized trusted markupContent.verbatimunescaped markup whose caller owns well-formedness and escaping
drawing = SVG :minimal do
  text "A & B"
  text SVG::Content.encoded("A & B")
  style SVG::Content.cdata(".note { fill: red; }")
  style SVG::Content.css(".note" => {fill: "red"})
  g SVG::Content.verbatim("<title>trusted markup</title>")
end

drawing.Render

Advanced consumers can subclass SVG::Content and implement render(output, depth). The rendering engine ignores the method's return value. A custom implementation must escape all data that it inserts into markup. Use SVG::Content.encoded(...).to_s instead of interpolating caller text directly. Built-in Content values are frozen and support empty?, ==, eql?, and hash.

Validation lifecycle

Use drawing.call to validate and lint a document and return its SVG as a String. Save and Out use this same checked path. Call PreRender(validate: true, lint: true) to run the checks by themselves. Render serializes the document or subtree directly, which also lets applications serialize non-SVG XML.

SVG validation lifecycleThe call, Save and Out operations prepare a document before serialization. Render serializes directly.SVG documentcall / Save / OutRenderPreRenderValidateLintSerialize SVG

For the standard vocabulary, use the MDN SVG element reference. For Sumi operations, use the DSL Catalog.