class PureDocx::Constructors::ImageSize

Constants

MAX_IMAGE_WIDTH
PX_EMU

Attributes

height[R]
real_height[R]
real_width[R]
width[R]

Public Class Methods

new(arguments = {}) click to toggle source
# File lib/puredocx/constructors/image_size.rb, line 9
def initialize(arguments = {})
  @width, @height           = arguments[:user_params]
  @real_width, @real_height = arguments[:real_params]
end

Public Instance Methods

prepare_size() click to toggle source
# File lib/puredocx/constructors/image_size.rb, line 14
def prepare_size
  new_width, new_height = set_new_size_params
  new_width, new_height = scaled_params(new_width, new_height) if new_width > MAX_IMAGE_WIDTH
  { width: new_width, height: new_height }.map { |key, value| [key, (value * PX_EMU)] }.to_h
end

Private Instance Methods

calculate_params() click to toggle source
# File lib/puredocx/constructors/image_size.rb, line 36
def calculate_params
  [
    (width  || height * real_width / real_height),
    (height || width  * real_height / real_width)
  ]
end
image_size_params_nil?() click to toggle source
# File lib/puredocx/constructors/image_size.rb, line 28
def image_size_params_nil?
  [height, width].all?(&:nil?)
end
image_size_params_present?() click to toggle source
# File lib/puredocx/constructors/image_size.rb, line 32
def image_size_params_present?
  [height, width].none?(&:nil?)
end
scaled_params(width, height) click to toggle source
# File lib/puredocx/constructors/image_size.rb, line 43
def scaled_params(width, height)
  size_factor = width.to_f / MAX_IMAGE_WIDTH
  [width, height].map { |value| size_factor > 1 ? (value / size_factor).to_i : value }
end
set_new_size_params() click to toggle source
# File lib/puredocx/constructors/image_size.rb, line 22
def set_new_size_params
  return [real_width, real_height] if image_size_params_nil?
  return [width, height]           if image_size_params_present?
  calculate_params
end