File: transformation_stack.rb

package info (click to toggle)
ruby-prawn 2.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,220 kB
  • ctags: 826
  • sloc: ruby: 14,469; sh: 43; makefile: 18
file content (42 lines) | stat: -rw-r--r-- 1,133 bytes parent folder | download
1
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
# encoding: utf-8
#
# transformation_stack.rb : Stores the transformations that have been applied to the document
#
# Copyright 2015, Roger Nesbitt. All Rights Reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.
#

require 'matrix'

module Prawn
  module TransformationStack
    def add_to_transformation_stack(a, b, c, d, e, f)
      @transformation_stack ||= [[]]
      @transformation_stack.last.push([a, b, c, d, e, f].map(&:to_f))
    end

    def save_transformation_stack
      @transformation_stack ||= [[]]
      @transformation_stack.push(@transformation_stack.last.dup)
    end

    def restore_transformation_stack
      @transformation_stack.pop if @transformation_stack
    end

    def current_transformation_matrix_with_translation(x = 0, y = 0)
      transformations = (@transformation_stack || [[]]).last

      matrix = Matrix.identity(3)

      transformations.each do |a, b, c, d, e, f|
        matrix *= Matrix[[a, c, e], [b, d, f], [0, 0, 1]]
      end

      matrix *= Matrix[[1, 0, x], [0, 1, y], [0, 0, 1]]

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