Class: Prawn::SVG::Calculators::DocumentSizing
- Inherits:
-
Object
- Object
- Prawn::SVG::Calculators::DocumentSizing
- Defined in:
- lib/prawn/svg/calculators/document_sizing.rb
Constant Summary collapse
- DEFAULT_ASPECT_RATIO =
'xMidYMid meet'.freeze
Instance Attribute Summary collapse
-
#bounds ⇒ Object
readonly
Returns the value of attribute bounds.
-
#document_height ⇒ Object
writeonly
Sets the attribute document_height.
-
#document_width ⇒ Object
writeonly
Sets the attribute document_width.
-
#output_height ⇒ Object
readonly
Returns the value of attribute output_height.
-
#output_width ⇒ Object
readonly
Returns the value of attribute output_width.
-
#preserve_aspect_ratio ⇒ Object
writeonly
Sets the attribute preserve_aspect_ratio.
-
#view_box ⇒ Object
writeonly
Sets the attribute view_box.
-
#viewport_diagonal ⇒ Object
readonly
Returns the value of attribute viewport_diagonal.
-
#viewport_height ⇒ Object
readonly
Returns the value of attribute viewport_height.
-
#viewport_width ⇒ Object
readonly
Returns the value of attribute viewport_width.
-
#x_offset ⇒ Object
readonly
Returns the value of attribute x_offset.
-
#x_scale ⇒ Object
readonly
Returns the value of attribute x_scale.
-
#y_offset ⇒ Object
readonly
Returns the value of attribute y_offset.
-
#y_scale ⇒ Object
readonly
Returns the value of attribute y_scale.
Instance Method Summary collapse
- #calculate ⇒ Object
-
#initialize(bounds, attributes = nil) ⇒ DocumentSizing
constructor
A new instance of DocumentSizing.
- #invalid? ⇒ Boolean
- #requested_height=(value) ⇒ Object
- #requested_width=(value) ⇒ Object
- #set_from_attributes(attributes) ⇒ Object
Constructor Details
#initialize(bounds, attributes = nil) ⇒ DocumentSizing
Returns a new instance of DocumentSizing.
12 13 14 15 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 12 def initialize(bounds, attributes = nil) @bounds = bounds set_from_attributes(attributes) if attributes end |
Instance Attribute Details
#bounds ⇒ Object (readonly)
Returns the value of attribute bounds.
8 9 10 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 8 def bounds @bounds end |
#document_height=(value) ⇒ Object (writeonly)
Sets the attribute document_height
5 6 7 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 5 def document_height=(value) @document_height = value end |
#document_width=(value) ⇒ Object (writeonly)
Sets the attribute document_width
5 6 7 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 5 def document_width=(value) @document_width = value end |
#output_height ⇒ Object (readonly)
Returns the value of attribute output_height.
10 11 12 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 10 def output_height @output_height end |
#output_width ⇒ Object (readonly)
Returns the value of attribute output_width.
10 11 12 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 10 def output_width @output_width end |
#preserve_aspect_ratio=(value) ⇒ Object (writeonly)
Sets the attribute preserve_aspect_ratio
6 7 8 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 6 def preserve_aspect_ratio=(value) @preserve_aspect_ratio = value end |
#view_box=(value) ⇒ Object (writeonly)
Sets the attribute view_box
6 7 8 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 6 def view_box=(value) @view_box = value end |
#viewport_diagonal ⇒ Object (readonly)
Returns the value of attribute viewport_diagonal.
10 11 12 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 10 def @viewport_diagonal end |
#viewport_height ⇒ Object (readonly)
Returns the value of attribute viewport_height.
10 11 12 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 10 def @viewport_height end |
#viewport_width ⇒ Object (readonly)
Returns the value of attribute viewport_width.
10 11 12 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 10 def @viewport_width end |
#x_offset ⇒ Object (readonly)
Returns the value of attribute x_offset.
9 10 11 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 9 def x_offset @x_offset end |
#x_scale ⇒ Object (readonly)
Returns the value of attribute x_scale.
9 10 11 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 9 def x_scale @x_scale end |
#y_offset ⇒ Object (readonly)
Returns the value of attribute y_offset.
9 10 11 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 9 def y_offset @y_offset end |
#y_scale ⇒ Object (readonly)
Returns the value of attribute y_scale.
9 10 11 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 9 def y_scale @y_scale end |
Instance Method Details
#calculate ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 24 def calculate @x_offset = @y_offset = 0 @x_scale = @y_scale = 1 container_width = @requested_width || @bounds[0] container_height = @requested_height || @bounds[1] @output_width = Pixels::Measurement.to_pixels(@document_width || @requested_width, container_width) @output_height = Pixels::Measurement.to_pixels(@document_height || @requested_height, container_height) if @view_box values = @view_box.strip.split(Prawn::SVG::Elements::COMMA_WSP_REGEXP) @x_offset, @y_offset, @viewport_width, @viewport_height = values.map(&:to_f) if @viewport_width.positive? && @viewport_height.positive? # If neither the width nor height was specified, use the entire width and the viewbox ratio # to determine the height. @output_width = container_width if @output_width.nil? && @output_height.nil? # If one of the output dimensions is missing, calculate it from the other one # using the ratio of the viewport width to height. @output_width ||= @output_height * @viewport_width / @viewport_height @output_height ||= @output_width * @viewport_height / @viewport_width aspect = AspectRatio.new(@preserve_aspect_ratio, [@output_width, @output_height], [@viewport_width, @viewport_height]) @x_scale = aspect.width / @viewport_width @y_scale = aspect.height / @viewport_height @x_offset -= aspect.x / @x_scale @y_offset -= aspect.y / @y_scale end else @output_width ||= container_width @output_height ||= container_height @viewport_width = @output_width @viewport_height = @output_height end return if invalid? # SVG 1.1 section 7.10 @viewport_diagonal = Math.sqrt((@viewport_width**2) + (@viewport_height**2)) / Math.sqrt(2) if @requested_width scale = @requested_width / @output_width @output_width = @requested_width @output_height *= scale @x_scale *= scale @y_scale *= scale elsif @requested_height scale = @requested_height / @output_height @output_height = @requested_height @output_width *= scale @x_scale *= scale @y_scale *= scale end self end |
#invalid? ⇒ Boolean
86 87 88 89 90 91 92 93 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 86 def invalid? @viewport_width <= 0 || @viewport_height <= 0 || @output_width <= 0 || @output_height <= 0 || (@requested_width && @requested_width <= 0) || (@requested_height && @requested_height <= 0) end |
#requested_height=(value) ⇒ Object
99 100 101 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 99 def requested_height=(value) @requested_height = value&.to_f end |
#requested_width=(value) ⇒ Object
95 96 97 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 95 def requested_width=(value) @requested_width = value&.to_f end |
#set_from_attributes(attributes) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/prawn/svg/calculators/document_sizing.rb', line 17 def set_from_attributes(attributes) @document_width = attributes['width'] @document_height = attributes['height'] @view_box = attributes['viewBox'] @preserve_aspect_ratio = attributes['preserveAspectRatio'] || DEFAULT_ASPECT_RATIO end |