]> git.donarmstrong.com Git - lilypond.git/blob - lily/ledger-line-spanner.cc
Add a space-to-barline property to allow looser spacing of clefs.
[lilypond.git] / lily / ledger-line-spanner.cc
1 /*
2   ledger-line-spanner.cc -- implement Ledger_line_spanner
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include <map>
10 using namespace std;
11
12 #include "note-head.hh"
13 #include "staff-symbol-referencer.hh"
14 #include "staff-symbol.hh"
15 #include "lookup.hh"
16 #include "spanner.hh"
17 #include "pointer-group-interface.hh"
18 #include "paper-column.hh"
19
20 struct Ledger_line_spanner
21 {
22   DECLARE_SCHEME_CALLBACK (print, (SCM));
23   DECLARE_SCHEME_CALLBACK (set_spacing_rods, (SCM));
24   static Stencil brew_ledger_lines (Grob *me,
25                                     int pos,
26                                     Interval,
27                                     Real, Real,
28                                     Interval x_extent,
29                                     Real left_shorten);
30
31   DECLARE_GROB_INTERFACE ();
32 };
33
34 Stencil
35 Ledger_line_spanner::brew_ledger_lines (Grob *staff,
36                                         int pos,
37                                         Interval staff_extent,
38                                         Real halfspace,
39                                         Real ledgerlinethickness,
40                                         Interval x_extent,
41                                         Real left_shorten)
42 {
43   int line_count = (staff_extent.contains (pos)
44                     ? 0
45                     : sign (pos) * int (rint (pos -  staff_extent[Direction (sign (pos))])) / 2);
46   Stencil stencil;
47   if (line_count)
48     {
49       Real blotdiameter = ledgerlinethickness;
50       Interval y_extent
51         = Interval (-0.5 * (ledgerlinethickness),
52                     +0.5 * (ledgerlinethickness));
53       Stencil proto_ledger_line
54         = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
55
56       x_extent[LEFT] += left_shorten;
57       Stencil proto_first_line
58         = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
59
60       Direction dir = (Direction)sign (pos);
61       Real offs = (Staff_symbol_referencer::on_line (staff, pos))
62         ? 0.0
63         : -dir * halfspace;
64
65       offs += pos * halfspace;
66       for (int i = 0; i < line_count; i++)
67         {
68           Stencil ledger_line ((i == 0)
69                                ? proto_first_line
70                                : proto_ledger_line);
71           ledger_line.translate_axis (-dir * halfspace * i * 2 + offs, Y_AXIS);
72           stencil.add_stencil (ledger_line);
73         }
74     }
75
76   return stencil;
77 }
78
79 static void
80 set_rods (Drul_array<Interval> const &current_extents,
81           Drul_array<Interval> const &previous_extents,
82           Item *current_column,
83           Item *previous_column,
84           Real min_length_fraction)
85 {
86   Direction d = UP;
87   do
88     {
89       if (!current_extents[d].is_empty ()
90           && !previous_extents[d].is_empty ())
91         {
92           Real total_head_length = previous_extents[d].length ()
93             + current_extents[d].length ();
94
95           Rod rod;
96           rod.distance_ = total_head_length
97             * (3 / 2 * min_length_fraction)
98             /*
99               we go from right to left.
100             */
101             - previous_extents[d][LEFT]
102             + current_extents[d][RIGHT];
103
104           rod.item_drul_[LEFT] = current_column;
105           rod.item_drul_[RIGHT] = previous_column;
106           rod.add_to_cols ();
107         }
108     }
109   while (flip (&d) != DOWN);
110 }
111
112 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, set_spacing_rods, 1);
113 SCM
114 Ledger_line_spanner::set_spacing_rods (SCM smob)
115 {
116   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
117
118   // find size of note heads.
119   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
120   if (!staff)
121     {
122       me->suicide ();
123       return SCM_EOL;
124     }
125
126   Real min_length_fraction
127     = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15);
128
129   Drul_array<Interval> current_extents;
130   Drul_array<Interval> previous_extents;
131   Item *previous_column = 0;
132   Item *current_column = 0;
133
134   Real halfspace = Staff_symbol::staff_space (staff) / 2;
135
136   Interval staff_extent = staff->extent (staff, Y_AXIS);
137   staff_extent *= 1 / halfspace;
138     
139   /*
140     Run through heads using a loop. Since Ledger_line_spanner can
141     contain a lot of noteheads, superlinear performance is too slow.
142   */
143   extract_item_set (me, "note-heads", heads);
144   for (vsize i = heads.size (); i--;)
145     {
146       Item *h = heads[i];
147
148       int pos = Staff_symbol_referencer::get_rounded_position (h);
149       if (staff_extent.contains (pos))
150         continue;
151
152       Item *column = h->get_column ();
153       if (current_column != column)
154         {
155           set_rods (current_extents, previous_extents,
156                     current_column, previous_column,
157                     min_length_fraction);
158
159           previous_column = current_column;
160           current_column = column;
161           previous_extents = current_extents;
162
163           current_extents[DOWN].set_empty ();
164           current_extents[UP].set_empty ();
165         }
166
167       Interval head_extent = h->extent (column, X_AXIS);
168       Direction vdir = Direction (sign (pos));
169       if (!vdir)
170         continue;
171
172       current_extents[vdir].unite (head_extent);
173     }
174
175   if (previous_column && current_column)
176     set_rods (current_extents, previous_extents,
177               current_column, previous_column,
178               min_length_fraction);
179
180   return SCM_UNSPECIFIED;
181 }
182
183 struct Ledger_request
184 {
185   Interval ledger_extent_;
186   Interval head_extent_;
187   int position_;
188   bool excentric_;
189   Ledger_request ()
190   {
191     ledger_extent_.set_empty ();
192     head_extent_.set_empty ();
193     position_ = 0;
194   }
195 };
196
197 typedef map < int, Drul_array<Ledger_request> > Ledger_requests;
198
199 /*
200   TODO: ledger share a lot of info. Lots of room to optimize away
201   common use of objects/variables.
202 */
203 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1);
204 SCM
205 Ledger_line_spanner::print (SCM smob)
206 {
207   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
208
209   extract_grob_set (me, "note-heads", heads);
210
211   if (heads.empty ())
212     return SCM_EOL;
213
214   // find size of note heads.
215   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
216   if (!staff)
217     return SCM_EOL;
218
219   Real halfspace = Staff_symbol::staff_space (staff) / 2;
220
221   Interval staff_extent = staff->extent (staff, Y_AXIS);
222   staff_extent *= 1 / halfspace;
223   
224   Real length_fraction
225     = robust_scm2double (me->get_property ("length-fraction"), 0.25);
226
227   Stencil ledgers;
228   Stencil default_ledger;
229
230   Grob *common[NO_AXES];
231
232   for (int i = X_AXIS; i < NO_AXES; i++)
233     {
234       Axis a = Axis (i);
235       common[a] = common_refpoint_of_array (heads, me, a);
236       for (vsize i = heads.size (); i--;)
237         if (Grob *g = unsmob_grob (me->get_object ("accidental-grob")))
238           common[a] = common[a]->common_refpoint (g, a);
239     }
240
241   Ledger_requests reqs;
242   for (vsize i = heads.size (); i--;)
243     {
244       Item *h = dynamic_cast<Item *> (heads[i]);
245
246       int pos = Staff_symbol_referencer::get_rounded_position (h);
247       if (pos && !staff_extent.contains (pos))
248         {
249           Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
250           Interval ledger_extent = head_extent;
251           ledger_extent.widen (length_fraction * head_extent.length ());
252
253           Direction vdir = Direction (sign (pos));
254           int rank = h->get_column ()->get_rank ();
255
256           reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
257           reqs[rank][vdir].head_extent_.unite (head_extent);
258           reqs[rank][vdir].position_
259             = vdir * max (vdir * reqs[rank][vdir].position_, vdir * pos);
260         }
261     }
262
263   // determine maximum size for non-colliding ledger.
264   Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
265   Ledger_requests::iterator last (reqs.end ());
266   for (Ledger_requests::iterator i (reqs.begin ());
267        i != reqs.end (); last = i++)
268     {
269       if (last == reqs.end ())
270         continue;
271
272       Direction d = DOWN;
273       do
274         {
275           if (!staff_extent.contains (last->second[d].position_)
276               && !staff_extent.contains (i->second[d].position_))
277             {
278               Real center
279                 = (last->second[d].head_extent_[RIGHT]
280                    + i->second[d].head_extent_[LEFT]) / 2;
281
282               Direction which = LEFT;
283               do
284                 {
285                   Ledger_request &lr = ((which == LEFT) ? * last : *i).second[d];
286
287                   // due tilt of quarter note-heads
288                   /* FIXME */
289                   bool both
290                     = (!staff_extent.contains (last->second[d].position_
291                                                - sign (last->second[d].position_))
292                        && !staff_extent.contains (i->second[d].position_
293                                                   - sign (i->second[d].position_)));
294                   Real limit = (center + (both ? which * gap / 2 : 0));
295                   lr.ledger_extent_.at (-which)
296                     = which * max (which * lr.ledger_extent_[-which], which * limit);
297                 }
298               while (flip (&which) != LEFT);
299             }
300         }
301       while (flip (&d) != DOWN);
302     }
303
304   // create ledgers for note heads
305   Real ledgerlinethickness
306     = Staff_symbol::get_ledger_line_thickness (staff);
307   for (vsize i = heads.size (); i--;)
308     {
309       Item *h = dynamic_cast<Item *> (heads[i]);
310
311       int pos = Staff_symbol_referencer::get_rounded_position (h);
312       if (!staff_extent.contains (pos - sign (pos)))
313         {
314           Interval head_size = h->extent (common[X_AXIS], X_AXIS);
315           Interval ledger_size = head_size;
316           ledger_size.widen (ledger_size.length () * length_fraction);
317
318           Interval max_size = reqs[h->get_column ()->get_rank ()]
319             [Direction (sign (pos))].ledger_extent_;
320
321           ledger_size.intersect (max_size);
322           Real left_shorten = 0.0;
323           if (Grob *g = unsmob_grob (h->get_object ("accidental-grob")))
324             {
325               Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
326               Real d
327                 = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
328                                                         head_size[LEFT]),
329                                       0.0);
330
331               left_shorten = max (-ledger_size[LEFT] + d, 0.0);
332
333               /*
334                 TODO: shorten 2 ledger lines for the case natural +
335                 downstem.
336               */
337             }
338
339           ledgers.add_stencil (brew_ledger_lines (staff, pos, staff_extent,
340                                                   halfspace,
341                                                   ledgerlinethickness,
342                                                   ledger_size,
343                                                   left_shorten));
344         }
345     }
346
347   ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
348                           X_AXIS);
349
350   return ledgers.smobbed_copy ();
351 }
352
353 ADD_INTERFACE (Ledger_line_spanner,
354
355                "This spanner draws the ledger lines of a staff.\n"
356                "This is a separate grob because it has to process\n"
357                "all potential collisions between all note heads.",
358
359                /* properties */
360                "gap "   
361                "length-fraction "       
362                "minimum-length-fraction "
363                "note-heads "
364                "thickness "
365                );
366
367 struct Ledgered_interface
368 {
369   DECLARE_GROB_INTERFACE ();
370 };
371
372 ADD_INTERFACE (Ledgered_interface,
373
374                "Objects that need ledger lines, typically "
375                "note heads. See also @ref{ledger-line-spanner-interface}.",
376
377                "no-ledgers");