]> git.donarmstrong.com Git - lilypond.git/blob - lily/piano-pedal-engraver.cc
(parse_symbol_list): Bugfix.
[lilypond.git] / lily / piano-pedal-engraver.cc
1 /*
2   piano-pedal-engraver.cc -- implement Piano_pedal_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7
8   Chris Jackson <chris@fluffhouse.org.uk> - extended to support
9   bracketed pedals.
10 */
11
12 #include "engraver.hh"
13 #include "lily-guile.hh"
14 #include "side-position-interface.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "axis-group-interface.hh"
17 #include "context.hh"
18 #include "directional-element-interface.hh"
19 #include "note-column.hh"
20 #include "warn.hh"
21
22 /*
23   Urgh. This engraver is too complex. rewrite. --hwn
24 */
25
26 struct Pedal_info
27 {
28   char const *name_;
29
30   /*
31     Event for currently running pedal.
32   */
33   Music *current_bracket_ev_;
34
35   /*
36     Event for currently starting pedal, (necessary?
37
38     distinct from current_bracket_ev_, since current_bracket_ev_ only
39     necessary for brackets, not for text style.
40   */
41   Music *start_ev_;
42
43   /*
44     Events that were found in this timestep.
45   */
46   Drul_array<Music *> event_drul_;
47   Item *item_;
48   Spanner *bracket_; // A single portion of a pedal bracket
49   Spanner *finished_bracket_;
50
51   /*
52     This grob contains all the pedals of the same type on the same staff
53   */
54   Spanner *line_spanner_;
55   Spanner *finished_line_spanner_;
56 };
57
58 class Piano_pedal_engraver : public Engraver
59 {
60 public:
61   TRANSLATOR_DECLARATIONS (Piano_pedal_engraver);
62   ~Piano_pedal_engraver ();
63 protected:
64   virtual void initialize ();
65   virtual void finalize ();
66   virtual bool try_music (Music *);
67   void stop_translation_timestep ();
68   DECLARE_ACKNOWLEDGER (note_column);
69   void process_music ();
70
71 private:
72
73   Pedal_info *info_list_;
74
75   /*
76     Record a stack of the current pedal spanners, so if more than one pedal
77     occurs simultaneously then extra space can be added between them.
78   */
79
80   Link_array<Spanner> previous_;
81   void del_linespanner (Spanner *);
82
83   void create_text_grobs (Pedal_info *p, bool);
84   void create_bracket_grobs (Pedal_info *p, bool);
85   void typeset_all (Pedal_info *p);
86 };
87
88 Piano_pedal_engraver::Piano_pedal_engraver ()
89 {
90   info_list_ = 0;
91 }
92
93 void
94 Piano_pedal_engraver::initialize ()
95 {
96   char *names [] = { "Sostenuto", "Sustain", "UnaCorda", 0 };
97
98   info_list_ = new Pedal_info[sizeof (names) / sizeof (char const *)];
99   Pedal_info *p = info_list_;
100
101   char **np = names;
102   do
103     {
104       p->name_ = *np;
105       p->item_ = 0;
106       p->bracket_ = 0;
107       p->finished_bracket_ = 0;
108       p->line_spanner_ = 0;
109       p->finished_line_spanner_ = 0;
110       p->current_bracket_ev_ = 0;
111       p->event_drul_[START] = 0;
112       p->event_drul_[STOP] = 0;
113       p->start_ev_ = 0;
114
115       p++;
116     }
117   while (* (np++));
118 }
119
120 Piano_pedal_engraver::~Piano_pedal_engraver ()
121 {
122   delete[] info_list_;
123 }
124
125 /*
126   Urg: Code dup
127   I'm a script
128 */
129 void
130 Piano_pedal_engraver::acknowledge_note_column (Grob_info info)
131 {
132   for (Pedal_info *p = info_list_; p && p->name_; p++)
133     {
134       if (p->line_spanner_)
135         {
136           Side_position_interface::add_support (p->line_spanner_, info.grob ());
137           add_bound_item (p->line_spanner_, info.grob ());
138         }
139       if (p->bracket_)
140         add_bound_item (p->bracket_, info.grob ());
141       if (p->finished_bracket_)
142         add_bound_item (p->finished_bracket_, info.grob ());
143     }
144 }
145
146 bool
147 Piano_pedal_engraver::try_music (Music *m)
148 {
149   if (m->is_mus_type ("pedal-event"))
150     {
151       for (Pedal_info *p = info_list_; p->name_; p++)
152         {
153           String nm = p->name_ + String ("Event");
154           if (ly_is_equal (m->get_property ("name"),
155                            scm_str2symbol (nm.to_str0 ())))
156             {
157               Direction d = to_dir (m->get_property ("span-direction"));
158               p->event_drul_[d] = m;
159               return true;
160             }
161         }
162     }
163   return false;
164 }
165
166 void
167 Piano_pedal_engraver::process_music ()
168 {
169   for (Pedal_info *p = info_list_; p && p->name_; p++)
170     {
171       if (p->event_drul_[STOP] || p->event_drul_[START])
172         {
173           if (!p->line_spanner_)
174             {
175               String name = String (p->name_) + "PedalLineSpanner";
176               Music *rq = (p->event_drul_[START] ? p->event_drul_[START] : p->event_drul_[STOP]);
177               p->line_spanner_ = make_spanner (name.to_str0 (), rq->self_scm ());
178             }
179
180           /* Choose the appropriate grobs to add to the line spanner
181              These can be text items or text-spanners
182           */
183
184           /*
185             ugh, code dup, should read grob to create from other
186             property.
187
188             bracket: |_________/\____|
189             text:    Ped.     *Ped.  *
190             mixed:   Ped. _____/\____|
191           */
192
193           String prop = String ("pedal") + p->name_ + "Style";
194           SCM style = get_property (prop.to_str0 ());
195
196           bool mixed = style == ly_symbol2scm ("mixed");
197           bool bracket = (mixed
198                           || style == ly_symbol2scm ("bracket"));
199           bool text = (style == ly_symbol2scm ("text")
200                        || mixed);
201
202           if (text && !p->item_)
203             create_text_grobs (p, mixed);
204           if (bracket)
205             create_bracket_grobs (p, mixed);
206         }
207     }
208 }
209
210 void
211 Piano_pedal_engraver::create_text_grobs (Pedal_info *p, bool mixed)
212 {
213   SCM s = SCM_EOL;
214   SCM strings = get_property (("pedal" + String (p->name_) + "Strings").to_str0 ());
215
216   if (scm_ilength (strings) < 3)
217     {
218       Music *m = p->event_drul_[START];
219       if (!m) m = p->event_drul_ [STOP];
220
221       String msg = _f ("expect 3 strings for piano pedals, found: %d",
222                        scm_ilength (strings));
223       if (m)
224         m->origin ()->warning (msg);
225       else
226         warning (msg);
227
228       return;
229     }
230
231   if (p->event_drul_[STOP] && p->event_drul_[START])
232     {
233       if (!mixed)
234         {
235           if (!p->start_ev_)
236             {
237               p->event_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: `%s'", p->name_));
238             }
239           else
240             s = scm_cadr (strings);
241           p->start_ev_ = p->event_drul_[START];
242         }
243     }
244   else if (p->event_drul_[STOP])
245     {
246       if (!mixed)
247         {
248           if (!p->start_ev_)
249             {
250               p->event_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: `%s'", p->name_));
251             }
252           else
253             s = scm_caddr (strings);
254           p->start_ev_ = 0;
255         }
256     }
257   else if (p->event_drul_[START])
258     {
259       p->start_ev_ = p->event_drul_[START];
260       s = scm_car (strings);
261       if (!mixed)
262         {
263           /*
264             Code dup?! see below.
265           */
266           if (previous_.size ())
267             // add extra space below the previous already-occuring pedal
268             Side_position_interface::add_support (p->line_spanner_,
269                                                   previous_.top ());
270           previous_.push (p->line_spanner_);
271         }
272     }
273
274   if (scm_is_string (s))
275     {
276       String propname = String (p->name_) + "Pedal";
277
278       p->item_ = make_item (propname.to_str0 (), (p->event_drul_[START]
279                                                   ? p->event_drul_[START]
280                                                   : p->event_drul_[STOP])->self_scm ());
281
282       p->item_->set_property ("text", s);
283       Axis_group_interface::add_element (p->line_spanner_, p->item_);
284     }
285
286   if (!mixed)
287     {
288       p->event_drul_[START] = 0;
289       p->event_drul_[STOP] = 0;
290     }
291 }
292
293 void
294 Piano_pedal_engraver::create_bracket_grobs (Pedal_info *p, bool mixed)
295 {
296   if (!p->bracket_ && p->event_drul_[STOP])
297     {
298       String msg = _f ("can't find start of piano pedal bracket: `%s'", p->name_);
299       p->event_drul_[STOP]->origin ()->warning (msg);
300       p->event_drul_[STOP] = 0;
301     }
302
303   if (p->event_drul_[STOP])
304     {
305       assert (!p->finished_bracket_);
306
307       Grob *cmc = unsmob_grob (get_property ("currentMusicalColumn"));
308
309       if (!p->bracket_->get_bound (RIGHT))
310         p->bracket_->set_bound (RIGHT, cmc);
311
312       /*
313         Set properties so that the stencil-creating function will
314         know whether the right edge should be flared ___/
315       */
316
317       if (!p->event_drul_[START])
318         {
319           SCM flare = p->bracket_->get_property ("bracket-flare");
320           p->bracket_->set_property ("bracket-flare", scm_cons (scm_car (flare),
321                                                                 scm_from_double (0)));
322         }
323
324       p->finished_bracket_ = p->bracket_;
325       p->bracket_ = 0;
326       p->current_bracket_ev_ = 0;
327     }
328
329   if (p->event_drul_[START])
330     {
331       p->start_ev_ = p->event_drul_[START];
332       p->current_bracket_ev_ = p->event_drul_[START];
333
334       p->bracket_ = make_spanner ("PianoPedalBracket", p->event_drul_[START]->self_scm ());
335
336       /*
337         Set properties so that the stencil-creating function will
338         know whether the left edge should be flared \___
339       */
340
341       if (!p->finished_bracket_)
342         {
343           SCM flare = p->bracket_->get_property ("bracket-flare");
344           p->bracket_->set_property ("bracket-flare", scm_cons (scm_from_double (0), scm_cdr (flare)));
345         }
346
347       /* Set this property for 'mixed style' pedals,    Ped._______/\ ,
348          so the stencil function will shorten the ____ line by the length of the Ped. text.
349       */
350
351       if (mixed)
352         {
353           /*
354             Mixed style: Store a pointer to the preceding text for use in
355             calculating the length of the line
356
357
358             TODO:
359
360             WTF is pedal-text not the bound of the object? --hwn
361           */
362           if (p->item_)
363             p->bracket_->set_object ("pedal-text", p->item_->self_scm ());
364         }
365
366       /*
367         We do not use currentMusicalColumn for the left span-point.
368         If the column as accidentals (eg on a different stave), the
369         currentMusicalColumn is too wide, making the bracket too big.
370
371         TODO:
372
373         Hmm. What do we do when there are no notes when the spanner starts?
374
375         TODO:
376
377         what about the right span point?
378
379       */
380       Axis_group_interface::add_element (p->line_spanner_, p->bracket_);
381
382       if (!p->event_drul_[STOP])
383         {
384
385           /*
386             code dup. --hwn.
387
388             // position new pedal spanner below the current one
389             */
390           if (previous_.size ())
391             Side_position_interface::add_support (p->line_spanner_, previous_.top ());
392
393           previous_.push (p->line_spanner_);
394         }
395     }
396
397   p->event_drul_[START] = 0;
398   p->event_drul_[STOP] = 0;
399 }
400
401 void
402 Piano_pedal_engraver::finalize ()
403 {
404   for (Pedal_info *p = info_list_; p && p->name_; p++)
405     {
406       /*
407         suicide?
408       */
409       if (p->line_spanner_
410           && !p->line_spanner_->is_live ())
411         p->line_spanner_ = 0;
412
413       if (p->bracket_
414           && !p->bracket_->is_live ())
415         p->bracket_ = 0;
416
417       if (p->bracket_)
418         {
419           SCM cc = get_property ("currentCommandColumn");
420           Item *c = unsmob_item (cc);
421           if (p->line_spanner_)
422             p->line_spanner_->set_bound (RIGHT, c);
423           p->bracket_->set_bound (RIGHT, c);
424
425           p->finished_bracket_ = p->bracket_;
426           p->bracket_ = 0;
427           p->finished_line_spanner_ = p->line_spanner_;
428           p->line_spanner_ = 0;
429           typeset_all (p);
430         }
431
432       if (p->line_spanner_)
433         {
434           p->finished_line_spanner_ = p->line_spanner_;
435           typeset_all (p);
436         }
437     }
438 }
439
440 void
441 Piano_pedal_engraver::del_linespanner (Spanner *g)
442 {
443   int idx = previous_.find_index (g);
444   if (idx >= 0)
445     previous_.del (idx);
446 }
447
448 void
449 Piano_pedal_engraver::stop_translation_timestep ()
450 {
451   for (Pedal_info *p = info_list_; p && p->name_; p++)
452     {
453       if (!p->bracket_)
454         {
455           p->finished_line_spanner_ = p->line_spanner_;
456           p->line_spanner_ = 0;
457           del_linespanner (p->finished_line_spanner_);
458         }
459
460       typeset_all (p);
461     }
462
463   for (Pedal_info *p = info_list_; p->name_; p++)
464     {
465       p->event_drul_[STOP] = 0;
466       p->event_drul_[START] = 0;
467     }
468 }
469
470 void
471 Piano_pedal_engraver::typeset_all (Pedal_info *p)
472 {
473   /*
474     Handle suicide.
475   */
476   if (p->finished_line_spanner_
477       && !p->finished_line_spanner_->is_live ())
478     p->finished_line_spanner_ = 0;
479   if (p->finished_bracket_
480       && !p->finished_bracket_->is_live ())
481     p->finished_bracket_ = 0;
482
483   if (p->item_)
484     {
485       p->item_ = 0;
486     }
487
488   if (p->finished_bracket_)
489     {
490       Grob *r = p->finished_bracket_->get_bound (RIGHT);
491       if (!r)
492         {
493           p->finished_bracket_->set_bound (RIGHT, unsmob_grob (get_property ("currentMusicalColumn")));
494         }
495
496       p->finished_bracket_ = 0;
497     }
498
499   if (p->finished_line_spanner_)
500     {
501       Grob *l = p->finished_line_spanner_->get_bound (LEFT);
502       Grob *r = p->finished_line_spanner_->get_bound (RIGHT);
503       if (!r && l)
504         p->finished_line_spanner_->set_bound (RIGHT, l);
505       else if (!l && r)
506         p->finished_line_spanner_->set_bound (LEFT, r);
507       else if (!r && !l)
508         {
509           Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
510           Item *ci = dynamic_cast<Item *> (cc);
511           p->finished_line_spanner_->set_bound (RIGHT, ci);
512           p->finished_line_spanner_->set_bound (LEFT, ci);
513         }
514
515       p->finished_line_spanner_ = 0;
516     }
517 }
518
519 #include "translator.icc"
520 ADD_ACKNOWLEDGER (Piano_pedal_engraver, note_column);
521 ADD_TRANSLATOR (Piano_pedal_engraver,
522                 /* doc */ "Engrave piano pedal symbols and brackets.",
523                 /* create */ "SostenutoPedal SustainPedal UnaCordaPedal SostenutoPedalLineSpanner SustainPedalLineSpanner UnaCordaPedalLineSpanner",
524                 /* accept */ "pedal-event",
525                 /* read */ "currentCommandColumn "
526                 "pedalSostenutoStrings pedalSustainStrings "
527                 "pedalUnaCordaStrings pedalSostenutoStyle "
528                 "pedalSustainStyle pedalUnaCordaStyle",
529                 /* write */ "");