]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
* scm/music-functions.scm (determine-split-list): further analysis.
[lilypond.git] / lily / script-engraver.cc
1 /*
2   script-engraver.cc -- implement Script_engraver
3
4   (c)  1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
5 */
6
7 #include "script.hh"
8 #include "side-position-interface.hh"
9 #include "event.hh"
10 #include "stem.hh"
11 #include "rhythmic-head.hh"
12 #include "engraver.hh"
13 #include "note-column.hh"
14 #include "translator-group.hh"
15
16 struct Script_tuple
17 {
18   Music *event_;
19   Grob * script_;
20   SCM description_;
21   Script_tuple ()
22   {
23     event_ = 0;
24     script_ = 0;
25     description_ = SCM_EOL;
26   }
27 };
28
29 class Script_engraver : public Engraver
30 {
31   Array<Script_tuple> scripts_;
32 public:
33   TRANSLATOR_DECLARATIONS(Script_engraver);
34 protected:
35   virtual bool try_music (Music*);
36   virtual void stop_translation_timestep ();
37   virtual void process_music ();
38   virtual void acknowledge_grob (Grob_info);
39 };
40
41 bool
42 Script_engraver::try_music (Music *r)
43 {
44   if (r->is_mus_type ("articulation-event"))
45     {
46       /*
47         Discard double articulations.
48         This is necessary for part-combining.
49        */
50       for (int j = 0; j < scripts_.size (); j++)
51         if (gh_equal_p (scripts_[j]. event_->get_mus_property ("articulation-type"),
52                         r->get_mus_property ("articulation-type")
53                         ))
54           return true;
55           
56       Script_tuple t;
57       t.event_ =r;
58       scripts_.push (t);
59       
60       return true;
61     }
62   return false;
63 }
64
65 void
66 copy_property (Grob * g , SCM sym, SCM alist)
67 {
68   if (g->internal_get_grob_property (sym) == SCM_EOL)
69     {
70       SCM entry = scm_assoc (sym,alist);
71       if (gh_pair_p (entry))
72         {
73           g->internal_set_grob_property (sym, gh_cdr (entry));
74         }
75     }
76 }
77
78
79 /*
80   We add the properties, one by one for each Script. We could save a
81   little space by tacking the props onto the Script grob (i.e. make
82   ScriptStaccato , ScriptMarcato, etc. )
83  */
84 void make_script_from_event (Grob *p,
85                              SCM * descr, Translator_group*tg, Music * event,
86                              int index)
87 {
88   SCM alist = tg->get_property ("scriptDefinitions");
89   SCM art_type= event->get_mus_property ("articulation-type");
90   SCM art = scm_assoc (art_type, alist);
91
92   if (art == SCM_BOOL_F)
93     {
94       event->origin ()->warning (_("Don't know how to interpret articulation:"));
95       event->origin ()->warning (_("Scheme encoding: "));
96       scm_write (art_type, scm_current_error_port ());
97       return  ;
98     }
99
100   art = gh_cdr (art);
101     
102   
103   *descr = art;  
104
105   SCM force_dir = event->get_mus_property ("direction");
106   if (is_direction (force_dir) && to_dir (force_dir))
107     p->set_grob_property ("direction", force_dir);
108
109   copy_property (p, ly_symbol2scm ("script-molecule"), art);
110   copy_property (p, ly_symbol2scm ("direction"), art);
111   copy_property (p, ly_symbol2scm ("side-relative-direction"), art);
112
113   int prio =0;
114   SCM sprio = scm_assoc (ly_symbol2scm ("script-priority"), art);
115   if (gh_pair_p (sprio))
116     prio = gh_scm2int (gh_cdr (sprio));
117
118
119   /*
120     Make sure they're in order of user input by adding index i.
121     Don't use the direction in this priority. Smaller means closer
122     to the head.
123   */
124   prio += index;
125
126   Side_position_interface::set_axis (p, Y_AXIS);
127   p->set_grob_property ("script-priority", gh_int2scm (prio));
128 }
129
130 void
131 Script_engraver::process_music ()
132 {
133   for (int i=0; i < scripts_.size (); i++)
134     {
135       Music* l=scripts_[i].event_;
136
137       Grob * p = make_item ("Script");
138
139       make_script_from_event (p, &scripts_[i].description_, daddy_trans_, l, i);
140
141       scripts_[i].script_ = p;
142       if (p)
143         announce_grob (p, l->self_scm());
144     }
145 }
146
147 void
148 Script_engraver::acknowledge_grob (Grob_info inf)
149 {
150   if (Stem::has_interface (inf.grob_))
151     {
152       for (int i=0; i < scripts_.size (); i++)
153         {
154           Grob*e = scripts_[i].script_;
155
156           if (to_dir (e->get_grob_property ("side-relative-direction")))
157             e->set_grob_property ("direction-source", inf.grob_->self_scm ());
158
159           /*
160             add dep ? 
161            */
162           e->add_dependency (inf.grob_);
163           Side_position_interface::add_support (e, inf.grob_);
164         }
165     }
166   else if (Rhythmic_head::has_interface (inf.grob_))
167     {
168       for (int i=0; i < scripts_.size (); i++)
169         {
170           Grob *e = scripts_[i].script_;
171           
172           if (Side_position_interface::get_axis (e) == X_AXIS
173               && !e->get_parent (Y_AXIS))
174             {
175               e->set_parent (inf.grob_, Y_AXIS);
176               e->add_dependency (inf.grob_); // ??
177             }
178           Side_position_interface::add_support (e,inf.grob_);
179         }
180     }
181   else if (Note_column::has_interface (inf.grob_))
182     {
183
184       /*
185         We make note column the parent of the script. That's not
186         correct, but due to seconds in a chord, noteheads may be
187         swapped around horizontally. We don't know which note head to
188         put it on, so we postpone this decision to
189         Script_interface::before_line_breaking ().
190  
191        */
192       for (int i=0; i < scripts_.size (); i++)
193         {
194           Grob *e = scripts_[i].script_;
195           
196           if (!e->get_parent (X_AXIS) &&
197               Side_position_interface::get_axis (e) == Y_AXIS)
198             {
199               e->set_parent (inf.grob_, X_AXIS);
200             }
201         }
202     }
203 }
204
205 void
206 Script_engraver::stop_translation_timestep ()
207 {
208   for (int i=0; i < scripts_.size (); i++) 
209     {
210       if (!scripts_[i].script_)
211         continue;
212       
213       Grob * sc = scripts_[i].script_;
214
215       SCM follow = scm_assoc (ly_symbol2scm ("follow-into-staff"), scripts_[i].description_);
216       if (gh_pair_p (follow) && to_boolean (gh_cdr (follow)))
217         {
218           sc->add_offset_callback (Side_position_interface::quantised_position_proc, Y_AXIS);
219           sc->set_grob_property ("staff-padding", SCM_EOL);
220         }
221       typeset_grob (sc);
222     }
223   scripts_.clear ();
224 }
225
226
227
228 Script_engraver::Script_engraver(){}
229
230 ENTER_DESCRIPTION(Script_engraver,
231 /* descr */       "Handles note scripted articulations.",
232 /* creats*/       "Script",
233 /* accepts */     "script-event articulation-event",
234 /* acks  */      "stem-interface rhythmic-head-interface note-column-interface",
235 /* reads */       "scriptDefinitions",
236 /* write */       "");