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