]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
(spanned_rank_iv): Bugfix.
[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   SCM description_;
28   Script_tuple ()
29   {
30     event_ = 0;
31     script_ = 0;
32     description_ = SCM_EOL;
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 space
89    (memory? --jcn) could be saved by tacking the props onto the Script
90    grob (i.e. make ScriptStaccato , ScriptMarcato, etc. ).  */
91 void make_script_from_event (Grob *p, SCM *descr, 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 (_ ("Do not know how to interpret articulation: "));
101       warning (_ ("Scheme encoding: "));
102       scm_write (art_type, scm_current_error_port ());
103       return;
104     }
105
106   art = ly_cdr (art);
107   *descr = art;
108
109   copy_property (p, ly_symbol2scm ("script-stencil"), 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 (ly_c_pair_p (sprio))
116     prio = ly_scm2int (ly_cdr (sprio));
117
118   /* Make sure they're in order of user input by adding index i.
119      Don't use the direction in this priority. Smaller means closer
120      to the head.  */
121   prio += index;
122
123   Side_position_interface::set_axis (p, Y_AXIS);
124   p->set_property ("script-priority", scm_int2num (prio));
125 }
126
127 void
128 Script_engraver::process_music ()
129 {
130   int script_count = scripts_.size ();
131   for (int i = 0; i < script_count; i++)
132     {
133       Music *m = scripts_[i].event_;
134
135       Grob *p = make_item ("Script", m->self_scm ());
136
137       make_script_from_event (p, &scripts_[i].description_, context (),
138                               m->get_property ("articulation-type"),
139                               i);
140
141       scripts_[i].script_ = p;
142
143       SCM force_dir = m->get_property ("direction");
144       if (is_direction (force_dir) && to_dir (force_dir))
145         p->set_property ("direction", force_dir);
146     }
147 }
148
149 void
150 Script_engraver::acknowledge_grob (Grob_info info)
151 {
152   int script_count = scripts_.size ();
153   if (Stem::has_interface (info.grob_))
154     {
155       for (int i = 0; i < script_count; i++)
156         {
157           Grob *e = scripts_[i].script_;
158
159           if (to_dir (e->get_property ("side-relative-direction")))
160             e->set_property ("direction-source", info.grob_->self_scm ());
161
162           /* FIXME: add dependency */
163           e->add_dependency (info.grob_);
164           Side_position_interface::add_support (e, info.grob_);
165         }
166     }
167   else if (Rhythmic_head::has_interface (info.grob_))
168     {
169       for (int i = 0; i < script_count; i++)
170         {
171           Grob *e = scripts_[i].script_;
172         
173           if (Side_position_interface::get_axis (e) == X_AXIS
174               && !e->get_parent (Y_AXIS))
175             {
176               e->set_parent (info.grob_, Y_AXIS);
177               e->add_dependency (info.grob_);
178             }
179           Side_position_interface::add_support (e,info.grob_);
180         }
181     }
182   else if (Note_column::has_interface (info.grob_))
183     {
184       /* Make note column the parent of the script.  That is not
185         correct, but due to seconds in a chord, noteheads may be
186         swapped around horizontally.
187
188         As the note head to put it on is not known now, postpone this
189         decision to Script_interface::before_line_breaking ().  */
190       for (int i = 0; i < script_count; i++)
191         {
192           Grob *e = scripts_[i].script_;
193
194           if (!e->get_parent (X_AXIS)
195               && Side_position_interface::get_axis (e) == Y_AXIS)
196             e->set_parent (info.grob_, X_AXIS);
197         }
198     }
199   else if (New_slur::has_interface (info.grob_))
200     slur_ = dynamic_cast<Spanner*> (info.grob_);
201 }
202
203 void
204 Script_engraver::stop_translation_timestep ()
205 {
206   int script_count = scripts_.size ();
207   for (int i = 0; i < script_count; i++)
208     if (Grob *sc = scripts_[i].script_)
209       {
210         SCM follow = scm_assoc (ly_symbol2scm ("follow-into-staff"),
211                                 scripts_[i].description_);
212         if (ly_c_pair_p (follow) && to_boolean (ly_cdr (follow)))
213           {
214             sc->add_offset_callback (Side_position_interface
215                                      ::quantised_position_proc, Y_AXIS);
216             sc->set_property ("staff-padding", SCM_EOL);
217           }
218         
219         /* Simplistic slur collision handling.  This fixes simple collisions
220            like
221            
222            a_\upbow( b)
223            
224            but it most probably breaks for more interesting cases.
225            Maybe make a new colission engraver.
226
227            
228            Assume that a SCRIPT that should collide with SLUR does not
229            have a negative priority. */
230         SCM priority = sc->get_property ("script-priority");
231
232 #if 0
233         // this segfaults...
234         int rank = Paper_column::get_rank (sc);
235 #endif
236         
237         int rank = -1;
238 #if 1
239         // this always yields -1
240         if (Item *i = dynamic_cast<Item*> (sc))
241           if (i->get_column ())
242             rank = i->get_column ()->rank_;
243 #else
244         // this always yields -1
245         for (Grob *p = sc; p && rank < 0; p = p->get_parent (Y_AXIS))
246           {
247             if (Item *i = dynamic_cast<Item*> (p))
248               if (i->get_column ())
249                 rank = i->get_column ()->rank_;
250           }
251 #endif
252
253         if (robust_scm2int (priority, 0) >= 0
254             && slur_
255             && get_grob_direction (sc) == get_slur_dir (slur_)
256             && (slur_->spanned_rank_iv ().contains (rank)
257                 /* I'm a bit out of ideas about and time to search for
258                    how to get our rank.  Sue me.  */
259                 || rank == -1))
260           {
261             Real ss = Staff_symbol_referencer::staff_space (sc);
262             Real pad = robust_scm2double (sc->get_property ("padding"), 0);
263
264             /* FIXME: 1ss padding hardcoded */
265             sc->set_property ("padding", scm_make_real (pad + ss));
266           }
267       }
268   scripts_.clear ();
269 }
270
271 ENTER_DESCRIPTION (Script_engraver,
272 /* descr */       "Handles note scripted articulations.",
273 /* creats*/       "Script",
274 /* accepts */     "script-event articulation-event",
275 /* acks  */       "stem-interface rhythmic-head-interface\
276  new-slur-interface note-column-interface",
277 /* reads */       "scriptDefinitions",
278 /* write */       "");