2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2003--2015 Juergen Reuter <reuter@ipd.uka.de>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "gregorian-ligature-engraver.hh"
22 #include "font-interface.hh"
23 #include "gregorian-ligature.hh"
24 #include "international.hh"
25 #include "output-def.hh"
26 #include "paper-column.hh"
27 #include "separation-item.hh"
29 #include "staff-symbol-referencer.hh"
30 #include "stream-event.hh"
31 #include "vaticana-ligature.hh"
33 #include "dot-column.hh"
34 #include "rhythmic-head.hh"
36 #include "translator.icc"
39 * This class implements the notation specific aspects of Vaticana
40 * style ligatures for Gregorian chant notation.
44 * TODO: Maybe move handling of dots/mora to
45 * Gregorian_ligature_engraver? It's probably common for all types of
46 * Gregorian chant notation that have dotted notes.
48 * FIXME: The horizontal alignment of the mora column is bad (too far
49 * to the left), if the last dotted note is not the last primitive in
50 * the ligature. Fortunately, in practice this bug should have no
51 * negative impact, since dotted notes appear within a ligature
52 * usually always at the end of the ligature, such that the bug never
53 * should apply for valid ligatures.
55 * TODO: Graduale Triplex, tempus per annum, hebdomada septima,
56 * alleluia (page 280) shows a counter-example for collecting dots
57 * always in a single column behind the ligature. Maybe only the last
58 * two dots in a ligature should be collected and all other dots put
59 * behind or on top of the head?
61 class Vaticana_ligature_engraver : public Gregorian_ligature_engraver
66 need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
67 int context_info, int delta_pitch);
68 bool is_stacked_head (int prefix_set,
70 Real align_heads (vector<Grob_info> const &primitives,
73 void check_for_prefix_loss (Item *primitive);
74 void check_for_ambiguous_dot_pitch (Grob_info primitive);
75 void add_mora_column (Paper_column *column);
76 vector<Grob_info> augmented_primitives_;
79 TRANSLATOR_DECLARATIONS (Vaticana_ligature_engraver);
80 TRANSLATOR_INHERIT (Gregorian_ligature_engraver)
82 virtual Spanner *create_ligature_spanner ();
83 virtual void transform_heads (Spanner *ligature,
84 vector<Grob_info> const &primitives);
85 void listen_pes_or_flexa (Stream_event *);
86 void listen_ligature (Stream_event *);
90 Vaticana_ligature_engraver::listen_pes_or_flexa (Stream_event *ev)
92 Gregorian_ligature_engraver::listen_pes_or_flexa (ev);
96 Vaticana_ligature_engraver::listen_ligature (Stream_event *ev)
98 Ligature_engraver::listen_ligature (ev);
101 Vaticana_ligature_engraver::Vaticana_ligature_engraver ()
103 brew_ligature_primitive_proc
104 = Vaticana_ligature::brew_ligature_primitive_proc;
105 augmented_primitives_.clear ();
109 Vaticana_ligature_engraver::create_ligature_spanner ()
111 return make_spanner ("VaticanaLigature", SCM_EOL);
115 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
120 // upper head of pes is stacked upon lower head of pes ...
121 is_stacked = context_info & PES_UPPER;
123 // ... unless this note starts a flexa
124 if (context_info & FLEXA_LEFT)
127 // ... or another pes
128 if (context_info & PES_LOWER)
131 // ... or the previous note is a semivocalis or inclinatum
132 if (context_info & AFTER_DEMINUTUM)
135 // auctum head is never stacked upon preceding note
136 if (prefix_set & AUCTUM)
139 // virga is never stacked upon preceding note
140 if (prefix_set & VIRGA)
143 // oriscus is never stacked upon preceding note
144 if (prefix_set & ORISCUS)
147 if ((prefix_set & DEMINUTUM)
148 && ! (prefix_set & INCLINATUM)
149 && (context_info & FLEXA_RIGHT))
150 is_stacked = true; // semivocalis head of deminutus form
156 * When aligning the heads, sometimes extra space is needed, e.g. to
157 * avoid clashing with the appendix of an adjacent notehead or with an
158 * adjacent notehead itself if it has the same pitch. Extra space is
159 * added at most once between to heads.
162 Vaticana_ligature_engraver::need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
163 int context_info, int delta_pitch)
165 if (prev_prefix_set & VIRGA)
167 * After a virga, make an additional small space such that the
168 * appendix on the right side of the head does not touch the
173 if ((prefix_set & INCLINATUM)
174 && ! (prev_prefix_set & INCLINATUM))
176 * Always start a series of inclinatum heads with an extra space.
180 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
182 * Before a flexa (but not within a torculus), make an
183 * additional small space such that the appendix on the left side
184 * of the flexa does not touch the this head.
188 if (delta_pitch == 0)
190 * If there are two adjacent noteheads with the same pitch, add
191 * additional small space between them, such that they do not
200 Vaticana_ligature_engraver::align_heads (vector<Grob_info> const &primitives,
204 if (!primitives.size ())
206 programming_error ("Vaticana_ligature:"
207 " empty ligature [ignored]");
212 * The paper column where we put the whole ligature into.
215 = dynamic_cast<Item *> (primitives[0].grob ())->get_column ();
218 = thickness * column->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
221 * Amount of extra space two put between some particular
222 * configurations of adjacent heads.
224 * TODO: make this a property of primtive grobs.
226 Real extra_space = 4.0 * join_thickness;
229 * Keep track of the total width of the ligature.
231 Real ligature_width = 0.0;
233 Item *prev_primitive = 0;
234 int prev_prefix_set = 0;
235 for (vsize i = 0; i < primitives.size (); i++)
237 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
239 = scm_to_int (primitive->get_property ("prefix-set"));
241 = scm_to_int (primitive->get_property ("context-info"));
244 * Get glyph_name, delta_pitch and context_info for this head.
247 SCM glyph_name_scm = primitive->get_property ("glyph-name");
248 if (scm_is_null (glyph_name_scm))
250 primitive->programming_error ("Vaticana_ligature:"
251 " undefined glyph-name ->"
255 string glyph_name = ly_scm2string (glyph_name_scm);
258 if (prev_primitive) /* urgh, need prev_primitive only here */
260 SCM delta_pitch_scm = prev_primitive->get_property ("delta-position");
261 if (!scm_is_null (delta_pitch_scm))
262 delta_pitch = scm_to_int (delta_pitch_scm);
265 primitive->programming_error ("Vaticana_ligature:"
266 " delta-position undefined ->"
273 * Now determine width and x-offset of head.
279 if (context_info & STACKED_HEAD)
282 * This head is stacked upon the previous one; hence, it
283 * does not contribute to the total width of the ligature,
284 * and its width is assumed to be 0.0. Moreover, it is
285 * shifted to the left by its width such that the right side
286 * of this and the other head are horizontally aligned.
289 x_offset = join_thickness
290 - Font_interface::get_default_font (primitive)->
291 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
293 else if (glyph_name == "flexa" || glyph_name == "")
296 * This head represents either half of a flexa shape.
297 * Hence, it is assigned half the width of this shape.
299 head_width = 0.5 * flexa_width;
305 * This is a regular head, placed right to the previous one.
306 * Retrieve its width from corresponding font.
309 = Font_interface::get_default_font (primitive)->
310 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
315 * Save the head's final x-offset.
317 primitive->set_property ("x-offset",
318 scm_from_double (x_offset));
321 * If the head is the 2nd head of a pes or flexa (but not a
322 * flexa shape), mark this head to be joined with the left-side
323 * neighbour head (i.e. the previous head) by a vertical beam.
325 if ((context_info & PES_UPPER)
326 || ((context_info & FLEXA_RIGHT)
327 && ! (context_info & PES_LOWER)))
331 primitive->programming_error ("Vaticana ligature: add-join:"
332 " missing previous primitive");
336 prev_primitive->set_property ("add-join",
340 * Create a small overlap of adjacent heads so that the join
341 * can be drawn perfectly between them.
343 ligature_width -= join_thickness;
346 else if (glyph_name == "")
349 * This is the 2nd (virtual) head of flexa shape. Join it
350 * tightly with 1st head, i.e. do *not* add additional
351 * space, such that next head will not be off from the flexa
356 if (need_extra_horizontal_space (prev_prefix_set, prefix_set,
357 context_info, delta_pitch))
358 ligature_width += extra_space;
361 * Horizontally line-up this head to form a ligature.
363 move_related_items_to_column (primitive, column, ligature_width);
364 ligature_width += head_width;
366 prev_primitive = primitive;
367 prev_prefix_set = prefix_set;
371 * Add extra horizontal padding space after ligature, such that
372 * neighbouring ligatures do not touch each other.
374 ligature_width += extra_space;
376 return ligature_width;
380 * Depending on the typographical features of a particular ligature
381 * style, some prefixes may be ignored. In particular, if a curved
382 * flexa shape is produced, any prefixes to either of the two
383 * contributing heads that would select a head other than punctum, is
384 * by definition ignored.
386 * This function prints a warning, if the given primitive is prefixed
387 * such that a head other than punctum would be chosen, if this
388 * primitive were engraved as a stand-alone head.
391 Vaticana_ligature_engraver::check_for_prefix_loss (Item *primitive)
394 = scm_to_int (primitive->get_property ("prefix-set"));
395 if (prefix_set & ~PES_OR_FLEXA)
397 string prefs = Gregorian_ligature::prefixes_to_str (primitive);
398 primitive->warning (_f ("ignored prefix(es) `%s' of this head"
399 " according to restrictions of the selected"
406 Vaticana_ligature_engraver::add_mora_column (Paper_column *column)
408 if (augmented_primitives_.size () == 0) // no dot for column
410 if (!column) // empty ligature???
412 augmented_primitives_[0].grob ()->
413 programming_error ("no paper column to add dot");
416 Item *dotcol = make_item ("DotColumn", SCM_EOL);
417 dotcol->set_parent (column, X_AXIS);
418 for (vsize i = 0; i < augmented_primitives_.size (); i++)
421 = dynamic_cast<Item *> (augmented_primitives_[i].grob ());
422 Item *dot = make_item ("Dots", primitive->self_scm ());
423 dot->set_property ("dot-count", scm_from_int (1));
424 dot->set_parent (primitive, Y_AXIS);
425 primitive->set_object ("dot", dot->self_scm ());
426 Dot_column::add_head (dotcol, primitive);
428 // FIXME: why isn't the dot picked up by Paper_column_engraver?
429 Separation_item::add_item (column, dot);
434 * This function prints a warning, if the given primitive has the same
435 * pitch as at least one of the primitives already stored in the
436 * augmented_primitives_ array.
438 * The rationale of this check is, that, if there are two dotted
439 * primitives with the same pitch, then collecting all dots in a dot
440 * column behind the ligature leads to a notational ambiguity of to
441 * which head the corresponding dot refers.
443 * Such a case should be treated as a badly specified ligature. The
444 * user should split the ligature to make the notation of dots
448 Vaticana_ligature_engraver::check_for_ambiguous_dot_pitch (Grob_info primitive)
450 // TODO: Fix performance, which is currently O (n^2) (since this
451 // method is called O (n) times and takes O (n) steps in the for
452 // loop), but could be O (n) (by replacing the for loop by e.g. a
453 // bitmask based O (1) test); where n=<number of primitives in the
454 // ligature> (which is typically small (n<10), though).
455 Stream_event *new_cause = primitive.event_cause ();
456 int new_pitch = unsmob<Pitch> (new_cause->get_property ("pitch"))->steps ();
457 for (vsize i = 0; i < augmented_primitives_.size (); i++)
459 Stream_event *cause = augmented_primitives_[i].event_cause ();
460 int pitch = unsmob<Pitch> (cause->get_property ("pitch"))->steps ();
461 if (pitch == new_pitch)
464 warning (_ ("Ambiguous use of dots in ligature: there are"
465 " multiple dotted notes with the same pitch."
466 " The ligature should be split."));
467 return; // supress multiple identical warnings
473 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
474 vector<Grob_info> const &primitives)
476 Real flexa_width = robust_scm2double (ligature->get_property ("flexa-width"), 2);
478 Real thickness = robust_scm2double (ligature->get_property ("thickness"), 1);
480 Item *prev_primitive = 0;
481 int prev_prefix_set = 0;
482 int prev_context_info = 0;
483 int prev_delta_pitch = 0;
484 string prev_glyph_name = "";
485 augmented_primitives_.clear ();
486 for (vsize i = 0; i < primitives.size (); i++)
488 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
491 SCM delta_pitch_scm = primitive->get_property ("delta-position");
492 if (!scm_is_null (delta_pitch_scm))
493 delta_pitch = scm_to_int (delta_pitch_scm);
496 primitive->programming_error ("Vaticana_ligature:"
497 " delta-position undefined ->"
502 /* retrieve & complete prefix_set and context_info */
504 = scm_to_int (primitive->get_property ("prefix-set"));
506 = scm_to_int (primitive->get_property ("context-info"));
508 if (Rhythmic_head::dot_count (primitive) > 0)
509 // remove dots from primitive and add remember primitive for
510 // creating a dot column
512 Rhythmic_head::get_dots (primitive)->set_property ("dot-count",
514 // TODO: Maybe completely remove grob "Dots" (dots->suicide
515 // () ?) rather than setting property "dot-count" to 0.
517 check_for_ambiguous_dot_pitch (primitives[i]);
518 augmented_primitives_.push_back (primitives[i]);
520 else if (augmented_primitives_.size () > 0)
522 primitive->warning (_ ("This ligature has a dotted head followed by"
523 " a non-dotted head. The ligature should be"
524 " split after the last dotted head before"
528 if (is_stacked_head (prefix_set, context_info))
530 context_info |= STACKED_HEAD;
531 primitive->set_property ("context-info",
532 scm_from_int (context_info));
536 * Now determine which head to typeset (this is context sensitive
537 * information, since it depends on neighbouring heads; therefore,
538 * this decision must be made here in the engraver rather than in
542 if (prefix_set & VIRGA)
544 glyph_name = "vaticana.punctum";
545 primitive->set_property ("add-stem", ly_bool2scm (true));
547 else if (prefix_set & QUILISMA)
548 glyph_name = "vaticana.quilisma";
549 else if (prefix_set & ORISCUS)
550 glyph_name = "solesmes.oriscus";
551 else if (prefix_set & STROPHA)
552 if (prefix_set & AUCTUM)
553 glyph_name = "solesmes.stropha.aucta";
554 else glyph_name = "solesmes.stropha";
555 else if (prefix_set & INCLINATUM)
556 if (prefix_set & AUCTUM)
557 glyph_name = "solesmes.incl.auctum";
558 else if (prefix_set & DEMINUTUM)
559 glyph_name = "solesmes.incl.parvum";
561 glyph_name = "vaticana.inclinatum";
562 else if (prefix_set & DEMINUTUM)
567 glyph_name = "vaticana.reverse.plica";
569 else if (prev_delta_pitch > 0)
572 if (! (prev_context_info & FLEXA_RIGHT))
574 /* correct head of previous primitive */
575 if (prev_delta_pitch > 1)
576 prev_glyph_name = "vaticana.epiphonus";
578 prev_glyph_name = "vaticana.vepiphonus";
580 if (prev_delta_pitch > 1)
581 glyph_name = "vaticana.plica";
583 glyph_name = "vaticana.vplica";
585 else if (prev_delta_pitch < 0)
588 if (! (prev_context_info & FLEXA_RIGHT))
589 /* correct head of previous primitive */
593 /* cephalicus head with fixed size cauda */
594 prev_glyph_name = "vaticana.inner.cephalicus";
598 /* cephalicus head without cauda */
599 prev_glyph_name = "vaticana.cephalicus";
603 * Flexa has no variable size cauda if its left head is
604 * stacked on the right head. This is true for
605 * cephalicus. Hence, remove the cauda.
607 * Urgh: for the current implementation, this rule only
608 * applies for cephalicus; but it is a fundamental rule.
609 * Therefore, the following line of code should be
610 * placed somewhere else.
612 prev_primitive->set_property ("add-cauda",
613 ly_bool2scm (false));
615 if (prev_delta_pitch < - 1)
616 glyph_name = "vaticana.reverse.plica";
618 glyph_name = "vaticana.reverse.vplica";
620 else // (prev_delta_pitch == 0)
622 primitive->programming_error ("Vaticana_ligature:"
623 " deminutum head must have different"
624 " pitch -> ignoring grob");
627 else if (prefix_set & (CAVUM | LINEA))
628 if ((prefix_set & CAVUM) && (prefix_set & LINEA))
629 glyph_name = "vaticana.linea.punctum.cavum";
630 else if (prefix_set & CAVUM)
631 glyph_name = "vaticana.punctum.cavum";
633 glyph_name = "vaticana.linea.punctum";
634 else if (prefix_set & AUCTUM)
635 if (prefix_set & ASCENDENS)
636 glyph_name = "solesmes.auct.asc";
638 glyph_name = "solesmes.auct.desc";
639 else if ((context_info & STACKED_HEAD)
640 && (context_info & PES_UPPER))
641 if (prev_delta_pitch > 1)
642 glyph_name = "vaticana.upes";
644 glyph_name = "vaticana.vupes";
646 glyph_name = "vaticana.punctum";
649 * This head needs a cauda, if it starts a flexa, is not the upper
650 * head of a pes, and if it is a punctum.
652 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
653 if (glyph_name == "vaticana.punctum")
654 primitive->set_property ("add-cauda", ly_bool2scm (true));
657 * Execptional rule for porrectus:
659 * If the current head is preceded by a \flexa and succeded by a
660 * \pes (e.g. "a \flexa g \pes a"), then join the current head and
661 * the previous head into a single curved flexa shape.
663 if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
665 check_for_prefix_loss (prev_primitive);
666 prev_glyph_name = "flexa";
667 prev_primitive->set_property ("flexa-height",
668 scm_from_int (prev_delta_pitch));
669 prev_primitive->set_property ("flexa-width",
670 scm_from_double (flexa_width));
671 bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
672 prev_primitive->set_property ("add-cauda",
673 ly_bool2scm (add_cauda));
674 check_for_prefix_loss (primitive);
676 primitive->set_property ("flexa-width",
677 scm_from_double (flexa_width));
681 * Exceptional rule for pes:
683 * If this head is stacked on the previous one due to a \pes, then
684 * set the glyph of the previous head to that for this special
685 * case, thereby avoiding potential vertical collision with the
688 if (prefix_set & PES_OR_FLEXA)
690 if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
692 if (prev_glyph_name == "vaticana.punctum")
694 if (prev_delta_pitch > 1)
695 prev_glyph_name = "vaticana.lpes";
697 prev_glyph_name = "vaticana.vlpes";
703 prev_primitive->set_property ("glyph-name",
704 ly_string2scm (prev_glyph_name));
707 * In the backend, flexa shapes and joins need to know about line
708 * thickness. Hence, for simplicity, let's distribute the
709 * ligature grob's value for thickness to each ligature head (even
710 * if not all of them need to know).
712 primitive->set_property ("thickness", scm_from_double (thickness));
714 prev_primitive = primitive;
715 prev_prefix_set = prefix_set;
716 prev_context_info = context_info;
717 prev_delta_pitch = delta_pitch;
718 prev_glyph_name = glyph_name;
721 prev_primitive->set_property ("glyph-name",
722 ly_string2scm (prev_glyph_name));
724 align_heads (primitives, flexa_width, thickness);
726 // append all dots to paper column of ligature's last head
727 add_mora_column (prev_primitive->get_column ());
729 #if 0 // experimental code to collapse spacing after ligature
730 /* TODO: set to max (old/new spacing-increment), since other
731 voices/staves also may want to set this property. */
732 Item *first_primitive = dynamic_cast<Item *> (primitives[0].grob ());
733 Paper_column *paper_column = first_primitive->get_column ();
734 paper_column->warning (_f ("Vaticana_ligature_engraver:"
735 " setting `spacing-increment = %f': ptr =%ul",
736 ligature_width, paper_column));
738 set_property ("forced-spacing", scm_from_double (ligature_width));
743 Vaticana_ligature_engraver::boot ()
745 ADD_LISTENER (Vaticana_ligature_engraver, pes_or_flexa);
746 ADD_LISTENER (Vaticana_ligature_engraver, ligature);
747 ADD_ACKNOWLEDGER (Ligature_engraver, rest);
748 ADD_ACKNOWLEDGER (Ligature_engraver, ligature_head);
751 ADD_TRANSLATOR (Vaticana_ligature_engraver,
753 "Handle ligatures by glueing special ligature heads"