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