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