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