Module: Sumi::Graphics::Mixtures::Wrappers

Defined in:
graphics/lib/sumi/graphics/mixtures/wrappers.rb

Overview

DSL wrappers for common SVG shapes and content patterns.

Instance Method Summary collapse

Instance Method Details

#ArcBy(dx:, dy:, rx:, ry:, x: 0, y: 0, rotation: 0, large: false, sweep: false) ⇒ Sumi::Graphics::Element

Builds an elliptical arc path ending at a relative offset.

Parameters:

  • dx (Numeric) —

    finite x displacement from the starting point

  • dy (Numeric) —

    finite y displacement from the starting point

  • rx (Numeric) —

    finite local x radius

  • ry (Numeric) —

    finite local y radius

  • x (Numeric) (defaults to: 0) —

    finite starting x coordinate

  • y (Numeric) (defaults to: 0) —

    finite starting y coordinate

  • rotation (Numeric) (defaults to: 0) —

    clockwise ellipse rotation in degrees

  • large (Boolean) (defaults to: false) —

    select the arc exceeding a half turn

  • sweep (Boolean) (defaults to: false) —

    select increasing parameter angles

Returns:

Raises:

See Also:



47
48
49
50
51
52
53
54
# File 'graphics/lib/sumi/graphics/mixtures/wrappers.rb', line 47

def ArcBy(dx:, dy:, rx:, ry:, x: 0, y: 0, rotation: 0, large: false, sweep: false, **)
  x, y, dx, dy, rx, ry, rotation = Scalar.numbers([x, y, dx, dy, rx, ry, rotation], context: "relative arc")
  unless [large, sweep].all? { it.equal?(true) || it.equal?(false) }
    ArgumentError.("Arc flags must be Boolean")
  end

  path(d: "M #{x} #{y} a #{rx} #{ry} #{rotation} #{large ? 1 : 0} #{sweep ? 1 : 0} #{dx} #{dy}", **)
end

#ArcTo(x2:, y2:, rx:, ry:, x1: 0, y1: 0, rotation: 0, large: false, sweep: false) ⇒ Sumi::Graphics::Element

Builds an elliptical arc path ending at an absolute point. SVG resolves the ellipse from its endpoints, radii, and flags, including radius correction and zero radii.

Examples:

Draw an upper semicircle in screen coordinates

Sumi::Graphics.SVG { ArcTo x1: 0, y1: 10, x2: 20, y2: 10, rx: 10, ry: 10, sweep: true }

Parameters:

  • x2 (Numeric) —

    finite ending x coordinate

  • y2 (Numeric) —

    finite ending y coordinate

  • rx (Numeric) —

    finite local x radius

  • ry (Numeric) —

    finite local y radius

  • x1 (Numeric) (defaults to: 0) —

    finite starting x coordinate

  • y1 (Numeric) (defaults to: 0) —

    finite starting y coordinate

  • rotation (Numeric) (defaults to: 0) —

    clockwise ellipse rotation in degrees

  • large (Boolean) (defaults to: false) —

    select the arc exceeding a half turn

  • sweep (Boolean) (defaults to: false) —

    select increasing parameter angles

Returns:

Raises:



25
26
27
28
29
30
31
32
# File 'graphics/lib/sumi/graphics/mixtures/wrappers.rb', line 25

def ArcTo(x2:, y2:, rx:, ry:, x1: 0, y1: 0, rotation: 0, large: false, sweep: false, **)
  x1, y1, x2, y2, rx, ry, rotation = Scalar.numbers([x1, y1, x2, y2, rx, ry, rotation], context: "absolute arc")
  unless [large, sweep].all? { it.equal?(true) || it.equal?(false) }
    ArgumentError.("Arc flags must be Boolean")
  end

  path(d: "M #{x1} #{y1} A #{rx} #{ry} #{rotation} #{large ? 1 : 0} #{sweep ? 1 : 0} #{x2} #{y2}", **)
end

#css(hash = nil, **attributes) ⇒ Sumi::Graphics::Element

Builds a style element with CSS content.

Parameters:

  • hash (Hash, nil) (defaults to: nil) —

    CSS rules

  • attributes (Hash) —

    style attributes or CSS rules when hash is nil

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when CSS rules are malformed, cyclic, cannot be stringified, or contain invalid encoding or illegal XML 1.0 characters



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

def css(hash = nil, **attributes)
  hash, attributes = attributes, {} if hash.nil?

  style(Content.css(hash), type: "text/css", **attributes)
end

#HLineBy(length:, x: 0, y: 0) ⇒ Sumi::Graphics::Element

Builds a relative horizontal line path.

Parameters:

  • length (Numeric) —

    finite line length

  • x (Numeric) (defaults to: 0) —

    finite starting x coordinate

  • y (Numeric) (defaults to: 0) —

    finite starting y coordinate

Returns:

Raises:



158
159
160
161
162
163
164
# File 'graphics/lib/sumi/graphics/mixtures/wrappers.rb', line 158

def HLineBy(length:, x: 0, y: 0, **)
  length = Scalar.number(length, context: "horizontal line", field: :length)
  x = Scalar.number(x, context: "horizontal line", field: :x)
  y = Scalar.number(y, context: "horizontal line", field: :y)

  path(d: "M #{x} #{y} h #{length}", **)
end

#HLineTo(x2:, x1: 0, y1: 0) ⇒ Sumi::Graphics::Element

Builds a horizontal line path ending at an absolute x coordinate.

Parameters:

  • x2 (Numeric) —

    finite ending x coordinate

  • x1 (Numeric) (defaults to: 0) —

    finite starting x coordinate

  • y1 (Numeric) (defaults to: 0) —

    finite starting y coordinate

Returns:

Raises:



