Sha256: 6b5c211e87b1b9a5895c908f7ffe733cf38f2eee2132a33b15864173caa4acb0

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

# 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'

# rubocop: disable Metrics/ParameterLists, Naming/MethodParameterName
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
    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
# rubocop: enable Metrics/ParameterLists, Naming/MethodParameterName

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prawn-2.4.0 lib/prawn/transformation_stack.rb
prawn-2.3.0 lib/prawn/transformation_stack.rb