]> git.donarmstrong.com Git - lilypond.git/blob - lily/mensural-ligature-engraver.cc
pass by reference-to-const, not by value
[lilypond.git] / lily / mensural-ligature-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2012 Juergen Reuter <reuter@ipd.uka.de>,
5   Pal Benko <benkop@freestart.hu>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "coherent-ligature-engraver.hh"
22 #include "font-interface.hh"
23 #include "international.hh"
24 #include "mensural-ligature.hh"
25 #include "note-column.hh"
26 #include "note-head.hh"
27 #include "output-def.hh"
28 #include "paper-column.hh"
29 #include "pitch.hh"
30 #include "rhythmic-head.hh"
31 #include "spanner.hh"
32 #include "staff-symbol-referencer.hh"
33 #include "stream-event.hh"
34 #include "warn.hh"
35
36 #include "translator.icc"
37
38 /*
39  * TODO: accidentals are aligned with the first note;
40  * they must appear ahead.
41  *
42  * TODO: prohibit ligatures having notes differing only in accidentals
43  * (like \[ a\breve g as \])
44  *
45  * TODO: do something with multiple voices within a ligature.  See
46  * for example:
47  * Ockeghem: Missa Ecce ancilla domini, bassus part, end of Christe.
48  *
49  * TODO: enhance robustness: in case of an invalid ligature (e.g. the
50  * input specifies a ligature that contains a minima), automatically
51  * break the ligature into smaller, valid pieces.  Such a piece may be
52  * a single note.
53  */
54
55 class Mensural_ligature_engraver : public Coherent_ligature_engraver
56 {
57
58 protected:
59   virtual Spanner *create_ligature_spanner ();
60   virtual void build_ligature (Spanner *ligature,
61                                vector<Grob_info> const &primitives);
62   DECLARE_TRANSLATOR_LISTENER (ligature);
63
64 public:
65   TRANSLATOR_DECLARATIONS (Mensural_ligature_engraver);
66
67 private:
68   void transform_heads (vector<Grob_info> const &primitives);
69   void propagate_properties (Spanner *ligature,
70                              vector<Grob_info> const &primitives);
71   void fold_up_primitives (vector<Grob_info> const &primitives);
72 };
73
74 IMPLEMENT_TRANSLATOR_LISTENER (Mensural_ligature_engraver, ligature);
75 void
76 Mensural_ligature_engraver::listen_ligature (Stream_event *ev)
77 {
78   Ligature_engraver::listen_ligature (ev);
79 }
80
81 Mensural_ligature_engraver::Mensural_ligature_engraver ()
82 {
83   brew_ligature_primitive_proc
84     = Mensural_ligature::brew_ligature_primitive_proc;
85 }
86
87 Spanner *
88 Mensural_ligature_engraver::create_ligature_spanner ()
89 {
90   return make_spanner ("MensuralLigature", SCM_EOL);
91 }
92
93 void
94 Mensural_ligature_engraver::transform_heads (vector<Grob_info> const &primitives)
95 {
96   if (primitives.size () < 2)
97     {
98       warning (_ ("ligature with less than 2 heads -> skipping"));
99       return;
100     }
101   int prev_pitch = 0;
102   bool at_beginning = true;
103
104   // needed so that we can check whether
105   // the previous note can be turned into a flexa
106   bool prev_brevis_shape = false;
107
108   bool prev_semibrevis = false;
109   Item *prev_primitive = NULL;
110
111   for (vsize i = 0, s = primitives.size (); i < s; i++)
112     {
113       Grob_info info = primitives[i];
114       Item *primitive = dynamic_cast<Item *> (info.grob ());
115       int duration_log = Rhythmic_head::duration_log (primitive);
116
117       Stream_event *nr = info.event_cause ();
118
119       /*
120         ugh. why not simply check for pitch?
121       */
122       if (!nr->in_event_class ("note-event"))
123         {
124           nr->origin ()->warning
125           (_ ("cannot determine pitch of ligature primitive -> skipping"));
126           at_beginning = true;
127           continue;
128         }
129
130       int pitch = unsmob_pitch (nr->get_property ("pitch"))->steps ();
131       int prim = 0;
132
133       if (at_beginning)
134         {
135           if (i == s - 1)
136             {
137               // we can get here after invalid input
138               nr->origin ()->warning
139               (_ ("single note ligature - skipping"));
140               break;
141             }
142           prev_semibrevis = prev_brevis_shape = false;
143           prev_primitive = NULL;
144         }
145       else
146         {
147           if (pitch == prev_pitch)
148             {
149               nr->origin ()->warning
150               (_ ("prime interval within ligature -> skipping"));
151               at_beginning = true;
152               prim = MLP_NONE;
153               continue;
154             }
155         }
156
157       if (duration_log < -3 // is this possible at all???
158           || duration_log > 0)
159         {
160           nr->origin ()->warning
161           (_ ("mensural ligature: duration none of Mx, L, B, S -> skipping"));
162           prim = MLP_NONE;
163           at_beginning = true;
164           continue;
165         }
166
167       bool general_case = true;
168       bool make_flexa = false;
169       bool allow_flexa = true;
170
171       // first check special cases
172       // 1. beginning
173       if (at_beginning)
174         {
175           // a. semibreves
176           if (duration_log == 0)
177             {
178               prim = MLP_UP | MLP_BREVIS;
179               general_case = false;
180             }
181           // b. descendens longa or brevis
182           else if (i < s - 1
183                    && (unsmob_pitch (primitives[i + 1].event_cause ()
184                                      ->get_property ("pitch"))->steps () < pitch)
185                    && duration_log > -3)
186             {
187               int left_stem = duration_log == -1 ? MLP_DOWN : 0;
188               prim = left_stem | MLP_BREVIS;
189               general_case = false;
190             }
191         }
192       // 2. initial semibrevis must be followed by another one
193       else if (prev_semibrevis)
194         {
195           prev_semibrevis = false;
196           if (duration_log == 0)
197             {
198               prim = MLP_BREVIS;
199               general_case = false;
200             }
201           else
202             {
203               nr->origin ()->warning
204               (_ ("semibrevis must be followed by another one -> skipping"));
205               prim = MLP_NONE;
206               at_beginning = true;
207               continue;
208             }
209         }
210       // 3. semibreves are otherwise not allowed
211       else if (duration_log == 0)
212         {
213           nr->origin ()->warning
214           (_ ("semibreves can only appear at the beginning of a ligature,\n"
215               "and there may be only zero or two of them"));
216           prim = MLP_NONE;
217           at_beginning = true;
218           continue;
219         }
220       // 4. end, descendens
221       else if (i == s - 1 && pitch < prev_pitch)
222         {
223           // brevis; previous note must be turned into flexa
224           if (duration_log == -1)
225             {
226               if (prev_brevis_shape)
227                 {
228                   make_flexa = true;
229                   general_case = false;
230                 }
231               else
232                 {
233                   nr->origin ()->warning
234                   (_ ("invalid ligatura ending:\n"
235                       "when the last note is a descending brevis,\n"
236                       "the penultimate note must be another one,\n"
237                       "or the ligatura must be LB or SSB"));
238                   prim = MLP_NONE;
239                   break;
240                 }
241             }
242           // longa
243           else if (duration_log == -2)
244             {
245               prim = MLP_BREVIS;
246               general_case = allow_flexa = false;
247             }
248           // else maxima; fall through to regular case below
249         }
250
251       if (allow_flexa
252           && to_boolean (primitive->get_property ("ligature-flexa")))
253         {
254           /*
255             flexa requested, check whether allowed:
256             - there should be a previous note
257             - both of the notes must be of brevis shape
258               (i.e. can't be maxima or flexa;
259               longa is forbidden as well - it's nonexistent anyway)
260             - no compulsory flexa for the next note,
261               i.e. it's not an ultimate descending breve
262           */
263           make_flexa = !at_beginning && prev_brevis_shape && duration_log > -2;
264           if (make_flexa && i == s - 2)
265             {
266               /*
267                 check last condition: look ahead to next note
268               */
269               Grob_info next_info = primitives[i + 1];
270               Item *next_primitive = dynamic_cast<Item *> (next_info.grob ());
271               if (Rhythmic_head::duration_log (next_primitive) == -1)
272                 {
273                   /*
274                     breve: check whether descending
275                   */
276                   int const next_pitch = unsmob_pitch
277                                          (next_info.event_cause ()->get_property ("pitch"))->steps ();
278                   if (next_pitch < pitch)
279                     /*
280                       sorry, forbidden
281                     */
282                     make_flexa = false;
283                 }
284             }
285         }
286
287       if (general_case)
288         {
289           static int const shape[3] = {MLP_MAXIMA, MLP_LONGA, MLP_BREVIS};
290
291           prim = shape[duration_log + 3];
292         }
293
294       if (make_flexa)
295         {
296           /*
297             turn the note with the previous one into a flexa
298           */
299           prev_primitive->set_property
300           ("primitive",
301            scm_from_int
302            (MLP_FLEXA_BEGIN
303             | (scm_to_int (prev_primitive->get_property ("primitive"))
304                & MLP_STEM)));
305           prev_primitive->set_property
306           ("flexa-interval", scm_from_int (pitch - prev_pitch));
307           prim = MLP_FLEXA_END;
308           primitive->set_property
309           ("flexa-interval", scm_from_int (pitch - prev_pitch));
310         }
311
312       // join_primitives replacement
313       if (!(at_beginning || make_flexa))
314         prev_primitive->set_property ("add-join", ly_bool2scm (true));
315
316       at_beginning = false;
317       prev_primitive = primitive;
318       prev_pitch = pitch;
319       primitive->set_property ("primitive", scm_from_int (prim));
320       prev_brevis_shape = (prim & MLP_BREVIS) != 0;
321       prev_semibrevis = (prim & MLP_UP) != 0;
322     }
323 }
324
325 /*
326  * A MensuralLigature grob consists of a bunch of NoteHead grobs that
327  * are glued together.  It (a) does not make sense to change
328  * properties like thickness or flexa-width from one head to the next
329  * within a ligature (this would totally screw up alignment), and (b)
330  * some of these properties (like flexa-width) are specific to
331  * e.g. the MensuralLigature (as in contrast to e.g. LigatureBracket),
332  * and therefore should not be handled in the NoteHead code (which is
333  * also used by LigatureBracket).  Therefore, we let the user control
334  * these properties via the concrete Ligature grob (like
335  * MensuralLigature) and then copy these properties as necessary to
336  * each of the NoteHead grobs.  This is what
337  * propagate_properties () does.
338  */
339 void
340 Mensural_ligature_engraver::propagate_properties (Spanner *ligature,
341                                                   vector<Grob_info> const &primitives)
342 {
343   Real thickness
344     = robust_scm2double (ligature->get_property ("thickness"), 1.3);
345   thickness
346   *= ligature->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
347
348   Real head_width
349     = Font_interface::get_default_font (ligature)->
350       find_by_name ("noteheads.sM1mensural").extent (X_AXIS).length ();
351   Real maxima_head_width
352     = Font_interface::get_default_font (ligature)->
353       find_by_name ("noteheads.sM3ligmensural").extent (X_AXIS).length ();
354
355   Item *prev_primitive = NULL;
356   for (vsize i = 0; i < primitives.size (); i++)
357     {
358       Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
359       int output = scm_to_int (primitive->get_property ("primitive"));
360       primitive->set_property ("thickness",
361                                scm_from_double (thickness));
362
363       switch (output & MLP_ANY)
364         {
365         case MLP_BREVIS:
366         case MLP_LONGA:
367           primitive->set_property ("head-width", scm_from_double (head_width));
368           break;
369         case MLP_MAXIMA:
370           primitive->set_property ("head-width",
371                                    scm_from_double (maxima_head_width));
372           break;
373         case MLP_FLEXA_BEGIN:
374           /*
375             the next note (should be MLP_FLEXA_END) will handle this one
376           */
377           break;
378         case MLP_FLEXA_END:
379           {
380             SCM flexa_scm = primitive->get_property ("flexa-width");
381             Real const flexa_width = robust_scm2double (flexa_scm, 2.0);
382             SCM head_width = scm_from_double (0.5 * (flexa_width + thickness));
383             primitive->set_property ("head-width", head_width);
384             prev_primitive->set_property ("head-width", head_width);
385             prev_primitive->set_property ("flexa-width", flexa_scm);
386           }
387           break;
388         default:
389           programming_error (_ ("unexpected case fall-through"));
390           break;
391         }
392
393       prev_primitive = primitive;
394     }
395 }
396
397 void
398 Mensural_ligature_engraver::fold_up_primitives (vector<Grob_info> const &primitives)
399 {
400   Item *first = 0;
401   Real distance = 0.0;
402   Real staff_space = 0.0;
403   Real thickness = 0.0;
404
405   for (vsize i = 0; i < primitives.size (); i++)
406     {
407       Item *current = dynamic_cast<Item *> (primitives[i].grob ());
408       if (i == 0)
409         {
410           first = current;
411           staff_space = Staff_symbol_referencer::staff_space (first);
412           thickness = scm_to_double (current->get_property ("thickness"));
413         }
414
415       move_related_items_to_column (current, first->get_column (),
416                                     distance);
417
418       Real head_width = scm_to_double (current->get_property ("head-width"));
419       distance += head_width - thickness;
420
421       if (Rhythmic_head::dot_count (current) > 0)
422         /*
423           Move dots above/behind the ligature.
424           dots should also avoid staff lines.
425         */
426         {
427           Grob *dot_gr = Rhythmic_head::get_dots (current);
428
429           bool const on_line = Staff_symbol_referencer::on_line
430                                (current,
431                                 robust_scm2int (current->get_property ("staff-position"), 0));
432           Real vert_shift = on_line ? staff_space * 0.5 : 0.0;
433           bool const flexa_begin
434             = scm_to_int (current->get_property ("primitive"))
435               & MLP_FLEXA_BEGIN;
436
437           if (i + 1 < primitives.size ())
438             /*
439               dot in the midst => avoid next note;
440               what to avoid and where depends on
441               being on a line or between lines
442             */
443             {
444               int const delta
445                 = scm_to_int (current->get_property ("delta-position"));
446               if (flexa_begin)
447                 vert_shift += delta < 0
448                               ? staff_space : (on_line ? -2.0 : -1.0) * staff_space;
449               else if (on_line)
450                 {
451                   if (0 < delta && delta < 3)
452                     vert_shift -= staff_space;
453                 }
454               else if (delta == 1 || delta == -1)
455                 vert_shift -= delta * staff_space;
456             }
457
458           dot_gr->translate_axis (vert_shift, Y_AXIS);
459
460           /*
461             move all dots behind head
462           */
463           dot_gr->translate_axis
464           ((flexa_begin ? staff_space * 0.6 : head_width) - 2.0 * thickness, X_AXIS);
465         }
466     }
467 }
468
469 void
470 Mensural_ligature_engraver::build_ligature (Spanner *ligature,
471                                             vector<Grob_info> const &primitives)
472 {
473   transform_heads (primitives);
474   propagate_properties (ligature, primitives);
475   fold_up_primitives (primitives);
476 }
477
478 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, rest);
479 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, ligature_head);
480
481 ADD_TRANSLATOR (Mensural_ligature_engraver,
482                 /* doc */
483                 "Handle @code{Mensural_ligature_events} by glueing special"
484                 " ligature heads together.",
485
486                 /* create */
487                 "MensuralLigature ",
488
489                 /* read */
490                 "",
491
492                 /* write */
493                 ""
494                );