Class: Pdfrb::Model::Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/model/matrix.rb

Overview

2D affine transformation matrix [a b c d e f] per s8.4.4. Composes via right-multiplication (PDF convention). Immutable value object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a = 1.0, b = 0.0, c = 0.0, d = 1.0, e = 0.0, f = 0.0) ⇒ Matrix

Returns a new instance of Matrix.



11
12
13
14
15
16
17
18
19
# File 'lib/pdfrb/model/matrix.rb', line 11

def initialize(a = 1.0, b = 0.0, c = 0.0, d = 1.0, e = 0.0, f = 0.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.



9
10
11
# File 'lib/pdfrb/model/matrix.rb', line 9

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



9
10
11
# File 'lib/pdfrb/model/matrix.rb', line 9

def b
  @b
end

#cObject (readonly)

Returns the value of attribute c.



9
10
11
# File 'lib/pdfrb/model/matrix.rb', line 9

def c
  @c
end

#dObject (readonly)

Returns the value of attribute d.



9
10
11
# File 'lib/pdfrb/model/matrix.rb', line 9

def d
  @d
end

#eObject (readonly)

Returns the value of attribute e.



9
10
11
# File 'lib/pdfrb/model/matrix.rb', line 9

def e
  @e
end

#fObject (readonly)

Returns the value of attribute f.



9
10
11
# File 'lib/pdfrb/model/matrix.rb', line 9

def f
  @f
end

Class Method Details

.from_array(arr) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/pdfrb/model/matrix.rb', line 37

def self.from_array(arr)
  case arr
  when Matrix then arr
  when PdfArray, Array then new(*arr.take(6))
  else raise ArgumentError, "cannot build Matrix from #{arr.class}"
  end
end

.identityObject



21
# File 'lib/pdfrb/model/matrix.rb', line 21

def self.identity; new; end

.rotate(rad) ⇒ Object



27
28
29
30
31
# File 'lib/pdfrb/model/matrix.rb', line 27

def self.rotate(rad)
  co = Math.cos(rad)
  si = Math.sin(rad)
  new(co, si, -si, co, 0, 0)
end

.scale(sx, sy = sx) ⇒ Object



25
# File 'lib/pdfrb/model/matrix.rb', line 25

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

.skew(alpha, beta) ⇒ Object



33
34
35
# File 'lib/pdfrb/model/matrix.rb', line 33

def self.skew(alpha, beta)
  new(1, Math.tan(alpha), Math.tan(beta), 1, 0, 0)
end

.translate(tx, ty) ⇒ Object



23
# File 'lib/pdfrb/model/matrix.rb', line 23

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

Instance Method Details

#*(other) ⇒ Object

Compose with other. Column-vector convention: the product M1 * M2 first transforms a point by M2, then by M1. Matches PDF's cm operator (CTM_new = M_argument * CTM_old).



48
49
50
51
52
53
54
55
56
57
# File 'lib/pdfrb/model/matrix.rb', line 48

def *(other)
  Matrix.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

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



71
72
73
# File 'lib/pdfrb/model/matrix.rb', line 71

def ==(other)
  other.is_a?(Matrix) && to_a == other.to_a
end

#hashObject



76
77
78
# File 'lib/pdfrb/model/matrix.rb', line 76

def hash
  to_a.hash
end

#identity?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/pdfrb/model/matrix.rb', line 63

def identity?
  @a == 1.0 && @b == 0.0 && @c == 0.0 && @d == 1.0 && @e == 0.0 && @f == 0.0
end

#inspectObject



80
81
82
# File 'lib/pdfrb/model/matrix.rb', line 80

def inspect
  "#<Matrix a=#{@a} b=#{@b} c=#{@c} d=#{@d} e=#{@e} f=#{@f}>"
end

#to_aObject



67
68
69
# File 'lib/pdfrb/model/matrix.rb', line 67

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

#transform_point(x, y) ⇒ Object



59
60
61
# File 'lib/pdfrb/model/matrix.rb', line 59

def transform_point(x, y)
  [@a * x + @c * y + @e, @b * x + @d * y + @f]
end