]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name-engraver.cc
Doc-es: various updates.
[lilypond.git] / lily / chord-name-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2015 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   void listen_note (Stream_event *);
43   void listen_rest (Stream_event *);
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 (Context *c)
56   : Engraver (c)
57 {
58   rest_event_ = 0;
59 }
60
61 void
62 Chord_name_engraver::process_music ()
63 {
64   if (!rest_event_ && notes_.empty ())
65     return;
66
67   SCM markup;
68   SCM bass = SCM_EOL;
69   SCM inversion = SCM_EOL;
70   SCM pitches = SCM_EOL;
71   Item *chord_name = 0;
72
73   // rest events present a hen-and-egg problem with regard to
74   // overriding the text property of the ChordName grob since we
75   // cannot create a ChordName grob, look at its text property and, if
76   // not set, use noChordSymbol to decide whether we should not have
77   // created the grob in the first place.
78   if (rest_event_)
79     {
80       SCM no_chord_markup = get_property ("noChordSymbol");
81       if (!Text_interface::is_markup (no_chord_markup))
82         return;
83       markup = no_chord_markup;
84       chord_name = make_item ("ChordName", rest_event_->self_scm ());
85       chord_name->set_property ("text", markup);
86     }
87   else
88     {
89       chord_name = make_item ("ChordName", notes_[0]->self_scm ());
90       // We cannot actually delay fetching the text property in case
91       // it is a callback since we need to compare the generated
92       // markups for the sake of chordChanges
93       markup = chord_name->get_property ("text");
94       if (!Text_interface::is_markup (markup))
95         {
96           for (vsize i = 0; i < notes_.size (); i++)
97             {
98               Stream_event *n = notes_[i];
99               SCM p = n->get_property ("pitch");
100               if (!unsmob<Pitch> (p))
101                 continue;
102
103               if (to_boolean (n->get_property ("bass")))
104                 bass = p;
105               else
106                 {
107                   SCM oct = n->get_property ("octavation");
108                   if (scm_is_number (oct))
109                     {
110                       Pitch orig = unsmob<Pitch> (p)->transposed (Pitch (-scm_to_int (oct), 0));
111                       pitches = scm_cons (orig.smobbed_copy (), pitches);
112                     }
113                   else
114                     pitches = scm_cons (p, pitches);
115                   if (to_boolean (n->get_property ("inversion")))
116                     {
117                       inversion = p;
118                       if (!scm_is_number (oct))
119                         programming_error ("inversion does not have original pitch");
120                     }
121                 }
122             }
123
124           pitches = scm_sort_list (pitches, Pitch::less_p_proc);
125
126           SCM name_proc = get_property ("chordNameFunction");
127           markup = scm_call_4 (name_proc, pitches, bass, inversion,
128                                context ()->self_scm ());
129           if (!Text_interface::is_markup (markup))
130             {
131               // Ugh, we created a grob, now we better populate it.
132               // Use an empty string.
133               markup = scm_string (SCM_EOL);
134             }
135           chord_name->set_property ("text", markup);
136         }
137     }
138
139   SCM chord_changes = get_property ("chordChanges");
140   SCM last_chord = get_property ("lastChord");
141   if (to_boolean (chord_changes) && ly_is_equal (markup, last_chord))
142     chord_name->set_property ("begin-of-line-visible", SCM_BOOL_T);
143
144   context ()->set_property ("lastChord", markup);
145 }
146
147 void
148 Chord_name_engraver::listen_note (Stream_event *ev)
149 {
150   notes_.push_back (ev);
151 }
152
153 void
154 Chord_name_engraver::listen_rest (Stream_event *ev)
155 {
156   ASSIGN_EVENT_ONCE (rest_event_, ev);
157 }
158
159 void
160 Chord_name_engraver::stop_translation_timestep ()
161 {
162   notes_.clear ();
163   rest_event_ = 0;
164 }
165
166 /*
167   The READs description is not strictly accurate:
168   which properties are read depend on the chord naming function active.
169 */
170 void
171 Chord_name_engraver::boot ()
172 {
173   ADD_LISTENER (Chord_name_engraver, note);
174   ADD_LISTENER (Chord_name_engraver, rest);
175 }
176
177 ADD_TRANSLATOR (Chord_name_engraver,
178                 /* doc */
179                 "Catch note and rest events and generate the appropriate chordname.",
180
181                 /* create */
182                 "ChordName ",
183
184                 /* read */
185                 "chordChanges "
186                 "chordNameExceptions "
187                 "chordNameFunction "
188                 "chordNoteNamer "
189                 "chordRootNamer "
190                 "chordNameExceptions "
191                 "lastChord "
192                 "majorSevenSymbol "
193                 "noChordSymbol ",
194
195                 /* write */
196                 "lastChord "
197                );