2 vaticana-ligature-engraver.cc -- implement Vaticana_ligature_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 2003--2007 Juergen Reuter <reuter@ipd.uka.de>
9 #include "gregorian-ligature-engraver.hh"
11 #include "font-interface.hh"
12 #include "gregorian-ligature.hh"
13 #include "international.hh"
14 #include "output-def.hh"
15 #include "paper-column.hh"
16 #include "separation-item.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "stream-event.hh"
20 #include "vaticana-ligature.hh"
22 #include "dot-column.hh"
23 #include "rhythmic-head.hh"
25 #include "translator.icc"
28 * This class implements the notation specific aspects of Vaticana
29 * style ligatures for Gregorian chant notation.
33 * TODO: Maybe move handling of dots/mora to
34 * Gregorian_ligature_engraver? It's probably common for all types of
35 * Gregorian chant notation that have dotted notes.
37 * FIXME: The horizontal alignment of the mora column is bad (too far
38 * to the left), if the last dotted note is not the last primitive in
39 * the ligature. Fortunately, in practice this bug should have no
40 * negative impact, since dotted notes appear within a ligature
41 * usually always at the end of the ligature, such that the bug never
42 * should apply for valid ligatures.
44 * TODO: Graduale Triplex, tempus per annum, hebdomada septima,
45 * alleluia (page 280) shows a counter-example for collecting dots
46 * always in a single column behind the ligature. Maybe only the last
47 * two dots in a ligature should be collected and all other dots put
48 * behind or on top of the head?
50 class Vaticana_ligature_engraver : public Gregorian_ligature_engraver
55 need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
56 int context_info, int delta_pitch);
57 bool is_stacked_head (int prefix_set,
59 Real align_heads (vector<Grob_info> primitives,
62 void check_for_prefix_loss (Item *primitive);
63 void check_for_ambiguous_dot_pitch (Grob_info primitive);
64 void add_mora_column (Paper_column *column);
65 vector<Grob_info> augmented_primitives_;
68 TRANSLATOR_DECLARATIONS (Vaticana_ligature_engraver);
71 virtual Spanner *create_ligature_spanner ();
72 virtual void transform_heads (Spanner *ligature,
73 vector<Grob_info> primitives);
74 DECLARE_TRANSLATOR_LISTENER (pes_or_flexa);
75 DECLARE_TRANSLATOR_LISTENER (ligature);
78 IMPLEMENT_TRANSLATOR_LISTENER (Vaticana_ligature_engraver, pes_or_flexa);
80 Vaticana_ligature_engraver::listen_pes_or_flexa (Stream_event *ev)
82 Gregorian_ligature_engraver::listen_pes_or_flexa (ev);
85 IMPLEMENT_TRANSLATOR_LISTENER (Vaticana_ligature_engraver, ligature);
87 Vaticana_ligature_engraver::listen_ligature (Stream_event *ev)
89 Ligature_engraver::listen_ligature (ev);
92 Vaticana_ligature_engraver::Vaticana_ligature_engraver ()
94 brew_ligature_primitive_proc =
95 Vaticana_ligature::brew_ligature_primitive_proc;
96 augmented_primitives_.clear ();
100 Vaticana_ligature_engraver::create_ligature_spanner ()
102 return make_spanner ("VaticanaLigature", SCM_EOL);
106 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
111 // upper head of pes is stacked upon lower head of pes ...
112 is_stacked_b = context_info & PES_UPPER;
114 // ... unless this note starts a flexa
115 if (context_info & FLEXA_LEFT)
116 is_stacked_b = false;
118 // ... or another pes
119 if (context_info & PES_LOWER)
120 is_stacked_b = false;
122 // ... or the previous note is a semivocalis or inclinatum
123 if (context_info & AFTER_DEMINUTUM)
124 is_stacked_b = false;
126 // auctum head is never stacked upon preceding note
127 if (prefix_set & AUCTUM)
128 is_stacked_b = false;
130 // virga is never stacked upon preceding note
131 if (prefix_set & VIRGA)
132 is_stacked_b = false;
134 // oriscus is never stacked upon preceding note
135 if (prefix_set & ORISCUS)
136 is_stacked_b = false;
138 if ((prefix_set & DEMINUTUM)
139 && ! (prefix_set & INCLINATUM)
140 && (context_info & FLEXA_RIGHT))
141 is_stacked_b = true; // semivocalis head of deminutus form
147 * When aligning the heads, sometimes extra space is needed, e.g. to
148 * avoid clashing with the appendix of an adjacent notehead or with an
149 * adjacent notehead itself if it has the same pitch. Extra space is
150 * added at most once between to heads.
153 Vaticana_ligature_engraver::need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
154 int context_info, int delta_pitch)
156 if (prev_prefix_set & VIRGA)
158 * After a virga, make a an additional small space such that the
159 * appendix on the right side of the head does not touch the
164 if ((prefix_set & INCLINATUM)
165 && ! (prev_prefix_set & INCLINATUM))
167 * Always start a series of inclinatum heads with an extra space.
171 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
173 * Before a flexa (but not within a torculus), make a an
174 * additional small space such that the appendix on the left side
175 * of the flexa does not touch the this head.
179 if (delta_pitch == 0)
181 * If there are two adjacent noteheads with the same pitch, add
182 * additional small space between them, such that they do not
191 Vaticana_ligature_engraver::align_heads (vector<Grob_info> primitives,
195 if (!primitives.size ())
197 programming_error ("Vaticana_ligature: "
198 "empty ligature [ignored]");
203 * The paper column where we put the whole ligature into.
206 = dynamic_cast<Item *> (primitives[0].grob ())->get_column ();
209 = thickness * column->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
212 * Amount of extra space two put between some particular
213 * configurations of adjacent heads.
215 * TODO: make this a property of primtive grobs.
217 Real extra_space = 4.0 * join_thickness;
220 * Keep track of the total width of the ligature.
222 Real ligature_width = 0.0;
224 Item *prev_primitive = 0;
225 int prev_prefix_set = 0;
226 for (vsize i = 0; i < primitives.size (); i++)
228 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
230 = scm_to_int (primitive->get_property ("prefix-set"));
232 = scm_to_int (primitive->get_property ("context-info"));
235 * Get glyph_name, delta_pitch and context_info for this head.
238 SCM glyph_name_scm = primitive->get_property ("glyph-name");
239 if (glyph_name_scm == SCM_EOL)
241 primitive->programming_error ("Vaticana_ligature:"
242 "undefined glyph-name -> "
246 string glyph_name = ly_scm2string (glyph_name_scm);
249 if (prev_primitive) /* urgh, need prev_primitive only here */
251 SCM delta_pitch_scm = prev_primitive->get_property ("delta-position");
252 if (delta_pitch_scm != SCM_EOL)
253 delta_pitch = scm_to_int (delta_pitch_scm);
256 primitive->programming_error ("Vaticana_ligature:"
257 "delta-position undefined -> "
264 * Now determine width and x-offset of head.
270 if (context_info & STACKED_HEAD)
273 * This head is stacked upon the previous one; hence, it
274 * does not contribute to the total width of the ligature,
275 * and its width is assumed to be 0.0. Moreover, it is
276 * shifted to the left by its width such that the right side
277 * of this and the other head are horizontally aligned.
280 x_offset = join_thickness
281 - Font_interface::get_default_font (primitive)->
282 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
284 else if (glyph_name == "flexa" || glyph_name == "")
287 * This head represents either half of a flexa shape.
288 * Hence, it is assigned half the width of this shape.
290 head_width = 0.5 * flexa_width;
296 * This is a regular head, placed right to the previous one.
297 * Retrieve its width from corresponding font.
300 = Font_interface::get_default_font (primitive)->
301 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
306 * Save the head's final x-offset.
308 primitive->set_property ("x-offset",
309 scm_from_double (x_offset));
312 * If the head is the 2nd head of a pes or flexa (but not a
313 * flexa shape), mark this head to be joined with the left-side
314 * neighbour head (i.e. the previous head) by a vertical beam.
316 if ((context_info & PES_UPPER)
317 || ((context_info & FLEXA_RIGHT)
318 && ! (context_info & PES_LOWER)))
322 primitive->programming_error ("vaticana ligature: add-join: "
323 "missing previous primitive");
327 prev_primitive->set_property ("add-join",
331 * Create a small overlap of adjacent heads so that the join
332 * can be drawn perfectly between them.
334 ligature_width -= join_thickness;
337 else if (glyph_name == "")
340 * This is the 2nd (virtual) head of flexa shape. Join it
341 * tightly with 1st head, i.e. do *not* add additional
342 * space, such that next head will not be off from the flexa
347 if (need_extra_horizontal_space (prev_prefix_set, prefix_set,
348 context_info, delta_pitch))
349 ligature_width += extra_space;
352 * Horizontally line-up this head to form a ligature.
354 move_related_items_to_column (primitive, column, ligature_width);
355 ligature_width += head_width;
357 prev_primitive = primitive;
358 prev_prefix_set = prefix_set;
362 * Add extra horizontal padding space after ligature, such that
363 * neighbouring ligatures do not touch each other.
365 ligature_width += extra_space;
367 return ligature_width;
371 * Depending on the typographical features of a particular ligature
372 * style, some prefixes may be ignored. In particular, if a curved
373 * flexa shape is produced, any prefixes to either of the two
374 * contributing heads that would select a head other than punctum, is
375 * by definition ignored.
377 * This function prints a warning, if the given primitive is prefixed
378 * such that a head other than punctum would be chosen, if this
379 * primitive were engraved as a stand-alone head.
382 Vaticana_ligature_engraver::check_for_prefix_loss (Item *primitive)
385 = scm_to_int (primitive->get_property ("prefix-set"));
386 if (prefix_set & ~PES_OR_FLEXA)
388 string prefs = Gregorian_ligature::prefixes_to_str (primitive);
389 primitive->warning (_f ("ignored prefix (es) `%s' of this head according "
390 "to restrictions of the selected ligature style",
396 Vaticana_ligature_engraver::add_mora_column (Paper_column *column)
398 if (augmented_primitives_.size () == 0) // no dot for column
400 if (!column) // empty ligature???
402 augmented_primitives_[0].grob ()->
403 programming_error ("no paper column to add dot");
406 Item *dotcol = make_item ("DotColumn", SCM_EOL);
407 dotcol->set_parent (column, X_AXIS);
408 for (vsize i = 0; i < augmented_primitives_.size (); i++)
411 dynamic_cast<Item *> (augmented_primitives_[i].grob ());
412 Item *dot = make_item ("Dots", primitive->self_scm ());
413 dot->set_property ("dot-count", scm_from_int (1));
414 dot->set_parent (primitive, Y_AXIS);
415 primitive->set_object ("dot", dot->self_scm ());
416 Dot_column::add_head (dotcol, primitive);
418 // FIXME: why isn't the dot picked up by Paper_column_engraver?
419 // it is, but in the wrong timestep. UGH
420 Separation_item::add_item (column, dot);
425 * This function prints a warning, if the given primitive has the same
426 * pitch as at least one of the primitives already stored in the
427 * augmented_primitives_ array.
429 * The rationale of this check is, that, if there are two dotted
430 * primitives with the same pitch, then collecting all dots in a dot
431 * column behind the ligature leads to a notational ambiguity of to
432 * which head the corresponding dot refers.
434 * Such a case should be treated as a badly specified ligature. The
435 * user should split the ligature to make the notation of dots
439 Vaticana_ligature_engraver::check_for_ambiguous_dot_pitch (Grob_info primitive)
441 // TODO: Fix performance, which is currently O (n^2) (since this
442 // method is called O (n) times and takes O (n) steps in the for
443 // loop), but could be O (n) (by replacing the for loop by e.g. a
444 // bitmask based O (1) test); where n=<number of primitives in the
445 // ligature> (which is typically small (n<10), though).
446 Stream_event *new_cause = primitive.event_cause ();
447 int new_pitch = unsmob_pitch (new_cause->get_property ("pitch"))->steps ();
448 for (vsize i = 0; i < augmented_primitives_.size (); i++)
450 Stream_event *cause = augmented_primitives_[i].event_cause ();
451 int pitch = unsmob_pitch (cause->get_property ("pitch"))->steps ();
452 if (pitch == new_pitch)
455 warning ("Ambiguous use of dots in ligature: there are "
456 "multiple dotted notes with the same pitch. "
457 "The ligature should be split.");
458 return; // supress multiple identical warnings
464 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
465 vector<Grob_info> primitives)
467 Real flexa_width = robust_scm2double (ligature->get_property ("flexa-width"), 2);
469 Real thickness = robust_scm2double (ligature->get_property ("thickness"), 1);
471 Item *prev_primitive = 0;
472 int prev_prefix_set = 0;
473 int prev_context_info = 0;
474 int prev_delta_pitch = 0;
475 string prev_glyph_name = "";
476 augmented_primitives_.clear ();
477 for (vsize i = 0; i < primitives.size (); i++)
479 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
482 SCM delta_pitch_scm = primitive->get_property ("delta-position");
483 if (delta_pitch_scm != SCM_EOL)
484 delta_pitch = scm_to_int (delta_pitch_scm);
487 primitive->programming_error ("Vaticana_ligature:"
488 "delta-position undefined -> "
493 /* retrieve & complete prefix_set and context_info */
495 = scm_to_int (primitive->get_property ("prefix-set"));
497 = scm_to_int (primitive->get_property ("context-info"));
499 if (Rhythmic_head::dot_count (primitive) > 0)
500 // remove dots from primitive and add remember primitive for
501 // creating a dot column
503 Rhythmic_head::get_dots (primitive)->set_property ("dot-count",
505 // TODO: Maybe completely remove grob "Dots" (dots->suicide
506 // () ?) rather than setting property "dot-count" to 0.
508 check_for_ambiguous_dot_pitch (primitives[i]);
509 augmented_primitives_.push_back (primitives[i]);
511 else if (augmented_primitives_.size () > 0)
513 primitive->warning ("This ligature has a dotted head followed by "
514 "a non-dotted head. The ligature should be "
515 "split after the last dotted head before "
519 if (is_stacked_head (prefix_set, context_info))
521 context_info |= STACKED_HEAD;
522 primitive->set_property ("context-info",
523 scm_from_int (context_info));
527 * Now determine which head to typeset (this is context sensitive
528 * information, since it depends on neighbouring heads; therefore,
529 * this decision must be made here in the engraver rather than in
533 if (prefix_set & VIRGA)
535 glyph_name = "vaticana.punctum";
536 primitive->set_property ("add-stem", ly_bool2scm (true));
538 else if (prefix_set & QUILISMA)
539 glyph_name = "vaticana.quilisma";
540 else if (prefix_set & ORISCUS)
541 glyph_name = "solesmes.oriscus";
542 else if (prefix_set & STROPHA)
543 if (prefix_set & AUCTUM)
544 glyph_name = "solesmes.stropha.aucta";
545 else glyph_name = "solesmes.stropha";
546 else if (prefix_set & INCLINATUM)
547 if (prefix_set & AUCTUM)
548 glyph_name = "solesmes.incl.auctum";
549 else if (prefix_set & DEMINUTUM)
550 glyph_name = "solesmes.incl.parvum";
552 glyph_name = "vaticana.inclinatum";
553 else if (prefix_set & DEMINUTUM)
557 glyph_name = "vaticana.reverse.plica";
559 else if (prev_delta_pitch > 0)
562 if (! (prev_context_info & FLEXA_RIGHT))
563 /* correct head of previous primitive */
564 if (prev_delta_pitch > 1)
565 prev_glyph_name = "vaticana.epiphonus";
567 prev_glyph_name = "vaticana.vepiphonus";
568 if (prev_delta_pitch > 1)
569 glyph_name = "vaticana.plica";
571 glyph_name = "vaticana.vplica";
573 else if (prev_delta_pitch < 0)
576 if (! (prev_context_info & FLEXA_RIGHT))
577 /* correct head of previous primitive */
581 /* cephalicus head with fixed size cauda */
582 prev_glyph_name = "vaticana.inner.cephalicus";
586 /* cephalicus head without cauda */
587 prev_glyph_name = "vaticana.cephalicus";
591 * Flexa has no variable size cauda if its left head is
592 * stacked on the right head. This is true for
593 * cephalicus. Hence, remove the cauda.
595 * Urgh: for the current implementation, this rule only
596 * applies for cephalicus; but it is a fundamental rule.
597 * Therefore, the following line of code should be
598 * placed somewhere else.
600 prev_primitive->set_property ("add-cauda",
601 ly_bool2scm (false));
603 if (prev_delta_pitch < - 1)
604 glyph_name = "vaticana.reverse.plica";
606 glyph_name = "vaticana.reverse.vplica";
608 else // (prev_delta_pitch == 0)
610 primitive->programming_error ("Vaticana_ligature:"
611 "deminutum head must have different "
612 "pitch -> ignoring grob");
614 else if (prefix_set & (CAVUM | LINEA))
615 if ((prefix_set & CAVUM) && (prefix_set & LINEA))
616 glyph_name = "vaticana.linea.punctum.cavum";
617 else if (prefix_set & CAVUM)
618 glyph_name = "vaticana.punctum.cavum";
620 glyph_name = "vaticana.linea.punctum";
621 else if (prefix_set & AUCTUM)
622 if (prefix_set & ASCENDENS)
623 glyph_name = "solesmes.auct.asc";
625 glyph_name = "solesmes.auct.desc";
626 else if ((context_info & STACKED_HEAD)
627 && (context_info & PES_UPPER))
628 if (prev_delta_pitch > 1)
629 glyph_name = "vaticana.upes";
631 glyph_name = "vaticana.vupes";
633 glyph_name = "vaticana.punctum";
636 * This head needs a cauda, if it starts a flexa, is not the upper
637 * head of a pes, and if it is a punctum.
639 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
640 if (glyph_name == "vaticana.punctum")
641 primitive->set_property ("add-cauda", ly_bool2scm (true));
644 * Execptional rule for porrectus:
646 * If the current head is preceded by a \flexa and succeded by a
647 * \pes (e.g. "a \flexa g \pes a"), then join the current head and
648 * the previous head into a single curved flexa shape.
650 if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
652 check_for_prefix_loss (prev_primitive);
653 prev_glyph_name = "flexa";
654 prev_primitive->set_property ("flexa-height",
655 scm_from_int (prev_delta_pitch));
656 prev_primitive->set_property ("flexa-width",
657 scm_from_double (flexa_width));
658 bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
659 prev_primitive->set_property ("add-cauda",
660 ly_bool2scm (add_cauda));
661 check_for_prefix_loss (primitive);
663 primitive->set_property ("flexa-width",
664 scm_from_double (flexa_width));
668 * Exceptional rule for pes:
670 * If this head is stacked on the previous one due to a \pes, then
671 * set the glyph of the previous head to that for this special
672 * case, thereby avoiding potential vertical collision with the
675 if (prefix_set & PES_OR_FLEXA)
677 if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
679 if (prev_glyph_name == "vaticana.punctum")
680 if (prev_delta_pitch > 1)
681 prev_glyph_name = "vaticana.lpes";
683 prev_glyph_name = "vaticana.vlpes";
688 prev_primitive->set_property ("glyph-name",
689 ly_string2scm (prev_glyph_name));
692 * In the backend, flexa shapes and joins need to know about line
693 * thickness. Hence, for simplicity, let's distribute the
694 * ligature grob's value for thickness to each ligature head (even
695 * if not all of them need to know).
697 primitive->set_property ("thickness", scm_from_double (thickness));
699 prev_primitive = primitive;
700 prev_prefix_set = prefix_set;
701 prev_context_info = context_info;
702 prev_delta_pitch = delta_pitch;
703 prev_glyph_name = glyph_name;
706 prev_primitive->set_property ("glyph-name",
707 ly_string2scm (prev_glyph_name));
709 align_heads (primitives, flexa_width, thickness);
711 // append all dots to paper column of ligature's last head
712 add_mora_column (prev_primitive->get_column ());
714 #if 0 // experimental code to collapse spacing after ligature
715 /* TODO: set to max (old/new spacing-increment), since other
716 voices/staves also may want to set this property. */
717 Item *first_primitive = dynamic_cast<Item *> (primitives[0].grob ());
718 Paper_column *paper_column = first_primitive->get_column ();
719 paper_column->warning (_f ("Vaticana_ligature_engraver: "
720 "setting `spacing-increment = %f': ptr =%ul",
721 ligature_width, paper_column));
723 set_property ("forced-spacing", scm_from_double (ligature_width));
727 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, rest);
728 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, note_head);
729 ADD_TRANSLATOR (Vaticana_ligature_engraver,
730 /* doc */ "Handles ligatures by glueing special ligature heads together.",
731 /* create */ "VaticanaLigature DotColumn",