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"
12 #include "mensural-ligature.hh"
16 #include "paper-column.hh"
17 #include "note-column.hh"
18 #include "rhythmic-head.hh"
19 #include "note-head.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "output-def.hh"
22 #include "font-interface.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, Array<Grob_info> primitives);
52 TRANSLATOR_DECLARATIONS (Mensural_ligature_engraver);
55 void transform_heads (Array<Grob_info> primitives);
56 void propagate_properties (Spanner *ligature, Array<Grob_info> primitives);
57 void fold_up_primitives (Array<Grob_info> primitives);
61 Mensural_ligature_engraver::Mensural_ligature_engraver ()
66 Mensural_ligature_engraver::create_ligature_spanner ()
68 return make_spanner ("MensuralLigature", SCM_EOL);
72 Mensural_ligature_engraver::transform_heads (Array<Grob_info> primitives)
74 if (primitives.size () < 2)
76 warning (_f ("ligature with less than 2 heads -> skipping"));
80 bool at_beginning = true;
82 // needed so that we can check whether
83 // the previous note can be turned into a flexa
84 bool prev_brevis_shape = false;
86 bool prev_semibrevis = false;
87 Item *prev_primitive = NULL;
89 for (int i = 0, s = primitives.size (); i < s; i++) {
90 Grob_info info = primitives[i];
91 Item *primitive = dynamic_cast<Item *> (info.grob_);
92 int duration_log = Note_head::get_balltype (primitive);
94 Music *nr = info.music_cause ();
97 ugh. why not simply check for pitch?
99 if (!nr->is_mus_type ("note-event"))
101 nr->origin ()->warning
102 (_f ("can not determine pitch of ligature primitive -> skipping"));
107 int pitch = unsmob_pitch (nr->get_property ("pitch"))->steps ();
114 // we can get here after invalid input
115 nr->origin ()->warning
116 (_f ("single note ligature - skipping"));
119 prev_semibrevis = prev_brevis_shape = false;
120 prev_primitive = NULL;
124 delta_pitch = pitch - prev_pitch;
125 if (delta_pitch == 0)
127 nr->origin ()->warning
128 (_f ("prime interval within ligature -> skipping"));
130 primitive->set_property ("primitive",
131 scm_int2num (MLP_NONE));
136 if (duration_log < -3 // is this possible at all???
139 nr->origin ()->warning
140 (_f ("mensural ligature: duration none of Mx, L, B, S -> skipping"));
141 primitive->set_property ("primitive",
142 scm_int2num (MLP_NONE));
147 // apply_transition replacement begins
148 bool general_case = true;
150 // first check special cases
155 if (duration_log == 0)
157 primitive->set_property ("primitive",
158 scm_int2num (MLP_UP | MLP_BREVIS));
159 prev_semibrevis = prev_brevis_shape = true;
160 general_case = false;
162 // b. descendens longa or brevis
164 && (unsmob_pitch (primitives[i + 1].music_cause ()
165 ->get_property ("pitch"))->steps () < pitch)
166 && duration_log > -3)
168 int left_stem = duration_log == -1 ? MLP_DOWN : 0;
170 primitive->set_property ("primitive",
171 scm_int2num (left_stem | MLP_BREVIS));
172 prev_brevis_shape = true;
173 prev_semibrevis = general_case = false;
176 // 2. initial semibrevis must be followed by another one
177 else if (prev_semibrevis)
179 prev_semibrevis = false;
180 if (duration_log == 0)
182 primitive->set_property ("primitive", scm_int2num(MLP_BREVIS));
183 general_case = false;
187 nr->origin ()->warning
188 (_f ("semibrevis must be followed by another one -> skipping"));
189 primitive->set_property ("primitive",
190 scm_int2num (MLP_NONE));
195 // 3. semibreves are otherwise not allowed
196 else if (duration_log == 0)
198 nr->origin ()->warning
199 (_f ("semibreves can only appear at the beginning of a ligature,\n"
200 "and there may be only zero or two of them"));
201 primitive->set_property ("primitive",
202 scm_int2num (MLP_NONE));
206 // 4. end, descendens
207 else if (i == s - 1 && delta_pitch < 0)
209 // brevis; previous note must be turned into flexa
210 if (duration_log == -1)
212 if (prev_brevis_shape)
214 prev_primitive->set_property
218 | (scm_to_int (prev_primitive->get_property ("primitive"))
220 primitive->set_property ("primitive", scm_int2num (MLP_NONE));
221 break; // no more notes, no join
225 nr->origin ()->warning
226 (_f ("invalid ligatura ending:\n"
227 "when the last note is a descending brevis,\n"
228 "the penultimate note must be another one,\n"
229 "or the ligatura must be LB or SSB"));
230 primitive->set_property ("primitive", scm_int2num (MLP_NONE));
235 else if (duration_log == -2)
237 primitive->set_property ("primitive", scm_int2num (MLP_BREVIS));
238 general_case = false;
240 // else maxima; fall through regular case below
245 static int const shape[3] = {MLP_MAXIMA, MLP_LONGA, MLP_BREVIS};
247 primitive->set_property ("primitive",
248 scm_int2num (shape[duration_log + 3]));
249 prev_brevis_shape = duration_log == -1;
252 // join_primitives replacement
256 if the previous note is longa-shaped and this note is lower,
257 then the joining line may hide the stem, so it is made longer
258 to serve as stem as well
261 && (scm_to_int (prev_primitive->get_property ("primitive"))
265 // instead of number 6
266 // the legth of the longa stem should be queried something like
267 // Font_interface::get_default_font (ligature)->find_by_name
268 // ("noteheads.s-2mensural").extent (Y_AXIS).length ()
270 prev_primitive->set_property ("join-right-amount",
271 scm_int2num (delta_pitch));
272 // perhaps set add-join as well
274 at_beginning = false;
275 prev_primitive = primitive;
277 // apply_transition replacement ends
282 * A MensuralLigature grob consists of a bunch of NoteHead grobs that
283 * are glued together. It (a) does not make sense to change
284 * properties like thickness or flexa-width from one head to the next
285 * within a ligature (this would totally screw up alignment), and (b)
286 * some of these properties (like flexa-width) are specific to
287 * e.g. the MensuralLigature (as in contrast to e.g. LigatureBracket),
288 * and therefore should not be handled in the NoteHead code (which is
289 * also used by LigatureBracket). Therefore, we let the user control
290 * these properties via the concrete Ligature grob (like
291 * MensuralLigature) and then copy these properties as necessary to
292 * each of the NoteHead grobs. This is what
293 * propagate_properties () does.
296 Mensural_ligature_engraver::propagate_properties (Spanner *ligature,
297 Array<Grob_info> primitives)
300 robust_scm2double (ligature->get_property ("thickness"), 1.4);
302 ligature->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
305 Font_interface::get_default_font (ligature)->
306 find_by_name ("noteheads.s-1mensural").extent (X_AXIS).length ();
308 robust_scm2double (ligature->get_property ("flexa-width"), 2);
309 Real maxima_head_width =
310 Font_interface::get_default_font (ligature)->
311 find_by_name ("noteheads.s-1neomensural").extent (X_AXIS).length ();
313 flexa_width *= Staff_symbol_referencer::staff_space (ligature);
315 Real half_flexa_width = 0.5 * (flexa_width + thickness);
317 for (int i = 0; i < primitives.size (); i++)
319 Item *primitive = dynamic_cast<Item*> (primitives[i].grob_);
320 int output = scm_to_int (primitive->get_property ("primitive"));
321 primitive->set_property ("thickness",
322 scm_make_real (thickness));
324 switch (output & MLP_ANY) {
326 primitive->set_property ("head-width",
327 scm_make_real (half_flexa_width));
331 primitive->set_property ("head-width",
332 scm_make_real (head_width));
335 primitive->set_property ("head-width",
336 scm_make_real (maxima_head_width));
339 primitive->set_property ("head-width",
340 scm_make_real (half_flexa_width));
341 primitive->set_property ("flexa-width",
342 scm_make_real (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_);
364 get_set_column (current, first->get_column ());
368 current->translate_axis (distance, X_AXIS);
372 scm_to_double (current->get_property ("head-width")) -
373 scm_to_double (current->get_property ("thickness"));
378 Mensural_ligature_engraver::build_ligature (Spanner *ligature,
379 Array<Grob_info> primitives)
381 transform_heads (primitives);
382 propagate_properties (ligature, primitives);
383 fold_up_primitives (primitives);
386 ADD_TRANSLATOR (Mensural_ligature_engraver,
387 /* descr */ "Handles Mensural_ligature_events by glueing special ligature heads together.",
388 /* creats*/ "MensuralLigature",
389 /* accepts */ "ligature-event",
390 /* acks */ "note-head-interface rest-interface",