Module: Sevgi::Graphics::Mixtures::Wrappers

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

Overview

DSL wrappers for common SVG shapes and content patterns.

Instance Method Summary collapse

Instance Method Details

#css(hash = nil, **attributes) ⇒ Sevgi::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:

  • (Sevgi::ArgumentError)

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



96
97
98
99
100
# File 'lib/sevgi/graphics/mixtures/wrappers.rb', line 96

def css(hash = nil, **attributes)
  hash, attributes = attributes, {} unless hash

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

#HLineBy(length:, x: 0, y: 0) ⇒ Sevgi::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:

  • (Sevgi::ArgumentError)

    when an operand is not a finite real number



108
109
110
111
112
113
114
# File 'lib/sevgi/graphics/mixtures/wrappers.rb', line 108

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) ⇒ Sevgi::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:

  • (Sevgi::ArgumentError)

    when a coordinate is not a finite real number



32
33
34
35
36
37
38
# File 'lib/sevgi/graphics/mixtures/wrappers.rb', line 32

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) ⇒ Sevgi::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:

  • (Sevgi::ArgumentError)

    when an operand is not a finite real number



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sevgi/graphics/mixtures/wrappers.rb', line 77

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 * ::Math.cos(angle.to_f / 180 * ::Math::PI),
      length * ::Math.sin(angle.to_f / 180 * ::Math::PI)
    ],
    context: "relative line result"
  )

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

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

Builds a line path ending at an absolute point.

Examples:

Render a rational coordinate as an SVG number

Sevgi::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:

  • (Sevgi::ArgumentError)

    when a coordinate is not a finite real number



17
18
19
20
21
22
23
24
# File 'lib/sevgi/graphics/mixtures/wrappers.rb', line 17

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:) ⇒ Sevgi::Graphics::Element

Builds a square rect.

Parameters:

  • length (Numeric)

    finite side length

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when length is not a finite real number



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

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

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

#Symbol(name, **kwargs) { ... } ⇒ Sevgi::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:



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

def Symbol(name, **kwargs, &block)
  id = (words = name.split).map(&:downcase).join("-")
  title = words.map(&:capitalize).join(" ")

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

#VLineBy(length:, x: 0, y: 0) ⇒ Sevgi::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:

  • (Sevgi::ArgumentError)

    when an operand is not a finite real number



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

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) ⇒ Sevgi::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:

  • (Sevgi::ArgumentError)

    when a coordinate is not a finite real number



62
63
64
65
66
67
68
# File 'lib/sevgi/graphics/mixtures/wrappers.rb', line 62

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