Class: Clef::Layout::Spacing

Inherits:
Object
  • Object
show all
Defined in:
lib/clef/layout/spacing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeline, style, extra_moments: []) ⇒ Spacing

Returns a new instance of Spacing.

Parameters:



11
12
13
14
15
16
# File 'lib/clef/layout/spacing.rb', line 11

def initialize(timeline, style, extra_moments: [])
  @timeline = timeline
  @style = style
  @extra_moments = extra_moments
  @stretch_factor = 1.0
end

Instance Attribute Details

#extra_momentsObject (readonly)

Returns the value of attribute extra_moments.



6
7
8
# File 'lib/clef/layout/spacing.rb', line 6

def extra_moments
  @extra_moments
end

#stretch_factorObject (readonly)

Returns the value of attribute stretch_factor.



6
7
8
# File 'lib/clef/layout/spacing.rb', line 6

def stretch_factor
  @stretch_factor
end

#styleObject (readonly)

Returns the value of attribute style.



6
7
8
# File 'lib/clef/layout/spacing.rb', line 6

def style
  @style
end

#timelineObject (readonly)

Returns the value of attribute timeline.



6
7
8
# File 'lib/clef/layout/spacing.rb', line 6

def timeline
  @timeline
end

Instance Method Details

#computeHash{Clef::Ir::Moment=>Float}

Returns:



19
20
21
22
23
24
# File 'lib/clef/layout/spacing.rb', line 19

def compute
  moments = (timeline.each_moment.to_a + extra_moments).uniq.sort
  return {} if moments.empty?

  build_positions(moments)
end

#stretch_to_fit(target_width) ⇒ Float

Parameters:

  • target_width (Numeric)

Returns:

  • (Float)


28
29
30
31
32
33
34
35
36
# File 'lib/clef/layout/spacing.rb', line 28

def stretch_to_fit(target_width)
  positions = compute
  return stretch_factor if positions.empty?

  current_width = positions.values.max
  return stretch_factor unless current_width.positive?

  @stretch_factor *= target_width.to_f / current_width
end