2 mensural-ligature-engraver.cc -- implement Mensural_ligature_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2006 Juergen Reuter <reuter@ipd.uka.de>,
7 Pal Benko <benkop@freestart.hu>
10 #include "coherent-ligature-engraver.hh"
11 #include "font-interface.hh"
12 #include "international.hh"
13 #include "mensural-ligature.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"
21 #include "staff-symbol-referencer.hh"
25 * TODO: accidentals are aligned with the first note;
26 * they must appear ahead.
28 * TODO: prohibit ligatures having notes differing only in accidentals
29 * (like \[ a\breve g as \])
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.
34 * TODO: do something with multiple voices within a ligature. See
36 * Ockeghem: Missa Ecce ancilla domini, bassus part, end of Christe.
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
44 class Mensural_ligature_engraver : public Coherent_ligature_engraver
48 virtual Spanner *create_ligature_spanner ();
49 virtual void build_ligature (Spanner *ligature, vector<Grob_info> primitives);
52 TRANSLATOR_DECLARATIONS (Mensural_ligature_engraver);
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);
60 Mensural_ligature_engraver::Mensural_ligature_engraver ()
62 brew_ligature_primitive_proc =
63 Mensural_ligature::brew_ligature_primitive_proc;
67 Mensural_ligature_engraver::create_ligature_spanner ()
69 return make_spanner ("MensuralLigature", SCM_EOL);
73 Mensural_ligature_engraver::transform_heads (vector<Grob_info> primitives)
75 if (primitives.size () < 2)
77 warning (_f ("ligature with less than 2 heads -> skipping"));
81 bool at_beginning = true;
83 // needed so that we can check whether
84 // the previous note can be turned into a flexa
85 bool prev_brevis_shape = false;
87 bool prev_semibrevis = false;
88 Item *prev_primitive = NULL;
90 for (vsize i = 0, s = primitives.size (); i < s; i++)
92 Grob_info info = primitives[i];
93 Item *primitive = dynamic_cast<Item *> (info.grob ());
94 int duration_log = Note_head::get_balltype (primitive);
96 Music *nr = info.music_cause ();
99 ugh. why not simply check for pitch?
101 if (!nr->is_mus_type ("note-event"))
103 nr->origin ()->warning
104 (_f ("cannot determine pitch of ligature primitive -> skipping"));
109 int pitch = unsmob_pitch (nr->get_property ("pitch"))->steps ();
116 // we can get here after invalid input
117 nr->origin ()->warning
118 (_f ("single note ligature - skipping"));
121 prev_semibrevis = prev_brevis_shape = false;
122 prev_primitive = NULL;
126 delta_pitch = pitch - prev_pitch;
127 if (delta_pitch == 0)
129 nr->origin ()->warning
130 (_f ("prime interval within ligature -> skipping"));
132 primitive->set_property ("primitive",
133 scm_from_int (MLP_NONE));
138 if (duration_log < -3 // is this possible at all???
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));
149 // apply_transition replacement begins
150 bool general_case = true;
152 // first check special cases
157 if (duration_log == 0)
159 primitive->set_property ("primitive",
160 scm_from_int (MLP_UP | MLP_BREVIS));
161 prev_semibrevis = prev_brevis_shape = true;
162 general_case = false;
164 // b. descendens longa or brevis
166 && (unsmob_pitch (primitives[i + 1].music_cause ()
167 ->get_property ("pitch"))->steps () < pitch)
168 && duration_log > -3)
170 int left_stem = duration_log == -1 ? MLP_DOWN : 0;
172 primitive->set_property ("primitive",
173 scm_from_int (left_stem | MLP_BREVIS));
174 prev_brevis_shape = true;
175 prev_semibrevis = general_case = false;
178 // 2. initial semibrevis must be followed by another one
179 else if (prev_semibrevis)
181 prev_semibrevis = false;
182 if (duration_log == 0)
184 primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
185 general_case = false;
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));
197 // 3. semibreves are otherwise not allowed
198 else if (duration_log == 0)
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));
208 // 4. end, descendens
209 else if (i == s - 1 && delta_pitch < 0)
211 // brevis; previous note must be turned into flexa
212 if (duration_log == -1)
214 if (prev_brevis_shape)
216 prev_primitive->set_property
220 | (scm_to_int (prev_primitive->get_property ("primitive"))
222 primitive->set_property ("primitive", scm_from_int (MLP_NONE));
223 break; // no more notes, no join
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));
237 else if (duration_log == -2)
239 primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
240 general_case = false;
242 // else maxima; fall through regular case below
247 static int const shape[3] = {MLP_MAXIMA, MLP_LONGA, MLP_BREVIS};
249 primitive->set_property ("primitive",
250 scm_from_int (shape[duration_log + 3]));
251 prev_brevis_shape = duration_log == -1;
254 // join_primitives replacement
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
263 && (scm_to_int (prev_primitive->get_property ("primitive"))
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 ()
272 prev_primitive->set_property ("join-right-amount",
273 scm_from_int (delta_pitch));
274 // perhaps set add-join as well
276 at_beginning = false;
277 prev_primitive = primitive;
279 // apply_transition replacement ends
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.
298 Mensural_ligature_engraver::propagate_properties (Spanner *ligature,
299 vector<Grob_info> primitives)
302 = robust_scm2double (ligature->get_property ("thickness"), 1.4);
304 *= ligature->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
307 = Font_interface::get_default_font (ligature)->
308 find_by_name ("noteheads.s-1mensural").extent (X_AXIS).length ();
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 ();
315 flexa_width *= Staff_symbol_referencer::staff_space (ligature);
317 Real half_flexa_width = 0.5 * (flexa_width + thickness);
319 for (vsize i = 0; i < primitives.size (); i++)
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));
326 switch (output & MLP_ANY)
329 primitive->set_property ("head-width",
330 scm_from_double (half_flexa_width));
334 primitive->set_property ("head-width",
335 scm_from_double (head_width));
338 primitive->set_property ("head-width",
339 scm_from_double (maxima_head_width));
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));
348 programming_error (_f ("unexpected case fall-through"));
355 Mensural_ligature_engraver::fold_up_primitives (vector<Grob_info> primitives)
359 for (vsize i = 0; i < primitives.size (); i++)
361 Item *current = dynamic_cast<Item *> (primitives[i].grob ());
365 get_set_column (current, first->get_column ());
368 current->translate_axis (distance, X_AXIS);
371 += scm_to_double (current->get_property ("head-width"))
372 - scm_to_double (current->get_property ("thickness"));
377 Mensural_ligature_engraver::build_ligature (Spanner *ligature,
378 vector<Grob_info> primitives)
380 transform_heads (primitives);
381 propagate_properties (ligature, primitives);
382 fold_up_primitives (primitives);
385 #include "translator.icc"
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",