1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 2003--2021 Juergen Reuter <reuter@ipd.uka.de>
5 
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.
10 
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.
15 
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/>.
18 */
19 
20 #include "coherent-ligature-engraver.hh"
21 
22 #include "warn.hh"
23 #include "paper-column.hh"
24 #include "pitch.hh"
25 #include "pointer-group-interface.hh"
26 #include "spanner.hh"
27 #include "staff-symbol-referencer.hh"
28 #include "stream-event.hh"
29 
30 using std::vector;
31 
32 /*
33  * This abstract class serves as common superclass for all ligature
34  * engravers thet produce a single connected graphical object of fixed
35  * width, consisting of noteheads and other primitives (see class
36  * Ligature_engraver for more information on the interaction between
37  * this class and its superclass).  In particular, it cares for the
38  * following tasks:
39  *
40  * - provide a function for putting all grobs of the ligature into a
41  * single paper column,
42  *
43  * - delegate actual creation of ligature to concrete subclass,
44  *
45  * - except in Kievan notation, collect all accidentals that occur
46  * within the ligature and put them at the left side of the ligature
47  * (TODO; see function collect_accidentals ()),
48  *
49  * - collapse superflous space after each ligature (TODO).
50  *
51  * Concrete subclasses must implement function build_ligature (Spanner *,
52  * vector<Grob_info_t<Item>>).  This function is responsible for actually
53  * building the ligature by transforming the array of noteheads.
54  *
55  * Currently, there are two subclasses: Gregorian_ligature_engraver
56  * for Gregorian chant notation (also known as plain song or cantus
57  * planus) and Mensural_ligature_engraver for white mensural notation.
58  * Subclasses for other music notation styles such as modal notation
59  * or ars nova notation may eventually be added.
60  */
61 
62 /*
63  * TODO: local accidentals: collect accidentals that occur within a
64  * ligature and put them before the ligature.  If an accidental
65  * changes within a ligature, print a warning (user error) and ignore
66  * any further accidental for that pitch within that ligature
67  * (actually, in such a case, the user should split the ligature into
68  * two separate ligatures).  Similarly, any object that, in ordinary
69  * notation, may be put to the left or to the right of a note-head,
70  * should be collected and put before or after the ligature.
71  *
72  * TODO: make spacing more robust: do not screw up spacing if user
73  * erroneously puts rest in ligature.
74  *
75  * TODO: for each ligature, add Rod that represents the total length
76  * of the ligature (to preemptively avoid collision with adjacent
77  * notes); or maybe just additionally create a
78  * mensural/vaticana/whatever-ligature grob (e.g. via
79  * Mensural_ligature::print (SCM)) that just consists of a
80  * bounding box around all primitives of the ligature.
81  *
82  * TODO: Maybe move functions fold_up_primitives () and
83  * join_primitives () from subclasses to here?  N.B. it is not
84  * appropriate to put these into Ligature_engraver, since, for
85  * example, Ligature_bracket_engraver does not share any of this code.
86  */
87 
88 /*
89  * TODO: move this function to class Item?
90  */
91 void
move_related_items_to_column(Item * item,Paper_column * target_column,Real offset)92 Coherent_ligature_engraver::move_related_items_to_column
93 (Item *item, Paper_column *target_column, Real offset)
94 {
95   Paper_column *source_column = item->get_column ();
96   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (item);
97   extract_item_set (source_column, "elements", elements);
98   for (vsize i = elements.size (); i--;)
99     {
100       Item *sibling = elements[i];
101       if (!sibling)
102         // should not occur, but who knows... -jr
103         continue;
104 
105       if (Staff_symbol_referencer::get_staff_symbol (sibling) != staff_symbol)
106         // sibling is from a staff different than that of the item of
107         // interest
108         continue;
109 
110 #if 0 /* experimental code to collapse spacing after ligature */
111       Grob *sibling_parent = sibling->get_x_parent ();
112       sibling_parent->warning (_f ("Coherent_ligature_engraver: "
113                                    "setting `spacing-increment="
114                                    "0.01': ptr=%ul", parent));
115       set_property (sibling_parent, "forced-spacing",
116                     to_scm (0.01));
117 #endif
118 
119       sibling->set_x_parent (target_column);
120       sibling->translate_axis (offset, X_AXIS);
121     }
122 }
123 
124 /*
125  * TODO: This function should collect all accidentals that occur
126  * within the ligature (by scanning through the primitives array) and
127  * place all of them at the left of the ligature.  If there is an
128  * alteration within the ligature (e.g. an "f" followed by a "fis"
129  * somewhere later in the ligature), issue a warning (and maybe create
130  * an additional natural symbol to explicitly make clear that there is
131  * an "f" first?).  The warning should suggest the user to break the
132  * ligature into two or more smaller ligatures such that no alteration
133  * occurs within the broken ligatures any more.
134  */
135 void
collect_accidentals(Spanner *,vector<Item * > const &)136 Coherent_ligature_engraver::collect_accidentals (Spanner *,
137                                                  vector<Item *> const &)
138 {
139   /* TODO */
140   /* NOTE: if implementing such a function, note that in Kievan notation,
141    * the B-flat accidental should not be "collected", but rather prints
142    * immediately before the note head as usual. */
143 }
144 
145 void
calc_delta_pitches(vector<Item * > const & primitives)146 calc_delta_pitches (vector<Item *> const &primitives)
147 {
148   if (primitives.empty ())
149     return;
150 
151   auto prev_pitch = unsmob<Pitch> (get_property (primitives[0]->event_cause (),
152                                                  "pitch"))->steps ();
153   for (vsize i = 1; i < primitives.size (); ++i)
154     {
155       auto *const cause = primitives[i]->event_cause ();
156       auto pitch = unsmob<Pitch> (get_property (cause, "pitch"))->steps ();
157 
158       auto *const prev_item = primitives[i - 1];
159       set_property (prev_item, "delta-position", to_scm (pitch - prev_pitch));
160 
161       prev_pitch = pitch;
162     }
163 
164   set_property (primitives.back (), "delta-position", to_scm (0));
165 }
166 
167 void
typeset_ligature(Spanner * ligature,vector<Item * > const & primitives)168 Coherent_ligature_engraver::typeset_ligature (Spanner *ligature,
169                                               vector<Item *> const &primitives)
170 {
171   // compute some commonly needed context info stored as grob
172   // properties
173   calc_delta_pitches (primitives);
174 
175   // prepare ligature for typesetting
176   build_ligature (ligature, primitives);
177   collect_accidentals (ligature, primitives);
178 }
179 
180 // no ADD_ACKNOWLEDGER / ADD_ACKNOWLEDGER / ADD_TRANSLATOR macro calls
181 // since this class is abstract
182