Module: Lydown::Rendering::Layout

Defined in:
lib/lydown/rendering/layout.rb

Constant Summary collapse

POINT_DIV =
{
  mm: 0.352778,
  cm: 3.52778
}
MEASUREMENT_RE =
/^([0-9\-\.]+)(mm|cm)$/

Class Method Summary collapse

Class Method Details

.calculate_vertical_margins(info) ⇒ Object

calculates inner vertical margins - that is, the space between the defined actual margin (used for header/footer) and the music/markup. an array of four values is returned: top-content, bottom-content



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lydown/rendering/layout.rb', line 65

def self.calculate_vertical_margins(info)
  ss = staff_space(info)
  
  dist = lambda do |k|
    (to_v(info["#{k}_content"]) - to_v(info[k])) / ss
  end
  
  staff_dist = lambda {|k| dist[k] + 2}
  
  [
    "%.1f" % staff_dist[:margin_top],
    "%.1f" % dist[:margin_top],
    "%.1f" % staff_dist[:margin_bottom]
  ]
end

.define_paper_size(layout) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lydown/rendering/layout.rb', line 8

def self.define_paper_size(layout)
  return "" unless layout[:paper]
  
  if layout[:paper] =~ /([^\s]+)\s?(portrait|landscape)?/
    size = $1
    orientation = $2 || 'portrait'
  else
    size = layout[:paper]
    orientation = 'portrait'
  end
  
  "#(set-paper-size \"#{size.downcase}\" '#{orientation})"
end

.fmt(v) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/lydown/rendering/layout.rb', line 41

def self.fmt(v)
  if v =~ MEASUREMENT_RE
    "#{$1}\\#{$2}"
  else
    raise "Invalid measurement #{v}"
  end
end

.staff_space(info) ⇒ Object

in points



58
59
60
# File 'lib/lydown/rendering/layout.rb', line 58

def self.staff_space(info)
  to_v(info[:staff_size]) / 4
end

.to_pp(v) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lydown/rendering/layout.rb', line 29

def self.to_pp(v)
  if v =~ MEASUREMENT_RE
    value = $1
    unit = $2
    
    factor = POINT_DIV[unit.to_sym]
    "%.1f" % (value.to_f / factor)
  else
    raise "Invalid measurement #{v}"
  end
end

.to_v(v) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/lydown/rendering/layout.rb', line 49

def self.to_v(v)
  if v =~ MEASUREMENT_RE
    $1.to_f
  else
    raise "Invalid measurement #{v}"
  end
end