]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name-engraver.cc
Issue 3966: Revert "Allows the user to override the text property of ChordName"
[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   Item *chord_name_;
46   vector<Stream_event *> notes_;
47
48   Stream_event *rest_event_;
49 };
50
51 void
52 Chord_name_engraver::finalize ()
53 {
54 }
55
56 Chord_name_engraver::Chord_name_engraver ()
57 {
58   chord_name_ = 0;
59   rest_event_ = 0;
60 }
61
62 void
63 Chord_name_engraver::process_music ()
64 {
65   SCM markup;
66   SCM bass = SCM_EOL;
67   SCM inversion = SCM_EOL;
68   SCM pitches = SCM_EOL;
69
70   if (rest_event_)
71     {
72       SCM no_chord_markup = get_property ("noChordSymbol");
73       if (!Text_interface::is_markup (no_chord_markup))
74         return;
75       markup = no_chord_markup;
76     }
77   else
78     {
79       if (!notes_.size ())
80         return;
81
82       Stream_event *inversion_event = 0;
83       for (vsize i = 0; i < notes_.size (); i++)
84         {
85           Stream_event *n = notes_[i];
86           SCM p = n->get_property ("pitch");
87           if (!unsmob_pitch (p))
88             continue;
89
90           if (n->get_property ("inversion") == SCM_BOOL_T)
91             {
92               inversion_event = n;
93               inversion = p;
94             }
95           else if (n->get_property ("bass") == SCM_BOOL_T)
96             bass = p;
97           else
98             pitches = scm_cons (p, pitches);
99         }
100
101       if (inversion_event)
102         {
103           SCM oct = inversion_event->get_property ("octavation");
104           if (scm_is_number (oct))
105             {
106               Pitch *p = unsmob_pitch (inversion_event->get_property ("pitch"));
107               int octavation = scm_to_int (oct);
108               Pitch orig = p->transposed (Pitch (-octavation, 0, 0));
109
110               pitches = scm_cons (orig.smobbed_copy (), pitches);
111             }
112           else
113             programming_error ("inversion does not have original pitch");
114         }
115
116       pitches = scm_sort_list (pitches, Pitch::less_p_proc);
117
118       SCM name_proc = get_property ("chordNameFunction");
119       markup = scm_call_4 (name_proc, pitches, bass, inversion,
120                            context ()->self_scm ());
121     }
122   /*
123     Ugh.
124   */
125   SCM chord_as_scm = scm_cons (pitches, scm_cons (bass, inversion));
126
127   chord_name_ = make_item ("ChordName",
128                            rest_event_ ? rest_event_->self_scm () : notes_[0]->self_scm ());
129   chord_name_->set_property ("text", markup);
130
131   SCM chord_changes = get_property ("chordChanges");
132   SCM last_chord = get_property ("lastChord");
133   if (to_boolean (chord_changes) && scm_is_pair (last_chord)
134       && ly_is_equal (chord_as_scm, last_chord))
135     chord_name_->set_property ("begin-of-line-visible", SCM_BOOL_T);
136
137   context ()->set_property ("lastChord", chord_as_scm);
138 }
139
140 IMPLEMENT_TRANSLATOR_LISTENER (Chord_name_engraver, note);
141 void
142 Chord_name_engraver::listen_note (Stream_event *ev)
143 {
144   notes_.push_back (ev);
145 }
146
147 IMPLEMENT_TRANSLATOR_LISTENER (Chord_name_engraver, rest);
148 void
149 Chord_name_engraver::listen_rest (Stream_event *ev)
150 {
151   ASSIGN_EVENT_ONCE (rest_event_, ev);
152 }
153
154 void
155 Chord_name_engraver::stop_translation_timestep ()
156 {
157   chord_name_ = 0;
158   notes_.clear ();
159   rest_event_ = 0;
160 }
161
162 /*
163   The READs description is not strictly accurate:
164   which properties are read depend on the chord naming function active.
165 */
166 ADD_TRANSLATOR (Chord_name_engraver,
167                 /* doc */
168                 "Catch note and rest events and generate the appropriate chordname.",
169
170                 /* create */
171                 "ChordName ",
172
173                 /* read */
174                 "chordChanges "
175                 "chordNameExceptions "
176                 "chordNameFunction "
177                 "chordNoteNamer "
178                 "chordRootNamer "
179                 "chordNameExceptions "
180                 "lastChord "
181                 "majorSevenSymbol "
182                 "noChordSymbol ",
183
184                 /* write */
185                 "lastChord "
186                );