]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name-engraver.cc
b4f1dd33be1384acb258619032f2a8bc80483544
[lilypond.git] / lily / chord-name-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2014 Jan Nieuwenhuizen <janneke@gnu.org>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "chord-name.hh"
21 #include "context.hh"
22 #include "dimensions.hh"
23 #include "engraver.hh"
24 #include "font-interface.hh"
25 #include "item.hh"
26 #include "output-def.hh"
27 #include "pitch.hh"
28 #include "protected-scm.hh"
29 #include "stream-event.hh"
30 #include "text-interface.hh"
31 #include "warn.hh"
32
33 #include "translator.icc"
34
35 class Chord_name_engraver : public Engraver
36 {
37   TRANSLATOR_DECLARATIONS (Chord_name_engraver);
38 protected:
39   void stop_translation_timestep ();
40   void process_music ();
41   virtual void finalize ();
42   DECLARE_TRANSLATOR_LISTENER (note);
43   DECLARE_TRANSLATOR_LISTENER (rest);
44 private:
45   vector<Stream_event *> notes_;
46
47   Stream_event *rest_event_;
48 };
49
50 void
51 Chord_name_engraver::finalize ()
52 {
53 }
54
55 Chord_name_engraver::Chord_name_engraver ()
56 {
57   rest_event_ = 0;
58 }
59
60 void
61 Chord_name_engraver::process_music ()
62 {
63   if (!rest_event_ && notes_.empty ())
64     return;
65
66   SCM markup;
67   SCM bass = SCM_EOL;
68   SCM inversion = SCM_EOL;
69   SCM pitches = SCM_EOL;
70   Item *chord_name = 0;
71
72   // rest events present a hen-and-egg problem with regard to
73   // overriding the text property of the ChordName grob since we
74   // cannot create a ChordName grob, look at its text property and, if
75   // not set, use noChordSymbol to decide whether we should not have
76   // created the grob in the first place.
77   if (rest_event_)
78     {
79       SCM no_chord_markup = get_property ("noChordSymbol");
80       if (!Text_interface::is_markup (no_chord_markup))
81         return;
82       markup = no_chord_markup;
83       chord_name = make_item ("ChordName", rest_event_->self_scm ());
84       chord_name->set_property ("text", markup);
85     }
86   else
87     {
88       chord_name = make_item ("ChordName", notes_[0]->self_scm ());
89       // We cannot actually delay fetching the text property in case
90       // it is a callback since we need to compare the generated
91       // markups for the sake of chordChanges
92       markup = chord_name->get_property ("text");
93       if (!Text_interface::is_markup (markup))
94         {
95           for (vsize i = 0; i < notes_.size (); i++)
96             {
97               Stream_event *n = notes_[i];
98               SCM p = n->get_property ("pitch");
99               if (!unsmob_pitch (p))
100                 continue;
101
102               if (n->get_property ("bass") == SCM_BOOL_T)
103                 bass = p;
104               else
105                 {
106                   SCM oct = n->get_property ("octavation");
107                   if (scm_is_number (oct))
108                     {
109                       Pitch orig = unsmob_pitch (p)->transposed (Pitch (-scm_to_int (oct), 0));
110                       pitches = scm_cons (orig.smobbed_copy (), pitches);
111                     }
112                   else
113                     pitches = scm_cons (p, pitches);
114                   if (n->get_property ("inversion") == SCM_BOOL_T)
115                     {
116                       inversion = p;
117                       if (!scm_is_number (oct))
118                         programming_error ("inversion does not have original pitch");
119                     }
120                 }
121             }
122
123           pitches = scm_sort_list (pitches, Pitch::less_p_proc);
124
125           SCM name_proc = get_property ("chordNameFunction");
126           markup = scm_call_4 (name_proc, pitches, bass, inversion,
127                                context ()->self_scm ());
128           if (!Text_interface::is_markup (markup))
129             {
130               // Ugh, we created a grob, now we better populate it.
131               // Use an empty string.
132               markup = scm_string (SCM_EOL);
133             }
134           chord_name->set_property ("text", markup);
135         }
136     }
137
138   SCM chord_changes = get_property ("chordChanges");
139   SCM last_chord = get_property ("lastChord");
140   if (to_boolean (chord_changes) && ly_is_equal (markup, last_chord))
141     chord_name->set_property ("begin-of-line-visible", SCM_BOOL_T);
142
143   context ()->set_property ("lastChord", markup);
144 }
145
146 IMPLEMENT_TRANSLATOR_LISTENER (Chord_name_engraver, note);
147 void
148 Chord_name_engraver::listen_note (Stream_event *ev)
149 {
150   notes_.push_back (ev);
151 }
152
153 IMPLEMENT_TRANSLATOR_LISTENER (Chord_name_engraver, rest);
154 void
155 Chord_name_engraver::listen_rest (Stream_event *ev)
156 {
157   ASSIGN_EVENT_ONCE (rest_event_, ev);
158 }
159
160 void
161 Chord_name_engraver::stop_translation_timestep ()
162 {
163   notes_.clear ();
164   rest_event_ = 0;
165 }
166
167 /*
168   The READs description is not strictly accurate:
169   which properties are read depend on the chord naming function active.
170 */
171 ADD_TRANSLATOR (Chord_name_engraver,
172                 /* doc */
173                 "Catch note and rest events and generate the appropriate chordname.",
174
175                 /* create */
176                 "ChordName ",
177
178                 /* read */
179                 "chordChanges "
180                 "chordNameExceptions "
181                 "chordNameFunction "
182                 "chordNoteNamer "
183                 "chordRootNamer "
184                 "chordNameExceptions "
185                 "lastChord "
186                 "majorSevenSymbol "
187                 "noChordSymbol ",
188
189                 /* write */
190                 "lastChord "
191                );