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.
Source Code
36 | def rotate(angle, options = {}, &block) |
37 | Prawn.verify_options(:origin, options) |
38 | rad = degree_to_rad(angle) |
39 | cos = Math.cos(rad) |
40 | sin = Math.sin(rad) |
41 | if options[:origin].nil? |
42 | transformation_matrix(cos, sin, -sin, cos, 0, 0, &block) |
43 | else
|
44 | raise Prawn::Errors::BlockRequired unless block |
45 | |
46 | x = options[:origin][0] + bounds.absolute_left |
47 | y = options[:origin][1] + bounds.absolute_bottom |
48 | x_prime = (x * cos) - (y * sin) |
49 | y_prime = (x * sin) + (y * cos) |
50 | translate(x - x_prime, y - y_prime) do |
51 | transformation_matrix(cos, sin, -sin, cos, 0, 0, &block) |
52 | end
|
53 | end
|
54 | 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.
Source Code
109 | def scale(factor, options = {}, &block) |
110 | Prawn.verify_options(:origin, options) |
111 | if options[:origin].nil? |
112 | transformation_matrix(factor, 0, 0, factor, 0, 0, &block) |
113 | else
|
114 | raise Prawn::Errors::BlockRequired unless block |
115 | |
116 | x = options[:origin][0] + bounds.absolute_left |
117 | y = options[:origin][1] + bounds.absolute_bottom |
118 | x_prime = factor * x |
119 | y_prime = factor * y |
120 | translate(x - x_prime, y - y_prime) do |
121 | transformation_matrix(factor, 0, 0, factor, 0, 0, &block) |
122 | end
|
123 | end
|
124 | 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
Source Code
154 | def transformation_matrix(*matrix) |
155 | if matrix.length != 6 |
156 | raise ArgumentError, |
157 | 'Transformation matrix must have exacty 6 elements'
|
158 | end
|
159 | save_graphics_state if block_given? |
160 | |
161 | add_to_transformation_stack(*matrix) |
162 | |
163 | values = PDF::Core.real_params(matrix) |
164 | renderer.add_content("#{values} cm") |
165 | if block_given? |
166 | yield
|
167 | restore_graphics_state
|
168 | end
|
169 | 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.
Source Code
78 | def translate(x, y, &block) |
79 | transformation_matrix(1, 0, 0, 1, x, y, &block) |
80 | end
|