]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
*** empty log message ***
[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 "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   SCM description_;
27   Script_tuple ()
28   {
29     event_ = 0;
30     script_ = 0;
31     description_ = SCM_EOL;
32   }
33 };
34
35 class Script_engraver : public Engraver
36 {
37   Array<Script_tuple> scripts_;
38   Grob *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 *r)
57 {
58   if (r->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_c_equal_p (scripts_[i].event_
64                           ->get_property ("articulation-type"),
65                           r->get_property ("articulation-type")))
66           return true;
67
68       Script_tuple t;
69       t.event_ = r;
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 (ly_c_pair_p (entry))
83         g->internal_set_property (sym, ly_cdr (entry));
84     }
85 }
86
87 /* Add the properties, one by one for each Script.  A little space
88    (memory? --jcn) could be saved by tacking the props onto the Script
89    grob (i.e. make ScriptStaccato , ScriptMarcato, etc. ).  */
90 void make_script_from_event (Grob *p, SCM *descr, Context *tg,
91                              SCM art_type, int index)
92 {
93   SCM alist = tg->get_property ("scriptDefinitions");
94   SCM art = scm_assoc (art_type, alist);
95
96   if (art == SCM_BOOL_F)
97     {
98       /* FIXME: */
99       warning (_ ("Do not know how to interpret articulation: "));
100       warning (_ ("Scheme encoding: "));
101       scm_write (art_type, scm_current_error_port ());
102       return;
103     }
104
105   art = ly_cdr (art);
106   *descr = art;
107
108   copy_property (p, ly_symbol2scm ("script-stencil"), art);
109   copy_property (p, ly_symbol2scm ("direction"), art);
110   copy_property (p, ly_symbol2scm ("side-relative-direction"), art);
111
112   int prio = 0;
113   SCM sprio = scm_assoc (ly_symbol2scm ("script-priority"), art);
114   if (ly_c_pair_p (sprio))
115     prio = ly_scm2int (ly_cdr (sprio));
116
117   /* Make sure they're in order of user input by adding index i.
118      Don't use the direction in this priority. Smaller means closer
119      to the head.  */
120   prio += index;
121
122   Side_position_interface::set_axis (p, Y_AXIS);
123   p->set_property ("script-priority", scm_int2num (prio));
124 }
125
126 void
127 Script_engraver::process_music ()
128 {
129   int script_count = scripts_.size ();
130   for (int i = 0; i < script_count; i++)
131     {
132       Music *m = scripts_[i].event_;
133
134       Grob *p = make_item ("Script", m->self_scm ());
135
136       make_script_from_event (p, &scripts_[i].description_, context (),
137                               m->get_property ("articulation-type"),
138                               i);
139
140       scripts_[i].script_ = p;
141
142       SCM force_dir = m->get_property ("direction");
143       if (is_direction (force_dir) && to_dir (force_dir))
144         p->set_property ("direction", force_dir);
145     }
146 }
147
148 void
149 Script_engraver::acknowledge_grob (Grob_info inf)
150 {
151   int script_count = scripts_.size ();
152   if (Stem::has_interface (inf.grob_))
153     {
154       for (int i = 0; i < script_count; i++)
155         {
156           Grob *e = scripts_[i].script_;
157
158           if (to_dir (e->get_property ("side-relative-direction")))
159             e->set_property ("direction-source", inf.grob_->self_scm ());
160
161           /* FIXME: add dependency */
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 < script_count; 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       /* Make note column the parent of the script.  That is not
184         correct, but due to seconds in a chord, noteheads may be
185         swapped around horizontally.
186
187         As the note head to put it on is not known now, postpone this
188         decision to Script_interface::before_line_breaking ().  */
189       for (int i = 0; i < script_count; i++)
190         {
191           Grob *e = scripts_[i].script_;
192
193           if (!e->get_parent (X_AXIS)
194               && Side_position_interface::get_axis (e) == Y_AXIS)
195             e->set_parent (inf.grob_, X_AXIS);
196         }
197     }
198   else if (New_slur::has_interface (inf.grob_) && script_count)
199     slur_ = inf.grob_;
200 }
201
202 void
203 Script_engraver::stop_translation_timestep ()
204 {
205   int script_count = scripts_.size ();
206   for (int i = 0; i < script_count; i++)
207     if (Grob *sc = scripts_[i].script_)
208       {
209         SCM follow = scm_assoc (ly_symbol2scm ("follow-into-staff"),
210                                 scripts_[i].description_);
211         if (ly_c_pair_p (follow) && to_boolean (ly_cdr (follow)))
212           {
213             sc->add_offset_callback (Side_position_interface
214                                      ::quantised_position_proc, Y_AXIS);
215             sc->set_property ("staff-padding", SCM_EOL);
216           }
217         
218         /* Simplistic slur collision handling.  This fixes simple collisions
219            like
220            
221            a_\upbow( b)
222            
223            but it most probably breaks for more interesting cases.
224            Maybe make a new colission engraver.
225
226            
227            Assume that a SCRIPT that should collide with SLUR does not
228            have a negative priority. */
229         SCM priority = sc->get_property ("script-priority");
230         if (robust_scm2int (priority, 0) >= 0
231             && slur_
232             && get_grob_direction (sc) == get_slur_dir (slur_))
233           {
234             Real ss = Staff_symbol_referencer::staff_space (sc);
235             Real pad = robust_scm2double (sc->get_property ("padding"), 0);
236
237             /* FIXME: 1ss padding hardcoded */
238             sc->set_property ("padding", scm_make_real (pad + ss));
239           }
240       }
241   slur_ = 0;
242   scripts_.clear ();
243 }
244
245 ENTER_DESCRIPTION (Script_engraver,
246 /* descr */       "Handles note scripted articulations.",
247 /* creats*/       "Script",
248 /* accepts */     "script-event articulation-event",
249 /* acks  */       "stem-interface rhythmic-head-interface\
250  new-slur-interface note-column-interface",
251 /* reads */       "scriptDefinitions",
252 /* write */       "");