]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
* lily/rest.cc (y_offset_callback): merge function of 3 callbacks.
[lilypond.git] / lily / script-engraver.cc
1 /*
2   script-engraver.cc -- engrave Scripts: Articulations.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "context.hh"
10 #include "directional-element-interface.hh"
11 #include "engraver.hh"
12 #include "slur.hh"
13 #include "note-column.hh"
14 #include "paper-column.hh"
15 #include "rhythmic-head.hh"
16 #include "script-interface.hh"
17 #include "side-position-interface.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "stem.hh"
20 #include "warn.hh"
21
22 struct Script_tuple
23 {
24   Music *event_;
25   Grob *script_;
26   Script_tuple ()
27   {
28     event_ = 0;
29     script_ = 0;
30   }
31 };
32
33 class Script_engraver : public Engraver
34 {
35   Array<Script_tuple> scripts_;
36   Spanner *slur_;
37
38 protected:
39   virtual bool try_music (Music *);
40   void stop_translation_timestep ();
41   void process_music ();
42
43   DECLARE_ACKNOWLEDGER (slur);
44   DECLARE_ACKNOWLEDGER (rhythmic_head);
45   DECLARE_ACKNOWLEDGER (stem);
46   DECLARE_ACKNOWLEDGER (note_column);
47
48 public:
49   TRANSLATOR_DECLARATIONS (Script_engraver);
50 };
51
52 Script_engraver::Script_engraver ()
53 {
54   slur_ = 0;
55 }
56
57 bool
58 Script_engraver::try_music (Music *m)
59 {
60   if (m->is_mus_type ("articulation-event"))
61     {
62       /* Discard double articulations for part-combining.  */
63       int script_count = scripts_.size ();
64       for (int i = 0; i < script_count; i++)
65         if (ly_is_equal (scripts_[i].event_
66                          ->get_property ("articulation-type"),
67                          m->get_property ("articulation-type")))
68           return true;
69
70       Script_tuple t;
71       t.event_ = m;
72       scripts_.push (t);
73       return true;
74     }
75   return false;
76 }
77
78 void
79 copy_property (Grob *g, SCM sym, SCM alist)
80 {
81   if (g->internal_get_property (sym) == SCM_EOL)
82     {
83       SCM entry = scm_assoc (sym, alist);
84       if (scm_is_pair (entry))
85         g->internal_set_property (sym, scm_cdr (entry));
86     }
87 }
88
89 /* Add the properties, one by one for each Script.  A little memory
90    could be saved by tacking the props onto the Script grob (i.e. make
91    ScriptStaccato , ScriptMarcato, etc. ).
92 */
93 void make_script_from_event (Grob *p,  Context *tg,
94                              SCM art_type, int index)
95 {
96   SCM alist = tg->get_property ("scriptDefinitions");
97   SCM art = scm_assoc (art_type, alist);
98
99   if (art == SCM_BOOL_F)
100     {
101       /* FIXME: */
102       warning (_ ("don't know how to interpret articulation: "));
103       warning (_ ("scheme encoding: "));
104       scm_write (art_type, scm_current_error_port ());
105       message ("");
106       return;
107     }
108
109   art = scm_cdr (art);
110
111   bool priority_found = false;
112
113   for (SCM s = art; scm_is_pair (s); s = scm_cdr (s))
114     {
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;
119
120       SCM val = scm_cdar (s);
121
122       if (sym == ly_symbol2scm ("script-priority"))
123         {
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;
129
130           val = scm_from_int (prio);
131         }
132
133       SCM preset = p->get_property_data (sym);
134       if (scm_call_1 (type, preset) == SCM_BOOL_F)
135         p->internal_set_property (sym, val);
136     }
137
138   if (!priority_found)
139     {
140       p->set_property ("script-priority",
141                        scm_from_int (index));
142     }
143
144   Side_position_interface::set_axis (p, Y_AXIS);
145 }
146
147 void
148 Script_engraver::process_music ()
149 {
150   for (int i = 0; i < scripts_.size (); i++)
151     {
152       Music *music = scripts_[i].event_;
153
154       Grob *p = make_item ("Script", music->self_scm ());
155
156       make_script_from_event (p, context (),
157                               music->get_property ("articulation-type"),
158                               i);
159
160       scripts_[i].script_ = p;
161
162       SCM force_dir = music->get_property ("direction");
163       if (is_direction (force_dir) && to_dir (force_dir))
164         p->set_property ("direction", force_dir);
165     }
166 }
167
168 void
169 Script_engraver::acknowledge_stem (Grob_info info)
170 {
171   int script_count = scripts_.size ();
172   for (int i = 0; i < script_count; i++)
173     {
174       Grob *e = scripts_[i].script_;
175
176       if (to_dir (e->get_property ("side-relative-direction")))
177         e->set_object ("direction-source", info.grob ()->self_scm ());
178
179       Side_position_interface::add_support (e, info.grob ());
180     }
181 }
182
183 void
184 Script_engraver::acknowledge_rhythmic_head (Grob_info info)
185 {
186   if (info.music_cause ())
187     {
188       for (int i = 0; i < scripts_.size (); i++)
189         {
190           Grob *e = scripts_[i].script_;
191
192           if (Side_position_interface::get_axis (e) == X_AXIS
193               && !e->get_parent (Y_AXIS))
194             {
195               e->set_parent (info.grob (), Y_AXIS);
196             }
197           Side_position_interface::add_support (e, info.grob ());
198         }
199     }
200 }
201
202 void
203 Script_engraver::acknowledge_note_column (Grob_info info)
204 {
205   /* Make note column the parent of the script.  That is not
206      correct, but due to seconds in a chord, noteheads may be
207      swapped around horizontally.
208
209      As the note head to put it on is not known now, postpone this
210      decision to Script_interface::calc_direction ().  */
211   for (int i = 0; i < scripts_.size (); i++)
212     {
213       Grob *e = scripts_[i].script_;
214
215       if (!e->get_parent (X_AXIS)
216           && Side_position_interface::get_axis (e) == Y_AXIS)
217         e->set_parent (info.grob (), X_AXIS);
218     }
219 }
220
221 void
222 Script_engraver::acknowledge_slur (Grob_info info)
223 {
224   slur_ = info.spanner ();
225 }
226
227 void
228 Script_engraver::stop_translation_timestep ()
229 {
230   scripts_.clear ();
231 }
232
233 #include "translator.icc"
234
235 ADD_ACKNOWLEDGER (Script_engraver, slur);
236 ADD_ACKNOWLEDGER (Script_engraver, rhythmic_head);
237 ADD_ACKNOWLEDGER (Script_engraver, stem);
238 ADD_ACKNOWLEDGER (Script_engraver, note_column);
239
240 ADD_TRANSLATOR (Script_engraver,
241                 /* doc */ "Handles note scripted articulations.",
242                 /* create */ "Script",
243                 /* accept */ "script-event articulation-event",
244                 /* read */ "scriptDefinitions",
245                 /* write */ "");