]> git.donarmstrong.com Git - lilypond.git/blob - lily/mensural-ligature-engraver.cc
new file.
[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 "event.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
60 Mensural_ligature_engraver::Mensural_ligature_engraver ()
61 {
62 }
63
64 Spanner *
65 Mensural_ligature_engraver::create_ligature_spanner ()
66 {
67   return make_spanner ("MensuralLigature", SCM_EOL);
68 }
69
70 void
71 Mensural_ligature_engraver::transform_heads (Array<Grob_info> primitives)
72 {
73   if (primitives.size () < 2)
74     {
75       warning (_f ("ligature with less than 2 heads -> skipping"));
76       return;
77     }
78   int prev_pitch = 0;
79   bool at_beginning = true;
80
81   // needed so that we can check whether
82   // the previous note can be turned into a flexa
83   bool prev_brevis_shape = false;
84
85   bool prev_semibrevis = false;
86   Item *prev_primitive = NULL;
87
88   for (int i = 0, s = primitives.size (); i < s; i++) {
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 ("can not 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_int2num (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_int2num (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_int2num (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_int2num (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_int2num(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_int2num (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_int2num (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_int2num
216                    (MLP_FLEXA
217                     | (scm_to_int (prev_primitive->get_property ("primitive"))
218                        & MLP_DOWN)));
219                 primitive->set_property ("primitive", scm_int2num (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_int2num (MLP_NONE));
230                 break;
231               }
232           }
233         // longa
234         else if (duration_log == -2)
235           {
236             primitive->set_property ("primitive", scm_int2num (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_int2num (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_int2num (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_make_real (thickness));
322
323       switch (output & MLP_ANY) {
324       case MLP_NONE:
325         primitive->set_property ("head-width",
326                                  scm_make_real (half_flexa_width));
327         break;
328       case MLP_BREVIS:
329       case MLP_LONGA:
330         primitive->set_property ("head-width",
331                                  scm_make_real (head_width));
332         break;
333       case MLP_MAXIMA:
334         primitive->set_property ("head-width",
335                                  scm_make_real (maxima_head_width));
336         break;
337       case MLP_FLEXA:
338         primitive->set_property ("head-width",
339                                  scm_make_real (half_flexa_width));
340         primitive->set_property ("flexa-width",
341                                  scm_make_real (flexa_width));
342         break;
343       default:
344         programming_error (_f ("unexpected case fall-through"));
345         break;
346       }
347     }
348 }
349
350 void
351 Mensural_ligature_engraver::fold_up_primitives (Array<Grob_info> primitives)
352 {
353   Item *first = 0;
354   Real distance = 0;
355   for (int i = 0; i < primitives.size (); i++)
356     {
357       Item *current = dynamic_cast<Item*> (primitives[i].grob_);
358       if (i == 0)
359         {
360           first = current;
361         }
362
363       get_set_column (current, first->get_column ());
364
365       if (i > 0)
366         {
367           current->translate_axis (distance, X_AXIS);
368         }
369
370       distance +=
371         scm_to_double (current->get_property ("head-width")) -
372         scm_to_double (current->get_property ("thickness"));
373     }
374 }
375
376 void
377 Mensural_ligature_engraver::build_ligature (Spanner *ligature,
378                                             Array<Grob_info> primitives)
379 {
380   transform_heads (primitives);
381   propagate_properties (ligature, primitives);
382   fold_up_primitives (primitives);
383 }
384
385 ADD_TRANSLATOR (Mensural_ligature_engraver,
386 /* descr */       "Handles Mensural_ligature_events by glueing special ligature heads together.",
387 /* creats*/       "MensuralLigature",
388 /* accepts */     "ligature-event",
389 /* acks  */      "note-head-interface rest-interface",
390 /* reads */       "",
391 /* write */       "");