]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
* lily/include/lily-guile.hh: rename ly_c_X_p -> ly_is_X
[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   virtual void stop_translation_timestep ();
43   virtual void process_music ();
44   virtual void acknowledge_grob (Grob_info);
45
46 public:
47   TRANSLATOR_DECLARATIONS (Script_engraver);
48 };
49
50 Script_engraver::Script_engraver ()
51 {
52   slur_ = 0;
53 }
54
55 bool
56 Script_engraver::try_music (Music *m)
57 {
58   if (m->is_mus_type ("articulation-event"))
59     {
60       /* Discard double articulations for part-combining.  */
61       int script_count = scripts_.size ();
62       for (int i = 0; i < script_count; i++)
63         if (ly_is_equal (scripts_[i].event_
64                           ->get_property ("articulation-type"),
65                           m->get_property ("articulation-type")))
66           return true;
67
68       Script_tuple t;
69       t.event_ = m;
70       scripts_.push (t);
71       return true;
72     }
73   return false;
74 }
75
76 void
77 copy_property (Grob *g, SCM sym, SCM alist)
78 {
79   if (g->internal_get_property (sym) == SCM_EOL)
80     {
81       SCM entry = scm_assoc (sym, alist);
82       if (scm_is_pair (entry))
83         g->internal_set_property (sym, scm_cdr (entry));
84     }
85 }
86
87 /* Add the properties, one by one for each Script.  A little memory
88    could be saved by tacking the props onto the Script grob (i.e. make
89    ScriptStaccato , ScriptMarcato, etc. ).
90 */
91 void make_script_from_event (Grob *p, bool *follow, Context *tg,
92                              SCM art_type, int index)
93 {
94   SCM alist = tg->get_property ("scriptDefinitions");
95   SCM art = scm_assoc (art_type, alist);
96
97   if (art == SCM_BOOL_F)
98     {
99       /* FIXME: */
100       warning (_ ("don't know how to interpret articulation: "));
101       warning (_ ("scheme encoding: "));
102       scm_write (art_type, scm_current_error_port ());
103       message ("");
104       return;
105     }
106
107   art = scm_cdr (art);
108
109   SCM follow_scm = scm_assoc (ly_symbol2scm ("follow-into-staff"),
110                               art);
111
112   *follow = scm_is_pair (follow_scm) && to_boolean (scm_cdr (follow_scm));
113   bool priority_found = false;
114
115   for (SCM s = art; scm_is_pair (s); s = scm_cdr (s))
116     {
117       SCM sym = scm_caar (s);
118       SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
119       if (!ly_is_procedure (type))
120         continue;
121
122       SCM val = scm_cdar (s);
123
124       if (sym == ly_symbol2scm ("script-priority"))
125         {
126           priority_found = true;
127           /* Make sure they're in order of user input by adding index i.
128              Don't use the direction in this priority. Smaller means closer
129              to the head.  */
130           int prio = scm_to_int (val) + index;
131
132           val = scm_int2num (prio);
133         }
134       if (p->internal_get_property (sym) == SCM_EOL)
135         p->internal_set_property (sym, val);
136     }
137
138   if (!priority_found)
139     {
140       p->set_property ("script-priority",
141                        scm_int2num (index));
142     }
143
144   Side_position_interface::set_axis (p, Y_AXIS);
145 }
146
147 void
148 Script_engraver::process_music ()
149 {
150   int script_count = scripts_.size ();
151   for (int i = 0; i < script_count; i++)
152     {
153       Music *m = scripts_[i].event_;
154
155       Grob *p = make_item ("Script", m->self_scm ());
156
157       make_script_from_event (p, &scripts_[i].follow_into_staff_, context (),
158                               m->get_property ("articulation-type"),
159                               i);
160
161       scripts_[i].script_ = p;
162
163       SCM force_dir = m->get_property ("direction");
164       if (is_direction (force_dir) && to_dir (force_dir))
165         p->set_property ("direction", force_dir);
166     }
167 }
168
169 void
170 Script_engraver::acknowledge_grob (Grob_info info)
171 {
172   int script_count = scripts_.size ();
173   if (Stem::has_interface (info.grob ()))
174     {
175       for (int i = 0; i < script_count; i++)
176         {
177           Grob *e = scripts_[i].script_;
178
179           if (to_dir (e->get_property ("side-relative-direction")))
180             e->set_property ("direction-source", info.grob ()->self_scm ());
181
182           /* FIXME: add dependency */
183           e->add_dependency (info.grob ());
184           Side_position_interface::add_support (e, info.grob ());
185         }
186     }
187   else if (Rhythmic_head::has_interface (info.grob ())
188            && info.music_cause ())
189     {
190       for (int i = 0; i < script_count; i++)
191         {
192           Grob *e = scripts_[i].script_;
193
194           if (Side_position_interface::get_axis (e) == X_AXIS
195               && !e->get_parent (Y_AXIS))
196             {
197               e->set_parent (info.grob (), Y_AXIS);
198               e->add_dependency (info.grob ());
199             }
200           Side_position_interface::add_support (e, info.grob ());
201         }
202     }
203   else if (Note_column::has_interface (info.grob ()))
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::before_line_breaking ().  */
211       for (int i = 0; i < script_count; 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   else if (Slur::has_interface (info.grob ()))
221     slur_ = dynamic_cast<Spanner *> (info.grob ());
222 }
223
224 void
225 Script_engraver::stop_translation_timestep ()
226 {
227   int script_count = scripts_.size ();
228   for (int i = 0; i < script_count; i++)
229     if (scripts_[i].follow_into_staff_)
230       {
231         Grob *sc = scripts_[i].script_;
232         sc->add_offset_callback (Side_position_interface
233                                  ::quantised_position_proc, Y_AXIS);
234         sc->set_property ("staff-padding", SCM_EOL);
235       }
236
237   scripts_.clear ();
238 }
239
240 ADD_TRANSLATOR (Script_engraver,
241                 /* descr */ "Handles note scripted articulations.",
242                 /* creats*/ "Script",
243                 /* accepts */ "script-event articulation-event",
244                 /* acks  */ "stem-interface rhythmic-head-interface "
245                 "slur-interface note-column-interface",
246                 /* reads */ "scriptDefinitions",
247                 /* write */ "");