Module: Sumi::Graphics::Mixtures::Transform

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

Overview

DSL helpers for SVG transform attributes.

Transform calls are appended in call order and return the element, so they can be composed.

Examples:

Compose transforms on one element

Sumi::Graphics.SVG(:minimal) do
  rect(width: 8, height: 4).Translate(12, 6).Rotate(15, 4, 2)
end

Instance Method Summary collapse

Instance Method Details

#Align(position, inner:, outer:) ⇒ Sumi::Graphics::Element

Note:

Size-only boxes have origin (0, 0). Positions describe known box geometry, not renderer-computed bounds.

Aligns an inner box inside an outer box.

Parameters:

  • position (Symbol, String, nil) —

    alignment name

  • inner (#width, #height, nil) —

    inner box, with optional position exposing x and y

  • outer (#width, #height, nil) —

    outer box, with optional position exposing x and y

Returns:

Raises:



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

def Align(position, inner:, outer:)
  offset = Alignment.offset(position, inner:, outer:)
  return self unless offset

  Translate(*offset)
end

#Flip ⇒ Sumi::Graphics::Element

Appends a scale(-1, -1) transform.

Returns:



75
76
77
78
79
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 75

def Flip
  tap do
    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "scale(-1, -1)"
  end
end

#FlipX ⇒ Sumi::Graphics::Element

Appends a horizontal flip transform.

Returns:



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

def FlipX
  tap do
    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "scale(-1, 1)"
  end
end

#FlipY ⇒ Sumi::Graphics::Element

Appends a vertical flip transform.

Returns:



91
92
93
94
95
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 91

def FlipY
  tap do
    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "scale(1, -1)"
  end
end

#Matrix(*values) ⇒ Sumi::Graphics::Element

Appends a six-value SVG matrix transform.

Parameters:

  • values (Array<Numeric>) —

    finite matrix values, normalized to SVG numbers

Returns:

Raises:



101
102
103
104
105
106
107
108
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 101

def Matrix(*values)
  tap do
    ArgumentError.("Incorrect transform matrix (six values required): #{values}") if values.size != 6
    values = Scalar.numbers(values, context: "matrix")

    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "matrix(#{values.join(" ")})"
  end
end

#Rotate(a, *origin) ⇒ Sumi::Graphics::Element

Appends an SVG rotate transform.

Parameters:

  • a (Numeric) —

    finite angle in degrees, normalized to an SVG number

  • origin (Array<Numeric>) —

    optional finite x and y origin, normalized to SVG numbers

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when angle or origin is not finite real, or origin is not two coordinates



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 115

def Rotate(a, *origin)
  tap do
    if !origin.empty? && origin.size != 2
      ArgumentError.("Incorrect origin (two coordinates required): #{origin}")
    end

    angle = Scalar.number(a, context: "rotation", field: :angle)
    origin = Scalar.numbers(origin, context: "rotation")

    next if angle.zero?

    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "rotate(#{[angle, *origin].join(", ")})"
  end
end

#RotateLeft(*origin) ⇒ Sumi::Graphics::Element

Appends a -90-degree SVG rotate transform.

Parameters:

  • origin (Array<Numeric>) —

    optional x and y origin

Returns:



140
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 140

def RotateLeft(...) = Rotate(-90, ...)

#RotateRight(*origin) ⇒ Sumi::Graphics::Element

Appends a 90-degree SVG rotate transform.

Parameters:

  • origin (Array<Numeric>) —

    optional x and y origin

Returns:



134
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 134

def RotateRight(...) = Rotate(90, ...)

#Scale(x, y = nil) ⇒ Sumi::Graphics::Element

Appends an SVG scale transform.

Parameters:

  • x (Numeric) —

    finite x scale, or uniform scale when y is nil

  • y (Numeric, nil) (defaults to: nil) —

    finite y scale

Returns:

Raises:



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

def Scale(x, y = nil)
  tap do
    x = Scalar.number(x, context: "scale", field: :x)
    y = Scalar.number(y, context: "scale", field: :y) unless y.nil?
    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "scale(#{(y.nil? ? [x] : [x, y]).join(", ")})"
  end
end

#Skew(ax, ay) ⇒ Sumi::Graphics::Element

Appends a skew transform through an SVG matrix.

Parameters:

  • ax (Numeric) —

    finite x skew angle in degrees, normalized before calculation

  • ay (Numeric) —

    finite y skew angle in degrees, normalized before calculation

Returns:

Raises:

  • (Sumi::ArgumentError) —

    when an angle is not a finite real number or is congruent to 90 degrees modulo 180



160
161
162
163
164
165
166
167
168
169
170
171
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 160

def Skew(ax, ay)
  ax = Scalar.number(ax, context: "skew", field: :x)
  ay = Scalar.number(ay, context: "skew", field: :y)

  [ax, ay].each do |angle|
    ArgumentError.("Skew angle must not be +/-90 (mod 180): #{angle}") if ((angle % 180) - 90).zero?
  end

  return self if ax.zero? && ay.zero?

  Matrix(1.0, F.tan(ay), F.tan(ax), 1.0, 0.0, 0.0)
end

#SkewX(a) ⇒ Sumi::Graphics::Element

Appends an SVG skewX transform.

Parameters:

  • a (Numeric) —

    finite angle in degrees, normalized to an SVG number

Returns:

Raises:



177
178
179
180
181
182
183
184
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 177

def SkewX(a)
  tap do
    angle = Scalar.number(a, context: "skew", field: :x)
    next if angle.zero?

    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "skewX(#{angle})"
  end
end

#SkewY(a) ⇒ Sumi::Graphics::Element

Appends an SVG skewY transform.

Parameters:

  • a (Numeric) —

    finite angle in degrees, normalized to an SVG number

Returns:

Raises:



190
191
192
193
194
195
196
197
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 190

def SkewY(a)
  tap do
    angle = Scalar.number(a, context: "skew", field: :y)
    next if angle.zero?

    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "skewY(#{angle})"
  end
end

#Translate(x, y = nil) ⇒ Sumi::Graphics::Element

Appends an SVG translate transform. One argument translates only the x axis.

Parameters:

  • x (Numeric) —

    finite x translation, normalized to an SVG number

  • y (Numeric, nil) (defaults to: nil) —

    finite y translation, normalized to an SVG number

Returns:

Raises:



204
205
206
207
208
209
210
211
212
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 204

def Translate(x, y = nil)
  tap do
    dx = Scalar.number(x, context: "translation", field: :x)
    dy = Scalar.number(y, context: "translation", field: :y) unless y.nil?
    next if dx.zero? && (dy.nil? || dy.zero?)

    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "translate(#{(dy.nil? ? [dx] : [dx, dy]).join(" ")})"
  end
end

#TranslateX(x) ⇒ Sumi::Graphics::Element

Appends an x-axis SVG translate transform.

Parameters:

  • x (Numeric) —

    finite x translation

Returns:

Raises:

See Also:



219
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 219

def TranslateX(x) = Translate(x)

#TranslateY(y) ⇒ Sumi::Graphics::Element

Appends a y-axis SVG translate transform.

Parameters:

  • y (Numeric) —

    finite y translation

Returns:

Raises:

See Also:



226
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 226

def TranslateY(y) = Translate(0, y)