2 mensural-ligature-engraver.cc -- implement Mensural_ligature_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2005 Juergen Reuter <reuter@ipd.uka.de>,
7 Pal Benko <benkop@freestart.hu>
10 #include "coherent-ligature-engraver.hh"
11 #include "mensural-ligature.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"
24 * TODO: accidentals are aligned with the first note;
25 * they must appear ahead.
27 * TODO: prohibit ligatures having notes differing only in accidentals
28 * (like \[ a\breve g as \])
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.
33 * TODO: do something with multiple voices within a ligature. See
35 * Ockeghem: Missa Ecce ancilla domini, bassus part, end of Christe.
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
43 class Mensural_ligature_engraver : public Coherent_ligature_engraver
47 virtual Spanner *create_ligature_spanner ();
48 virtual void build_ligature (Spanner *ligature, Array<Grob_info> primitives);
51 TRANSLATOR_DECLARATIONS (Mensural_ligature_engraver);
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);
59 Mensural_ligature_engraver::Mensural_ligature_engraver ()
64 Mensural_ligature_engraver::create_ligature_spanner ()
66 return make_spanner ("MensuralLigature", SCM_EOL);
70 Mensural_ligature_engraver::transform_heads (Array<Grob_info> primitives)
72 if (primitives.size () < 2)
74 warning (_f ("ligature with less than 2 heads -> skipping"));
78 bool at_beginning = true;
80 // needed so that we can check whether
81 // the previous note can be turned into a flexa
82 bool prev_brevis_shape = false;
84 bool prev_semibrevis = false;
85 Item *prev_primitive = NULL;
87 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);
93 Music *nr = info.music_cause ();
96 ugh. why not simply check for pitch?
98 if (!nr->is_mus_type ("note-event"))
100 nr->origin ()->warning
101 (_f ("cannot determine pitch of ligature primitive -> skipping"));
106 int pitch = unsmob_pitch (nr->get_property ("pitch"))->steps ();
113 // we can get here after invalid input
114 nr->origin ()->warning
115 (_f ("single note ligature - skipping"));
118 prev_semibrevis = prev_brevis_shape = false;
119 prev_primitive = NULL;
123 delta_pitch = pitch - prev_pitch;
124 if (delta_pitch == 0)
126 nr->origin ()->warning
127 (_f ("prime interval within ligature -> skipping"));
129 primitive->set_property ("primitive",
130 scm_from_int (MLP_NONE));
135 if (duration_log < -3 // is this possible at all???
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));
146 // apply_transition replacement begins
147 bool general_case = true;
149 // first check special cases
154 if (duration_log == 0)
156 primitive->set_property ("primitive",
157 scm_from_int (MLP_UP | MLP_BREVIS));
158 prev_semibrevis = prev_brevis_shape = true;
159 general_case = false;
161 // b. descendens longa or brevis
163 && (unsmob_pitch (primitives[i + 1].music_cause ()
164 ->get_property ("pitch"))->steps () < pitch)
165 && duration_log > -3)
167 int left_stem = duration_log == -1 ? MLP_DOWN : 0;
169 primitive->set_property ("primitive",
170 scm_from_int (left_stem | MLP_BREVIS));
171 prev_brevis_shape = true;
172 prev_semibrevis = general_case = false;
175 // 2. initial semibrevis must be followed by another one
176 else if (prev_semibrevis)
178 prev_semibrevis = false;
179 if (duration_log == 0)
181 primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
182 general_case = false;
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));
194 // 3. semibreves are otherwise not allowed
195 else if (duration_log == 0)
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));
205 // 4. end, descendens
206 else if (i == s - 1 && delta_pitch < 0)
208 // brevis; previous note must be turned into flexa
209 if (duration_log == -1)
211 if (prev_brevis_shape)
213 prev_primitive->set_property
217 | (scm_to_int (prev_primitive->get_property ("primitive"))
219 primitive->set_property ("primitive", scm_from_int (MLP_NONE));
220 break; // no more notes, no join
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));
234 else if (duration_log == -2)
236 primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
237 general_case = false;
239 // else maxima; fall through regular case below
244 static int const shape[3] = {MLP_MAXIMA, MLP_LONGA, MLP_BREVIS};
246 primitive->set_property ("primitive",
247 scm_from_int (shape[duration_log + 3]));
248 prev_brevis_shape = duration_log == -1;
251 // join_primitives replacement
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
260 && (scm_to_int (prev_primitive->get_property ("primitive"))
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 ()
269 prev_primitive->set_property ("join-right-amount",
270 scm_from_int (delta_pitch));
271 // perhaps set add-join as well
273 at_beginning = false;
274 prev_primitive = primitive;
276 // apply_transition replacement ends
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.
295 Mensural_ligature_engraver::propagate_properties (Spanner *ligature,
296 Array<Grob_info> primitives)
299 = robust_scm2double (ligature->get_property ("thickness"), 1.4);
301 *= ligature->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
304 = Font_interface::get_default_font (ligature)->
305 find_by_name ("noteheads.s-1mensural").extent (X_AXIS).length ();
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 ();
312 flexa_width *= Staff_symbol_referencer::staff_space (ligature);
314 Real half_flexa_width = 0.5 * (flexa_width + thickness);
316 for (int i = 0; i < primitives.size (); i++)
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));
323 switch (output & MLP_ANY)
326 primitive->set_property ("head-width",
327 scm_from_double (half_flexa_width));
331 primitive->set_property ("head-width",
332 scm_from_double (head_width));
335 primitive->set_property ("head-width",
336 scm_from_double (maxima_head_width));
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));
345 programming_error (_f ("unexpected case fall-through"));
352 Mensural_ligature_engraver::fold_up_primitives (Array<Grob_info> primitives)
356 for (int i = 0; i < primitives.size (); i++)
358 Item *current = dynamic_cast<Item *> (primitives[i].grob ());
362 get_set_column (current, first->get_column ());
365 current->translate_axis (distance, X_AXIS);
368 += scm_to_double (current->get_property ("head-width"))
369 - scm_to_double (current->get_property ("thickness"));
374 Mensural_ligature_engraver::build_ligature (Spanner *ligature,
375 Array<Grob_info> primitives)
377 transform_heads (primitives);
378 propagate_properties (ligature, primitives);
379 fold_up_primitives (primitives);
382 #include "translator.icc"
384 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, rest);
385 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, note_head);
386 ADD_TRANSLATOR (Mensural_ligature_engraver,
387 /* doc */ "Handles Mensural_ligature_events by glueing special ligature heads together.",
388 /* create */ "MensuralLigature",
389 /* accept */ "ligature-event",