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);
87 Vaticana_ligature_engraver::Vaticana_ligature_engraver (Context *c)
88 : Gregorian_ligature_engraver (c)
90 brew_ligature_primitive_proc
91 = Vaticana_ligature::brew_ligature_primitive_proc;
92 augmented_primitives_.clear ();
96 Vaticana_ligature_engraver::create_ligature_spanner ()
98 return make_spanner ("VaticanaLigature", SCM_EOL);
102 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
107 // upper head of pes is stacked upon lower head of pes ...
108 is_stacked = context_info & PES_UPPER;
110 // ... unless this note starts a flexa
111 if (context_info & FLEXA_LEFT)
114 // ... or another pes
115 if (context_info & PES_LOWER)
118 // ... or the previous note is a semivocalis or inclinatum
119 if (context_info & AFTER_DEMINUTUM)
122 // auctum head is never stacked upon preceding note
123 if (prefix_set & AUCTUM)
126 // virga is never stacked upon preceding note
127 if (prefix_set & VIRGA)
130 // oriscus is never stacked upon preceding note
131 if (prefix_set & ORISCUS)
134 if ((prefix_set & DEMINUTUM)
135 && ! (prefix_set & INCLINATUM)
136 && (context_info & FLEXA_RIGHT))
137 is_stacked = true; // semivocalis head of deminutus form
143 * When aligning the heads, sometimes extra space is needed, e.g. to
144 * avoid clashing with the appendix of an adjacent notehead or with an
145 * adjacent notehead itself if it has the same pitch. Extra space is
146 * added at most once between to heads.
149 Vaticana_ligature_engraver::need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
150 int context_info, int delta_pitch)
152 if (prev_prefix_set & VIRGA)
154 * After a virga, make an additional small space such that the
155 * appendix on the right side of the head does not touch the
160 if ((prefix_set & INCLINATUM)
161 && ! (prev_prefix_set & INCLINATUM))
163 * Always start a series of inclinatum heads with an extra space.
167 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
169 * Before a flexa (but not within a torculus), make an
170 * additional small space such that the appendix on the left side
171 * of the flexa does not touch the this head.
175 if (delta_pitch == 0)
177 * If there are two adjacent noteheads with the same pitch, add
178 * additional small space between them, such that they do not
187 Vaticana_ligature_engraver::align_heads (vector<Grob_info> const &primitives,
191 if (!primitives.size ())
193 programming_error ("Vaticana_ligature:"
194 " empty ligature [ignored]");
199 * The paper column where we put the whole ligature into.
202 = dynamic_cast<Item *> (primitives[0].grob ())->get_column ();
205 = thickness * column->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
208 * Amount of extra space two put between some particular
209 * configurations of adjacent heads.
211 * TODO: make this a property of primtive grobs.
213 Real extra_space = 4.0 * join_thickness;
216 * Keep track of the total width of the ligature.
218 Real ligature_width = 0.0;
220 Item *prev_primitive = 0;
221 int prev_prefix_set = 0;
222 for (vsize i = 0; i < primitives.size (); i++)
224 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
226 = scm_to_int (primitive->get_property ("prefix-set"));
228 = scm_to_int (primitive->get_property ("context-info"));
231 * Get glyph_name, delta_pitch and context_info for this head.
234 SCM glyph_name_scm = primitive->get_property ("glyph-name");
235 if (scm_is_null (glyph_name_scm))
237 primitive->programming_error ("Vaticana_ligature:"
238 " undefined glyph-name ->"
242 string glyph_name = ly_scm2string (glyph_name_scm);
245 if (prev_primitive) /* urgh, need prev_primitive only here */
247 SCM delta_pitch_scm = prev_primitive->get_property ("delta-position");
248 if (!scm_is_null (delta_pitch_scm))
249 delta_pitch = scm_to_int (delta_pitch_scm);
252 primitive->programming_error ("Vaticana_ligature:"
253 " delta-position undefined ->"
260 * Now determine width and x-offset of head.
266 if (context_info & STACKED_HEAD)
269 * This head is stacked upon the previous one; hence, it
270 * does not contribute to the total width of the ligature,
271 * and its width is assumed to be 0.0. Moreover, it is
272 * shifted to the left by its width such that the right side
273 * of this and the other head are horizontally aligned.
276 x_offset = join_thickness
277 - Font_interface::get_default_font (primitive)->
278 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
280 else if (glyph_name == "flexa" || glyph_name == "")
283 * This head represents either half of a flexa shape.
284 * Hence, it is assigned half the width of this shape.
286 head_width = 0.5 * flexa_width;
292 * This is a regular head, placed right to the previous one.
293 * Retrieve its width from corresponding font.
296 = Font_interface::get_default_font (primitive)->
297 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
302 * Save the head's final x-offset.
304 primitive->set_property ("x-offset",
305 scm_from_double (x_offset));
308 * If the head is the 2nd head of a pes or flexa (but not a
309 * flexa shape), mark this head to be joined with the left-side
310 * neighbour head (i.e. the previous head) by a vertical beam.
312 if ((context_info & PES_UPPER)
313 || ((context_info & FLEXA_RIGHT)
314 && ! (context_info & PES_LOWER)))
318 primitive->programming_error ("Vaticana ligature: add-join:"
319 " missing previous primitive");
323 prev_primitive->set_property ("add-join",
327 * Create a small overlap of adjacent heads so that the join
328 * can be drawn perfectly between them.
330 ligature_width -= join_thickness;
333 else if (glyph_name == "")
336 * This is the 2nd (virtual) head of flexa shape. Join it
337 * tightly with 1st head, i.e. do *not* add additional
338 * space, such that next head will not be off from the flexa
343 if (need_extra_horizontal_space (prev_prefix_set, prefix_set,
344 context_info, delta_pitch))
345 ligature_width += extra_space;
348 * Horizontally line-up this head to form a ligature.
350 move_related_items_to_column (primitive, column, ligature_width);
351 ligature_width += head_width;
353 prev_primitive = primitive;
354 prev_prefix_set = prefix_set;
358 * Add extra horizontal padding space after ligature, such that
359 * neighbouring ligatures do not touch each other.
361 ligature_width += extra_space;
363 return ligature_width;
367 * Depending on the typographical features of a particular ligature
368 * style, some prefixes may be ignored. In particular, if a curved
369 * flexa shape is produced, any prefixes to either of the two
370 * contributing heads that would select a head other than punctum, is
371 * by definition ignored.
373 * This function prints a warning, if the given primitive is prefixed
374 * such that a head other than punctum would be chosen, if this
375 * primitive were engraved as a stand-alone head.
378 Vaticana_ligature_engraver::check_for_prefix_loss (Item *primitive)
381 = scm_to_int (primitive->get_property ("prefix-set"));
382 if (prefix_set & ~PES_OR_FLEXA)
384 string prefs = Gregorian_ligature::prefixes_to_str (primitive);
385 primitive->warning (_f ("ignored prefix(es) `%s' of this head"
386 " according to restrictions of the selected"
393 Vaticana_ligature_engraver::add_mora_column (Paper_column *column)
395 if (augmented_primitives_.size () == 0) // no dot for column
397 if (!column) // empty ligature???
399 augmented_primitives_[0].grob ()->
400 programming_error ("no paper column to add dot");
403 Item *dotcol = make_item ("DotColumn", SCM_EOL);
404 dotcol->set_parent (column, X_AXIS);
405 for (vsize i = 0; i < augmented_primitives_.size (); i++)
408 = dynamic_cast<Item *> (augmented_primitives_[i].grob ());
409 Item *dot = make_item ("Dots", primitive->self_scm ());
410 dot->set_property ("dot-count", scm_from_int (1));
411 dot->set_parent (primitive, Y_AXIS);
412 primitive->set_object ("dot", dot->self_scm ());
413 Dot_column::add_head (dotcol, primitive);
415 // FIXME: why isn't the dot picked up by Paper_column_engraver?
416 Separation_item::add_item (column, dot);
421 * This function prints a warning, if the given primitive has the same
422 * pitch as at least one of the primitives already stored in the
423 * augmented_primitives_ array.
425 * The rationale of this check is, that, if there are two dotted
426 * primitives with the same pitch, then collecting all dots in a dot
427 * column behind the ligature leads to a notational ambiguity of to
428 * which head the corresponding dot refers.
430 * Such a case should be treated as a badly specified ligature. The
431 * user should split the ligature to make the notation of dots
435 Vaticana_ligature_engraver::check_for_ambiguous_dot_pitch (Grob_info primitive)
437 // TODO: Fix performance, which is currently O (n^2) (since this
438 // method is called O (n) times and takes O (n) steps in the for
439 // loop), but could be O (n) (by replacing the for loop by e.g. a
440 // bitmask based O (1) test); where n=<number of primitives in the
441 // ligature> (which is typically small (n<10), though).
442 Stream_event *new_cause = primitive.event_cause ();
443 int new_pitch = unsmob<Pitch> (new_cause->get_property ("pitch"))->steps ();
444 for (vsize i = 0; i < augmented_primitives_.size (); i++)
446 Stream_event *cause = augmented_primitives_[i].event_cause ();
447 int pitch = unsmob<Pitch> (cause->get_property ("pitch"))->steps ();
448 if (pitch == new_pitch)
451 warning (_ ("Ambiguous use of dots in ligature: there are"
452 " multiple dotted notes with the same pitch."
453 " The ligature should be split."));
454 return; // supress multiple identical warnings
460 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
461 vector<Grob_info> const &primitives)
463 Real flexa_width = robust_scm2double (ligature->get_property ("flexa-width"), 2);
465 Real thickness = robust_scm2double (ligature->get_property ("thickness"), 1);
467 Item *prev_primitive = 0;
468 int prev_prefix_set = 0;
469 int prev_context_info = 0;
470 int prev_delta_pitch = 0;
471 string prev_glyph_name = "";
472 augmented_primitives_.clear ();
473 for (vsize i = 0; i < primitives.size (); i++)
475 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
478 SCM delta_pitch_scm = primitive->get_property ("delta-position");
479 if (!scm_is_null (delta_pitch_scm))
480 delta_pitch = scm_to_int (delta_pitch_scm);
483 primitive->programming_error ("Vaticana_ligature:"
484 " delta-position undefined ->"
489 /* retrieve & complete prefix_set and context_info */
491 = scm_to_int (primitive->get_property ("prefix-set"));
493 = scm_to_int (primitive->get_property ("context-info"));
495 if (Rhythmic_head::dot_count (primitive) > 0)
496 // remove dots from primitive and add remember primitive for
497 // creating a dot column
499 Rhythmic_head::get_dots (primitive)->set_property ("dot-count",
501 // TODO: Maybe completely remove grob "Dots" (dots->suicide
502 // () ?) rather than setting property "dot-count" to 0.
504 check_for_ambiguous_dot_pitch (primitives[i]);
505 augmented_primitives_.push_back (primitives[i]);
507 else if (augmented_primitives_.size () > 0)
509 primitive->warning (_ ("This ligature has a dotted head followed by"
510 " a non-dotted head. The ligature should be"
511 " split after the last dotted head before"
515 if (is_stacked_head (prefix_set, context_info))
517 context_info |= STACKED_HEAD;
518 primitive->set_property ("context-info",
519 scm_from_int (context_info));
523 * Now determine which head to typeset (this is context sensitive
524 * information, since it depends on neighbouring heads; therefore,
525 * this decision must be made here in the engraver rather than in
529 if (prefix_set & VIRGA)
531 glyph_name = "vaticana.punctum";
532 primitive->set_property ("add-stem", ly_bool2scm (true));
534 else if (prefix_set & QUILISMA)
535 glyph_name = "vaticana.quilisma";
536 else if (prefix_set & ORISCUS)
537 glyph_name = "solesmes.oriscus";
538 else if (prefix_set & STROPHA)
539 if (prefix_set & AUCTUM)
540 glyph_name = "solesmes.stropha.aucta";
541 else glyph_name = "solesmes.stropha";
542 else if (prefix_set & INCLINATUM)
543 if (prefix_set & AUCTUM)
544 glyph_name = "solesmes.incl.auctum";
545 else if (prefix_set & DEMINUTUM)
546 glyph_name = "solesmes.incl.parvum";
548 glyph_name = "vaticana.inclinatum";
549 else if (prefix_set & DEMINUTUM)
554 glyph_name = "vaticana.reverse.plica";
556 else if (prev_delta_pitch > 0)
559 if (! (prev_context_info & FLEXA_RIGHT))
561 /* correct head of previous primitive */
562 if (prev_delta_pitch > 1)
563 prev_glyph_name = "vaticana.epiphonus";
565 prev_glyph_name = "vaticana.vepiphonus";
567 if (prev_delta_pitch > 1)
568 glyph_name = "vaticana.plica";
570 glyph_name = "vaticana.vplica";
572 else if (prev_delta_pitch < 0)
575 if (! (prev_context_info & FLEXA_RIGHT))
576 /* correct head of previous primitive */
580 /* cephalicus head with fixed size cauda */
581 prev_glyph_name = "vaticana.inner.cephalicus";
585 /* cephalicus head without cauda */
586 prev_glyph_name = "vaticana.cephalicus";
590 * Flexa has no variable size cauda if its left head is
591 * stacked on the right head. This is true for
592 * cephalicus. Hence, remove the cauda.
594 * Urgh: for the current implementation, this rule only
595 * applies for cephalicus; but it is a fundamental rule.
596 * Therefore, the following line of code should be
597 * placed somewhere else.
599 prev_primitive->set_property ("add-cauda",
600 ly_bool2scm (false));
602 if (prev_delta_pitch < - 1)
603 glyph_name = "vaticana.reverse.plica";
605 glyph_name = "vaticana.reverse.vplica";
607 else // (prev_delta_pitch == 0)
609 primitive->programming_error ("Vaticana_ligature:"
610 " deminutum head must have different"
611 " 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")
681 if (prev_delta_pitch > 1)
682 prev_glyph_name = "vaticana.lpes";
684 prev_glyph_name = "vaticana.vlpes";
690 prev_primitive->set_property ("glyph-name",
691 ly_string2scm (prev_glyph_name));
694 * In the backend, flexa shapes and joins need to know about line
695 * thickness. Hence, for simplicity, let's distribute the
696 * ligature grob's value for thickness to each ligature head (even
697 * if not all of them need to know).
699 primitive->set_property ("thickness", scm_from_double (thickness));
701 prev_primitive = primitive;
702 prev_prefix_set = prefix_set;
703 prev_context_info = context_info;
704 prev_delta_pitch = delta_pitch;
705 prev_glyph_name = glyph_name;
708 prev_primitive->set_property ("glyph-name",
709 ly_string2scm (prev_glyph_name));
711 align_heads (primitives, flexa_width, thickness);
713 // append all dots to paper column of ligature's last head
714 add_mora_column (prev_primitive->get_column ());
716 #if 0 // experimental code to collapse spacing after ligature
717 /* TODO: set to max (old/new spacing-increment), since other
718 voices/staves also may want to set this property. */
719 Item *first_primitive = dynamic_cast<Item *> (primitives[0].grob ());
720 Paper_column *paper_column = first_primitive->get_column ();
721 paper_column->warning (_f ("Vaticana_ligature_engraver:"
722 " setting `spacing-increment = %f': ptr =%ul",
723 ligature_width, paper_column));
725 set_property ("forced-spacing", scm_from_double (ligature_width));
730 Vaticana_ligature_engraver::boot ()
732 ADD_LISTENER (Vaticana_ligature_engraver, pes_or_flexa);
733 ADD_LISTENER (Vaticana_ligature_engraver, ligature);
734 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, rest);
735 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, ligature_head);
738 ADD_TRANSLATOR (Vaticana_ligature_engraver,
740 "Handle ligatures by glueing special ligature heads"