]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
* input/regression/slur-script-inside.ly: new file.
[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--2004 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 "event.hh"
13 #include "new-slur.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 "staff-symbol-referencer.hh"
20 #include "stem.hh"
21 #include "warn.hh"
22
23 struct Script_tuple
24 {
25   Music *event_;
26   Grob *script_;
27   bool follow_into_staff_;
28   Script_tuple ()
29   {
30     follow_into_staff_ = false;
31     event_ = 0;
32     script_ = 0;
33   }
34 };
35
36 class Script_engraver : public Engraver
37 {
38   Array<Script_tuple> scripts_;
39   Spanner *slur_;
40
41 protected:
42   virtual bool try_music (Music*);
43   virtual void stop_translation_timestep ();
44   virtual void process_music ();
45   virtual void acknowledge_grob (Grob_info);
46
47 public:
48   TRANSLATOR_DECLARATIONS (Script_engraver);
49 };
50
51 Script_engraver::Script_engraver ()
52 {
53   slur_ = 0;
54 }
55
56 bool
57 Script_engraver::try_music (Music *m)
58 {
59   if (m->is_mus_type ("articulation-event"))
60     {
61       /* Discard double articulations for part-combining.  */
62       int script_count = scripts_.size ();
63       for (int i = 0; i < script_count; i++)
64         if (ly_c_equal_p (scripts_[i].event_
65                           ->get_property ("articulation-type"),
66                           m->get_property ("articulation-type")))
67           return true;
68
69       Script_tuple t;
70       t.event_ = m;
71       scripts_.push (t);
72       return true;
73     }
74   return false;
75 }
76
77 void
78 copy_property (Grob *g, SCM sym, SCM alist)
79 {
80   if (g->internal_get_property (sym) == SCM_EOL)
81     {
82       SCM entry = scm_assoc (sym, alist);
83       if (ly_c_pair_p (entry))
84         g->internal_set_property (sym, ly_cdr (entry));
85     }
86 }
87
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. ).
91
92 */
93 void make_script_from_event (Grob *p, bool * follow, 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 (_ ("Do not know how to interpret articulation: "));
103       warning (_ ("Scheme encoding: "));
104       scm_write (art_type, scm_current_error_port ());
105       return;
106     }
107
108   art = ly_cdr (art);
109
110   SCM follow_scm = scm_assoc (ly_symbol2scm ("follow-into-staff"),
111                               art);
112
113   *follow = ly_c_pair_p (follow_scm) && to_boolean (ly_cdr (follow_scm));
114   bool priority_found = false ; 
115     
116   for (SCM s = art ; ly_c_pair_p (s); s = ly_cdr (s))
117     {
118       SCM sym = ly_caar (s);
119       SCM val = ly_cdar (s);
120
121       if (sym == ly_symbol2scm ("script-priority"))
122         {
123           priority_found = true;
124           /* Make sure they're in order of user input by adding index i.
125              Don't use the direction in this priority. Smaller means closer
126              to the head.  */
127           int prio  = ly_scm2int (val) +  index;
128           
129          
130           val = scm_int2num (prio);
131         }
132       if (p->internal_get_property (sym) == SCM_EOL)
133         p->internal_set_property (sym, val);
134     }
135
136   if (!priority_found)
137     {
138       p->set_property ("script-priority",
139                        scm_int2num (index));
140     }
141   
142   Side_position_interface::set_axis (p, Y_AXIS);
143
144 }
145
146 void
147 Script_engraver::process_music ()
148 {
149   int script_count = scripts_.size ();
150   for (int i = 0; i < script_count; i++)
151     {
152       Music *m = scripts_[i].event_;
153
154       Grob *p = make_item ("Script", m->self_scm ());
155
156       make_script_from_event (p, &scripts_[i].follow_into_staff_, context (),
157                               m->get_property ("articulation-type"),
158                               i);
159
160       scripts_[i].script_ = p;
161
162       SCM force_dir = m->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_grob (Grob_info info)
170 {
171   int script_count = scripts_.size ();
172   if (Stem::has_interface (info.grob_))
173     {
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_property ("direction-source", info.grob_->self_scm ());
180
181           /* FIXME: add dependency */
182           e->add_dependency (info.grob_);
183           Side_position_interface::add_support (e, info.grob_);
184         }
185     }
186   else if (Rhythmic_head::has_interface (info.grob_)
187            && info.music_cause ())
188     {
189       for (int i = 0; i < script_count; i++)
190         {
191           Grob *e = scripts_[i].script_;
192         
193           if (Side_position_interface::get_axis (e) == X_AXIS
194               && !e->get_parent (Y_AXIS))
195             {
196               e->set_parent (info.grob_, Y_AXIS);
197               e->add_dependency (info.grob_);
198             }
199           Side_position_interface::add_support (e,info.grob_);
200         }
201     }
202   else if (Note_column::has_interface (info.grob_))
203     {
204       /* Make note column the parent of the script.  That is not
205         correct, but due to seconds in a chord, noteheads may be
206         swapped around horizontally.
207
208         As the note head to put it on is not known now, postpone this
209         decision to Script_interface::before_line_breaking ().  */
210       for (int i = 0; i < script_count; i++)
211         {
212           Grob *e = scripts_[i].script_;
213
214           if (!e->get_parent (X_AXIS)
215               && Side_position_interface::get_axis (e) == Y_AXIS)
216             e->set_parent (info.grob_, X_AXIS);
217         }
218     }
219   else if (New_slur::has_interface (info.grob_))
220     slur_ = dynamic_cast<Spanner*> (info.grob_);
221 }
222
223 void
224 Script_engraver::stop_translation_timestep ()
225 {
226   int script_count = scripts_.size ();
227   for (int i = 0; i < script_count; i++)
228     if (scripts_[i].follow_into_staff_)
229       {
230         Grob *sc = scripts_[i].script_;
231         sc->add_offset_callback (Side_position_interface
232                                  ::quantised_position_proc, Y_AXIS);
233         sc->set_property ("staff-padding", SCM_EOL);
234       }
235   
236   scripts_.clear ();
237 }
238
239 ENTER_DESCRIPTION (Script_engraver,
240 /* descr */       "Handles note scripted articulations.",
241 /* creats*/       "Script",
242 /* accepts */     "script-event articulation-event",
243 /* acks  */       "stem-interface rhythmic-head-interface\
244  new-slur-interface note-column-interface",
245 /* reads */       "scriptDefinitions",
246 /* write */       "");