]> git.donarmstrong.com Git - lilypond.git/blob - lily/coherent-ligature-engraver.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / coherent-ligature-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2003--2015 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>).  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
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_parent (X_AXIS);
112       sibling_parent->warning (_f ("Coherent_ligature_engraver: "
113                                    "setting `spacing-increment="
114                                    "0.01': ptr=%ul", parent));
115       sibling_parent->set_property ("forced-spacing",
116                                     scm_from_double (0.01));
117 #endif
118
119       sibling->set_parent (target_column, X_AXIS);
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
136 Coherent_ligature_engraver::collect_accidentals (Spanner *,
137                                                  vector<Grob_info> 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
146 compute_delta_pitches (vector<Grob_info> const &primitives)
147 {
148   int prev_pitch = 0;
149   int delta_pitch = 0;
150   Item *prev_primitive = 0, *primitive = 0;
151   for (vsize i = 0; i < primitives.size (); i++)
152     {
153       primitive = dynamic_cast<Item *> (primitives[i].grob ());
154       Stream_event *cause = primitives[i].event_cause ();
155       int pitch
156         = unsmob<Pitch> (cause->get_property ("pitch"))->steps ();
157       if (prev_primitive)
158         {
159           delta_pitch = pitch - prev_pitch;
160           prev_primitive->set_property ("delta-position",
161                                         scm_from_int (delta_pitch));
162         }
163       prev_pitch = pitch;
164       prev_primitive = primitive;
165     }
166   primitive->set_property ("delta-position", scm_from_int (0));
167 }
168
169 void
170 Coherent_ligature_engraver::typeset_ligature (Spanner *ligature,
171                                               vector<Grob_info> const &primitives)
172 {
173   // compute some commonly needed context info stored as grob
174   // properties
175   compute_delta_pitches (primitives);
176
177   // prepare ligature for typesetting
178   build_ligature (ligature, primitives);
179   collect_accidentals (ligature, primitives);
180 }
181
182 // no ADD_ACKNOWLEDGER / ADD_ACKNOWLEDGER / ADD_TRANSLATOR macro calls
183 // since this class is abstract