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