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?
@output_width = container_width if @output_width.nil? && @output_height.nil?
@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?
@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