Module: Prawn::SVG::TransformParser

Included in:
Elements::Base
Defined in:
lib/prawn/svg/transform_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse_transform_attribute(transform) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
57
58
59
60
61
# File 'lib/prawn/svg/transform_parser.rb', line 2

def parse_transform_attribute(transform)
  matrix = Matrix.identity(3)

  parse_css_method_calls(transform).each do |name, arguments|
    case name
    when 'translate'
      x, y = arguments
      matrix *= Matrix[[1, 0, x_pixels(x.to_f)], [0, 1, -y_pixels(y.to_f)], [0, 0, 1]]

    when 'translateX'
      x = arguments.first
      matrix *= Matrix[[1, 0, x_pixels(x.to_f)], [0, 1, 0], [0, 0, 1]]

    when 'translateY'
      y = arguments.first
      matrix *= Matrix[[1, 0, 0], [0, 1, -y_pixels(y.to_f)], [0, 0, 1]]

    when 'rotate'
      angle, x, y = arguments.collect(&:to_f)
      angle = angle * Math::PI / 180.0

      case arguments.length
      when 1
        matrix *= Matrix[[Math.cos(angle), Math.sin(angle), 0], [-Math.sin(angle), Math.cos(angle), 0], [0, 0, 1]]
      when 3
        matrix *= Matrix[[1, 0, x_pixels(x.to_f)], [0, 1, -y_pixels(y.to_f)], [0, 0, 1]]
        matrix *= Matrix[[Math.cos(angle), Math.sin(angle), 0], [-Math.sin(angle), Math.cos(angle), 0], [0, 0, 1]]
        matrix *= Matrix[[1, 0, -x_pixels(x.to_f)], [0, 1, y_pixels(y.to_f)], [0, 0, 1]]
      else
        warnings << "transform 'rotate' must have either one or three arguments"
      end

    when 'scale'
      x_scale = arguments[0].to_f
      y_scale = (arguments[1] || x_scale).to_f
      matrix *= Matrix[[x_scale, 0, 0], [0, y_scale, 0], [0, 0, 1]]

    when 'skewX'
      angle = arguments[0].to_f * Math::PI / 180.0
      matrix *= Matrix[[1, -Math.tan(angle), 0], [0, 1, 0], [0, 0, 1]]

    when 'skewY'
      angle = arguments[0].to_f * Math::PI / 180.0
      matrix *= Matrix[[1, 0, 0], [-Math.tan(angle), 1, 0], [0, 0, 1]]

    when 'matrix'
      if arguments.length == 6
        a, b, c, d, e, f = arguments.collect(&:to_f)
        matrix *= Matrix[[a, -c, e], [-b, d, -f], [0, 0, 1]]
      else
        warnings << "transform 'matrix' must have six arguments"
      end

    else
      warnings << "Unknown/unsupported transformation '#{name}'; ignoring"
    end
  end

  matrix.to_a[0..1].transpose.flatten
end