]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
* The grand 2005-2006 replace.
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.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 (stem_tremolo);
47   DECLARE_ACKNOWLEDGER (note_column);
48
49 public:
50   TRANSLATOR_DECLARATIONS (Script_engraver);
51 };
52
53 Script_engraver::Script_engraver ()
54 {
55   slur_ = 0;
56 }
57
58 bool
59 Script_engraver::try_music (Music *m)
60 {
61   if (m->is_mus_type ("articulation-event"))
62     {
63       /* Discard double articulations for part-combining.  */
64       int script_count = scripts_.size ();
65       for (int i = 0; i < script_count; i++)
66         if (ly_is_equal (scripts_[i].event_
67                          ->get_property ("articulation-type"),
68                          m->get_property ("articulation-type")))
69           return true;
70
71       Script_tuple t;
72       t.event_ = m;
73       scripts_.push (t);
74       return true;
75     }
76   return false;
77 }
78
79 void
80 copy_property (Grob *g, SCM sym, SCM alist)
81 {
82   if (g->internal_get_property (sym) == SCM_EOL)
83     {
84       SCM entry = scm_assoc (sym, alist);
85       if (scm_is_pair (entry))
86         g->internal_set_property (sym, scm_cdr (entry));
87     }
88 }
89
90 /* Add the properties, one by one for each Script.  A little memory
91    could be saved by tacking the props onto the Script grob (i.e. make
92    ScriptStaccato , ScriptMarcato, etc. ).
93 */
94 void make_script_from_event (Grob *p,  Context *tg,
95                              SCM art_type, int index)
96 {
97   SCM alist = tg->get_property ("scriptDefinitions");
98   SCM art = scm_assoc (art_type, alist);
99
100   if (art == SCM_BOOL_F)
101     {
102       /* FIXME: */
103       warning (_ ("don't know how to interpret articulation: "));
104       warning (_ ("scheme encoding: "));
105       scm_write (art_type, scm_current_error_port ());
106       message ("");
107       return;
108     }
109
110   art = scm_cdr (art);
111
112   bool priority_found = false;
113
114   for (SCM s = art; scm_is_pair (s); s = scm_cdr (s))
115     {
116       SCM sym = scm_caar (s);
117       SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
118       if (!ly_is_procedure (type))
119         continue;
120
121       SCM val = scm_cdar (s);
122
123       if (sym == ly_symbol2scm ("script-priority"))
124         {
125           priority_found = true;
126           /* Make sure they're in order of user input by adding index i.
127              Don't use the direction in this priority. Smaller means closer
128              to the head.  */
129           int prio = scm_to_int (val) + index;
130
131           val = scm_from_int (prio);
132         }
133
134       SCM preset = p->get_property_data (sym);
135       if (val == SCM_EOL
136           || scm_call_1 (type, preset) == SCM_BOOL_F)
137         p->internal_set_property (sym, val);
138     }
139
140   if (!priority_found)
141     {
142       p->set_property ("script-priority",
143                        scm_from_int (index));
144     }
145
146   Side_position_interface::set_axis (p, Y_AXIS);
147 }
148
149 void
150 Script_engraver::process_music ()
151 {
152   for (int i = 0; i < scripts_.size (); i++)
153     {
154       Music *music = scripts_[i].event_;
155
156       Grob *p = make_item ("Script", music->self_scm ());
157
158       make_script_from_event (p, context (),
159                               music->get_property ("articulation-type"),
160                               i);
161
162       scripts_[i].script_ = p;
163
164       SCM force_dir = music->get_property ("direction");
165       if (is_direction (force_dir) && to_dir (force_dir))
166         p->set_property ("direction", force_dir);
167     }
168 }
169
170 void
171 Script_engraver::acknowledge_stem (Grob_info info)
172 {
173   int script_count = scripts_.size ();
174   for (int i = 0; i < script_count; i++)
175     {
176       Grob *e = scripts_[i].script_;
177
178       if (to_dir (e->get_property ("side-relative-direction")))
179         e->set_object ("direction-source", info.grob ()->self_scm ());
180
181       Side_position_interface::add_support (e, info.grob ());
182     }
183 }
184
185 void
186 Script_engraver::acknowledge_stem_tremolo (Grob_info info)
187 {
188   int script_count = scripts_.size ();
189   for (int i = 0; i < script_count; i++)
190     {
191       Grob *e = scripts_[i].script_;
192       Side_position_interface::add_support (e, info.grob ());
193     }
194 }
195
196
197 void
198 Script_engraver::acknowledge_rhythmic_head (Grob_info info)
199 {
200   if (info.music_cause ())
201     {
202       for (int i = 0; i < scripts_.size (); i++)
203         {
204           Grob *e = scripts_[i].script_;
205
206           if (Side_position_interface::get_axis (e) == X_AXIS
207               && !e->get_parent (Y_AXIS))
208             {
209               e->set_parent (info.grob (), Y_AXIS);
210             }
211           Side_position_interface::add_support (e, info.grob ());
212         }
213     }
214 }
215
216 void
217 Script_engraver::acknowledge_note_column (Grob_info info)
218 {
219   /* Make note column the parent of the script.  That is not
220      correct, but due to seconds in a chord, noteheads may be
221      swapped around horizontally.
222
223      As the note head to put it on is not known now, postpone this
224      decision to Script_interface::calc_direction ().  */
225   for (int i = 0; i < scripts_.size (); i++)
226     {
227       Grob *e = scripts_[i].script_;
228
229       if (!e->get_parent (X_AXIS)
230           && Side_position_interface::get_axis (e) == Y_AXIS)
231         e->set_parent (info.grob (), X_AXIS);
232     }
233 }
234
235 void
236 Script_engraver::acknowledge_slur (Grob_info info)
237 {
238   slur_ = info.spanner ();
239 }
240
241 void
242 Script_engraver::stop_translation_timestep ()
243 {
244   scripts_.clear ();
245 }
246
247 #include "translator.icc"
248
249 ADD_ACKNOWLEDGER (Script_engraver, slur);
250 ADD_ACKNOWLEDGER (Script_engraver, rhythmic_head);
251 ADD_ACKNOWLEDGER (Script_engraver, stem);
252 ADD_ACKNOWLEDGER (Script_engraver, note_column);
253 ADD_ACKNOWLEDGER (Script_engraver, stem_tremolo);
254
255 ADD_TRANSLATOR (Script_engraver,
256                 /* doc */ "Handles note scripted articulations.",
257                 /* create */ "Script",
258                 /* accept */ "script-event articulation-event",
259                 /* read */ "scriptDefinitions",
260                 /* write */ "");