Class: Pdfrb::Content::TransformationMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/content/transformation_matrix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0) ⇒ TransformationMatrix

Returns a new instance of TransformationMatrix.



8
9
10
11
12
13
14
15
16
# File 'lib/pdfrb/content/transformation_matrix.rb', line 8

def initialize(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0)
  @a = a.to_f
  @b = b.to_f
  @c = c.to_f
  @d = d.to_f
  @e = e.to_f
  @f = f.to_f
  freeze
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



6
7
8
# File 'lib/pdfrb/content/transformation_matrix.rb', line 6

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



6
7
8
# File 'lib/pdfrb/content/transformation_matrix.rb', line 6

def b
  @b
end

#cObject (readonly)

Returns the value of attribute c.



6
7
8
# File 'lib/pdfrb/content/transformation_matrix.rb', line 6

def c
  @c
end

#dObject (readonly)

Returns the value of attribute d.



6
7
8
# File 'lib/pdfrb/content/transformation_matrix.rb', line 6

def d
  @d
end

#eObject (readonly)

Returns the value of attribute e.



6
7
8
# File 'lib/pdfrb/content/transformation_matrix.rb', line 6

def e
  @e
end

#fObject (readonly)

Returns the value of attribute f.



6
7
8
# File 'lib/pdfrb/content/transformation_matrix.rb', line 6

def f
  @f
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



55
56
57
# File 'lib/pdfrb/content/transformation_matrix.rb', line 55

def ==(other)
  other.is_a?(self.class) && to_a == other.to_a
end

#identity?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/pdfrb/content/transformation_matrix.rb', line 18

def identity?
  @a == 1 && @b.zero? && @c.zero? && @d == 1 && @e.zero? && @f.zero?
end

#multiply(other) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/pdfrb/content/transformation_matrix.rb', line 22

def multiply(other)
  self.class.new(
    (@a * other.a) + (@c * other.b),
    (@b * other.a) + (@d * other.b),
    (@a * other.c) + (@c * other.d),
    (@b * other.c) + (@d * other.d),
    (@a * other.e) + (@c * other.f) + @e,
    (@b * other.e) + (@d * other.f) + @f,
  )
end

#rotate(angle_radians) ⇒ Object



41
42
43
44
45
# File 'lib/pdfrb/content/transformation_matrix.rb', line 41

def rotate(angle_radians)
  cos = Math.cos(angle_radians)
  sin = Math.sin(angle_radians)
  multiply(self.class.new(cos, sin, -sin, cos, 0, 0))
end

#scale(sx, sy = sx) ⇒ Object



37
38
39
# File 'lib/pdfrb/content/transformation_matrix.rb', line 37

def scale(sx, sy = sx)
  multiply(self.class.new(sx, 0, 0, sy, 0, 0))
end

#skew(fa, fb) ⇒ Object



47
48
49
# File 'lib/pdfrb/content/transformation_matrix.rb', line 47

def skew(fa, fb)
  multiply(self.class.new(1, Math.tan(fa), Math.tan(fb), 1, 0, 0))
end

#to_aObject



51
52
53
# File 'lib/pdfrb/content/transformation_matrix.rb', line 51

def to_a
  [@a, @b, @c, @d, @e, @f]
end

#translate(tx, ty) ⇒ Object



33
34
35
# File 'lib/pdfrb/content/transformation_matrix.rb', line 33

def translate(tx, ty)
  multiply(self.class.new(1, 0, 0, 1, tx, ty))
end