Class: Lydown::Rendering::Setting

Inherits:
Base
  • Object
show all
Includes:
Notes
Defined in:
lib/lydown/rendering/settings.rb

Constant Summary collapse

SETTING_KEYS =
%w{
  key time pickup clef part movement tempo accidentals beams end_barline 
  macros empty_staves midi_tempo instrument_names instrument_name_style 
  parts score movement_source colla_parte include require mode nomode 
  bar_numbers document notation_size work transpose editions layout
  movement_title
}
RENDERABLE_SETTING_KEYS =
[
  'key', 'time', 'clef', 'beams'
]
ALLOWED_SETTING_VALUES =
{
  'accidentals' => ['manual', 'auto'],
  'beams' => ['manual', 'auto'],
  'empty_staves' => ['hide', 'show'],
  'instrument_names' => ['hide', 'show', 'inline', 'inline-right-align', 'inline-center-align'],
  'instrument_name_style' => ['normal', 'smallcaps'],
  'page_break' => ['none', 'before', 'after', 'before and after', 'blank page before', 'bookpart before'],
  'mode' => ['score', 'part', 'none'],
  'bar_numbers' => ['hide', 'shows'],
  'notation_size' => ['huge', 'large', 'normalsize', 'small', 'tiny', 'teeny']
}

Constants included from Notes

Notes::ADD_LINK_COMMAND, Notes::DYNAMICS, Notes::LILYPOND_EXPRESSIONS, Notes::MARKUP_ALIGNMENT, Notes::TEXTMATE_URL, Notes::TRANSPARENT_NOTE, Notes::TRANSPARENT_TIE

Constants included from Figures

Figures::ALTERATION, Figures::ALTERATION_RE, Figures::BLANK_EXTENDER, Figures::BLANK_EXTENDER_START, Figures::BLANK_EXTENDER_STOP, Figures::EXTENDERS_OFF, Figures::EXTENDERS_ON, Figures::HIDDEN_FORMAT

Instance Method Summary collapse

Methods included from Notes

#add_chord, add_duration_macro_group, #add_macro_event, #add_macro_note, #add_note, cleanup_duration_macro, #cross_bar_dot_lilypond_note, #lilypond_chord, #lilypond_note, #lilypond_phrasing, #lydown_phrasing_close, #lydown_phrasing_open, #note_event_url_link, #translate_expressions, #translate_string_expression

Methods included from Figures

#add_figures, #add_stand_alone_figures, #check_tenues, #lilypond_figures, #next_figures_event, #translate_figures

Methods inherited from Base

#find_prev_event, #initialize, #next_event, #prev_event

Constructor Details

This class inherits a constructor from Lydown::Rendering::Base

Instance Method Details

#add_include(includes_path, path) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/lydown/rendering/settings.rb', line 179

def add_include(includes_path, path)
  includes = @context.get_current_setting(includes_path) || []

  source_filename = @context['process/last_filename']
  if source_filename
    absolute_path = File.expand_path(
      File.join(File.dirname(source_filename), path)
    )
    # calculate relative path to working directory
    pwd = Pathname.new(FileUtils.pwd)
    includes << Pathname.new(absolute_path).relative_path_from(pwd).to_s
  else
    includes << path
  end

  @context.set_setting(includes_path, includes)
end

#add_require(requires_path, package) ⇒ Object



205
206
207
208
209
# File 'lib/lydown/rendering/settings.rb', line 205

def add_require(requires_path, package)
  requires = @context.get_current_setting(requires_path) || []
  requires << package
  @context.set_setting(requires_path, requires)
end

#check_setting_value(key, value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/lydown/rendering/settings.rb', line 108

