]> git.donarmstrong.com Git - lilypond.git/blob - lily/piano-pedal-engraver.cc
Issue 4989/3: Add a guard against invalid Type 1 (PFB) font
[lilypond.git] / lily / piano-pedal-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2015 Jan Nieuwenhuizen <janneke@gnu.org>,
5                  Erik Sandberg <mandolaerik@gmail.com>
6
7   Chris Jackson <chris@fluffhouse.org.uk> - extended to support
8   bracketed pedals.
9
10   LilyPond is free software: you can redistribute it and/or modify
11   it under the terms of the GNU General Public License as published by
12   the Free Software Foundation, either version 3 of the License, or
13   (at your option) any later version.
14
15   LilyPond is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License for more details.
19
20   You should have received a copy of the GNU General Public License
21   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "engraver.hh"
25
26 #include "axis-group-interface.hh"
27 #include "context.hh"
28 #include "directional-element-interface.hh"
29 #include "international.hh"
30 #include "lily-guile.hh"
31 #include "note-column.hh"
32 #include "side-position-interface.hh"
33 #include "staff-symbol-referencer.hh"
34 #include "stream-event.hh"
35 #include "string-convert.hh"
36 #include "warn.hh"
37 #include "spanner.hh"
38 #include "item.hh"
39
40 #include "translator.icc"
41
42 #include <string.h>
43
44 /*
45   TODO:
46
47   * Junk hardcoded sustain/sostenuto/una_corda distinction;
48     Softcode using (list (sustain-event SustainPedal PianoPedalBracket) ... )
49
50   * Try to use same engraver for dynamics.
51 */
52
53 /* Ugh: This declaration is duplicated in piano-pedal-performer */
54 enum Pedal_type
55 {
56   SOSTENUTO,
57   SUSTAIN,
58   UNA_CORDA,
59   NUM_PEDAL_TYPES
60 };
61
62 /*
63   Static precalculated data (symbols and strings) for the different
64   pedal types
65 */
66 struct Pedal_type_info
67 {
68   string base_name_;
69   SCM event_class_sym_;
70   SCM style_sym_;
71   SCM strings_sym_;
72
73   const char *pedal_c_str_;
74
75   Pedal_type_info ()
76   {
77     event_class_sym_ = SCM_EOL;
78     style_sym_ = SCM_EOL;
79     strings_sym_ = SCM_EOL;
80     pedal_c_str_ = 0;
81   }
82   void protect ()
83   {
84     scm_gc_protect_object (event_class_sym_);
85     scm_gc_protect_object (style_sym_);
86     scm_gc_protect_object (strings_sym_);
87   }
88 };
89
90 struct Pedal_info
91 {
92   const Pedal_type_info *type_;
93
94   /*
95     Event for currently running pedal.
96   */
97   Stream_event *current_bracket_ev_;
98
99   /*
100     Event for currently starting pedal, (necessary?
101
102     distinct from current_bracket_ev_, since current_bracket_ev_ only
103     necessary for brackets, not for text style.
104   */
105   Stream_event *start_ev_;
106
107   /*
108     Events that were found in this timestep.
109   */
110   Drul_array<Stream_event *> event_drul_;
111   Item *item_;
112   Spanner *bracket_; // A single portion of a pedal bracket
113   Spanner *finished_bracket_;
114 };
115
116 static Pedal_type_info pedal_types_[NUM_PEDAL_TYPES];
117
118 class Piano_pedal_engraver : public Engraver
119 {
120 public:
121   TRANSLATOR_DECLARATIONS (Piano_pedal_engraver);
122
123 protected:
124   virtual void initialize ();
125   virtual void finalize ();
126   void listen_sustain (Stream_event *);
127   void listen_una_corda (Stream_event *);
128   void listen_sostenuto (Stream_event *);
129   void stop_translation_timestep ();
130   void process_music ();
131
132 private:
133   Pedal_info info_list_[NUM_PEDAL_TYPES + 1];
134
135   void create_text_grobs (Pedal_info *p, bool);
136   void create_bracket_grobs (Pedal_info *p, bool);
137   void typeset_all (Pedal_info *p);
138 };
139
140 static void
141 init_pedal_types ()
142 {
143   const char *names [NUM_PEDAL_TYPES];
144   names[SOSTENUTO] = "Sostenuto";
145   names[SUSTAIN] = "Sustain";
146   names[UNA_CORDA] = "UnaCorda";
147
148   for (int i = 0; i < NUM_PEDAL_TYPES; i++)
149     {
150       const char *name = names[i];
151       /* FooBar */
152       string base_name = name;
153       /* foo-bar */
154       string base_ident = "";
155       int prev_pos = 0;
156       int cur_pos;
157       for (cur_pos = 1; name[cur_pos]; cur_pos++)
158         if (isupper (name[cur_pos]))
159           {
160             base_ident = base_ident + String_convert::to_lower (string (name, prev_pos, cur_pos - prev_pos)) + "-";
161             prev_pos = cur_pos;
162           }
163       base_ident += String_convert::to_lower (string (name, prev_pos, cur_pos - prev_pos));
164
165       /*
166         be careful, as we don't want to loose references to the _sym_ members.
167        */
168       Pedal_type_info info;
169       info.event_class_sym_ = scm_from_ascii_symbol ((base_ident + "-event").c_str ());
170       info.style_sym_ = scm_from_ascii_symbol (("pedal" + base_name + "Style").c_str ());
171       info.strings_sym_ = scm_from_ascii_symbol (("pedal" + base_name + "Strings").c_str ());
172
173       info.base_name_ = name;
174       info.pedal_c_str_ = strdup ((base_name + "Pedal").c_str ());
175
176       info.protect ();
177
178       pedal_types_[i] = info;
179     }
180 }
181
182 ADD_SCM_INIT_FUNC (Piano_pedal_engraver_init_pedal_types_, init_pedal_types);
183
184 Piano_pedal_engraver::Piano_pedal_engraver ()
185 {
186 }
187
188 void
189 Piano_pedal_engraver::initialize ()
190 {
191   for (int i = 0; i < NUM_PEDAL_TYPES; i++)
192     {
193       Pedal_type_info *s = &pedal_types_[i];
194       Pedal_info *info = &info_list_[i];
195
196       info->type_ = s;
197       info->item_ = 0;
198       info->bracket_ = 0;
199       info->finished_bracket_ = 0;
200       info->current_bracket_ev_ = 0;
201       info->event_drul_[START] = 0;
202       info->event_drul_[STOP] = 0;
203       info->start_ev_ = 0;
204     }
205   info_list_[NUM_PEDAL_TYPES].type_ = 0;
206 }
207
208 void
209 Piano_pedal_engraver::listen_sostenuto (Stream_event *ev)
210 {
211   Direction d = to_dir (ev->get_property ("span-direction"));
212   ASSIGN_EVENT_ONCE (info_list_[SOSTENUTO].event_drul_[d], ev);
213 }
214
215 void
216 Piano_pedal_engraver::listen_sustain (Stream_event *ev)
217 {
218   Direction d = to_dir (ev->get_property ("span-direction"));
219   ASSIGN_EVENT_ONCE (info_list_[SUSTAIN].event_drul_[d], ev);
220 }
221
222 void
223 Piano_pedal_engraver::listen_una_corda (Stream_event *ev)
224 {
225   Direction d = to_dir (ev->get_property ("span-direction"));
226   ASSIGN_EVENT_ONCE (info_list_[UNA_CORDA].event_drul_[d], ev);
227 }
228
229 void
230 Piano_pedal_engraver::process_music ()
231 {
232   for (Pedal_info *p = info_list_; p->type_; p++)
233     {
234       if (p->event_drul_[STOP] || p->event_drul_[START])
235         {
236           /* Choose the appropriate grobs to add to the line spanner
237              These can be text items or text-spanners
238           */
239
240           /*
241             ugh, code dup, should read grob to create from other
242             property.
243
244             bracket: |_________/\____|
245             text:    Ped.     *Ped.  *
246             mixed:   Ped. _____/\____|
247           */
248
249           SCM style = get_property (p->type_->style_sym_);
250
251           bool mixed = scm_is_eq (style, ly_symbol2scm ("mixed"));
252           bool bracket = (mixed
253                           || scm_is_eq (style, ly_symbol2scm ("bracket")));
254           bool text = (mixed
255                        || scm_is_eq (style, ly_symbol2scm ("text")));
256
257           if (text && !p->item_)
258             create_text_grobs (p, mixed);
259           if (bracket)
260             create_bracket_grobs (p, mixed);
261         }
262     }
263 }
264
265 void
266 Piano_pedal_engraver::create_text_grobs (Pedal_info *p, bool mixed)
267 {
268   SCM s = SCM_EOL;
269   SCM strings = get_property (p->type_->strings_sym_);
270
271   if (scm_ilength (strings) < 3)
272     {
273       Stream_event *m = p->event_drul_[START];
274       if (!m) m = p->event_drul_ [STOP];
275
276       string msg = _f ("expect 3 strings for piano pedals, found: %ld",
277                        scm_ilength (strings));
278       if (m)
279         m->origin ()->warning (msg);
280       else
281         warning (msg);
282
283       return;
284     }
285
286   if (p->event_drul_[STOP] && p->event_drul_[START])
287     {
288       if (!mixed)
289         {
290           if (!p->start_ev_)
291             p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
292           else
293             s = scm_cadr (strings);
294           p->start_ev_ = p->event_drul_[START];
295         }
296     }
297   else if (p->event_drul_[STOP])
298     {
299       if (!mixed)
300         {
301           if (!p->start_ev_)
302             p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", p->type_->base_name_.c_str ()));
303           else
304             s = scm_caddr (strings);
305           p->start_ev_ = 0;
306         }
307     }
308   else if (p->event_drul_[START])
309     {
310       p->start_ev_ = p->event_drul_[START];
311       s = scm_car (strings);
312     }
313
314   if (scm_is_string (s))
315     {
316       const char *propname = p->type_->pedal_c_str_;
317
318       p->item_ = make_item (propname, (p->event_drul_[START]
319                                        ? p->event_drul_[START]
320                                        : p->event_drul_[STOP])->self_scm ());
321
322       p->item_->set_property ("text", s);
323     }
324
325   if (!mixed)
326     {
327       p->event_drul_[START] = 0;
328       p->event_drul_[STOP] = 0;
329     }
330 }
331
332 void
333 Piano_pedal_engraver::create_bracket_grobs (Pedal_info *p, bool mixed)
334 {
335   if (!p->bracket_ && p->event_drul_[STOP])
336     {
337       string msg = _f ("cannot find start of piano pedal bracket: `%s'", p->type_->base_name_.c_str ());
338       p->event_drul_[STOP]->origin ()->warning (msg);
339       p->event_drul_[STOP] = 0;
340     }
341
342   if (p->event_drul_[STOP])
343     {
344       assert (!p->finished_bracket_);
345
346       Grob *cmc = unsmob<Grob> (get_property ("currentMusicalColumn"));
347       p->bracket_->set_bound (RIGHT, cmc);
348
349       /*
350         Set properties so that the stencil-creating function will
351         know whether the right edge should be flared ___/
352       */
353
354       if (!p->event_drul_[START])
355         {
356           SCM flare = p->bracket_->get_property ("bracket-flare");
357           if (scm_is_pair (flare))
358             p->bracket_->set_property ("bracket-flare", scm_cons (scm_car (flare),
359                                                                   scm_from_double (0)));
360         }
361
362       p->finished_bracket_ = p->bracket_;
363       p->bracket_ = 0;
364
365       announce_end_grob (p->finished_bracket_, p->event_drul_[STOP]->self_scm ());
366
367       p->current_bracket_ev_ = 0;
368     }
369
370   if (p->event_drul_[START])
371     {
372       p->start_ev_ = p->event_drul_[START];
373       p->current_bracket_ev_ = p->event_drul_[START];
374
375       p->bracket_ = make_spanner ("PianoPedalBracket", p->event_drul_[START]->self_scm ());
376
377       /*
378         Set properties so that the stencil-creating function will
379         know whether the left edge should be flared \___
380       */
381
382       if (!p->finished_bracket_)
383         {
384           SCM flare = p->bracket_->get_property ("bracket-flare");
385           p->bracket_->set_property ("bracket-flare", scm_cons (scm_from_double (0), scm_cdr (flare)));
386         }
387
388       /* Set this property for 'mixed style' pedals,    Ped._______/\ ,
389          so the stencil function will shorten the ____ line by the length of the Ped. text.
390       */
391
392       if (mixed)
393         {
394           /*
395             Mixed style: Store a pointer to the preceding text for use in
396             calculating the length of the line
397
398
399             TODO:
400
401             WTF is pedal-text not the bound of the object? --hwn
402           */
403           if (p->item_)
404             p->bracket_->set_object ("pedal-text", p->item_->self_scm ());
405         }
406     }
407
408   p->event_drul_[START] = 0;
409   p->event_drul_[STOP] = 0;
410 }
411
412 void
413 Piano_pedal_engraver::finalize ()
414 {
415   for (Pedal_info *p = info_list_; p->type_; p++)
416     {
417       if (p->bracket_
418           && !p->bracket_->is_live ())
419         p->bracket_ = 0;
420
421       if (p->bracket_)
422         {
423           SCM cc = get_property ("currentCommandColumn");
424           Item *c = unsmob<Item> (cc);
425           p->bracket_->set_bound (RIGHT, c);
426
427           p->finished_bracket_ = p->bracket_;
428           p->bracket_ = 0;
429           typeset_all (p);
430         }
431
432     }
433 }
434
435 void
436 Piano_pedal_engraver::stop_translation_timestep ()
437 {
438   for (Pedal_info *p = info_list_; p->type_; p++)
439     {
440
441       typeset_all (p);
442       if (p->bracket_ && !p->bracket_->get_bound (LEFT))
443         {
444           Grob *cmc = unsmob<Grob> (get_property ("currentMusicalColumn"));
445           p->bracket_->set_bound (LEFT, cmc);
446         }
447     }
448
449   for (Pedal_info *p = info_list_; p->type_; p++)
450     {
451       p->event_drul_[STOP] = 0;
452       p->event_drul_[START] = 0;
453     }
454 }
455
456 void
457 Piano_pedal_engraver::typeset_all (Pedal_info *p)
458 {
459   /*
460     Handle suicide.
461   */
462   if (p->finished_bracket_
463       && !p->finished_bracket_->is_live ())
464     p->finished_bracket_ = 0;
465
466   if (p->item_)
467     p->item_ = 0;
468
469   if (p->finished_bracket_)
470     {
471       Grob *r = p->finished_bracket_->get_bound (RIGHT);
472       if (!r)
473         p->finished_bracket_->set_bound (RIGHT, unsmob<Grob> (get_property ("currentMusicalColumn")));
474
475       p->finished_bracket_ = 0;
476     }
477 }
478
479 void
480 Piano_pedal_engraver::boot ()
481 {
482   ADD_LISTENER (Piano_pedal_engraver, sostenuto);
483   ADD_LISTENER (Piano_pedal_engraver, sustain);
484   ADD_LISTENER (Piano_pedal_engraver, una_corda);
485 }
486
487 ADD_TRANSLATOR (Piano_pedal_engraver,
488                 /* doc */
489                 "Engrave piano pedal symbols and brackets.",
490
491                 /* create */
492                 "PianoPedalBracket "
493                 "SostenutoPedal "
494                 "SustainPedal "
495                 "UnaCordaPedal ",
496
497                 /* read */
498                 "currentCommandColumn "
499                 "pedalSostenutoStrings "
500                 "pedalSostenutoStyle "
501                 "pedalSustainStrings "
502                 "pedalSustainStyle "
503                 "pedalUnaCordaStrings "
504                 "pedalUnaCordaStyle ",
505
506                 /* write */
507                 ""
508                );