* The grand 2005-2006 replace.
[lilypond/patrick.git] / lily / mensural-ligature-engraver.cc
blob79b6ccdf2b474cd80b8f07d0b144842060526263
1 /*
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>
8 */
10 #include "coherent-ligature-engraver.hh"
11 #include "mensural-ligature.hh"
12 #include "music.hh"
13 #include "warn.hh"
14 #include "spanner.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
34 * for example:
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
40 * a single note.
43 class Mensural_ligature_engraver : public Coherent_ligature_engraver
46 protected:
47 virtual Spanner *create_ligature_spanner ();
48 virtual void build_ligature (Spanner *ligature, Array<Grob_info> primitives);
50 public:
51 TRANSLATOR_DECLARATIONS (Mensural_ligature_engraver);
53 private:
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 ()
61 brew_ligature_primitive_proc =
62 Mensural_ligature::brew_ligature_primitive_proc;
65 Spanner *
66 Mensural_ligature_engraver::create_ligature_spanner ()
68 return make_spanner ("MensuralLigature", SCM_EOL);
71 void
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"));
77 return;
79 int prev_pitch = 0;
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++)
91 Grob_info info = primitives[i];
92 Item *primitive = dynamic_cast<Item *> (info.grob ());
93 int duration_log = Note_head::get_balltype (primitive);
95 Music *nr = info.music_cause ();
98 ugh. why not simply check for pitch?
100 if (!nr->is_mus_type ("note-event"))
102 nr->origin ()->warning
103 (_f ("cannot determine pitch of ligature primitive -> skipping"));
104 at_beginning = true;
105 continue;
108 int pitch = unsmob_pitch (nr->get_property ("pitch"))->steps ();
109 int delta_pitch = 0;
111 if (at_beginning)
113 if (i == s - 1)
115 // we can get here after invalid input
116 nr->origin ()->warning
117 (_f ("single note ligature - skipping"));
118 break;
120 prev_semibrevis = prev_brevis_shape = false;
121 prev_primitive = NULL;
123 else
125 delta_pitch = pitch - prev_pitch;
126 if (delta_pitch == 0)
128 nr->origin ()->warning
129 (_f ("prime interval within ligature -> skipping"));
130 at_beginning = true;
131 primitive->set_property ("primitive",
132 scm_from_int (MLP_NONE));
133 continue;
137 if (duration_log < -3 // is this possible at all???
138 || duration_log > 0)
140 nr->origin ()->warning
141 (_f ("mensural ligature: duration none of Mx, L, B, S -> skipping"));
142 primitive->set_property ("primitive",
143 scm_from_int (MLP_NONE));
144 at_beginning = true;
145 continue;
148 // apply_transition replacement begins
149 bool general_case = true;
151 // first check special cases
152 // 1. beginning
153 if (at_beginning)
155 // a. semibreves
156 if (duration_log == 0)
158 primitive->set_property ("primitive",
159 scm_from_int (MLP_UP | MLP_BREVIS));
160 prev_semibrevis = prev_brevis_shape = true;
161 general_case = false;
163 // b. descendens longa or brevis
164 else if (i < s - 1
165 && (unsmob_pitch (primitives[i + 1].music_cause ()
166 ->get_property ("pitch"))->steps () < pitch)
167 && duration_log > -3)
169 int left_stem = duration_log == -1 ? MLP_DOWN : 0;
171 primitive->set_property ("primitive",
172 scm_from_int (left_stem | MLP_BREVIS));
173 prev_brevis_shape = true;
174 prev_semibrevis = general_case = false;
177 // 2. initial semibrevis must be followed by another one
178 else if (prev_semibrevis)
180 prev_semibrevis = false;
181 if (duration_log == 0)
183 primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
184 general_case = false;
186 else
188 nr->origin ()->warning
189 (_f ("semibrevis must be followed by another one -> skipping"));
190 primitive->set_property ("primitive",
191 scm_from_int (MLP_NONE));
192 at_beginning = true;
193 continue;
196 // 3. semibreves are otherwise not allowed
197 else if (duration_log == 0)
199 nr->origin ()->warning
200 (_f ("semibreves can only appear at the beginning of a ligature,\n"
201 "and there may be only zero or two of them"));
202 primitive->set_property ("primitive",
203 scm_from_int (MLP_NONE));
204 at_beginning = true;
205 continue;
207 // 4. end, descendens
208 else if (i == s - 1 && delta_pitch < 0)
210 // brevis; previous note must be turned into flexa
211 if (duration_log == -1)
213 if (prev_brevis_shape)
215 prev_primitive->set_property
216 ("primitive",
217 scm_from_int
218 (MLP_FLEXA
219 | (scm_to_int (prev_primitive->get_property ("primitive"))
220 & MLP_DOWN)));
221 primitive->set_property ("primitive", scm_from_int (MLP_NONE));
222 break; // no more notes, no join
224 else
226 nr->origin ()->warning
227 (_f ("invalid ligatura ending:\n"
228 "when the last note is a descending brevis,\n"
229 "the penultimate note must be another one,\n"
230 "or the ligatura must be LB or SSB"));
231 primitive->set_property ("primitive", scm_from_int (MLP_NONE));
232 break;
235 // longa
236 else if (duration_log == -2)
238 primitive->set_property ("primitive", scm_from_int (MLP_BREVIS));
239 general_case = false;
241 // else maxima; fall through regular case below
244 if (general_case)
246 static int const shape[3] = {MLP_MAXIMA, MLP_LONGA, MLP_BREVIS};
248 primitive->set_property ("primitive",
249 scm_from_int (shape[duration_log + 3]));
250 prev_brevis_shape = duration_log == -1;
253 // join_primitives replacement
254 if (!at_beginning)
257 if the previous note is longa-shaped and this note is lower,
258 then the joining line may hide the stem, so it is made longer
259 to serve as stem as well
261 if (delta_pitch < 0
262 && (scm_to_int (prev_primitive->get_property ("primitive"))
263 & MLP_LONGA))
265 delta_pitch -= 6;
266 // instead of number 6
267 // the legth of the longa stem should be queried something like
268 // Font_interface::get_default_font (ligature)->find_by_name
269 // ("noteheads.s-2mensural").extent (Y_AXIS).length ()
271 prev_primitive->set_property ("join-right-amount",
272 scm_from_int (delta_pitch));
273 // perhaps set add-join as well
275 at_beginning = false;
276 prev_primitive = primitive;
277 prev_pitch = pitch;
278 // apply_transition replacement ends
283 * A MensuralLigature grob consists of a bunch of NoteHead grobs that
284 * are glued together. It (a) does not make sense to change
285 * properties like thickness or flexa-width from one head to the next
286 * within a ligature (this would totally screw up alignment), and (b)
287 * some of these properties (like flexa-width) are specific to
288 * e.g. the MensuralLigature (as in contrast to e.g. LigatureBracket),
289 * and therefore should not be handled in the NoteHead code (which is
290 * also used by LigatureBracket). Therefore, we let the user control
291 * these properties via the concrete Ligature grob (like
292 * MensuralLigature) and then copy these properties as necessary to
293 * each of the NoteHead grobs. This is what
294 * propagate_properties () does.
296 void
297 Mensural_ligature_engraver::propagate_properties (Spanner *ligature,
298 Array<Grob_info> primitives)
300 Real thickness
301 = robust_scm2double (ligature->get_property ("thickness"), 1.4);
302 thickness
303 *= ligature->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
305 Real head_width
306 = Font_interface::get_default_font (ligature)->
307 find_by_name ("noteheads.s-1mensural").extent (X_AXIS).length ();
308 Real flexa_width
309 = robust_scm2double (ligature->get_property ("flexa-width"), 2);
310 Real maxima_head_width
311 = Font_interface::get_default_font (ligature)->
312 find_by_name ("noteheads.s-1neomensural").extent (X_AXIS).length ();
314 flexa_width *= Staff_symbol_referencer::staff_space (ligature);
316 Real half_flexa_width = 0.5 * (flexa_width + thickness);
318 for (int i = 0; i < primitives.size (); i++)
320 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
321 int output = scm_to_int (primitive->get_property ("primitive"));
322 primitive->set_property ("thickness",
323 scm_from_double (thickness));
325 switch (output & MLP_ANY)
327 case MLP_NONE:
328 primitive->set_property ("head-width",
329 scm_from_double (half_flexa_width));
330 break;
331 case MLP_BREVIS:
332 case MLP_LONGA:
333 primitive->set_property ("head-width",
334 scm_from_double (head_width));
335 break;
336 case MLP_MAXIMA:
337 primitive->set_property ("head-width",
338 scm_from_double (maxima_head_width));
339 break;
340 case MLP_FLEXA:
341 primitive->set_property ("head-width",
342 scm_from_double (half_flexa_width));
343 primitive->set_property ("flexa-width",
344 scm_from_double (flexa_width));
345 break;
346 default:
347 programming_error (_f ("unexpected case fall-through"));
348 break;
353 void
354 Mensural_ligature_engraver::fold_up_primitives (Array<Grob_info> primitives)
356 Item *first = 0;
357 Real distance = 0;
358 for (int i = 0; i < primitives.size (); i++)
360 Item *current = dynamic_cast<Item *> (primitives[i].grob ());
361 if (i == 0)
362 first = current;
364 get_set_column (current, first->get_column ());
366 if (i > 0)
367 current->translate_axis (distance, X_AXIS);
369 distance
370 += scm_to_double (current->get_property ("head-width"))
371 - scm_to_double (current->get_property ("thickness"));
375 void
376 Mensural_ligature_engraver::build_ligature (Spanner *ligature,
377 Array<Grob_info> primitives)
379 transform_heads (primitives);
380 propagate_properties (ligature, primitives);
381 fold_up_primitives (primitives);
384 #include "translator.icc"
386 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, rest);
387 ADD_ACKNOWLEDGER (Mensural_ligature_engraver, note_head);
388 ADD_TRANSLATOR (Mensural_ligature_engraver,
389 /* doc */ "Handles Mensural_ligature_events by glueing special ligature heads together.",
390 /* create */ "MensuralLigature",
391 /* accept */ "ligature-event",
392 /* read */ "",
393 /* write */ "");