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"
22 #include "stream-event.hh"
25 #include "translator.icc"
28 * TODO: accidentals are aligned with the first note;
29 * they must appear ahead.
31 * TODO: prohibit ligatures having notes differing only in accidentals
32 * (like \[ a\breve g as \])
34 * TODO: dotted heads: avoid next note colliding with the dot, e.g. by
35 * putting it *above* (rather than after) the affected ligature head.
37 * TODO: do something with multiple voices within a ligature. See
39 * Ockeghem: Missa Ecce ancilla domini, bassus part, end of Christe.
41 * TODO: enhance robustness: in case of an illegal ligature (e.g. the
42 * input specifies a ligature that contains a minima), automatically
43 * break the ligature into smaller, valid pieces. Such a piece may be
47 class Mensural_ligature_engraver : public Coherent_ligature_engraver
51 virtual Spanner *create_ligature_spanner ();
52 virtual void build_ligature (Spanner *ligature, vector<Grob_info> primitives);
53 DECLARE_TRANSLATOR_LISTENER (ligature);
56 TRANSLATOR_DECLARATIONS (Mensural_ligature_engraver);
59 void transform_heads (vector<Grob_info> primitives);
60 void propagate_properties (Spanner *ligature, vector<Grob_info> primitives);
61 void fold_up_primitives (vector<Grob_info> primitives);
64 IMPLEMENT_TRANSLATOR_LISTENER (Mensural_ligature_engraver, ligature);
66 Mensural_ligature_engraver::listen_ligature (Stream_event *ev)
68 Ligature_engraver::listen_ligature (ev);
71 Mensural_ligature_engraver::Mensural_ligature_engraver ()
73 brew_ligature_primitive_proc =
74 Mensural_ligature::brew_ligature_primitive_proc;
78 Mensural_ligature_engraver::create_ligature_spanner ()
80 return make_spanner ("MensuralLigature", SCM_EOL);
84 Mensural_ligature_engraver::transform_heads (vector<Grob_info> primitives)
86 if (primitives.size () < 2)
88 warning (_f ("ligature with less than 2 heads -> skipping"));
92 bool at_beginning = true;
94 // needed so that we can check whether
95 // the previous note can be turned into a flexa
96 bool prev_brevis_shape = false;
98 bool prev_semibrevis = false;
99 Item *prev_primitive = NULL;
101 for (vsize i = 0, s = primitives.size (); i < s; i++)
103 Grob_info info = primitives[i];
104 Item *primitive = dynamic_cast<Item *> (info.grob ());
105 int duration_log = Note_head::get_balltype (primitive);
107 Music *nr = info.music_cause ();
110 ugh. why not simply check for pitch?
112 if (!nr->is_mus_type ("note-event"))
114 nr->origin ()->warning
115 (_f ("cannot determine pitch of ligature primitive -> skipping"));
120 int pitch = unsmob_pitch (nr->get_property ("pitch"))->steps ();
127 // we can get here after invalid input
128 nr->origin ()->warning
129 (_f ("single note ligature - skipping"));
132 prev_semibrevis = prev_brevis_shape = false;
133 prev_primitive = NULL;
137 delta_pitch = pitch - prev_pitch;
138 if (delta_pitch == 0)
140 nr->origin ()->warning
141 (_f ("prime interval within ligature -> skipping"));
143 primitive->set_property ("primitive",
144 scm_from_int (MLP_NONE));
149 if (duration_log < -3 // is this possible at all???
152 nr->origin ()->warning
153 (_f ("mensural ligature: duration none of Mx, L, B, S -> skipping"));
154 primitive->set_property ("primitive",
155 scm_from_int (MLP_NONE));
160 // apply_transition replacement begins
161 bool general_case = true;
163 // first check special cases
168 if (duration_log == 0)
170 primitive->set_property ("primitive",
171 scm_from_int (MLP_UP | MLP_BREVIS));
172 prev_semibrevis = prev_brevis_shape = true;
173 general_case = false;
175 // b. descendens longa or brevis
177 && (unsmob_pitch (primitives[i + 1].music_cause ()
178 ->get_property ("pitch"))->steps () < pitch)
179 && duration_log > -3)
181 int left_stem = duration_log == -1 ? MLP_DOWN : 0;
183 primitive->set_property ("primitive",
184 scm_from_int (left_stem | MLP_BREVIS));
185 prev_brevis_shape = true;
186 prev_semibrevis = general_case = false;
189 // 2. initial semibrevis must be followed by another one
190 else if (prev_semibrevis)
192 prev_semibrevis = false;
193 if (duration_log == 0)
195 primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
196 general_case = false;
200 nr->origin ()->warning
201 (_f ("semibrevis must be followed by another one -> skipping"));
202 primitive->set_property ("primitive",
203 scm_from_int (MLP_NONE));
208 // 3. semibreves are otherwise not allowed
209 else if (duration_log == 0)
211 nr->origin ()->warning
212 (_f ("semibreves can only appear at the beginning of a ligature,\n"
213 "and there may be only zero or two of them"));
214 primitive->set_property ("primitive",
215 scm_from_int (MLP_NONE));
219 // 4. end, descendens
220 else if (i == s - 1 && delta_pitch < 0)
222 // brevis; previous note must be turned into flexa
223 if (duration_log == -1)
225 if (prev_brevis_shape)
227 prev_primitive->set_property
231 | (scm_to_int (prev_primitive->get_property ("primitive"))
233 primitive->set_property ("primitive", scm_from_int (MLP_NONE));
234 break; // no more notes, no join
238 nr->origin ()->warning
239 (_f ("invalid ligatura ending:\n"
240 "when the last note is a descending brevis,\n"
241 "the penultimate note must be another one,\n"
242 "or the ligatura must be LB or SSB"));
243 primitive->set_property ("primitive", scm_from_int (MLP_NONE));
248 else if (duration_log == -2)
250 primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
251 general_case = false;
253 // else maxima; fall through regular case below
258 static int const shape[3] = {MLP_MAXIMA, MLP_LONGA, MLP_BREVIS};
260 primitive->set_property ("primitive",
261 scm_from_int (shape[duration_log + 3]));
262 prev_brevis_shape = duration_log == -1;
265 // join_primitives replacement
269 if the previous note is longa-shaped and this note is lower,
270 then the joining line may hide the stem, so it is made longer
271 to serve as stem as well
274 && (scm_to_int (prev_primitive->get_property ("primitive"))
278 // instead of number 6
279 // the legth of the longa stem should be queried something like
280 // Font_interface::get_default_font (ligature)->find_by_name
281 // ("noteheads.s-2mensural").extent (Y_AXIS).length ()
283 prev_primitive->set_property ("join-right-amount",
284 scm_from_int (delta_pitch));
285 // perhaps set add-join as well
287 at_beginning = false;
288 prev_primitive = primitive;
290 // apply_transition replacement ends
295 * A MensuralLigature grob consists of a bunch of NoteHead grobs that
296 * are glued together. It (a) does not make sense to change
297 * properties like thickness or flexa-width from one head to the next
298 * within a ligature (this would totally screw up alignment), and (b)
299 * some of these properties (like flexa-width) are specific to
300 * e.g. the MensuralLigature (as in contrast to e.g. LigatureBracket),
301 * and therefore should not be handled in the NoteHead code (which is
302 * also used by LigatureBracket). Therefore, we let the user control
303 * these properties via the concrete Ligature grob (like
304 * MensuralLigature) and then copy these properties as necessary to
305 * each of the NoteHead grobs. This is what
306 * propagate_properties () does.
309 Mensural_ligature_engraver::propagate_properties (Spanner *ligature,
310 vector<Grob_info> primitives)
313 = robust_scm2double (ligature->get_property ("thickness"), 1.4);
315 *= ligature->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
318 = Font_interface::get_default_font (ligature)->
319 find_by_name ("noteheads.s-1mensural").extent (X_AXIS).length ();
321 = robust_scm2double (ligature->get_property ("flexa-width"), 2);
322 Real maxima_head_width
323 = Font_interface::get_default_font (ligature)->
324 find_by_name ("noteheads.s-1neomensural").extent (X_AXIS).length ();
326 flexa_width *= Staff_symbol_referencer::staff_space (ligature);
328 Real half_flexa_width = 0.5 * (flexa_width + thickness);
330 for (vsize i = 0; i < primitives.size (); i++)
332 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
333 int output = scm_to_int (primitive->get_property ("primitive"));
334 primitive->set_property ("thickness",
335 scm_from_double (thickness));
337 switch (output & MLP_ANY)
340 primitive->set_property ("head-width",
341 scm_from_double (half_flexa_width));
345 primitive->set_property ("head-width",
346 scm_from_double (head_width));
349 primitive->set_property ("head-width",
350 scm_from_double (maxima_head_width));
353 primitive->set_property ("head-width",
354 scm_from_double (half_flexa_width));
355 primitive->set_property ("flexa-width",
356 scm_from_double (flexa_width));
359 programming_error (_f ("unexpected case fall-through"));
366 Mensural_ligature_engraver::fold_up_primitives (vector<Grob_info> primitives)
370 for (vsize i = 0; i < primitives.size (); i++)
372 Item *current = dynamic_cast<Item *> (primitives[i].grob ());
376 get_set_column (current, first->get_column ());
379 current->translate_axis (distance, X_AXIS);
382 += scm_to_double (current->get_property ("head-width"))
383 - scm_to_double (current->get_property ("thickness"));
388 Mensural_ligature_engraver::build_ligature (Spanner *ligature,
389 vector<Grob_info> primitives)
391 transform_heads (primitives);
392 propagate_properties (ligature, primitives);
393 fold_up_primitives (primitives);
396 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, rest);
397 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, note_head);
398 ADD_TRANSLATOR (Mensural_ligature_engraver,
399 /* doc */ "Handles Mensural_ligature_events by glueing special ligature heads together.",
400 /* create */ "MensuralLigature",
401 /* accept */ "ligature-event",