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