Class: LogoSoup::Core::DimensionCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/logosoup/core/dimension_calculator.rb

Overview

Computes normalized render dimensions from intrinsic dimensions.

Constant Summary collapse

REFERENCE_DENSITY =
0.35
MIN_DENSITY_SCALE =
0.5
MAX_DENSITY_SCALE =
2.0

Class Method Summary collapse

Class Method Details

.call(width:, height:, base_size:, scale_factor:, density_factor: 0.0, pixel_density: nil) ⇒ Array(Integer, Integer)

Parameters:

  • width (Numeric)
  • height (Numeric)
  • base_size (Numeric)
  • scale_factor (Numeric)
  • density_factor (Numeric) (defaults to: 0.0)
  • pixel_density (Float, nil) (defaults to: nil)

Returns:

  • (Array(Integer, Integer))


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/logosoup/core/dimension_calculator.rb', line 18

def self.call(width:, height:, base_size:, scale_factor:, density_factor: 0.0, pixel_density: nil)
  w = width.to_f
  h = height.to_f
  base = base_size.to_f

  return [base.round, base.round] if w <= 0 || h <= 0

  aspect_ratio = w / h
  normalized_width = (aspect_ratio**scale_factor.to_f) * base
  normalized_height = normalized_width / aspect_ratio

  df = density_factor.to_f
  if df.positive? && pixel_density
    density_ratio = pixel_density.to_f / REFERENCE_DENSITY
    if density_ratio.positive?
      density_scale = (1.0 / density_ratio)**(df * 0.5)
      clamped_scale = [[density_scale, MAX_DENSITY_SCALE].min, MIN_DENSITY_SCALE].max
      normalized_width *= clamped_scale
      normalized_height *= clamped_scale
    end
  end

  [normalized_width.round, normalized_height.round]
end