lib/prawn/svg/calculators/document_sizing.rb in prawn-svg-0.27.0 vs lib/prawn/svg/calculators/document_sizing.rb in prawn-svg-0.27.1

- old
+ new

@@ -26,24 +26,38 @@ @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 || container_width, container_width) - @output_height = Pixels::Measurement.to_pixels(@document_height || container_height, container_height) + @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 {|value| value.to_f} if @viewport_width > 0 && @viewport_height > 0 + # If neither the width nor height was specified, use the entire space available + if @output_width.nil? && @output_height.nil? + @output_width = container_width + @output_height = container_height + end + + # 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?