]> git.donarmstrong.com Git - lilypond.git/blob - lily/mensural-ligature-engraver.cc
(class Phrasing_slur_engraver):
[lilypond.git] / lily / mensural-ligature-engraver.cc
1 /*
2   mensural-ligature-engraver.cc -- implement Mensural_ligature_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2002--2005 Juergen Reuter <reuter@ipd.uka.de>,
7   Pal Benko <benkop@freestart.hu>
8 */
9
10 #include "coherent-ligature-engraver.hh"
11 #include "mensural-ligature.hh"
12 #include "music.hh"
13 #include "warn.hh"
14 #include "spanner.hh"
15 #include "paper-column.hh"
16 #include "note-column.hh"
17 #include "rhythmic-head.hh"
18 #include "note-head.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "output-def.hh"
21 #include "font-interface.hh"
22
23 /*
24  * TODO: accidentals are aligned with the first note;
25  * they must appear ahead.
26  *
27  * TODO: prohibit ligatures having notes differing only in accidentals
28  * (like \[ a\breve g as \])
29  *
30  * TODO: dotted heads: avoid next note colliding with the dot, e.g. by
31  * putting it *above* (rather than after) the affected ligature head.
32  *
33  * TODO: do something with multiple voices within a ligature.  See
34  * for example:
35  * Ockeghem: Missa Ecce ancilla domini, bassus part, end of Christe.
36  *
37  * TODO: enhance robustness: in case of an illegal ligature (e.g. the
38  * input specifies a ligature that contains a minima), automatically
39  * break the ligature into smaller, valid pieces.  Such a piece may be
40  * a single note.
41  */
42
43 class Mensural_ligature_engraver : public Coherent_ligature_engraver
44 {
45
46 protected:
47   virtual Spanner *create_ligature_spanner ();
48   virtual void build_ligature (Spanner *ligature, Array<Grob_info> primitives);
49
50 public:
51   TRANSLATOR_DECLARATIONS (Mensural_ligature_engraver);
52
53 private:
54   void transform_heads (Array<Grob_info> primitives);
55   void propagate_properties (Spanner *ligature, Array<Grob_info> primitives);
56   void fold_up_primitives (Array<Grob_info> primitives);
57 };
58
59 Mensural_ligature_engraver::Mensural_ligature_engraver ()
60 {
61 }
62
63 Spanner *
64 Mensural_ligature_engraver::create_ligature_spanner ()
65 {
66   return make_spanner ("MensuralLigature", SCM_EOL);
67 }
68
69 void
70 Mensural_ligature_engraver::transform_heads (Array<Grob_info> primitives)
71 {
72   if (primitives.size () < 2)
73     {
74       warning (_f ("ligature with less than 2 heads -> skipping"));
75       return;
76     }
77   int prev_pitch = 0;
78   bool at_beginning = true;
79
80   // needed so that we can check whether
81   // the previous note can be turned into a flexa
82   bool prev_brevis_shape = false;
83
84   bool prev_semibrevis = false;
85   Item *prev_primitive = NULL;
86
87   for (int i = 0, s = primitives.size (); i < s; i++)
88     {
89       Grob_info info = primitives[i];
90       Item *primitive = dynamic_cast<Item *> (info.grob ());
91       int duration_log = Note_head::get_balltype (primitive);
92
93       Music *nr = info.music_cause ();
94
95       /*
96         ugh. why not simply check for pitch?
97       */
98       if (!nr->is_mus_type ("note-event"))
99         {
100           nr->origin ()->warning
101             (_f ("cannot determine pitch of ligature primitive -> skipping"));
102           at_beginning = true;
103           continue;
104         }
105
106       int pitch = unsmob_pitch (nr->get_property ("pitch"))->steps ();
107       int delta_pitch = 0;
108
109       if (at_beginning)
110         {
111           if (i == s - 1)
112             {
113               // we can get here after invalid input
114               nr->origin ()->warning
115                 (_f ("single note ligature - skipping"));
116               break;
117             }
118           prev_semibrevis = prev_brevis_shape = false;
119           prev_primitive = NULL;
120         }
121       else
122         {
123           delta_pitch = pitch - prev_pitch;
124           if (delta_pitch == 0)
125             {
126               nr->origin ()->warning
127                 (_f ("prime interval within ligature -> skipping"));
128               at_beginning = true;
129               primitive->set_property ("primitive",
130                                        scm_from_int (MLP_NONE));
131               continue;
132             }
133         }
134
135       if (duration_log < -3 // is this possible at all???
136           || duration_log > 0)
137         {
138           nr->origin ()->warning
139             (_f ("mensural ligature: duration none of Mx, L, B, S -> skipping"));
140           primitive->set_property ("primitive",
141                                    scm_from_int (MLP_NONE));
142           at_beginning = true;
143           continue;
144         }
145
146       // apply_transition replacement begins
147       bool general_case = true;
148
149       // first check special cases
150       // 1. beginning
151       if (at_beginning)
152         {
153           // a. semibreves
154           if (duration_log == 0)
155             {
156               primitive->set_property ("primitive",
157                                        scm_from_int (MLP_UP | MLP_BREVIS));
158               prev_semibrevis = prev_brevis_shape = true;
159               general_case = false;
160             }
161           // b. descendens longa or brevis
162           else if (i < s - 1
163                    && (unsmob_pitch (primitives[i + 1].music_cause ()
164                                      ->get_property ("pitch"))->steps () < pitch)
165                    && duration_log > -3)
166             {
167               int left_stem = duration_log == -1 ? MLP_DOWN : 0;
168
169               primitive->set_property ("primitive",
170                                        scm_from_int (left_stem | MLP_BREVIS));
171               prev_brevis_shape = true;
172               prev_semibrevis = general_case = false;
173             }
174         }
175       // 2. initial semibrevis must be followed by another one
176       else if (prev_semibrevis)
177         {
178           prev_semibrevis = false;
179           if (duration_log == 0)
180             {
181               primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
182               general_case = false;
183             }
184           else
185             {
186               nr->origin ()->warning
187                 (_f ("semibrevis must be followed by another one -> skipping"));
188               primitive->set_property ("primitive",
189                                        scm_from_int (MLP_NONE));
190               at_beginning = true;
191               continue;
192             }
193         }
194       // 3. semibreves are otherwise not allowed
195       else if (duration_log == 0)
196         {
197           nr->origin ()->warning
198             (_f ("semibreves can only appear at the beginning of a ligature,\n"
199                  "and there may be only zero or two of them"));
200           primitive->set_property ("primitive",
201                                    scm_from_int (MLP_NONE));
202           at_beginning = true;
203           continue;
204         }
205       // 4. end, descendens
206       else if (i == s - 1 && delta_pitch < 0)
207         {
208           // brevis; previous note must be turned into flexa
209           if (duration_log == -1)
210             {
211               if (prev_brevis_shape)
212                 {
213                   prev_primitive->set_property
214                     ("primitive",
215                      scm_from_int
216                      (MLP_FLEXA
217                       | (scm_to_int (prev_primitive->get_property ("primitive"))
218                          & MLP_DOWN)));
219                   primitive->set_property ("primitive", scm_from_int (MLP_NONE));
220                   break; // no more notes, no join
221                 }
222               else
223                 {
224                   nr->origin ()->warning
225                     (_f ("invalid ligatura ending:\n"
226                          "when the last note is a descending brevis,\n"
227                          "the penultimate note must be another one,\n"
228                          "or the ligatura must be LB or SSB"));
229                   primitive->set_property ("primitive", scm_from_int (MLP_NONE));
230                   break;
231                 }
232             }
233           // longa
234           else if (duration_log == -2)
235             {
236               primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
237               general_case = false;
238             }
239           // else maxima; fall through regular case below
240         }
241
242       if (general_case)
243         {
244           static int const shape[3] = {MLP_MAXIMA, MLP_LONGA, MLP_BREVIS};
245
246           primitive->set_property ("primitive",
247                                    scm_from_int (shape[duration_log + 3]));
248           prev_brevis_shape = duration_log == -1;
249         }
250
251       // join_primitives replacement
252       if (!at_beginning)
253         {
254           /*
255             if the previous note is longa-shaped and this note is lower,
256             then the joining line may hide the stem, so it is made longer
257             to serve as stem as well
258           */
259           if (delta_pitch < 0
260               && (scm_to_int (prev_primitive->get_property ("primitive"))
261                   & MLP_LONGA))
262             {
263               delta_pitch -= 6;
264               // instead of number 6
265               // the legth of the longa stem should be queried something like
266               // Font_interface::get_default_font (ligature)->find_by_name
267               //  ("noteheads.s-2mensural").extent (Y_AXIS).length ()
268             }
269           prev_primitive->set_property ("join-right-amount",
270                                         scm_from_int (delta_pitch));
271           // perhaps set add-join as well
272         }
273       at_beginning = false;
274       prev_primitive = primitive;
275       prev_pitch = pitch;
276       // apply_transition replacement ends
277     }
278 }
279
280 /*
281  * A MensuralLigature grob consists of a bunch of NoteHead grobs that
282  * are glued together.  It (a) does not make sense to change
283  * properties like thickness or flexa-width from one head to the next
284  * within a ligature (this would totally screw up alignment), and (b)
285  * some of these properties (like flexa-width) are specific to
286  * e.g. the MensuralLigature (as in contrast to e.g. LigatureBracket),
287  * and therefore should not be handled in the NoteHead code (which is
288  * also used by LigatureBracket).  Therefore, we let the user control
289  * these properties via the concrete Ligature grob (like
290  * MensuralLigature) and then copy these properties as necessary to
291  * each of the NoteHead grobs.  This is what
292  * propagate_properties () does.
293  */
294 void
295 Mensural_ligature_engraver::propagate_properties (Spanner *ligature,
296                                                   Array<Grob_info> primitives)
297 {
298   Real thickness
299     = robust_scm2double (ligature->get_property ("thickness"), 1.4);
300   thickness
301     *= ligature->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
302
303   Real head_width
304     = Font_interface::get_default_font (ligature)->
305     find_by_name ("noteheads.s-1mensural").extent (X_AXIS).length ();
306   Real flexa_width
307     = robust_scm2double (ligature->get_property ("flexa-width"), 2);
308   Real maxima_head_width
309     = Font_interface::get_default_font (ligature)->
310     find_by_name ("noteheads.s-1neomensural").extent (X_AXIS).length ();
311
312   flexa_width *= Staff_symbol_referencer::staff_space (ligature);
313
314   Real half_flexa_width = 0.5 * (flexa_width + thickness);
315
316   for (int i = 0; i < primitives.size (); i++)
317     {
318       Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
319       int output = scm_to_int (primitive->get_property ("primitive"));
320       primitive->set_property ("thickness",
321                                scm_from_double (thickness));
322
323       switch (output & MLP_ANY)
324         {
325         case MLP_NONE:
326           primitive->set_property ("head-width",
327                                    scm_from_double (half_flexa_width));
328           break;
329         case MLP_BREVIS:
330         case MLP_LONGA:
331           primitive->set_property ("head-width",
332                                    scm_from_double (head_width));
333           break;
334         case MLP_MAXIMA:
335           primitive->set_property ("head-width",
336                                    scm_from_double (maxima_head_width));
337           break;
338         case MLP_FLEXA:
339           primitive->set_property ("head-width",
340                                    scm_from_double (half_flexa_width));
341           primitive->set_property ("flexa-width",
342                                    scm_from_double (flexa_width));
343           break;
344         default:
345           programming_error (_f ("unexpected case fall-through"));
346           break;
347         }
348     }
349 }
350
351 void
352 Mensural_ligature_engraver::fold_up_primitives (Array<Grob_info> primitives)
353 {
354   Item *first = 0;
355   Real distance = 0;
356   for (int i = 0; i < primitives.size (); i++)
357     {
358       Item *current = dynamic_cast<Item *> (primitives[i].grob ());
359       if (i == 0)
360         {
361           first = current;
362         }
363
364       get_set_column (current, first->get_column ());
365
366       if (i > 0)
367         {
368           current->translate_axis (distance, X_AXIS);
369         }
370
371       distance
372         += scm_to_double (current->get_property ("head-width"))
373         - scm_to_double (current->get_property ("thickness"));
374     }
375 }
376
377 void
378 Mensural_ligature_engraver::build_ligature (Spanner *ligature,
379                                             Array<Grob_info> primitives)
380 {
381   transform_heads (primitives);
382   propagate_properties (ligature, primitives);
383   fold_up_primitives (primitives);
384 }
385
386 #include "translator.icc"
387
388 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, rest);
389 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, note_head);
390 ADD_TRANSLATOR (Mensural_ligature_engraver,
391                 /* doc */ "Handles Mensural_ligature_events by glueing special ligature heads together.",
392                 /* create */ "MensuralLigature",
393                 /* accept */ "ligature-event",
394                 /* read */ "",
395                 /* write */ "");