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