]> git.donarmstrong.com Git - lilypond.git/blob - lily/coherent-ligature-engraver.cc
* lily/include/event.hh: remove file.
[lilypond.git] / lily / coherent-ligature-engraver.cc
1 /*
2   coherent-ligature-engraver.cc -- implement Coherent_ligature_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2003--2005 Juergen Reuter <reuter@ipd.uka.de>
7 */
8
9 #include "coherent-ligature-engraver.hh"
10
11 #include "warn.hh"
12 #include "staff-symbol-referencer.hh"
13 #include "spanner.hh"
14 #include "paper-column.hh"
15 #include "pitch.hh"
16
17 /*
18  * This abstract class serves as common superclass for all ligature
19  * engravers thet produce a single connected graphical object of fixed
20  * width, consisting of noteheads and other primitives (see class
21  * Ligature_engraver for more information on the interaction between
22  * this class and its superclass).  In particular, it cares for the
23  * following tasks:
24  *
25  * - provide a function for putting all grobs of the ligature into a
26  * single paper column,
27  *
28  * - delegate actual creation of ligature to concrete subclass,
29  *
30  * - collect all accidentals that occur within the ligature and put
31  * them at the left side of the ligature (TODO; see function
32  * collect_accidentals ()),
33  *
34  * - collapse superflous space after each ligature (TODO).
35  *
36  * Concrete subclasses must implement function build_ligature (Spanner
37  * *, Array<Grob_info>).  This function is responsible for actually
38  * building the ligature by transforming the array of noteheads.
39  *
40  * Currently, there are two subclasses: Gregorian_ligature_engraver
41  * for Gregorian chant notation (also known as plain song or cantus
42  * planus) and Mensural_ligature_engraver for white mensural notation.
43  * Subclasses for other music notation styles such as modal notation
44  * or ars nova notation may eventually be added.
45  */
46
47 /*
48  * TODO: local accidentals: collect accidentals that occur within a
49  * ligature and put them before the ligature.  If an accidental
50  * changes within a ligature, print a warning (user error) and ignore
51  * any further accidental for that pitch within that ligature
52  * (actually, in such a case, the user should split the ligature into
53  * two separate ligatures).  Similarly, any object that, in ordinary
54  * notation, may be put to the left or to the right of a note-head,
55  * should be collected and put before or after the ligature.
56  *
57  * TODO: make spacing more robust: do not screw up spacing if user
58  * erroneously puts rest in ligature.
59  *
60  * TODO: for each ligature, add Rod that represents the total length
61  * of the ligature (to preemptively avoid collision with adjacent
62  * notes); or maybe just additionally create a
63  * mensural/vaticana/whatever-ligature grob (e.g. via
64  * Mensural_ligature::print (SCM)) that just consists of a
65  * bounding box around all primitives of the ligature.
66  *
67  * TODO: Maybe move functions fold_up_primitives () and
68  * join_primitives () from subclasses to here?  N.B. it is not
69  * appropriate to put these into Ligature_engraver, since, for
70  * example, Ligature_bracket_engraver does not share any of this code.
71  */
72
73 /*
74  * TODO: Let superflous space after each ligature collapse.  The
75  * following code should help in doing so (though it does not yet
76  * fully work).  Just put the following code into
77  * Spacing_spanner::do_measure ().  I put it temporarily here as memo
78  * until it really works and I also get Han-Wen's/Jan's permission to
79  * add it to the spacing spanner code.
80  */
81 #if 0 // experimental code to collapse spacing after ligature
82 SCM incr_scm = lc->get_property ("forced-spacing");
83 if (incr_scm != SCM_EOL) /* (Paper_column::is_musical (l)) */
84   {
85     me->warning (_f ("gotcha: ptr=%ul", lc));//debug
86     ly_display_scm (lc->self_scm ());
87     Real distance;
88     if (incr_scm != SCM_EOL)
89       {
90         distance = scm_to_double (incr_scm);
91       }
92     else
93       {
94         me->warning (_ ("distance undefined, assuming 0.1"));
95         distance = 0.1;
96       }
97     me->warning (_f ("distance=%f", distance));//debug
98     Real inverse_strength = 1.0;
99     Spaceable_grob::add_spring (lc, rc, distance, inverse_strength);
100     if (Item *rb = r->find_prebroken_piece (LEFT))
101       Spaceable_grob::add_spring (lc, rb, distance, inverse_strength);
102
103     continue;
104   }
105 #endif
106
107 Coherent_ligature_engraver::Coherent_ligature_engraver ()
108 {
109 }
110
111 /*
112  * TODO: move this function to class Item?
113  */
114 void
115 Coherent_ligature_engraver::get_set_column (Item *item, Paper_column *column)
116 {
117   Item *parent = dynamic_cast<Item *> (item->get_parent (X_AXIS));
118   if (!parent)
119     {
120       programming_error ("failed tweaking paper column in ligature");
121       return;
122     }
123
124   String name = parent->name ();
125   if (!String::compare (name, "PaperColumn"))
126     {
127       // Change column not only for targeted item (NoteColumn), but
128       // also for all associated grobs (NoteSpacing, SeparationItem).
129       Grob *sl = Staff_symbol_referencer::get_staff_symbol (item);
130       for (SCM tail = parent->get_property ("elements");
131            scm_is_pair (tail);
132            tail = scm_cdr (tail))
133         {
134           Item *sibling = unsmob_item (scm_car (tail));
135           if ((sibling)
136               && (Staff_symbol_referencer::get_staff_symbol (sibling) == sl))
137             {
138 #if 0 // experimental code to collapse spacing after ligature
139               Grob *sibling_parent = sibling->get_parent (X_AXIS);
140               sibling_parent->warning (_f ("Coherent_ligature_engraver: "
141                                            "setting `spacing-increment="
142                                            "0.01': ptr=%ul", parent));
143               sibling_parent->set_property ("forced-spacing",
144                                             scm_make_real (0.01));
145 #endif
146               sibling->set_parent (column, X_AXIS);
147             }
148         }
149     }
150   else
151     {
152       get_set_column (parent, column);
153     }
154 }
155
156 /*
157  * TODO: This function should collect all accidentals that occur
158  * within the ligature (by scanning through the primitives array) and
159  * place all of them at the left of the ligature.  If there is an
160  * alteration within the ligature (e.g. an "f" followed by a "fis"
161  * somewhere later in the ligature), issue a warning (and maybe create
162  * an additional natural symbol to explicitly make clear that there is
163  * an "f" first?).  The warning should suggest the user to break the
164  * ligature into two or more smaller ligatures such that no alteration
165  * occurs within the broken ligatures any more.
166  */
167 void
168 Coherent_ligature_engraver::collect_accidentals (Spanner *, Array<Grob_info>)
169 {
170   /* TODO */
171 }
172
173 void
174 compute_delta_pitches (Array<Grob_info> primitives)
175 {
176   int prev_pitch = 0;
177   int delta_pitch = 0;
178   Item *prev_primitive = 0, *primitive = 0;
179   for (int i = 0; i < primitives.size (); i++)
180     {
181       primitive = dynamic_cast<Item *> (primitives[i].grob ());
182       Music *music_cause = primitives[i].music_cause ();
183       int pitch
184         = unsmob_pitch (music_cause->get_property ("pitch"))->steps ();
185       if (prev_primitive)
186         {
187           delta_pitch = pitch - prev_pitch;
188           prev_primitive->set_property ("delta-pitch",
189                                         scm_int2num (delta_pitch));
190         }
191       prev_pitch = pitch;
192       prev_primitive = primitive;
193     }
194   primitive->set_property ("delta-pitch", scm_int2num (0));
195 }
196
197 void
198 Coherent_ligature_engraver::build_ligature (Spanner *, Array<Grob_info>)
199 {
200   programming_error ("Coherent_ligature_engraver::build_ligature (): "
201                      "this is an abstract method that should not be called, "
202                      "but overridden by a subclass");
203 }
204
205 void
206 Coherent_ligature_engraver::typeset_ligature (Spanner *ligature,
207                                               Array<Grob_info> primitives)
208 {
209   // compute some commonly needed context info stored as grob
210   // properties
211   compute_delta_pitches (primitives);
212
213   // prepare ligature for typesetting
214   build_ligature (ligature, primitives);
215   collect_accidentals (ligature, primitives);
216 }
217
218 ADD_TRANSLATOR (Coherent_ligature_engraver,
219                 /* descr */ "This is an abstract class.  Subclasses such as Gregorian_ligature_engraver handle ligatures by glueing special ligature heads together.",
220                 /* creats*/ "",
221                 /* accepts */ "ligature-event",
222                 /* acks  */ "note-head-interface rest-interface",
223                 /* reads */ "",
224                 /* write */ "");