]> git.donarmstrong.com Git - lilypond.git/blob - lily/tab-note-heads-engraver.cc
Cleanup: remove Simple_font_metric, Font_metric::text_dimension
[lilypond.git] / lily / tab-note-heads-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2010 Han-Wen Nienhuys, Jean-Baptiste Lamy <jiba@tuxfamily.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 <cctype>
21 #include <cstdio>
22
23 #include "engraver.hh"
24
25 using namespace std;
26
27 #include "duration.hh"
28 #include "item.hh"
29 #include "output-def.hh"
30 #include "pitch.hh"
31 #include "rhythmic-head.hh"
32 #include "stream-event.hh"
33 #include "warn.hh"
34 #include "context.hh"
35
36 #include "translator.icc"
37
38 /**
39    make (guitar-like) tablature note
40 */
41 class Tab_note_heads_engraver : public Engraver
42 {
43   vector<Item*> notes_;
44
45   vector<Stream_event*> note_events_;
46   vector<Stream_event*> tabstring_events_;
47 public:
48   TRANSLATOR_DECLARATIONS (Tab_note_heads_engraver);
49
50 protected:
51   DECLARE_TRANSLATOR_LISTENER (note);
52   DECLARE_TRANSLATOR_LISTENER (string_number);
53   void process_music ();
54
55   void stop_translation_timestep ();
56 };
57
58 Tab_note_heads_engraver::Tab_note_heads_engraver ()
59 {
60 }
61
62 IMPLEMENT_TRANSLATOR_LISTENER (Tab_note_heads_engraver, note);
63 void
64 Tab_note_heads_engraver::listen_note (Stream_event *ev)
65 {
66   note_events_.push_back (ev);
67 }
68
69 IMPLEMENT_TRANSLATOR_LISTENER (Tab_note_heads_engraver, string_number);
70 void
71 Tab_note_heads_engraver::listen_string_number (Stream_event *ev)
72 {
73   tabstring_events_.push_back (ev);
74 }
75
76 void
77 Tab_note_heads_engraver::process_music ()
78 {
79   vsize j = 0;
80
81   vector<Stream_event *> string_events;
82
83   for (vsize i = 0; i < note_events_.size (); i++)
84     {
85
86       Stream_event *event = note_events_[i];
87
88       Stream_event *tabstring_event = 0;
89
90       /*
91          For notes inside a chord construct, string indications are
92          stored as articulations on the note, so we check through
93          the notes
94       */
95       for (SCM s = event->get_property ("articulations");
96            !tabstring_event && scm_is_pair (s); s = scm_cdr (s))
97         {
98           Stream_event *art = unsmob_stream_event (scm_car (s));
99
100           if (art->in_event_class ("string-number-event"))
101             tabstring_event = art;
102         }
103
104       /*
105          For string indications listed outside a chord construct,
106          a string_number_event is generated, so if there was no string
107          in the articulations, we check for string events outside
108          the chord construct
109       */
110       if (!tabstring_event && j < tabstring_events_.size ())
111         {
112           tabstring_event = tabstring_events_[j];
113           if (j + 1 < tabstring_events_.size ())
114             j++;
115         }
116       if (tabstring_event)
117         string_events.push_back (tabstring_event);
118     }
119
120   SCM tab_notes = ly_cxx_vector_to_list (note_events_);
121   SCM tab_strings = SCM_EOL;
122   if (string_events.size ())
123     tab_strings = ly_cxx_vector_to_list (string_events);
124   SCM proc = get_property ("noteToFretFunction");
125   SCM string_fret_finger = SCM_EOL;
126   if (ly_is_procedure (proc))
127     string_fret_finger = scm_call_3 (proc,
128                                      context ()->self_scm (),
129                                      tab_notes,
130                                      tab_strings);
131   SCM note_entry = SCM_EOL;
132   SCM string_number = SCM_EOL;
133   SCM fret = SCM_EOL;
134   SCM fret_label = SCM_EOL;
135   SCM fret_procedure = get_property ("tablatureFormat");
136   SCM staff_line_procedure = get_property ("tabStaffLineLayoutFunction");
137   SCM staff_position = SCM_EOL;
138   vsize fret_count = (vsize) scm_ilength (string_fret_finger);
139   bool length_changed = (note_events_.size () != fret_count);
140   vsize index;
141
142   if (string_fret_finger != SCM_EOL)
143     for (vsize i=0; i < fret_count; i++)
144       {
145          note_entry = scm_list_ref (string_fret_finger, scm_from_int (i));
146          string_number = scm_car (note_entry);
147          fret = scm_cadr (note_entry);
148          fret_label = scm_call_3 (fret_procedure,
149                                   context ()->self_scm (),
150                                   string_number,
151                                   fret);
152          index = length_changed ? 0 : i;
153          Item *note = make_item ("TabNoteHead", note_events_[index]->self_scm ());
154          note->set_property ("text", fret_label);
155          staff_position = scm_call_2 (staff_line_procedure,
156                                       context ()->self_scm (),
157                                       string_number);
158          note->set_property ("staff-position", staff_position);
159          notes_.push_back (note);
160       }
161 }
162
163 void
164 Tab_note_heads_engraver::stop_translation_timestep ()
165 {
166   notes_.clear ();
167   note_events_.clear ();
168   tabstring_events_.clear ();
169 }
170
171 ADD_TRANSLATOR (Tab_note_heads_engraver,
172                 /* doc */
173                 "Generate one or more tablature noteheads from event of type"
174                 " @code{NoteEvent}.",
175
176                 /* create */
177                 "TabNoteHead ",
178
179                 /* read */
180                 "fretLabels "
181                 "highStringOne "
182                 "middleCPosition "
183                 "minimumFret "
184                 "noteToFretFunction "
185                 "stringOneTopmost "
186                 "stringTunings "
187                 "tablatureFormat "
188                 "tabStaffLineLayoutFunction ",
189
190                 /* write */ ""
191                 );
192