]> git.donarmstrong.com Git - lilypond.git/blob - lily/mensural-ligature-engraver.cc
bfa30550e58efd99ff0605572d0f0f0db22101fd
[lilypond.git] / lily / mensural-ligature-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2015 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   TRANSLATOR_INHERIT (Coherent_ligature_engraver);
67
68 private:
69   void transform_heads (vector<Grob_info> const &primitives);
70   void propagate_properties (Spanner *ligature,
71                              vector<Grob_info> const &primitives,
72                              Real &min_length);
73   void fold_up_primitives (vector<Grob_info> const &primitives,
74                            Real &min_length);
75 };
76
77 IMPLEMENT_TRANSLATOR_LISTENER (Mensural_ligature_engraver, ligature);
78 void
79 Mensural_ligature_engraver::listen_ligature (Stream_event *ev)
80 {
81   Ligature_engraver::listen_ligature (ev);
82 }
83
84 Mensural_ligature_engraver::Mensural_ligature_engraver ()
85 {
86   brew_ligature_primitive_proc
87     = Mensural_ligature::brew_ligature_primitive_proc;
88 }
89
90 Spanner *
91 Mensural_ligature_engraver::create_ligature_spanner ()
92 {
93   return make_spanner ("MensuralLigature", SCM_EOL);
94 }
95
96 void
97 Mensural_ligature_engraver::transform_heads (vector<Grob_info> const &primitives)
98 {
99   if (primitives.size () < 2)
100     {
101       warning (_ ("ligature with less than 2 heads -> skipping"));
102       return;
103     }
104   int prev_pitch = 0;
105   bool at_beginning = true;
106
107   // needed so that we can check whether
108   // the previous note can be turned into a flexa
109   bool prev_brevis_shape = false;
110
111   bool prev_semibrevis = false;
112   Item *prev_primitive = NULL;
113
114   for (vsize i = 0, s = primitives.size (); i < s; i++)
115     {
116       Grob_info info = primitives[i];
117       Item *primitive = dynamic_cast<Item *> (info.grob ());
118       int duration_log = Rhythmic_head::duration_log (primitive);
119
120       Stream_event *nr = info.event_cause ();
121
122       /*
123         ugh. why not simply check for pitch?
124       */
125       if (!nr->in_event_class ("note-event"))
126         {
127           nr->origin ()->warning
128           (_ ("cannot determine pitch of ligature primitive -> skipping"));
129           at_beginning = true;
130           continue;
131         }
132
133       int pitch = unsmob<Pitch> (nr->get_property ("pitch"))->steps ();
134       int prim = 0;
135
136       if (at_beginning)
137         {
138           if (i == s - 1)
139             {
140               // we can get here after invalid input
141               nr->origin ()->warning
142               (_ ("single note ligature - skipping"));
143               break;
144             }
145           prev_semibrevis = prev_brevis_shape = false;
146           prev_primitive = NULL;
147         }
148       else
149         {
150           if (pitch == prev_pitch)
151             {
152               nr->origin ()->warning
153               (_ ("prime interval within ligature -> skipping"));
154               at_beginning = true;
155               prim = MLP_NONE;
156               continue;
157             }
158         }
159
160       if (duration_log < -3 // is this possible at all???
161           || duration_log > 0)
162         {
163           nr->origin ()->warning
164           (_ ("mensural ligature: duration none of Mx, L, B, S -> skipping"));
165           prim = MLP_NONE;
166           at_beginning = true;
167           continue;
168         }
169
170       bool general_case = true;
171       bool make_flexa = false;
172       bool allow_flexa = true;
173
174       // first check special cases
175       // 1. beginning
176       if (at_beginning)
177         {
178           // a. semibreves
179           if (duration_log == 0)
180             {
181               prim = MLP_UP | MLP_BREVIS;
182               general_case = false;
183             }
184           // b. descendens longa or brevis
185           else if (i < s - 1
186                    && (unsmob<Pitch> (primitives[i + 1].event_cause ()
187                                      ->get_property ("pitch"))->steps () < pitch)
188                    && duration_log > -3)
189             {
190               int left_stem = duration_log == -1 ? MLP_DOWN : 0;
191               prim = left_stem | MLP_BREVIS;
192               general_case = false;
193             }
194         }
195       // 2. initial semibrevis must be followed by another one
196       else if (prev_semibrevis)
197         {
198           prev_semibrevis = false;
199           if (duration_log == 0)
200             {
201               prim = MLP_BREVIS;
202               general_case = false;
203             }
204           else
205             {
206               nr->origin ()->warning
207               (_ ("semibrevis must be followed by another one -> skipping"));
208               prim = MLP_NONE;
209               at_beginning = true;
210               continue;
211             }
212         }
213       // 3. semibreves are otherwise not allowed
214       else if (duration_log == 0)
215         {
216           nr->origin ()->warning
217           (_ ("semibreves can only appear at the beginning of a ligature,\n"
218               "and there may be only zero or two of them"));
219           prim = MLP_NONE;
220           at_beginning = true;
221           continue;
222         }
223       // 4. end, descendens
224       else if (i == s - 1 && pitch < prev_pitch)
225         {
226           // brevis; previous note must be turned into flexa
227           if (duration_log == -1)
228             {
229               if (prev_brevis_shape)
230                 {
231                   make_flexa = true;
232                   general_case = false;
233                 }
234               else
235                 {
236                   nr->origin ()->warning
237                   (_ ("invalid ligatura ending:\n"
238                       "when the last note is a descending brevis,\n"
239                       "the penultimate note must be another one,\n"
240                       "or the ligatura must be LB or SSB"));
241                   prim = MLP_NONE;
242                   break;
243                 }
244             }
245           // longa
246           else if (duration_log == -2)
247             {
248               prim = MLP_BREVIS;
249               general_case = allow_flexa = false;
250             }
251           // else maxima; fall through to regular case below
252         }
253
254       if (allow_flexa
255           && to_boolean (primitive->get_property ("ligature-flexa")))
256         {
257           /*
258             flexa requested, check whether allowed:
259             - there should be a previous note
260             - both of the notes must be of brevis shape
261               (i.e. can't be maxima or flexa;
262               longa is forbidden as well - it's nonexistent anyway)
263             - no compulsory flexa for the next note,
264               i.e. it's not an ultimate descending breve
265           */
266           make_flexa = !at_beginning && prev_brevis_shape && duration_log > -2;
267           if (make_flexa && i == s - 2)
268             {
269               /*
270                 check last condition: look ahead to next note
271               */
272               Grob_info next_info = primitives[i + 1];
273               Item *next_primitive = dynamic_cast<Item *> (next_info.grob ());
274               if (Rhythmic_head::duration_log (next_primitive) == -1)
275                 {
276                   /*
277                     breve: check whether descending
278                   */
279                   int const next_pitch = unsmob<Pitch>
280                                          (next_info.event_cause ()->get_property ("pitch"))->steps ();
281                   if (next_pitch < pitch)
282                     /*
283                       sorry, forbidden
284                     */
285                     make_flexa = false;
286                 }
287             }
288         }
289
290       if (general_case)
291         {
292           static int const shape[3] = {MLP_MAXIMA, MLP_LONGA, MLP_BREVIS};
293
294           prim = shape[duration_log + 3];
295         }
296
297       if (make_flexa)
298         {
299           /*
300             turn the note with the previous one into a flexa
301           */
302           prev_primitive->set_property
303           ("primitive",
304            scm_from_int
305            (MLP_FLEXA_BEGIN
306             | (scm_to_int (prev_primitive->get_property ("primitive"))
307                & MLP_STEM)));
308           prev_primitive->set_property
309           ("flexa-interval", scm_from_int (pitch - prev_pitch));
310           prim = MLP_FLEXA_END;
311           primitive->set_property
312           ("flexa-interval", scm_from_int (pitch - prev_pitch));
313         }
314
315       // join_primitives replacement
316       if (!(at_beginning || make_flexa))
317         prev_primitive->set_property ("add-join", ly_bool2scm (true));
318
319       at_beginning = false;
320       prev_primitive = primitive;
321       prev_pitch = pitch;
322       primitive->set_property ("primitive", scm_from_int (prim));
323       prev_brevis_shape = (prim & MLP_BREVIS) != 0;
324       prev_semibrevis = (prim & MLP_UP) != 0;
325     }
326 }
327
328 /*
329  * A MensuralLigature grob consists of a bunch of NoteHead grobs that
330  * are glued together.  It (a) does not make sense to change
331  * properties like thickness or flexa-width from one head to the next
332  * within a ligature (this would totally screw up alignment), and (b)
333  * some of these properties (like flexa-width) are specific to
334  * e.g. the MensuralLigature (as in contrast to e.g. LigatureBracket),
335  * and therefore should not be handled in the NoteHead code (which is
336  * also used by LigatureBracket).  Therefore, we let the user control
337  * these properties via the concrete Ligature grob (like
338  * MensuralLigature) and then copy these properties as necessary to
339  * each of the NoteHead grobs.  This is what
340  * propagate_properties () does.
341  */
342 void
343 Mensural_ligature_engraver::propagate_properties (Spanner *ligature,
344                                                   vector<Grob_info> const &primitives,
345                                                   Real &min_length)
346 {
347   Real thickness
348     = robust_scm2double (ligature->get_property ("thickness"), 1.3);
349   thickness
350   *= ligature->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
351
352   Real head_width
353     = Font_interface::get_default_font (ligature)->
354       find_by_name ("noteheads.sM1mensural").extent (X_AXIS).length ();
355   Real maxima_head_width
356     = Font_interface::get_default_font (ligature)->
357       find_by_name ("noteheads.sM3ligmensural").extent (X_AXIS).length ();
358
359   min_length = 0.0;
360   Item *prev_primitive = NULL;
361   for (vsize i = 0; i < primitives.size (); i++)
362     {
363       Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
364       int output = scm_to_int (primitive->get_property ("primitive"));
365       primitive->set_property ("thickness",
366                                scm_from_double (thickness));
367
368       switch (output & MLP_ANY)
369         {
370         case MLP_BREVIS:
371         case MLP_LONGA:
372           min_length += head_width;
373           primitive->set_property ("head-width", scm_from_double (head_width));
374           break;
375         case MLP_MAXIMA:
376           min_length += maxima_head_width;
377           primitive->set_property ("head-width",
378                                    scm_from_double (maxima_head_width));
379           break;
380         case MLP_FLEXA_BEGIN:
381           /*
382             the next note (should be MLP_FLEXA_END) will handle this one
383           */
384           break;
385         case MLP_FLEXA_END:
386           {
387             SCM flexa_scm = primitive->get_property ("flexa-width");
388             Real const flexa_width = robust_scm2double (flexa_scm, 2.0);
389             min_length += flexa_width + thickness;
390             SCM head_width = scm_from_double (0.5 * (flexa_width + thickness));
391             primitive->set_property ("head-width", head_width);
392             prev_primitive->set_property ("head-width", head_width);
393             prev_primitive->set_property ("flexa-width", flexa_scm);
394           }
395           break;
396         default:
397           programming_error (_ ("unexpected case fall-through"));
398           break;
399         }
400
401       prev_primitive = primitive;
402     }
403 }
404
405 void
406 Mensural_ligature_engraver::fold_up_primitives (vector<Grob_info> const &primitives,
407                                                 Real &min_length)
408 {
409   Item *first = 0;
410   Real distance = 0.0;
411   Real staff_space = 0.0;
412   Real thickness = 0.0;
413
414   for (vsize i = 0; i < primitives.size (); i++)
415     {
416       Item *current = dynamic_cast<Item *> (primitives[i].grob ());
417       if (i == 0)
418         {
419           first = current;
420           staff_space = Staff_symbol_referencer::staff_space (first);
421           thickness = scm_to_double (current->get_property ("thickness"));
422         }
423
424       move_related_items_to_column (current, first->get_column (),
425                                     distance);
426
427       Real head_width = scm_to_double (current->get_property ("head-width"));
428       distance += head_width - thickness;
429
430       if (size_t const dot_count = Rhythmic_head::dot_count (current))
431         /*
432           Move dots above/behind the ligature.
433           dots should also avoid staff lines.
434         */
435         {
436           Grob *dot_gr = Rhythmic_head::get_dots (current);
437
438           bool const on_line = Staff_symbol_referencer::on_line
439                                (current,
440                                 robust_scm2int (current->get_property ("staff-position"), 0));
441           Real vert_shift = on_line ? staff_space * 0.5 : 0.0;
442           bool const flexa_begin
443             = scm_to_int (current->get_property ("primitive"))
444               & MLP_FLEXA_BEGIN;
445
446           if (i + 1 < primitives.size ())
447             /*
448               dot in the midst => avoid next note;
449               what to avoid and where depends on
450               being on a line or between lines
451             */
452             {
453               int const delta
454                 = scm_to_int (current->get_property ("delta-position"));
455               if (flexa_begin)
456                 vert_shift += delta < 0
457                               ? staff_space : (on_line ? -2.0 : -1.0) * staff_space;
458               else if (on_line)
459                 {
460                   if (0 < delta && delta < 3)
461                     vert_shift -= staff_space;
462                 }
463               else if (delta == 1 || delta == -1)
464                 vert_shift -= delta * staff_space;
465             }
466           else
467             min_length += head_width * dot_count;
468
469           dot_gr->translate_axis (vert_shift, Y_AXIS);
470
471           /*
472             move all dots behind head
473           */
474           dot_gr->translate_axis
475           ((flexa_begin ? staff_space * 0.6 : head_width) - 2.0 * thickness, X_AXIS);
476         }
477     }
478 }
479
480 void
481 Mensural_ligature_engraver::build_ligature (Spanner *ligature,
482                                             vector<Grob_info> const &primitives)
483 {
484   /*
485     the X extent of the actual graphics representing the ligature;
486     less space than that means collision
487   */
488   Real min_length;
489
490   transform_heads (primitives);
491   propagate_properties (ligature, primitives, min_length);
492   fold_up_primitives (primitives, min_length);
493
494   if (robust_scm2double (ligature->get_property ("minimum-length"), 0.0)
495       < min_length)
496     ligature->set_property ("minimum-length", scm_from_double (min_length));
497 }
498
499 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, rest);
500 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, ligature_head);
501
502 ADD_TRANSLATOR (Mensural_ligature_engraver,
503                 /* doc */
504                 "Handle @code{Mensural_ligature_events} by glueing special"
505                 " ligature heads together.",
506
507                 /* create */
508                 "MensuralLigature ",
509
510                 /* read */
511                 "",
512
513                 /* write */
514                 ""
515                );