Merge branch 'fret-diagram-details'
[lilypond/csorensen.git] / lily / script-engraver.cc
blobd0d713bfc495c4b2b6965e2ce04e4a78a4274717
1 /*
2 script-engraver.cc -- engrave Scripts: Articulations.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "engraver.hh"
11 #include "context.hh"
12 #include "directional-element-interface.hh"
13 #include "international.hh"
14 #include "note-column.hh"
15 #include "paper-column.hh"
16 #include "rhythmic-head.hh"
17 #include "script-interface.hh"
18 #include "side-position-interface.hh"
19 #include "slur.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "stem.hh"
22 #include "stream-event.hh"
23 #include "warn.hh"
25 #include "translator.icc"
27 struct Script_tuple
29 Stream_event *event_;
30 Grob *script_;
31 Script_tuple ()
33 event_ = 0;
34 script_ = 0;
38 class Script_engraver : public Engraver
40 vector<Script_tuple> scripts_;
42 protected:
43 void stop_translation_timestep ();
44 void process_music ();
46 DECLARE_TRANSLATOR_LISTENER (articulation);
47 DECLARE_ACKNOWLEDGER (rhythmic_head);
48 DECLARE_ACKNOWLEDGER (stem);
49 DECLARE_ACKNOWLEDGER (stem_tremolo);
50 DECLARE_ACKNOWLEDGER (note_column);
52 public:
53 TRANSLATOR_DECLARATIONS (Script_engraver);
56 Script_engraver::Script_engraver ()
60 IMPLEMENT_TRANSLATOR_LISTENER (Script_engraver, articulation);
61 void
62 Script_engraver::listen_articulation (Stream_event *ev)
64 /* Discard double articulations for part-combining. */
65 int script_count = scripts_.size ();
66 for (int i = 0; i < script_count; i++)
67 if (ly_is_equal (scripts_[i].event_
68 ->get_property ("articulation-type"),
69 ev->get_property ("articulation-type")))
70 return;
72 Script_tuple t;
73 t.event_ = ev;
74 scripts_.push_back (t);
77 void
78 copy_property (Grob *g, SCM sym, SCM alist)
80 if (g->internal_get_property (sym) == SCM_EOL)
82 SCM entry = scm_assoc (sym, alist);
83 if (scm_is_pair (entry))
84 g->set_property (sym, scm_cdr (entry));
88 /* Add the properties, one by one for each Script. A little memory
89 could be saved by tacking the props onto the Script grob (i.e. make
90 ScriptStaccato , ScriptMarcato, etc. ).
92 void
93 make_script_from_event (Grob *p, Context *tg,
94 SCM art_type, int index)
96 SCM alist = tg->get_property ("scriptDefinitions");
97 SCM art = scm_assoc (art_type, alist);
99 if (art == SCM_BOOL_F)
101 /* FIXME: */
102 warning (_ ("do not know how to interpret articulation: "));
103 warning (_ ("scheme encoding: "));
104 scm_write (art_type, scm_current_error_port ());
105 message ("");
106 return;
109 art = scm_cdr (art);
111 bool priority_found = false;
113 for (SCM s = art; scm_is_pair (s); s = scm_cdr (s))
115 SCM sym = scm_caar (s);
116 SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
117 if (!ly_is_procedure (type))
118 continue;
120 SCM val = scm_cdar (s);
122 if (sym == ly_symbol2scm ("script-priority"))
124 priority_found = true;
125 /* Make sure they're in order of user input by adding index i.
126 Don't use the direction in this priority. Smaller means closer
127 to the head. */
128 int prio = scm_to_int (val) + index;
130 val = scm_from_int (prio);
133 SCM preset = p->get_property_data (sym);
134 if (val == SCM_EOL
135 || scm_call_1 (type, preset) == SCM_BOOL_F)
136 p->set_property (sym, val);
139 if (!priority_found)
141 p->set_property ("script-priority",
142 scm_from_int (index));
146 void
147 Script_engraver::process_music ()
149 for (vsize i = 0; i < scripts_.size (); i++)
151 Stream_event *ev = scripts_[i].event_;
153 Grob *p = make_item ("Script", ev->self_scm ());
155 make_script_from_event (p, context (),
156 ev->get_property ("articulation-type"),
159 scripts_[i].script_ = p;
161 SCM force_dir = ev->get_property ("direction");
162 if (is_direction (force_dir) && to_dir (force_dir))
163 p->set_property ("direction", force_dir);
167 void
168 Script_engraver::acknowledge_stem (Grob_info info)
170 int script_count = scripts_.size ();
171 for (int i = 0; i < script_count; i++)
173 Grob *e = scripts_[i].script_;
175 if (to_dir (e->get_property ("side-relative-direction")))
176 e->set_object ("direction-source", info.grob ()->self_scm ());
178 Side_position_interface::add_support (e, info.grob ());
182 void
183 Script_engraver::acknowledge_stem_tremolo (Grob_info info)
185 int script_count = scripts_.size ();
186 for (int i = 0; i < script_count; i++)
188 Grob *e = scripts_[i].script_;
189 Side_position_interface::add_support (e, info.grob ());
194 void
195 Script_engraver::acknowledge_rhythmic_head (Grob_info info)
197 if (info.event_cause ())
199 for (vsize i = 0; i < scripts_.size (); i++)
201 Grob *e = scripts_[i].script_;
203 if (Side_position_interface::get_axis (e) == X_AXIS
204 && !e->get_parent (Y_AXIS))
206 e->set_parent (info.grob (), Y_AXIS);
208 Side_position_interface::add_support (e, info.grob ());
213 void
214 Script_engraver::acknowledge_note_column (Grob_info info)
216 /* Make note column the parent of the script. That is not
217 correct, but due to seconds in a chord, noteheads may be
218 swapped around horizontally.
220 As the note head to put it on is not known now, postpone this
221 decision to Script_interface::calc_direction (). */
222 for (vsize i = 0; i < scripts_.size (); i++)
224 Grob *e = scripts_[i].script_;
226 if (!e->get_parent (X_AXIS)
227 && Side_position_interface::get_axis (e) == Y_AXIS)
228 e->set_parent (info.grob (), X_AXIS);
232 void
233 Script_engraver::stop_translation_timestep ()
235 scripts_.clear ();
238 ADD_ACKNOWLEDGER (Script_engraver, rhythmic_head);
239 ADD_ACKNOWLEDGER (Script_engraver, stem);
240 ADD_ACKNOWLEDGER (Script_engraver, note_column);
241 ADD_ACKNOWLEDGER (Script_engraver, stem_tremolo);
243 ADD_TRANSLATOR (Script_engraver,
244 /* doc */
245 "Handle note scripted articulations.",
247 /* create */
248 "Script ",
250 /* read */
251 "scriptDefinitions ",
253 /* write */