Class: Postsvg::Svg::TransformList

Inherits:
Object
  • Object
show all
Defined in:
lib/postsvg/svg/transform_list.rb

Overview

Parsed SVG transform attribute. Produces a list of Postsvg::Matrix instances that, multiplied in order, give the composite CTM for the element.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matrices = []) ⇒ TransformList

Returns a new instance of TransformList.



11
12
13
14
# File 'lib/postsvg/svg/transform_list.rb', line 11

def initialize(matrices = [])
  @matrices = matrices.freeze
  freeze
end

Instance Attribute Details

#matricesObject (readonly)

Returns the value of attribute matrices.



9
10
11
# File 'lib/postsvg/svg/transform_list.rb', line 9

def matrices
  @matrices
end

Class Method Details

.parse(text) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/postsvg/svg/transform_list.rb', line 26

def self.parse(text)
  return new if text.nil? || text.strip.empty?

  matrices = []
  text.scan(/(\w+)\s*\(([^)]*)\)/).each do |name, args|
    nums = args.split(/[\s,]+/).map(&:to_f)
    case name
    when "matrix"
      next unless nums.length == 6

      matrices << Matrix.new(a: nums[0], b: nums[1], c: nums[2],
                             d: nums[3], e: nums[4], f: nums[5])
    when "translate"
      tx = nums[0] || 0
      ty = nums[1] || 0
      matrices << Matrix.new(e: tx, f: ty)
    when "scale"
      sx = nums[0] || 1
      sy = nums.length > 1 ? nums[1] : sx
      matrices << Matrix.new(a: sx, d: sy)
    when "rotate"
      angle = nums[0] || 0
      matrices << Matrix.new.rotate(angle)
    when "skewX"
      matrices << Matrix.new.skew_x(nums[0] || 0)
    when "skewY"
      matrices << Matrix.new.skew_y(nums[0] || 0)
    end
  end
  new(matrices)
end

Instance Method Details

#compositeObject



20
21
22
23
24
# File 'lib/postsvg/svg/transform_list.rb', line 20

def composite
  return Matrix.new if @matrices.empty?

  @matrices.reduce(Matrix.new) { |acc, m| acc.multiply(m) }
end

#empty?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/postsvg/svg/transform_list.rb', line 16

def empty?
  @matrices.empty?
end