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