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