81
82
83
84
85
86
87
# File 'graphics/lib/sumi/graphics/mixtures/wrappers.rb', line 81

def HLineTo(x2:, x1: 0, y1: 0, **)
  x1 = Scalar.number(x1, context: "horizontal line", field: :x1)
  y1 = Scalar.number(y1, context: "horizontal line", field: :y1)
  x2 = Scalar.number(x2, context: "horizontal line", field: :x2)

  path(d: "M #{x1} #{y1} H #{x2}", **)
end

#LineBy(angle:, length:, x: 0, y: 0) ⇒ Sumi::Graphics::Element

Builds a relative line path from angle and length.

Parameters:

  • angle (Numeric) —

    finite angle in degrees, normalized before calculation

  • length (Numeric) —

    finite line length, normalized before calculation

  • x (Numeric) (defaults to: 0) —

    finite starting x coordinate, normalized to an SVG number

  • y (Numeric) (defaults to: 0) —

    finite starting y coordinate, normalized to an SVG number

Returns:

Raises:



133
134
135
136
137
138
# File 'graphics/lib/sumi/graphics/mixtures/wrappers.rb', line 133

def LineBy(angle:, length:, x: 0, y: 0, **)
  angle, length, x, y = Scalar.numbers([angle, length, x, y], context: "relative line")
  dx, dy = Scalar.numbers([length * F.cos(angle), length * F.sin(angle)], context: "relative line result")

  path(d: "M #{x} #{y} l #{dx} #{dy}", **)
end

#LineTo(x2:, y2:, x1: 0, y1: 0) ⇒ Sumi::Graphics::Element

Builds a line path ending at an absolute point.

Examples:

Render a rational coordinate as an SVG number

Sumi::Graphics.SVG { LineTo(x2: Rational(1, 2), y2: 1) }

Parameters:

  • x2 (Numeric) —

    finite ending x coordinate

  • y2 (Numeric) —

    finite ending y coordinate

  • x1 (Numeric) (defaults to: 0) —

    finite starting x coordinate

  • y1 (Numeric) (defaults to: 0) —

    finite starting y coordinate

Returns:

Raises:



66
67
68
69
70
71
72
73
# File 'graphics/lib/sumi/graphics/mixtures/wrappers.rb', line 66

def LineTo(x2:, y2:, x1: 0, y1: 0, **)
  x1 = Scalar.number(x1, context: "absolute line", field: :x1)
  y1 = Scalar.number(y1, context: "absolute line", field: :y1)
  x2 = Scalar.number(x2, context: "absolute line", field: :x2)
  y2 = Scalar.number(y2, context: "absolute line", field: :y2)

  path(d: "M #{x1} #{y1} L #{x2} #{y2}", **)
end

#square(length:) ⇒ Sumi::Graphics::Element

Builds a square rect.

Parameters:

  • length (Numeric) —

    finite side length

Returns:

Raises:



170
171
172
173
174
# File 'graphics/lib/sumi/graphics/mixtures/wrappers.rb', line 170

def square(length:, **)
  length = Scalar.number(length, context: "square", field: :length)

  rect(width: length, height: length, **)
end

#Symbol(name, **kwargs) { ... } ⇒ Sumi::Graphics::Element

Builds a titled SVG symbol.

Parameters:

  • name (String) —

    human-readable symbol name

  • kwargs (Hash) —

    symbol attributes

Yields:

  • evaluates the drawing DSL in the symbol element

Yield Returns:

  • (Object) —

    ignored block result

Returns:

Raises:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'graphics/lib/sumi/graphics/mixtures/wrappers.rb', line 96

def Symbol(name, **kwargs, &block)
  unless name.is_a?(::String) && !(words = name.split).empty?
    ArgumentError.("Symbol name must be a non-empty String")
  end

  ArgumentError.("Block required") unless block

  id = words.map(&:downcase).join("-")
  title = words.map(&:capitalize).join(" ")

  symbol(id:, **kwargs) do
    title(title)
    Within(&block)
  end
end

#VLineBy(length:, x: 0, y: 0) ⇒ Sumi::Graphics::Element

Builds a relative vertical line path.

Parameters:

  • length (Numeric) —

    finite line length

  • x (Numeric) (defaults to: 0) —

    finite starting x coordinate

  • y (Numeric) (defaults to: 0) —

    finite starting y coordinate

Returns:

Raises:



182
183
184
185
186
187
188
# File 'graphics/lib/sumi/graphics/mixtures/wrappers.rb', line 182

def VLineBy(length:, x: 0, y: 0, **)
  length = Scalar.number(length, context: "vertical line", field: :length)
  x = Scalar.number(x, context: "vertical line", field: :x)
  y = Scalar.number(y, context: "vertical line", field: :y)

  path(d: "M #{x} #{y} v #{length}", **)
end

#VLineTo(y2:, x1: 0, y1: 0) ⇒ Sumi::Graphics::Element

Builds a vertical line path ending at an absolute y coordinate.

Parameters:

  • y2 (Numeric) —

    finite ending y coordinate

  • x1 (Numeric) (defaults to: 0) —

    finite starting x coordinate

  • y1 (Numeric) (defaults to: 0) —

    finite starting y coordinate

Returns:

Raises:



118
119
120
121
122
123
124
# File 'graphics/lib/sumi/graphics/mixtures/wrappers.rb', line 118

def VLineTo(y2:, x1: 0, y1: 0, **)
  x1 = Scalar.number(x1, context: "vertical line", field: :x1)
  y1 = Scalar.number(y1, context: "vertical line", field: :y1)
  y2 = Scalar.number(y2, context: "vertical line", field: :y2)

  path(d: "M #{x1} #{y1} V #{y2}", **)
end