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