2 mensural-ligature-engraver.cc -- implement Mensural_ligature_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2008 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"
14 #include "note-column.hh"
15 #include "note-head.hh"
16 #include "output-def.hh"
17 #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: 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 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
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
);
50 DECLARE_TRANSLATOR_LISTENER (ligature
);
53 TRANSLATOR_DECLARATIONS (Mensural_ligature_engraver
);
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
);
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
;
75 Mensural_ligature_engraver::create_ligature_spanner ()
77 return make_spanner ("MensuralLigature", SCM_EOL
);
81 Mensural_ligature_engraver::transform_heads (vector
<Grob_info
> primitives
)
83 if (primitives
.size () < 2)
85 warning (_ ("ligature with less than 2 heads -> skipping"));
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"));
117 int pitch
= unsmob_pitch (nr
->get_property ("pitch"))->steps ();
124 // we can get here after invalid input
125 nr
->origin ()->warning
126 (_ ("single note ligature - skipping"));
129 prev_semibrevis
= prev_brevis_shape
= false;
130 prev_primitive
= NULL
;
134 delta_pitch
= pitch
- prev_pitch
;
135 if (delta_pitch
== 0)
137 nr
->origin ()->warning
138 (_ ("prime interval within ligature -> skipping"));
140 primitive
->set_property ("primitive",
141 scm_from_int (MLP_NONE
));
146 if (duration_log
< -3 // is this possible at all???
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
));
157 // apply_transition replacement begins
158 bool general_case
= true;
160 // first check special cases
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
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;
197 nr
->origin ()->warning
198 (_ ("semibrevis must be followed by another one -> skipping"));
199 primitive
->set_property ("primitive",
200 scm_from_int (MLP_NONE
));
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
));
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
228 | (scm_to_int (prev_primitive
->get_property ("primitive"))
230 primitive
->set_property ("primitive", scm_from_int (MLP_NONE
));
231 break; // no more notes, no join
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
));
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
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
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
271 && (scm_to_int (prev_primitive
->get_property ("primitive"))
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
;
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.
306 Mensural_ligature_engraver::propagate_properties (Spanner
*ligature
,
307 vector
<Grob_info
> primitives
)
310 = robust_scm2double (ligature
->get_property ("thickness"), 1.4);
312 *= ligature
->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
315 = Font_interface::get_default_font (ligature
)->
316 find_by_name ("noteheads.sM1mensural").extent (X_AXIS
).length ();
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
)
337 primitive
->set_property ("head-width",
338 scm_from_double (half_flexa_width
));
342 primitive
->set_property ("head-width",
343 scm_from_double (head_width
));
346 primitive
->set_property ("head-width",
347 scm_from_double (maxima_head_width
));
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
));
356 programming_error (_ ("unexpected case fall-through"));
363 Mensural_ligature_engraver::fold_up_primitives (vector
<Grob_info
> primitives
)
367 Real dot_shift
= 0.0;
368 for (vsize i
= 0; i
< primitives
.size (); i
++)
370 Item
*current
= dynamic_cast<Item
*> (primitives
[i
].grob ());
374 dot_shift
= 1.5 * Staff_symbol_referencer::staff_space (first
);
377 move_related_items_to_column (current
, first
->get_column (),
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
393 // FIXME: dot placement is horizontally slightly off.
394 Rhythmic_head::get_dots (current
)->translate_axis (dot_shift
, Y_AXIS
);
397 // trailing dot => move behind head
400 scm_to_double (current
->get_property ("head-width"));
401 Rhythmic_head::get_dots (current
)->
402 translate_axis (head_width
, X_AXIS
);
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
,
422 "Handle @code{Mensural_ligature_events} by glueing special"
423 " ligature heads together.",