Nitpick: ly:spanner-bound grob name slur -> spanner.
[lilypond.git] / lily / mensural-ligature-engraver.cc
blob249506ed34fb26ba16ce8b8c964e530a56a54100
1 /*
2 mensural-ligature-engraver.cc -- implement Mensural_ligature_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2009 Juergen Reuter <reuter@ipd.uka.de>,
7 Pal Benko <benkop@freestart.hu>
8 */
10 #include "coherent-ligature-engraver.hh"
11 #include "font-interface.hh"
12 #include "international.hh"
13 #include "mensural-ligature.hh"
14 #include "note-column.hh"
15 #include "note-head.hh"
16 #include "output-def.hh"
17 #include "paper-column.hh"
18 #include "pitch.hh"
19 #include "rhythmic-head.hh"
20 #include "spanner.hh"
21 #include "staff-symbol-referencer.hh"
22 #include "stream-event.hh"
23 #include "warn.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: do something with multiple voices within a ligature. See
35 * for example:
36 * Ockeghem: Missa Ecce ancilla domini, bassus part, end of Christe.
38 * TODO: enhance robustness: in case of an invalid 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.
44 class Mensural_ligature_engraver : public Coherent_ligature_engraver
47 protected:
48 virtual Spanner *create_ligature_spanner ();
49 virtual void build_ligature (Spanner *ligature, vector<Grob_info> primitives);
50 DECLARE_TRANSLATOR_LISTENER (ligature);
52 public:
53 TRANSLATOR_DECLARATIONS (Mensural_ligature_engraver);
55 private:
56 void transform_heads (vector<Grob_info> primitives);
57 void propagate_properties (Spanner *ligature, vector<Grob_info> primitives);
58 void fold_up_primitives (vector<Grob_info> primitives);
61 IMPLEMENT_TRANSLATOR_LISTENER (Mensural_ligature_engraver, ligature);
62 void
63 Mensural_ligature_engraver::listen_ligature (Stream_event *ev)
65 Ligature_engraver::listen_ligature (ev);
68 Mensural_ligature_engraver::Mensural_ligature_engraver ()
70 brew_ligature_primitive_proc =
71 Mensural_ligature::brew_ligature_primitive_proc;
74 Spanner *
75 Mensural_ligature_engraver::create_ligature_spanner ()
77 return make_spanner ("MensuralLigature", SCM_EOL);
80 void
81 Mensural_ligature_engraver::transform_heads (vector<Grob_info> primitives)
83 if (primitives.size () < 2)
85 warning (_ ("ligature with less than 2 heads -> skipping"));
86 return;
88 int prev_pitch = 0;
89 bool at_beginning = true;
91 // needed so that we can check whether
92 // the previous note can be turned into a flexa
93 bool prev_brevis_shape = false;
95 bool prev_semibrevis = false;
96 Item *prev_primitive = NULL;
98 for (vsize i = 0, s = primitives.size (); i < s; i++)
100 Grob_info info = primitives[i];
101 Item *primitive = dynamic_cast<Item *> (info.grob ());
102 int duration_log = Rhythmic_head::duration_log (primitive);
104 Stream_event *nr = info.event_cause ();
107 ugh. why not simply check for pitch?
109 if (!nr->in_event_class ("note-event"))
111 nr->origin ()->warning
112 (_ ("cannot determine pitch of ligature primitive -> skipping"));
113 at_beginning = true;
114 continue;
117 int pitch = unsmob_pitch (nr->get_property ("pitch"))->steps ();
118 int delta_pitch = 0;
120 if (at_beginning)
122 if (i == s - 1)
124 // we can get here after invalid input
125 nr->origin ()->warning
126 (_ ("single note ligature - skipping"));
127 break;
129 prev_semibrevis = prev_brevis_shape = false;
130 prev_primitive = NULL;
132 else
134 delta_pitch = pitch - prev_pitch;
135 if (delta_pitch == 0)
137 nr->origin ()->warning
138 (_ ("prime interval within ligature -> skipping"));
139 at_beginning = true;
140 primitive->set_property ("primitive",
141 scm_from_int (MLP_NONE));
142 continue;
146 if (duration_log < -3 // is this possible at all???
147 || duration_log > 0)
149 nr->origin ()->warning
150 (_ ("mensural ligature: duration none of Mx, L, B, S -> skipping"));
151 primitive->set_property ("primitive",
152 scm_from_int (MLP_NONE));
153 at_beginning = true;
154 continue;
157 // apply_transition replacement begins
158 bool general_case = true;
160 // first check special cases
161 // 1. beginning
162 if (at_beginning)
164 // a. semibreves
165 if (duration_log == 0)
167 primitive->set_property ("primitive",
168 scm_from_int (MLP_UP | MLP_BREVIS));
169 prev_semibrevis = prev_brevis_shape = true;
170 general_case = false;
172 // b. descendens longa or brevis
173 else if (i < s - 1
174 && (unsmob_pitch (primitives[i + 1].event_cause ()
175 ->get_property ("pitch"))->steps () < pitch)
176 && duration_log > -3)
178 int left_stem = duration_log == -1 ? MLP_DOWN : 0;
180 primitive->set_property ("primitive",
181 scm_from_int (left_stem | MLP_BREVIS));
182 prev_brevis_shape = true;
183 prev_semibrevis = general_case = false;
186 // 2. initial semibrevis must be followed by another one
187 else if (prev_semibrevis)
189 prev_semibrevis = false;
190 if (duration_log == 0)
192 primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
193 general_case = false;
195 else
197 nr->origin ()->warning
198 (_ ("semibrevis must be followed by another one -> skipping"));
199 primitive->set_property ("primitive",
200 scm_from_int (MLP_NONE));
201 at_beginning = true;
202 continue;
205 // 3. semibreves are otherwise not allowed
206 else if (duration_log == 0)
208 nr->origin ()->warning
209 (_ ("semibreves can only appear at the beginning of a ligature,\n"
210 "and there may be only zero or two of them"));
211 primitive->set_property ("primitive",
212 scm_from_int (MLP_NONE));
213 at_beginning = true;
214 continue;
216 // 4. end, descendens
217 else if (i == s - 1 && delta_pitch < 0)
219 // brevis; previous note must be turned into flexa
220 if (duration_log == -1)
222 if (prev_brevis_shape)
224 prev_primitive->set_property
225 ("primitive",
226 scm_from_int
227 (MLP_FLEXA
228 | (scm_to_int (prev_primitive->get_property ("primitive"))
229 & MLP_DOWN)));
230 primitive->set_property ("primitive", scm_from_int (MLP_NONE));
231 break; // no more notes, no join
233 else
235 nr->origin ()->warning
236 (_ ("invalid ligatura ending:\n"
237 "when the last note is a descending brevis,\n"
238 "the penultimate note must be another one,\n"
239 "or the ligatura must be LB or SSB"));
240 primitive->set_property ("primitive", scm_from_int (MLP_NONE));
241 break;
244 // longa
245 else if (duration_log == -2)
247 primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
248 general_case = false;
250 // else maxima; fall through regular case below
253 if (general_case)
255 static int const shape[3] = {MLP_MAXIMA, MLP_LONGA, MLP_BREVIS};
257 primitive->set_property ("primitive",
258 scm_from_int (shape[duration_log + 3]));
259 prev_brevis_shape = duration_log == -1;
262 // join_primitives replacement
263 if (!at_beginning)
266 if the previous note is longa-shaped and this note is lower,
267 then the joining line may hide the stem, so it is made longer
268 to serve as stem as well
270 if (delta_pitch < 0
271 && (scm_to_int (prev_primitive->get_property ("primitive"))
272 & MLP_LONGA))
274 delta_pitch -= 6;
275 // instead of number 6
276 // the legth of the longa stem should be queried something like
277 // Font_interface::get_default_font (ligature)->find_by_name
278 // ("noteheads.sM2mensural").extent (Y_AXIS).length ()
280 prev_primitive->set_property ("join-right-amount",
281 scm_from_int (delta_pitch));
282 // perhaps set add-join as well
284 at_beginning = false;
285 prev_primitive = primitive;
286 prev_pitch = pitch;
287 // apply_transition replacement ends
292 * A MensuralLigature grob consists of a bunch of NoteHead grobs that
293 * are glued together. It (a) does not make sense to change
294 * properties like thickness or flexa-width from one head to the next
295 * within a ligature (this would totally screw up alignment), and (b)
296 * some of these properties (like flexa-width) are specific to
297 * e.g. the MensuralLigature (as in contrast to e.g. LigatureBracket),
298 * and therefore should not be handled in the NoteHead code (which is
299 * also used by LigatureBracket). Therefore, we let the user control
300 * these properties via the concrete Ligature grob (like
301 * MensuralLigature) and then copy these properties as necessary to
302 * each of the NoteHead grobs. This is what
303 * propagate_properties () does.
305 void
306 Mensural_ligature_engraver::propagate_properties (Spanner *ligature,
307 vector<Grob_info> primitives)
309 Real thickness
310 = robust_scm2double (ligature->get_property ("thickness"), 1.4);
311 thickness
312 *= ligature->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
314 Real head_width
315 = Font_interface::get_default_font (ligature)->
316 find_by_name ("noteheads.sM1mensural").extent (X_AXIS).length ();
317 Real flexa_width
318 = robust_scm2double (ligature->get_property ("flexa-width"), 2);
319 Real maxima_head_width
320 = Font_interface::get_default_font (ligature)->
321 find_by_name ("noteheads.sM1neomensural").extent (X_AXIS).length ();
323 flexa_width *= Staff_symbol_referencer::staff_space (ligature);
325 Real half_flexa_width = 0.5 * (flexa_width + thickness);
327 for (vsize i = 0; i < primitives.size (); i++)
329 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
330 int output = scm_to_int (primitive->get_property ("primitive"));
331 primitive->set_property ("thickness",
332 scm_from_double (thickness));
334 switch (output & MLP_ANY)
336 case MLP_NONE:
337 primitive->set_property ("head-width",
338 scm_from_double (half_flexa_width));
339 break;
340 case MLP_BREVIS:
341 case MLP_LONGA:
342 primitive->set_property ("head-width",
343 scm_from_double (head_width));
344 break;
345 case MLP_MAXIMA:
346 primitive->set_property ("head-width",
347 scm_from_double (maxima_head_width));
348 break;
349 case MLP_FLEXA:
350 primitive->set_property ("head-width",
351 scm_from_double (half_flexa_width));
352 primitive->set_property ("flexa-width",
353 scm_from_double (flexa_width));
354 break;
355 default:
356 programming_error (_ ("unexpected case fall-through"));
357 break;
362 void
363 Mensural_ligature_engraver::fold_up_primitives (vector<Grob_info> primitives)
365 Item *first = 0;
366 Real distance = 0.0;
367 Real dot_shift = 0.0;
368 for (vsize i = 0; i < primitives.size (); i++)
370 Item *current = dynamic_cast<Item *> (primitives[i].grob ());
371 if (i == 0)
373 first = current;
374 dot_shift = 1.5 * Staff_symbol_referencer::staff_space (first);
377 move_related_items_to_column (current, first->get_column (),
378 distance);
380 distance
381 += scm_to_double (current->get_property ("head-width"))
382 - scm_to_double (current->get_property ("thickness"));
384 if (Rhythmic_head::dot_count (current) > 0)
385 // Move dots above/behind the ligature.
387 if (i + 1 < primitives.size ())
388 // dot in the midst => move above head
390 // FIXME: Amount of vertical dot-shift should depend on
391 // pitch.
393 // FIXME: dot placement is horizontally slightly off.
394 Rhythmic_head::get_dots (current)->translate_axis (dot_shift, Y_AXIS);
396 else
397 // trailing dot => move behind head
399 double head_width =
400 scm_to_double (current->get_property ("head-width"));
401 Rhythmic_head::get_dots (current)->
402 translate_axis (head_width, X_AXIS);
408 void
409 Mensural_ligature_engraver::build_ligature (Spanner *ligature,
410 vector<Grob_info> primitives)
412 transform_heads (primitives);
413 propagate_properties (ligature, primitives);
414 fold_up_primitives (primitives);
417 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, rest);
418 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, note_head);
420 ADD_TRANSLATOR (Mensural_ligature_engraver,
421 /* doc */
422 "Handle @code{Mensural_ligature_events} by glueing special"
423 " ligature heads together.",
425 /* create */
426 "MensuralLigature ",
428 /* read */
431 /* write */