def check_setting_value(key, value)
  if key == 'key'
    # process shorthand notation
    if value =~ /^([a-gA-G])[\+\-#ß]*$/
      mode = $1.downcase == $1 ? 'minor' : 'major'
      value = "#{value.downcase} #{mode}"
    end
  elsif ALLOWED_SETTING_VALUES[key]
    unless ALLOWED_SETTING_VALUES[key].include?(value)
      raise LydownError, "Invalid value for setting #{key}: #{value.inspect}"
    end
  end
  value
end

#render_setting(key, value) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/lydown/rendering/settings.rb', line 123

def render_setting(key, value)
  setting = ""
  case key
  when 'time'
    cadenza_mode = @context[:cadenza_mode]
    should_cadence = value == 'unmetered'
    @context[:cadenza_mode] = should_cadence

    if should_cadence && !cadenza_mode
      setting = "\\cadenzaOn "
    elsif !should_cadence && cadenza_mode
      setting = "\\cadenzaOff "
    end

    unless should_cadence
      signature = value.sub(/[0-9]+$/) { |m| LILYPOND_DURATIONS[m] || m }
      setting << "\\time #{signature} "
    end
  when 'key'
    # If the next event is a key signature, no need to emit this one
    e = next_event
    return if e && (e[:type] == :setting) && (e[:key] == 'key')

    unless value =~ /^([A-Ga-g][\+\-#ß]*) (major|minor)$/
      raise LydownError, "Invalid key signature #{value.inspect}"
    end

    note = Lydown::Rendering::Accidentals.lilypond_note_name($1)
    mode = $2
    setting = "\\#{key} #{note} \\#{mode} "
  when 'clef'
    setting = "\\#{key} \"#{value}\" "
    # If no music is there, and we're rendering a clef command, we need 
    # tell lydown to not render a first clef command inside the Staff 
    # context.
    unless @context.get_current_setting(:got_music)
      @context.set_setting(:inhibit_first_clef, true)
    end
  when 'beams'
    setting = (value == 'manual') ? '\autoBeamOff ' : '\autoBeamOn '
  else
    setting = "\\#{key} #{value} "
  end

  @context.emit(:music, setting)
end

#set_lyrics_markup(path, fn) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/lydown/rendering/settings.rb', line 197

def set_lyrics_markup(path, fn)
  source_filename = @context['process/last_filename']
  if source_filename
    fn = File.expand_path(File.join(File.dirname(source_filename), fn))
  end
  @context.set_setting("#{path}lyrics_markup", IO.read(fn))
end

#set_mode(mode) ⇒ Object



170
171
172
# File 'lib/lydown/rendering/settings.rb', line 170

def set_mode(mode)
  @context['process/mode'] = (mode == :none) ? nil : mode
end

#set_staff_order(path, order) ⇒ Object



174
175
176
177
# File 'lib/lydown/rendering/settings.rb', line 174

def set_staff_order(path, order)
  order = order.split(',').map {|s| s.strip}
  @context.set_setting("#{path}order", order)
end

#translateObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lydown/rendering/settings.rb', line 34

def translate
  # if setting while doing a macro, insert it into the current macro group
  if @context['process/duration_macro'] && @event[:raw]
    return add_macro_event(@event[:raw])
  end
  
  key = @event[:key]
  value = @event[:value] && @event[:value].gsub("\\n", "\n")
  level = @event[:level] || 0
  
  unless (level > 0) || SETTING_KEYS.include?(key)
    raise LydownError, "Invalid setting (#{key})"
  end

  value = check_setting_value(key, value)

  if level == 0
    movement = @context[:movement]
    case key
    when 'part'
      @context[:part] = value
      @context.set_part_context(value)
      
      # when changing parts we repeat the last set time and key signature
      time = @context.get_current_setting(:time)
      key =  @context.get_current_setting(:key)

      render_setting('time', time) unless time == '4/4'
      render_setting('key', key) unless key == 'c major'

      @context.reset(:part)
    when 'movement'
      @context[:movement] = value
      @context.reset(:movement)
    when 'include'
      add_include(:includes, value)
    when 'require'
      add_require(:requires, value)
    when 'mode'
      set_mode(value.nil? ? :none : value.to_sym)
    when 'nomode'
      set_mode(:none)
    else
      @context.set_setting(key, value) unless @event[:ephemeral]
    end

    if RENDERABLE_SETTING_KEYS.include?(key)
      render_setting(key, value)
    end
  else
    # nested settings
    l, path = 0, ''
    while l < level
      path << "#{@context['process/setting_levels'][l]}/"; l += 1
    end
    case key
    when 'include'
      add_include(path + 'includes', value)
    when 'require'
      add_require(path + 'requires', value)
    when 'lyrics_markup_file'
      set_lyrics_markup(path, value)
    when 'order' # staff order
      set_staff_order(path, value)
    else
      path << key
      @context.set_setting(path, value)
    end
  end

  @context['process/setting_levels'] ||= {}
  @context['process/setting_levels'][level] = key
end