Module: Prawn::Graphics::Transformation
- Included in:
- Prawn::Graphics
- Defined in:
- lib/prawn/graphics/transformation.rb
Overview
Implements user-space coordinate transformation.
Stable API collapse
-
#rotate(angle, options = {}) { ... } ⇒ void
Rotate the user space.
-
#scale(factor, options = {}) { ... } ⇒ void
Scale the user space.
-
#transformation_matrix(*matrix) { ... } ⇒ void
Transform the user space (see notes for rotate regarding graphics state) Generally, one would use the #rotate, #scale, and #translate convenience methods instead of calling transformation_matrix directly.
-
#translate(x, y) { ... } ⇒ void
Translate the user space.
Instance Method Details
#rotate(angle, options = {}) { ... } ⇒ void
This method returns an undefined value.
Rotate the user space. If a block is not provided, then you must save and restore the graphics state yourself.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/prawn/graphics/transformation.rb', line 36 def rotate(angle, = {}, &block) Prawn.(:origin, ) rad = degree_to_rad(angle) cos = Math.cos(rad) sin = Math.sin(rad) if [:origin].nil? transformation_matrix(cos, sin, -sin, cos, 0, 0, &block) else raise Prawn::Errors::BlockRequired unless block x = [:origin][0] + bounds.absolute_left y = [:origin][1] + bounds.absolute_bottom x_prime = (x * cos) - (y * sin) y_prime = (x * sin) + (y * cos) translate(x - x_prime, y - y_prime) do transformation_matrix(cos, sin, -sin, cos, 0, 0, &block) end end end |
#scale(factor, options = {}) { ... } ⇒ void
This method returns an undefined value.
Scale the user space. If a block is not provided, then you must save and restore the graphics state yourself.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/prawn/graphics/transformation.rb', line 109 def scale(factor, = {}, &block) Prawn.(:origin, ) if [:origin].nil? transformation_matrix(factor, 0, 0, factor, 0, 0, &block) else raise Prawn::Errors::BlockRequired unless block x = [:origin][0] + bounds.absolute_left y = [:origin][1] + bounds.absolute_bottom x_prime = factor * x y_prime = factor * y translate(x - x_prime, y - y_prime) do transformation_matrix(factor, 0, 0, factor, 0, 0, &block) end end end |
#transformation_matrix(*matrix) { ... } ⇒ void
This method returns an undefined value.
Transform the user space (see notes for rotate regarding graphics state) Generally, one would use the #rotate, #scale, and #translate convenience methods instead of calling transformation_matrix directly
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/prawn/graphics/transformation.rb', line 154 def transformation_matrix(*matrix) if matrix.length != 6 raise ArgumentError, 'Transformation matrix must have exacty 6 elements' end save_graphics_state if block_given? add_to_transformation_stack(*matrix) values = PDF::Core.real_params(matrix) renderer.add_content("#{values} cm") if block_given? yield restore_graphics_state end end |
#translate(x, y) { ... } ⇒ void
This method returns an undefined value.
Translate the user space. If a block is not provided, then you must save and restore the graphics state yourself.
78 79 80 |
# File 'lib/prawn/graphics/transformation.rb', line 78 def translate(x, y, &block) transformation_matrix(1, 0, 0, 1, x, y, &block) end |