]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-heads-engraver.cc
Issue 4965: Create and use Grob::parent_relative
[lilypond.git] / lily / note-heads-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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 "engraver.hh"
21
22 #include <cctype>
23 using namespace std;
24
25 #include "duration.hh"
26 #include "item.hh"
27 #include "output-def.hh"
28 #include "pitch.hh"
29 #include "rhythmic-head.hh"
30 #include "staff-symbol-referencer.hh"
31 #include "stream-event.hh"
32 #include "warn.hh"
33
34 #include "translator.icc"
35
36 class Note_heads_engraver : public Engraver
37 {
38   vector<Stream_event *> note_evs_;
39
40 public:
41   TRANSLATOR_DECLARATIONS (Note_heads_engraver);
42
43 protected:
44   void listen_note (Stream_event *);
45   void process_music ();
46   void stop_translation_timestep ();
47 };
48
49 Note_heads_engraver::Note_heads_engraver ()
50 {
51 }
52
53 void
54 Note_heads_engraver::listen_note (Stream_event *ev)
55 {
56   note_evs_.push_back (ev);
57 }
58
59 void
60 Note_heads_engraver::process_music ()
61 {
62   SCM c0 = get_property ("middleCPosition");
63   SCM layout_proc = get_property ("staffLineLayoutFunction");
64
65   for (vsize i = 0; i < note_evs_.size (); i++)
66     {
67       Stream_event *ev = note_evs_[i];
68       Item *note = make_item ("NoteHead", ev->self_scm ());
69
70       Pitch *pit = unsmob<Pitch> (ev->get_property ("pitch"));
71
72 #if 0 /* TODO: should have a mechanism to switch off these warnings. */
73
74       if (!pit)
75         ev->origin ()->warning (_ ("NoteEvent without pitch"));
76 #endif
77
78       int pos;
79       if (pit == 0)
80         pos = 0;
81       else if (ly_is_procedure (layout_proc))
82         {
83           SCM pitch = ev->get_property ("pitch");
84           pos = scm_to_int (scm_call_1 (layout_proc, pitch));
85         }
86       else
87         pos = pit->steps ();
88
89       if (scm_is_number (c0))
90         pos += scm_to_int (c0);
91
92       note->set_property ("staff-position", scm_from_int (pos));
93
94       /*
95         Shape note heads change on step of the scale.
96       */
97       SCM shape_vector = get_property ("shapeNoteStyles");
98       if (scm_is_vector (shape_vector))
99         {
100           SCM scm_tonic = get_property ("tonic");
101           Pitch tonic;
102           if (unsmob<Pitch> (scm_tonic))
103             tonic = *unsmob<Pitch> (scm_tonic);
104
105           unsigned int delta = (pit->get_notename () - tonic.get_notename () + 7) % 7;
106
107           SCM style = SCM_EOL;
108           if (scm_c_vector_length (shape_vector) > delta
109               && scm_is_symbol (scm_vector_ref (shape_vector, scm_from_int (delta))))
110             style = scm_vector_ref (shape_vector, scm_from_int (delta));
111           if (scm_is_symbol (style))
112             note->set_property ("style", style);
113         }
114     }
115 }
116
117 void
118 Note_heads_engraver::stop_translation_timestep ()
119 {
120   note_evs_.clear ();
121 }
122
123 void
124 Note_heads_engraver::boot ()
125 {
126   ADD_LISTENER (Note_heads_engraver, note);
127 }
128
129 ADD_TRANSLATOR (Note_heads_engraver,
130                 /* doc */
131                 "Generate note heads.",
132
133                 /* create */
134                 "NoteHead ",
135
136                 /* read */
137                 "middleCPosition "
138                 "staffLineLayoutFunction ",
139
140                 /* write */
141                 ""
142                );